@@ -21,7 +21,7 @@ set(llvmproject_src_dir ${TOOLCHAIN_SOURCE_DIR}/../..)
2121# CMake arguments are loaded from the JSON file depending on which C
2222# library is used, so this must be set before the JSON is processed.
2323set (C_LIBRARY "picolibc" CACHE STRING "Which C library to use." )
24- set_property (CACHE C_LIBRARY PROPERTY STRINGS picolibc newlib llvmlibc)
24+ set_property (CACHE C_LIBRARY PROPERTY STRINGS picolibc newlib newlib-nano llvmlibc)
2525
2626set (VARIANT_JSON "" CACHE STRING "JSON file to load args from." )
2727if (VARIANT_JSON)
@@ -37,7 +37,12 @@ if(VARIANT_JSON)
3737 set (${json_param} _def ${json_val} )
3838 endforeach ()
3939 # Load arguments specific to the chosen library, overwriting any existing values.
40- string (JSON json_args GET ${variant_json_read} "args" ${C_LIBRARY} )
40+ if (C_LIBRARY MATCHES "^newlib" )
41+ # Treat newlib variants as newlib for library compatibility
42+ string (JSON json_args GET ${variant_json_read} "args" "newlib" )
43+ else ()
44+ string (JSON json_args GET ${variant_json_read} "args" ${C_LIBRARY} )
45+ endif ()
4146 string (JSON json_args_len LENGTH ${json_args} )
4247 math (EXPR json_args_len_dec "${json_args_len} - 1" )
4348 foreach (json_idx RANGE ${json_args_len_dec} )
@@ -85,7 +90,7 @@ set(PICOLIBC_BUILD_TYPE ${PICOLIBC_BUILD_TYPE_def} CACHE STRING "Picolibc config
8590set_property (CACHE PICOLIBC_BUILD_TYPE PROPERTY STRINGS minsize release)
8691
8792set (ENABLE_CXX_LIBS ${ENABLE_CXX_LIBS_def} CACHE BOOL "Build CXX libs" )
88- set (ENABLE_LIBC_TESTS ${ENABLE_LIBC_TESTS_def} CACHE BOOL "Enable libc tests (picolibc, newlib or llvm-libc)." )
93+ set (ENABLE_LIBC_TESTS ${ENABLE_LIBC_TESTS_def} CACHE BOOL "Enable libc tests (picolibc, newlib, newlib-nano or llvm-libc)." )
8994set (ENABLE_COMPILER_RT_TESTS ${ENABLE_COMPILER_RT_TESTS_def} CACHE BOOL "Enable compiler-rt tests." )
9095set (ENABLE_LIBCXX_TESTS ${ENABLE_LIBCXX_TESTS_def} CACHE BOOL "Enable libcxx tests." )
9196set (LLVM_BINARY_DIR "" CACHE PATH "Path to LLVM toolchain root to build libraries with" )
@@ -459,11 +464,60 @@ endif()
459464# newlib
460465###############################################################################
461466
462- if (C_LIBRARY STREQUAL newlib)
467+ if (C_LIBRARY MATCHES "^ newlib" )
463468 if (ENABLE_LIBC_TESTS)
464469 message (FATAL_ERROR "Tests cannot yet be enabled using newlib libc." )
465470 endif ()
466471
472+ if (C_LIBRARY STREQUAL newlib-nano)
473+ set (newlib_optim_flags
474+ "-g -Oz" )
475+ if (TARGET_ARCH MATCHES "^aarch64" )
476+ # When trying to build newlib-nano on AArch64, fall back to regular newlib
477+ # See https://gitlab.arm.com/tooling/gnu-devtools-for-arm/-/blob/9e3db8c042400c9c59686dec8eda2e8c44e5fb5d/build-baremetal-toolchain.sh#L625
478+ # ARMGCC just skips building the nano libs for AArch64, but in order to not break integration and testing of multilib, we are building
479+ # regular newlib for AArch64 with extreme size optimisation and calling it newlib-nano.
480+ set (newlib_flags
481+ --enable-newlib-io-long-long
482+ --enable-newlib-register-fini
483+ --disable-newlib-supplied-syscalls
484+ --enable-newlib-io-c99-formats
485+ --disable-nls
486+ --enable-lite-exit
487+ --disable-multilib
488+ --enable-newlib-retargetable-locking)
489+ else ()
490+ # Taken from https://gitlab.arm.com/tooling/gnu-devtools-for-arm/-/blob/9e3db8c042400c9c59686dec8eda2e8c44e5fb5d/build-baremetal-toolchain.sh#L582
491+ set (newlib_flags
492+ --disable-newlib-supplied-syscalls
493+ --disable-nls
494+ --enable-lite-exit
495+ --disable-multilib
496+ --enable-newlib-retargetable-locking
497+ --enable-newlib-nano-malloc
498+ --disable-newlib-unbuf-stream-opt
499+ --enable-newlib-reent-small
500+ --disable-newlib-fseek-optimization
501+ --enable-newlib-nano-formatted-io
502+ --disable-newlib-fvwrite-in-streamio
503+ --disable-newlib-wide-orient
504+ --enable-lite-exit
505+ --enable-newlib-global -atexit)
506+ endif ()
507+ else ()
508+ set (newlib_flags
509+ --enable-newlib-io-long-long
510+ --enable-newlib-register-fini
511+ --disable-newlib-supplied-syscalls
512+ --enable-newlib-io-c99-formats
513+ --disable-nls
514+ --enable-lite-exit
515+ --disable-multilib
516+ --enable-newlib-retargetable-locking)
517+ set (newlib_optim_flags
518+ "-g -O2" )
519+ endif ()
520+
467521 include (${TOOLCHAIN_SOURCE_DIR} /cmake/fetch_newlib.cmake)
468522 set (build_env
469523 "CC_FOR_TARGET=${LLVM_BINARY_DIR} /bin/clang -target ${target_triple} -ffreestanding"
@@ -474,8 +528,8 @@ if(C_LIBRARY STREQUAL newlib)
474528 "RANLIB_FOR_TARGET=${LLVM_BINARY_DIR} /bin/llvm-ranlib"
475529 "READELF_FOR_TARGET=${LLVM_BINARY_DIR} /bin/llvm-readelf"
476530 "STRIP_FOR_TARGET=${LLVM_BINARY_DIR} /bin/llvm-strip"
477- "CFLAGS_FOR_TARGET=${flags} -Wno-error=implicit-function-declaration -D__USES_INITFINI__ -U_HAVE_INIT_FINI --sysroot ${TEMP_LIB_DIR} "
478- "CCASFLAGS=${flags} -Wno-error=implicit-function-declaration -D__USES_INITFINI__ -U_HAVE_INIT_FINI --sysroot ${TEMP_LIB_DIR} "
531+ "CFLAGS_FOR_TARGET=${flags} ${newlib_optim_flags} ${lib_compile_flags} -Wno-error=implicit-function-declaration -D__USES_INITFINI__ -U_HAVE_INIT_FINI"
532+ "CCASFLAGS=${flags} ${newlib_optim_flags} ${lib_compile_flags} -Wno-error=implicit-function-declaration -D__USES_INITFINI__ -U_HAVE_INIT_FINI"
479533 )
480534
481535 include (ProcessorCount)
@@ -486,11 +540,11 @@ if(C_LIBRARY STREQUAL newlib)
486540 endif ()
487541
488542 ExternalProject_Add(
489- newlib
490- STAMP_DIR ${PROJECT_PREFIX} /newlib /${VARIANT_BUILD_ID} /stamp
491- BINARY_DIR ${PROJECT_PREFIX} /newlib /${VARIANT_BUILD_ID} /build
492- DOWNLOAD_DIR ${PROJECT_PREFIX} /newlib /${VARIANT_BUILD_ID} /dl
493- TMP_DIR ${PROJECT_PREFIX} /newlib /${VARIANT_BUILD_ID} /tmp
543+ ${C_LIBRARY}
544+ STAMP_DIR ${PROJECT_PREFIX} /${C_LIBRARY} /${VARIANT_BUILD_ID} /stamp
545+ BINARY_DIR ${PROJECT_PREFIX} /${C_LIBRARY} /${VARIANT_BUILD_ID} /build
546+ DOWNLOAD_DIR ${PROJECT_PREFIX} /${C_LIBRARY} /${VARIANT_BUILD_ID} /dl
547+ TMP_DIR ${PROJECT_PREFIX} /${C_LIBRARY} /${VARIANT_BUILD_ID} /tmp
494548 SOURCE_DIR ${newlib_SOURCE_DIR}
495549 INSTALL_DIR ${TEMP_LIB_DIR}
496550 CONFIGURE_COMMAND
@@ -499,14 +553,7 @@ if(C_LIBRARY STREQUAL newlib)
499553 --target =${target_triple}
500554 --prefix "${TEMP_LIB_DIR} "
501555 --exec_prefix <BINARY_DIR>/tmpinstall
502- --enable-newlib-io-long-long
503- --enable-newlib-register-fini
504- --disable-newlib-supplied-syscalls
505- --enable-newlib-io-c99-formats
506- --disable-nls
507- --enable-lite-exit
508- --disable-multilib
509- --enable-newlib-retargetable-locking
556+ ${newlib_flags}
510557 BUILD_COMMAND
511558 ${CMAKE_COMMAND} -E env ${build_env}
512559 make ${make_flags}
@@ -690,19 +737,34 @@ if(ENABLE_CXX_LIBS)
690737 -DLLVM_LIT_ARGS=${cxxlibs_lit_args}
691738 )
692739 endif ()
693- elseif (C_LIBRARY STREQUAL newlib)
740+ elseif (C_LIBRARY MATCHES "^ newlib" )
694741 set (cxxlibs_extra_cmake_options
695742 -DLIBCXXABI_ENABLE_THREADS=OFF
696743 -DLIBCXX_ENABLE_MONOTONIC_CLOCK=OFF
697744 -DLIBCXX_ENABLE_RANDOM_DEVICE=OFF
698745 -DLIBCXX_ENABLE_THREADS=OFF
699- -DLIBCXX_ENABLE_WIDE_CHARACTERS=ON
700746 -DLIBUNWIND_ENABLE_THREADS=OFF
701747 -DLIBCXXABI_ENABLE_EXCEPTIONS=${ENABLE_EXCEPTIONS}
702748 -DLIBCXXABI_ENABLE_STATIC_UNWINDER=${ENABLE_EXCEPTIONS}
703749 -DLIBCXX_ENABLE_EXCEPTIONS=${ENABLE_EXCEPTIONS}
704750 -DLIBCXX_ENABLE_RTTI=${ENABLE_RTTI}
705751 )
752+
753+ if (C_LIBRARY STREQUAL newlib-nano)
754+ set (lib_compile_flags "${lib_compile_flags} -g -Oz" )
755+ endif ()
756+
757+ if (C_LIBRARY STREQUAL newlib-nano AND NOT TARGET_ARCH MATCHES "^aarch64" )
758+ set (cxxlibs_extra_cmake_options
759+ ${cxxlibs_extra_cmake_options}
760+ -DLIBCXX_ENABLE_WIDE_CHARACTERS=OFF
761+ )
762+ else ()
763+ set (cxxlibs_extra_cmake_options
764+ ${cxxlibs_extra_cmake_options}
765+ -DLIBCXX_ENABLE_WIDE_CHARACTERS=ON
766+ )
767+ endif ()
706768 endif ()
707769
708770 ExternalProject_Add(
0 commit comments