-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCMakeLists.txt
81 lines (71 loc) · 2.91 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
cmake_minimum_required(VERSION 3.13)
# project info
project(compiler
VERSION 2024
DESCRIPTION "ZheJiang University 2023~2024 Spring-Summer Compiler Project"
LANGUAGES C CXX)
# CTest
enable_testing()
# cmake configurations
set(CMAKE_CONFIGURATION_TYPES "Debug;Release" CACHE STRING "limited configs list: Debug, Release" FORCE)
# set cmake debug postfix
set(CMAKE_DEBUG_POSTFIX "d")
# settings
# set to OFF to enable C mode
set(CPP_MODE ON)
if(CPP_MODE)
set(FB_EXT ".cc")
else()
set(FB_EXT ".c")
endif()
message(STATUS "Flex/Bison generated source file extension: ${FB_EXT}")
# enable all warnings
if(MSVC)
add_compile_options(/W3)
add_compile_options("$<$<CONFIG:Debug>:/fsanitize=address;/Zi>;$<$<CONFIG:Release>:/O2>")
add_link_options("$<$<CONFIG:Debug>:/fsanitize=address;/DEBUG:FULL>")
else()
# disable warnings caused by old version of Flex
add_compile_options(-Wall -Wextra -Wno-register)
add_compile_options("$<$<CONFIG:Debug>:-O0;-g;-fsanitize=address;-fno-omit-frame-pointer;-fno-optimize-sibling-calls>;$<$<CONFIG:Release>:-O2;-DNDEBUG>")
add_link_options("$<$<CONFIG:Debug>:-fsanitize=address>")
endif()
# find Flex/Bison
find_package(FLEX REQUIRED)
find_package(BISON REQUIRED)
# generate lexer/parser
file(GLOB_RECURSE L_SOURCES "src/*.l")
file(GLOB_RECURSE Y_SOURCES "src/*.y")
if(NOT (L_SOURCES STREQUAL "" AND Y_SOURCES STREQUAL ""))
string(REGEX REPLACE ".*/(.*)\\.l" "${CMAKE_CURRENT_BINARY_DIR}/\\1.lex${FB_EXT}" L_OUTPUTS "${L_SOURCES}")
string(REGEX REPLACE ".*/(.*)\\.y" "${CMAKE_CURRENT_BINARY_DIR}/\\1.tab${FB_EXT}" Y_OUTPUTS "${Y_SOURCES}")
flex_target(Lexer ${L_SOURCES} ${L_OUTPUTS})
bison_target(Parser ${Y_SOURCES} ${Y_OUTPUTS})
add_flex_bison_dependency(Lexer Parser)
endif()
# add fmt
add_subdirectory(third_party/fmt EXCLUDE_FROM_ALL)
# project include directories
include_directories(include)
include_directories(third_party/fmt/include)
include_directories(accsys/include)
# all of C/C++ source files
file(GLOB_RECURSE C_SOURCES "src/*.c")
file(GLOB_RECURSE CXX_SOURCES "src/*.cpp")
file(GLOB_RECURSE CC_SOURCES "src/*.cc")
set(SOURCES ${C_SOURCES} ${CXX_SOURCES} ${CC_SOURCES}
${FLEX_Lexer_OUTPUTS} ${BISON_Parser_OUTPUT_SOURCE})
# executable
add_executable(compiler ${SOURCES})
set_target_properties(compiler PROPERTIES C_STANDARD 11 CXX_STANDARD 17)
target_link_libraries(compiler PRIVATE fmt::fmt-header-only)
# add accsys template library dependencies.
add_subdirectory(accsys)
target_link_libraries(compiler PRIVATE accsys::accsys)
target_include_directories(compiler INTERFACE accsys::accsys)
# some possible build options you may use.
# you can specify whatever you prefer.
# disable rtti and exception.
# target_compile_options(compiler PRIVATE -fno-rtti -fno-exceptions)
# enable address sanitizer if you encounter segmentation fault.
# target_compile_options(compiler PRIVATE -O0 -g -fsanitize=address -fno-omit-frame-pointer -fno-optimize-sibling-calls)