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.
This commit is contained in:
ayeteadoe 2025-05-09 18:30:45 -07:00 committed by Andrew Kaster
commit 8f670950e2
Notes: github-actions[bot] 2025-05-12 09:23:44 +00:00

View file

@ -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)