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