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