forked from GrammaTech/gtirb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CMakeLists.txt
362 lines (307 loc) · 12.2 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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
#
# Cmake Configuration
#
# Need 3.9 to support CXX_STANDARD=17 and protobuf
cmake_minimum_required(VERSION 3.9.0)
# The version.txt file is the official record of the version number. We use the
# contents of that file to set the project version for use in other CMake files.
file(READ "${CMAKE_CURRENT_SOURCE_DIR}/version.txt" ver)
string(REGEX MATCH "VERSION_MAJOR ([0-9]*)" _ ${ver})
set(GTIRB_MAJOR_VERSION ${CMAKE_MATCH_1})
string(REGEX MATCH "VERSION_MINOR ([0-9]*)" _ ${ver})
set(GTIRB_MINOR_VERSION ${CMAKE_MATCH_1})
string(REGEX MATCH "VERSION_PATCH ([0-9]*)" _ ${ver})
set(GTIRB_PATCH_VERSION ${CMAKE_MATCH_1})
string(REGEX MATCH "VERSION_PROTOBUF ([0-9]*)" _ ${ver})
set(GTIRB_PROTOBUF_VERSION ${CMAKE_MATCH_1})
cmake_policy(SET CMP0048 NEW)
project(
GTIRB
VERSION "${GTIRB_MAJOR_VERSION}.${GTIRB_MINOR_VERSION}.${GTIRB_PATCH_VERSION}"
)
include(CheckFunctionExists)
include(CheckCXXSourceCompiles)
include(CheckIncludeFile)
include(Macros.cmake)
include(AlignOf.cmake)
include(CMakePackageConfigHelpers)
# ---------------------------------------------------------------------------
# Build options
# ---------------------------------------------------------------------------
set(GTIRB_MSVC_PARALLEL_COMPILE_JOBS
"0"
CACHE
STRING
"Number of parallel compiler jobs used for Visual Studio compiles. 0 means use all processors. Default is 0."
)
option(GTIRB_ENABLE_TESTS "Enable building and running unit tests." ON)
# This just sets the builtin BUILD_SHARED_LIBS, but if defaults to ON instead of
# OFF.
option(GTIRB_BUILD_SHARED_LIBS "Build shared libraries." ON)
if(GTIRB_BUILD_SHARED_LIBS)
set(BUILD_SHARED_LIBS ON)
else()
set(BUILD_SHARED_LIBS OFF)
endif()
if(UNIX AND NOT BUILD_SHARED_LIBS)
# Find only static libraries
set(CMAKE_FIND_LIBRARY_SUFFIXES ".a")
add_compile_options(-static)
endif()
enable_testing()
# Whether or not to run clang-tidy (if present)
option(GTIRB_RUN_CLANG_TIDY "Enable running of clang-tidy." ON)
# These options control what APIs get built.
find_program(PYTHON "python3")
if(PYTHON)
set(GTIRB_PY_API_DEFAULT ON)
else()
set(GTIRB_PY_API_DEFAULT OFF)
endif()
option(GTIRB_CXX_API "Whether or not the C++ API is built." ON)
option(GTIRB_PY_API "Whether or not the Python API is built."
${GTIRB_PY_API_DEFAULT}
)
# ---------------------------------------------------------------------------
# Global settings
# ---------------------------------------------------------------------------
set_property(GLOBAL PROPERTY USE_FOLDERS ON)
set(CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/src)
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)
if(GTIRB_CXX_API)
configure_file(
"${CMAKE_CURRENT_SOURCE_DIR}/include/gtirb/version.h.in"
"${CMAKE_CURRENT_BINARY_DIR}/include/gtirb/version.h" @ONLY
)
install(FILES "${CMAKE_CURRENT_BINARY_DIR}/include/gtirb/version.h"
DESTINATION include/gtirb
)
# Use C++17
set(CMAKE_CXX_STANDARD 17)
# Error if it's not available
set(CMAKE_CXX_STANDARD_REQUIRED ON)
# Specifically check for gcc-7 or later. gcc-5 is installed on many systems
# and will accept -std=c++17, but does not fully support the standard.
if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
if(CMAKE_CXX_COMPILER_VERSION VERSION_LESS "7.0.0")
message(FATAL_ERROR "gcc 7 or later is required to build gtirb")
endif()
endif()
set(CMAKE_CXX_VISIBILITY_PRESET hidden)
# Base include path for the GTIRB library.
include_directories(${CMAKE_BINARY_DIR}/include)
#
# Global Options (Compile / Link)
#
add_compile_options(-DBOOST_MULTI_INDEX_DISABLE_SERIALIZATION)
# MSVC-specific Options
if(${CMAKE_CXX_COMPILER_ID} STREQUAL MSVC)
if(NOT GTIRB_MSVC_PARALLEL_COMPILE_JOBS STREQUAL "1")
if(GTIRB_MSVC_PARALLEL_COMPILE_JOBS STREQUAL "0")
add_compile_options(-MP)
message(STATUS "Parallel compilation enabled")
else()
add_compile_options(-MP${GTIRB_MSVC_PARALLEL_COMPILE_JOBS})
message(
STATUS
"Parallel compilation with ${GTIRB_MSVC_PARALLEL_COMPILE_JOBS} jobs"
)
endif()
else()
message(STATUS "Parallel compilation disabled")
endif()
add_compile_options(-D_CRT_SECURE_NO_WARNINGS)
add_compile_options(-D_SCL_SECURE_NO_WARNINGS)
# We need to add both so that there is not a mismatch between Win32 SDK
# headers (which use UNICODE) and C Standard Library headers (which use
# _UNICODE).
add_compile_options(-D_UNICODE)
add_compile_options(-DUNICODE)
add_compile_options(-D_WIN32)
# Disable macro definitions for min and max that conflict with the STL.
add_compile_options(-DNOMINMAX)
# Enable RTTI. FIXME: stop using typeid so we can disable this and add -fno-
# rtti to the Clang/GCC compiler options.
add_compile_options(-GR)
# Enable exceptions, which are basically required because of our reliance on
# boost.
add_compile_options(-EHsc)
# Enabled a sensible warning level and treat all warnings as errors.
add_compile_options(-W4)
add_compile_options(-WX)
add_compile_options(-sdl) # Enable extra security checks
add_compile_options(-permissive-) # Disable permissive mode
add_compile_options(-wd4996) # VC8: Deprecated libc functions.
# This is a warning about a change in behavior from old versions of visual
# c++. We want the new (standard-compliant) behavior, so we don't want the
# warning. The warning is that using an array in a class initializer list
# will cause its elements to be default initialized.
add_compile_options(-wd4351)
add_compile_options(-wd4146) # unary minus operator applied to unsigned
# type, result still unsigned
# Release target options
add_compile_options($<$<CONFIG:Release>:-GL>) # Enable whole program
# optimization
add_link_options($<$<CONFIG:Release>:-ltcg>) # Enable link-time code
# generation
elseif((${CMAKE_CXX_COMPILER_ID} STREQUAL GNU) OR (${CMAKE_CXX_COMPILER_ID}
STREQUAL Clang)
)
add_compile_options(-Wall -Wextra -Wpointer-arith -Wshadow -Werror)
add_compile_options(-fPIC)
endif()
endif()
# ---------------------------------------------------------------------------
# Boost
# ---------------------------------------------------------------------------
if(GTIRB_CXX_API)
find_package(Boost 1.67 REQUIRED)
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()
include_directories(SYSTEM ${Boost_INCLUDE_DIRS})
endif()
# ---------------------------------------------------------------------------
# Google Test Application
# ---------------------------------------------------------------------------
if(GTIRB_ENABLE_TESTS AND GTIRB_CXX_API)
# Pull in Google Test
# https://github.com/google/googletest/tree/master/googletest#incorporating-
# into-an-existing-cmake-project
# Download and unpack googletest at configure time
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(WARNING "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(WARNING "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")
endif()
# ---------------------------------------------------------------------------
# protobuf
# ---------------------------------------------------------------------------
find_package(Protobuf 3.0.0 REQUIRED)
if(NOT BUILD_SHARED_LIBS)
set(Protobuf_USE_STATIC_LIBS ON)
endif()
include_directories(SYSTEM ${PROTOBUF_INCLUDE_DIRS})
add_subdirectory(proto)
# ---------------------------------------------------------------------------
# gtirb sources
# ---------------------------------------------------------------------------
if(GTIRB_CXX_API)
add_subdirectory(src)
endif()
if(GTIRB_PY_API)
add_subdirectory(python)
endif()
add_subdirectory(doc)
# ---------------------------------------------------------------------------
# Export config for use by other CMake projects
# ---------------------------------------------------------------------------
if(GTIRB_CXX_API)
# --- For direct use from the build directory/cmake registry ---
# This exports the targets
export(TARGETS gtirb proto
FILE "${CMAKE_CURRENT_BINARY_DIR}/gtirbTargets.cmake"
)
# This is the main config file that find_package will look for.
file(
WRITE "${CMAKE_CURRENT_BINARY_DIR}/gtirbConfig.cmake"
"
include(\"\$\{CMAKE_CURRENT_LIST_DIR\}/gtirbTargets.cmake\")
set_property(
TARGET gtirb
APPEND PROPERTY
INTERFACE_INCLUDE_DIRECTORIES \"${CMAKE_BINARY_DIR}/include\" \"${PROTOBUF_INCLUDE_DIRS}\"
)
"
)
# Add the build directory to the user CMake registry, so find_package can
# locate it automatically.
export(PACKAGE gtirb)
# --- For the installed copy ---
# Main config file for find_package, just includes the targets file.
file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/export/gtirbConfig.cmake"
"include(\"\$\{CMAKE_CURRENT_LIST_DIR\}/gtirbTargets.cmake\")"
)
# In this mode, find_package also seems to require a version file
set(version_file "${CMAKE_CURRENT_BINARY_DIR}/gtirbConfig-version.cmake")
write_basic_package_version_file(
${version_file}
VERSION ${GTIRB_VERSION}
COMPATIBILITY AnyNewerVersion
)
# Copy the config files to the install location
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/export/gtirbConfig.cmake
${version_file} DESTINATION lib/gtirb
)
# This exports the targets to the install location.
install(EXPORT gtirbTargets DESTINATION lib/gtirb)
endif()
# ---------------------------------------------------------------------------
# Package generation with cpack
# ---------------------------------------------------------------------------
set(CMAKE_PROJECT_HOMEPAGE_URL https://github.com/GrammaTech/gtirb)
set(CPACK_PACKAGE_VERSION_MAJOR ${GTIRB_MAJOR_VERSION})
set(CPACK_PACKAGE_VERSION_MINOR ${GTIRB_MINOR_VERSION})
set(CPACK_PACKAGE_VERSION_PATCH ${GTIRB_PATCH_VERSION})
set(CPACK_PACKAGE_DESCRIPTION_SUMMARY
"The GrammaTech Intermediate Representation for Binaries (GTIRB) is a machine code analysis and rewriting data structure."
)
set(CPACK_PACKAGE_VENDOR "GrammaTech Inc.")
set(CPACK_PACKAGE_CONTACT [email protected])
set(CPACK_PACKAGE_DESCRIPTION_FILE ${CMAKE_CURRENT_SOURCE_DIR}/README.md)
set(CPACK_PACKAGE_RESOURCE_FILE_LICENSE ${CMAKE_CURRENT_SOURCE_DIR}/LICENSE.md)
set(CPACK_DEBIAN_PACKAGE_SECTION devel)
include(CPack)