From 8f670950e2364309826d3f95705f74f2661aa228 Mon Sep 17 00:00:00 2001 From: ayeteadoe Date: Fri, 9 May 2025 18:30:45 -0700 Subject: [PATCH] Meta: Add opt-in explicit symbol export for libraries On Windows, `set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON)` is currently used. But that will need to change as LibWeb exceeds the maximum symbols that can be exported from a single executable/dll (See LNK1189 error). So to prevent bitrot and Unix/Windows getting out of sync, this function that generates the export header for the target also sets the symbol visibility to hidden on Unix to ensure Link errors also occur on Unix if the the required library components are not annotated with the FOO_API macro. --- Meta/Lagom/CMakeLists.txt | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/Meta/Lagom/CMakeLists.txt b/Meta/Lagom/CMakeLists.txt index 6f429615963..5d45cbc4698 100644 --- a/Meta/Lagom/CMakeLists.txt +++ b/Meta/Lagom/CMakeLists.txt @@ -205,7 +205,7 @@ install( ) function(lagom_lib target_name fs_name) - cmake_parse_arguments(LAGOM_LIBRARY "" "LIBRARY_TYPE" "SOURCES;LIBS" ${ARGN}) + cmake_parse_arguments(LAGOM_LIBRARY "EXPLICIT_SYMBOL_EXPORT" "LIBRARY_TYPE" "SOURCES;LIBS" ${ARGN}) string(REPLACE "Lib" "" library ${target_name}) if (NOT LAGOM_LIBRARY_LIBRARY_TYPE) set(LAGOM_LIBRARY_LIBRARY_TYPE "") @@ -253,6 +253,19 @@ function(lagom_lib target_name fs_name) ) endif() serenity_generated_sources(${target_name}) + + if (LAGOM_LIBRARY_EXPLICIT_SYMBOL_EXPORT) + # Temporary helper to allow libraries to opt-in to using X_API macros + # to export symbols required by external consumers. This allows the codebase + # to gradually slowly migrate instead of an all-or-nothing approach. + if (NOT WIN32) + target_compile_options(${target_name} PRIVATE -fvisibility=hidden) + endif() + include(GenerateExportHeader) + string(TOUPPER ${fs_name} fs_name_upper) + generate_export_header(${target_name} EXPORT_MACRO_NAME "${fs_name_upper}_API" EXPORT_FILE_NAME "Export.h") + target_sources(${target_name} PRIVATE "${CMAKE_CURRENT_BINARY_DIR}/Export.h") + endif() endfunction() function(lagom_test source) @@ -303,8 +316,12 @@ function(serenity_bin name) endfunction() function(serenity_lib name fs_name) - cmake_parse_arguments(PARSE_ARGV 2 SERENITY_LIB "" "TYPE" "") - lagom_lib(${name} ${fs_name} LIBRARY_TYPE ${SERENITY_LIB_TYPE} SOURCES ${SOURCES} ${GENERATED_SOURCES}) + cmake_parse_arguments(PARSE_ARGV 2 SERENITY_LIB "EXPLICIT_SYMBOL_EXPORT" "TYPE" "") + set(EXPLICIT_SYMBOL_EXPORT "") + if (SERENITY_LIB_EXPLICIT_SYMBOL_EXPORT) + set(EXPLICIT_SYMBOL_EXPORT "EXPLICIT_SYMBOL_EXPORT") + endif() + lagom_lib(${name} ${fs_name} LIBRARY_TYPE ${SERENITY_LIB_TYPE} ${EXPLICIT_SYMBOL_EXPORT} SOURCES ${SOURCES} ${GENERATED_SOURCES}) endfunction() macro(add_serenity_subdirectory path)