From 5cfebad3986c17ac07a6c978c5372ff99efd716a Mon Sep 17 00:00:00 2001 From: Shane Lawrence Date: Mon, 23 Dec 2024 16:54:53 +0000 Subject: [PATCH 1/2] Add RelWithDebInfo target to produce release binary with separate debug symbols file. Signed-off-by: Shane Lawrence --- cmake/modules/CompilerFlags.cmake | 18 ++++++++++++++++++ userspace/falco/CMakeLists.txt | 14 ++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/cmake/modules/CompilerFlags.cmake b/cmake/modules/CompilerFlags.cmake index 62ed575810a..70c7f874982 100644 --- a/cmake/modules/CompilerFlags.cmake +++ b/cmake/modules/CompilerFlags.cmake @@ -23,6 +23,9 @@ endif() string(TOLOWER "${CMAKE_BUILD_TYPE}" CMAKE_BUILD_TYPE) if(CMAKE_BUILD_TYPE STREQUAL "debug") set(KBUILD_FLAGS "${FALCO_EXTRA_DEBUG_FLAGS} ${FALCO_EXTRA_FEATURE_FLAGS}") +elseif(CMAKE_BUILD_TYPE STREQUAL "relwithdebinfo") + set(KBUILD_FLAGS "${FALCO_EXTRA_FEATURE_FLAGS}") + add_definitions(-DBUILD_TYPE_RELWITHDEBINFO) else() set(CMAKE_BUILD_TYPE "release") set(KBUILD_FLAGS "${FALCO_EXTRA_FEATURE_FLAGS}") @@ -85,6 +88,13 @@ if(NOT MSVC) set(CMAKE_C_FLAGS_RELEASE "-O3 -fno-strict-aliasing -DNDEBUG") set(CMAKE_CXX_FLAGS_RELEASE "-O3 -fno-strict-aliasing -DNDEBUG") + set(CMAKE_C_FLAGS_RELWITHDEBINFO "-O3 -fno-strict-aliasing -DNDEBUG -g") + set(CMAKE_CXX_FLAGS_RELWITHDEBINFO "-O3 -fno-strict-aliasing -DNDEBUG -g") + + # Add linker flags to generate separate debug files + set(CMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO "${CMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO} -Wl,--build-id") + set(CMAKE_SHARED_LINKER_FLAGS_RELWITHDEBINFO "${CMAKE_SHARED_LINKER_FLAGS_RELWITHDEBINFO} -Wl,--build-id") + else() # MSVC set(MINIMAL_BUILD ON) set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$:Debug>") @@ -99,6 +109,11 @@ else() # MSVC set(FALCOSECURITY_LIBS_COMMON_FLAGS "/EHsc /W3 /Zi /std:c++17") set(FALCOSECURITY_LIBS_DEBUG_FLAGS "/MTd /Od") set(FALCOSECURITY_LIBS_RELEASE_FLAGS "/MT") + set(FALCOSECURITY_LIBS_RELWITHDEBINFO_FLAGS "/MT /Zi") + + # Ensure linker generates PDB files for MSVC + set(CMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO "${CMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO} /DEBUG") + set(CMAKE_SHARED_LINKER_FLAGS_RELWITHDEBINFO "${CMAKE_SHARED_LINKER_FLAGS_RELWITHDEBINFO} /DEBUG") set(CMAKE_C_FLAGS "${FALCOSECURITY_LIBS_COMMON_FLAGS}") set(CMAKE_CXX_FLAGS "${FALCOSECURITY_LIBS_COMMON_FLAGS}") @@ -109,4 +124,7 @@ else() # MSVC set(CMAKE_C_FLAGS_RELEASE "${FALCOSECURITY_LIBS_RELEASE_FLAGS}") set(CMAKE_CXX_FLAGS_RELEASE "${FALCOSECURITY_LIBS_RELEASE_FLAGS}") + set(CMAKE_C_FLAGS_RELWITHDEBINFO "${FALCOSECURITY_LIBS_RELWITHDEBINFO_FLAGS}") + set(CMAKE_CXX_FLAGS_RELWITHDEBINFO "${FALCOSECURITY_LIBS_RELWITHDEBINFO_FLAGS}") + endif() diff --git a/userspace/falco/CMakeLists.txt b/userspace/falco/CMakeLists.txt index d6c399059e1..7685b8b0f58 100644 --- a/userspace/falco/CMakeLists.txt +++ b/userspace/falco/CMakeLists.txt @@ -194,6 +194,20 @@ if(MUSL_OPTIMIZED_BUILD AND CMAKE_BUILD_TYPE STREQUAL "release") ) endif() +if(CMAKE_BUILD_TYPE STREQUAL "relwithdebinfo" AND NOT WIN32) + find_program(OBJCOPY_EXECUTABLE NAMES objcopy) + if(OBJCOPY_EXECUTABLE) + add_custom_command(TARGET falco POST_BUILD + COMMAND ${OBJCOPY_EXECUTABLE} --only-keep-debug $ $.debug + COMMAND ${OBJCOPY_EXECUTABLE} --strip-debug --strip-unneeded $ + COMMAND ${OBJCOPY_EXECUTABLE} --add-gnu-debuglink=$.debug $ + COMMENT "Generating separate debug file for falco" + ) + else() + message(WARNING "objcopy not found; separate debug files will not be generated.") + endif() +endif() + if(EMSCRIPTEN) install( FILES "$/falco.js" "$/falco.wasm" From 09418cb7b4d5664d637180e0f206179907512a4a Mon Sep 17 00:00:00 2001 From: Shane Lawrence Date: Mon, 23 Dec 2024 12:38:51 -0500 Subject: [PATCH 2/2] Format cmake files for line length. Signed-off-by: Shane Lawrence --- cmake/modules/CompilerFlags.cmake | 12 +++++++++--- userspace/falco/CMakeLists.txt | 10 +++++++--- 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/cmake/modules/CompilerFlags.cmake b/cmake/modules/CompilerFlags.cmake index 70c7f874982..41829d67f26 100644 --- a/cmake/modules/CompilerFlags.cmake +++ b/cmake/modules/CompilerFlags.cmake @@ -92,8 +92,12 @@ if(NOT MSVC) set(CMAKE_CXX_FLAGS_RELWITHDEBINFO "-O3 -fno-strict-aliasing -DNDEBUG -g") # Add linker flags to generate separate debug files - set(CMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO "${CMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO} -Wl,--build-id") - set(CMAKE_SHARED_LINKER_FLAGS_RELWITHDEBINFO "${CMAKE_SHARED_LINKER_FLAGS_RELWITHDEBINFO} -Wl,--build-id") + set(CMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO + "${CMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO} -Wl,--build-id" + ) + set(CMAKE_SHARED_LINKER_FLAGS_RELWITHDEBINFO + "${CMAKE_SHARED_LINKER_FLAGS_RELWITHDEBINFO} -Wl,--build-id" + ) else() # MSVC set(MINIMAL_BUILD ON) @@ -113,7 +117,9 @@ else() # MSVC # Ensure linker generates PDB files for MSVC set(CMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO "${CMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO} /DEBUG") - set(CMAKE_SHARED_LINKER_FLAGS_RELWITHDEBINFO "${CMAKE_SHARED_LINKER_FLAGS_RELWITHDEBINFO} /DEBUG") + set(CMAKE_SHARED_LINKER_FLAGS_RELWITHDEBINFO + "${CMAKE_SHARED_LINKER_FLAGS_RELWITHDEBINFO} /DEBUG" + ) set(CMAKE_C_FLAGS "${FALCOSECURITY_LIBS_COMMON_FLAGS}") set(CMAKE_CXX_FLAGS "${FALCOSECURITY_LIBS_COMMON_FLAGS}") diff --git a/userspace/falco/CMakeLists.txt b/userspace/falco/CMakeLists.txt index 7685b8b0f58..60cf976de35 100644 --- a/userspace/falco/CMakeLists.txt +++ b/userspace/falco/CMakeLists.txt @@ -197,10 +197,14 @@ endif() if(CMAKE_BUILD_TYPE STREQUAL "relwithdebinfo" AND NOT WIN32) find_program(OBJCOPY_EXECUTABLE NAMES objcopy) if(OBJCOPY_EXECUTABLE) - add_custom_command(TARGET falco POST_BUILD - COMMAND ${OBJCOPY_EXECUTABLE} --only-keep-debug $ $.debug + add_custom_command( + TARGET falco + POST_BUILD + COMMAND ${OBJCOPY_EXECUTABLE} --only-keep-debug $ + $.debug COMMAND ${OBJCOPY_EXECUTABLE} --strip-debug --strip-unneeded $ - COMMAND ${OBJCOPY_EXECUTABLE} --add-gnu-debuglink=$.debug $ + COMMAND ${OBJCOPY_EXECUTABLE} --add-gnu-debuglink=$.debug + $ COMMENT "Generating separate debug file for falco" ) else()