1 /*
2  *  Written by Joel Sherrill <joel@OARcorp.com>.
3  *
4  *  COPYRIGHT (c) 1989-2014.
5  *
6  *  On-Line Applications Research Corporation (OAR).
7  *
8  *  Permission to use, copy, modify, and distribute this software for any
9  *  purpose without fee is hereby granted, provided that this entire notice
10  *  is included in all copies of any software which is or includes a copy
11  *  or modification of this software.
12  *
13  *  THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR IMPLIED
14  *  WARRANTY.  IN PARTICULAR,  THE AUTHOR MAKES NO REPRESENTATION
15  *  OR WARRANTY OF ANY KIND CONCERNING THE MERCHANTABILITY OF THIS
16  *  SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR PURPOSE.
17  *
18  *  $Id$
19  */
20 
21 #ifndef _SYS_FEATURES_H
22 #define _SYS_FEATURES_H
23 
24 #ifdef __cplusplus
25 extern "C" {
26 #endif
27 
28 #include <picolibc.h>
29 
30 /* Macro to test version of GCC.  Returns 0 for non-GCC or too old GCC. */
31 #ifndef __GNUC_PREREQ
32 # if defined __GNUC__ && defined __GNUC_MINOR__
33 #  define __GNUC_PREREQ(maj, min) \
34 	((__GNUC__ << 16) + __GNUC_MINOR__ >= ((maj) << 16) + (min))
35 # else
36 #  define __GNUC_PREREQ(maj, min) 0
37 # endif
38 #endif /* __GNUC_PREREQ */
39 /* Version with trailing underscores for BSD compatibility. */
40 #define	__GNUC_PREREQ__(ma, mi)	__GNUC_PREREQ(ma, mi)
41 
42 /*
43  * Feature test macros control which symbols are exposed by the system
44  * headers.  Any of these must be defined before including any headers.
45  *
46  * __STRICT_ANSI__ (defined by gcc -ansi, -std=c90, -std=c99, or -std=c11)
47  *	ISO C
48  *
49  * _POSIX_SOURCE (deprecated by _POSIX_C_SOURCE=1)
50  * _POSIX_C_SOURCE >= 1
51  *	POSIX.1-1990
52  *
53  * _POSIX_C_SOURCE >= 2
54  *	POSIX.2-1992
55  *
56  * _POSIX_C_SOURCE >= 199309L
57  *	POSIX.1b-1993 Real-time extensions
58  *
59  * _POSIX_C_SOURCE >= 199506L
60  *	POSIX.1c-1995 Threads extensions
61  *
62  * _POSIX_C_SOURCE >= 200112L
63  *	POSIX.1-2001 and C99
64  *
65  * _POSIX_C_SOURCE >= 200809L
66  *	POSIX.1-2008
67  *
68  * _POSIX_C_SOURCE >= 202405L
69  *	POSIX.1-2024
70  *
71  * _XOPEN_SOURCE
72  *	POSIX.1-1990 and XPG4
73  *
74  * _XOPEN_SOURCE_EXTENDED
75  *	SUSv1 (POSIX.2-1992 plus XPG4v2)
76  *
77  * _XOPEN_SOURCE >= 500
78  *	SUSv2 (POSIX.1c-1995 plus XSI)
79  *
80  * _XOPEN_SOURCE >= 600
81  *	SUSv3 (POSIX.1-2001 plus XSI) and C99
82  *
83  * _XOPEN_SOURCE >= 700
84  *	SUSv4 (POSIX.1-2008 plus XSI)
85  *
86  * _ISOC99_SOURCE or gcc -std=c99 or g++
87  *	ISO C99
88  *
89  * _ISOC11_SOURCE or gcc -std=c11 or g++ -std=c++11
90  *	ISO C11
91  *
92  * _ISOC2x_SOURCE or gcc -std=c2x or g++ -std=c++20
93  *	ISO C11
94  *
95  * _ATFILE_SOURCE (implied by _POSIX_C_SOURCE >= 200809L)
96  *	"at" functions
97  *
98  * _LARGEFILE_SOURCE (deprecated by _XOPEN_SOURCE >= 500)
99  *	fseeko, ftello
100  *
101  * _GNU_SOURCE
102  *	All of the above plus GNU extensions
103  *
104  * _BSD_SOURCE (deprecated by _DEFAULT_SOURCE)
105  * _SVID_SOURCE (deprecated by _DEFAULT_SOURCE)
106  * _DEFAULT_SOURCE (or none of the above)
107  *	POSIX-1.2024 with BSD and SVr4 extensions
108  *
109  * _FORTIFY_SOURCE = 1, 2 or 3
110  * 	Object Size Checking function wrappers
111  *
112  * _ZEPHYR_SOURCE
113  *      Zephyr. ISO C + a small selection of other APIs.
114  */
115 
116 #ifdef _GNU_SOURCE
117 #undef _ATFILE_SOURCE
118 #define	_ATFILE_SOURCE		1
119 #undef	_DEFAULT_SOURCE
120 #define	_DEFAULT_SOURCE		1
121 #undef _ISOC99_SOURCE
122 #define	_ISOC99_SOURCE		1
123 #undef _ISOC11_SOURCE
124 #define	_ISOC11_SOURCE		1
125 #undef _ISOC2X_SOURCE
126 #define	_ISOC2X_SOURCE		1
127 #undef _POSIX_SOURCE
128 #define	_POSIX_SOURCE		1
129 #undef _POSIX_C_SOURCE
130 #define	_POSIX_C_SOURCE		200809L
131 #undef _XOPEN_SOURCE
132 #define	_XOPEN_SOURCE		700
133 #undef _XOPEN_SOURCE_EXTENDED
134 #define	_XOPEN_SOURCE_EXTENDED	1
135 #undef _LARGEFILE64_SOURCE
136 #define _LARGEFILE64_SOURCE     1
137 #endif /* _GNU_SOURCE */
138 
139 /* When building for Zephyr, set _ZEPHYR_SOURCE unless some other API
140  * indicator is set by the application. Don't check __STRICT_ANSI__ as that
141  * is set by the compiler for -std=cxx, or _POSIX_C_SOURCE as Zephyr defines
142  * that for picolibc currently.
143  */
144 
145 #if defined(__ZEPHYR__) && !defined(_ZEPHYR_SOURCE) &&                  \
146     !defined(_GNU_SOURCE)&&                                             \
147     !defined(_BSD_SOURCE) &&                                            \
148     !defined(_SVID_SOURCE) &&                                           \
149     !defined(_DEFAULT_SOURCE)
150 #define _ZEPHYR_SOURCE      1
151 #endif
152 
153 #if defined(_BSD_SOURCE) || defined(_SVID_SOURCE) || \
154    (!defined(__STRICT_ANSI__) && !defined(_ANSI_SOURCE) && \
155    !defined(_ISOC99_SOURCE) && !defined(_POSIX_SOURCE) && \
156    !defined(_POSIX_C_SOURCE) && !defined(_XOPEN_SOURCE) && \
157    !defined(_ZEPHYR_SOURCE))
158 #undef _DEFAULT_SOURCE
159 #define	_DEFAULT_SOURCE		1
160 #endif
161 
162 #if defined(_DEFAULT_SOURCE)
163 #undef _POSIX_SOURCE
164 #define	_POSIX_SOURCE		1
165 #undef _POSIX_C_SOURCE
166 #define	_POSIX_C_SOURCE		202405L
167 #endif
168 
169 #if !defined(_POSIX_SOURCE) && !defined(_POSIX_C_SOURCE) && \
170   ((!defined(__STRICT_ANSI__) && !defined(_ANSI_SOURCE)) || \
171    (_XOPEN_SOURCE - 0) >= 500)
172 #define	_POSIX_SOURCE		1
173 #if !defined(_XOPEN_SOURCE) || (_XOPEN_SOURCE - 0) >= 700
174 #define	_POSIX_C_SOURCE		200809L
175 #elif (_XOPEN_SOURCE - 0) >= 600
176 #define	_POSIX_C_SOURCE		200112L
177 #elif (_XOPEN_SOURCE - 0) >= 500
178 #define	_POSIX_C_SOURCE		199506L
179 #elif (_XOPEN_SOURCE - 0) < 500
180 #define	_POSIX_C_SOURCE		2
181 #endif
182 #endif
183 
184 #if defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE >= 200809
185 #undef _ATFILE_SOURCE
186 #define	_ATFILE_SOURCE		1
187 #endif
188 
189 #ifdef _ZEPHYR_SOURCE
190 #undef _ISOC99_SOURCE
191 #define	_ISOC99_SOURCE		1
192 #undef _ISOC11_SOURCE
193 #define	_ISOC11_SOURCE		1
194 #undef _ANSI_SOURCE
195 #define _ANSI_SOURCE            1
196 #endif
197 
198 /*
199  * The following private macros are used throughout the headers to control
200  * which symbols should be exposed.  They are for internal use only, as
201  * indicated by the leading double underscore, and must never be used outside
202  * of these headers.
203  *
204  * __POSIX_VISIBLE
205  *	any version of POSIX.1; enabled by default, or with _POSIX_SOURCE,
206  *	any value of _POSIX_C_SOURCE, or _XOPEN_SOURCE >= 500.
207  *
208  * __POSIX_VISIBLE >= 2
209  *	POSIX.2-1992; enabled by default, with _POSIX_C_SOURCE >= 2,
210  *	or _XOPEN_SOURCE >= 500.
211  *
212  * __POSIX_VISIBLE >= 199309
213  *	POSIX.1b-1993; enabled by default, with _POSIX_C_SOURCE >= 199309L,
214  *	or _XOPEN_SOURCE >= 500.
215  *
216  * __POSIX_VISIBLE >= 199506
217  *	POSIX.1c-1995; enabled by default, with _POSIX_C_SOURCE >= 199506L,
218  *	or _XOPEN_SOURCE >= 500.
219  *
220  * __POSIX_VISIBLE >= 200112
221  *	POSIX.1-2001; enabled by default, with _POSIX_C_SOURCE >= 200112L,
222  *	or _XOPEN_SOURCE >= 600.
223  *
224  * __POSIX_VISIBLE >= 200809
225  *	POSIX.1-2008; enabled by default, with _POSIX_C_SOURCE >= 200809L,
226  *	or _XOPEN_SOURCE >= 700.
227  *
228  * __POSIX_VISIBLE >= 202405
229  *	POSIX.1-2024; enabled by default, with _POSIX_C_SOURCE >= 202405L,
230  *	or _XOPEN_SOURCE >= 700.
231  *
232  * __XSI_VISIBLE
233  *	XPG4 XSI extensions; enabled with any version of _XOPEN_SOURCE.
234  *
235  * __XSI_VISIBLE >= 4
236  *	SUSv1 XSI extensions; enabled with both _XOPEN_SOURCE and
237  *	_XOPEN_SOURCE_EXTENDED together.
238  *
239  * __XSI_VISIBLE >= 500
240  *	SUSv2 XSI extensions; enabled with _XOPEN_SOURCE >= 500.
241  *
242  * __XSI_VISIBLE >= 600
243  *	SUSv3 XSI extensions; enabled with _XOPEN_SOURCE >= 600.
244  *
245  * __XSI_VISIBLE >= 700
246  *	SUSv4 XSI extensions; enabled with _XOPEN_SOURCE >= 700.
247  *
248  * __ISO_C_VISIBLE >= 1999
249  *	ISO C99; enabled with gcc -std=c99 or newer (on by default since GCC 5),
250  *	any version of C++, or with _ISOC99_SOURCE, _POSIX_C_SOURCE >= 200112L,
251  *	or _XOPEN_SOURCE >= 600.
252  *
253  * __ISO_C_VISIBLE >= 2011
254  *	ISO C11; enabled with gcc -std=c11 or newer (on by default since GCC 5),
255  *	g++ -std=c++11 or newer (on by default since GCC 6), or with
256  *	_ISOC11_SOURCE.
257  *
258  * __ISO_C_VISIBLE >= 2020
259  *	ISO C2x; enabled with gcc -std=c2x or newer,
260  *	g++ -std=c++20 or newer, or with
261  *	_ISOC2X_SOURCE.
262  *
263  * __ATFILE_VISIBLE
264  *	"at" functions; enabled by default, with _ATFILE_SOURCE,
265  *	_POSIX_C_SOURCE >= 200809L, or _XOPEN_SOURCE >= 700.
266  *
267  * __LARGEFILE_VISIBLE
268  *	fseeko, ftello; enabled with _LARGEFILE_SOURCE or _XOPEN_SOURCE >= 500.
269  *
270  * __LARGEFILE64_VISIBLE
271  *      additional large file extensions; enabled with _LARGEFILE64_SOURCE.
272  *
273  * __BSD_VISIBLE
274  *	BSD extensions; enabled by default, or with _BSD_SOURCE.
275  *
276  * __SVID_VISIBLE
277  *	SVr4 extensions; enabled by default, or with _SVID_SOURCE.
278  *
279  * __MISC_VISIBLE
280  *	Extensions found in both BSD and SVr4 (shorthand for
281  *	(__BSD_VISIBLE || __SVID_VISIBLE)), or newlib-specific
282  *	extensions; enabled by default.
283  *
284  * __GNU_VISIBLE
285  *	GNU extensions; enabled with _GNU_SOURCE.
286  *
287  * __SSP_FORTIFY_LEVEL
288  *	Object Size Checking; defined to 0 (off), 1, 2 or 3.
289  *
290  * __ZEPHYR_VISIBLE
291  *      Zephyr extensions; enabled with _ZEPHYR_SOURCE.
292  *
293  * In all cases above, "enabled by default" means either by defining
294  * _DEFAULT_SOURCE, or by not defining any of the public feature test macros.
295  */
296 
297 #ifdef _ATFILE_SOURCE
298 #define	__ATFILE_VISIBLE	1
299 #else
300 #define	__ATFILE_VISIBLE	0
301 #endif
302 
303 #ifdef _DEFAULT_SOURCE
304 #define	__BSD_VISIBLE		1
305 #else
306 #define	__BSD_VISIBLE		0
307 #endif
308 
309 #ifdef _GNU_SOURCE
310 #define	__GNU_VISIBLE		1
311 #else
312 #define	__GNU_VISIBLE		0
313 #endif
314 
315 #ifdef _ZEPHYR_SOURCE
316 #define __ZEPHYR_VISIBLE        1
317 #else
318 #define __ZEPHYR_VISIBLE        0
319 #endif
320 
321 #if defined(_ISOC2X_SOURCE) || \
322   (__STDC_VERSION__ - 0) > 201710L || (__cplusplus - 0) >= 202002L
323 #define __ISO_C_VISIBLE		2020
324 #elif defined(_ISOC11_SOURCE) || \
325   (__STDC_VERSION__ - 0) >= 201112L || (__cplusplus - 0) >= 201103L
326 #define	__ISO_C_VISIBLE		2011
327 #elif defined(_ISOC99_SOURCE) || (_POSIX_C_SOURCE - 0) >= 200112L || \
328   (__STDC_VERSION__ - 0) >= 199901L || defined(__cplusplus)
329 #define	__ISO_C_VISIBLE		1999
330 #else
331 #define	__ISO_C_VISIBLE		1990
332 #endif
333 
334 #if defined(_LARGEFILE_SOURCE) || (_XOPEN_SOURCE - 0) >= 500
335 #define	__LARGEFILE_VISIBLE	1
336 #else
337 #define	__LARGEFILE_VISIBLE	0
338 #endif
339 
340 #ifdef _LARGEFILE64_SOURCE
341 #define __LARGEFILE64_VISIBLE   1
342 #else
343 #define __LARGEFILE64_VISIBLE   0
344 #endif
345 
346 #ifdef _DEFAULT_SOURCE
347 #define	__MISC_VISIBLE		1
348 #else
349 #define	__MISC_VISIBLE		0
350 #endif
351 
352 #if (_POSIX_C_SOURCE - 0) >= 202405L
353 #define	__POSIX_VISIBLE		202405
354 #elif (_POSIX_C_SOURCE - 0) >= 200809L
355 #define	__POSIX_VISIBLE		200809
356 #elif (_POSIX_C_SOURCE - 0) >= 200112L
357 #define	__POSIX_VISIBLE		200112
358 #elif (_POSIX_C_SOURCE - 0) >= 199506L
359 #define	__POSIX_VISIBLE		199506
360 #elif (_POSIX_C_SOURCE - 0) >= 199309L
361 #define	__POSIX_VISIBLE		199309
362 #elif (_POSIX_C_SOURCE - 0) >= 2 || defined(_XOPEN_SOURCE)
363 #define	__POSIX_VISIBLE		199209
364 #elif defined(_POSIX_SOURCE) || defined(_POSIX_C_SOURCE)
365 #define	__POSIX_VISIBLE		199009
366 #else
367 #define	__POSIX_VISIBLE		0
368 #endif
369 
370 #ifdef _DEFAULT_SOURCE
371 #define	__SVID_VISIBLE		1
372 #else
373 #define	__SVID_VISIBLE		0
374 #endif
375 
376 #if (_XOPEN_SOURCE - 0) >= 700
377 #define	__XSI_VISIBLE		700
378 #elif (_XOPEN_SOURCE - 0) >= 600
379 #define	__XSI_VISIBLE		600
380 #elif (_XOPEN_SOURCE - 0) >= 500
381 #define	__XSI_VISIBLE		500
382 #elif defined(_XOPEN_SOURCE) && defined(_XOPEN_SOURCE_EXTENDED)
383 #define	__XSI_VISIBLE		4
384 #elif defined(_XOPEN_SOURCE)
385 #define	__XSI_VISIBLE		1
386 #else
387 #define	__XSI_VISIBLE		0
388 #endif
389 
390 #if _FORTIFY_SOURCE > 0 && !defined(__cplusplus) && !defined(__lint__) && \
391    (__OPTIMIZE__ > 0 || defined(__clang__)) && __GNUC_PREREQ__(4, 1) && \
392    !defined(_LIBC)
393 #  if _FORTIFY_SOURCE > 2 && defined(__has_builtin)
394 #    if __has_builtin(__builtin_dynamic_object_size)
395 #      define __SSP_FORTIFY_LEVEL 3
396 #    else
397 #      define __SSP_FORTIFY_LEVEL 2
398 #    endif
399 #  elif _FORTIFY_SOURCE > 1
400 #    define __SSP_FORTIFY_LEVEL 2
401 #  else
402 #    define __SSP_FORTIFY_LEVEL 1
403 #  endif
404 #else
405 #  define __SSP_FORTIFY_LEVEL 0
406 #endif
407 
408 /* RTEMS adheres to POSIX -- 1003.1b with some features from annexes.  */
409 
410 #ifdef __rtems__
411 #define _POSIX_JOB_CONTROL		1
412 #define _POSIX_SAVED_IDS		1
413 #define _POSIX_VERSION			199309L
414 #define _POSIX_ASYNCHRONOUS_IO		1
415 #define _POSIX_FSYNC			1
416 #define _POSIX_MAPPED_FILES		1
417 #define _POSIX_MEMLOCK			1
418 #define _POSIX_MEMLOCK_RANGE		1
419 #define _POSIX_MEMORY_PROTECTION	1
420 #define _POSIX_MESSAGE_PASSING		1
421 #define _POSIX_MONOTONIC_CLOCK		200112L
422 #define _POSIX_CLOCK_SELECTION		200112L
423 #define _POSIX_PRIORITIZED_IO		1
424 #define _POSIX_PRIORITY_SCHEDULING	1
425 #define _POSIX_REALTIME_SIGNALS		1
426 #define _POSIX_SEMAPHORES		1
427 #define _POSIX_SHARED_MEMORY_OBJECTS	1
428 #define _POSIX_SYNCHRONIZED_IO		1
429 #define _POSIX_TIMERS			1
430 #define _POSIX_BARRIERS                 200112L
431 #define _POSIX_READER_WRITER_LOCKS      200112L
432 #define _POSIX_SPIN_LOCKS               200112L
433 
434 
435 /* In P1003.1b but defined by drafts at least as early as P1003.1c/D10  */
436 #define _POSIX_THREADS				1
437 #define _POSIX_THREAD_ATTR_STACKADDR		1
438 #define _POSIX_THREAD_ATTR_STACKSIZE		1
439 #define _POSIX_THREAD_PRIORITY_SCHEDULING	1
440 #define _POSIX_THREAD_PRIO_INHERIT		1
441 #define _POSIX_THREAD_PRIO_PROTECT		1
442 #define _POSIX_THREAD_PROCESS_SHARED		1
443 #define _POSIX_THREAD_SAFE_FUNCTIONS		1
444 
445 /* P1003.4b/D8 defines the constants below this comment. */
446 #define _POSIX_SPAWN				1
447 #define _POSIX_TIMEOUTS				1
448 #define _POSIX_CPUTIME				1
449 #define _POSIX_THREAD_CPUTIME			1
450 #define _POSIX_SPORADIC_SERVER			1
451 #define _POSIX_THREAD_SPORADIC_SERVER		1
452 #define _POSIX_DEVICE_CONTROL			1
453 #define _POSIX_DEVCTL_DIRECTION			1
454 #define _POSIX_INTERRUPT_CONTROL		1
455 #define _POSIX_ADVISORY_INFO			1
456 
457 /* UNIX98 added some new pthread mutex attributes */
458 #define _UNIX98_THREAD_MUTEX_ATTRIBUTES         1
459 
460 /* POSIX 1003.26-2003 defined device control method */
461 #define _POSIX_26_VERSION			200312L
462 
463 #endif
464 
465 /* XMK loosely adheres to POSIX -- 1003.1 */
466 #ifdef __XMK__
467 #define _POSIX_THREADS				1
468 #define _POSIX_THREAD_PRIORITY_SCHEDULING	1
469 #endif
470 
471 
472 #ifdef __svr4__
473 # define _POSIX_JOB_CONTROL     1
474 # define _POSIX_SAVED_IDS       1
475 # define _POSIX_VERSION 199009L
476 #endif
477 
478 #ifdef __cplusplus
479 }
480 #endif
481 #endif /* _SYS_FEATURES_H */
482