Imre Kis | dd15411 | 2021-10-08 11:21:14 +0200 | [diff] [blame] | 1 | #------------------------------------------------------------------------------- |
Gyorgy Szing | 51f45d2 | 2021-12-08 00:44:18 +0000 | [diff] [blame] | 2 | # Copyright (c) 2021-2022, Arm Limited and Contributors. All rights reserved. |
Imre Kis | dd15411 | 2021-10-08 11:21:14 +0200 | [diff] [blame] | 3 | # |
| 4 | # SPDX-License-Identifier: BSD-3-Clause |
| 5 | # |
| 6 | #------------------------------------------------------------------------------- |
| 7 | |
Gyorgy Szing | 51f45d2 | 2021-12-08 00:44:18 +0000 | [diff] [blame] | 8 | # Add newlib specific porting files to the project. |
Gyorgy Szing | 693877e | 2021-12-12 02:51:10 +0100 | [diff] [blame] | 9 | if (NOT DEFINED TGT) |
| 10 | message(FATAL_ERROR "mandatory parameter TGT is not defined.") |
| 11 | endif() |
| 12 | |
Imre Kis | 217192d | 2021-12-17 14:04:43 +0100 | [diff] [blame] | 13 | # Adding libc interface |
| 14 | add_components(TARGET ${TGT} |
| 15 | BASE_DIR ${TS_ROOT} |
| 16 | COMPONENTS |
| 17 | components/common/libc |
| 18 | ) |
| 19 | |
Gyorgy Szing | 693877e | 2021-12-12 02:51:10 +0100 | [diff] [blame] | 20 | # Compile TS specific newlib porting files. |
| 21 | target_sources(${TGT} PRIVATE |
| 22 | "${CMAKE_CURRENT_LIST_DIR}/newlib_init.c" |
| 23 | "${CMAKE_CURRENT_LIST_DIR}/newlib_sp_assert.c" |
| 24 | "${CMAKE_CURRENT_LIST_DIR}/newlib_sp_heap.c" |
| 25 | ) |
| 26 | |
Gyorgy Szing | 51f45d2 | 2021-12-08 00:44:18 +0000 | [diff] [blame] | 27 | # Fetch newlib from external repository |
| 28 | set(NEWLIB_URL "https://sourceware.org/git/newlib-cygwin.git" |
| 29 | CACHE STRING "newlib repository URL") |
| 30 | set(NEWLIB_REFSPEC "newlib-4.1.0" |
| 31 | CACHE STRING "newlib git refspec") |
| 32 | set(NEWLIB_SOURCE_DIR "${CMAKE_CURRENT_BINARY_DIR}/_deps/newlib-src" |
| 33 | CACHE PATH "newlib source-code location") |
| 34 | set(NEWLIB_INSTALL_DIR "${CMAKE_CURRENT_BINARY_DIR}/newlib_install" |
| 35 | CACHE PATH "newlib installation directory") |
Imre Kis | dd15411 | 2021-10-08 11:21:14 +0200 | [diff] [blame] | 36 | |
Gyorgy Szing | 693877e | 2021-12-12 02:51:10 +0100 | [diff] [blame] | 37 | # Extracting compiler prefix without the trailing hyphen from the C compiler name |
Imre Kis | dd15411 | 2021-10-08 11:21:14 +0200 | [diff] [blame] | 38 | get_filename_component(COMPILER_PATH ${CMAKE_C_COMPILER} DIRECTORY) |
| 39 | get_filename_component(COMPILER_NAME ${CMAKE_C_COMPILER} NAME) |
Imre Kis | 2b7ef37 | 2022-01-04 12:36:12 +0100 | [diff] [blame] | 40 | string(REGEX REPLACE "(.*)-[^-]+$" "\\1" COMPILER_PREFIX ${COMPILER_NAME}) |
Imre Kis | dd15411 | 2021-10-08 11:21:14 +0200 | [diff] [blame] | 41 | |
Gyorgy Szing | 51f45d2 | 2021-12-08 00:44:18 +0000 | [diff] [blame] | 42 | find_library(NEWLIB_LIBC_PATH |
| 43 | NAMES libc.a c.a libc.lib c.lib |
| 44 | PATHS ${NEWLIB_INSTALL_DIR} |
| 45 | PATH_SUFFIXES "${COMPILER_PREFIX}/lib" |
| 46 | DOC "Location of newlib::libc library." |
| 47 | NO_DEFAULT_PATH |
Imre Kis | dd15411 | 2021-10-08 11:21:14 +0200 | [diff] [blame] | 48 | ) |
Gyorgy Szing | 51f45d2 | 2021-12-08 00:44:18 +0000 | [diff] [blame] | 49 | set(NEWLIB_LIBC_PATH ${NEWLIB_LIBC_PATH}) |
| 50 | unset(NEWLIB_LIBC_PATH CACHE) |
Imre Kis | dd15411 | 2021-10-08 11:21:14 +0200 | [diff] [blame] | 51 | |
Gyorgy Szing | 51f45d2 | 2021-12-08 00:44:18 +0000 | [diff] [blame] | 52 | find_library(NEWLIB_LIBNOSYS_PATH |
| 53 | NAMES libnosys.a nosys.a nosys.lib nosys.lib |
| 54 | PATHS ${NEWLIB_INSTALL_DIR} |
| 55 | PATH_SUFFIXES "${COMPILER_PREFIX}/lib" |
| 56 | DOC "Location of newlib::libnosys library." |
| 57 | NO_DEFAULT_PATH |
| 58 | ) |
Gyorgy Szing | f7a91e7 | 2022-02-09 11:59:07 +0100 | [diff] [blame] | 59 | set(NEWLIB_LIBNOSYS_PATH ${NEWLIB_LIBNOSYS_PATH}) |
Gyorgy Szing | 51f45d2 | 2021-12-08 00:44:18 +0000 | [diff] [blame] | 60 | unset(NEWLIB_LIBNOSYS_PATH CACHE) |
| 61 | |
Gyorgy Szing | 69a10c3 | 2022-03-02 21:10:20 +0000 | [diff] [blame] | 62 | # -noxxx options require explicitly specifying GCC specific library on the |
Gyorgy Szing | 309e971 | 2022-02-24 18:35:28 +0100 | [diff] [blame] | 63 | # linker command line. |
Gyorgy Szing | 69a10c3 | 2022-03-02 21:10:20 +0000 | [diff] [blame] | 64 | # Use LIBGCC_PATH to manually override the search process below. |
| 65 | if (NOT DEFINED LIBGCC_PATH) |
| 66 | include(${TS_ROOT}/tools/cmake/compiler/GCC.cmake) |
| 67 | gcc_get_lib_location(LIBRARY_NAME "libgcc.a" RES _TMP_VAR) |
| 68 | |
| 69 | if (NOT _TMP_VAR) |
| 70 | message(FATAL_ERROR "Location of libgcc.a can not be determined. Please set LIBGCC_PATH on the command line.") |
| 71 | endif() |
| 72 | set(LIBGCC_PATH ${_TMP_VAR} CACHE PATH "location of libgcc.a") |
| 73 | unset(_TMP_VAR) |
| 74 | endif() |
| 75 | |
| 76 | if(NOT EXISTS "${LIBGCC_PATH}" OR IS_DIRECTORY "${LIBGCC_PATH}") |
| 77 | message(FATAL_ERROR "LIBGCC_PATH \"${LIBGCC_PATH}\" must be the full path of a library file." |
| 78 | " Either set LIBGCC_PATH on the command line, or fix the value if already set.") |
| 79 | endif() |
| 80 | message(STATUS "libgcc.a is used from ${LIBGCC_PATH}") |
| 81 | |
Gyorgy Szing | 309e971 | 2022-02-24 18:35:28 +0100 | [diff] [blame] | 82 | # Moreover the GCC specific header file include directory is also required. |
Gyorgy Szing | 69a10c3 | 2022-03-02 21:10:20 +0000 | [diff] [blame] | 83 | # Specify LIBGCC_INCLUDE_DIRS in the command line to manually override the libgcc relative location below. |
| 84 | if(NOT DEFINED LIBGCC_INCLUDE_DIRS) |
| 85 | get_filename_component(_TMP_VAR "${LIBGCC_PATH}" DIRECTORY) |
| 86 | set(LIBGCC_INCLUDE_DIRS |
| 87 | "${_TMP_VAR}/include" |
| 88 | "${_TMP_VAR}/include-fixed" CACHE STRING "GCC specific include PATHs") |
| 89 | unset(_TMP_VAR) |
| 90 | endif() |
| 91 | |
Gyorgy Szing | 309e971 | 2022-02-24 18:35:28 +0100 | [diff] [blame] | 92 | # There is no way to stop cmake from filtering out built in compiler include paths |
| 93 | # from compiler command line (see https://gitlab.kitware.com/cmake/cmake/-/issues/19227) |
| 94 | # As a workaround copy headers to build directory and set include path to the new |
| 95 | # location. |
Gyorgy Szing | 69a10c3 | 2022-03-02 21:10:20 +0000 | [diff] [blame] | 96 | foreach(_dir IN LISTS LIBGCC_INCLUDE_DIRS) |
| 97 | if(NOT IS_DIRECTORY "${_dir}") |
| 98 | message(FATAL_ERROR "GCC specific include PATH \"${_dir}\" does not exist. Try setting LIBGCC_INCLUDE_DIRS" |
| 99 | " on the command line.") |
| 100 | endif() |
| 101 | file(COPY "${_dir}" DESTINATION "${CMAKE_CURRENT_BINARY_DIR}/gcc-include") |
| 102 | get_filename_component(_TMP_VAR "${_dir}" NAME) |
| 103 | list(APPEND _gcc_include_dirs "${CMAKE_CURRENT_BINARY_DIR}/gcc-include/${_TMP_VAR}") |
| 104 | message(STATUS "Using compiler specific include path \"${_dir}\" mirrored to" |
| 105 | " \"${CMAKE_CURRENT_BINARY_DIR}/gcc-include/${_TMP_VAR}\".") |
| 106 | endforeach() |
Gyorgy Szing | 309e971 | 2022-02-24 18:35:28 +0100 | [diff] [blame] | 107 | unset(_TMP_VAR) |
| 108 | |
Gyorgy Szing | 51f45d2 | 2021-12-08 00:44:18 +0000 | [diff] [blame] | 109 | if (NOT NEWLIB_LIBC_PATH) |
| 110 | # Determine the number of processes to run while running parallel builds. |
| 111 | # Pass -DPROCESSOR_COUNT=<n> to cmake to override. |
| 112 | if(NOT DEFINED PROCESSOR_COUNT) |
| 113 | include(ProcessorCount) |
| 114 | ProcessorCount(PROCESSOR_COUNT) |
| 115 | set(PROCESSOR_COUNT ${PROCESSOR_COUNT} |
| 116 | CACHE STRING "Number of cores to use for parallel builds.") |
| 117 | endif() |
| 118 | |
| 119 | # See if the source is available locally |
| 120 | find_file(NEWLIB_HEADER_FILE |
| 121 | NAMES newlib.h |
| 122 | PATHS ${NEWLIB_SOURCE_DIR} |
| 123 | PATH_SUFFIXES "newlib/libc/include" |
| 124 | NO_DEFAULT_PATH |
| 125 | ) |
| 126 | set(NEWLIB_HEADER_FILE ${NEWLIB_HEADER_FILE}) |
| 127 | unset(NEWLIB_HEADER_FILE CACHE) |
| 128 | |
| 129 | # Source not found, fetch it. |
| 130 | if (NOT NEWLIB_HEADER_FILE) |
| 131 | include(FetchContent) |
| 132 | |
| 133 | # Checking git |
| 134 | find_program(GIT_COMMAND "git") |
| 135 | if (NOT GIT_COMMAND) |
| 136 | message(FATAL_ERROR "Please install git") |
| 137 | endif() |
| 138 | |
Gyorgy Szing | b202de3 | 2022-09-15 21:37:17 +0200 | [diff] [blame] | 139 | # List patch files. |
| 140 | file(GLOB _patch_files LIST_DIRECTORIES false "${CMAKE_CURRENT_LIST_DIR}/[0-9]*-[!0-9]*.patch") |
| 141 | # Sort items in natural order to ensure patches are amended in the right order. |
| 142 | list(SORT _patch_files COMPARE NATURAL) |
| 143 | # Convert the list to a string of concatenated quoted list items. |
| 144 | string(REPLACE ";" "\" \"" _patch_files "${_patch_files}") |
| 145 | set(_patch_files "\"${_patch_files}\"") |
| 146 | # Create a shell script pathching newlib witht the files listed above |
| 147 | string(APPEND _patch_script "#!/bin/sh\n" |
| 148 | " ${GIT_COMMAND} stash\n" |
| 149 | " ${GIT_COMMAND} branch ts-bf-am\n" |
| 150 | " ${GIT_COMMAND} am ${_patch_files}\n" |
| 151 | " ${GIT_COMMAND} reset ts-bf-am\n" |
| 152 | " ${GIT_COMMAND} branch -D ts-bf-am\n" |
| 153 | ) |
| 154 | file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/patch-newlib "${_patch_script}") |
| 155 | |
Gyorgy Szing | 51f45d2 | 2021-12-08 00:44:18 +0000 | [diff] [blame] | 156 | # Fetching newlib |
| 157 | FetchContent_Declare( |
| 158 | newlib |
| 159 | SOURCE_DIR ${NEWLIB_SOURCE_DIR} |
| 160 | GIT_REPOSITORY ${NEWLIB_URL} |
| 161 | GIT_TAG ${NEWLIB_REFSPEC} |
Julian Hall | a628af3 | 2022-04-01 10:08:18 +0100 | [diff] [blame] | 162 | GIT_SHALLOW FALSE |
Gyorgy Szing | b202de3 | 2022-09-15 21:37:17 +0200 | [diff] [blame] | 163 | PATCH_COMMAND sh ${CMAKE_CURRENT_BINARY_DIR}/patch-newlib |
Gyorgy Szing | 51f45d2 | 2021-12-08 00:44:18 +0000 | [diff] [blame] | 164 | ) |
| 165 | |
| 166 | # FetchContent_GetProperties exports newlib_SOURCE_DIR and newlib_BINARY_DIR variables |
| 167 | FetchContent_GetProperties(newlib) |
| 168 | # FetchContent_Populate will fail if the source directory is removed since it will try to |
| 169 | # do an "update" and not a "populate" action. As a workaround, remove the subbuild directory. |
| 170 | # Note: this fix assumes, the default subbuild location is used. |
| 171 | file(REMOVE_RECURSE "${CMAKE_CURRENT_BINARY_DIR}/_deps/newlib-subbuild") |
| 172 | |
| 173 | if(NOT newlib_POPULATED) |
| 174 | message(STATUS "Fetching newlib") |
| 175 | FetchContent_Populate(newlib) |
| 176 | endif() |
| 177 | set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS ${NEWLIB_SOURCE_DIR}) |
| 178 | endif() |
| 179 | |
Gyorgy Szing | 69a10c3 | 2022-03-02 21:10:20 +0000 | [diff] [blame] | 180 | string(REPLACE ";" " -isystem " CFLAGS_FOR_TARGET "${_gcc_include_dirs}") |
Gyorgy Szing | 309e971 | 2022-02-24 18:35:28 +0100 | [diff] [blame] | 181 | set(CFLAGS_FOR_TARGET "-isystem ${CFLAGS_FOR_TARGET} -fpic") |
| 182 | |
Gyorgy Szing | b202de3 | 2022-09-15 21:37:17 +0200 | [diff] [blame] | 183 | # Split a newlib extra build parameter into a list of parameters |
| 184 | set(NEWLIB_EXTRAS ${NEWLIB_EXTRA}) |
| 185 | separate_arguments(NEWLIB_EXTRAS) |
| 186 | |
Gyorgy Szing | 51f45d2 | 2021-12-08 00:44:18 +0000 | [diff] [blame] | 187 | # Newlib configure step |
| 188 | # CC env var must be unset otherwise configure will assume the cross compiler is the host |
| 189 | # compiler. |
| 190 | # The configure options are set to minimize code size and memory usage. |
| 191 | execute_process(COMMAND |
| 192 | ${CMAKE_COMMAND} -E env --unset=CC PATH=${COMPILER_PATH}:$ENV{PATH} ./configure |
| 193 | --target=${COMPILER_PREFIX} |
Gyorgy Szing | b202de3 | 2022-09-15 21:37:17 +0200 | [diff] [blame] | 194 | --host=${COMPILER_PREFIX} |
Gyorgy Szing | 51f45d2 | 2021-12-08 00:44:18 +0000 | [diff] [blame] | 195 | --prefix=${NEWLIB_INSTALL_DIR} |
| 196 | --enable-newlib-nano-formatted-io |
| 197 | --enable-newlib-nano-malloc |
| 198 | --enable-lite-exit |
| 199 | --enable-newlib-reent-small |
| 200 | --enable-newlib-global-atexit |
| 201 | --disable-multilib |
Gyorgy Szing | b202de3 | 2022-09-15 21:37:17 +0200 | [diff] [blame] | 202 | ${NEWLIB_EXTRAS} |
Gyorgy Szing | 309e971 | 2022-02-24 18:35:28 +0100 | [diff] [blame] | 203 | CFLAGS_FOR_TARGET=${CFLAGS_FOR_TARGET} |
Gyorgy Szing | 51f45d2 | 2021-12-08 00:44:18 +0000 | [diff] [blame] | 204 | LDFLAGS_FOR_TARGET=-fpie |
| 205 | WORKING_DIRECTORY |
| 206 | ${NEWLIB_SOURCE_DIR} |
| 207 | RESULT_VARIABLE _newlib_error |
| 208 | ) |
| 209 | |
| 210 | if (_newlib_error) |
| 211 | message(FATAL_ERROR "Configuration step of newlib failed with ${_newlib_error}.") |
| 212 | endif() |
| 213 | |
| 214 | # Newlib build step |
| 215 | execute_process(COMMAND |
| 216 | ${CMAKE_COMMAND} -E env --unset=CC PATH=${COMPILER_PATH}:$ENV{PATH} |
| 217 | make -j${PROCESSOR_COUNT} |
| 218 | WORKING_DIRECTORY |
| 219 | ${NEWLIB_SOURCE_DIR} |
| 220 | RESULT_VARIABLE _newlib_error |
| 221 | ) |
| 222 | |
| 223 | if (_newlib_error) |
| 224 | message(FATAL_ERROR "Build step of newlib failed with ${_newlib_error}.") |
| 225 | endif() |
| 226 | |
| 227 | # Newlib install step |
| 228 | execute_process(COMMAND |
| 229 | ${CMAKE_COMMAND} -E env --unset=CC PATH=${COMPILER_PATH}:$ENV{PATH} make install |
| 230 | WORKING_DIRECTORY |
| 231 | ${NEWLIB_SOURCE_DIR} |
| 232 | RESULT_VARIABLE _newlib_error |
| 233 | ) |
| 234 | |
| 235 | if (_newlib_error) |
| 236 | message(FATAL_ERROR "Install step of newlib failed with ${_newlib_error}.") |
| 237 | endif() |
| 238 | |
| 239 | set(NEWLIB_LIBC_PATH "${NEWLIB_INSTALL_DIR}/${COMPILER_PREFIX}/lib/libc.a") |
| 240 | set(NEWLIB_LIBNOSYS_PATH "${NEWLIB_INSTALL_DIR}/${COMPILER_PREFIX}/lib/libnosys.a") |
Imre Kis | dd15411 | 2021-10-08 11:21:14 +0200 | [diff] [blame] | 241 | endif() |
| 242 | |
Gyorgy Szing | 51f45d2 | 2021-12-08 00:44:18 +0000 | [diff] [blame] | 243 | set_property(DIRECTORY ${CMAKE_SOURCE_DIR} |
| 244 | APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS ${NEWLIB_LIBC_PATH}) |
Imre Kis | dd15411 | 2021-10-08 11:21:14 +0200 | [diff] [blame] | 245 | |
| 246 | # libc |
| 247 | add_library(c STATIC IMPORTED) |
Gyorgy Szing | 69a10c3 | 2022-03-02 21:10:20 +0000 | [diff] [blame] | 248 | link_libraries(c INTERFACE ${LIBGCC_PATH}) |
Gyorgy Szing | 51f45d2 | 2021-12-08 00:44:18 +0000 | [diff] [blame] | 249 | set_property(TARGET c PROPERTY IMPORTED_LOCATION "${NEWLIB_LIBC_PATH}") |
| 250 | set_property(TARGET c PROPERTY |
| 251 | INTERFACE_INCLUDE_DIRECTORIES "${NEWLIB_INSTALL_DIR}/${COMPILER_PREFIX}/include") |
Gyorgy Szing | 693877e | 2021-12-12 02:51:10 +0100 | [diff] [blame] | 252 | target_compile_options(c INTERFACE -nostdinc) |
Gyorgy Szing | 8fad9bc | 2021-12-12 03:56:41 +0100 | [diff] [blame] | 253 | target_compile_definitions(c INTERFACE ENABLE_CDEFSH_FIX) |
Gyorgy Szing | 693877e | 2021-12-12 02:51:10 +0100 | [diff] [blame] | 254 | target_link_options(c INTERFACE -nostartfiles -nodefaultlibs) |
Gyorgy Szing | 69a10c3 | 2022-03-02 21:10:20 +0000 | [diff] [blame] | 255 | target_include_directories(c SYSTEM INTERFACE ${_gcc_include_dirs}) |
Gyorgy Szing | 309e971 | 2022-02-24 18:35:28 +0100 | [diff] [blame] | 256 | |
| 257 | # Make standard library available in the build system. |
| 258 | add_library(stdlib::c ALIAS c) |
Imre Kis | dd15411 | 2021-10-08 11:21:14 +0200 | [diff] [blame] | 259 | |
| 260 | # libnosys |
| 261 | add_library(nosys STATIC IMPORTED) |
Gyorgy Szing | 51f45d2 | 2021-12-08 00:44:18 +0000 | [diff] [blame] | 262 | set_property(TARGET nosys PROPERTY IMPORTED_LOCATION "${NEWLIB_LIBNOSYS_PATH}") |
| 263 | set_property(TARGET nosys PROPERTY |
| 264 | INTERFACE_INCLUDE_DIRECTORIES "${NEWLIB_INSTALL_DIR}/${COMPILER_PREFIX}/include") |
Gyorgy Szing | 693877e | 2021-12-12 02:51:10 +0100 | [diff] [blame] | 265 | target_compile_options(nosys INTERFACE -nostdinc) |
| 266 | target_link_options(nosys INTERFACE -nostartfiles -nodefaultlibs) |
| 267 | target_link_libraries(c INTERFACE nosys) |