xref: /illumos-gate/usr/src/uts/common/sys/feature_tests.h (revision c5bab7026b8e0ac44b25ee08507ea360f177d844)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2013 Garrett D'Amore <garrett@damore.org>
24  * Copyright 2016 Joyent, Inc.
25  *
26  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
27  * Use is subject to license terms.
28  */
29 
30 #ifndef _SYS_FEATURE_TESTS_H
31 #define	_SYS_FEATURE_TESTS_H
32 
33 #include <sys/ccompile.h>
34 #include <sys/isa_defs.h>
35 
36 #ifdef	__cplusplus
37 extern "C" {
38 #endif
39 
40 /*
41  * Values of _POSIX_C_SOURCE
42  *
43  *		undefined   not a POSIX compilation
44  *		1	    POSIX.1-1990 compilation
45  *		2	    POSIX.2-1992 compilation
46  *		199309L	    POSIX.1b-1993 compilation (Real Time)
47  *		199506L	    POSIX.1c-1995 compilation (POSIX Threads)
48  *		200112L	    POSIX.1-2001 compilation (Austin Group Revision)
49  *		200809L     POSIX.1-2008 compilation
50  */
51 #if defined(_POSIX_SOURCE) && !defined(_POSIX_C_SOURCE)
52 #define	_POSIX_C_SOURCE 1
53 #endif
54 
55 /*
56  * The feature test macros __XOPEN_OR_POSIX, _STRICT_STDC, _STRICT_SYMBOLS,
57  * and _STDC_C99 are Sun implementation specific macros created in order to
58  * compress common standards specified feature test macros for easier reading.
59  * These macros should not be used by the application developer as
60  * unexpected results may occur. Instead, the user should reference
61  * standards(5) for correct usage of the standards feature test macros.
62  *
63  * __XOPEN_OR_POSIX     Used in cases where a symbol is defined by both
64  *                      X/Open or POSIX or in the negative, when neither
65  *                      X/Open or POSIX defines a symbol.
66  *
67  * _STRICT_STDC         __STDC__ is specified by the C Standards and defined
68  *                      by the compiler. For Sun compilers the value of
69  *                      __STDC__ is either 1, 0, or not defined based on the
70  *                      compilation mode (see cc(1)). When the value of
71  *                      __STDC__ is 1 and in the absence of any other feature
72  *                      test macros, the namespace available to the application
73  *                      is limited to only those symbols defined by the C
74  *                      Standard. _STRICT_STDC provides a more readable means
75  *                      of identifying symbols defined by the standard, or in
76  *                      the negative, symbols that are extensions to the C
77  *                      Standard. See additional comments for GNU C differences.
78  *
79  * _STDC_C99            __STDC_VERSION__ is specified by the C standards and
80  *                      defined by the compiler and indicates the version of
81  *                      the C standard. A value of 199901L indicates a
82  *                      compiler that complies with ISO/IEC 9899:1999, other-
83  *                      wise known as the C99 standard.
84  *
85  * _STDC_C11		Like _STDC_C99 except that the value of __STDC_VERSION__
86  *                      is 201112L indicating a compiler that compiles with
87  *                      ISO/IEXC 9899:2011, otherwise known as the C11 standard.
88  *
89  * _STRICT_SYMBOLS	Used in cases where symbol visibility is restricted
90  *                      by the standards, and the user has not explicitly
91  *                      relaxed the strictness via __EXTENSIONS__.
92  */
93 
94 #if defined(_XOPEN_SOURCE) || defined(_POSIX_C_SOURCE)
95 #define	__XOPEN_OR_POSIX
96 #endif
97 
98 /*
99  * ISO/IEC 9899:1990 and it's revisions, ISO/IEC 9899:1999 and ISO/IEC
100  * 99899:2011 specify the following predefined macro name:
101  *
102  * __STDC__	The integer constant 1, intended to indicate a conforming
103  *		implementation.
104  *
105  * Furthermore, a strictly conforming program shall use only those features
106  * of the language and library specified in these standards. A conforming
107  * implementation shall accept any strictly conforming program.
108  *
109  * Based on these requirements, Sun's C compiler defines __STDC__ to 1 for
110  * strictly conforming environments and __STDC__ to 0 for environments that
111  * use ANSI C semantics but allow extensions to the C standard. For non-ANSI
112  * C semantics, Sun's C compiler does not define __STDC__.
113  *
114  * The GNU C project interpretation is that __STDC__ should always be defined
115  * to 1 for compilation modes that accept ANSI C syntax regardless of whether
116  * or not extensions to the C standard are used. Violations of conforming
117  * behavior are conditionally flagged as warnings via the use of the
118  * -pedantic option. In addition to defining __STDC__ to 1, the GNU C
119  * compiler also defines __STRICT_ANSI__ as a means of specifying strictly
120  * conforming environments using the -ansi or -std=<standard> options.
121  *
122  * In the absence of any other compiler options, Sun and GNU set the value
123  * of __STDC__ as follows when using the following options:
124  *
125  *				Value of __STDC__  __STRICT_ANSI__
126  *
127  * cc -Xa (default)			0	      undefined
128  * cc -Xt (transitional)		0             undefined
129  * cc -Xc (strictly conforming)		1	      undefined
130  * cc -Xs (K&R C)		    undefined	      undefined
131  *
132  * gcc (default)			1	      undefined
133  * gcc -ansi, -std={c89, c99,...)  	1              defined
134  * gcc -traditional (K&R)	    undefined	      undefined
135  *
136  * The default compilation modes for Sun C compilers versus GNU C compilers
137  * results in a differing value for __STDC__ which results in a more
138  * restricted namespace when using Sun compilers. To allow both GNU and Sun
139  * interpretations to peacefully co-exist, we use the following Sun
140  * implementation _STRICT_STDC_ macro:
141  */
142 
143 #if (__STDC__ - 0 == 1 && !defined(__GNUC__)) || \
144 	(defined(__GNUC__) && defined(__STRICT_ANSI__))
145 #define	_STRICT_STDC
146 #else
147 #undef	_STRICT_STDC
148 #endif
149 
150 /*
151  * Compiler complies with ISO/IEC 9899:1999 or ISO/IEC 9989:2011
152  */
153 
154 #if __STDC_VERSION__ - 0 >= 201112L
155 #define	_STDC_C11
156 #endif
157 
158 #if __STDC_VERSION__ - 0 >= 199901L
159 #define	_STDC_C99
160 #endif
161 
162 /*
163  * Use strict symbol visibility.
164  */
165 #if (defined(_STRICT_STDC) || defined(__XOPEN_OR_POSIX)) && \
166 	!defined(__EXTENSIONS__)
167 #define	_STRICT_SYMBOLS
168 #endif
169 
170 /*
171  * Large file interfaces:
172  *
173  *	_LARGEFILE_SOURCE
174  *		1		large file-related additions to POSIX
175  *				interfaces requested (fseeko, etc.)
176  *	_LARGEFILE64_SOURCE
177  *		1		transitional large-file-related interfaces
178  *				requested (seek64, stat64, etc.)
179  *
180  * The corresponding announcement macros are respectively:
181  *	_LFS_LARGEFILE
182  *	_LFS64_LARGEFILE
183  * (These are set in <unistd.h>.)
184  *
185  * Requesting _LARGEFILE64_SOURCE implies requesting _LARGEFILE_SOURCE as
186  * well.
187  *
188  * The large file interfaces are made visible regardless of the initial values
189  * of the feature test macros under certain circumstances:
190  *    -	If no explicit standards-conforming environment is requested (neither
191  *	of _POSIX_SOURCE nor _XOPEN_SOURCE is defined and the value of
192  *	__STDC__ does not imply standards conformance).
193  *    -	Extended system interfaces are explicitly requested (__EXTENSIONS__
194  * 	is defined).
195  *    -	Access to in-kernel interfaces is requested (_KERNEL or _KMEMUSER is
196  *	defined).  (Note that this dependency is an artifact of the current
197  *	kernel implementation and may change in future releases.)
198  */
199 #if	(!defined(_STRICT_STDC) && !defined(__XOPEN_OR_POSIX)) || \
200 		defined(_KERNEL) || defined(_KMEMUSER) || \
201 		defined(__EXTENSIONS__)
202 #undef	_LARGEFILE64_SOURCE
203 #define	_LARGEFILE64_SOURCE	1
204 #endif
205 #if	_LARGEFILE64_SOURCE - 0 == 1
206 #undef	_LARGEFILE_SOURCE
207 #define	_LARGEFILE_SOURCE	1
208 #endif
209 
210 /*
211  * Large file compilation environment control:
212  *
213  * The setting of _FILE_OFFSET_BITS controls the size of various file-related
214  * types and governs the mapping between file-related source function symbol
215  * names and the corresponding binary entry points.
216  *
217  * In the 32-bit environment, the default value is 32; if not set, set it to
218  * the default here, to simplify tests in other headers.
219  *
220  * In the 64-bit compilation environment, the only value allowed is 64.
221  */
222 #if defined(_LP64)
223 #ifndef _FILE_OFFSET_BITS
224 #define	_FILE_OFFSET_BITS	64
225 #endif
226 #if	_FILE_OFFSET_BITS - 0 != 64
227 #error	"invalid _FILE_OFFSET_BITS value specified"
228 #endif
229 #else	/* _LP64 */
230 #ifndef	_FILE_OFFSET_BITS
231 #define	_FILE_OFFSET_BITS	32
232 #endif
233 #if	_FILE_OFFSET_BITS - 0 != 32 && _FILE_OFFSET_BITS - 0 != 64
234 #error	"invalid _FILE_OFFSET_BITS value specified"
235 #endif
236 #endif	/* _LP64 */
237 
238 /*
239  * Use of _XOPEN_SOURCE
240  *
241  * The following X/Open specifications are supported:
242  *
243  * X/Open Portability Guide, Issue 3 (XPG3)
244  * X/Open CAE Specification, Issue 4 (XPG4)
245  * X/Open CAE Specification, Issue 4, Version 2 (XPG4v2)
246  * X/Open CAE Specification, Issue 5 (XPG5)
247  * Open Group Technical Standard, Issue 6 (XPG6), also referred to as
248  *    IEEE Std. 1003.1-2001 and ISO/IEC 9945:2002.
249  * Open Group Technical Standard, Issue 7 (XPG7), also referred to as
250  *    IEEE Std. 1003.1-2008 and ISO/IEC 9945:2009.
251  *
252  * XPG4v2 is also referred to as UNIX 95 (SUS or SUSv1).
253  * XPG5 is also referred to as UNIX 98 or the Single Unix Specification,
254  *     Version 2 (SUSv2)
255  * XPG6 is the result of a merge of the X/Open and POSIX specifications
256  *     and as such is also referred to as IEEE Std. 1003.1-2001 in
257  *     addition to UNIX 03 and SUSv3.
258  * XPG7 is also referred to as UNIX 08 and SUSv4.
259  *
260  * When writing a conforming X/Open application, as per the specification
261  * requirements, the appropriate feature test macros must be defined at
262  * compile time. These are as follows. For more info, see standards(5).
263  *
264  * Feature Test Macro				     Specification
265  * ------------------------------------------------  -------------
266  * _XOPEN_SOURCE                                         XPG3
267  * _XOPEN_SOURCE && _XOPEN_VERSION = 4                   XPG4
268  * _XOPEN_SOURCE && _XOPEN_SOURCE_EXTENDED = 1           XPG4v2
269  * _XOPEN_SOURCE = 500                                   XPG5
270  * _XOPEN_SOURCE = 600  (or POSIX_C_SOURCE=200112L)      XPG6
271  * _XOPEN_SOURCE = 700  (or POSIX_C_SOURCE=200809L)      XPG7
272  *
273  * In order to simplify the guards within the headers, the following
274  * implementation private test macros have been created. Applications
275  * must NOT use these private test macros as unexpected results will
276  * occur.
277  *
278  * Note that in general, the use of these private macros is cumulative.
279  * For example, the use of _XPG3 with no other restrictions on the X/Open
280  * namespace will make the symbols visible for XPG3 through XPG6
281  * compilation environments. The use of _XPG4_2 with no other X/Open
282  * namespace restrictions indicates that the symbols were introduced in
283  * XPG4v2 and are therefore visible for XPG4v2 through XPG6 compilation
284  * environments, but not for XPG3 or XPG4 compilation environments.
285  *
286  * _XPG3    X/Open Portability Guide, Issue 3 (XPG3)
287  * _XPG4    X/Open CAE Specification, Issue 4 (XPG4)
288  * _XPG4_2  X/Open CAE Specification, Issue 4, Version 2 (XPG4v2/UNIX 95/SUS)
289  * _XPG5    X/Open CAE Specification, Issue 5 (XPG5/UNIX 98/SUSv2)
290  * _XPG6    Open Group Technical Standard, Issue 6 (XPG6/UNIX 03/SUSv3)
291  * _XPG7    Open Group Technical Standard, Issue 7 (XPG7/UNIX 08/SUSv4)
292  */
293 
294 /* X/Open Portability Guide, Issue 3 */
295 #if defined(_XOPEN_SOURCE) && (_XOPEN_SOURCE - 0 < 500) && \
296 	(_XOPEN_VERSION - 0 < 4) && !defined(_XOPEN_SOURCE_EXTENDED)
297 #define	_XPG3
298 /* X/Open CAE Specification, Issue 4 */
299 #elif	(defined(_XOPEN_SOURCE) && _XOPEN_VERSION - 0 == 4)
300 #define	_XPG4
301 #define	_XPG3
302 /* X/Open CAE Specification, Issue 4, Version 2 */
303 #elif (defined(_XOPEN_SOURCE) && _XOPEN_SOURCE_EXTENDED - 0 == 1)
304 #define	_XPG4_2
305 #define	_XPG4
306 #define	_XPG3
307 /* X/Open CAE Specification, Issue 5 */
308 #elif	(_XOPEN_SOURCE - 0 == 500)
309 #define	_XPG5
310 #define	_XPG4_2
311 #define	_XPG4
312 #define	_XPG3
313 #undef	_POSIX_C_SOURCE
314 #define	_POSIX_C_SOURCE			199506L
315 /* Open Group Technical Standard , Issue 6 */
316 #elif	(_XOPEN_SOURCE - 0 == 600) || (_POSIX_C_SOURCE - 0 == 200112L)
317 #define	_XPG6
318 #define	_XPG5
319 #define	_XPG4_2
320 #define	_XPG4
321 #define	_XPG3
322 #undef	_POSIX_C_SOURCE
323 #define	_POSIX_C_SOURCE			200112L
324 #undef	_XOPEN_SOURCE
325 #define	_XOPEN_SOURCE			600
326 
327 /* Open Group Technical Standard, Issue 7 */
328 #elif	(_XOPEN_SOURCE - 0 == 700) || (_POSIX_C_SOURCE - 0 == 200809L)
329 #define	_XPG7
330 #define	_XPG6
331 #define	_XPG5
332 #define	_XPG4_2
333 #define	_XPG4
334 #define	_XPG3
335 #undef	_POSIX_C_SOURCE
336 #define	_POSIX_C_SOURCE			200809L
337 #undef	_XOPEN_SOURCE
338 #define	_XOPEN_SOURCE			700
339 #endif
340 
341 /*
342  * _XOPEN_VERSION is defined by the X/Open specifications and is not
343  * normally defined by the application, except in the case of an XPG4
344  * application.  On the implementation side, _XOPEN_VERSION defined with
345  * the value of 3 indicates an XPG3 application. _XOPEN_VERSION defined
346  * with the value of 4 indicates an XPG4 or XPG4v2 (UNIX 95) application.
347  * _XOPEN_VERSION  defined with a value of 500 indicates an XPG5 (UNIX 98)
348  * application and with a value of 600 indicates an XPG6 (UNIX 03)
349  * application and with a value of 700 indicates an XPG7 (UNIX 08).
350  * The appropriate version is determined by the use of the
351  * feature test macros described earlier.  The value of _XOPEN_VERSION
352  * defaults to 3 otherwise indicating support for XPG3 applications.
353  */
354 #ifndef _XOPEN_VERSION
355 #if	defined(_XPG7)
356 #define	_XOPEN_VERSION 700
357 #elif	defined(_XPG6)
358 #define	_XOPEN_VERSION 600
359 #elif defined(_XPG5)
360 #define	_XOPEN_VERSION 500
361 #elif	defined(_XPG4_2)
362 #define	_XOPEN_VERSION  4
363 #else
364 #define	_XOPEN_VERSION  3
365 #endif
366 #endif
367 
368 /*
369  * ANSI C and ISO 9899:1990 say the type long long doesn't exist in strictly
370  * conforming environments.  ISO 9899:1999 says it does.
371  *
372  * The presence of _LONGLONG_TYPE says "long long exists" which is therefore
373  * defined in all but strictly conforming environments that disallow it.
374  */
375 #if !defined(_STDC_C99) && defined(_STRICT_STDC) && !defined(__GNUC__)
376 /*
377  * Resist attempts to force the definition of long long in this case.
378  */
379 #if defined(_LONGLONG_TYPE)
380 #error	"No long long in strictly conforming ANSI C & 1990 ISO C environments"
381 #endif
382 #else
383 #if !defined(_LONGLONG_TYPE)
384 #define	_LONGLONG_TYPE
385 #endif
386 #endif
387 
388 /*
389  * It is invalid to compile an XPG3, XPG4, XPG4v2, or XPG5 application
390  * using c99.  The same is true for POSIX.1-1990, POSIX.2-1992, POSIX.1b,
391  * and POSIX.1c applications. Likewise, it is invalid to compile an XPG6
392  * or a POSIX.1-2001 application with anything other than a c99 or later
393  * compiler.  Therefore, we force an error in both cases.
394  */
395 #if defined(_STDC_C99) && (defined(__XOPEN_OR_POSIX) && !defined(_XPG6))
396 #error "Compiler or options invalid for pre-UNIX 03 X/Open applications \
397 	and pre-2001 POSIX applications"
398 #elif !defined(_STDC_C99) && \
399 	(defined(__XOPEN_OR_POSIX) && defined(_XPG6))
400 #error "Compiler or options invalid; UNIX 03 and POSIX.1-2001 applications \
401 	require the use of c99"
402 #endif
403 
404 /*
405  * The following macro defines a value for the ISO C99 restrict
406  * keyword so that _RESTRICT_KYWD resolves to "restrict" if
407  * an ISO C99 compiler is used, "__restrict" for c++ and "" (null string)
408  * if any other compiler is used. This allows for the use of single
409  * prototype declarations regardless of compiler version.
410  */
411 #if (defined(__STDC__) && defined(_STDC_C99))
412 #ifdef __cplusplus
413 #define	_RESTRICT_KYWD	__restrict
414 #else
415 /*
416  * NOTE: The whitespace between the '#' and 'define' is significant.
417  * It foils gcc's fixincludes from defining a redundant 'restrict'.
418  */
419 /* CSTYLED */
420 # define	_RESTRICT_KYWD	restrict
421 #endif
422 #else
423 #define	_RESTRICT_KYWD
424 #endif
425 
426 /*
427  * The following macro defines a value for the ISO C11 _Noreturn
428  * keyword so that _NORETURN_KYWD resolves to "_Noreturn" if
429  * an ISO C11 compiler is used and "" (null string) if any other
430  * compiler is used. This allows for the use of single prototype
431  * declarations regardless of compiler version.
432  */
433 #if (defined(__STDC__) && defined(_STDC_C11)) && !defined(__cplusplus)
434 #define	_NORETURN_KYWD	_Noreturn
435 #else
436 #define	_NORETURN_KYWD
437 #endif
438 
439 
440 /*
441  * The following macro indicates header support for the ANSI C++
442  * standard.  The ISO/IEC designation for this is ISO/IEC FDIS 14882.
443  */
444 #define	_ISO_CPP_14882_1998
445 
446 /*
447  * The following macro indicates header support for the C99 standard,
448  * ISO/IEC 9899:1999, Programming Languages - C.
449  */
450 #define	_ISO_C_9899_1999
451 
452 /*
453  * The following macro indicates header support for the C99 standard,
454  * ISO/IEC 9899:2011, Programming Languages - C.
455  */
456 #define	_ISO_C_9899_2011
457 
458 /*
459  * The following macro indicates header support for DTrace. The value is an
460  * integer that corresponds to the major version number for DTrace.
461  */
462 #define	_DTRACE_VERSION	1
463 
464 #ifdef	__cplusplus
465 }
466 #endif
467 
468 #endif	/* _SYS_FEATURE_TESTS_H */
469