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