forked from niwis/seL4
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CMakeLists.txt
578 lines (526 loc) · 17.6 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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
#
# 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 GNU General Public License version 2. Note that NO WARRANTY is provided.
# See "LICENSE_GPLv2.txt" for details.
#
# @TAG(DATA61_GPL)
#
cmake_minimum_required(VERSION 3.8.2)
include(CheckCCompilerFlag)
project(seL4 C ASM)
# First find our helpers
find_file(KERNEL_HELPERS_PATH helpers.cmake PATHS tools CMAKE_FIND_ROOT_PATH_BOTH)
mark_as_advanced(FORCE KERNEL_HELPERS_PATH)
include(${KERNEL_HELPERS_PATH})
function(RequireTool config file)
RequireFile("${config}" "${file}" PATHS tools)
endfunction(RequireTool)
RequireTool(KERNEL_FLAGS_PATH flags.cmake)
if(CCACHEFOUND)
set(ccache "ccache")
endif()
include(tools/internal.cmake)
# Define tools used by the kernel
set(PYTHON "python2" CACHE INTERNAL "")
set(PYTHON3 "python3" CACHE INTERNAL "")
RequireTool(CPP_GEN_PATH cpp_gen.sh)
RequireTool(CIRCULAR_INCLUDES circular_includes.py)
RequireTool(BF_GEN_PATH bitfield_gen.py)
RequireTool(HARDWARE_GEN_PATH hardware_gen.py)
RequireTool(INVOCATION_ID_GEN_PATH invocation_header_gen.py)
RequireTool(SYSCALL_ID_GEN_PATH syscall_header_gen.py)
RequireTool(XMLLINT_PATH xmllint.sh)
set(c_sources "")
set(asm_sources "")
set(bf_declarations "")
foreach(file IN LISTS config_c_sources)
list(APPEND c_sources "${CMAKE_CURRENT_SOURCE_DIR}/${file}")
endforeach()
foreach(file IN LISTS config_asm_sources)
list(APPEND asm_sources "${CMAKE_CURRENT_SOURCE_DIR}/${file}")
endforeach()
foreach(file IN LISTS config_bf_declarations)
list(APPEND bf_declarations "${CMAKE_CURRENT_SOURCE_DIR}/${file}")
endforeach()
set(KernelDTSList "${config_KernelDTSList}")
# Process the configuration scripts
include(config.cmake)
# Define default global flag information so that users can compile with the same basic architecture
# flags as the kernel
if(KernelArchX86)
if(${KernelX86MicroArch} STREQUAL generic)
set(build_arch "-mtune=generic")
else()
set(build_arch "-march=${KernelX86MicroArch}")
endif()
if(Kernel64)
if(NOT LLVM_TOOLCHAIN)
string(APPEND asm_common_flags " -Wa,--64")
endif()
string(APPEND c_common_flags " -m64")
else()
if(NOT LLVM_TOOLCHAIN)
string(APPEND asm_common_flags " -Wa,--32")
endif()
string(APPEND c_common_flags " -m32")
endif()
endif()
if(KernelArchARM)
set(arm_march "${KernelArmArmV}${KernelArmMachFeatureModifiers}")
string(APPEND c_common_flags " -march=${arm_march}")
string(APPEND asm_common_flags " -march=${arm_march}")
# Explicitly request ARM instead of THUMB for compilation. This option is not
# relevant on aarch64
if(NOT KernelSel4ArchAarch64)
string(APPEND c_common_flags " -marm")
endif()
endif()
if(KernelArchRiscV)
if(Kernel64)
string(APPEND common_flags " -march=rv64imac")
string(APPEND common_flags " -mabi=lp64")
else()
string(APPEND common_flags " -march=rv32imac")
string(APPEND common_flags " -mabi=ilp32")
endif()
endif()
string(APPEND common_flags " ${build_arch}")
if(Kernel64)
string(APPEND common_flags " -D__KERNEL_64__")
else()
string(APPEND common_flags " -D__KERNEL_32__")
endif()
set(
BASE_ASM_FLAGS "${asm_common_flags} ${common_flags}"
CACHE INTERNAL "Default ASM flags for compilation \
(subset of flags used by the kernel build)"
)
set(
BASE_C_FLAGS "${c_common_flags} ${common_flags}"
CACHE INTERNAL "Default C flags for compilation \
(subset of flags used by the kernel)"
)
set(
BASE_CXX_FLAGS "${cxx_common_flags} ${c_common_flags} ${common_flags}"
CACHE INTERNAL "Default CXX flags for compilation"
)
if(KernelArchX86)
if(Kernel64)
string(APPEND common_exe_flags " -Wl,-m -Wl,elf_x86_64")
else()
string(APPEND common_exe_flags " -Wl,-m -Wl,elf_i386")
endif()
endif()
set(
BASE_EXE_LINKER_FLAGS "${common_flags} ${common_exe_flags} "
CACHE INTERNAL "Default flags for linker an elf binary application"
)
# Initializing the kernel build flags starting from the same base flags that the users will use
include(${KERNEL_FLAGS_PATH})
# Setup kernel specific flags
macro(KernelCommonFlags)
foreach(common_flag IN ITEMS ${ARGV})
add_compile_options(${common_flag})
string(APPEND CMAKE_EXE_LINKER_FLAGS " ${common_flag} ")
endforeach()
endmacro(KernelCommonFlags)
KernelCommonFlags(-nostdinc -nostdlib ${KernelOptimisation} -DHAVE_AUTOCONF)
if(KernelFWholeProgram)
KernelCommonFlags(-fwhole-program)
endif()
if(KernelDebugBuild)
KernelCommonFlags(-DDEBUG -g -ggdb)
# Pretend to CMake that we're a release build with debug info. This is because
# we do actually allow CMake to do the final link step, so we'd like it not to
# strip our binary
set(CMAKE_BUILD_TYPE "RelWithDebInfo")
else()
set(CMAKE_BUILD_TYPE "Release")
endif()
if(KernelArchX86 AND Kernel64)
KernelCommonFlags(-mcmodel=kernel)
endif()
if(KernelArchARM)
if(KernelSel4ArchAarch64)
KernelCommonFlags(-mgeneral-regs-only)
else()
KernelCommonFlags(-mfloat-abi=soft)
endif()
endif()
if(KernelArchRiscV)
KernelCommonFlags(-mcmodel=medany)
endif()
KernelCommonFlags(-fno-pic -fno-pie)
add_compile_options(
-fno-stack-protector
-fno-asynchronous-unwind-tables
-std=c99
-Wall
-Werror
-Wstrict-prototypes
-Wmissing-prototypes
-Wnested-externs
-Wmissing-declarations
-Wundef
-Wpointer-arith
-Wno-nonnull
-ffreestanding
)
# Add all the common flags to the linker args
string(APPEND CMAKE_EXE_LINKER_FLAGS " -ffreestanding -Wl,--build-id=none -static -Wl,-n ")
if(KernelArchX86)
add_compile_options(-mno-mmx -mno-sse -mno-sse2 -mno-3dnow)
endif()
# Sort the C sources to ensure a stable layout of the final C file
list(SORT c_sources)
# Add the domain schedule now that its sorted
list(APPEND c_sources "${KernelDomainSchedule}")
# Add static header includes
include_directories("include")
include_directories("include/${KernelWordSize}")
include_directories("include/arch/${KernelArch}")
include_directories("include/arch/${KernelArch}/arch/${KernelWordSize}/")
include_directories("include/plat/${KernelPlatform}/")
include_directories("include/plat/${KernelPlatform}/plat/${KernelWordSize}/")
if(KernelArchARM)
include_directories("include/arch/arm/armv/${KernelArmArmV}/")
include_directories("include/arch/arm/armv/${KernelArmArmV}/${KernelWordSize}")
endif()
if(KernelArmMach STREQUAL "exynos")
include_directories("include/plat/exynos_common/")
endif()
if(KernelArchRiscV)
include_directories("include/arch/${KernelArch}/arch/${KernelWordSize}")
include_directories("include/plat/${KernelPlatform}/plat/${KernelWordSize}")
endif()
# Add libsel4 include directories.
# These are explicitly added instead of calling target_link_libraries(${target} sel4)
# because we don't want to inherit any other build options from libsel4.
include_directories(
"libsel4/include"
"libsel4/arch_include/${KernelArch}"
"libsel4/sel4_arch_include/${KernelSel4Arch}"
"libsel4/sel4_plat_include/${KernelPlatform}"
"libsel4/mode_include/${KernelWordSize}"
)
#
# Config generation
#
include_directories($<TARGET_PROPERTY:kernel_Config,INTERFACE_INCLUDE_DIRECTORIES>)
# The kernel expects to be able to include an 'autoconf.h' file at the moment. So lets
# generate one for it to use
# TODO: use the kernel_Config directly
generate_autoconf(kernel_autoconf "kernel")
include_directories($<TARGET_PROPERTY:kernel_autoconf,INTERFACE_INCLUDE_DIRECTORIES>)
# Target for the config / autoconf headers. This is what all the other generated headers
# can depend upon
add_custom_target(
kernel_config_headers
DEPENDS
kernel_autoconf_Gen
kernel_autoconf
kernel_Config
kernel_Gen
)
# Target for all generated headers. We start with just all the config / autoconf headers
add_custom_target(kernel_headers DEPENDS kernel_config_headers)
# Build up a list of generated files. needed for dependencies in custom commands
get_generated_files(gen_files_list kernel_autoconf_Gen)
get_generated_files(gen_files2 kernel_Gen)
list(APPEND gen_files_list "${gen_files2}")
#
# C source generation
#
# Kernel compiles all C sources as a single C file, this provides
# rules for doing the concatenation
add_custom_command(
OUTPUT kernel_all.c
COMMAND
"${CPP_GEN_PATH}" ${c_sources} > kernel_all.c
DEPENDS "${CPP_GEN_PATH}" ${c_sources}
COMMENT "Concatenating C files"
VERBATIM
)
add_custom_target(kernel_all_c_wrapper DEPENDS kernel_all.c)
#
# Header Generation
#
# Rules for generating invocation and syscall headers
# Aside from generating file rules for dependencies this section will also produce a target
# that can be depended upon (along with the desired files themselves) to control parallelism
set(xml_headers "")
set(header_dest "gen_headers/arch/api/invocation.h")
gen_invocation_header(
OUTPUT ${header_dest}
XML ${CMAKE_CURRENT_SOURCE_DIR}/libsel4/arch_include/${KernelArch}/interfaces/sel4arch.xml
ARCH
)
list(APPEND xml_headers "${header_dest}")
list(APPEND gen_files_list "${header_dest}")
set(header_dest "gen_headers/arch/api/sel4_invocation.h")
gen_invocation_header(
OUTPUT "${header_dest}"
XML
"${CMAKE_CURRENT_SOURCE_DIR}/libsel4/sel4_arch_include/${KernelSel4Arch}/interfaces/sel4arch.xml"
SEL4ARCH
)
list(APPEND xml_headers "${header_dest}")
list(APPEND gen_files_list "${header_dest}")
set(header_dest "gen_headers/api/invocation.h")
gen_invocation_header(
OUTPUT "${header_dest}"
XML "${CMAKE_CURRENT_SOURCE_DIR}/libsel4/include/interfaces/sel4.xml"
)
list(APPEND xml_headers "${header_dest}")
list(APPEND gen_files_list "${header_dest}")
set(syscall_xml_base "${CMAKE_CURRENT_SOURCE_DIR}/libsel4/include/api")
set(syscall_dest "gen_headers/arch/api/syscall.h")
if(KernelIsMCS)
set(mcs --mcs)
endif()
add_custom_command(
OUTPUT ${syscall_dest}
COMMAND
"${XMLLINT_PATH}"
--noout
--schema "${syscall_xml_base}/syscall.xsd" "${syscall_xml_base}/syscall.xml"
COMMAND
${CMAKE_COMMAND} -E remove -f "${syscall_dest}"
COMMAND
${PYTHON3} "${SYSCALL_ID_GEN_PATH}"
--xml "${syscall_xml_base}/syscall.xml"
--kernel_header "${syscall_dest}" ${mcs}
DEPENDS
"${XMLLINT_PATH}"
"${SYSCALL_ID_GEN_PATH}"
"${syscall_xml_base}/syscall.xsd"
"${syscall_xml_base}/syscall.xml"
COMMENT "Generate syscall invocations"
VERBATIM
)
list(APPEND xml_headers "${syscall_dest}")
list(APPEND gen_files_list "${syscall_dest}")
# Construct target for just the xml headers
add_custom_target(xml_headers_target DEPENDS ${xml_headers})
# Add the xml headers to all the kernel headers
add_dependencies(kernel_headers xml_headers_target)
include_directories("${CMAKE_CURRENT_BINARY_DIR}/gen_headers")
#
# Prune list generation
#
# When generating bitfield files we can pass multiple '--prune' parameters that are source
# files that get searched for determing which bitfield functions are used. This allows the
# bitfield generator to only generate functions that are used. Whilst irrelevant for
# normal compilation, not generating unused functions has significant time savings for the
# automated verification tools
# To generate a prune file we 'build' the kernel (similar to the kernel_all_pp.c rule
# below) but strictly WITHOUT the generated header directory where the bitfield generated
# headers are. This means our preprocessed file will contain all the code used by the
# normal compilation, just without the bitfield headers (which we generate dummy versions of).
# If we allowed the bitfield headers to be included then we would have a circular
# dependency. As a result this rule comes *before* the Bitfield header generation section
set(dummy_headers "")
foreach(bf_dec ${bf_declarations})
string(
REPLACE
":"
";"
bf_dec
${bf_dec}
)
list(GET bf_dec 0 bf_file)
list(GET bf_dec 1 bf_gen_dir)
get_filename_component(bf_name "${bf_file}" NAME)
string(
REPLACE
".bf"
"_gen.h"
bf_target
"${bf_name}"
)
list(
APPEND
dummy_headers "${CMAKE_CURRENT_BINARY_DIR}/generated_prune/${bf_gen_dir}/${bf_target}"
)
endforeach()
add_custom_command(
OUTPUT ${dummy_headers}
COMMAND
${CMAKE_COMMAND} -E touch ${dummy_headers}
COMMENT "Generate dummy headers for prune compilation"
)
add_custom_target(dummy_header_wrapper DEPENDS ${dummy_headers})
cppfile(
kernel_all_pp_prune.c
kernel_all_pp_prune_wrapper
kernel_all.c
EXTRA_FLAGS
-CC
"-I${CMAKE_CURRENT_BINARY_DIR}/generated_prune"
EXTRA_DEPS
kernel_all_c_wrapper
dummy_header_wrapper
xml_headers_target
kernel_config_headers
${gen_files_list}
)
#
# Bitfield header generation
#
# Need to generate a bunch of unique targets, we'll do this with piano numbers
set(bf_gen_target "kernel_bf_gen_target_1")
foreach(bf_dec ${bf_declarations})
string(
REPLACE
":"
";"
bf_dec
${bf_dec}
)
list(GET bf_dec 0 bf_file)
list(GET bf_dec 1 bf_gen_dir)
get_filename_component(bf_name "${bf_file}" NAME)
string(
REPLACE
".bf"
"_gen.h"
bf_target
"${bf_name}"
)
string(
REPLACE
".bf"
"_defs.thy"
defs_target
"${bf_name}"
)
string(
REPLACE
".bf"
"_proofs.thy"
proofs_target
"${bf_name}"
)
set(pbf_name "generated/${bf_gen_dir}/${bf_name}.pbf")
set(pbf_target "${bf_gen_target}_pbf")
cppfile(
"${pbf_name}"
"${pbf_target}"
"${bf_file}"
EXTRA_FLAGS
-P
EXTRA_DEPS
kernel_config_headers
${gen_files_list}
)
GenHBFTarget(
""
${bf_gen_target}
"generated/${bf_gen_dir}/${bf_target}"
"${pbf_name}"
"${pbf_target}"
"kernel_all_pp_prune.c"
"kernel_all_pp_prune_wrapper"
)
GenDefsBFTarget(
"${bf_gen_target}_def"
"generated/${bf_gen_dir}/${defs_target}"
"${pbf_name}"
"${pbf_target}"
"kernel_all_pp_prune.c"
"kernel_all_pp_prune_wrapper"
)
GenProofsBFTarget(
"${bf_gen_target}_proof"
"generated/${bf_gen_dir}/${proofs_target}"
"${pbf_name}"
"${pbf_target}"
"kernel_all_pp_prune.c"
"kernel_all_pp_prune_wrapper"
)
list(
APPEND
theories_deps
"${bf_gen_target}_def"
"${CMAKE_CURRENT_BINARY_DIR}/generated/${bf_gen_dir}/${defs_target}"
"${bf_gen_target}_proof"
"${CMAKE_CURRENT_BINARY_DIR}/generated/${bf_gen_dir}/${proofs_target}"
)
add_dependencies(kernel_headers "${bf_gen_target}")
list(APPEND gen_files_list "${CMAKE_CURRENT_BINARY_DIR}/generated/${bf_gen_dir}/${bf_target}")
set(bf_gen_target "${bf_gen_target}1")
endforeach()
# At this point we have generated a bunch of headers into ${CMAKE_CURRENT_BINARY_DIR}/generated
# but we do not pass this to include_directories, as that will cause it to be an include directory
# for *all* targets in this file (including ones we defined earlier) and the prune generation
# *must not* see this files and generate dependencies on them as this will result in nonsense.
# As such we must manually add this as an include directory to future targets
set(CPPExtraFlags "-I${CMAKE_CURRENT_BINARY_DIR}/generated")
#
# Kernel compilation
#
cppfile(
kernel_all.i
kernel_i_wrapper
kernel_all.c
EXTRA_DEPS
kernel_all_c_wrapper
kernel_headers
${gen_files_list}
EXTRA_FLAGS
-CC
"${CPPExtraFlags}"
# The circular_includes script relies upon parsing out exactly 'kernel_all_copy.c' as
# a special case so we must ask cppfile to use this input name
EXACT_NAME kernel_all_copy.c
)
# Explain to cmake that our object file is actually a C input file
set_property(SOURCE kernel_all.i PROPERTY LANGUAGE C)
if(KernelArchARM)
set(linker_source "src/arch/arm/common_arm.lds")
elseif(KernelArchRiscV)
set(linker_source "src/arch/riscv/common_riscv.lds")
else()
set(linker_source "src/plat/${KernelPlatform}/linker.lds")
endif()
set(linker_lds_path "${CMAKE_CURRENT_BINARY_DIR}/linker.lds_pp")
# Preprocess the linker script
cppfile(
"${linker_lds_path}"
linker_ld_wrapper
"${linker_source}"
EXTRA_DEPS
kernel_headers
${gen_files_list}
EXTRA_FLAGS
-CC
-P
"${CPPExtraFlags}"
)
add_custom_command(
OUTPUT circular_includes_valid
COMMAND ${CIRCULAR_INCLUDES} --ignore kernel_all_copy.c < kernel_all.i
COMMAND touch circular_includes_valid
DEPENDS kernel_i_wrapper kernel_all.i
)
add_custom_target(circular_includes DEPENDS circular_includes_valid)
add_custom_command(
OUTPUT kernel_all_pp.c
COMMAND
${CMAKE_COMMAND} -E copy kernel_all.i kernel_all_pp.c
DEPENDS kernel_i_wrapper kernel_all.i
)
add_custom_target(kernel_all_pp_wrapper DEPENDS kernel_all_pp.c)
add_custom_target(kernel_theories DEPENDS ${theories_deps})
# Declare final kernel output
add_executable(kernel.elf EXCLUDE_FROM_ALL ${asm_sources} kernel_all.c)
target_include_directories(kernel.elf PRIVATE ${config_dir})
target_include_directories(kernel.elf PRIVATE include)
target_include_directories(kernel.elf PRIVATE "${CMAKE_CURRENT_BINARY_DIR}/generated")
target_link_libraries(kernel.elf PRIVATE kernel_Config kernel_autoconf)
set_property(TARGET kernel.elf APPEND_STRING PROPERTY LINK_FLAGS " -Wl,-T ${linker_lds_path} ")
add_dependencies(kernel.elf linker_ld_wrapper circular_includes)