-
Notifications
You must be signed in to change notification settings - Fork 2
/
CMakeLists.txt
126 lines (106 loc) · 3.46 KB
/
CMakeLists.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
cmake_minimum_required(VERSION 3.17)
project(jack2llvm)
set(CMAKE_VERBOSE_MAKEFILE ON)
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_VISIBILITY_PRESET default)
################################
# Prepare third party packages #
################################
if(APPLE)
set(LLVM_DIR ${CMAKE_SOURCE_DIR}/third_party/llvm-10.0.0-m1/lib/cmake/llvm)
set(ANTLR_RUNTIME_DIR ${CMAKE_SOURCE_DIR}/third_party/antlr_runtime_lib/runtime-osx/)
else()
set(LLVM_DIR ${CMAKE_SOURCE_DIR}/third_party/taichi-llvm-10.0.0-linux/lib/cmake/llvm)
set(ANTLR_RUNTIME_DIR ${CMAKE_SOURCE_DIR}/third_party/antlr_runtime_lib/runtime-linux/)
endif()
find_package(Java REQUIRED)
find_package(glog REQUIRED)
# Get LLVM
# http://llvm.org/docs/CMake.html#embedding-llvm-in-your-project
find_package(LLVM REQUIRED CONFIG NO_DEFAULT_PATH)
add_definitions(${LLVM_DEFINITIONS})
llvm_map_components_to_libnames(llvm_libs
Core
ExecutionEngine
InstCombine
OrcJIT
RuntimeDyld
TransformUtils
BitReader
BitWriter
Object
ScalarOpts
Support
native
Linker
Target
MC
Passes
ipo
Analysis
)
if (APPLE AND "${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "arm64")
llvm_map_components_to_libnames(llvm_aarch64_libs AArch64)
endif()
message("LLVM include dirs ${LLVM_INCLUDE_DIRS}")
message("LLVM library dirs ${LLVM_LIBRARY_DIRS}")
# Prepare antlr lexer and parser source codes
set(LANG_DIRECTORY "${CMAKE_SOURCE_DIR}/src/language_src")
file(MAKE_DIRECTORY ${LANG_DIRECTORY})
execute_process(
COMMAND
${Java_JAVA_EXECUTABLE} -jar ${CMAKE_SOURCE_DIR}/third_party/antlr-4.9.3-complete.jar -Dlanguage=Cpp -o ${LANG_DIRECTORY} ${CMAKE_SOURCE_DIR}/src/Jack.g4 -no-listener -visitor
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
)
###############################################
# Compile antlr lexer and parser source codes #
###############################################
add_library(language_lib OBJECT
${LANG_DIRECTORY}/JackLexer.cpp
${LANG_DIRECTORY}/JackParser.cpp
${LANG_DIRECTORY}/JackVisitor.cpp
)
target_include_directories(language_lib
PUBLIC
${CMAKE_SOURCE_DIR}/src
${LANG_DIRECTORY}
${ANTLR_RUNTIME_DIR}/antlr4-runtime
)
#########################
# Compile Jack compiler #
#########################
add_library(jack_lib OBJECT
${CMAKE_SOURCE_DIR}/src/JackExpressionVisitor.cpp
${CMAKE_SOURCE_DIR}/src/JackStatementVisitor.cpp
${CMAKE_SOURCE_DIR}/src/JackClassVisitor.cpp
)
target_link_libraries (jack_lib glog::glog)
target_include_directories(jack_lib
PUBLIC
${CMAKE_SOURCE_DIR}/src
${LANG_DIRECTORY}
${ANTLR_RUNTIME_DIR}/antlr4-runtime
${LLVM_INCLUDE_DIR}
)
###############################
# Compile compiler executable #
###############################
add_executable(jack2llvm ${CMAKE_SOURCE_DIR}/src/main.cpp)
target_include_directories(jack2llvm PUBLIC ${LLVM_INCLUDE_DIRS})
target_link_libraries(jack2llvm jack_lib language_lib)
target_link_libraries(jack2llvm ${ANTLR_RUNTIME_DIR}/lib/libantlr4-runtime.a)
target_link_libraries(jack2llvm glog::glog)
target_include_directories(jack2llvm
PUBLIC
${CMAKE_SOURCE_DIR}/src
${LANG_DIRECTORY}
${ANTLR_RUNTIME_DIR}/antlr4-runtime
)
target_link_libraries(jack2llvm ${llvm_libs})
# Build test cases
include(cmake/Test.cmake)
add_subdirectory(tests)
string(REPLACE " " "\n" JACK_TEST_CASES ${JACK_TEST_CASES})
FILE(WRITE ${CMAKE_BINARY_DIR}/run_tests.sh
${JACK_TEST_CASES}
)