CMake: Make libc.a self-contained

Previously, libc.a contained undefined symbols from ssp and libsystem,
which caused static compilation to fail.

We now generate libc.a with a custom CMake rule that combines all
object files from libc, ssp and libsystem to form libc.a

Closes #5758.
This commit is contained in:
Itamar 2021-03-14 21:39:09 +02:00 committed by Andreas Kling
parent ad40c5f9a9
commit ae67cabe11
Notes: sideshowbarker 2024-07-18 21:14:00 +09:00
2 changed files with 27 additions and 4 deletions

View File

@ -85,11 +85,31 @@ add_custom_command(
set(SOURCES ${LIBC_SOURCES} ${AK_SOURCES} ${ELF_SOURCES} ${ASM_SOURCES}) set(SOURCES ${LIBC_SOURCES} ${AK_SOURCES} ${ELF_SOURCES} ${ASM_SOURCES})
serenity_libc_static(LibCStatic c) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -static-libstdc++")
target_link_libraries(LibCStatic crt0 ssp system) add_library(LibCStaticWithoutDeps STATIC ${SOURCES})
add_dependencies(LibCStatic LibM LibSystem) target_link_libraries(LibCStaticWithoutDeps crt0 ssp)
add_dependencies(LibCStaticWithoutDeps LibM LibSystem)
add_custom_target(LibCStatic
COMMAND ${CMAKE_AR} -x $<TARGET_FILE:LibCStaticWithoutDeps>
COMMAND ${CMAKE_AR} -x $<TARGET_FILE:ssp>
COMMAND ${CMAKE_AR} -x $<TARGET_FILE:LibSystemStatic>
COMMAND ${CMAKE_AR} -qcs ${CMAKE_CURRENT_BINARY_DIR}/libc.a *.o
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
DEPENDS LibCStaticWithoutDeps ssp LibSystemStatic
)
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/libc.a DESTINATION ${CMAKE_INSTALL_PREFIX}/usr/lib/)
file(GLOB TEMP_OBJ_FILES ${CMAKE_CURRENT_BINARY_DIR}/*.o)
set_property(
TARGET LibCStatic
APPEND
PROPERTY ADDITIONAL_CLEAN_FILES ${TEMP_OBJ_FILES}
)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -static-libstdc++") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -static-libstdc++")
serenity_libc(LibC c) serenity_libc(LibC c)
target_link_libraries(LibC crt0 ssp system) target_link_libraries(LibC crt0 ssp system)
add_dependencies(LibC LibM LibSystem)
# We mark LibCStatic as a dependency of LibC because this triggers the build of the LibCStatic target
add_dependencies(LibC LibM LibSystem LibCStatic)

View File

@ -5,3 +5,6 @@ set(SOURCES
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -nostdlib") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -nostdlib")
serenity_libc(LibSystem system) serenity_libc(LibSystem system)
target_include_directories(LibSystem PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}) target_include_directories(LibSystem PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
add_library(LibSystemStatic STATIC ${SOURCES})
target_include_directories(LibSystemStatic PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})