1#
2# SPDX-License-Identifier: BSD-3-Clause
3#
4# Copyright © 2022 Keith Packard
5#
6# Redistribution and use in source and binary forms, with or without
7# modification, are permitted provided that the following conditions
8# are met:
9#
10# 1. Redistributions of source code must retain the above copyright
11#    notice, this list of conditions and the following disclaimer.
12#
13# 2. Redistributions in binary form must reproduce the above
14#    copyright notice, this list of conditions and the following
15#    disclaimer in the documentation and/or other materials provided
16#    with the distribution.
17#
18# 3. Neither the name of the copyright holder nor the names of its
19#    contributors may be used to endorse or promote products derived
20#    from this software without specific prior written permission.
21#
22# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26# COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
27# INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
28# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
29# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
31# STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
33# OF THE POSSIBILITY OF SUCH DAMAGE.
34#
35
36cmake_minimum_required(VERSION 3.20.0)
37
38project(Picolibc VERSION 1.8.6 LANGUAGES C ASM)
39
40find_program(CCACHE_PROGRAM ccache)
41if(CCACHE_PROGRAM)
42  message(STATUS "Using ccache: ${CCACHE_PROGRAM}")
43  set(CMAKE_C_COMPILER_LAUNCHER "${CCACHE_PROGRAM}")
44endif()
45
46if(${CMAKE_SYSTEM_PROCESSOR} STREQUAL "arm64")
47  set(CMAKE_SYSTEM_PROCESSOR "aarch64")
48endif()
49
50set(CMAKE_SYSTEM_SUB_PROCESSOR ${CMAKE_SYSTEM_PROCESSOR})
51
52if(${CMAKE_SYSTEM_PROCESSOR} STREQUAL "i686" OR
53    ${CMAKE_SYSTEM_PROCESSOR} STREQUAL "x86_64")
54  set(CMAKE_SYSTEM_PROCESSOR "x86")
55endif()
56
57if(ZEPHYR_BASE)
58  if(NOT CONFIG_PICOLIBC_USE_MODULE)
59    return()
60  endif()
61  include(zephyr/zephyr.cmake)
62endif()
63
64include(cmake/picolibc.cmake)
65
66enable_testing()
67
68set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY)
69
70# Set all configure values to defaults for now
71
72# Use atomics for fgetc/ungetc for re-entrancy
73set(ATOMIC_UNGETC 1)
74
75# Always optimize strcmp for performance
76if(NOT DEFINED FAST_STRCMP)
77  option(FAST_STRCMP "Always optimize strcmp for performance" ON)
78endif()
79
80# Obsoleted. Use regular syscalls
81set(MISSING_SYSCALL_NAMES 0)
82
83# use global errno variable
84if(NOT DEFINED NEWLIB_GLOBAL_ERRNO)
85  option(NEWLIB_GLOBAL_ERRNO "use global errno variable" OFF)
86endif()
87
88# use thread local storage
89if(NOT DEFINED PICOLIBC_TLS)
90  option(PICOLIBC_TLS "use thread local storage for static data" ON)
91endif()
92
93# use thread local storage
94set(NEWLIB_TLS ${PICOLIBC_TLS})
95
96# Use open/close/read/write in tinystdio
97option(POSIX_IO "Provide fopen/fdopen using POSIX I/O (open, close, read, write, lseek)" ON)
98
99option(POSIX_CONSOLE "Use POSIX I/O for stdin/stdout/stderr" OFF)
100
101# Optimize for space over speed
102
103if(NOT DEFINED PREFER_SIZE_OVER_SPEED)
104  if(CMAKE_BUILD_TYPE STREQUAL "MinSizeRel")
105    set(default_prefer_size 1)
106  else()
107    set(default_prefer_size 0)
108  endif()
109  option(PREFER_SIZE_OVER_SPEED ${default_prefer_size})
110endif()
111
112# Obsoleted. Reentrant syscalls provided for us
113set(REENTRANT_SYSCALLS_PROVIDED 0)
114
115# Use tiny stdio from gcc avr
116option(TINY_STDIO "Use tiny stdio from avr libc" ON)
117
118if(NOT TLS_MODEL)
119  set(TLS_MODEL "local-exec")
120endif()
121
122set(_ATEXIT_DYNAMIC_ALLOC 0)
123
124set(_FSEEK_OPTIMIZATION 0)
125
126set(_FVWRITE_IN_STREAMIO 0)
127
128# Compiler supports alias symbol attribute
129picolibc_flag(_HAVE_ALIAS_ATTRIBUTE)
130
131# The compiler REALLY has the attribute __alloc_size__
132picolibc_flag(_HAVE_ALLOC_SIZE)
133
134# The compiler supports the always_inline function attribute
135picolibc_flag(_HAVE_ATTRIBUTE_ALWAYS_INLINE)
136
137# The compiler supports the gnu_inline function attribute
138picolibc_flag(_HAVE_ATTRIBUTE_GNU_INLINE)
139
140# Use bitfields in packed structs
141picolibc_flag(_HAVE_BITFIELDS_IN_PACKED_STRUCTS)
142
143# The compiler supports __builtin_alloca
144picolibc_flag(_HAVE_BUILTIN_ALLOCA)
145
146# The compiler supports __builtin_copysign
147picolibc_flag(_HAVE_BUILTIN_COPYSIGN)
148
149# The compiler supports __builtin_copysignl
150picolibc_flag(_HAVE_BUILTIN_COPYSIGNL)
151
152# The compiler supports __builtin_ctz
153picolibc_flag(_HAVE_BUILTIN_CTZ)
154
155# The compiler supports __builtin_ctzl
156picolibc_flag(_HAVE_BUILTIN_CTZL)
157
158# The compiler supports __builtin_ctzll
159picolibc_flag(_HAVE_BUILTIN_CTZLL)
160
161# Compiler has __builtin_expect
162picolibc_flag(_HAVE_BUILTIN_EXPECT)
163
164# The compiler supports __builtin_ffs
165picolibc_flag(_HAVE_BUILTIN_FFS)
166
167# The compiler supports __builtin_ffsl
168picolibc_flag(_HAVE_BUILTIN_FFSL)
169
170# The compiler supports __builtin_ffsll
171picolibc_flag(_HAVE_BUILTIN_FFSLL)
172
173# The compiler supports __builtin_finitel
174picolibc_flag(_HAVE_BUILTIN_FINITEL)
175
176# The compiler supports __builtin_isfinite
177picolibc_flag(_HAVE_BUILTIN_ISFINITE)
178
179# The compiler supports __builtin_isinf
180picolibc_flag(_HAVE_BUILTIN_ISINF)
181
182# The compiler supports __builtin_isinfl
183picolibc_flag(_HAVE_BUILTIN_ISINFL)
184
185# The compiler supports __builtin_isnan
186picolibc_flag(_HAVE_BUILTIN_ISNAN)
187
188# The compiler supports __builtin_isnanl
189picolibc_flag(_HAVE_BUILTIN_ISNANL)
190
191# Compiler has __builtin_mul_overflow
192picolibc_flag(_HAVE_BUILTIN_MUL_OVERFLOW)
193
194# Compiler has __builtin_add_overflow
195picolibc_flag(_HAVE_BUILTIN_ADD_OVERFLOW)
196
197# Compiler has support for flag to prevent mis-optimizing memcpy/memset patterns
198picolibc_flag(_HAVE_CC_INHIBIT_LOOP_TO_LIBCALL)
199
200# Compiler supports _Complex
201picolibc_flag(_HAVE_COMPLEX)
202
203# Compiler supports __builtin_complex
204picolibc_flag(_HAVE_BUILTIN_COMPLEX)
205
206# Compiler supports format function attribute
207picolibc_flag(_HAVE_FORMAT_ATTRIBUTE 1)
208
209# Compiler supports weak attribute
210picolibc_flag(_HAVE_WEAK_ATTRIBUTE)
211
212set(_HAVE_FCNTL 0)
213
214# IEEE fp funcs available
215set(_HAVE_IEEEFP_FUNCS 0)
216
217# compiler supports INIT_ARRAY sections
218set(_HAVE_INITFINI_ARRAY 1)
219
220# Support _init() and _fini() functions
221set(_HAVE_INIT_FINI 1)
222
223# Compiler has long double type
224picolibc_flag(_HAVE_LONG_DOUBLE)
225
226# Compiler attribute to prevent the optimizer from adding new builtin calls
227picolibc_flag(_HAVE_NO_BUILTIN_ATTRIBUTE)
228
229# _set_tls and _init_tls functions available
230if(NOT DEFINED _HAVE_PICOLIBC_TLS_API OR NOT PICOLIBC_TLS)
231  set(_HAVE_PICOLIBC_TLS_API 0)
232endif()
233
234set(_HAVE_SEMIHOST 1)
235set(_ICONV_ENABLE_EXTERNAL_CCS 0)
236set(_ICONV_FROM_ENCODING_ 1)
237set(_ICONV_FROM_ENCODING_BIG5 1)
238set(_ICONV_FROM_ENCODING_CP775 1)
239set(_ICONV_FROM_ENCODING_CP850 1)
240set(_ICONV_FROM_ENCODING_CP852 1)
241set(_ICONV_FROM_ENCODING_CP855 1)
242set(_ICONV_FROM_ENCODING_CP866 1)
243set(_ICONV_FROM_ENCODING_EUC_JP 1)
244set(_ICONV_FROM_ENCODING_EUC_KR 1)
245set(_ICONV_FROM_ENCODING_EUC_TW 1)
246set(_ICONV_FROM_ENCODING_ISO_8859_1 1)
247set(_ICONV_FROM_ENCODING_ISO_8859_10 1)
248set(_ICONV_FROM_ENCODING_ISO_8859_11 1)
249set(_ICONV_FROM_ENCODING_ISO_8859_13 1)
250set(_ICONV_FROM_ENCODING_ISO_8859_14 1)
251set(_ICONV_FROM_ENCODING_ISO_8859_15 1)
252set(_ICONV_FROM_ENCODING_ISO_8859_2 1)
253set(_ICONV_FROM_ENCODING_ISO_8859_3 1)
254set(_ICONV_FROM_ENCODING_ISO_8859_4 1)
255set(_ICONV_FROM_ENCODING_ISO_8859_5 1)
256set(_ICONV_FROM_ENCODING_ISO_8859_6 1)
257set(_ICONV_FROM_ENCODING_ISO_8859_7 1)
258set(_ICONV_FROM_ENCODING_ISO_8859_8 1)
259set(_ICONV_FROM_ENCODING_ISO_8859_9 1)
260set(_ICONV_FROM_ENCODING_ISO_IR_111 1)
261set(_ICONV_FROM_ENCODING_KOI8_R 1)
262set(_ICONV_FROM_ENCODING_KOI8_RU 1)
263set(_ICONV_FROM_ENCODING_KOI8_U 1)
264set(_ICONV_FROM_ENCODING_KOI8_UNI 1)
265set(_ICONV_FROM_ENCODING_UCS_2 1)
266set(_ICONV_FROM_ENCODING_UCS_2BE 1)
267set(_ICONV_FROM_ENCODING_UCS_2LE 1)
268set(_ICONV_FROM_ENCODING_UCS_2_INTERNAL 1)
269set(_ICONV_FROM_ENCODING_UCS_4 1)
270set(_ICONV_FROM_ENCODING_UCS_4BE 1)
271set(_ICONV_FROM_ENCODING_UCS_4LE 1)
272set(_ICONV_FROM_ENCODING_UCS_4_INTERNAL 1)
273set(_ICONV_FROM_ENCODING_US_ASCII 1)
274set(_ICONV_FROM_ENCODING_UTF_16 1)
275set(_ICONV_FROM_ENCODING_UTF_16BE 1)
276set(_ICONV_FROM_ENCODING_UTF_16LE 1)
277set(_ICONV_FROM_ENCODING_UTF_8 1)
278set(_ICONV_FROM_ENCODING_WIN_1250 1)
279set(_ICONV_FROM_ENCODING_WIN_1251 1)
280set(_ICONV_FROM_ENCODING_WIN_1252 1)
281set(_ICONV_FROM_ENCODING_WIN_1253 1)
282set(_ICONV_FROM_ENCODING_WIN_1254 1)
283set(_ICONV_FROM_ENCODING_WIN_1255 1)
284set(_ICONV_FROM_ENCODING_WIN_1256 1)
285set(_ICONV_FROM_ENCODING_WIN_1257 1)
286set(_ICONV_FROM_ENCODING_WIN_1258 1)
287set(_ICONV_TO_ENCODING_ 1)
288set(_ICONV_TO_ENCODING_BIG5 1)
289set(_ICONV_TO_ENCODING_CP775 1)
290set(_ICONV_TO_ENCODING_CP850 1)
291set(_ICONV_TO_ENCODING_CP852 1)
292set(_ICONV_TO_ENCODING_CP855 1)
293set(_ICONV_TO_ENCODING_CP866 1)
294set(_ICONV_TO_ENCODING_EUC_JP 1)
295set(_ICONV_TO_ENCODING_EUC_KR 1)
296set(_ICONV_TO_ENCODING_EUC_TW 1)
297set(_ICONV_TO_ENCODING_ISO_8859_1 1)
298set(_ICONV_TO_ENCODING_ISO_8859_10 1)
299set(_ICONV_TO_ENCODING_ISO_8859_11 1)
300set(_ICONV_TO_ENCODING_ISO_8859_13 1)
301set(_ICONV_TO_ENCODING_ISO_8859_14 1)
302set(_ICONV_TO_ENCODING_ISO_8859_15 1)
303set(_ICONV_TO_ENCODING_ISO_8859_2 1)
304set(_ICONV_TO_ENCODING_ISO_8859_3 1)
305set(_ICONV_TO_ENCODING_ISO_8859_4 1)
306set(_ICONV_TO_ENCODING_ISO_8859_5 1)
307set(_ICONV_TO_ENCODING_ISO_8859_6 1)
308set(_ICONV_TO_ENCODING_ISO_8859_7 1)
309set(_ICONV_TO_ENCODING_ISO_8859_8 1)
310set(_ICONV_TO_ENCODING_ISO_8859_9 1)
311set(_ICONV_TO_ENCODING_ISO_IR_111 1)
312set(_ICONV_TO_ENCODING_KOI8_R 1)
313set(_ICONV_TO_ENCODING_KOI8_RU 1)
314set(_ICONV_TO_ENCODING_KOI8_U 1)
315set(_ICONV_TO_ENCODING_KOI8_UNI 1)
316set(_ICONV_TO_ENCODING_UCS_2 1)
317set(_ICONV_TO_ENCODING_UCS_2BE 1)
318set(_ICONV_TO_ENCODING_UCS_2LE 1)
319set(_ICONV_TO_ENCODING_UCS_2_INTERNAL 1)
320set(_ICONV_TO_ENCODING_UCS_4 1)
321set(_ICONV_TO_ENCODING_UCS_4BE 1)
322set(_ICONV_TO_ENCODING_UCS_4LE 1)
323set(_ICONV_TO_ENCODING_UCS_4_INTERNAL 1)
324set(_ICONV_TO_ENCODING_US_ASCII 1)
325set(_ICONV_TO_ENCODING_UTF_16 1)
326set(_ICONV_TO_ENCODING_UTF_16BE 1)
327set(_ICONV_TO_ENCODING_UTF_16LE 1)
328set(_ICONV_TO_ENCODING_UTF_8 1)
329set(_ICONV_TO_ENCODING_WIN_1250 1)
330set(_ICONV_TO_ENCODING_WIN_1251 1)
331set(_ICONV_TO_ENCODING_WIN_1252 1)
332set(_ICONV_TO_ENCODING_WIN_1253 1)
333set(_ICONV_TO_ENCODING_WIN_1254 1)
334set(_ICONV_TO_ENCODING_WIN_1255 1)
335set(_ICONV_TO_ENCODING_WIN_1256 1)
336set(_ICONV_TO_ENCODING_WIN_1257 1)
337set(_ICONV_TO_ENCODING_WIN_1258 1)
338
339# math library does not set errno (offering only ieee semantics)
340set(_IEEE_LIBM 1)
341
342if(NOT DEFINED _IO_FLOAT_EXACT)
343  option(_IO_FLOAT_EXACT "Provide exact binary/decimal conversion for printf/scanf" 1)
344endif()
345
346set(_LITE_EXIT 1)
347
348set(_PICO_EXIT 1)
349
350if(NOT DEFINED _ASSERT_VERBOSE)
351  option(_ASSERT_VERBOSE "Assert provides verbose information" 1)
352endif()
353
354if(NOT DEFINED _MB_CAPABLE)
355  option(_MB_CAPABLE "Enable multi-byte support" 0)
356endif()
357
358set(_MB_LEN_MAX 1 1)
359
360set(_NANO_FORMATTED_IO 0)
361
362option(_NANO_MALLOC "Use smaller malloc implementation" 1)
363
364set(_REENT_GLOBAL_ATEXIT 0)
365
366set(_UNBUF_STREAM_OPT 0)
367
368if(NOT DEFINED _WANT_IO_C99_FORMATS)
369  option(_WANT_IO_C99_FORMATS "Support C99 formats in printf/scanf" ON)
370endif()
371
372if(NOT DEFINED _WANT_IO_LONG_LONG)
373  option(_WANT_IO_LONG_LONG "Support long long in integer printf/scanf" OFF)
374endif()
375
376if(NOT DEFINED _WANT_IO_LONG_DOUBLE)
377  option(_WANT_IO_LONG_DOUBLE "Support long double in printf/scanf" OFF)
378endif()
379
380if(NOT DEFINED _WANT_MINIMAL_IO_LONG_LONG)
381  option(_WANT_MINIMAL_IO_LONG_LONG "Support long long in minimal printf/scanf" OFF)
382endif()
383
384if(NOT DEFINED _WANT_IO_POS_ARGS)
385  option(_WANT_IO_POS_ARGS "Support positional args in integer printf/scanf" OFF)
386endif()
387
388option(__IO_FLOAT "Support floating point in printf/scanf by default" OFF)
389
390if(NOT DEFINED _PRINTF_SMALL_ULTOA)
391  option(_PRINTF_SMALL_ULTOA "Avoid soft divide in printf" ON)
392endif()
393
394if(NOT DEFINED _WANT_IO_PERCENT_B)
395  option(_WANT_IO_PERCENT_B "Support %b/%B formats in printf/scanf" OFF)
396endif()
397
398if(NOT DEFINED _FORMAT_DEFAULT_MINIMAL)
399  set(_FORMAT_DEFAULT_MINIMAL ${IO_MINIMAL})
400endif()
401
402if(_FORMAT_DEFAULT_MINIMAL)
403  set(_FORMAT_DEFAULT_DOUBLE 0)
404  set(_FORMAT_DEFAULT_FLOAT 0)
405  set(_FORMAT_DEFAULT_INTEGER 0)
406  set(_FORMAT_DEFAULT_LONG_LONG 0)
407endif()
408
409if(NOT DEFINED _FORMAT_DEFAULT_DOUBLE)
410  set(_FORMAT_DEFAULT_DOUBLE ${__IO_FLOAT})
411endif()
412
413if(NOT DEFINED _FORMAT_DEFAULT_FLOAT)
414  set(_FORMAT_DEFAULT_FLOAT 0)
415endif()
416
417if(NOT DEFINED _FORMAT_DEFAULT_LONG_LONG)
418  set(_FORMAT_DEFAULT_LONG_LONG 0)
419endif()
420
421if(NOT DEFINED _FORMAT_DEFAULT_INTEGER)
422  if (__IO_FLOAT)
423    set(_FORMAT_DEFAULT_INTEGER 0)
424  else()
425    set(_FORMAT_DEFAULT_INTEGER 1)
426  endif()
427endif()
428
429# math library sets errno
430set(_WANT_MATH_ERRNO 0)
431
432set(_WANT_REENT_SMALL 0)
433
434set(_WANT_REGISTER_FINI 0)
435
436# Obsoleted. Define time_t to long instead of using a 64-bit type
437set(_WANT_USE_LONG_TIME_T 0)
438
439set(_WIDE_ORIENT 0)
440
441# locale support
442if(NOT DEFINED __HAVE_LOCALE_INFO__)
443  option(__HAVE_LOCALE_INFO__ "Provide locale support" 0)
444endif()
445
446# extended locale support
447if(NOT DEFINED __HAVE_LOCALE_INFO_EXTENDED__)
448  option(__HAVE_LOCALE_INFO_EXTENDED__ "Provide even more locale support" 0)
449endif()
450
451if(__HAVE_LOCALE_INFO_EXTENDED__ AND NOT __HAVE_LOCALE_INFO__)
452  message(FATAL_ERROR "Must have locale support to enable extended locale support")
453endif()
454
455# Use old math code for double funcs (0 no, 1 yes)
456set(__OBSOLETE_MATH_FLOAT 1)
457
458# Use old math code for double funcs (0 no, 1 yes)
459set(__OBSOLETE_MATH_DOUBLE 1)
460
461# Compute static memory area sizes at runtime instead of link time
462set(__PICOLIBC_CRT_RUNTIME_SIZE 0)
463
464if(NOT DEFINED __SINGLE_THREAD__)
465  option(__SINGLE_THREAD__ "Disable multithreading support" 0)
466endif()
467
468if(__SINGLE_THREAD__)
469  set(_RETARGETABLE_LOCKING 0)
470else()
471  set(_RETARGETABLE_LOCKING 1)
472endif()
473
474set(NEWLIB_VERSION 4.3.0)
475set(NEWLIB_MAJOR 4)
476set(NEWLIB_MINOR 3)
477set(NEWLIB_PATCH 0)
478
479set(PICOLIBC_INCLUDE ${PROJECT_BINARY_DIR}/picolibc/include)
480
481set(PICOLIBC_SCRIPTS "${CMAKE_CURRENT_SOURCE_DIR}/scripts")
482
483include(CheckIncludeFile)
484
485CHECK_INCLUDE_FILE(xtensa/config/core-isa.h _XTENSA_HAVE_CONFIG_CORE_ISA_H)
486
487configure_file(picolibc.h.in "${PICOLIBC_INCLUDE}/picolibc.h")
488
489set(INCLUDEDIR include)
490set(LIBDIR .)
491if(PICOLIBC_TLS)
492  set(TLSMODEL "-ftls-model=${TLS_MODEL}")
493endif()
494set(LINK_SPEC "")
495set(CC1_SPEC "")
496set(CC1PLUS_SPEC "")
497set(ADDITIONAL_LIBS "")
498set(SPECS_EXTRA "")
499set(SPECS_ISYSTEM "-isystem ${PROJECT_BINARY_DIR}/${include}")
500set(SPECS_LIBPATH "-L${PROJECT_BINARY_DIR}")
501set(SPECS_STARTFILE "${PROJECT_BINARY_DIR}/crt0.o")
502string(APPEND SPECS_PRINTF "%{DPICOLIBC_FLOAT_PRINTF_SCANF:--defsym=vfprintf=__f_vfprintf}"
503  " %{DPICOLIBC_FLOAT_PRINTF_SCANF:--defsym=vfscanf=__f_vfscanf}"
504  " %{DPICOLIBC_DOUBLE_PRINTF_SCANF:--defsym=vfprintf=__d_vfprintf}"
505  " %{DPICOLIBC_DOUBLE_PRINTF_SCANF:--defsym=vfscanf=__d_vfscanf}"
506  " %{DPICOLIBC_INTEGER_PRINTF_SCANF:--defsym=vfprintf=__i_vfprintf}"
507  " %{DPICOLIBC_INTEGER_PRINTF_SCANF:--defsym=vfscanf=__i_vfscanff}"
508  " %{DPICOLIBC_MINIMAL_PRINTF_SCANF:--defsym=vfprintf=__m_vfprintf}"
509  " %{DPICOLIBC_MINIMAL_PRINTF_SCANF:--defsym=vfscanf=__i_vfscanff}"
510  )
511set(PREFIX "${PROJECT_BINARY_DIR}")
512
513configure_file(picolibc.specs.in "${PROJECT_BINARY_DIR}/picolibc.specs" @ONLY)
514
515set(PICOLIBC_COMPILE_OPTIONS
516  "-include" "${PICOLIBC_INCLUDE}/picolibc.h"
517  "-nostdlib"
518  "-D_LIBC"
519  ${TLSMODEL}
520  ${TARGET_COMPILE_OPTIONS}
521  ${PICOLIBC_EXTRA_COMPILE_OPTIONS}
522  ${PICOLIBC_MATH_FLAGS}
523  )
524
525# Strip out any generator expressions as those cannot be used with
526# try_compile
527
528foreach(c_option "${PICOLIBC_COMPILE_OPTIONS}")
529  if(NOT "${c_option}" MATCHES "\\$")
530    list(APPEND PICOLIBC_TEST_COMPILE_OPTIONS "${c_option}")
531  endif()
532endforeach()
533
534picolibc_supported_compile_options(
535  "-fno-common"
536  "-fno-stack-protector"
537  "-ffunction-sections"
538  "-fdata-sections"
539  "-Wall"
540  "-Wextra"
541  "-Werror=implicit-function-declaration"
542  "-Werror=vla"
543  "-Warray-bounds"
544  "-Wold-style-definition"
545  "-frounding-math"
546  "-fsignaling-nans"
547  )
548
549add_library(c STATIC)
550
551target_compile_options(c PRIVATE ${PICOLIBC_COMPILE_OPTIONS})
552
553set(PICOLIBC_INCLUDE_DIRECTORIES
554  "${PICOLIBC_INCLUDE}")
555
556target_include_directories(c SYSTEM PUBLIC ${PICOLIBC_INCLUDE_DIRECTORIES})
557
558define_property(GLOBAL PROPERTY PICOLIBC_HEADERS
559  BRIEF_DOCS "Installed header files"
560  FULL_DOCS "These are names of header files which are to be installed.")
561
562add_subdirectory(newlib)
563
564install(TARGETS c
565  LIBRARY DESTINATION lib
566  PUBLIC_HEADER DESTINATION include)
567
568option(TESTS "Enable tests" OFF)
569if(TESTS)
570
571  # This could use some generalization, but it's good enough to do
572  # semihosting-based tests
573
574  add_subdirectory(semihost)
575  add_subdirectory(picocrt)
576
577  set(PICOCRT_OBJ $<TARGET_OBJECTS:picocrt>)
578  set(PICOCRT_SEMIHOST_OBJ $<TARGET_OBJECTS:picocrt-semihost>)
579
580  # semihost and libc have mutual-dependencies, so place them in a
581  # linker group
582
583  set(PICOLIBC_TEST_LINK_LIBRARIES
584    ${PICOCRT_SEMIHOST_OBJ}
585    -Wl,--start-group c semihost -Wl,--end-group
586    ${PICOLIBC_LINK_FLAGS}
587    )
588
589  add_subdirectory(test)
590
591endif()
592
593install(FILES ${CMAKE_BINARY_DIR}/picolibc.specs DESTINATION lib)
594install(FILES ${CMAKE_BINARY_DIR}/picolibc/include/picolibc.h DESTINATION include)
595