-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCMakeLists.txt
172 lines (145 loc) · 7.5 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
################################################## Project ##################################################
cmake_minimum_required(VERSION 3.10 FATAL_ERROR)
project (Manta VERSION 1.0 LANGUAGES CXX)
list (APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake)
set_property (GLOBAL PROPERTY USE_FOLDERS ON)
set (CMAKE_CXX_STANDARD 20)
set (CMAKE_CXX_STANDARD_REQUIRED ON)
set (CMAKE_CXX_EXTENSIONS OFF)
set (CMAKE_CXX_VISIBILITY_PRESET hidden)
set (CMAKE_VISIBILITY_INLINES_HIDDEN 1)
include (set_max_warning_level)
set_max_warning_level ()
include (FetchContent)
################################################## Options ##################################################
option(BUILD_SHARED_LIBS "Build shared (dynamic) libraries." OFF)
option(BUILD_TESTS "Build tests." ON)
option(BUILD_APPLICATIONS "Build applications." ON)
################################################## Sources ##################################################
file(GLOB_RECURSE PROJECT_HEADERS include/manta/**.h include/manta/**.hpp)
file(GLOB_RECURSE PROJECT_SOURCES source/manta/**.c source/manta/**.cpp)
file(GLOB_RECURSE PROJECT_CMAKE_UTILS cmake/*.cmake)
file(GLOB_RECURSE PROJECT_MISC *.md *.txt)
set (PROJECT_EXPORT_HPP include/${PROJECT_NAME}/export.h)
set (PROJECT_FILES
${PROJECT_HEADERS}
${PROJECT_SOURCES}
${PROJECT_CMAKE_UTILS}
${PROJECT_MISC}
${PROJECT_EXPORT_HPP})
include (assign_source_group)
assign_source_group(${PROJECT_FILES})
################################################## Dependencies ##################################################
include(conan)
include(conan_utility)
include(import_library)
add_conan_remote(bincrafter https://api.bintray.com/conan/bincrafters/public-conan)
add_conan_remote(conan-community https://api.bintray.com/conan/conan-community/conan )
# ADD NON-CONAN LIBRARIES HERE.
# Examples:
# - Header Only:
# find_package (GLM REQUIRED)
# import_library(GLM_INCLUDE_DIRS)
# - Identical Debug and Release:
# find_package (OpenGL REQUIRED)
# import_library(OPENGL_INCLUDE_DIR OPENGL_LIBRARIES)
# - Separate Debug and Release:
# find_package (Boost REQUIRED iostreams)
# import_library(Boost_INCLUDE_DIRS Boost_IOSTREAMS_LIBRARY_DEBUG Boost_IOSTREAMS_LIBRARY_RELEASE)
# - You may also set the PROJECT_INCLUDE_DIRS and PROJECT_LIBRARIES instead of using import_library.
FetchContent_Declare(
Lightning
GIT_REPOSITORY https://github.com/nrupprecht/Lightning.git
GIT_TAG 9b2760c
)
FetchContent_MakeAvailable(Lightning)
include_directories(${Lightning_SOURCE_DIR}/include)
conan_cmake_run(CONANFILE conanfile.py BASIC_SETUP BUILD missing)
list (APPEND PROJECT_INCLUDE_DIRS ${CONAN_INCLUDE_DIRS_RELEASE})
list (APPEND PROJECT_COMPILE_DEFINITIONS ${CONAN_COMPILE_DEFINITIONS_RELEASE})
foreach (_LIB ${CONAN_LIBS_DEBUG})
message(STATUS " * Adding library ${_LIB} as debug")
list (APPEND PROJECT_LIBRARIES debug ${_LIB})
endforeach ()
foreach (_LIB ${CONAN_LIBS_RELEASE})
message(STATUS " * Adding library ${_LIB} as release")
list (APPEND PROJECT_LIBRARIES optimized ${_LIB})
endforeach ()
foreach (_LIB ${CONAN_LIBS})
message(STATUS " * Adding library ${_LIB}")
list (APPEND PROJECT_LIBRARIES ${_LIB})
endforeach ()
################################################## Targets ##################################################
# If I don't include this, the programs can't find their dynamically linked libraries.
# See: https://stackoverflow.com/questions/47697761/cmake-run-time-error-dyld-library-not-loaded-for-dynamically-linked-resource
if (${APPLE})
set(CMAKE_MACOSX_RPATH OFF)
endif()
add_library(${PROJECT_NAME} ${PROJECT_FILES})
#set(CMAKE_MACOSX_RPATH OFF)
set_property(TARGET ${PROJECT_NAME} PROPERTY POSITION_INDEPENDENT_CODE ON)
if (CMAKE_CXX_COMPILER_ID MATCHES "GNU")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fPIC")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIC")
endif()
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
target_include_directories(${PROJECT_NAME} PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}>
$<INSTALL_INTERFACE:include> PRIVATE source)
target_include_directories(${PROJECT_NAME} PUBLIC ${PROJECT_INCLUDE_DIRS})
target_link_libraries (${PROJECT_NAME} PUBLIC ${PROJECT_LIBRARIES})
target_link_libraries (${PROJECT_NAME} PUBLIC ${CONAN_LIBS})
target_compile_definitions(${PROJECT_NAME} PUBLIC ${PROJECT_COMPILE_DEFINITIONS})
set_target_properties (${PROJECT_NAME} PROPERTIES LINKER_LANGUAGE CXX)
if(NOT BUILD_SHARED_LIBS)
message(STATUS " * NOTE: Build shared libs is off")
string (TOUPPER ${PROJECT_NAME} PROJECT_NAME_UPPER)
set_target_properties(${PROJECT_NAME} PROPERTIES COMPILE_FLAGS -D${PROJECT_NAME_UPPER}_STATIC)
endif()
# Set all warnings as errors.
target_compile_options(${PROJECT_NAME} PRIVATE
$<$<CXX_COMPILER_ID:MSVC>:/W3 /WX>
$<$<NOT:$<CXX_COMPILER_ID:MSVC>>:-Werror>)
################################################## Postbuild ##################################################
include (GenerateExportHeader)
string (TOUPPER ${PROJECT_NAME} PROJECT_NAME_UPPER)
generate_export_header(${PROJECT_NAME}
EXPORT_FILE_NAME ${PROJECT_SOURCE_DIR}/include/${PROJECT_NAME}/export.h
EXPORT_MACRO_NAME ${PROJECT_NAME_UPPER}_EXPORT
STATIC_DEFINE ${PROJECT_NAME_UPPER}_STATIC)
################################################## Testing ##################################################
if(BUILD_TESTS)
message(STATUS "Building tests...")
enable_testing ()
set (TEST_MAIN_NAME test_main)
set (TEST_MAIN_SOURCES UnitTest/internal/main.cpp)
add_library (${TEST_MAIN_NAME} OBJECT ${TEST_MAIN_SOURCES})
target_link_libraries(${TEST_MAIN_NAME} ${PROJECT_NAME})
set_property (TARGET ${TEST_MAIN_NAME} PROPERTY FOLDER tests)
assign_source_group (${TEST_MAIN_SOURCES})
# Find tests even in nexted directories.
file(GLOB_RECURSE PROJECT_TEST_CPPS UnitTest/**UT_*.cpp)
foreach(_SOURCE ${PROJECT_TEST_CPPS})
get_filename_component(_NAME ${_SOURCE} NAME_WE)
add_executable (${_NAME} ${_SOURCE} $<TARGET_OBJECTS:${TEST_MAIN_NAME}>)
target_link_libraries (${_NAME} ${PROJECT_NAME})
# NOTE: I used to just have add_test(${_NAME} ${_NAME}) here, but then when I would try to run all CTests,
# it couldn't find the executables.
add_test (NAME ${_NAME} COMMAND ${_NAME})
set_property (TARGET ${_NAME} PROPERTY FOLDER tests)
assign_source_group (${_SOURCE})
endforeach()
endif()
if (${BUILD_APPLICATIONS})
message(STATUS "Building applications...")
add_subdirectory("${PROJECT_SOURCE_DIR}/applications" "applications")
endif()
################################################## Installation ##################################################
install(TARGETS ${PROJECT_NAME} EXPORT ${PROJECT_NAME}-config
ARCHIVE DESTINATION lib
LIBRARY DESTINATION lib
RUNTIME DESTINATION bin)
install(DIRECTORY include/ DESTINATION include)
install(EXPORT ${PROJECT_NAME}-config DESTINATION cmake)
export (TARGETS ${PROJECT_NAME} FILE ${PROJECT_NAME}-config.cmake)