-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathCMakeLists.txt
493 lines (469 loc) · 18.3 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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
cmake_minimum_required(VERSION 3.14)
project(
Hercules
VERSION "0.2.9"
HOMEPAGE_URL "https://github.com/gottingen/hercules"
DESCRIPTION "high-performance, extensible Python compiler")
set(HERCULES_JIT_PYTHON_VERSION "0.1.7")
set(CMAKE_CXX_STANDARD 17)
configure_file("${PROJECT_SOURCE_DIR}/cmake/config.h.in"
"${PROJECT_SOURCE_DIR}/hercules/config/config.h")
configure_file("${PROJECT_SOURCE_DIR}/cmake/config.py.in"
"${PROJECT_SOURCE_DIR}/jit/hercules/version.py")
option(HERCULES_GPU "build Hercules GPU backend" OFF)
option(BUILD_TEST "build Hercules test" ON)
option(INSTALL_EXAMPLE "install Hercules examples" ON)
option(INSTALL_TUTORIAL "install Hercules examples" ON)
list(APPEND CMAKE_PREFIX_PATH ${PROJECT_SOURCE_DIR}/carbin)
include_directories(${PROJECT_SOURCE_DIR}/carbin/include)
cmake_policy(SET CMP0028 NEW)
set(CMAKE_VERBOSE_MAKEFILE OFF)
#set_target_properties(xz PROPERTIES EXCLUDE_FROM_ALL ON)
#set_target_properties(xzdec PROPERTIES EXCLUDE_FROM_ALL ON)
link_directories(${CONDA_PREFIX}/lib)
include_directories(${CONDA_PREFIX}/include)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
if (CMAKE_CXX_COMPILER_ID MATCHES "Clang")
set(CMAKE_CXX_FLAGS
"${CMAKE_CXX_FLAGS} -pedantic -fvisibility-inlines-hidden -Wno-return-type-c-linkage -Wno-gnu-zero-variadic-macro-arguments -Wno-deprecated-declarations"
)
else ()
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-return-type")
endif ()
set(CMAKE_CXX_FLAGS_DEBUG "-g")
if (CMAKE_CXX_COMPILER_ID MATCHES "Clang")
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -fno-limit-debug-info")
endif ()
set(CMAKE_CXX_FLAGS_RELEASE "-O3")
include_directories(.)
set(APPLE_ARM OFF)
if (APPLE AND CMAKE_HOST_SYSTEM_PROCESSOR STREQUAL "arm64")
set(APPLE_ARM ON)
endif ()
list(APPEND CMAKE_PREFIX_PATH "$ENV{CONDA_PREFIX}")
set(THREADS_PREFER_PTHREAD_FLAG ON)
find_package(Threads REQUIRED)
find_package(LLVM 17 REQUIRED)
find_package(Clang 17 REQUIRED)
get_property(CLANG_LOCATION TARGET clang PROPERTY LOCATION)
message(STATUS "Using clang ${CLANG_LOCATION}")
include_directories(${CLANG_INCLUDE_DIRS})
if (CMAKE_VERSION VERSION_GREATER_EQUAL "3.24.0")
cmake_policy(SET CMP0135 NEW)
endif ()
message(STATUS "Found LLVM ${LLVM_PACKAGE_VERSION}")
message(STATUS "Using LLVMConfig.cmake in: ${LLVM_DIR}")
include(${CMAKE_SOURCE_DIR}/cmake/deps.cmake)
set(CMAKE_BUILD_WITH_INSTALL_RPATH ON)
if (APPLE)
set(CMAKE_INSTALL_RPATH "@loader_path;@loader_path/../lib/hercules")
set(STATIC_LIBCPP "")
else ()
set(CMAKE_INSTALL_RPATH "$ORIGIN:$ORIGIN/../lib/hercules")
set(STATIC_LIBCPP "-static-libstdc++")
endif ()
add_executable(peg2cc hercules/app/peg2cc.cc)
add_dependencies(peg2cc collie::collie)
target_link_libraries(peg2cc PRIVATE ${collie_INCLUDE_DIR})
target_link_libraries(peg2cc PRIVATE Threads::Threads collie::collie)
add_custom_command(
OUTPUT hercules_rules.cc
COMMAND peg2cc generate -g ${CMAKE_SOURCE_DIR}/hercules/parser/peg/grammar.peg
-o hercules_rules.cc -n hercules
DEPENDS peg2cc hercules/parser/peg/grammar.peg)
add_custom_command(
OUTPUT omp_rules.cc
COMMAND peg2cc generate -g ${CMAKE_SOURCE_DIR}/hercules/parser/peg/openmp.peg -o omp_rules.cc
-n omp
DEPENDS peg2cc hercules/parser/peg/openmp.peg)
# Hercules Jupyter library
set(HERCULES_JUPYTER_FILES hercules/util/jupyter.h hercules/util/jupyter.cc)
add_library(hercules_jupyter SHARED ${HERCULES_JUPYTER_FILES})
# Hercules runtime library
set(HERCULESRT_FILES
hercules/runtime/lib.h
hercules/runtime/hs_str.h
hercules/runtime/hs_str.cc
hercules/runtime/lib.cc
hercules/runtime/re.cc
hercules/runtime/exc.cc
hercules/runtime/gpu.cc
hercules/runtime/libinfo.cc
)
add_library(herculesrt SHARED ${HERCULESRT_FILES})
add_dependencies(herculesrt BDWgc::gc backtrace bz2 liblzma::liblzma re2::re2 turbo::turbo_STATIC collie::collie)
if (APPLE AND APPLE_ARM)
add_dependencies(herculesrt unwind_shared)
endif ()
target_include_directories(herculesrt PRIVATE ${backtrace_SOURCE_DIR}
${re2_INCLUDE_DIR}
${turbo_SOURCE_DIR}
${collie_INCLUDE_DIR}
"${gc_SOURCE_DIR}/include" runtime)
target_link_libraries(herculesrt PRIVATE ${OMP} backtrace ${STATIC_LIBCPP}
LLVMSupport)
if (APPLE)
target_link_libraries(
herculesrt
PRIVATE -Wl,-force_load,zlibstatic
-Wl,-force_load,$<TARGET_FILE:BDWgc::gc>
-Wl,-force_load,$<TARGET_FILE:turbo::turbo_STATIC>
-Wl,-force_load,$<TARGET_FILE:bz2>
-Wl,-force_load,$<TARGET_FILE:liblzma::liblzma>
-Wl,-force_load,$<TARGET_FILE:re2::re2>)
else ()
target_link_libraries(
herculesrt
PRIVATE -Wl,--whole-archive zlibstatic $<TARGET_FILE:BDWgc::gc>
$<TARGET_FILE:bz2> $<TARGET_FILE:liblzma::liblzma> $<TARGET_FILE:re2::re2> $<TARGET_FILE:turbo::turbo_STATIC>
-Wl,--no-whole-archive)
endif ()
if (ASAN)
target_compile_options(
herculesrt PRIVATE "-fno-omit-frame-pointer" "-fsanitize=address"
"-fsanitize-recover=address")
target_link_libraries(
herculesrt PRIVATE "-fno-omit-frame-pointer" "-fsanitize=address"
"-fsanitize-recover=address")
endif ()
if (HERCULES_GPU)
add_compile_definitions(HERCULES_GPU)
find_package(CUDAToolkit REQUIRED)
target_link_libraries(herculesrt PRIVATE CUDA::cudart_static CUDA::cuda_driver)
endif ()
#copy openmp
add_custom_command(
TARGET herculesrt
POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy ${OMP} ${CMAKE_BINARY_DIR})
if (APPLE AND APPLE_ARM)
add_custom_command(
TARGET herculesrt
POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_if_different $<TARGET_FILE:unwind_shared>
${CMAKE_BINARY_DIR})
endif ()
# Hercules compiler library
include_directories(${LLVM_INCLUDE_DIRS})
add_definitions(${LLVM_DEFINITIONS})
set(HERCULES_HPPFILES
hercules/compiler/compiler.h
hercules/compiler/debug_listener.h
hercules/compiler/engine.h
hercules/compiler/error.h
hercules/compiler/jit.h
hercules/compiler/memory_manager.h
hercules/dsl/dsl.h
hercules/dsl/plugins.h
hercules/parser/ast.h
hercules/parser/ast/expr.h
hercules/parser/ast/stmt.h
hercules/parser/ast/types.h
hercules/parser/ast/types/type.h
hercules/parser/ast/types/link.h
hercules/parser/ast/types/class.h
hercules/parser/ast/types/function.h
hercules/parser/ast/types/union.h
hercules/parser/ast/types/static.h
hercules/parser/ast/types/traits.h
hercules/parser/cache.h
hercules/parser/common.h
hercules/parser/ctx.h
hercules/parser/peg/peg.h
hercules/parser/peg/rules.h
hercules/parser/visitors/doc/doc.h
hercules/parser/visitors/format/format.h
hercules/parser/visitors/simplify/simplify.h
hercules/parser/visitors/simplify/ctx.h
hercules/parser/visitors/translate/translate.h
hercules/parser/visitors/translate/translate_ctx.h
hercules/parser/visitors/typecheck/typecheck.h
hercules/parser/visitors/typecheck/ctx.h
hercules/parser/visitors/visitor.h
hercules/hir/analyze/analysis.h
hercules/hir/analyze/dataflow/capture.h
hercules/hir/analyze/dataflow/cfg.h
hercules/hir/analyze/dataflow/dominator.h
hercules/hir/analyze/dataflow/reaching.h
hercules/hir/analyze/module/global_vars.h
hercules/hir/analyze/module/side_effect.h
hercules/hir/attribute.h
hercules/hir/base.h
hercules/hir/const.h
hercules/hir/dsl/codegen.h
hercules/hir/dsl/nodes.h
hercules/hir/flow.h
hercules/hir/func.h
hercules/hir/instr.h
hercules/hir/llvm/gpu.h
hercules/hir/llvm/llvisitor.h
hercules/hir/llvm/llvm.h
hercules/hir/llvm/optimize.h
hercules/hir/module.h
hercules/hir/pyextension.h
hercules/hir/ir.h
hercules/hir/transform/cleanup/canonical.h
hercules/hir/transform/cleanup/dead_code.h
hercules/hir/transform/cleanup/global_demote.h
hercules/hir/transform/cleanup/replacer.h
hercules/hir/transform/folding/const_fold.h
hercules/hir/transform/folding/const_prop.h
hercules/hir/transform/folding/folding.h
hercules/hir/transform/folding/rule.h
hercules/hir/transform/lowering/imperative.h
hercules/hir/transform/lowering/pipeline.h
hercules/hir/transform/manager.h
hercules/hir/transform/parallel/openmp.h
hercules/hir/transform/parallel/schedule.h
hercules/hir/transform/pass.h
hercules/hir/transform/pythonic/dict.h
hercules/hir/transform/pythonic/generator.h
hercules/hir/transform/pythonic/io.h
hercules/hir/transform/pythonic/list.h
hercules/hir/transform/pythonic/str.h
hercules/hir/transform/rewrite.h
hercules/hir/types/types.h
hercules/hir/util/cloning.h
hercules/hir/util/context.h
hercules/hir/util/format.h
hercules/hir/util/inlining.h
hercules/hir/util/irtools.h
hercules/hir/util/iterators.h
hercules/hir/util/matching.h
hercules/hir/util/operator.h
hercules/hir/util/outlining.h
hercules/hir/util/packs.h
hercules/hir/util/side_effect.h
hercules/hir/util/visitor.h
hercules/hir/value.h
hercules/hir/var.h
hercules/util/common.h
hercules/compiler/jit_extern.h)
set(HERCULES_CPPFILES
hercules/compiler/compiler.cc
hercules/compiler/debug_listener.cc
hercules/compiler/engine.cc
hercules/compiler/error.cc
hercules/compiler/jit.cc
hercules/compiler/memory_manager.cc
hercules/dsl/plugins.cc
hercules/engine/vm.cc
hercules/parser/ast/expr.cc
hercules/parser/ast/stmt.cc
hercules/parser/ast/types/type.cc
hercules/parser/ast/types/link.cc
hercules/parser/ast/types/class.cc
hercules/parser/ast/types/function.cc
hercules/parser/ast/types/union.cc
hercules/parser/ast/types/static.cc
hercules/parser/ast/types/traits.cc
hercules/parser/cache.cc
hercules/parser/common.cc
hercules/parser/peg/peg.cc
hercules/parser/visitors/doc/doc.cc
hercules/parser/visitors/format/format.cc
hercules/parser/visitors/simplify/simplify.cc
hercules/parser/visitors/simplify/ctx.cc
hercules/parser/visitors/simplify/assign.cc
hercules/parser/visitors/simplify/basic.cc
hercules/parser/visitors/simplify/call.cc
hercules/parser/visitors/simplify/class.cc
hercules/parser/visitors/simplify/collections.cc
hercules/parser/visitors/simplify/cond.cc
hercules/parser/visitors/simplify/function.cc
hercules/parser/visitors/simplify/access.cc
hercules/parser/visitors/simplify/import.cc
hercules/parser/visitors/simplify/loops.cc
hercules/parser/visitors/simplify/op.cc
hercules/parser/visitors/simplify/error.cc
hercules/parser/visitors/translate/translate.cc
hercules/parser/visitors/translate/translate_ctx.cc
hercules/parser/visitors/typecheck/typecheck.cc
hercules/parser/visitors/typecheck/infer.cc
hercules/parser/visitors/typecheck/ctx.cc
hercules/parser/visitors/typecheck/assign.cc
hercules/parser/visitors/typecheck/basic.cc
hercules/parser/visitors/typecheck/call.cc
hercules/parser/visitors/typecheck/class.cc
hercules/parser/visitors/typecheck/collections.cc
hercules/parser/visitors/typecheck/cond.cc
hercules/parser/visitors/typecheck/function.cc
hercules/parser/visitors/typecheck/access.cc
hercules/parser/visitors/typecheck/loops.cc
hercules/parser/visitors/typecheck/op.cc
hercules/parser/visitors/typecheck/error.cc
hercules/parser/visitors/visitor.cc
hercules/hir/attribute.cc
hercules/hir/analyze/analysis.cc
hercules/hir/analyze/dataflow/capture.cc
hercules/hir/analyze/dataflow/cfg.cc
hercules/hir/analyze/dataflow/dominator.cc
hercules/hir/analyze/dataflow/reaching.cc
hercules/hir/analyze/module/global_vars.cc
hercules/hir/analyze/module/side_effect.cc
hercules/hir/base.cc
hercules/hir/const.cc
hercules/hir/dsl/nodes.cc
hercules/hir/flow.cc
hercules/hir/func.cc
hercules/hir/instr.cc
hercules/hir/llvm/gpu.cc
hercules/hir/llvm/llvisitor.cc
hercules/hir/llvm/optimize.cc
hercules/hir/module.cc
hercules/hir/transform/cleanup/canonical.cc
hercules/hir/transform/cleanup/dead_code.cc
hercules/hir/transform/cleanup/global_demote.cc
hercules/hir/transform/cleanup/replacer.cc
hercules/hir/transform/folding/const_fold.cc
hercules/hir/transform/folding/const_prop.cc
hercules/hir/transform/folding/folding.cc
hercules/hir/transform/lowering/imperative.cc
hercules/hir/transform/lowering/pipeline.cc
hercules/hir/transform/manager.cc
hercules/hir/transform/parallel/openmp.cc
hercules/hir/transform/parallel/schedule.cc
hercules/hir/transform/pass.cc
hercules/hir/transform/pythonic/dict.cc
hercules/hir/transform/pythonic/generator.cc
hercules/hir/transform/pythonic/io.cc
hercules/hir/transform/pythonic/list.cc
hercules/hir/transform/pythonic/str.cc
hercules/hir/types/types.cc
hercules/hir/util/cloning.cc
hercules/hir/util/format.cc
hercules/hir/util/inlining.cc
hercules/hir/util/irtools.cc
hercules/hir/util/matching.cc
hercules/hir/util/outlining.cc
hercules/hir/util/side_effect.cc
hercules/hir/util/visitor.cc
hercules/hir/value.cc
hercules/hir/var.cc
hercules/util/common.cc
hercules/builtin/builtin.cc
hercules/builtin/debug/trace.cc
)
add_library(herculesc SHARED ${HERCULES_HPPFILES})
target_include_directories(herculesc PRIVATE ${collie_INCLUDE_DIR})
target_sources(herculesc PRIVATE ${HERCULES_CPPFILES} hercules_rules.cc omp_rules.cc)
if (ASAN)
target_compile_options(
herculesc PRIVATE "-fno-omit-frame-pointer" "-fsanitize=address"
"-fsanitize-recover=address")
target_link_libraries(
herculesc PRIVATE "-fno-omit-frame-pointer" "-fsanitize=address"
"-fsanitize-recover=address")
endif ()
if (CMAKE_BUILD_TYPE MATCHES Debug)
set_source_files_properties(hercules_rules.cc hercules/parser/peg/peg.cc
PROPERTIES COMPILE_FLAGS "-O2")
endif ()
llvm_map_components_to_libnames(
LLVM_LIBS
AllTargetsAsmParsers
AllTargetsCodeGens
AllTargetsDescs
AllTargetsInfos
AggressiveInstCombine
Analysis
AsmParser
BitWriter
CodeGen
Core
Extensions
IPO
IRReader
InstCombine
Instrumentation
MC
MCJIT
ObjCARCOpts
OrcJIT
Remarks
ScalarOpts
Support
Symbolize
Target
TransformUtils
Vectorize
Passes)
if (APPLE)
target_link_libraries(herculesc PRIVATE ${LLVM_LIBS} dl herculesrt)
else ()
target_link_libraries(herculesc PRIVATE ${STATIC_LIBCPP} ${LLVM_LIBS} dl herculesrt)
endif ()
# Gather headers
add_custom_target(
headers ALL
COMMENT "Collecting headers"
BYPRODUCTS "${CMAKE_BINARY_DIR}/include"
VERBATIM
COMMAND ${CMAKE_COMMAND} -E make_directory "${CMAKE_BINARY_DIR}/include/hercules"
COMMAND ${CMAKE_COMMAND} -E copy_directory "${CMAKE_SOURCE_DIR}/hercules"
"${CMAKE_BINARY_DIR}/include/hercules"
COMMAND find "${CMAKE_BINARY_DIR}/include" -type f ! -name "*.h" -exec rm {}
\\;)
add_dependencies(headers herculesrt herculesc)
# Prepare lib directory for plugin compilation
add_custom_target(
libs ALL
COMMENT "Collecting libraries"
BYPRODUCTS "${CMAKE_BINARY_DIR}/lib"
VERBATIM
COMMAND ${CMAKE_COMMAND} -E make_directory "${CMAKE_BINARY_DIR}/lib/hercules"
COMMAND
${CMAKE_COMMAND} -E copy
"${CMAKE_BINARY_DIR}/libherculesc${CMAKE_SHARED_LIBRARY_SUFFIX}"
"${CMAKE_BINARY_DIR}/lib/hercules"
COMMAND
${CMAKE_COMMAND} -E copy
"${CMAKE_BINARY_DIR}/libherculesrt${CMAKE_SHARED_LIBRARY_SUFFIX}"
"${CMAKE_BINARY_DIR}/lib/hercules"
COMMAND
${CMAKE_COMMAND} -E copy
"${CMAKE_BINARY_DIR}/libomp${CMAKE_SHARED_LIBRARY_SUFFIX}"
"${CMAKE_BINARY_DIR}/lib/hercules")
add_dependencies(libs herculesrt herculesc)
if (APPLE AND APPLE_ARM)
# add_custom_target(
# libs_apple_arm ALL
# COMMENT "Collecting Apple-specific libraries"
# BYPRODUCTS "${CMAKE_BINARY_DIR}/lib"
# VERBATIM
# COMMAND ${CMAKE_COMMAND} -E make_directory "${CMAKE_BINARY_DIR}/lib/hercules"
# COMMAND
# ${CMAKE_COMMAND} -E copy
# "${CMAKE_BINARY_DIR}/libunwind${CMAKE_SHARED_LIBRARY_SUFFIX}"
# "${CMAKE_BINARY_DIR}/lib/hercules")
# add_dependencies(libs_apple_arm herculesrt)
endif ()
# Hercules command-line tool
set(HBIN_SRC
hercules/app/hercules.cc
hercules/app/jupyter/jupyter.cc
hercules/app/verison.cc
)
add_executable(hercules ${HBIN_SRC})
target_link_libraries(hercules PUBLIC ${STATIC_LIBCPP} herculesc hercules_jupyter Threads::Threads)
if (BUILD_TEST)
enable_testing()
add_subdirectory(test)
endif()
if(INSTALL_EXAMPLE)
install(DIRECTORY ${PROJECT_SOURCE_DIR}/examples DESTINATION share/hercules)
endif()
if(INSTALL_TUTORIAL)
install(DIRECTORY ${PROJECT_SOURCE_DIR}/tutorial DESTINATION share/hercules)
endif ()
install(TARGETS herculesrt herculesc hercules_jupyter DESTINATION lib/hercules)
install(FILES ${CMAKE_BINARY_DIR}/libomp${CMAKE_SHARED_LIBRARY_SUFFIX} DESTINATION lib/hercules)
install(TARGETS hercules DESTINATION bin)
install(DIRECTORY ${PROJECT_SOURCE_DIR}/hercules
DESTINATION include
FILES_MATCHING
PATTERN "*.h"
PATTERN "*.hpp")
install(DIRECTORY ${CMAKE_SOURCE_DIR}/stdlib DESTINATION lib/hercules)
install(DIRECTORY ${CMAKE_SOURCE_DIR}/jit/ DESTINATION python)
install(DIRECTORY DESTINATION lib/hercules/plugins)