Skip to content
This repository was archived by the owner on Jul 3, 2021. It is now read-only.

Commit 87cb540

Browse files
Wire up LLVM
1 parent f9f8c82 commit 87cb540

File tree

3 files changed

+63
-3
lines changed

3 files changed

+63
-3
lines changed

CMakeLists.txt

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,52 @@ if(NOT CMAKE_TOOLCHAIN_FILE)
88
include(DefaultToolchain)
99
endif()
1010

11+
include(LLVM)
12+
13+
if(LLVM_ENABLE_RTTI)
14+
message(WARNING "LLVM built with RTTI; enabling it for JitFromScratch to avoid quirks")
15+
set(rtti_flags ${HOST_ENABLE_RTTI})
16+
else()
17+
set(rtti_flags ${HOST_DISABLE_RTTI})
18+
endif()
19+
20+
# If we have a LLVM source-tree, symlink it for debug source maps
21+
if(LLVM_BUILD_MAIN_SRC_DIR AND EXISTS "${LLVM_BUILD_MAIN_SRC_DIR}" AND UNIX)
22+
execute_process(
23+
COMMAND ${CMAKE_COMMAND} -E create_symlink ${LLVM_BUILD_MAIN_SRC_DIR}
24+
${CMAKE_BINARY_DIR}/llvm-project
25+
)
26+
endif()
27+
28+
if(NOT CMAKE_BUILD_TYPE)
29+
message(STATUS "No explicit CMAKE_BUILD_TYPE provided; JitFromScratch defaults to the build type of LLVM")
30+
set(CMAKE_BUILD_TYPE ${LLVM_BUILD_TYPE} CACHE STRING "" FORCE)
31+
elseif(NOT ${LLVM_BUILD_TYPE} STREQUAL ${CMAKE_BUILD_TYPE})
32+
message(WARNING "JitFromScratch set to build as ${CMAKE_BUILD_TYPE}, which differs from LLVM's build type")
33+
endif()
34+
1135
add_executable(JitFromScratch
1236
main.cpp
1337
)
1438

1539
target_include_directories(JitFromScratch PRIVATE
1640
${CMAKE_CURRENT_SOURCE_DIR}
41+
${LLVM_INCLUDE_DIRS}
1742
)
1843

44+
set(llvm_libs
45+
LLVMBinaryFormat
46+
LLVMCore
47+
LLVMDemangle
48+
LLVMRemarks
49+
LLVMSupport
50+
)
51+
52+
separate_arguments(LLVM_DEFINITIONS)
53+
target_compile_definitions(JitFromScratch PRIVATE ${LLVM_DEFINITIONS})
54+
target_link_libraries(JitFromScratch PRIVATE ${llvm_libs})
55+
target_compile_options(JitFromScratch PRIVATE ${rtti_flags})
56+
1957
function(dump_target_properties target property)
2058
get_target_property(values ${target} ${property})
2159
if(values)

cmake/modules/LLVM.cmake

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
if(LLVM_DIR)
2+
file(TO_CMAKE_PATH ${LLVM_DIR} LLVM_DIR)
3+
message(STATUS "Configure with explicit LLVM_DIR=${LLVM_DIR}")
4+
else()
5+
message(STATUS
6+
"No explicit LLVM_DIR provided. If find_package() fails, pass "
7+
"-DLLVM_DIR=/path/to/llvm-build/lib/cmake/llvm to CMake.")
8+
endif()
9+
10+
find_package(LLVM 9.0 REQUIRED PATHS /usr/local/opt/llvm/lib/cmake/llvm)
11+
12+
if(LLVM_BUILD_MAIN_SRC_DIR)
13+
message(STATUS "LLVM Source directory: ${LLVM_BUILD_MAIN_SRC_DIR}")
14+
message(STATUS "LLVM Build directory: ${LLVM_BINARY_DIR}")
15+
else()
16+
message(STATUS "LLVM Install directory: ${LLVM_BINARY_DIR}")
17+
endif()
18+
19+
message(STATUS "LLVM Build type: ${LLVM_BUILD_TYPE}")

main.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
#include <stdio.h>
2-
#include <stdlib.h>
1+
#include <llvm/Support/Format.h>
2+
#include <llvm/Support/raw_ostream.h>
3+
4+
using namespace llvm;
35

46
// Determine the size of a C array at compile-time.
57
template <typename T, size_t sizeOfArray>
@@ -39,7 +41,8 @@ int main(int argc, char **argv) {
3941
int y[]{3, 1, -1};
4042
int *z = integerDistances(x, y);
4143

42-
printf("Integer Distances: %d, %d, %d\n\n", z[0], z[1], z[2]);
44+
outs() << format("Integer Distances: %d, %d, %d\n\n", z[0], z[1], z[2]);
45+
outs().flush();
4346

4447
return 0;
4548
}

0 commit comments

Comments
 (0)