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 | |
| 16 | set(NEWLIB_URL "https://sourceware.org/git/newlib-cygwin.git" CACHE STRING "newlib repository URL") |
| 17 | set(NEWLIB_REFSPEC "newlib-4.1.0" CACHE STRING "newlib git refspec") |
| 18 | set(NEWLIB_INSTALL_PATH "${CMAKE_CURRENT_BINARY_DIR}/newlib_install" CACHE PATH "newlib installation directory") |
| 19 | |
| 20 | include(FetchContent) |
| 21 | |
| 22 | # Checking git |
| 23 | find_program(GIT_COMMAND "git") |
| 24 | if (NOT GIT_COMMAND) |
| 25 | message(FATAL_ERROR "Please install git") |
| 26 | endif() |
| 27 | |
| 28 | # Fetching newlib |
| 29 | FetchContent_Declare( |
| 30 | newlib |
| 31 | GIT_REPOSITORY ${NEWLIB_URL} |
| 32 | GIT_TAG ${NEWLIB_REFSPEC} |
| 33 | GIT_SHALLOW TRUE |
| 34 | PATCH_COMMAND git stash |
| 35 | COMMAND git am ${CMAKE_CURRENT_LIST_DIR}/0001-Allow-aarch64-linux-gcc-to-compile-bare-metal-lib.patch |
| 36 | COMMAND git reset HEAD~1 |
| 37 | ) |
| 38 | |
| 39 | # FetchContent_GetProperties exports newlib_SOURCE_DIR and newlib_BINARY_DIR variables |
| 40 | FetchContent_GetProperties(newlib) |
| 41 | if(NOT newlib_POPULATED) |
| 42 | message(STATUS "Fetching newlib") |
| 43 | FetchContent_Populate(newlib) |
| 44 | endif() |
| 45 | |
| 46 | # Extracing compiler prefix without the trailing hyphen from the C compiler name |
| 47 | get_filename_component(COMPILER_PATH ${CMAKE_C_COMPILER} DIRECTORY) |
| 48 | get_filename_component(COMPILER_NAME ${CMAKE_C_COMPILER} NAME) |
| 49 | string(REGEX REPLACE "([^-]+-[^-]+-[^-]+).*" "\\1" COMPILER_PREFIX ${COMPILER_NAME}) |
| 50 | |
| 51 | # Newlib configure step |
| 52 | # CC env var must be unset otherwise configure will assume the cross compiler is the host compiler |
| 53 | # The configure options are set to minimize code size and memory usage. |
| 54 | execute_process(COMMAND |
| 55 | ${CMAKE_COMMAND} -E env --unset=CC PATH=${COMPILER_PATH}:$ENV{PATH} ./configure |
| 56 | --target=${COMPILER_PREFIX} |
| 57 | --prefix=${NEWLIB_INSTALL_PATH} |
| 58 | --enable-newlib-nano-formatted-io |
| 59 | --enable-newlib-nano-malloc |
| 60 | --enable-lite-exit |
| 61 | --enable-newlib-reent-small |
| 62 | --enable-newlib-global-atexit |
| 63 | --disable-multilib |
| 64 | CFLAGS_FOR_TARGET=-fpic |
| 65 | LDFLAGS_FOR_TARGET=-fpie |
| 66 | WORKING_DIRECTORY |
| 67 | ${newlib_SOURCE_DIR} |
| 68 | RESULT_VARIABLE _newlib_error |
| 69 | ) |
| 70 | |
| 71 | if (_newlib_error) |
| 72 | message(FATAL_ERROR "Configuration step of newlib failed with ${_newlib_error}.") |
| 73 | endif() |
| 74 | |
| 75 | # Newlib build step |
| 76 | execute_process(COMMAND |
| 77 | ${CMAKE_COMMAND} -E env --unset=CC PATH=${COMPILER_PATH}:$ENV{PATH} make -j${PROCESSOR_COUNT} |
| 78 | WORKING_DIRECTORY |
| 79 | ${newlib_SOURCE_DIR} |
| 80 | RESULT_VARIABLE _newlib_error |
| 81 | ) |
| 82 | |
| 83 | if (_newlib_error) |
| 84 | message(FATAL_ERROR "Build step of newlib failed with ${_newlib_error}.") |
| 85 | endif() |
| 86 | |
| 87 | # Newlib install step |
| 88 | execute_process(COMMAND |
| 89 | ${CMAKE_COMMAND} -E env --unset=CC PATH=${COMPILER_PATH}:$ENV{PATH} make install |
| 90 | WORKING_DIRECTORY |
| 91 | ${newlib_SOURCE_DIR} |
| 92 | RESULT_VARIABLE _newlib_error |
| 93 | ) |
| 94 | |
| 95 | if (_newlib_error) |
| 96 | message(FATAL_ERROR "Install step of newlib failed with ${_newlib_error}.") |
| 97 | endif() |
| 98 | |
| 99 | # libc |
| 100 | add_library(c STATIC IMPORTED) |
| 101 | set_property(TARGET c PROPERTY IMPORTED_LOCATION "${NEWLIB_INSTALL_PATH}/${COMPILER_PREFIX}/lib/libc.a") |
| 102 | set_property(TARGET c PROPERTY INTERFACE_INCLUDE_DIRECTORIES "${NEWLIB_INSTALL_PATH}/${COMPILER_PREFIX}/include") |
| 103 | |
| 104 | # libnosys |
| 105 | add_library(nosys STATIC IMPORTED) |
| 106 | set_property(TARGET nosys PROPERTY IMPORTED_LOCATION "${NEWLIB_INSTALL_PATH}/${COMPILER_PREFIX}/lib/libnosys.a") |
| 107 | set_property(TARGET nosys PROPERTY INTERFACE_INCLUDE_DIRECTORIES "${NEWLIB_INSTALL_PATH}/${COMPILER_PREFIX}/include") |