From c406015c389decc4559fd44cb69604ddd24a0ddb Mon Sep 17 00:00:00 2001 From: Julia Vassiliki Date: Mon, 24 Mar 2025 17:44:21 +1100 Subject: [PATCH] cmake: remove default flags added to the compiler Previously, CMake defaults to adding various flags to the compile options, such as `-O3`, `-DNDEBUG`, and `-g`. Here is an excerpt from CMakeCache.txt: //Flags used by the C compiler during RELEASE builds. CMAKE_C_FLAGS_RELEASE:STRING=-O3 -DNDEBUG //Flags used by the C compiler during RELWITHDEBINFO builds. CMAKE_C_FLAGS_RELWITHDEBINFO:STRING=-O2 -g -DNDEBUG We instead change the CMAKE_BUILD_TYPE to 'None' so that CMake doesn't try to be clever and append extra compile flags. Anything that depends on seL4 will be unaffected as CMake variables are scoped. This should have no meaningful change to the kernel's generated code, as the compile flags were being overridden anyway. Relevant parts of the build.ninja file for comparison: - Now, `-DRELEASE=ON`: FLAGS = -march=armv8-a -D__KERNEL_64__ -std=c99 -Wall -Werror -Wstrict-prototypes -Wmissing-prototypes -Wnested-externs -Wmissing-declarations -Wundef -Wpointer-arith -Wno-nonnull -nostdinc -ffreestanding -fno-stack-protector -fno-asynchronous-unwind-tables -fno-common -O2 -nostdlib -fno-pic -fno-pie -mgeneral-regs-only -mno-outline-atomics -E -CC -I/sel4test-manifest/build/kernel/generated_prune - Before, `-DRELEASE=ON`: FLAGS = -march=armv8-a -D__KERNEL_64__ -O3 -DNDEBUG -std=c99 -Wall -Werror -Wstrict-prototypes -Wmissing-prototypes -Wnested-externs -Wmissing-declarations -Wundef -Wpointer-arith -Wno-nonnull -nostdinc -ffreestanding -fno-stack-protector -fno-asynchronous-unwind-tables -fno-common -O2 -nostdlib -fno-pic -fno-pie -mgeneral-regs-only -mno-outline-atomics -E -CC -I/sel4test-manifest/build/kernel/generated_prune Note the lack of -O3 -DNDEBUG options. Signed-off-by: Julia Vassiliki --- CMakeLists.txt | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 01320ea3f..715c275fb 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -247,14 +247,16 @@ if(KernelFWholeProgram) KernelCommonFlags(-fwhole-program) endif() +# Stop CMake from appending various flags such as -O3/-O2, -DNDEBUG, -g, etc. +# The kernel build sidesteps most of the CMake notions of release/debug and +# has its own conceptualisations of the current optimisation levels that +# conflict with CMake appending flags. +set(CMAKE_BUILD_TYPE "None") + if(KernelDebugBuild) + # As we control debug/release mode independently of CMake, + # append the debug information flags manually. KernelCommonFlags(-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)