Skip to content

Commit 774022a

Browse files
committed
Add LLVM
1 parent 17a8f87 commit 774022a

File tree

3 files changed

+90
-0
lines changed

3 files changed

+90
-0
lines changed

CMakeLists.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ target_sources(scratchcpp
6767

6868
if (LIBSCRATCHCPP_USE_LLVM)
6969
target_compile_definitions(scratchcpp PUBLIC USE_LLVM)
70+
7071
else()
7172
target_sources(scratchcpp
7273
PUBLIC
@@ -103,6 +104,12 @@ if (LIBSCRATCHCPP_NETWORK_SUPPORT)
103104
target_compile_definitions(scratchcpp PRIVATE LIBSCRATCHCPP_NETWORK_SUPPORT)
104105
endif()
105106

107+
if (LIBSCRATCHCPP_USE_LLVM)
108+
include(build/HunterPackages.cmake)
109+
include(build/LLVM.cmake)
110+
target_link_libraries(scratchcpp PRIVATE LLVM)
111+
endif()
112+
106113
target_compile_definitions(scratchcpp PRIVATE LIBSCRATCHCPP_LIBRARY)
107114
target_compile_definitions(scratchcpp PRIVATE LIBSCRATCHCPP_VERSION="${PROJECT_VERSION}")
108115
target_compile_definitions(scratchcpp PRIVATE LIBSCRATCHCPP_VERSION_MAJOR=${PROJECT_VERSION_MAJOR})

build/HunterPackages.cmake

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# https://layle.me/posts/using-llvm-with-cmake/
2+
# HUNTER_URL is the URL to the latest source code archive on GitHub
3+
# HUNTER_SHA1 is the hash of the downloaded archive
4+
5+
set(OLD_PROJECT_NAME ${PROJECT_NAME})
6+
set(PROJECT_NAME "")
7+
8+
set(HUNTER_URL "https://github.com/scratchcpp/hunter/archive/ee768cdd2c027b5be346f114e726d4b0c4296de6.zip")
9+
set(HUNTER_SHA1 "4A018750743AC656A859C99C655723EAF68EE038")
10+
11+
set(HUNTER_LLVM_VERSION 19.1.0)
12+
set(HUNTER_LLVM_CMAKE_ARGS
13+
LLVM_ENABLE_CRASH_OVERRIDES=OFF
14+
LLVM_ENABLE_ASSERTIONS=ON
15+
LLVM_ENABLE_ZLIB=OFF
16+
LLVM_ENABLE_RTTI=ON
17+
LLVM_BUILD_EXAMPLES=OFF
18+
LLVM_BUILD_TOOLS=OFF
19+
LLVM_BUILD_LLVM_DYLIB=ON
20+
LLVM_INCLUDE_EXAMPLES=OFF
21+
LLVM_TARGETS_TO_BUILD=host
22+
)
23+
24+
set(HUNTER_PACKAGES LLVM)
25+
26+
include(FetchContent)
27+
message(STATUS "Fetching hunter...")
28+
FetchContent_Declare(SetupHunter GIT_REPOSITORY https://github.com/cpp-pm/gate)
29+
FetchContent_MakeAvailable(SetupHunter)
30+
31+
set(PROJECT_NAME ${OLD_PROJECT_NAME})

build/LLVM.cmake

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# https://layle.me/posts/using-llvm-with-cmake/
2+
# This is an INTERFACE target for LLVM, usage:
3+
# target_link_libraries(${PROJECT_NAME} <PRIVATE|PUBLIC|INTERFACE> LLVM)
4+
# The include directories and compile definitions will be properly handled.
5+
6+
set(CMAKE_FOLDER_LLVM "${CMAKE_FOLDER}")
7+
if(CMAKE_FOLDER)
8+
set(CMAKE_FOLDER "${CMAKE_FOLDER}/LLVM")
9+
else()
10+
set(CMAKE_FOLDER "LLVM")
11+
endif()
12+
13+
# Find LLVM
14+
find_package(LLVM REQUIRED CONFIG)
15+
16+
message(STATUS "Found LLVM ${LLVM_PACKAGE_VERSION}")
17+
message(STATUS "Using LLVMConfig.cmake in: ${LLVM_DIR}")
18+
19+
# Split the definitions properly (https://weliveindetail.github.io/blog/post/2017/07/17/notes-setup.html)
20+
separate_arguments(LLVM_DEFINITIONS)
21+
22+
# Some diagnostics (https://stackoverflow.com/a/17666004/1806760)
23+
message(STATUS "LLVM libraries: ${LLVM_LIBRARIES}")
24+
message(STATUS "LLVM includes: ${LLVM_INCLUDE_DIRS}")
25+
message(STATUS "LLVM definitions: ${LLVM_DEFINITIONS}")
26+
message(STATUS "LLVM tools: ${LLVM_TOOLS_BINARY_DIR}")
27+
28+
if (NOT TARGET LLVM)
29+
add_library(LLVM INTERFACE)
30+
endif()
31+
32+
target_include_directories(LLVM SYSTEM INTERFACE ${LLVM_INCLUDE_DIRS})
33+
target_link_libraries(LLVM INTERFACE ${LLVM_AVAILABLE_LIBS})
34+
target_compile_definitions(LLVM INTERFACE ${LLVM_DEFINITIONS} -DNOMINMAX)
35+
36+
set(CMAKE_FOLDER "${CMAKE_FOLDER_LLVM}")
37+
unset(CMAKE_FOLDER_LLVM)
38+
39+
# MSVC-specific options
40+
if(MSVC)
41+
# This assumes the installed LLVM was built in Release mode
42+
set(CMAKE_C_FLAGS_RELWITHDEBINFO "/ZI /Od /Ob0 /DNDEBUG" CACHE STRING "" FORCE)
43+
set(CMAKE_CXX_FLAGS_RELWITHDEBINFO "/ZI /Od /Ob0 /DNDEBUG" CACHE STRING "" FORCE)
44+
45+
if(${LLVM_USE_CRT_RELEASE} STREQUAL "MD")
46+
set(CMAKE_MSVC_RUNTIME_LIBRARY MultiThreadedDLL)
47+
elseif(${LLVM_USE_CRT_RELEASE} STREQUAL "MT")
48+
set(CMAKE_MSVC_RUNTIME_LIBRARY MultiThreaded)
49+
else()
50+
message(FATAL_ERROR "Unsupported LLVM_USE_CRT_RELEASE=${LLVM_USE_CRT_RELEASE}")
51+
endif()
52+
endif()

0 commit comments

Comments
 (0)