Skip to content

Commit e31a3d3

Browse files
authored
CMake: Use LDC as (cross-)archiver for static runtime libs (#2319)
By default for all non-MSVC targets; overrideable via CMake var RT_ARCHIVE_WITH_LDC. LDC/LLVM handles cross-archiving and LTO objects out of the box.
1 parent 6ad0193 commit e31a3d3

File tree

1 file changed

+26
-4
lines changed

1 file changed

+26
-4
lines changed

runtime/CMakeLists.txt

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ set(D_FLAGS -w CACHE STRING "Runt
3939
set(D_FLAGS_DEBUG -g;-link-debuglib CACHE STRING "Runtime D compiler flags (debug libraries), separated by ';'")
4040
set(D_FLAGS_RELEASE -O3;-release CACHE STRING "Runtime D compiler flags (release libraries), separated by ';'")
4141
set(COMPILE_ALL_D_FILES_AT_ONCE ON CACHE BOOL "Compile all D files for a lib in a single command line instead of separately")
42+
set(RT_ARCHIVE_WITH_LDC AUTO CACHE STRING "Whether to archive the static runtime libs via LDC instead of CMake archiver")
4243
set(RT_CFLAGS "" CACHE STRING "Runtime extra C compiler flags, separated by ' '")
4344
set(LD_FLAGS "" CACHE STRING "Runtime extra C linker flags, separated by ' '")
4445
set(C_SYSTEM_LIBS AUTO CACHE STRING "C system libraries for linking shared libraries and test runners, separated by ';'")
@@ -77,6 +78,16 @@ else()
7778
set(MULTILIB_SUFFIX 64)
7879
endif()
7980

81+
if(${RT_ARCHIVE_WITH_LDC} STREQUAL "AUTO")
82+
# LLVM's MSVC archiver (v4.0.1) apparently has issues with objects with debuginfos,
83+
# leading to MS linker warnings when linking against the static debug runtime libs.
84+
if("${TARGET_SYSTEM}" MATCHES "MSVC")
85+
set(RT_ARCHIVE_WITH_LDC OFF)
86+
else()
87+
set(RT_ARCHIVE_WITH_LDC ON)
88+
endif()
89+
endif()
90+
8091
set(SHARED_LIBS_SUPPORTED OFF)
8192
if("${TARGET_SYSTEM}" MATCHES "Linux|FreeBSD|APPLE"
8293
AND (NOT "${TARGET_SYSTEM}" MATCHES "Android"))
@@ -427,13 +438,24 @@ macro(compile_phobos2 d_flags lib_suffix path_suffix all_at_once outlist_o outli
427438
)
428439
endmacro()
429440

441+
# Define an optional 'D' CMake linker language for the static runtime libs,
442+
# using LDC as archiver. LDC handles (cross-)archiving internally via LLVM
443+
# and supports LTO objects.
444+
set(CMAKE_D_CREATE_STATIC_LIBRARY "${LDC_EXE_FULL} -lib -of=<TARGET> <OBJECTS>")
445+
foreach(f ${D_FLAGS})
446+
append("\"${f}\"" CMAKE_D_CREATE_STATIC_LIBRARY)
447+
endforeach()
448+
430449
# Sets the common properties for all library targets.
431-
function(set_common_library_properties target)
450+
function(set_common_library_properties target is_shared)
432451
set_target_properties(${target} PROPERTIES
433452
VERSION ${DMDFE_VERSION}
434453
SOVERSION ${DMDFE_PATCH_VERSION}
435454
LINKER_LANGUAGE C
436455
)
456+
if(RT_ARCHIVE_WITH_LDC AND NOT is_shared)
457+
set_target_properties(${target} PROPERTIES LINKER_LANGUAGE D)
458+
endif()
437459

438460
# ldc2 defaults to position-independent code on Linux to match the implicit
439461
# linker default on Ubuntu 16.10 and above. As we might be building on an
@@ -468,7 +490,7 @@ macro(build_runtime_libs druntime_o druntime_bc phobos2_o phobos2_bc c_flags ld_
468490
COMPILE_FLAGS "${c_flags}"
469491
LINK_FLAGS "${ld_flags}"
470492
)
471-
set_common_library_properties(druntime-ldc${target_suffix})
493+
set_common_library_properties(druntime-ldc${target_suffix} "${is_shared}")
472494

473495
# When building a shared library, we need to link in all the default
474496
# libraries otherwise implicitly added by LDC to make it loadable from
@@ -491,7 +513,7 @@ macro(build_runtime_libs druntime_o druntime_bc phobos2_o phobos2_bc c_flags ld_
491513
COMPILE_FLAGS "${c_flags}"
492514
LINK_FLAGS "${ld_flags}"
493515
)
494-
set_common_library_properties(phobos2-ldc${target_suffix})
516+
set_common_library_properties(phobos2-ldc${target_suffix} "${is_shared}")
495517

496518
if("${is_shared}" STREQUAL "ON")
497519
target_link_libraries(phobos2-ldc${target_suffix}
@@ -605,7 +627,7 @@ macro(build_all_runtime_variants d_flags c_flags ld_flags path_suffix outlist_ta
605627
# static profile-rt
606628
build_profile_runtime("${d_flags};${D_FLAGS};${D_FLAGS_RELEASE}" "${c_flags}" "${ld_flags}" "" "${path_suffix}" ${outlist_targets})
607629
get_target_suffix("" "${path_suffix}" target_suffix)
608-
set_common_library_properties(ldc-profile-rt${target_suffix})
630+
set_common_library_properties(ldc-profile-rt${target_suffix} OFF)
609631
endif()
610632
endmacro()
611633

0 commit comments

Comments
 (0)