Skip to content

Commit 5c271ef

Browse files
committed
CMake: Support BUILD_SHARED_LIBS for Windows too
By compiling druntime/Phobos with `-fvisibility=public` for the new DLLs. Compiling and linking druntime works fine: > ls -lh lib/druntime-ldc* -rwxr-xr-x 1 Martin 197121 2.4M Apr 24 19:28 lib/druntime-ldc-debug-shared.dll -rw-r--r-- 1 Martin 197121 2.9M Apr 24 19:28 lib/druntime-ldc-debug-shared.lib -rw-r--r-- 1 Martin 197121 12M Apr 24 19:28 lib/druntime-ldc-debug-shared.pdb -rw-r--r-- 1 Martin 197121 8.6M Apr 24 19:28 lib/druntime-ldc-debug.lib -rw-r--r-- 1 Martin 197121 4.7M Apr 24 19:28 lib/druntime-ldc-lto.lib -rwxr-xr-x 1 Martin 197121 2.2M Apr 24 19:28 lib/druntime-ldc-shared.dll -rw-r--r-- 1 Martin 197121 2.8M Apr 24 18:46 lib/druntime-ldc-shared.lib -rw-r--r-- 1 Martin 197121 8.5M Apr 24 19:28 lib/druntime-ldc-shared.pdb -rw-r--r-- 1 Martin 197121 4.4M Apr 24 19:28 lib/druntime-ldc.lib The druntime DLL exports more than 10k symbols. The MS C runtime is linked statically; that should probably be changed to the MS DLLs too. Linking a non-Phobos hello-world app against the druntime DLL works; it unsurprisingly crashes at runtime though. The Phobos DLL can be compiled too, but linking fails (builtin TypeInfo init symbols and vtables etc.).
1 parent 5acfc16 commit 5c271ef

File tree

3 files changed

+48
-21
lines changed

3 files changed

+48
-21
lines changed

.azure-pipelines/windows.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ steps:
7676
clang-cl --version
7777
mkdir bootstrap-ldc
7878
cd bootstrap-ldc
79-
cmake -G Ninja %BUILD_SOURCESDIRECTORY% -DCMAKE_C_COMPILER:PATH=clang-cl.exe -DCMAKE_CXX_COMPILER:PATH=clang-cl.exe -DCMAKE_BUILD_TYPE=Release -DLLVM_ROOT_DIR=%CD%/../llvm -DD_COMPILER=%CD%/../host-ldc/bin/ldmd2 -DBUILD_LTO_LIBS=ON
79+
cmake -G Ninja %BUILD_SOURCESDIRECTORY% -DCMAKE_C_COMPILER:PATH=clang-cl.exe -DCMAKE_CXX_COMPILER:PATH=clang-cl.exe -DCMAKE_BUILD_TYPE=Release -DLLVM_ROOT_DIR=%CD%/../llvm -DD_COMPILER=%CD%/../host-ldc/bin/ldmd2 -DBUILD_SHARED_LIBS=OFF -DBUILD_LTO_LIBS=ON
8080
ninja -j4 || exit /b
8181
bin\ldc2 --version
8282
displayName: Build bootstrap LDC

runtime/CMakeLists.txt

+46-19
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ else()
9191
endif()
9292

9393
set(SHARED_LIBS_SUPPORTED OFF)
94-
if("${TARGET_SYSTEM}" MATCHES "Linux|FreeBSD|DragonFly|APPLE"
94+
if("${TARGET_SYSTEM}" MATCHES "Windows|Linux|FreeBSD|DragonFly|APPLE"
9595
AND (NOT "${TARGET_SYSTEM}" MATCHES "Android"))
9696
set(SHARED_LIBS_SUPPORTED ON)
9797
endif()
@@ -107,7 +107,7 @@ endif()
107107
set(SHARED_LIB_SUFFIX "")
108108
if(NOT ${BUILD_SHARED_LIBS} STREQUAL "OFF")
109109
if(NOT SHARED_LIBS_SUPPORTED)
110-
message(FATAL_ERROR "Shared libraries (BUILD_SHARED_LIBS) are only supported on Linux, macOS, FreeBSD and DragonFly for the time being.")
110+
message(FATAL_ERROR "Shared libraries (BUILD_SHARED_LIBS) are only supported on Windows, Linux, macOS, FreeBSD and DragonFly for the time being.")
111111
endif()
112112
set(SHARED_LIB_SUFFIX "-shared")
113113
endif()
@@ -638,26 +638,30 @@ macro(build_runtime_variant d_flags c_flags ld_flags lib_suffix path_suffix emit
638638
set(is_unittest ON)
639639
endif()
640640

641-
# Only compile Phobos modules once for static+shared libs.
642-
# If a shared Phobos lib is generated (too), compile explicitly with `-relocation-model=pic`.
643641
set(phobos2_d_flags ${d_flags})
644642
if(is_unittest)
645643
list(APPEND phobos2_d_flags ${PHOBOS2_EXTRA_UNITTEST_FLAGS})
646644
endif()
647-
if(NOT ${BUILD_SHARED_LIBS} STREQUAL "OFF")
648-
list(APPEND phobos2_d_flags -relocation-model=pic)
649-
endif()
650645

651-
set(phobos2_o "")
652-
set(phobos2_bc "")
653-
compile_phobos2("${phobos2_d_flags}" "${lib_suffix}" "${path_suffix}" "${emit_bc}" "${all_d_files_at_once}" phobos2_o phobos2_bc)
646+
# Posix: only compile Phobos modules once for static+shared libs.
647+
# If a shared Phobos lib is generated (too), compile explicitly with `-relocation-model=pic`.
648+
if(NOT "${TARGET_SYSTEM}" MATCHES "Windows")
649+
if(NOT ${BUILD_SHARED_LIBS} STREQUAL "OFF")
650+
list(APPEND phobos2_d_flags -relocation-model=pic)
651+
endif()
654652

655-
# Use a dummy custom target ('phobos2-ldc…-common') depending solely on the Phobos D objects
656-
# (custom commands) as dependency for static+shared Phobos libs.
657-
# Otherwise building both libs in parallel may result in conflicting Phobos module compilations
658-
# (at least with the make generator), a known CMake issue.
659-
set(phobos2_common phobos2-ldc${target_suffix}-common)
660-
add_custom_target(${phobos2_common} DEPENDS ${phobos2_o} ${phobos2_bc})
653+
set(phobos2_o "")
654+
set(phobos2_bc "")
655+
compile_phobos2("${phobos2_d_flags}" "${lib_suffix}"
656+
"${path_suffix}" "${emit_bc}" "${all_d_files_at_once}" phobos2_o phobos2_bc)
657+
658+
# Use a dummy custom target ('phobos2-ldc…-common') depending solely on the Phobos D objects
659+
# (custom commands) as dependency for static+shared Phobos libs.
660+
# Otherwise building both libs in parallel may result in conflicting Phobos module compilations
661+
# (at least with the make generator), a known CMake issue.
662+
set(phobos2_common phobos2-ldc${target_suffix}-common)
663+
add_custom_target(${phobos2_common} DEPENDS ${phobos2_o} ${phobos2_bc})
664+
endif()
661665

662666
set(druntime_d_flags ${d_flags})
663667
if(is_unittest)
@@ -671,22 +675,45 @@ macro(build_runtime_variant d_flags c_flags ld_flags lib_suffix path_suffix emit
671675
compile_druntime("${druntime_d_flags}" "${lib_suffix}"
672676
"${path_suffix}" "${emit_bc}" "${all_d_files_at_once}" druntime_o druntime_bc)
673677

678+
# Windows: compile Phobos with hidden visibility
679+
if("${TARGET_SYSTEM}" MATCHES "Windows")
680+
set(phobos2_o "")
681+
set(phobos2_bc "")
682+
compile_phobos2("${phobos2_d_flags}" "${lib_suffix}"
683+
"${path_suffix}" "${emit_bc}" "${all_d_files_at_once}" phobos2_o phobos2_bc)
684+
endif()
685+
674686
build_runtime_libs("${druntime_o}" "${druntime_bc}" "${phobos2_o}" "${phobos2_bc}"
675687
"${c_flags}" "${ld_flags}" "${lib_suffix}" "${path_suffix}"
676688
"OFF" "${emit_bc}" ${outlist_targets})
677-
add_dependencies(phobos2-ldc${target_suffix} ${phobos2_common})
689+
690+
if(NOT "${TARGET_SYSTEM}" MATCHES "Windows")
691+
add_dependencies(phobos2-ldc${target_suffix} ${phobos2_common})
692+
endif()
678693
endif()
679694
# shared druntime/Phobos
680695
if(NOT ${BUILD_SHARED_LIBS} STREQUAL "OFF")
696+
# compile druntime with extra `version (Shared)`
681697
set(druntime_o "")
682698
set(druntime_bc "")
683-
compile_druntime("${druntime_d_flags};-relocation-model=pic;-d-version=Shared" "${lib_suffix}${SHARED_LIB_SUFFIX}"
699+
compile_druntime("${druntime_d_flags};-relocation-model=pic;-d-version=Shared;-fvisibility=public" "${lib_suffix}${SHARED_LIB_SUFFIX}"
684700
"${path_suffix}" "OFF" "${all_d_files_at_once}" druntime_o druntime_bc)
685701

702+
# Windows: compile Phobos with public visibility
703+
if("${TARGET_SYSTEM}" MATCHES "Windows")
704+
set(phobos2_o "")
705+
set(phobos2_bc "")
706+
compile_phobos2("${phobos2_d_flags};-fvisibility=public" "${lib_suffix}${SHARED_LIB_SUFFIX}"
707+
"${path_suffix}" "OFF" "${all_d_files_at_once}" phobos2_o phobos2_bc)
708+
endif()
709+
686710
build_runtime_libs("${druntime_o}" "${druntime_bc}" "${phobos2_o}" "${phobos2_bc}"
687711
"${c_flags}" "${ld_flags}" "${lib_suffix}${SHARED_LIB_SUFFIX}" "${path_suffix}"
688712
"ON" "OFF" ${outlist_targets})
689-
add_dependencies(phobos2-ldc${target_suffix} ${phobos2_common})
713+
714+
if(NOT "${TARGET_SYSTEM}" MATCHES "Windows")
715+
add_dependencies(phobos2-ldc${target_suffix} ${phobos2_common})
716+
endif()
690717
endif()
691718
endmacro()
692719

runtime/druntime

0 commit comments

Comments
 (0)