Skip to content

Commit 4959cce

Browse files
authored
Use a Shared build for cppinterop while building for wasm (#375)
1 parent 7038fe7 commit 4959cce

File tree

4 files changed

+193
-104
lines changed

4 files changed

+193
-104
lines changed

.github/workflows/ci.yml

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1566,14 +1566,15 @@ jobs:
15661566
cd build
15671567
export CPPINTEROP_BUILD_DIR=$PWD
15681568
if [[ "${cling_on}" == "ON" ]]; then
1569-
emcmake cmake -DCMAKE_BUILD_TYPE=${{ env.BUILD_TYPE }} \
1569+
emcmake cmake -DCMAKE_BUILD_TYPE=${{ env.BUILD_TYPE }} \
15701570
-DUSE_CLING=ON \
15711571
-DUSE_REPL=OFF \
15721572
-DCMAKE_PREFIX_PATH=$PREFIX \
15731573
-DCling_DIR=$LLVM_BUILD_DIR/tools/cling \
15741574
-DLLVM_DIR=$LLVM_BUILD_DIR/lib/cmake/llvm \
1575+
-DLLD_DIR=$LLVM_BUILD_DIR/lib/cmake/lld \
15751576
-DClang_DIR=$LLVM_BUILD_DIR/lib/cmake/clang \
1576-
-DBUILD_SHARED_LIBS=OFF \
1577+
-DBUILD_SHARED_LIBS=ON \
15771578
-DCODE_COVERAGE=${{ env.CODE_COVERAGE }} \
15781579
-DCMAKE_INSTALL_PREFIX=$PREFIX \
15791580
-DCMAKE_FIND_ROOT_PATH_MODE_PACKAGE=ON \
@@ -1582,10 +1583,11 @@ jobs:
15821583
emcmake cmake -DCMAKE_BUILD_TYPE=${{ env.BUILD_TYPE }} \
15831584
-DUSE_CLING=OFF \
15841585
-DUSE_REPL=ON \
1585-
-DCMAKE_PREFIX_PATH=$PREFIX \
1586-
-DLLVM_DIR=$LLVM_BUILD_DIR/lib/cmake/llvm \
1586+
-DCMAKE_PREFIX_PATH=$PREFIX \
1587+
-DLLVM_DIR=$LLVM_BUILD_DIR/lib/cmake/llvm \
1588+
-DLLD_DIR=$LLVM_BUILD_DIR/lib/cmake/lld \
15871589
-DClang_DIR=$LLVM_BUILD_DIR/lib/cmake/clang \
1588-
-DBUILD_SHARED_LIBS=OFF \
1590+
-DBUILD_SHARED_LIBS=ON \
15891591
-DCODE_COVERAGE=${{ env.CODE_COVERAGE }} \
15901592
-DCMAKE_INSTALL_PREFIX=$PREFIX \
15911593
-DCMAKE_FIND_ROOT_PATH_MODE_PACKAGE=ON \
@@ -1623,4 +1625,4 @@ jobs:
16231625
-DCMAKE_FIND_ROOT_PATH_MODE_PACKAGE=ON \
16241626
-DCppInterOp_DIR="${{ env.CPPINTEROP_BUILD_DIR }}/lib/cmake/CppInterOp" \
16251627
..
1626-
EMCC_CFLAGS='-sERROR_ON_UNDEFINED_SYMBOLS=0' emmake make -j ${{ env.ncpus }}
1628+
emmake make -j ${{ env.ncpus }}

.github/workflows/deploy-pages.yml

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,9 @@ jobs:
124124
-DCMAKE_PREFIX_PATH=$PREFIX \
125125
-DCling_DIR=$LLVM_BUILD_DIR/tools/cling \
126126
-DLLVM_DIR=$LLVM_BUILD_DIR/lib/cmake/llvm \
127+
-DLLD_DIR=$LLVM_BUILD_DIR/lib/cmake/lld \
127128
-DClang_DIR=$LLVM_BUILD_DIR/lib/cmake/clang \
128-
-DBUILD_SHARED_LIBS=OFF \
129+
-DBUILD_SHARED_LIBS=ON \
129130
-DCODE_COVERAGE=${{ env.CODE_COVERAGE }} \
130131
-DCMAKE_INSTALL_PREFIX=$PREFIX \
131132
-DCMAKE_FIND_ROOT_PATH_MODE_PACKAGE=ON \
@@ -136,8 +137,9 @@ jobs:
136137
-DUSE_REPL=ON \
137138
-DCMAKE_PREFIX_PATH=$PREFIX \
138139
-DLLVM_DIR=$LLVM_BUILD_DIR/lib/cmake/llvm \
140+
-DLLD_DIR=$LLVM_BUILD_DIR/lib/cmake/lld \
139141
-DClang_DIR=$LLVM_BUILD_DIR/lib/cmake/clang \
140-
-DBUILD_SHARED_LIBS=OFF \
142+
-DBUILD_SHARED_LIBS=ON \
141143
-DCODE_COVERAGE=${{ env.CODE_COVERAGE }} \
142144
-DCMAKE_INSTALL_PREFIX=$PREFIX \
143145
-DCMAKE_FIND_ROOT_PATH_MODE_PACKAGE=ON \
@@ -175,7 +177,7 @@ jobs:
175177
-DCMAKE_FIND_ROOT_PATH_MODE_PACKAGE=ON \
176178
-DCppInterOp_DIR="${{ env.CPPINTEROP_BUILD_DIR }}/lib/cmake/CppInterOp" \
177179
..
178-
EMCC_CFLAGS='-sERROR_ON_UNDEFINED_SYMBOLS=0' emmake make -j ${{ env.ncpus }} install
180+
emmake make -j ${{ env.ncpus }} install
179181
180182
- name: Jupyter Lite integration
181183
shell: bash -l {0}

CMakeLists.txt

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,28 @@ if( CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR )
2424
if (NOT DEFINED Cling_DIR)
2525
set(Cling_DIR ${LLVM_DIR})
2626
endif()
27+
if (NOT DEFINED LLD_DIR)
28+
set(LLD_DIR ${LLVM_DIR})
29+
endif()
30+
endif()
31+
if (DEFINED LLD_DIR)
32+
if (NOT DEFINED LLVM_DIR)
33+
set(LLVM_DIR ${LLD_DIR})
34+
endif()
35+
if (NOT DEFINED Clang_DIR)
36+
set(Clang_DIR ${LLD_DIR})
37+
endif()
38+
if (NOT DEFINED Cling_DIR)
39+
set(Cling_DIR ${LLD_DIR})
40+
endif()
2741
endif()
2842
if (DEFINED Clang_DIR)
2943
if (NOT DEFINED LLVM_DIR)
3044
set(LLVM_DIR ${Clang_DIR})
3145
endif()
46+
if (NOT DEFINED LLD_DIR)
47+
set(LLD_DIR ${Clang_DIR})
48+
endif()
3249
if (NOT DEFINED Cling_DIR)
3350
set(Cling_DIR ${Clang_DIR})
3451
endif()
@@ -54,6 +71,9 @@ if( CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR )
5471
set(CLANG_MIN_SUPPORTED 13.0)
5572
set(CLANG_MAX_SUPPORTED "19.1.x")
5673
set(CLANG_VERSION_UPPER_BOUND 20.0.0)
74+
set(LLD_MIN_SUPPORTED 13.0)
75+
set(LLD_MAX_SUPPORTED "19.1.x")
76+
set(LLD_VERSION_UPPER_BOUND 20.0.0)
5777
set(LLVM_MIN_SUPPORTED 13.0)
5878
set(LLVM_MAX_SUPPORTED "19.1.x")
5979
set(LLVM_VERSION_UPPER_BOUND 20.0.0)
@@ -68,6 +88,11 @@ if( CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR )
6888
if (DEFINED LLVM_DIR)
6989
set(llvm_search_hints PATHS ${LLVM_DIR} HINTS "${LLVM_DIR}/lib/cmake/llvm" "${LLVM_DIR}/cmake" "${LLVM_CONFIG_EXTRA_PATH_HINTS}")
7090
set(clang_search_hints PATHS ${LLVM_DIR} HINTS "${LLVM_DIR}/lib/cmake/clang" "${LLVM_DIR}/cmake")
91+
set(lld_search_hints PATHS ${LLVM_DIR} HINTS "${LLVM_DIR}/lib/cmake/lld" "${LLVM_DIR}/cmake")
92+
endif()
93+
if (DEFINED LLD_DIR)
94+
set(llvm_search_hints PATHS ${LLD_DIR} HINTS "${LLD_DIR}/lib/cmake/llvm" "${LLD_DIR}/cmake")
95+
set(lld_search_hints PATHS ${LLD_DIR} HINTS "${lld_search_hints}" "${LLD_DIR}/lib/cmake/lld" "${LLD_DIR}/cmake")
7196
endif()
7297
if (DEFINED Clang_DIR)
7398
set(llvm_search_hints PATHS ${Clang_DIR} HINTS "${Clang_DIR}/lib/cmake/llvm" "${Clang_DIR}/cmake")
@@ -130,6 +155,45 @@ if( CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR )
130155
message(STATUS "Found supported version: LLVM ${LLVM_PACKAGE_VERSION}")
131156
message(STATUS "Using LLVMConfig.cmake in: ${LLVM_DIR}")
132157

158+
## Find supported LLD only while building for webassembly against emscripten
159+
160+
if(EMSCRIPTEN)
161+
if (DEFINED LLD_VERSION)
162+
if (LLD_VERSION VERSION_GREATER_EQUAL LLD_VERSION_UPPER_BOUND)
163+
set(LLD_VERSION ${LLD_VERSION_UPPER_BOUND})
164+
endif()
165+
if (LLD_VERSION VERSION_LESS LLD_MIN_SUPPORTED)
166+
set(LLD_VERSION ${LLD_MIN_SUPPORTED})
167+
endif()
168+
169+
find_package(LLD ${LLD_VERSION} REQUIRED CONFIG ${lld_search_hints} NO_DEFAULT_PATH)
170+
endif()
171+
172+
if (NOT LLD_FOUND AND DEFINED LLD_DIR)
173+
find_package(LLD REQUIRED CONFIG ${lld_search_hints} NO_DEFAULT_PATH)
174+
endif()
175+
176+
if (NOT LLD_FOUND)
177+
find_package(LLD REQUIRED CONFIG)
178+
endif()
179+
180+
if (NOT LLD_FOUND)
181+
message(FATAL_ERROR "Please set LLD_DIR pointing to the LLD build or installation folder")
182+
endif()
183+
184+
set(LLD_VERSION_MAJOR ${LLVM_VERSION_MAJOR})
185+
set(LLD_VERSION_MINOR ${LLVM_VERSION_MINOR})
186+
set(LLD_VERSION_PATCH ${LLVM_VERSION_PATCH})
187+
set(LLD_PACKAGE_VERSION ${LLVM_PACKAGE_VERSION})
188+
189+
if (LLD_PACKAGE_VERSION VERSION_LESS LLD_MIN_SUPPORTED OR LLD_PACKAGE_VERSION VERSION_GREATER_EQUAL LLD_VERSION_UPPER_BOUND)
190+
message(FATAL_ERROR "Found unsupported version: LLD ${LLD_PACKAGE_VERSION};\nPlease set LLD_DIR pointing to the LLD version ${LLD_MIN_SUPPORTED} to ${LLD_MAX_SUPPORTED} build or installation folder")
191+
endif()
192+
193+
message(STATUS "Found supported version: LLD ${LLD_PACKAGE_VERSION}")
194+
message(STATUS "Using LLDConfig.cmake in: ${LLD_DIR}")
195+
endif()
196+
133197
## Find supported Clang
134198

135199
if (DEFINED CLANG_VERSION)

lib/Interpreter/CMakeLists.txt

Lines changed: 116 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -1,107 +1,128 @@
1-
set(LLVM_LINK_COMPONENTS
2-
${LLVM_TARGETS_TO_BUILD}
3-
BinaryFormat
4-
Core
5-
Object
6-
OrcJit
7-
Support
8-
)
9-
# FIXME: Investigate why this needs to be conditionally included.
10-
if ("LLVMFrontendDriver" IN_LIST LLVM_AVAILABLE_LIBS)
11-
list(APPEND LLVM_LINK_COMPONENTS FrontendDriver)
12-
endif()
13-
if ("LLVMOrcDebugging" IN_LIST LLVM_AVAILABLE_LIBS)
14-
list(APPEND LLVM_LINK_COMPONENTS OrcDebugging)
15-
endif()
1+
if(EMSCRIPTEN)
2+
set_property(GLOBAL PROPERTY TARGET_SUPPORTS_SHARED_LIBS TRUE)
3+
set(CMAKE_SHARED_LIBRARY_CREATE_C_FLAGS "-s SIDE_MODULE=1")
4+
set(CMAKE_SHARED_LIBRARY_CREATE_CXX_FLAGS "-s SIDE_MODULE=1")
5+
set(CMAKE_STRIP FALSE)
166

17-
set(DLM
18-
DynamicLibraryManager.cpp
19-
DynamicLibraryManagerSymbol.cpp
20-
Paths.cpp
21-
)
22-
if (USE_CLING)
23-
set(LLVM_OPTIONAL_SOURCES ${LLVM_OPTIONAL_SOURCES} ${DLM})
24-
set(DLM)
25-
endif(USE_CLING)
26-
if (USE_REPL)
27-
#Use DML optional sources
28-
endif(USE_REPL)
7+
add_llvm_library(clangCppInterOp
8+
SHARED
299

30-
if (USE_CLING)
31-
set(cling_clang_interp clingInterpreter)
32-
endif()
33-
if (USE_REPL)
34-
set(cling_clang_interp clangInterpreter)
35-
endif()
10+
CppInterOp.cpp
11+
CXCppInterOp.cpp
12+
DynamicLibraryManager.cpp
13+
DynamicLibraryManagerSymbol.cpp
14+
Paths.cpp
3615

37-
set(link_libs
38-
${cling_clang_interp}
39-
clangAST
40-
clangBasic
41-
clangFrontend
42-
clangLex
43-
clangSema
16+
# Additional libraries from Clang and LLD
17+
LINK_LIBS
18+
clangInterpreter
19+
)
20+
else()
21+
set(LLVM_LINK_COMPONENTS
22+
${LLVM_TARGETS_TO_BUILD}
23+
BinaryFormat
24+
Core
25+
Object
26+
OrcJit
27+
Support
4428
)
29+
# FIXME: Investigate why this needs to be conditionally included.
30+
if ("LLVMFrontendDriver" IN_LIST LLVM_AVAILABLE_LIBS)
31+
list(APPEND LLVM_LINK_COMPONENTS FrontendDriver)
32+
endif()
33+
if ("LLVMOrcDebugging" IN_LIST LLVM_AVAILABLE_LIBS)
34+
list(APPEND LLVM_LINK_COMPONENTS OrcDebugging)
35+
endif()
4536

46-
if(NOT WIN32)
47-
list(APPEND link_libs dl)
48-
endif()
37+
set(DLM
38+
DynamicLibraryManager.cpp
39+
DynamicLibraryManagerSymbol.cpp
40+
Paths.cpp
41+
)
42+
if (USE_CLING)
43+
set(LLVM_OPTIONAL_SOURCES ${LLVM_OPTIONAL_SOURCES} ${DLM})
44+
set(DLM)
45+
endif(USE_CLING)
46+
if (USE_REPL)
47+
#Use DML optional sources
48+
endif(USE_REPL)
4949

50-
# Get rid of libLLVM-X.so which is appended to the list of static libraries.
51-
if (LLVM_LINK_LLVM_DYLIB)
52-
set(new_libs ${link_libs})
53-
set(libs ${new_libs})
54-
while(NOT "${new_libs}" STREQUAL "")
55-
foreach(lib ${new_libs})
56-
if(TARGET ${lib})
57-
get_target_property(transitive_libs ${lib} INTERFACE_LINK_LIBRARIES)
58-
if (NOT transitive_libs)
59-
continue()
60-
endif()
61-
foreach(transitive_lib ${transitive_libs})
62-
get_target_property(lib_type ${transitive_lib} TYPE)
63-
if("${lib_type}" STREQUAL "STATIC_LIBRARY")
64-
list(APPEND static_transitive_libs ${transitive_lib})
65-
else()
66-
# Filter our libLLVM.so and friends.
50+
if (USE_CLING)
51+
set(cling_clang_interp clingInterpreter)
52+
endif()
53+
if (USE_REPL)
54+
set(cling_clang_interp clangInterpreter)
55+
endif()
56+
57+
set(link_libs
58+
${cling_clang_interp}
59+
clangAST
60+
clangBasic
61+
clangFrontend
62+
clangLex
63+
clangSema
64+
)
65+
66+
if(NOT WIN32)
67+
list(APPEND link_libs dl)
68+
endif()
69+
70+
# Get rid of libLLVM-X.so which is appended to the list of static libraries.
71+
if (LLVM_LINK_LLVM_DYLIB)
72+
set(new_libs ${link_libs})
73+
set(libs ${new_libs})
74+
while(NOT "${new_libs}" STREQUAL "")
75+
foreach(lib ${new_libs})
76+
if(TARGET ${lib})
77+
get_target_property(transitive_libs ${lib} INTERFACE_LINK_LIBRARIES)
78+
if (NOT transitive_libs)
6779
continue()
6880
endif()
69-
if(NOT ${transitive_lib} IN_LIST libs)
70-
list(APPEND newer_libs ${transitive_lib})
71-
list(APPEND libs ${transitive_lib})
72-
endif()
73-
endforeach(transitive_lib)
74-
# Update the target properties with the list of only static libraries.
75-
set_target_properties(${lib} PROPERTIES INTERFACE_LINK_LIBRARIES "${static_transitive_libs}")
76-
set(static_transitive_libs "")
77-
endif()
78-
endforeach(lib)
79-
set(new_libs ${newer_libs})
80-
set(newer_libs "")
81-
endwhile()
82-
# We just got rid of the libLLVM.so and other components shipped as shared
83-
# libraries, we need to make up for the missing dependency.
84-
list(APPEND LLVM_LINK_COMPONENTS
85-
Coverage
86-
FrontendHLSL
87-
LTO
88-
)
89-
# We will need to append the missing dependencies to pull in the right
90-
# LLVM library dependencies.
91-
list(APPEND link_libs
92-
clangCodeGen
93-
clangStaticAnalyzerCore
94-
)
95-
endif(LLVM_LINK_LLVM_DYLIB)
81+
foreach(transitive_lib ${transitive_libs})
82+
get_target_property(lib_type ${transitive_lib} TYPE)
83+
if("${lib_type}" STREQUAL "STATIC_LIBRARY")
84+
list(APPEND static_transitive_libs ${transitive_lib})
85+
else()
86+
# Filter our libLLVM.so and friends.
87+
continue()
88+
endif()
89+
if(NOT ${transitive_lib} IN_LIST libs)
90+
list(APPEND newer_libs ${transitive_lib})
91+
list(APPEND libs ${transitive_lib})
92+
endif()
93+
endforeach(transitive_lib)
94+
# Update the target properties with the list of only static libraries.
95+
set_target_properties(${lib} PROPERTIES INTERFACE_LINK_LIBRARIES "${static_transitive_libs}")
96+
set(static_transitive_libs "")
97+
endif()
98+
endforeach(lib)
99+
set(new_libs ${newer_libs})
100+
set(newer_libs "")
101+
endwhile()
102+
# We just got rid of the libLLVM.so and other components shipped as shared
103+
# libraries, we need to make up for the missing dependency.
104+
list(APPEND LLVM_LINK_COMPONENTS
105+
Coverage
106+
FrontendHLSL
107+
LTO
108+
)
109+
# We will need to append the missing dependencies to pull in the right
110+
# LLVM library dependencies.
111+
list(APPEND link_libs
112+
clangCodeGen
113+
clangStaticAnalyzerCore
114+
)
115+
endif(LLVM_LINK_LLVM_DYLIB)
96116

97-
add_llvm_library(clangCppInterOp
98-
DISABLE_LLVM_LINK_LLVM_DYLIB
99-
CppInterOp.cpp
100-
CXCppInterOp.cpp
101-
${DLM}
102-
LINK_LIBS
103-
${link_libs}
104-
)
117+
add_llvm_library(clangCppInterOp
118+
DISABLE_LLVM_LINK_LLVM_DYLIB
119+
CppInterOp.cpp
120+
CXCppInterOp.cpp
121+
${DLM}
122+
LINK_LIBS
123+
${link_libs}
124+
)
125+
endif()
105126

106127
string(REPLACE ";" "\;" _VER CPPINTEROP_VERSION)
107128
set_source_files_properties(CppInterOp.cpp PROPERTIES COMPILE_DEFINITIONS

0 commit comments

Comments
 (0)