-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCMakeLists.txt
More file actions
242 lines (201 loc) · 8.67 KB
/
Copy pathCMakeLists.txt
File metadata and controls
242 lines (201 loc) · 8.67 KB
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
# Need 3.9 to support CXX_STANDARD=17
cmake_minimum_required(VERSION 3.9.0)
# Read version.txt and setup project
file(READ "${CMAKE_CURRENT_SOURCE_DIR}/version.txt" VERSION_FILE)
string(REGEX MATCH "VERSION_MAJOR ([0-9]*)" _ ${VERSION_FILE})
set(INSTR_TRACE_MAJOR_VERSION ${CMAKE_MATCH_1})
string(REGEX MATCH "VERSION_MINOR ([0-9]*)" _ ${VERSION_FILE})
set(INSTR_TRACE_MINOR_VERSION ${CMAKE_MATCH_1})
string(REGEX MATCH "VERSION_PATCH ([0-9]*)" _ ${VERSION_FILE})
set(INSTR_TRACE_PATCH_VERSION ${CMAKE_MATCH_1})
cmake_policy(SET CMP0048 NEW)
project(
instr_trace
VERSION
"${INSTR_TRACE_MAJOR_VERSION}.${INSTR_TRACE_MINOR_VERSION}.${INSTR_TRACE_PATCH_VERSION}"
)
# ---------------------------------------------------------------------------
# Build options
# ---------------------------------------------------------------------------
if(NOT DEFINED INSTR_TRACE_BUILD_TEST_TARGETS)
option(INSTR_TRACE_BUILD_TEST_TARGETS "Build test targets." OFF)
endif()
option(ENABLE_WINDOWS_DEBUG "Enable debugging on Windows binaries" OFF)
# ---------------------------------------------------------------------------
# Boost
# ---------------------------------------------------------------------------
set(IMPORTED_TARGETS)
if(NOT DEFINED BOOST_ROOT AND ${CMAKE_CXX_COMPILER_ID} STREQUAL MSVC)
set(BOOST_ROOT "C:/Boost")
set(Boost_COMPILER "-vc142")
add_definitions(-DBOOST_ALL_NO_LIB)
set(BOOST_INCLUDEDIR "C:/Boost/include/boost-1_69")
set(BOOST_LIBRARYDIR "C:/Boost/lib")
set(Boost_USE_STATIC_LIBS ON)
set(Boost_USE_STATIC_RUNTIME ON)
set(Boost_ARCHITECTURE "-x32")
endif()
find_package(Boost 1.69 REQUIRED COMPONENTS program_options filesystem)
add_compile_options(-DBOOST_CONFIG_SUPPRESS_OUTDATED_MESSAGE)
add_compile_options(-DBOOST_SYSTEM_NO_DEPRECATED)
# Boost versions 1.70.0+ may use Boost's provided CMake support rather than
# CMake's internal Boost support. The former uses "Boost::boost" and so on,
# while the latter uses "Boost_BOOST" and so on. This normalizes the two cases
# to use Boost_INCLUDE_DIRS and Boost_LIBRARIES.
if(TARGET Boost::headers)
get_target_property(Boost_INCLUDE_DIRS Boost::headers
INTERFACE_INCLUDE_DIRECTORIES)
endif()
list(APPEND IMPORTED_TARGETS Boost::boost ${Boost_LIBRARIES})
# ---------------------------------------------------------------------------
# Global settings
# ---------------------------------------------------------------------------
# set_property(GLOBAL PROPERTY USE_FOLDERS ON)
# Use C++17
set(CMAKE_CXX_STANDARD 17)
# Error if it's not available
set(CMAKE_CXX_STANDARD_REQUIRED ON)
if(WIN32)
set(CMAKE_DEBUG_POSTFIX
"d"
CACHE STRING "add a postfix, usually d on windows")
endif()
set(CMAKE_RELEASE_POSTFIX
""
CACHE STRING "add a postfix, usually empty on windows")
set(CMAKE_RELWITHDEBINFO_POSTFIX
""
CACHE STRING "add a postfix, usually empty on windows")
set(CMAKE_MINSIZEREL_POSTFIX
""
CACHE STRING "add a postfix, usually empty on windows")
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
# ---------------------------------------------------------------------------
# Compile options
# ---------------------------------------------------------------------------
if(${CMAKE_CXX_COMPILER_ID} STREQUAL MSVC)
add_compile_options(-D_MBCS)
add_compile_options(-D_UNICODE)
add_compile_options(-DUNICODE)
add_compile_options(-D_WIN32)
# Enabled a sensible warning level and treat all warnings as errors.
add_compile_options(-W4)
add_compile_options(-WX)
# Hack - there are issues with doing a debug build (Boost and static debug CRT
# don't seem to get along). In order to support debugging instr_trace, use
# the release build and just add these flags. They will produce the
# appropriate unoptimized binaries and PDB files.
if(ENABLE_WINDOWS_DEBUG)
add_compile_options(-DDRRUN_DEBUG) # Enable debug options in drrun
add_compile_options(-Zi) # Produce PDB files
add_compile_options(-Od) # No optimizations - easier debugging
add_link_options(-DEBUG:FULL) # All debug info is in the PDB
endif()
add_compile_options(-EHsc) # Enable exceptions
add_compile_options(-sdl) # Enable extra security checks
add_compile_options(-permissive-) # Disable permissive mode
add_compile_options($<$<CONFIG:Release>:-GL>) # Enable whole program
# optimization
# Windows secure CRT functions (*_s) are not portable, allow the original
# functions to be used
add_compile_options(-D_CRT_SECURE_NO_WARNINGS)
# Disable min/max macros in Windows headers so they do not conflict with std
# library min/max functions
add_compile_options(-DNOMINMAX)
add_link_options($<$<CONFIG:Release>:-ltcg>) # Enable link-time code
# generation
# Statically link in run-time library - carry C/C++ runtime along, don't
# depend on the target to have the correct one installed
add_compile_options($<$<CONFIG:>:/MT> $<$<CONFIG:Debug>:/MTd>
$<$<CONFIG:Release>:/MT>)
elseif((${CMAKE_CXX_COMPILER_ID} STREQUAL GNU) OR (${CMAKE_CXX_COMPILER_ID}
STREQUAL Clang))
add_compile_options(-Wall -Wextra -Wpointer-arith -Wshadow -Werror -Wpedantic)
add_compile_options(-fPIC)
endif()
# ---------------------------------------------------------------------------
# Google Test Application
# ---------------------------------------------------------------------------
configure_file(CMakeLists.googletest googletest-download/CMakeLists.txt)
execute_process(
COMMAND "${CMAKE_COMMAND}" -G "${CMAKE_GENERATOR}" .
RESULT_VARIABLE result
WORKING_DIRECTORY "${CMAKE_BINARY_DIR}/googletest-download")
if(result)
message(ERROR "CMake step for googletest failed: ${result}")
endif()
execute_process(
COMMAND "${CMAKE_COMMAND}" --build .
RESULT_VARIABLE result
WORKING_DIRECTORY "${CMAKE_BINARY_DIR}/googletest-download")
if(result)
message(ERROR "Build step for googletest failed: ${result}")
endif()
# Prevent overriding the parent project's compiler/linker settings on Windows
set(gtest_force_shared_crt
ON
CACHE BOOL "" FORCE)
# Add googletest directly to our build. This defines the gtest and gtest_main
# targets.
add_subdirectory("${CMAKE_BINARY_DIR}/googletest-src"
"${CMAKE_BINARY_DIR}/googletest-build" EXCLUDE_FROM_ALL)
include_directories("${gtest_SOURCE_DIR}/include")
enable_testing()
# ---------------------------------------------------------------------------
# Source files
# ---------------------------------------------------------------------------
function(install_linux_debug_info TARGET COMPONENT_NAME)
if(UNIX
AND NOT CYGWIN
AND ("${CMAKE_BUILD_TYPE}" STREQUAL "RelWithDebInfo"
OR "${CMAKE_BUILD_TYPE}" STREQUAL "Debug"))
string(
RANDOM
LENGTH 32
ALPHABET "abcdef0123456789" BUILD_ID)
string(SUBSTRING "${BUILD_ID}" 0 2 BUILD_ID_PREFIX)
string(SUBSTRING "${BUILD_ID}" 2 32 BUILD_ID_SUFFIX)
if("${TARGET}" STREQUAL "instrumenter")
target_link_libraries(${TARGET} "-Wl,--build-id=0x${BUILD_ID}")
else()
target_link_libraries(${TARGET} PRIVATE "-Wl,--build-id=0x${BUILD_ID}")
endif()
add_custom_command(
TARGET ${TARGET}
POST_BUILD
COMMAND objcopy --only-keep-debug $<TARGET_FILE:${TARGET}>
${CMAKE_BINARY_DIR}/bin/${BUILD_ID_SUFFIX}.debug
COMMAND objcopy --strip-debug $<TARGET_FILE:${TARGET}>)
install(
FILES "${CMAKE_BINARY_DIR}/bin/${BUILD_ID_SUFFIX}.debug"
COMPONENT "${COMPONENT_NAME}"
DESTINATION "lib/debug/.build-id/${BUILD_ID_PREFIX}")
endif()
endfunction()
# instr_trace package source code
add_subdirectory(src)
# googletest unit tests
add_subdirectory(tst)
# ---------------------------------------------------------------------------
# Export config for use by other CMake projects
# ---------------------------------------------------------------------------
export(TARGETS instr_trace instrumenter fifo_packet utils
FILE "${CMAKE_CURRENT_BINARY_DIR}/instr_traceTargets.cmake")
# Build tree config
configure_file("${CMAKE_CURRENT_LIST_DIR}/instr_traceConfig.cmake.in"
"${CMAKE_CURRENT_BINARY_DIR}/instr_traceConfig.cmake" @ONLY)
# Install tree config
configure_file(
"${CMAKE_CURRENT_LIST_DIR}/instr_traceConfig.cmake.in"
"${CMAKE_CURRENT_BINARY_DIR}/export/instr_traceConfig.cmake" @ONLY)
install(
FILES ${CMAKE_CURRENT_BINARY_DIR}/export/instr_traceConfig.cmake
DESTINATION lib/instr_trace
COMPONENT cmake_config)
install(
EXPORT instr_traceTargets
DESTINATION lib/instr_trace
COMPONENT cmake_target)
export(PACKAGE instr_trace)