forked from tristanseifert/libcommunism
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCMakeLists.txt
175 lines (151 loc) · 6.63 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
###############################################################################
# Primary CMakeLists for libcommunism
###############################################################################
cmake_minimum_required(VERSION 3.18 FATAL_ERROR)
project(libcommunism VERSION 0.1 LANGUAGES C CXX)
### Define the build options
option(BUILD_LIBCOMMUNISM_TESTS "Build libcommunism test cases" OFF)
### Include some modules
set(CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake;${CMAKE_MODULE_PATH}")
include(ExternalProject)
include(FetchContent)
include(CheckSymbolExists)
include(TargetArch)
### Force C and C++ standards, warnings
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang" OR "${CMAKE_CXX_COMPILER_ID}" STREQUAL "AppleClang")
add_compile_options(-Wall -Wextra -Wmissing-declarations -Wformat=2 -Wcast-qual -Wundef
-fdiagnostics-color=always -Wwrite-strings -Wimplicit-fallthrough
-Wno-unused-private-field)
add_compile_options(-Werror)
elseif ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
add_compile_options(-Wall -Wextra -Wmissing-declarations -Wformat=2 -Wcast-qual -Wundef
-fdiagnostics-color=always -Wwrite-strings -Wimplicit-fallthrough -Wno-invalid-offsetof)
add_compile_options(-Werror)
elseif ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC")
# default warnings, treat warnings as errors
# this breaks the MASM :(
#add_compile_options(/W4 /WX)
else()
message(FATAL_ERROR "We do not know what C/C++ warning flags to use for compiler '${CMAKE_CXX_COMPILER_ID}'!")
endif()
### Determine the architecture we're compiling for
target_architecture(TARGET_ARCH)
message(STATUS "Target architecture: ${TARGET_ARCH}")
### Use the manually specified arch code override, or autodetect it
set(PLATFORM_LIBCOMMUNISM "Auto" CACHE STRING "Architecture specific code to be built")
set_property(CACHE PLATFORM_LIBCOMMUNISM PROPERTY STRINGS Auto amd64-sysv amd64-win aarch64-aapcs
setjmp ucontext x86-fastcall)
if("Auto" STREQUAL ${PLATFORM_LIBCOMMUNISM})
if("x86_64" STREQUAL ${TARGET_ARCH})
if(WIN32)
set(PLATFORM_SOURCES_TYPE "amd64-win")
else()
set(PLATFORM_SOURCES_TYPE "amd64-sysv")
endif()
elseif("aarch64" STREQUAL ${TARGET_ARCH})
# TODO: are there aarch64 platforms that don't use AAPCS?
set(PLATFORM_SOURCES_TYPE "aarch64-aapcs")
elseif("i386" STREQUAL ${TARGET_ARCH})
set(PLATFORM_SOURCES_TYPE "x86-fastcall")
endif()
else()
set(PLATFORM_SOURCES_TYPE ${PLATFORM_LIBCOMMUNISM})
endif()
### if still unset, select the best generic approach (first sigsetjmp, then ucontext)
if(NOT DEFINED PLATFORM_SOURCES_TYPE)
message(WARNING "Failed to select an optimized platform; attempting to pick a generic one")
# check for the methods required by the setjmp approach
check_symbol_exists(sigsetjmp "setjmp.h" HAVE_SIGSETJMP)
check_symbol_exists(siglongjmp "setjmp.h" HAVE_SIGLONGJMP)
check_symbol_exists(sigaltstack "signal.h" HAVE_SIGALTSTACK)
check_symbol_exists(sigaction "signal.h" HAVE_SIGACTION)
# check for the methods required by the ucontext approach
set(CMAKE_REQUIRED_DEFINITIONS "-D_XOPEN_SOURCE")
check_symbol_exists(getcontext "ucontext.h" HAVE_GETCONTEXT)
check_symbol_exists(swapcontext "ucontext.h" HAVE_SWAPCONTEXT)
# are all the methods for `setjmp` available?
if(DEFINED HAVE_SIGSETJMP AND DEFINED HAVE_SIGLONGJMP AND DEFINED HAVE_SIGALTSTACK
AND DEFINED HAVE_SIGACTION)
set(PLATFORM_SOURCES_TYPE "setjmp")
# are all the methods for `ucontext` available?
elseif(DEFINED HAVE_GETCONTEXT AND DEFINED HAVE_SWAPCONTEXT)
set(PLATFORM_SOURCES_TYPE "ucontext")
# no suitable alternate methods
else()
message(FATAL_ERROR "Failed to autodetect a suitable generic platform implementation, "
"please file an issue")
endif()
endif()
message(STATUS "Platform support variant: ${PLATFORM_SOURCES_TYPE}")
### Build the core library
add_library(libcommunism
src/Cothread.cpp
)
set_target_properties(libcommunism PROPERTIES OUTPUT_NAME communism)
set_target_properties(libcommunism PROPERTIES POSITION_INDEPENDENT_CODE ON)
target_include_directories(libcommunism PRIVATE ${CMAKE_CURRENT_LIST_DIR}/src)
target_include_directories(libcommunism PUBLIC ${CMAKE_CURRENT_LIST_DIR}/include)
### Add target specific sources
if("amd64-win" STREQUAL ${PLATFORM_SOURCES_TYPE})
enable_language(ASM_MASM)
target_sources(libcommunism PRIVATE
src/arch/amd64/Common.cpp
src/arch/Amd64/Windows.cpp
src/arch/Amd64/Windows.asm
)
target_compile_definitions(libcommunism PRIVATE -DPLATFORM_AMD64_WINDOWS)
elseif("amd64-sysv" STREQUAL ${PLATFORM_SOURCES_TYPE})
enable_language(ASM)
target_sources(libcommunism PRIVATE
src/arch/amd64/Common.cpp
src/arch/amd64/SysV.cpp
src/arch/amd64/SysV.S
)
target_compile_definitions(libcommunism PRIVATE -DPLATFORM_AMD64_SYSV)
elseif("aarch64-aapcs" STREQUAL ${PLATFORM_SOURCES_TYPE})
enable_language(ASM)
target_sources(libcommunism PRIVATE
src/arch/aarch64/Common.cpp
src/arch/aarch64/AAPCS.cpp
src/arch/aarch64/AAPCS.S
)
target_compile_definitions(libcommunism PRIVATE -DPLATFORM_AARCH64_AAPCS)
elseif("x86-fastcall" STREQUAL ${PLATFORM_SOURCES_TYPE})
# compiled sources are always the same as fastcall calling convention is identical
target_sources(libcommunism PRIVATE
src/arch/x86/Common.cpp
)
# for MSVC toolchain, use the MASM formatted assembler
if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC")
enable_language(ASM_MASM)
target_sources(libcommunism PRIVATE
src/arch/x86/Fastcall.asm
)
# otherwise, use the GNU AS style
else()
enable_language(ASM)
target_sources(libcommunism PRIVATE
src/arch/x86/Fastcall.S
)
endif()
target_compile_definitions(libcommunism PRIVATE -DPLATFORM_X86_FASTCALL)
elseif("setjmp" STREQUAL ${PLATFORM_SOURCES_TYPE})
target_sources(libcommunism PRIVATE
src/arch/setjmp/SetJmp.cpp
)
target_compile_definitions(libcommunism PRIVATE -DPLATFORM_SETJMP)
elseif("ucontext" STREQUAL ${PLATFORM_SOURCES_TYPE})
target_sources(libcommunism PRIVATE
src/arch/ucontext/UContext.cpp
)
target_compile_definitions(libcommunism PRIVATE -DPLATFORM_UCONTEXT)
else()
message(SEND_ERROR "don't know what arch specific sources are needed for '${PLATFORM_SOURCES_TYPE}'!")
endif()
### TODO: define install step
### If tests are desired, include the tests directory
if(BUILD_LIBCOMMUNISM_TESTS)
add_subdirectory(test)
endif()