Skip to content

Commit

Permalink
Attempt to use a shared build for CppInterOp (#1454)
Browse files Browse the repository at this point in the history
* Attempt to use a shared build for CppInterOp
  • Loading branch information
anutosh491 authored Dec 5, 2024
1 parent 453e5d4 commit 63fa946
Show file tree
Hide file tree
Showing 3 changed files with 131 additions and 57 deletions.
4 changes: 2 additions & 2 deletions recipes/recipes_emscripten/cppinterop/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ export CMAKE_PREFIX_PATH=$PREFIX
export CMAKE_SYSTEM_PREFIX_PATH=$PREFIX

# Configure step
emcmake cmake -DCMAKE_BUILD_TYPE=Release \
emcmake cmake -DCMAKE_BUILD_TYPE=Release \
-DUSE_CLING=OFF \
-DUSE_REPL=ON \
-DCMAKE_PREFIX_PATH=$PREFIX \
-DLLVM_DIR=$PREFIX \
-DLLD_DIR=$PREFIX \
-DClang_DIR=$PREFIX \
-DBUILD_SHARED_LIBS=OFF \
-DBUILD_SHARED_LIBS=ON \
-DCMAKE_INSTALL_PREFIX=$PREFIX \
-DCMAKE_FIND_ROOT_PATH_MODE_PACKAGE=ON \
../
Expand Down
182 changes: 128 additions & 54 deletions recipes/recipes_emscripten/cppinterop/patches/lld.patch
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
diff --git a/CMakeLists.txt b/CMakeLists.txt
index bcd16a9..8060d9f 100644
index bcd16a9..1b0a528 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -24,11 +24,28 @@ if( CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR )
Expand Down Expand Up @@ -97,36 +97,11 @@ index bcd16a9..8060d9f 100644
## Find supported Clang

if (DEFINED CLANG_VERSION)
@@ -237,6 +299,7 @@ if( CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR )
if (USE_CLING)
include_directories(SYSTEM ${CLING_INCLUDE_DIRS})
endif(USE_CLING)
+ include_directories(SYSTEM ${LLD_INCLUDE_DIRS})
include_directories(SYSTEM ${CLANG_INCLUDE_DIRS})
include_directories(SYSTEM ${LLVM_INCLUDE_DIRS})
separate_arguments(LLVM_DEFINITIONS_LIST NATIVE_COMMAND ${LLVM_DEFINITIONS})
@@ -247,6 +310,7 @@ if( CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR )
endif(USE_CLING)
message(STATUS "CLANG_INCLUDE_DIRS: ${CLANG_INCLUDE_DIRS}")
message(STATUS "LLVM_INCLUDE_DIRS: ${LLVM_INCLUDE_DIRS}")
+ message(STATUS "LLD_INCLUDE_DIRS: ${LLD_INCLUDE_DIRS}")
message(STATUS "LLVM_DEFINITIONS_LIST: ${LLVM_DEFINITIONS_LIST}")

# If the llvm sources are present add them with higher priority.
@@ -299,7 +363,7 @@ endif()

# Add appropriate flags for GCC
if (LLVM_COMPILER_IS_GCC_COMPATIBLE)
- set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-common -Woverloaded-virtual -Wcast-qual -fno-strict-aliasing -pedantic -Wno-long-long -Wall -W -Wno-unused-parameter -Wwrite-strings")
+ set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-common -Woverloaded-virtual -Wcast-qual -fno-strict-aliasing -pedantic -Wno-long-long -Wall -W -Wno-unused-parameter -Wwrite-strings -fPIC")
endif ()

# Fixes "C++ exception handler used, but unwind semantics are not enabled" warning Windows
diff --git a/lib/Interpreter/CMakeLists.txt b/lib/Interpreter/CMakeLists.txt
index 7f4ca53..a151d96 100644
index 7f4ca53..7df92e7 100644
--- a/lib/Interpreter/CMakeLists.txt
+++ b/lib/Interpreter/CMakeLists.txt
@@ -1,108 +1,22 @@
@@ -1,106 +1,126 @@
-set(LLVM_LINK_COMPONENTS
- ${LLVM_TARGETS_TO_BUILD}
- BinaryFormat
Expand All @@ -142,19 +117,16 @@ index 7f4ca53..a151d96 100644
-if ("LLVMOrcDebugging" IN_LIST LLVM_AVAILABLE_LIBS)
- list(APPEND LLVM_LINK_COMPONENTS OrcDebugging)
-endif()
+set_property(GLOBAL PROPERTY TARGET_SUPPORTS_SHARED_LIBS TRUE)
+set(CMAKE_SHARED_LIBRARY_CREATE_C_FLAGS "-s SIDE_MODULE=1 -s EXPORT_ALL=1 -s LINKABLE=1")
+set(CMAKE_SHARED_LIBRARY_CREATE_CXX_FLAGS "-s SIDE_MODULE=1 -s EXPORT_ALL=1 -s LINKABLE=1")
+set(CMAKE_STRIP FALSE)
+
+add_llvm_library(clangCppInterOp
+ SHARED
+if(EMSCRIPTEN)
+ set_property(GLOBAL PROPERTY TARGET_SUPPORTS_SHARED_LIBS TRUE)
+ set(CMAKE_SHARED_LIBRARY_CREATE_C_FLAGS "-s SIDE_MODULE=1")
+ set(CMAKE_SHARED_LIBRARY_CREATE_CXX_FLAGS "-s SIDE_MODULE=1")
+ set(CMAKE_STRIP FALSE)

-set(DLM
+ CppInterOp.cpp
DynamicLibraryManager.cpp
DynamicLibraryManagerSymbol.cpp
Paths.cpp
- DynamicLibraryManager.cpp
- DynamicLibraryManagerSymbol.cpp
- Paths.cpp
-)
-if (USE_CLING)
- set(LLVM_OPTIONAL_SOURCES ${LLVM_OPTIONAL_SOURCES} ${DLM})
Expand All @@ -163,27 +135,64 @@ index 7f4ca53..a151d96 100644
-if (USE_REPL)
- #Use DML optional sources
-endif(USE_REPL)
-
+ add_llvm_library(clangCppInterOp
+ SHARED

-if (USE_CLING)
- set(cling_clang_interp clingInterpreter)
-endif()
-if (USE_REPL)
- set(cling_clang_interp clangInterpreter)
-endif()
-
+ CppInterOp.cpp
+ DynamicLibraryManager.cpp
+ DynamicLibraryManagerSymbol.cpp
+ Paths.cpp

-set(link_libs
- ${cling_clang_interp}
- clangAST
- clangBasic
- clangFrontend
- clangLex
- clangSema
- )
+ # Additional libraries from Clang and LLD
+ LINK_LIBS
+ clangInterpreter
+ )
+else()
+ set(LLVM_LINK_COMPONENTS
+ ${LLVM_TARGETS_TO_BUILD}
+ BinaryFormat
+ Core
+ Object
+ OrcJit
+ Support
)
+ # FIXME: Investigate why this needs to be conditionally included.
+ if ("LLVMFrontendDriver" IN_LIST LLVM_AVAILABLE_LIBS)
+ list(APPEND LLVM_LINK_COMPONENTS FrontendDriver)
+ endif()
+ if ("LLVMOrcDebugging" IN_LIST LLVM_AVAILABLE_LIBS)
+ list(APPEND LLVM_LINK_COMPONENTS OrcDebugging)
+ endif()

- if(NOT WIN32)
- list(APPEND link_libs dl)
- endif()
-
+ set(DLM
+ DynamicLibraryManager.cpp
+ DynamicLibraryManagerSymbol.cpp
+ Paths.cpp
+ )
+ if (USE_CLING)
+ set(LLVM_OPTIONAL_SOURCES ${LLVM_OPTIONAL_SOURCES} ${DLM})
+ set(DLM)
+ endif(USE_CLING)
+ if (USE_REPL)
+ #Use DML optional sources
+ endif(USE_REPL)

-# Get rid of libLLVM-X.so which is appended to the list of static libraries.
-if (LLVM_LINK_LLVM_DYLIB)
- set(new_libs ${link_libs})
Expand All @@ -201,8 +210,37 @@ index 7f4ca53..a151d96 100644
- list(APPEND static_transitive_libs ${transitive_lib})
- else()
- # Filter our libLLVM.so and friends.
- continue()
- endif()
+ if (USE_CLING)
+ set(cling_clang_interp clingInterpreter)
+ endif()
+ if (USE_REPL)
+ set(cling_clang_interp clangInterpreter)
+ endif()
+
+ set(link_libs
+ ${cling_clang_interp}
+ clangAST
+ clangBasic
+ clangFrontend
+ clangLex
+ clangSema
+ )
+
+ if(NOT WIN32)
+ list(APPEND link_libs dl)
+ endif()
+
+ # Get rid of libLLVM-X.so which is appended to the list of static libraries.
+ if (LLVM_LINK_LLVM_DYLIB)
+ set(new_libs ${link_libs})
+ set(libs ${new_libs})
+ while(NOT "${new_libs}" STREQUAL "")
+ foreach(lib ${new_libs})
+ if(TARGET ${lib})
+ get_target_property(transitive_libs ${lib} INTERFACE_LINK_LIBRARIES)
+ if (NOT transitive_libs)
continue()
endif()
- if(NOT ${transitive_lib} IN_LIST libs)
- list(APPEND newer_libs ${transitive_lib})
- list(APPEND libs ${transitive_lib})
Expand Down Expand Up @@ -230,21 +268,57 @@ index 7f4ca53..a151d96 100644
- clangStaticAnalyzerCore
- )
-endif(LLVM_LINK_LLVM_DYLIB)
-
+ foreach(transitive_lib ${transitive_libs})
+ get_target_property(lib_type ${transitive_lib} TYPE)
+ if("${lib_type}" STREQUAL "STATIC_LIBRARY")
+ list(APPEND static_transitive_libs ${transitive_lib})
+ else()
+ # Filter our libLLVM.so and friends.
+ continue()
+ endif()
+ if(NOT ${transitive_lib} IN_LIST libs)
+ list(APPEND newer_libs ${transitive_lib})
+ list(APPEND libs ${transitive_lib})
+ endif()
+ endforeach(transitive_lib)
+ # Update the target properties with the list of only static libraries.
+ set_target_properties(${lib} PROPERTIES INTERFACE_LINK_LIBRARIES "${static_transitive_libs}")
+ set(static_transitive_libs "")
+ endif()
+ endforeach(lib)
+ set(new_libs ${newer_libs})
+ set(newer_libs "")
+ endwhile()
+ # We just got rid of the libLLVM.so and other components shipped as shared
+ # libraries, we need to make up for the missing dependency.
+ list(APPEND LLVM_LINK_COMPONENTS
+ Coverage
+ FrontendHLSL
+ LTO
+ )
+ # We will need to append the missing dependencies to pull in the right
+ # LLVM library dependencies.
+ list(APPEND link_libs
+ clangCodeGen
+ clangStaticAnalyzerCore
+ )
+ endif(LLVM_LINK_LLVM_DYLIB)

-add_llvm_library(clangCppInterOp
- DISABLE_LLVM_LINK_LLVM_DYLIB
- CppInterOp.cpp
- ${DLM}
+ # Additional libraries from Clang and LLD
LINK_LIBS
- LINK_LIBS
- ${link_libs}
- )
+ clangInterpreter
+)
+ add_llvm_library(clangCppInterOp
+ DISABLE_LLVM_LINK_LLVM_DYLIB
+ CppInterOp.cpp
+ ${DLM}
+ LINK_LIBS
+ ${link_libs}
+ )
+endif()

string(REPLACE ";" "\;" _VER CPPINTEROP_VERSION)
set_source_files_properties(CppInterOp.cpp PROPERTIES COMPILE_DEFINITIONS
"LLVM_BINARY_DIR=\"${LLVM_BINARY_DIR}\";CPPINTEROP_VERSION=\"${_VAR}\""
-)
+)
\ No newline at end of file
2 changes: 1 addition & 1 deletion recipes/recipes_emscripten/cppinterop/recipe.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ source:
- patches/new.patch

build:
number: 3
number: 4

requirements:
build:
Expand Down

0 comments on commit 63fa946

Please sign in to comment.