cc.tcl 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759
  1. # Copyright (c) 2010 WorkWare Systems http://www.workware.net.au/
  2. # All rights reserved
  3. # @synopsis:
  4. #
  5. # The 'cc' module supports checking various 'features' of the C or C++
  6. # compiler/linker environment. Common commands are 'cc-check-includes',
  7. # 'cc-check-types', 'cc-check-functions', 'cc-with' and 'make-config-header'
  8. #
  9. # The following environment variables are used if set:
  10. #
  11. ## CC - C compiler
  12. ## CXX - C++ compiler
  13. ## CPP - C preprocessor
  14. ## CCACHE - Set to "none" to disable automatic use of ccache
  15. ## CPPFLAGS - Additional C preprocessor compiler flags (C and C++), before CFLAGS, CXXFLAGS
  16. ## CFLAGS - Additional C compiler flags
  17. ## CXXFLAGS - Additional C++ compiler flags
  18. ## LDFLAGS - Additional compiler flags during linking
  19. ## LINKFLAGS - ?How is this different from LDFLAGS?
  20. ## LIBS - Additional libraries to use (for all tests)
  21. ## CROSS - Tool prefix for cross compilation
  22. #
  23. # The following variables are defined from the corresponding
  24. # environment variables if set.
  25. #
  26. ## CC_FOR_BUILD
  27. ## LD
  28. use system
  29. options {}
  30. # Checks for the existence of the given function by linking
  31. #
  32. proc cctest_function {function} {
  33. cctest -link 1 -declare "extern void $function\(void);" -code "$function\();"
  34. }
  35. # Checks for the existence of the given type by compiling
  36. proc cctest_type {type} {
  37. cctest -code "$type _x;"
  38. }
  39. # Checks for the existence of the given type/structure member.
  40. # e.g. "struct stat.st_mtime"
  41. proc cctest_member {struct_member} {
  42. # split at the first dot
  43. regexp {^([^.]+)[.](.*)$} $struct_member -> struct member
  44. cctest -code "static $struct _s; return sizeof(_s.$member);"
  45. }
  46. # Checks for the existence of the given define by compiling
  47. #
  48. proc cctest_define {name} {
  49. cctest -code "#ifndef $name\n#error not defined\n#endif"
  50. }
  51. # Checks for the existence of the given name either as
  52. # a macro (#define) or an rvalue (such as an enum)
  53. #
  54. proc cctest_decl {name} {
  55. cctest -code "#ifndef $name\n(void)$name;\n#endif"
  56. }
  57. # @cc-check-sizeof type ...
  58. #
  59. # Checks the size of the given types (between 1 and 32, inclusive).
  60. # Defines a variable with the size determined, or 'unknown' otherwise.
  61. # e.g. for type 'long long', defines 'SIZEOF_LONG_LONG'.
  62. # Returns the size of the last type.
  63. #
  64. proc cc-check-sizeof {args} {
  65. foreach type $args {
  66. msg-checking "Checking for sizeof $type..."
  67. set size unknown
  68. # Try the most common sizes first
  69. foreach i {4 8 1 2 16 32} {
  70. if {[cctest -code "static int _x\[sizeof($type) == $i ? 1 : -1\] = { 1 };"]} {
  71. set size $i
  72. break
  73. }
  74. }
  75. msg-result $size
  76. set define [feature-define-name $type SIZEOF_]
  77. define $define $size
  78. }
  79. # Return the last result
  80. get-define $define
  81. }
  82. # Checks for each feature in $list by using the given script.
  83. #
  84. # When the script is evaluated, $each is set to the feature
  85. # being checked, and $extra is set to any additional cctest args.
  86. #
  87. # Returns 1 if all features were found, or 0 otherwise.
  88. proc cc-check-some-feature {list script} {
  89. set ret 1
  90. foreach each $list {
  91. if {![check-feature $each $script]} {
  92. set ret 0
  93. }
  94. }
  95. return $ret
  96. }
  97. # @cc-check-includes includes ...
  98. #
  99. # Checks that the given include files can be used.
  100. proc cc-check-includes {args} {
  101. cc-check-some-feature $args {
  102. set with {}
  103. if {[dict exists $::autosetup(cc-include-deps) $each]} {
  104. set deps [dict keys [dict get $::autosetup(cc-include-deps) $each]]
  105. msg-quiet cc-check-includes {*}$deps
  106. foreach i $deps {
  107. if {[have-feature $i]} {
  108. lappend with $i
  109. }
  110. }
  111. }
  112. if {[llength $with]} {
  113. cc-with [list -includes $with] {
  114. cctest -includes $each
  115. }
  116. } else {
  117. cctest -includes $each
  118. }
  119. }
  120. }
  121. # @cc-include-needs include required ...
  122. #
  123. # Ensures that when checking for '$include', a check is first
  124. # made for each '$required' file, and if found, it is included with '#include'.
  125. proc cc-include-needs {file args} {
  126. foreach depfile $args {
  127. dict set ::autosetup(cc-include-deps) $file $depfile 1
  128. }
  129. }
  130. # @cc-check-types type ...
  131. #
  132. # Checks that the types exist.
  133. proc cc-check-types {args} {
  134. cc-check-some-feature $args {
  135. cctest_type $each
  136. }
  137. }
  138. # @cc-check-defines define ...
  139. #
  140. # Checks that the given preprocessor symbols are defined.
  141. proc cc-check-defines {args} {
  142. cc-check-some-feature $args {
  143. cctest_define $each
  144. }
  145. }
  146. # @cc-check-decls name ...
  147. #
  148. # Checks that each given name is either a preprocessor symbol or rvalue
  149. # such as an enum. Note that the define used is 'HAVE_DECL_xxx'
  150. # rather than 'HAVE_xxx'.
  151. proc cc-check-decls {args} {
  152. set ret 1
  153. foreach name $args {
  154. msg-checking "Checking for $name..."
  155. set r [cctest_decl $name]
  156. define-feature "decl $name" $r
  157. if {$r} {
  158. msg-result "ok"
  159. } else {
  160. msg-result "not found"
  161. set ret 0
  162. }
  163. }
  164. return $ret
  165. }
  166. # @cc-check-functions function ...
  167. #
  168. # Checks that the given functions exist (can be linked).
  169. proc cc-check-functions {args} {
  170. cc-check-some-feature $args {
  171. cctest_function $each
  172. }
  173. }
  174. # @cc-check-members type.member ...
  175. #
  176. # Checks that the given type/structure members exist.
  177. # A structure member is of the form 'struct stat.st_mtime'.
  178. proc cc-check-members {args} {
  179. cc-check-some-feature $args {
  180. cctest_member $each
  181. }
  182. }
  183. # @cc-check-function-in-lib function libs ?otherlibs?
  184. #
  185. # Checks that the given function can be found in one of the libs.
  186. #
  187. # First checks for no library required, then checks each of the libraries
  188. # in turn.
  189. #
  190. # If the function is found, the feature is defined and 'lib_$function' is defined
  191. # to '-l$lib' where the function was found, or "" if no library required.
  192. # In addition, '-l$lib' is prepended to the 'LIBS' define.
  193. #
  194. # If additional libraries may be needed for linking, they should be specified
  195. # with '$extralibs' as '-lotherlib1 -lotherlib2'.
  196. # These libraries are not automatically added to 'LIBS'.
  197. #
  198. # Returns 1 if found or 0 if not.
  199. #
  200. proc cc-check-function-in-lib {function libs {otherlibs {}}} {
  201. msg-checking "Checking libs for $function..."
  202. set found 0
  203. cc-with [list -libs $otherlibs] {
  204. if {[cctest_function $function]} {
  205. msg-result "none needed"
  206. define lib_$function ""
  207. incr found
  208. } else {
  209. foreach lib $libs {
  210. cc-with [list -libs -l$lib] {
  211. if {[cctest_function $function]} {
  212. msg-result -l$lib
  213. define lib_$function -l$lib
  214. # prepend to LIBS
  215. define LIBS "-l$lib [get-define LIBS]"
  216. incr found
  217. break
  218. }
  219. }
  220. }
  221. }
  222. }
  223. define-feature $function $found
  224. if {!$found} {
  225. msg-result "no"
  226. }
  227. return $found
  228. }
  229. # @cc-check-tools tool ...
  230. #
  231. # Checks for existence of the given compiler tools, taking
  232. # into account any cross compilation prefix.
  233. #
  234. # For example, when checking for 'ar', first 'AR' is checked on the command
  235. # line and then in the environment. If not found, '${host}-ar' or
  236. # simply 'ar' is assumed depending upon whether cross compiling.
  237. # The path is searched for this executable, and if found 'AR' is defined
  238. # to the executable name.
  239. # Note that even when cross compiling, the simple 'ar' is used as a fallback,
  240. # but a warning is generated. This is necessary for some toolchains.
  241. #
  242. # It is an error if the executable is not found.
  243. #
  244. proc cc-check-tools {args} {
  245. foreach tool $args {
  246. set TOOL [string toupper $tool]
  247. set exe [get-env $TOOL [get-define cross]$tool]
  248. if {[find-executable $exe]} {
  249. define $TOOL $exe
  250. continue
  251. }
  252. if {[find-executable $tool]} {
  253. msg-result "Warning: Failed to find $exe, falling back to $tool which may be incorrect"
  254. define $TOOL $tool
  255. continue
  256. }
  257. user-error "Failed to find $exe"
  258. }
  259. }
  260. # @cc-check-progs prog ...
  261. #
  262. # Checks for existence of the given executables on the path.
  263. #
  264. # For example, when checking for 'grep', the path is searched for
  265. # the executable, 'grep', and if found 'GREP' is defined as 'grep'.
  266. #
  267. # If the executable is not found, the variable is defined as 'false'.
  268. # Returns 1 if all programs were found, or 0 otherwise.
  269. #
  270. proc cc-check-progs {args} {
  271. set failed 0
  272. foreach prog $args {
  273. set PROG [string toupper $prog]
  274. msg-checking "Checking for $prog..."
  275. if {![find-executable $prog]} {
  276. msg-result no
  277. define $PROG false
  278. incr failed
  279. } else {
  280. msg-result ok
  281. define $PROG $prog
  282. }
  283. }
  284. expr {!$failed}
  285. }
  286. # @cc-path-progs prog ...
  287. #
  288. # Like cc-check-progs, but sets the define to the full path rather
  289. # than just the program name.
  290. #
  291. proc cc-path-progs {args} {
  292. set failed 0
  293. foreach prog $args {
  294. set PROG [string toupper $prog]
  295. msg-checking "Checking for $prog..."
  296. set path [find-executable-path $prog]
  297. if {$path eq ""} {
  298. msg-result no
  299. define $PROG false
  300. incr failed
  301. } else {
  302. msg-result $path
  303. define $PROG $path
  304. }
  305. }
  306. expr {!$failed}
  307. }
  308. # Adds the given settings to $::autosetup(ccsettings) and
  309. # returns the old settings.
  310. #
  311. proc cc-add-settings {settings} {
  312. if {[llength $settings] % 2} {
  313. autosetup-error "settings list is missing a value: $settings"
  314. }
  315. set prev [cc-get-settings]
  316. # workaround a bug in some versions of jimsh by forcing
  317. # conversion of $prev to a list
  318. llength $prev
  319. array set new $prev
  320. foreach {name value} $settings {
  321. switch -exact -- $name {
  322. -cflags - -includes {
  323. # These are given as lists
  324. lappend new($name) {*}[list-non-empty $value]
  325. }
  326. -declare {
  327. lappend new($name) $value
  328. }
  329. -libs {
  330. # Note that new libraries are added before previous libraries
  331. set new($name) [list {*}[list-non-empty $value] {*}$new($name)]
  332. }
  333. -link - -lang - -nooutput {
  334. set new($name) $value
  335. }
  336. -source - -sourcefile - -code {
  337. # XXX: These probably are only valid directly from cctest
  338. set new($name) $value
  339. }
  340. default {
  341. autosetup-error "unknown cctest setting: $name"
  342. }
  343. }
  344. }
  345. cc-store-settings [array get new]
  346. return $prev
  347. }
  348. proc cc-store-settings {new} {
  349. set ::autosetup(ccsettings) $new
  350. }
  351. proc cc-get-settings {} {
  352. return $::autosetup(ccsettings)
  353. }
  354. # Similar to cc-add-settings, but each given setting
  355. # simply replaces the existing value.
  356. #
  357. # Returns the previous settings
  358. proc cc-update-settings {args} {
  359. set prev [cc-get-settings]
  360. cc-store-settings [dict merge $prev $args]
  361. return $prev
  362. }
  363. # @cc-with settings ?{ script }?
  364. #
  365. # Sets the given 'cctest' settings and then runs the tests in '$script'.
  366. # Note that settings such as '-lang' replace the current setting, while
  367. # those such as '-includes' are appended to the existing setting.
  368. #
  369. # If no script is given, the settings become the default for the remainder
  370. # of the 'auto.def' file.
  371. #
  372. ## cc-with {-lang c++} {
  373. ## # This will check with the C++ compiler
  374. ## cc-check-types bool
  375. ## cc-with {-includes signal.h} {
  376. ## # This will check with the C++ compiler, signal.h and any existing includes.
  377. ## ...
  378. ## }
  379. ## # back to just the C++ compiler
  380. ## }
  381. #
  382. # The '-libs' setting is special in that newer values are added *before* earlier ones.
  383. #
  384. ## cc-with {-libs {-lc -lm}} {
  385. ## cc-with {-libs -ldl} {
  386. ## cctest -libs -lsocket ...
  387. ## # libs will be in this order: -lsocket -ldl -lc -lm
  388. ## }
  389. ## }
  390. #
  391. # If you wish to invoke something like cc-check-flags but not have -cflags updated,
  392. # use the following idiom:
  393. #
  394. ## cc-with {} {
  395. ## cc-check-flags ...
  396. ## }
  397. proc cc-with {settings args} {
  398. if {[llength $args] == 0} {
  399. cc-add-settings $settings
  400. } elseif {[llength $args] > 1} {
  401. autosetup-error "usage: cc-with settings ?script?"
  402. } else {
  403. set save [cc-add-settings $settings]
  404. set rc [catch {uplevel 1 [lindex $args 0]} result info]
  405. cc-store-settings $save
  406. if {$rc != 0} {
  407. return -code [dict get $info -code] $result
  408. }
  409. return $result
  410. }
  411. }
  412. # @cctest ?settings?
  413. #
  414. # Low level C/C++ compiler checker. Compiles and or links a small C program
  415. # according to the arguments and returns 1 if OK, or 0 if not.
  416. #
  417. # Supported settings are:
  418. #
  419. ## -cflags cflags A list of flags to pass to the compiler
  420. ## -includes list A list of includes, e.g. {stdlib.h stdio.h}
  421. ## -declare code Code to declare before main()
  422. ## -link 1 Don't just compile, link too
  423. ## -lang c|c++ Use the C (default) or C++ compiler
  424. ## -libs liblist List of libraries to link, e.g. {-ldl -lm}
  425. ## -code code Code to compile in the body of main()
  426. ## -source code Compile a complete program. Ignore -includes, -declare and -code
  427. ## -sourcefile file Shorthand for -source [readfile [get-define srcdir]/$file]
  428. ## -nooutput 1 Treat any compiler output (e.g. a warning) as an error
  429. #
  430. # Unless '-source' or '-sourcefile' is specified, the C program looks like:
  431. #
  432. ## #include <firstinclude> /* same for remaining includes in the list */
  433. ## declare-code /* any code in -declare, verbatim */
  434. ## int main(void) {
  435. ## code /* any code in -code, verbatim */
  436. ## return 0;
  437. ## }
  438. #
  439. # And the command line looks like:
  440. #
  441. ## CC -cflags CFLAGS CPPFLAGS conftest.c -o conftest.o
  442. ## CXX -cflags CXXFLAGS CPPFLAGS conftest.cpp -o conftest.o
  443. #
  444. # And if linking:
  445. #
  446. ## CC LDFLAGS -cflags CFLAGS conftest.c -o conftest -libs LIBS
  447. ## CXX LDFLAGS -cflags CXXFLAGS conftest.c -o conftest -libs LIBS
  448. #
  449. # Any failures are recorded in 'config.log'
  450. #
  451. proc cctest {args} {
  452. set tmp conftest__
  453. # Easiest way to merge in the settings
  454. cc-with $args {
  455. array set opts [cc-get-settings]
  456. }
  457. if {[info exists opts(-sourcefile)]} {
  458. set opts(-source) [readfile [get-define srcdir]/$opts(-sourcefile) "#error can't find $opts(-sourcefile)"]
  459. }
  460. if {[info exists opts(-source)]} {
  461. set lines $opts(-source)
  462. } else {
  463. foreach i $opts(-includes) {
  464. if {$opts(-code) ne "" && ![feature-checked $i]} {
  465. # Compiling real code with an unchecked header file
  466. # Quickly (and silently) check for it now
  467. # Remove all -includes from settings before checking
  468. set saveopts [cc-update-settings -includes {}]
  469. msg-quiet cc-check-includes $i
  470. cc-store-settings $saveopts
  471. }
  472. if {$opts(-code) eq "" || [have-feature $i]} {
  473. lappend source "#include <$i>"
  474. }
  475. }
  476. lappend source {*}$opts(-declare)
  477. lappend source "int main(void) {"
  478. lappend source $opts(-code)
  479. lappend source "return 0;"
  480. lappend source "}"
  481. set lines [join $source \n]
  482. }
  483. # Build the command line
  484. set cmdline {}
  485. lappend cmdline {*}[get-define CCACHE]
  486. switch -exact -- $opts(-lang) {
  487. c++ {
  488. set src conftest__.cpp
  489. lappend cmdline {*}[get-define CXX]
  490. set cflags [get-define CXXFLAGS]
  491. }
  492. c {
  493. set src conftest__.c
  494. lappend cmdline {*}[get-define CC]
  495. set cflags [get-define CFLAGS]
  496. }
  497. default {
  498. autosetup-error "cctest called with unknown language: $opts(-lang)"
  499. }
  500. }
  501. if {$opts(-link)} {
  502. lappend cmdline {*}[get-define LDFLAGS]
  503. } else {
  504. lappend cflags {*}[get-define CPPFLAGS]
  505. set tmp conftest__.o
  506. lappend cmdline -c
  507. }
  508. lappend cmdline {*}$opts(-cflags) {*}[get-define cc-default-debug ""] {*}$cflags
  509. lappend cmdline $src -o $tmp
  510. if {$opts(-link)} {
  511. lappend cmdline {*}$opts(-libs) {*}[get-define LIBS]
  512. }
  513. # At this point we have the complete command line and the
  514. # complete source to be compiled. Get the result from cache if
  515. # we can
  516. if {[info exists ::cc_cache($cmdline,$lines)]} {
  517. msg-checking "(cached) "
  518. set ok $::cc_cache($cmdline,$lines)
  519. if {$::autosetup(debug)} {
  520. configlog "From cache (ok=$ok): [join $cmdline]"
  521. configlog "============"
  522. configlog $lines
  523. configlog "============"
  524. }
  525. return $ok
  526. }
  527. writefile $src $lines\n
  528. set ok 1
  529. set err [catch {exec-with-stderr {*}$cmdline} result errinfo]
  530. if {$err || ($opts(-nooutput) && [string length $result])} {
  531. configlog "Failed: [join $cmdline]"
  532. configlog $result
  533. configlog "============"
  534. configlog "The failed code was:"
  535. configlog $lines
  536. configlog "============"
  537. set ok 0
  538. } elseif {$::autosetup(debug)} {
  539. configlog "Compiled OK: [join $cmdline]"
  540. configlog "============"
  541. configlog $lines
  542. configlog "============"
  543. }
  544. file delete $src
  545. file delete $tmp
  546. # cache it
  547. set ::cc_cache($cmdline,$lines) $ok
  548. return $ok
  549. }
  550. # @make-autoconf-h outfile ?auto-patterns=HAVE_*? ?bare-patterns=SIZEOF_*?
  551. #
  552. # Deprecated - see 'make-config-header'
  553. proc make-autoconf-h {file {autopatterns {HAVE_*}} {barepatterns {SIZEOF_* HAVE_DECL_*}}} {
  554. user-notice "*** make-autoconf-h is deprecated -- use make-config-header instead"
  555. make-config-header $file -auto $autopatterns -bare $barepatterns
  556. }
  557. # @make-config-header outfile ?-auto patternlist? ?-bare patternlist? ?-none patternlist? ?-str patternlist? ...
  558. #
  559. # Examines all defined variables which match the given patterns
  560. # and writes an include file, '$file', which defines each of these.
  561. # Variables which match '-auto' are output as follows:
  562. # - defines which have the value '0' are ignored.
  563. # - defines which have integer values are defined as the integer value.
  564. # - any other value is defined as a string, e.g. '"value"'
  565. # Variables which match '-bare' are defined as-is.
  566. # Variables which match '-str' are defined as a string, e.g. '"value"'
  567. # Variables which match '-none' are omitted.
  568. #
  569. # Note that order is important. The first pattern that matches is selected.
  570. # Default behaviour is:
  571. #
  572. ## -bare {SIZEOF_* HAVE_DECL_*} -auto HAVE_* -none *
  573. #
  574. # If the file would be unchanged, it is not written.
  575. proc make-config-header {file args} {
  576. set guard _[string toupper [regsub -all {[^a-zA-Z0-9]} [file tail $file] _]]
  577. file mkdir [file dirname $file]
  578. set lines {}
  579. lappend lines "#ifndef $guard"
  580. lappend lines "#define $guard"
  581. # Add some defaults
  582. lappend args -bare {SIZEOF_* HAVE_DECL_*} -auto HAVE_*
  583. foreach n [lsort [dict keys [all-defines]]] {
  584. set value [get-define $n]
  585. set type [calc-define-output-type $n $args]
  586. switch -exact -- $type {
  587. -bare {
  588. # Just output the value unchanged
  589. }
  590. -none {
  591. continue
  592. }
  593. -str {
  594. set value \"[string map [list \\ \\\\ \" \\\"] $value]\"
  595. }
  596. -auto {
  597. # Automatically determine the type
  598. if {$value eq "0"} {
  599. lappend lines "/* #undef $n */"
  600. continue
  601. }
  602. if {![string is integer -strict $value]} {
  603. set value \"[string map [list \\ \\\\ \" \\\"] $value]\"
  604. }
  605. }
  606. "" {
  607. continue
  608. }
  609. default {
  610. autosetup-error "Unknown type in make-config-header: $type"
  611. }
  612. }
  613. lappend lines "#define $n $value"
  614. }
  615. lappend lines "#endif"
  616. set buf [join $lines \n]
  617. write-if-changed $file $buf {
  618. msg-result "Created $file"
  619. }
  620. }
  621. proc calc-define-output-type {name spec} {
  622. foreach {type patterns} $spec {
  623. foreach pattern $patterns {
  624. if {[string match $pattern $name]} {
  625. return $type
  626. }
  627. }
  628. }
  629. return ""
  630. }
  631. proc cc-init {} {
  632. global autosetup
  633. # Initialise some values from the environment or commandline or default settings
  634. foreach i {LDFLAGS LIBS CPPFLAGS LINKFLAGS CFLAGS} {
  635. lassign $i var default
  636. define $var [get-env $var $default]
  637. }
  638. if {[env-is-set CC]} {
  639. # Set by the user, so don't try anything else
  640. set try [list [get-env CC ""]]
  641. } else {
  642. # Try some reasonable options
  643. set try [list [get-define cross]cc [get-define cross]gcc]
  644. }
  645. define CC [find-an-executable {*}$try]
  646. if {[get-define CC] eq ""} {
  647. user-error "Could not find a C compiler. Tried: [join $try ", "]"
  648. }
  649. define CPP [get-env CPP "[get-define CC] -E"]
  650. # XXX: Could avoid looking for a C++ compiler until requested
  651. # If CXX isn't found, it is set to the empty string.
  652. if {[env-is-set CXX]} {
  653. define CXX [find-an-executable -required [get-env CXX ""]]
  654. } else {
  655. define CXX [find-an-executable [get-define cross]c++ [get-define cross]g++]
  656. }
  657. # CXXFLAGS default to CFLAGS if not specified
  658. define CXXFLAGS [get-env CXXFLAGS [get-define CFLAGS]]
  659. # May need a CC_FOR_BUILD, so look for one
  660. define CC_FOR_BUILD [find-an-executable [get-env CC_FOR_BUILD ""] cc gcc false]
  661. # These start empty and never come from the user or environment
  662. define AS_CFLAGS ""
  663. define AS_CPPFLAGS ""
  664. define AS_CXXFLAGS ""
  665. define CCACHE [find-an-executable [get-env CCACHE ccache]]
  666. # If any of these are set in the environment, propagate them to the AUTOREMAKE commandline
  667. foreach i {CC CXX CCACHE CPP CFLAGS CXXFLAGS CXXFLAGS LDFLAGS LIBS CROSS CPPFLAGS LINKFLAGS CC_FOR_BUILD LD} {
  668. if {[env-is-set $i]} {
  669. # Note: If the variable is set on the command line, get-env will return that value
  670. # so the command line will continue to override the environment
  671. define-append-argv AUTOREMAKE $i=[get-env $i ""]
  672. }
  673. }
  674. # Initial cctest settings
  675. cc-store-settings {-cflags {} -includes {} -declare {} -link 0 -lang c -libs {} -code {} -nooutput 0}
  676. set autosetup(cc-include-deps) {}
  677. msg-result "C compiler...[get-define CCACHE] [get-define CC] [get-define CFLAGS] [get-define CPPFLAGS]"
  678. if {[get-define CXX] ne "false"} {
  679. msg-result "C++ compiler...[get-define CCACHE] [get-define CXX] [get-define CXXFLAGS] [get-define CPPFLAGS]"
  680. }
  681. msg-result "Build C compiler...[get-define CC_FOR_BUILD]"
  682. # On Darwin, we prefer to use -g0 to avoid creating .dSYM directories
  683. # but some compilers may not support it, so test here.
  684. switch -glob -- [get-define host] {
  685. *-*-darwin* {
  686. if {[cctest -cflags {-g0}]} {
  687. define cc-default-debug -g0
  688. }
  689. }
  690. }
  691. if {![cc-check-includes stdlib.h]} {
  692. user-error "Compiler does not work. See config.log"
  693. }
  694. }
  695. cc-init