-
Notifications
You must be signed in to change notification settings - Fork 12
/
CMakeLists.txt
259 lines (213 loc) · 8.66 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
# -----------------------------------------------------------------------------
# Copyright (c) 2021, Microsoft Research, Daan Leijen
# -----------------------------------------------------------------------------
cmake_minimum_required(VERSION 3.12)
project(libmprompt C CXX ASM)
set(CMAKE_C_STANDARD 11)
set(CMAKE_CXX_STANDARD 14)
option(MP_USE_C "Build C versions of the library without exception support" OFF)
option(MP_DEBUG_UBSAN "Build with undefined behaviour sanitizer" OFF)
option(MP_DEBUG_ASAN "Build with address sanitizer" OFF)
set(mp_version "0.6")
# all sources are included in one file so we can generate independent libraries and stand-alone object files.
set(mprompt_sources src/mprompt/main.c)
# util.c gstack_pool.c gstack_win.c gstack_mmap.c gstack_mmap_mach.c gstack.c mprompt.c
set(mpeff_sources src/mpeff/main.c)
# src/mpeff/mpeff.c
set(test_mpe_main_sources
test/common_util.c
test/common_effects.c
test/src/reader.c
test/src/counter.c
test/src/countern.c
test/src/mstate.c
test/src/amb.c
test/src/amb_state.c
test/src/nqueens.c
test/src/rehandle.c
test/src/triples.c
test/test_mpe_main.c)
if (NOT MP_USE_C)
list(APPEND test_mpe_main_sources
test/src/exn.cpp
test/src/multi_unwind.cpp
test/src/throw.cpp)
endif()
set(test_mp_async_sources
test/test_mp_async.c
test/common_util.c)
set(test_mp_example_generator_sources
test/test_mp_example_generator.c)
set(test_mp_example_async_sources
test/test_mp_example_async.c)
list(APPEND test_sources
${test_mpe_main_sources}
${test_mp_async_sources}
${test_mp_example_generator_sources}
${test_mp_example_async_sources})
set(mp_cflags)
set(mp_install_dir)
set(mp_libs)
# -----------------------------------------------------------------------------
# C or C++ (default)
# -----------------------------------------------------------------------------
if (MP_USE_C)
# C
message(STATUS "Use the C compiler to compile (MP_USE_C=ON)")
set(mp_mprompt_name "mprompt")
set(mp_mpeff_name "mpeff")
if(CMAKE_C_COMPILER_ID MATCHES "MSVC|Intel")
message(WARNING "It is not recommended to use plain C with this compiler (due to SEH) (${CMAKE_C_COMPILER_ID})")
endif()
else()
# C++
message(STATUS "Use the C++ compiler to compile (${CMAKE_CXX_COMPILER_ID}) (MP_USE_C=OFF)")
set(mp_mprompt_name "mpromptx")
set(mp_mpeff_name "mpeffx")
SET_SOURCE_FILES_PROPERTIES(${mprompt_sources} PROPERTIES LANGUAGE CXX )
SET_SOURCE_FILES_PROPERTIES(${mpeff_sources} PROPERTIES LANGUAGE CXX )
SET_SOURCE_FILES_PROPERTIES(${test_sources} PROPERTIES LANGUAGE CXX )
endif()
# -----------------------------------------------------------------------------
# Check architecture: currently only amd64 and arm64 (experimental)
# -----------------------------------------------------------------------------
set(mprompt_asm_source)
string(TOLOWER "${CMAKE_SYSTEM_PROCESSOR}" mp_proc)
if (mp_proc MATCHES "(x86_64)|(amd64)")
if (CMAKE_C_COMPILER_ID MATCHES "MSVC")
set(mprompt_asm_source src/mprompt/asm/longjmp_amd64_win.asm)
enable_language(ASM_MASM)
else()
set(mprompt_asm_source src/mprompt/asm/longjmp_amd64.S)
endif()
else()
if (mp_proc MATCHES "(aarch64)|(arm64)")
if (APPLE)
message(WARNING "arm64 support on Apple is untested")
endif()
set(mprompt_asm_source src/mprompt/asm/longjmp_arm64.S)
else()
message(WARNING "unsupported architecture: ${mp_proc}")
endif()
endif()
# -----------------------------------------------------------------------------
# Convenience: set default build type depending on the build directory
# -----------------------------------------------------------------------------
if (NOT CMAKE_BUILD_TYPE)
if ("${CMAKE_BINARY_DIR}" MATCHES ".*(Debug|debug|ubsan|tsan|asan)$")
message(STATUS "No build type selected, default to: Debug")
set(CMAKE_BUILD_TYPE "Debug")
else()
message(STATUS "No build type selected, default to: Release")
set(CMAKE_BUILD_TYPE "Release")
endif()
endif()
# -----------------------------------------------------------------------------
# Sanitizers
# -----------------------------------------------------------------------------
if(MP_DEBUG_UBSAN OR MP_DEBUG_ASAN)
if(NOT MP_USE_C AND (CMAKE_BUILD_TYPE MATCHES "Debug") AND (CMAKE_CXX_COMPILER_ID MATCHES "Clang"))
set(mp_san)
if (MP_DEBUG_UBSAN)
list(APPEND mp_san "undefined")
endif()
if (MP_DEBUG_ASAN)
list(APPEND mp_san "address")
endif()
list(JOIN mp_san "," mp_san)
list(APPEND mp_cflags -fsanitize=${mp_san})
list(APPEND CMAKE_EXE_LINKER_FLAGS -fsanitize=${mp_san})
else()
message(WARNING "Can only use sanitizer with a clang++ debug build (currently: ${CMAKE_CXX_COMPILER_ID}, CMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}, MP_USE_C=${MP_USE_C})")
endif()
endif()
# -----------------------------------------------------------------------------
# Flags
# -----------------------------------------------------------------------------
if((CMAKE_BUILD_TYPE MATCHES "Release") OR ((CMAKE_BUILD_TYPE MATCHES "RelWithDebInfo") AND NOT APPLE))
set(CMAKE_INTERPROCEDURAL_OPTIMIZATION ON)
endif()
if (C_COMPILER_ID MATCHES "AppleClang|Clang|GNU|Intel")
list(APPEND mp_cflags -Wall -Wextra -Wno-unknown-pragmas -fvisibility=hidden)
endif()
if(APPLE)
# list(APPEND mp_cflags -fasynchronous-unwind-tables)
endif()
# treat C extension as C++
if (NOT MP_USE_C)
if(CMAKE_CXX_COMPILER_ID MATCHES "AppleClang|Clang")
list(APPEND mp_cflags -Wno-deprecated)
endif()
if(CMAKE_CXX_COMPILER_ID MATCHES "Intel")
list(APPEND mp_cflags -Kc++)
endif()
endif()
if (NOT WIN32)
list(APPEND mp_libs pthread)
find_library(mp_execinfo NAMES execinfo)
if (mp_execinfo) # on freeBSD
list(APPEND mp_libs execinfo)
endif()
endif()
# -----------------------------------------------------------------------------
# Overview
# -----------------------------------------------------------------------------
message(STATUS "")
message(STATUS "Libraries : lib${mp_mprompt_name}, lib${mp_mpeff_name}")
message(STATUS "Build type: ${CMAKE_BUILD_TYPE}")
if(MP_USE_C)
message(STATUS "Compiler : ${CMAKE_C_COMPILER}")
else()
message(STATUS "Compiler : ${CMAKE_CXX_COMPILER}")
endif()
message(STATUS " ${mp_cflags}")
message(STATUS "Link with : ${mp_libs}")
message(STATUS "")
# -----------------------------------------------------------------------------
# Libraries
# Each library is standalone; mphnd includes mprompt, while mpeff includes mphnd (and mprompt)
# -----------------------------------------------------------------------------
add_library(mprompt STATIC ${mprompt_sources} ${mprompt_asm_source})
# set_property(TARGET mprompt PROPERTY POSITION_INDEPENDENT_CODE ON)
set_target_properties(mprompt PROPERTIES VERSION ${mp_version} OUTPUT_NAME ${mp_mprompt_name} )
target_compile_definitions(mprompt PRIVATE MP_STATIC_LIB)
target_compile_options(mprompt PRIVATE ${mp_cflags})
target_include_directories(mprompt PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:${mp_install_dir}/include>
)
if (NOT WIN32)
target_link_libraries(mprompt PUBLIC ${mp_libs})
endif()
if (APPLE)
# target_link_options(mprompt PUBLIC -Wl,-no_compact_unwind -Wl,-keep_dwarf_unwind)
# target_link_options(mprompt PUBLIC -Wl,-no_keep_dwarf_unwind)
endif()
# mpeff library
add_library(mpeff STATIC ${mpeff_sources} ${mprompt_asm_source})
set_target_properties(mpeff PROPERTIES VERSION ${mp_version} OUTPUT_NAME ${mp_mpeff_name} )
target_compile_definitions(mpeff PRIVATE MPE_STATIC_LIB)
target_compile_options(mpeff PRIVATE ${mp_cflags})
target_include_directories(mpeff PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:${mp_install_dir}/include>
)
if (NOT WIN32)
target_link_libraries(mpeff PUBLIC ${mp_libs})
endif()
#---------------------------------------------------------------
# tests
#---------------------------------------------------------------
add_executable(test_mpe_main ${test_mpe_main_sources})
add_executable(test_mp_async ${test_mp_async_sources})
add_executable(test_mp_example_generator ${test_mp_example_generator_sources})
add_executable(test_mp_example_async ${test_mp_example_async_sources})
set(test_targets test_mpe_main test_mp_async test_mp_example_generator test_mp_example_async)
# finalize tests
enable_testing()
foreach(test_target ${test_targets} )
target_compile_options(${test_target} PRIVATE ${mp_cflags})
target_include_directories(${test_target} PRIVATE include test)
target_link_libraries(${test_target} PRIVATE mpeff)
add_test( ${test_target} ${test_target})
endforeach()