blob: e04f08c141ab946cf298389c69eb251461dcd7ac [file] [log] [blame]
Imre Kisdd154112021-10-08 11:21:14 +02001#-------------------------------------------------------------------------------
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.
10if(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.")
14endif()
15
Gyorgy Szing693877e2021-12-12 02:51:10 +010016if (NOT DEFINED TGT)
17 message(FATAL_ERROR "mandatory parameter TGT is not defined.")
18endif()
19
20# Compile TS specific newlib porting files.
21target_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 Kisdd154112021-10-08 11:21:14 +020027set(NEWLIB_URL "https://sourceware.org/git/newlib-cygwin.git" CACHE STRING "newlib repository URL")
28set(NEWLIB_REFSPEC "newlib-4.1.0" CACHE STRING "newlib git refspec")
29set(NEWLIB_INSTALL_PATH "${CMAKE_CURRENT_BINARY_DIR}/newlib_install" CACHE PATH "newlib installation directory")
30
31include(FetchContent)
32
33# Checking git
34find_program(GIT_COMMAND "git")
35if (NOT GIT_COMMAND)
36 message(FATAL_ERROR "Please install git")
37endif()
38
39# Fetching newlib
40FetchContent_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
51FetchContent_GetProperties(newlib)
52if(NOT newlib_POPULATED)
53 message(STATUS "Fetching newlib")
54 FetchContent_Populate(newlib)
55endif()
56
Gyorgy Szing693877e2021-12-12 02:51:10 +010057# Extracting compiler prefix without the trailing hyphen from the C compiler name
Imre Kisdd154112021-10-08 11:21:14 +020058get_filename_component(COMPILER_PATH ${CMAKE_C_COMPILER} DIRECTORY)
59get_filename_component(COMPILER_NAME ${CMAKE_C_COMPILER} NAME)
60string(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.
65execute_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
82if (_newlib_error)
83 message(FATAL_ERROR "Configuration step of newlib failed with ${_newlib_error}.")
84endif()
85
86# Newlib build step
87execute_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
94if (_newlib_error)
95 message(FATAL_ERROR "Build step of newlib failed with ${_newlib_error}.")
96endif()
97
98# Newlib install step
99execute_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
106if (_newlib_error)
107 message(FATAL_ERROR "Install step of newlib failed with ${_newlib_error}.")
108endif()
109
110# libc
111add_library(c STATIC IMPORTED)
112set_property(TARGET c PROPERTY IMPORTED_LOCATION "${NEWLIB_INSTALL_PATH}/${COMPILER_PREFIX}/lib/libc.a")
113set_property(TARGET c PROPERTY INTERFACE_INCLUDE_DIRECTORIES "${NEWLIB_INSTALL_PATH}/${COMPILER_PREFIX}/include")
Gyorgy Szing693877e2021-12-12 02:51:10 +0100114target_compile_options(c INTERFACE -nostdinc)
115target_link_options(c INTERFACE -nostartfiles -nodefaultlibs)
Imre Kisdd154112021-10-08 11:21:14 +0200116
117# libnosys
118add_library(nosys STATIC IMPORTED)
119set_property(TARGET nosys PROPERTY IMPORTED_LOCATION "${NEWLIB_INSTALL_PATH}/${COMPILER_PREFIX}/lib/libnosys.a")
120set_property(TARGET nosys PROPERTY INTERFACE_INCLUDE_DIRECTORIES "${NEWLIB_INSTALL_PATH}/${COMPILER_PREFIX}/include")
Gyorgy Szing693877e2021-12-12 02:51:10 +0100121target_compile_options(nosys INTERFACE -nostdinc)
122target_link_options(nosys INTERFACE -nostartfiles -nodefaultlibs)
123target_link_libraries(c INTERFACE nosys)
124
125# Make standard library available in the build system.
126add_library(stdlib::c ALIAS c)
127
128# -noxxx options above require explicitly naming GCC specific library on the
129# linker command line.
130include(${TS_ROOT}/tools/cmake/compiler/GCC.cmake)
131gcc_get_lib_location(LIBRARY_NAME "libgcc.a" RES _TMP_VAR)
132link_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.
138get_filename_component(_TMP_VAR "${_TMP_VAR}" DIRECTORY)
139file(COPY "${_TMP_VAR}/include" DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/gcc_include)
140file(COPY "${_TMP_VAR}/include-fixed" DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/gcc-include)
141include_directories("${CMAKE_CURRENT_BINARY_DIR}/gcc_include/include")
142include_directories("${CMAKE_CURRENT_BINARY_DIR}/gcc-include/include-fixed")
143unset(_TMP_VAR)