-
Notifications
You must be signed in to change notification settings - Fork 7
/
CMakeLists.txt
305 lines (250 loc) · 10.7 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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
# Copyright 2017 Politecnico di Milano.
# Developed by : Stefano Cherubin, Davide Gadioli
# PhD student, Politecnico di Milano
# <first_name>.<family_name>@polimi.it
#
# This file is part of libVersioningCompiler
#
# libVersioningCompiler is free software: you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public License
# as published by the Free Software Foundation, either version 3
# of the License, or (at your option) any later version.
#
# libVersioningCompiler is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with libVersioningCompiler. If not, see <http://www.gnu.org/licenses/>
# cmake_path had been introduced in cmake 3.20
cmake_minimum_required(VERSION 3.20)
project(
libVersioningCompiler
VERSION 3.0
DESCRIPTION
"library for runtime compilation and dynamic loading of a C/C++ function"
LANGUAGES CXX C)
#----- Set the name of the Version Compiler library
set(VC_LIB_NAME "VersioningCompiler")
#############################################
# Set the library directory #
#############################################
link_directories(${CMAKE_CURRENT_BINARY_DIR})
#############################################
# EXTERNAL LIB #
#############################################
# Load additional CMake modules
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
# check for DL library
if(NOT APPLE AND NOT CMAKE_DL_LIBS)
message(FATAL_ERROR "Missing DL libs!")
endif(NOT APPLE AND NOT CMAKE_DL_LIBS)
# The uuuid library
find_package(UUID REQUIRED)
include_directories(${UUID_INCLUDE_DIR})
link_directories(${UUID_LIBRARY})
# LibClang library
find_package(LibClang)
if(LIBCLANG_FOUND)
include_directories(${LIBCLANG_INCLUDE_DIRS})
link_directories(${LIBCLANG_LIBRARY_DIR})
endif(LIBCLANG_FOUND)
# LLVM library
find_package(LLVM_config)
if(LLVM_FOUND)
include_directories(${LLVM_INCLUDE_DIR})
link_directories(${LLVM_LIBRARY_DIR})
endif(LLVM_FOUND)
set(LLVM_VERSION_MIN_REQUIRED "13.0.0")
if(LLVM_FOUND
AND (NOT (LLVM_PACKAGE_VERSION VERSION_LESS LLVM_VERSION_MIN_REQUIRED))
AND LIBCLANG_FOUND)
set(ENABLE_CLANG_AS_LIB true)
else()
if(NOT LLVM_FOUND)
message(WARNING "LLVM not detected")
else(NOT LLVM_FOUND)
if(LLVM_PACKAGE_VERSION VERSION_LESS LLVM_VERSION_MIN_REQUIRED)
message(WARNING "You are using an obsolete version of LLVM:\
\n\t${LLVM_PACKAGE_VERSION} detected.\t\
Minimum required is ${LLVM_VERSION_MIN_REQUIRED}")
else(LLVM_PACKAGE_VERSION VERSION_LESS LLVM_VERSION_MIN_REQUIRED)
message(WARNING "LibClang not detected")
endif(LLVM_PACKAGE_VERSION VERSION_LESS LLVM_VERSION_MIN_REQUIRED)
endif(NOT LLVM_FOUND)
message(WARNING "Clang-as-a-library will be disabled")
endif()
set(VC_LIB_DEPS ${CMAKE_DL_LIBS} ${UUID_LIBRARY} stdc++)
if(ENABLE_CLANG_AS_LIB)
set(VC_LIB_DEPS ${VC_LIB_DEPS} ${LIBCLANG_LIBRARIES} ${LLVM_MODULE_LIBFILES}
${LLVM_SYSTEM})
set(VC_DEPS_EXTRA_LINK_FLAGS "${LLVM_LFLAGS}")
endif(ENABLE_CLANG_AS_LIB)
list(REMOVE_DUPLICATES VC_LIB_DEPS)
#############################################
# LIBRARY #
#############################################
if(NOT CMAKE_BUILD_TYPE)
message("No build type detected. Build type set to Release by default")
set(CMAKE_BUILD_TYPE Release)
endif()
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
# https://gcc.gnu.org/wiki/Visibility#line-14 - Much lower chance of symbol collision.
set(CMAKE_VISIBILITY_INLINES_HIDDEN ON)
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
# CMAKE_INTERPROCEDURAL_OPTIMIZATION applies different optimizations depending on the compiler.
# It breaks cross compiler compatibility (e.g. library compiled with g++ which is being used with clang later on), so
# I'm not enabling link time optimization by default.
set(CMAKE_INTERPROCEDURAL_OPTIMIZATION OFF)
# There is no no-rtti cmake setting so far (related https://gitlab.kitware.com/cmake/cmake/-/issues/17991) and no no-use-cxa-atexit cmake setting as well.
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-use-cxa-atexit -fno-rtti")
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -O3")
#----- Sources
set(SRC_PREFIX "lib")
set(VC_LIB_SRC
${SRC_PREFIX}/Version.cpp
${SRC_PREFIX}/Option.cpp
${SRC_PREFIX}/Compiler.cpp
${SRC_PREFIX}/CompilerImpl/SystemCompiler.cpp
${SRC_PREFIX}/CompilerImpl/SystemCompilerOptimizer.cpp
${SRC_PREFIX}/CompilerImpl/TAFFOCompiler.cpp)
#----- Headers
set(VC_LIB_HDR_PREFIX ${CMAKE_CURRENT_SOURCE_DIR}/include/versioningCompiler)
set(VC_LIB_HDR1
${VC_LIB_HDR_PREFIX}/Version.hpp ${VC_LIB_HDR_PREFIX}/Option.hpp
${VC_LIB_HDR_PREFIX}/Compiler.hpp ${VC_LIB_HDR_PREFIX}/Utils.hpp
${VC_LIB_HDR_PREFIX}/DebugUtils.hpp ${VC_LIB_HDR_PREFIX}/JITUtils.hpp)
set(VC_LIB_HDR_PREFIX2
${CMAKE_CURRENT_SOURCE_DIR}/include/versioningCompiler/CompilerImpl)
set(VC_LIB_HDR2
${VC_LIB_HDR_PREFIX2}/SystemCompiler.hpp
${VC_LIB_HDR_PREFIX2}/SystemCompilerOptimizer.hpp
${VC_LIB_HDR_PREFIX2}/TAFFOCompiler.hpp)
#----- Additionl files to enable Clang-as-a-library
if(ENABLE_CLANG_AS_LIB)
set(COMPILE_DEFINITIONS "ENABLE_CLANG_AS_LIB=true")
set(VC_LIB_SRC
${VC_LIB_SRC}
${SRC_PREFIX}/CompilerImpl/JITCompiler.cpp
${SRC_PREFIX}/CompilerImpl/ClangLibCompiler.cpp
${SRC_PREFIX}/CompilerImpl/ClangLLVM/FileLogDiagnosticConsumer.cpp
${SRC_PREFIX}/CompilerImpl/ClangLLVM/LLVMInstanceManager.cpp)
set(VC_LIB_HDR2 ${VC_LIB_HDR2} ${VC_LIB_HDR_PREFIX2}/JITCompiler.hpp
${VC_LIB_HDR_PREFIX2}/ClangLibCompiler.hpp)
set(VC_LIB_HDR_PREFIX3
${CMAKE_CURRENT_SOURCE_DIR}/include/versioningCompiler/CompilerImpl/ClangLLVM
)
set(VC_LIB_HDR3
${VC_LIB_HDR_PREFIX3}/OptUtils.hpp
${VC_LIB_HDR_PREFIX3}/FileLogDiagnosticConsumer.hpp
${VC_LIB_HDR_PREFIX3}/LLVMInstanceManager.hpp)
endif(ENABLE_CLANG_AS_LIB)
set(VC_LIB_HDR ${VC_LIB_HDR1} ${VC_LIB_HDR2} ${VC_LIB_HDR3})
#----- setup library
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/include)
if(ENABLE_CLANG_AS_LIB)
link_directories(${LLVM_LIBRARY_DIR} ${LIBCLANG_LIBRARY_DIR}
"${CMAKE_INSTALL_PREFIX}/lib")
endif(ENABLE_CLANG_AS_LIB)
add_library(${VC_LIB_NAME} STATIC ${VC_LIB_SRC} ${VC_LIB_HDR})
target_link_libraries(${VC_LIB_NAME} ${VC_LIB_DEPS})
#----- additional filesystem helpers
include("${CMAKE_CURRENT_LIST_DIR}/cmake/NormalizePathHelper.cmake")
#############################################
# CONFIGURE #
#############################################
set(FIND_LIBVC_OUTPUT
"${CMAKE_CURRENT_SOURCE_DIR}/config/FindLibVersioningCompiler.cmake")
configure_file(
"${CMAKE_CURRENT_SOURCE_DIR}/config/FindLibVersioningCompiler.cmake.in"
"${FIND_LIBVC_OUTPUT}" @ONLY)
#############################################
# INSTALL LIBRARY VC #
#############################################
install(TARGETS ${VC_LIB_NAME} DESTINATION lib)
install(FILES ${VC_LIB_HDR1} DESTINATION include/versioningCompiler)
install(FILES ${VC_LIB_HDR2}
DESTINATION include/versioningCompiler/CompilerImpl)
install(FILES ${FIND_LIBVC_OUTPUT} DESTINATION lib/cmake)
if(ENABLE_CLANG_AS_LIB)
install(FILES ${VC_LIB_HDR3}
DESTINATION include/versioningCompiler/CompilerImpl/ClangLLVM)
endif(ENABLE_CLANG_AS_LIB)
#############################################
# TEST APP #
#############################################
# If not specified, guess which c++ library should use
if(NOT CPP_LIBRARY)
if(APPLE)
set(CPP_LIBRARY "c++")
elseif(UNIX)
set(CPP_LIBRARY "stdc++")
else()
set(CPP_LIBRARY "c++")
endif()
endif(NOT CPP_LIBRARY)
# TestJit.cpp
#----- Sources
# It does make no sense building it if there is no LLVM, given that the JIT implementation relies on LLVM.
if(ENABLE_CLANG_AS_LIB)
set(VC_TESTJIT_APP_SRC TestJit.cpp)
set(VC_EXEJIT_NAME libVC_testJit)
add_executable(${VC_EXEJIT_NAME} ${VC_TESTJIT_APP_SRC})
target_link_libraries(${VC_EXEJIT_NAME} ${VC_LIB_NAME} ${VC_LIB_DEPS}
${CPP_LIBRARY})
target_compile_definitions(${VC_EXEJIT_NAME} PRIVATE -DHAVE_CLANG_AS_LIB)
set(TEST_CODE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/test_code")
convert_to_native_normalized_path(${TEST_CODE_PATH})
target_compile_definitions(${VC_EXEJIT_NAME}
PRIVATE -DFORCED_PATH_TO_TEST="${TEST_CODE_PATH}")
if(CMAKE_BUILD_TYPE MATCHES DEBUG)
target_compile_definitions(${VC_EXEJIT_NAME} PRIVATE -DVC_DEBUG)
endif(CMAKE_BUILD_TYPE MATCHES DEBUG)
endif(ENABLE_CLANG_AS_LIB)
# Test.cpp
#----- Sources
set(VC_TEST_APP_SRC Test.cpp)
set(VC_EXE_NAME libVC_test)
add_executable(${VC_EXE_NAME} ${VC_TEST_APP_SRC})
target_link_libraries(${VC_EXE_NAME} ${VC_LIB_NAME} ${VC_LIB_DEPS}
${CPP_LIBRARY})
set(TEST_CODE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/test_code")
convert_to_native_normalized_path(${TEST_CODE_PATH})
target_compile_definitions(${VC_EXE_NAME}
PRIVATE -DFORCED_PATH_TO_TEST="${TEST_CODE_PATH}")
target_compile_definitions(
${VC_EXE_NAME} PRIVATE -DLLVM_TOOLS_BINARY_DIR="${LLVM_TOOLS_BINARY_DIR}")
if(ENABLE_CLANG_AS_LIB)
target_compile_definitions(${VC_EXE_NAME} PRIVATE -DHAVE_CLANG_AS_LIB)
endif(ENABLE_CLANG_AS_LIB)
if(CMAKE_BUILD_TYPE MATCHES DEBUG)
target_compile_definitions(${VC_EXE_NAME} PRIVATE -DVC_DEBUG)
endif(CMAKE_BUILD_TYPE MATCHES DEBUG)
# testUtils.cpp
#----- Sources
set(VC_TESTUTILS_APP_SRC TestUtils.cpp)
set(VC_EXEUTILS_NAME libVC_testUtils)
add_executable(${VC_EXEUTILS_NAME} ${VC_TESTUTILS_APP_SRC})
target_link_libraries(${VC_EXEUTILS_NAME} ${VC_LIB_NAME} ${VC_LIB_DEPS}
${CPP_LIBRARY})
set(TEST_CODE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/test_code")
convert_to_native_normalized_path(${TEST_CODE_PATH})
target_compile_definitions(${VC_EXEUTILS_NAME}
PRIVATE -DFORCED_PATH_TO_TEST="${TEST_CODE_PATH}")
if(ENABLE_CLANG_AS_LIB)
target_compile_definitions(${VC_EXEUTILS_NAME} PRIVATE -DHAVE_CLANG_AS_LIB)
endif(ENABLE_CLANG_AS_LIB)
if(CMAKE_BUILD_TYPE MATCHES DEBUG)
target_compile_definitions(${VC_EXEUTILS_NAME} PRIVATE -DVC_DEBUG)
endif(CMAKE_BUILD_TYPE MATCHES DEBUG)
#############################################
# INSTALL TEST BIN #
#############################################
install(TARGETS ${VC_EXE_NAME} DESTINATION bin/test)
install(TARGETS ${VC_EXEUTILS_NAME} DESTINATION bin/test)
if(ENABLE_CLANG_AS_LIB)
install(TARGETS ${VC_EXEJIT_NAME} DESTINATION bin/test)
endif(ENABLE_CLANG_AS_LIB)