diff --git a/CMakeLists.txt b/CMakeLists.txt index e28ae07..47a1d01 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -8,6 +8,11 @@ set(FETCHCONTENT_QUIET OFF) # the source. See exampleConfig.h.in for some more details. project(shared-state VERSION 0.0.0.1 LANGUAGES CXX) + +if(NOT CMAKE_BUILD_TYPE) + set(CMAKE_BUILD_TYPE Release) +endif() + option( SS_STAT_FILE_LOCKING "Enable shared-state network statistics file locking" OFF ) @@ -25,6 +30,23 @@ option( SS_CPPTRACE_STACKTRACE our implementation" ON ) +option(SS_STATIC_LIBSTDCPP "Statically link libstdc++" ON) +option(SS_STRIP_SYMBOLS "Strip unused symbols from binary" ON) + +set(OPTIMIZATION_FLAGS + "-O3" # Aggressive optimization + "-ffunction-sections" # Place each function in its own section + "-fdata-sections" # Place each data item in its own section + "-fvisibility=hidden" # Hide symbols by default + "-fvisibility-inlines-hidden" # Hide inline function symbols +) +set(LINKER_FLAGS + "-Wl,--gc-sections" + "-Wl,--as-needed" +) +if(SS_STATIC_LIBSTDCPP) + list(APPEND LINKER_FLAGS "-static-libstdc++") +endif() set(LIBRARY_SOURCES src/accept_operation.cc @@ -130,12 +152,14 @@ set(CLI_SOURCES add_library(${LIBRARY_NAME} OBJECT ${LIBRARY_SOURCES}) target_include_directories(${LIBRARY_NAME} PUBLIC ${PROJECT_SOURCE_DIR}/include) # TODO: check if coroutines support has been added to target_compile_features() -target_compile_options(${LIBRARY_NAME} PUBLIC "-fcoroutines") +target_compile_options(${LIBRARY_NAME} PUBLIC "-fcoroutines" ${OPTIMIZATION_FLAGS}) +target_link_options(${LIBRARY_NAME} PUBLIC ${LINKER_FLAGS}) add_executable(${EXECUTABLE_NAME} ${CLI_SOURCES}) target_link_libraries(${EXECUTABLE_NAME} PRIVATE ${LIBRARY_NAME}) # TODO: check if coroutines support has been added to target_compile_features() -target_compile_options(${EXECUTABLE_NAME} PRIVATE "-fcoroutines") +target_compile_options(${EXECUTABLE_NAME} PRIVATE "-fcoroutines" ${OPTIMIZATION_FLAGS}) +target_link_options(${EXECUTABLE_NAME} PRIVATE ${LINKER_FLAGS}) set_target_properties( ${LIBRARY_NAME} ${EXECUTABLE_NAME} @@ -145,6 +169,28 @@ set_target_properties( #CXX_EXTENSIONS YES ) +if(SS_STRIP_SYMBOLS) + if(NOT CMAKE_STRIP) + # Tomamos el path del compilador C (gcc o clang usado por OpenWrt) + get_filename_component(TOOLCHAIN_BIN_DIR "${CMAKE_C_COMPILER}" DIRECTORY) + get_filename_component(TOOLCHAIN_PREFIX "${CMAKE_C_COMPILER}" NAME_WE) # nombre sin extensión + + # Reemplazamos el "gcc" por "strip" para deducir la ruta correcta + string(REPLACE "gcc" "strip" TOOLCHAIN_STRIP "${TOOLCHAIN_BIN_DIR}/${TOOLCHAIN_PREFIX}") + + if(EXISTS "${TOOLCHAIN_STRIP}") + set(CMAKE_STRIP "${TOOLCHAIN_STRIP}" CACHE FILEPATH "Path to toolchain strip" FORCE) + message(STATUS "Using toolchain strip: ${CMAKE_STRIP}") + add_custom_command(TARGET ${EXECUTABLE_NAME} POST_BUILD + COMMAND ${CMAKE_STRIP} --strip-unneeded $ + COMMENT "Stripping unused symbols from ${EXECUTABLE_NAME}") + else() + message(WARNING "No toolchain strip found at ${TOOLCHAIN_STRIP}, will skip stripping") + endif() + endif() + +endif() + install(TARGETS ${LIBRARY_NAME} ${EXECUTABLE_NAME} ) # Optional IPO/LTO. Do not enable them if it's not supported by compiler.