expr: prevent stack overflow
[jimtcl.git] / autosetup / cc-lib.tcl
blobc4b45bcae6b04c1f2edcee97b890d43ec9bfbccc
1 # Copyright (c) 2011 WorkWare Systems http://www.workware.net.au/
2 # All rights reserved
4 # @synopsis:
6 # Provides a library of common tests on top of the 'cc' module.
8 use cc
10 module-options {}
12 # @cc-check-lfs
14 # The equivalent of the 'AC_SYS_LARGEFILE' macro.
16 # defines 'HAVE_LFS' if LFS is available,
17 # and defines '_FILE_OFFSET_BITS=64' if necessary
19 # Returns 1 if 'LFS' is available or 0 otherwise
21 proc cc-check-lfs {} {
22 cc-check-includes sys/types.h
23 msg-checking "Checking if -D_FILE_OFFSET_BITS=64 is needed..."
24 set lfs 1
25 if {[msg-quiet cc-with {-includes sys/types.h} {cc-check-sizeof off_t}] == 8} {
26 msg-result no
27 } elseif {[msg-quiet cc-with {-includes sys/types.h -cflags -D_FILE_OFFSET_BITS=64} {cc-check-sizeof off_t}] == 8} {
28 define _FILE_OFFSET_BITS 64
29 msg-result yes
30 } else {
31 set lfs 0
32 msg-result none
34 define-feature lfs $lfs
35 return $lfs
38 # @cc-check-endian
40 # The equivalent of the 'AC_C_BIGENDIAN' macro.
42 # defines 'HAVE_BIG_ENDIAN' if endian is known to be big,
43 # or 'HAVE_LITTLE_ENDIAN' if endian is known to be little.
45 # Returns 1 if determined, or 0 if not.
47 proc cc-check-endian {} {
48 cc-check-includes sys/types.h sys/param.h
49 set rc 0
50 msg-checking "Checking endian..."
51 cc-with {-includes {sys/types.h sys/param.h}} {
52 if {[cctest -code {
53 #if !defined(BIG_ENDIAN) || !defined(BYTE_ORDER)
54 #error unknown
55 #elif BYTE_ORDER != BIG_ENDIAN
56 #error little
57 #endif
58 }]} {
59 define-feature big-endian
60 msg-result "big"
61 set rc 1
62 } elseif {[cctest -code {
63 #if !defined(LITTLE_ENDIAN) || !defined(BYTE_ORDER)
64 #error unknown
65 #elif BYTE_ORDER != LITTLE_ENDIAN
66 #error big
67 #endif
68 }]} {
69 define-feature little-endian
70 msg-result "little"
71 set rc 1
72 } else {
73 msg-result "unknown"
76 return $rc
79 # @cc-check-flags flag ?...?
81 # Checks whether the given C/C++ compiler flags can be used. Defines feature
82 # names prefixed with 'HAVE_CFLAG' and 'HAVE_CXXFLAG' respectively, and
83 # appends working flags to '-cflags' and 'CFLAGS' or 'CXXFLAGS'.
84 proc cc-check-flags {args} {
85 set result 1
86 array set opts [cc-get-settings]
87 switch -exact -- $opts(-lang) {
88 c++ {
89 set lang C++
90 set prefix CXXFLAG
92 c {
93 set lang C
94 set prefix CFLAG
96 default {
97 autosetup-error "cc-check-flags failed with unknown language: $opts(-lang)"
100 foreach flag $args {
101 msg-checking "Checking whether the $lang compiler accepts $flag..."
102 if {[cctest -cflags $flag]} {
103 msg-result yes
104 define-feature $prefix$flag
105 cc-with [list -cflags [list $flag]]
106 define-append ${prefix}S $flag
107 } else {
108 msg-result no
109 set result 0
112 return $result
115 # @cc-check-standards ver ?...?
117 # Checks whether the C/C++ compiler accepts one of the specified '-std=$ver'
118 # options, and appends the first working one to '-cflags' and 'CFLAGS' or
119 # 'CXXFLAGS'.
120 proc cc-check-standards {args} {
121 array set opts [cc-get-settings]
122 foreach std $args {
123 if {[cc-check-flags -std=$std]} {
124 return $std
127 return ""
130 # Checks whether $keyword is usable as alignof
131 proc cctest_alignof {keyword} {
132 msg-checking "Checking for $keyword..."
133 if {[cctest -code "int x = ${keyword}(char), y = ${keyword}('x');"]} then {
134 msg-result ok
135 define-feature $keyword
136 } else {
137 msg-result "not found"
141 # @cc-check-c11
143 # Checks for several C11/C++11 extensions and their alternatives. Currently
144 # checks for '_Static_assert', '_Alignof', '__alignof__', '__alignof'.
145 proc cc-check-c11 {} {
146 msg-checking "Checking for _Static_assert..."
147 if {[cctest -code {
148 _Static_assert(1, "static assertions are available");
149 }]} then {
150 msg-result ok
151 define-feature _Static_assert
152 } else {
153 msg-result "not found"
156 cctest_alignof _Alignof
157 cctest_alignof __alignof__
158 cctest_alignof __alignof
161 # @cc-check-alloca
163 # The equivalent of the 'AC_FUNC_ALLOCA' macro.
165 # Checks for the existence of 'alloca'
166 # defines 'HAVE_ALLOCA' and returns 1 if it exists.
167 proc cc-check-alloca {} {
168 cc-check-some-feature alloca {
169 cctest -includes alloca.h -code { alloca (2 * sizeof (int)); }
173 # @cc-signal-return-type
175 # The equivalent of the 'AC_TYPE_SIGNAL' macro.
177 # defines 'RETSIGTYPE' to 'int' or 'void'.
178 proc cc-signal-return-type {} {
179 msg-checking "Checking return type of signal handlers..."
180 cc-with {-includes {sys/types.h signal.h}} {
181 if {[cctest -code {return *(signal (0, 0)) (0) == 1;}]} {
182 set type int
183 } else {
184 set type void
186 define RETSIGTYPE $type
187 msg-result $type