Imre Kis | dd15411 | 2021-10-08 11:21:14 +0200 | [diff] [blame] | 1 | #------------------------------------------------------------------------------- |
| 2 | # Copyright (c) 2021, Arm Limited and Contributors. All rights reserved. |
| 3 | # |
| 4 | # SPDX-License-Identifier: BSD-3-Clause |
| 5 | # |
| 6 | #------------------------------------------------------------------------------- |
| 7 | |
| 8 | # Determine the number of processes to run while running parallel builds. |
| 9 | # Pass -DPROCESSOR_COUNT=<n> to cmake to override. |
| 10 | if(NOT DEFINED PROCESSOR_COUNT) |
| 11 | include(ProcessorCount) |
| 12 | ProcessorCount(PROCESSOR_COUNT) |
| 13 | set(PROCESSOR_COUNT ${PROCESSOR_COUNT} CACHE STRING "Number of cores to use for parallel builds.") |
| 14 | endif() |
| 15 | |
Gyorgy Szing | 693877e | 2021-12-12 02:51:10 +0100 | [diff] [blame^] | 16 | if (NOT DEFINED TGT) |
| 17 | message(FATAL_ERROR "mandatory parameter TGT is not defined.") |
| 18 | endif() |
| 19 | |
| 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 | |
Imre Kis | dd15411 | 2021-10-08 11:21:14 +0200 | [diff] [blame] | 27 | set(NEWLIB_URL "https://sourceware.org/git/newlib-cygwin.git" CACHE STRING "newlib repository URL") |
| 28 | set(NEWLIB_REFSPEC "newlib-4.1.0" CACHE STRING "newlib git refspec") |
| 29 | set(NEWLIB_INSTALL_PATH "${CMAKE_CURRENT_BINARY_DIR}/newlib_install" CACHE PATH "newlib installation directory") |
| 30 | |
| 31 | include(FetchContent) |
| 32 | |
| 33 | # Checking git |
| 34 | find_program(GIT_COMMAND "git") |
| 35 | if (NOT GIT_COMMAND) |
| 36 | message(FATAL_ERROR "Please install git") |
| 37 | endif() |
| 38 | |
| 39 | # Fetching newlib |
| 40 | FetchContent_Declare( |
| 41 | newlib |
| 42 | GIT_REPOSITORY ${NEWLIB_URL} |
| 43 | GIT_TAG ${NEWLIB_REFSPEC} |
| 44 | GIT_SHALLOW TRUE |
| 45 | PATCH_COMMAND git stash |
| 46 | COMMAND git am ${CMAKE_CURRENT_LIST_DIR}/0001-Allow-aarch64-linux-gcc-to-compile-bare-metal-lib.patch |
| 47 | COMMAND git reset HEAD~1 |
| 48 | ) |
| 49 | |
| 50 | # FetchContent_GetProperties exports newlib_SOURCE_DIR and newlib_BINARY_DIR variables |
| 51 | FetchContent_GetProperties(newlib) |
| 52 | if(NOT newlib_POPULATED) |
| 53 | message(STATUS "Fetching newlib") |
| 54 | FetchContent_Populate(newlib) |
| 55 | endif() |
| 56 | |
Gyorgy Szing | 693877e | 2021-12-12 02:51:10 +0100 | [diff] [blame^] | 57 | # Extracting compiler prefix without the trailing hyphen from the C compiler name |
Imre Kis | dd15411 | 2021-10-08 11:21:14 +0200 | [diff] [blame] | 58 | get_filename_component(COMPILER_PATH ${CMAKE_C_COMPILER} DIRECTORY) |
| 59 | get_filename_component(COMPILER_NAME ${CMAKE_C_COMPILER} NAME) |
| 60 | string(REGEX REPLACE "([^-]+-[^-]+-[^-]+).*" "\\1" COMPILER_PREFIX ${COMPILER_NAME}) |
| 61 | |
| 62 | # Newlib configure step |
| 63 | # CC env var must be unset otherwise configure will assume the cross compiler is the host compiler |
| 64 | # The configure options are set to minimize code size and memory usage. |
| 65 | execute_process(COMMAND |
| 66 | ${CMAKE_COMMAND} -E env --unset=CC PATH=${COMPILER_PATH}:$ENV{PATH} ./configure |
| 67 | --target=${COMPILER_PREFIX} |
| 68 | --prefix=${NEWLIB_INSTALL_PATH} |
| 69 | --enable-newlib-nano-formatted-io |
| 70 | --enable-newlib-nano-malloc |
| 71 | --enable-lite-exit |
| 72 | --enable-newlib-reent-small |
| 73 | --enable-newlib-global-atexit |
| 74 | --disable-multilib |
| 75 | CFLAGS_FOR_TARGET=-fpic |
| 76 | LDFLAGS_FOR_TARGET=-fpie |
| 77 | WORKING_DIRECTORY |
| 78 | ${newlib_SOURCE_DIR} |
| 79 | RESULT_VARIABLE _newlib_error |
| 80 | ) |
| 81 | |
| 82 | if (_newlib_error) |
| 83 | message(FATAL_ERROR "Configuration step of newlib failed with ${_newlib_error}.") |
| 84 | endif() |
| 85 | |
| 86 | # Newlib build step |
| 87 | execute_process(COMMAND |
| 88 | ${CMAKE_COMMAND} -E env --unset=CC PATH=${COMPILER_PATH}:$ENV{PATH} make -j${PROCESSOR_COUNT} |
| 89 | WORKING_DIRECTORY |
| 90 | ${newlib_SOURCE_DIR} |
| 91 | RESULT_VARIABLE _newlib_error |
| 92 | ) |
| 93 | |
| 94 | if (_newlib_error) |
| 95 | message(FATAL_ERROR "Build step of newlib failed with ${_newlib_error}.") |
| 96 | endif() |
| 97 | |
| 98 | # Newlib install step |
| 99 | execute_process(COMMAND |
| 100 | ${CMAKE_COMMAND} -E env --unset=CC PATH=${COMPILER_PATH}:$ENV{PATH} make install |
| 101 | WORKING_DIRECTORY |
| 102 | ${newlib_SOURCE_DIR} |
| 103 | RESULT_VARIABLE _newlib_error |
| 104 | ) |
| 105 | |
| 106 | if (_newlib_error) |
| 107 | message(FATAL_ERROR "Install step of newlib failed with ${_newlib_error}.") |
| 108 | endif() |
| 109 | |
| 110 | # libc |
| 111 | add_library(c STATIC IMPORTED) |
| 112 | set_property(TARGET c PROPERTY IMPORTED_LOCATION "${NEWLIB_INSTALL_PATH}/${COMPILER_PREFIX}/lib/libc.a") |
| 113 | set_property(TARGET c PROPERTY INTERFACE_INCLUDE_DIRECTORIES "${NEWLIB_INSTALL_PATH}/${COMPILER_PREFIX}/include") |
Gyorgy Szing | 693877e | 2021-12-12 02:51:10 +0100 | [diff] [blame^] | 114 | target_compile_options(c INTERFACE -nostdinc) |
| 115 | target_link_options(c INTERFACE -nostartfiles -nodefaultlibs) |
Imre Kis | dd15411 | 2021-10-08 11:21:14 +0200 | [diff] [blame] | 116 | |
| 117 | # libnosys |
| 118 | add_library(nosys STATIC IMPORTED) |
| 119 | set_property(TARGET nosys PROPERTY IMPORTED_LOCATION "${NEWLIB_INSTALL_PATH}/${COMPILER_PREFIX}/lib/libnosys.a") |
| 120 | set_property(TARGET nosys PROPERTY INTERFACE_INCLUDE_DIRECTORIES "${NEWLIB_INSTALL_PATH}/${COMPILER_PREFIX}/include") |
Gyorgy Szing | 693877e | 2021-12-12 02:51:10 +0100 | [diff] [blame^] | 121 | target_compile_options(nosys INTERFACE -nostdinc) |
| 122 | target_link_options(nosys INTERFACE -nostartfiles -nodefaultlibs) |
| 123 | target_link_libraries(c INTERFACE nosys) |
| 124 | |
| 125 | # Make standard library available in the build system. |
| 126 | add_library(stdlib::c ALIAS c) |
| 127 | |
| 128 | # -noxxx options above require explicitly naming GCC specific library on the |
| 129 | # linker command line. |
| 130 | include(${TS_ROOT}/tools/cmake/compiler/GCC.cmake) |
| 131 | gcc_get_lib_location(LIBRARY_NAME "libgcc.a" RES _TMP_VAR) |
| 132 | link_libraries(${_TMP_VAR}) |
| 133 | # Moreover the GCC specific header file include directory is also required. |
| 134 | # There is no way to stop cmake from filtering out built in compiler include paths |
| 135 | # from compiler command line (see https://gitlab.kitware.com/cmake/cmake/-/issues/19227) |
| 136 | # As a workaround copy headers to build directory and set include path to the new |
| 137 | # location. |
| 138 | get_filename_component(_TMP_VAR "${_TMP_VAR}" DIRECTORY) |
| 139 | file(COPY "${_TMP_VAR}/include" DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/gcc_include) |
| 140 | file(COPY "${_TMP_VAR}/include-fixed" DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/gcc-include) |
| 141 | include_directories("${CMAKE_CURRENT_BINARY_DIR}/gcc_include/include") |
| 142 | include_directories("${CMAKE_CURRENT_BINARY_DIR}/gcc-include/include-fixed") |
| 143 | unset(_TMP_VAR) |