Repository navigation
Expand file tree
/
Copy pathCMakeLists.txt
More file actions
360 lines (301 loc) · 12.9 KB
/
Copy pathCMakeLists.txt
File metadata and controls
360 lines (301 loc) · 12.9 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
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
# Need 3.10 to support CXX_STANDARD=17
cmake_minimum_required(VERSION 3.10.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(TBDISASM_MAJOR_VERSION ${CMAKE_MATCH_1})
string(REGEX MATCH "VERSION_MINOR ([0-9]*)" _ ${VERSION_FILE})
set(TBDISASM_MINOR_VERSION ${CMAKE_MATCH_1})
string(REGEX MATCH "VERSION_PATCH ([0-9]*)" _ ${VERSION_FILE})
set(TBDISASM_PATCH_VERSION ${CMAKE_MATCH_1})
cmake_policy(SET CMP0048 NEW)
project(
tbdisasm
VERSION
"${TBDISASM_MAJOR_VERSION}.${TBDISASM_MINOR_VERSION}.${TBDISASM_PATCH_VERSION}"
)
# ---------------------------------------------------------------------------
# Build options
# ---------------------------------------------------------------------------
# Wrapper for BUILD_SHARED_LIBS which defaults to 'ON'
option(TBDISASM_BUILD_SHARED_LIBS "Build shared libraries." ON)
if(TBDISASM_BUILD_SHARED_LIBS)
set(BUILD_SHARED_LIBS ON)
else()
set(BUILD_SHARED_LIBS OFF)
endif()
option(TBDISASM_ENABLE_TESTS "Enable build and running unit tests." ON)
option(TBDISASM_USE_INSTR_TRACE "Enable the instr_trace utility as a tracer."
ON)
option(TBDISASM_USE_CAPSTONE "Enable use of capstone as a decoder." ON)
option(TBDISASM_BUILD_DRIVER "Build the tbdisasm command-line utility." ON)
option(ENABLE_WINDOWS_DEBUG "Enable debugging on Windows binaries" OFF)
# ---------------------------------------------------------------------------
# Global settings
# ---------------------------------------------------------------------------
if(TBDISASM_USE_INSTR_TRACE AND TBDISASM_USE_CAPSTONE)
set(TBDISASM_BINARY ON)
else()
set(TBDISASM_BINARY OFF)
endif()
# 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)
# Turn off deprecation warnings
add_compile_options(-D_CRT_SECURE_NO_WARNINGS)
add_compile_options(-D_SILENCE_ALL_CXX17_DEPRECATION_WARNINGS)
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
add_link_options($<$<CONFIG:Release>:-ltcg>) # Enable link-time code
# generation
# 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 tbdisasm, use the
# 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()
elseif((${CMAKE_CXX_COMPILER_ID} STREQUAL GNU) OR (${CMAKE_CXX_COMPILER_ID}
STREQUAL Clang))
add_compile_options(-Wall -Wextra -Wpointer-arith -Werror)
add_compile_options(-fPIC)
# Boost gets these.
add_compile_options(-Wno-unused-local-typedefs)
# Seen in protobuf headers.
add_compile_options(-Wno-unused-parameter)
endif()
set(IMPORTED_TARGETS)
# ---------------------------------------------------------------------------
# GT Dependences
# ---------------------------------------------------------------------------
find_package(gtirb REQUIRED)
add_definitions(-DGTIRB_WRAP_UTILS_IN_NAMESPACE)
list(APPEND IMPORTED_TARGETS gtirb)
include_directories(${gtirb_INCLUDE_DIRS})
if(TBDISASM_USE_INSTR_TRACE)
find_package(instr_trace REQUIRED)
list(APPEND IMPORTED_TARGETS instr_trace)
include_directories(${instr_trace_INCLUDE_DIRS})
endif()
# ---------------------------------------------------------------------------
# Boost
# ---------------------------------------------------------------------------
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)
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.
set(BOOST_COMPONENTS filesystem iterator program_options)
if(TARGET Boost::headers)
get_target_property(Boost_INCLUDE_DIRS Boost::headers
INTERFACE_INCLUDE_DIRECTORIES)
endif()
list(APPEND IMPORTED_TARGETS Boost::boost ${Boost_LIBRARIES})
include_directories(${Boost_INCLUDE_DIRS})
# ---------------------------------------------------------------------------
# capstone
# ---------------------------------------------------------------------------
if(TBDISASM_USE_CAPSTONE)
if(BUILD_SHARED_LIBS)
find_library(CAPSTONE NAMES capstone)
else()
if(${CMAKE_CXX_COMPILER_ID} STREQUAL MSVC)
find_library(CAPSTONE NAMES capstone)
else()
find_library(CAPSTONE NAMES libcapstone.a)
endif()
endif()
if(CAPSTONE)
find_program(CSTOOL "cstool")
execute_process(COMMAND "${CSTOOL}" "-v" OUTPUT_VARIABLE CSTOOL_VERSION)
string(REGEX MATCH "v([0-9]+\.[0-9]+\.[0-9]+)" CSTOOL_VERSION
"${CSTOOL_VERSION}")
set(CSTOOL_VERSION "${CMAKE_MATCH_1}")
# cmake cannot find capstone headers automatically on Windows so manually
# add them. The include directory should be in the same directory as
# capstone.dll and capstone.lib.
if(${CMAKE_CXX_COMPILER_ID} STREQUAL MSVC)
get_filename_component(CAPSTONE_DIR ${CAPSTONE} DIRECTORY)
get_filename_component(CAPSTONE_INC_DIR "include" REALPATH BASE_DIR
${CAPSTONE_DIR})
include_directories(${CAPSTONE_INC_DIR})
endif()
endif()
if(NOT CAPSTONE OR "${CSTOOL_VERSION}" VERSION_LESS "4.0.1")
message(
FATAL_ERROR
" No Capstone installation found.\n"
" - If Capstone is not installed, install it from souce.\n"
" You can get the latest version of Capstone at:\n"
" http://www.capstone-engine.org/\n"
" - If Capstone is installed, make sure the installation location is in your PATH,\n"
" and it is at least version 4.0.1.\n")
endif()
if(NOT CAPSTONE_INCLUDE_DIR)
get_filename_component(CAPSTONE_LIB_DIR ${CAPSTONE} DIRECTORY)
get_filename_component(CAPSTONE_INCLUDE_DIR ${CAPSTONE_LIB_DIR}/../include
ABSOLUTE)
if(NOT EXISTS ${CAPSTONE_INCLUDE_DIR}/capstone)
message(WARNING "CAPSTONE_INCLUDE_DIR not found")
unset(CAPSTONE_INCLUDE_DIR)
endif()
unset(CAPSTONE_LIB_DIR)
endif()
endif()
# ---------------------------------------------------------------------------
# LIEF
# ---------------------------------------------------------------------------
if(${CMAKE_CXX_COMPILER_ID} STREQUAL MSVC)
set(LIEF_DIR "C:/lief-0.13.0/share/LIEF/cmake")
endif()
find_package(LIEF 0.13.0 REQUIRED COMPONENTS STATIC)
if(${CMAKE_CXX_COMPILER_ID} STREQUAL MSVC)
include_directories("C:/lief-0.13.0/include")
elseif((${CMAKE_CXX_COMPILER_ID} STREQUAL GNU) OR (${CMAKE_CXX_COMPILER_ID}
STREQUAL Clang))
include_directories("/opt/lief/include")
endif()
# ---------------------------------------------------------------------------
# Google Test Application
# ---------------------------------------------------------------------------
if(TBDISASM_ENABLE_TESTS)
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")
enable_testing()
endif()
# ---------------------------------------------------------------------------
# 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)
target_link_libraries(${TARGET} PRIVATE "-Wl,--build-id=0x${BUILD_ID}")
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()
# Add src subdirectory which contains targets
add_subdirectory(src)
# ---------------------------------------------------------------------------
# Export config for use by other CMake projects
# ---------------------------------------------------------------------------
export(TARGETS tbdisasm
FILE "${CMAKE_CURRENT_BINARY_DIR}/tbdisasmTargets.cmake")
# Build tree config
configure_file("${CMAKE_CURRENT_LIST_DIR}/tbdisasmConfig.cmake.in"
"${CMAKE_CURRENT_BINARY_DIR}/tbdisasmConfig.cmake")
# Install tree config
configure_file("${CMAKE_CURRENT_LIST_DIR}/tbdisasmConfig.cmake.in"
"${CMAKE_CURRENT_BINARY_DIR}/export/tbdisasmConfig.cmake")
install(
FILES ${CMAKE_CURRENT_BINARY_DIR}/export/tbdisasmConfig.cmake
DESTINATION lib/tbdisasm
COMPONENT cmake_config)
install(
EXPORT tbdisasmTargets
DESTINATION lib/tbdisasm
COMPONENT cmake_target)
export(PACKAGE tbdisasm)
# ---------------------------------------------------------------------------
# Debian package generation
# ---------------------------------------------------------------------------
set(CPACK_PROJECT_CONFIG_FILE ${CMAKE_CURRENT_SOURCE_DIR}/cpack-config.cmake)
set(CMAKE_PROJECT_HOMEPAGE_URL
https://git.grammatech.com/rewriting/Templates/CMake)
set(CPACK_PACKAGE_VERSION_MAJOR ${TBDISASM_MAJOR_VERSION})
set(CPACK_PACKAGE_VERSION_MINOR ${TBDISASM_MINOR_VERSION})
set(CPACK_PACKAGE_VERSION_PATCH ${TBDISASM_PATCH_VERSION})
set(CPACK_PACKAGE_DESCRIPTION_SUMMARY "Package description")
set(CPACK_PACKAGE_VENDOR "GrammaTech Inc.")
set(CPACK_PACKAGE_CONTACT someone@grammatech.com)
set(CPACK_PACKAGE_DESCRIPTION_FILE ${CMAKE_CURRENT_SOURCE_DIR}/README.md)
set(CPACK_PACKAGE_RESOURCE_FILE_LICENSE ${CMAKE_CURRENT_SOURCE_DIR}/LICENSE.txt)
set(CPACK_DEBIAN_PACKAGE_SECTION devel)
set(CPACK_TBDISASM_VERSION "${tbdisasm_VERSION}")
include(CPack)