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