forked from seL4/musllibc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CMakeLists.txt
164 lines (149 loc) · 5.18 KB
/
CMakeLists.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#
# Copyright 2017, Data61
# Commonwealth Scientific and Industrial Research Organisation (CSIRO)
# ABN 41 687 119 230.
#
# This software may be distributed and modified according to the terms of
# the BSD 2-Clause license. Note that NO WARRANTY is provided.
# See "LICENSE_BSD2.txt" for details.
#
# @TAG(DATA61_BSD)
#
cmake_minimum_required(VERSION 3.5.1)
project(libmuslc NONE)
set(LIB_MUSLC_PARALLEL_BUILD true CACHE BOOL "Use `make -j` when building libmuslc")
# Map KernelSel4Arch to muslc architecture.
set(
arch_mapping # element format: "KernelSel4Arch[:muslc_arch]"
"ia32:i386"
"x86_64" # use KernelSel4Arch
"aarch32:arm"
"arm_hyp:arm" # arm_hyp is legacy hack that might get removed one day
"aarch64" # use KernelSel4Arch
"riscv32:riscv" # no difference between 32/64 bit
"riscv64:riscv" # no difference between 32/64 bit
)
# Find the tuple with the mapping information. If KernelSel4Arch is not set at
# all, matching will fail.
if(NOT ";${arch_mapping};" MATCHES ";${KernelSel4Arch}(:([^;]*))?;")
message(FATAL_ERROR "unsupported KernelSel4Arch: '${KernelSel4Arch}'")
elseif(CMAKE_MATCH_2)
set(muslc_arch "${CMAKE_MATCH_2}")
else()
set(muslc_arch "${KernelSel4Arch}")
endif()
message(
STATUS "${PROJECT_NAME} architecture: '${muslc_arch}' (from KernelSel4Arch '${KernelSel4Arch}')"
)
set(
LIB_MUSLC_GLOB_SOURCES OFF
CACHE BOOL "This flag causes CMake to add lots of source files to its dependency lists
which slows down its configuration times.
When not making changes in the musllibc directory, it is likely not
necessary to have this enabled."
)
mark_as_advanced(LIB_MUSLC_PARALLEL_BUILD LIB_MUSLC_GLOB_SOURCES)
if(LIB_MUSLC_GLOB_SOURCES)
file(
GLOB_RECURSE
deps
**/*.c
**/*.h
**/*.S
**/*.s
)
endif()
set(parallel "")
if(LIB_MUSLC_PARALLEL_BUILD)
include(ProcessorCount)
ProcessorCount(N)
if(N EQUAL 0)
message(WARNING "Could not detect ProcessorCount for building musllibc.")
else()
# We set tasks to number of CPUs + 2 to account for blocking IO, this is
# a similar heuristic to what Ninja uses by default. Note that this gets
# used in a rule generated by add_custom_command and is additive with
# any existing Ninja jobs.
math(EXPR Nplus2 "${N} + 2")
set(parallel "-j${Nplus2}")
endif()
endif()
# We make an attempt to extract compiler options from CMake
get_property(compile_options DIRECTORY PROPERTY COMPILE_OPTIONS)
# Also inherit the CMAKE build type flags from one of the following variables
# depending on the configured build type:
# - CMAKE_C_FLAGS_DEBUG
# - CMAKE_C_FLAGS_RELEASE
# - CMAKE_C_FLAGS_RELWITHDEBINFO
# - CMAKE_C_FLAGS_MINSIZEREL
string(TOUPPER CMAKE_BUILD_TYPE build_type)
set(cmake_built_type_config_name CMAKE_C_FLAGS_${build_type})
separate_arguments(
cmake_c_flags_sep NATIVE_COMMAND "${CMAKE_C_FLAGS};${${cmake_built_type_config_name}}"
)
list(APPEND compile_options "${cmake_c_flags_sep}")
# Add the target triple to the compile flags if we are using clang
# It is not part of the CMAKE_C_FLAGS variable
if(CMAKE_C_COMPILER_ID STREQUAL "Clang")
list(APPEND compile_options "${CMAKE_C_COMPILE_OPTIONS_TARGET}${CMAKE_C_COMPILER_TARGET}")
endif()
include(memoize)
# memoize this installation rule which will save the resulting artifact in a
# cache and reuse it across builds. This will rebuild from source if the git
# directory has changes or has a changed commit hash.
memoize_add_custom_command(
musllibc
"${CMAKE_CURRENT_BINARY_DIR}/build-temp/stage"
"${CMAKE_CURRENT_SOURCE_DIR}"
"${compile_options} ${muslc_arch}"
""
OUTPUT
build-temp/stage/lib/libc.a
# If we have to rebuild, first clear the temporary build directory as we
# have no correctly captured the output files or dependencies
COMMAND
rm
-r
build-temp
COMMAND
mkdir
-p
build-temp
COMMAND
${CMAKE_COMMAND}
-E
env
TOOLPREFIX=${CROSS_COMPILER_PREFIX}
C_COMPILER=${CMAKE_C_COMPILER}
# Suppress make output by default. Verbose output can now be achieved by
# "MAKEFLAGS=V3 ninja". "${MAKEFLAGS:--s}" expands to "-s" if MAKEFLAGS is
# unset.
"MAKEFLAGS=\"\$\${MAKEFLAGS:--s}\""
# TODO: should also pass in the cflags for the CMAKE_BUILD_TYPE ?
NK_CFLAGS="${compile_options}"
TARGET=${muslc_arch}
make
-C
build-temp
${parallel}
-f
"${CMAKE_CURRENT_SOURCE_DIR}/Makefile"
"STAGE_DIR=${CMAKE_CURRENT_BINARY_DIR}/build-temp/stage"
"SOURCE_DIR=${CMAKE_CURRENT_SOURCE_DIR}"
DEPENDS
${deps}
COMMAND_EXPAND_LISTS
COMMENT
"Invoking muslc build system"
)
add_custom_target(muslc_gen DEPENDS build-temp/stage/lib/libc.a)
add_library(muslc_imported STATIC IMPORTED GLOBAL)
add_dependencies(muslc_imported muslc_gen)
set_property(
TARGET muslc_imported
PROPERTY IMPORTED_LOCATION "${CMAKE_CURRENT_BINARY_DIR}/build-temp/stage/lib/libc.a"
)
add_library(muslc INTERFACE)
add_dependencies(muslc muslc_imported)
set_property(TARGET muslc PROPERTY INTERFACE_LINK_LIBRARIES muslc_imported)
target_include_directories(muslc INTERFACE "${CMAKE_CURRENT_BINARY_DIR}/build-temp/stage/include")