From baf03e97a47330d11c30630fb3cbf77f54e3c141 Mon Sep 17 00:00:00 2001 From: fef Date: Wed, 22 Dec 2021 22:19:09 +0100 Subject: [PATCH] cmake: add cflags utility macros --- CMakeLists.txt | 20 ++++++++++++++------ cmake/util.cmake | 13 +++++++++++++ 2 files changed, 27 insertions(+), 6 deletions(-) create mode 100644 cmake/util.cmake diff --git a/CMakeLists.txt b/CMakeLists.txt index 67d7647..beb6d13 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -19,6 +19,7 @@ if(CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME) endif() include(cmake/git.cmake) +include(cmake/util.cmake) include(cmake/config.cmake) configure_file( @@ -28,10 +29,17 @@ configure_file( include("arch/${ARCH}/config/toolchain.cmake") -# Used by the KASSERT macro for printing the filename relative to the source root -set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -D__FILENAME__='\"$(subst ${CMAKE_SOURCE_DIR}/,,$(abspath $<))\"'") +gay_append_cmake_c_flags( + -ggdb + -pipe + -nostdinc + -ffreestanding + -fno-stack-protector + -Wused-but-marked-unused + # Used by the KASSERT macro for printing the filename relative to the source root + "-D__FILENAME__='\"$(subst ${CMAKE_SOURCE_DIR}/,,$(abspath $<))\"'" +) -set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -pipe -nostdinc -ffreestanding -fno-stack-protector") set(CMAKE_ASM_FLAGS "${CMAKE_ASM_FLAGS} -pipe -D_ASM_SOURCE") if(DEBUG) # -Og on gcc optimizes for the best debugging experience, but apparently this @@ -39,12 +47,12 @@ if(DEBUG) # you're supposed to use -O0 in order to get the most submissive and debuggable # code instead: # - set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -O0 -ggdb") + gay_append_cmake_c_flags(-O0) # i don't think -O0 does anything for assembly code, but it can't hurt to # have it in there just in case (somebody check on this at some point plz) - set(CMAKE_ASM_FLAGS "${CMAKE_ASM_FLAGS} -O0 -ggdb") + gay_append_cmake_asm_flags(-O0) else() - set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -O2 -ggdb") + gay_append_cmake_c_flags(-O2) endif() # --whole-archive ensures no (seemingly) unused symbols diff --git a/cmake/util.cmake b/cmake/util.cmake new file mode 100644 index 0000000..6701f8e --- /dev/null +++ b/cmake/util.cmake @@ -0,0 +1,13 @@ +# Copyright (C) 2021 fef . All rights reserved. + +macro(gay_append_cmake_c_flags) + foreach(flag IN ITEMS ${ARGN}) + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${flag}") + endforeach() +endmacro() + +macro(gay_append_cmake_asm_flags) + foreach(flag IN ITEMS ${ARGN}) + set(CMAKE_ASM_FLAGS "${CMAKE_ASM_FLAGS} ${flag}") + endforeach() +endmacro()