-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathCMakeLists.txt
266 lines (209 loc) · 7.37 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
# Copyright (c) 2024, Trail of Bits, Inc.
# This source code is licensed in accordance with the terms specified in the
# LICENSE file found in the root directory of this source tree.
cmake_minimum_required(VERSION 3.25)
if (POLICY CMP0025)
cmake_policy(SET CMP0025 NEW)
endif()
if(POLICY CMP0068)
cmake_policy(SET CMP0068 NEW)
set(CMAKE_BUILD_WITH_INSTALL_NAME_DIR ON)
endif()
if(POLICY CMP0075)
cmake_policy(SET CMP0075 NEW)
endif()
if(POLICY CMP0077)
cmake_policy(SET CMP0077 NEW)
endif()
if (POLICY CMP0116)
cmake_policy(SET CMP0116 NEW)
endif()
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
include(prevent_in_source_builds)
# check if patchestry is being used directly or via add_subdirectory,
# but allow overriding
if(NOT DEFINED PATCHESTRY_MASTER_PROJECT)
if(CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR)
set(PATCHESTRY_MASTER_PROJECT ON)
else()
set(PATCHESTRY_MASTER_PROJECT OFF)
endif()
endif()
project(
patchestry
VERSION 0.1.0
DESCRIPTION
"Patchestry is a binary patching framework built with MLIR and Ghidra."
HOMEPAGE_URL "https://github.com/lifting-bits/patchestry"
LANGUAGES C CXX
)
include(project_settings)
include(project_utils)
#
# Setup package version
#
setup_package_version_variables(patchestry)
set(PATCHESTRY_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR})
set(PATCHESTRY_BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR})
set(PATCHESTRY_INCLUDE_DIR ${PATCHESTRY_BINARY_DIR}/include)
# Configure Patchestry Version.inc file.
configure_file(
${CMAKE_CURRENT_SOURCE_DIR}/include/patchestry/Version.inc.in
${CMAKE_CURRENT_BINARY_DIR}/include/patchestry/Version.inc
)
#
# CCACHE
#
find_program(CCACHE_PROGRAM ccache)
if (CCACHE_PROGRAM)
set_property(GLOBAL PROPERTY RULE_LAUNCH_COMPILE "${CCACHE_PROGRAM}")
endif()
FindAndSelectClangCompiler()
#
# LLVM & MLIR & Clang
#
find_package(LLVM 19.1 REQUIRED CONFIG)
message(STATUS "Found LLVM ${LLVM_PACKAGE_VERSION}")
message(STATUS "Using LLVMConfig.cmake in: ${LLVM_DIR}")
list(APPEND CMAKE_MODULE_PATH "${LLVM_CMAKE_DIR}")
include(TableGen)
include(AddLLVM)
include(HandleLLVMOptions)
find_package(MLIR ${LLVM_PACKAGE_VERSION} REQUIRED CONFIG)
message(STATUS "Using MLIRConfig.cmake in: ${MLIR_DIR}")
list(APPEND CMAKE_MODULE_PATH "${MLIR_CMAKE_DIR}")
include(AddMLIR)
find_package(Clang ${LLVM_PACKAGE_VERSION} CONFIG REQUIRED)
message(STATUS "Using ClangConfig.cmake in: ${Clang_DIR}")
list(APPEND CMAKE_MODULE_PATH "${MLIR_CMAKE_DIR}")
find_package(VAST CONFIG REQUIRED)
message(STATUS "Using VASTConfig.cmake in: ${VAST_DIR}")
find_package(gap CONFIG REQUIRED)
message(STATUS "Using gapConfig.cmake in: ${gap_DIR}")
set(LLVM_RUNTIME_OUTPUT_INTDIR ${CMAKE_BINARY_DIR}/bin)
set(LLVM_LIBRARY_OUTPUT_INTDIR ${CMAKE_BINARY_DIR}/lib)
include_directories(SYSTEM ${LLVM_INCLUDE_DIRS})
include_directories(SYSTEM ${MLIR_INCLUDE_DIRS})
include_directories(${PROJECT_SOURCE_DIR}/include)
include_directories(${PROJECT_BINARY_DIR}/include)
add_definitions(${LLVM_DEFINITIONS})
separate_arguments(LLVM_DEFINITIONS_LIST NATIVE_COMMAND ${LLVM_DEFINITIONS})
add_definitions(${LLVM_DEFINITIONS})
if (MLIR_LINK_MLIR_DYLIB)
set(MLIR_LIBS MLIR)
else()
get_property(MLIR_DIALECT_LIBS GLOBAL PROPERTY MLIR_DIALECT_LIBS)
get_property(MLIR_CONVERSION_LIBS GLOBAL PROPERTY MLIR_CONVERSION_LIBS)
get_property(MLIR_EXTENSION_LIBS GLOBAL PROPERTY MLIR_EXTENSION_LIBS)
get_property(MLIR_TRANSLATION_LIBS GLOBAL PROPERTY MLIR_TRANSLATION_LIBS)
set(MLIR_LIBS
MLIRAnalysis
MLIRDialect
MLIRExecutionEngine
MLIRIR
MLIRParser
MLIRPass
MLIRSupport
MLIRTransforms
MLIRTransformUtils
${MLIR_DIALECT_LIBS}
${MLIR_CONVERSION_LIBS}
${MLIR_EXTENSION_LIBS}
${MLIR_TRANSLATION_LIBS}
)
endif()
add_custom_target(patchestry-generic-headers)
set_target_properties(patchestry-generic-headers PROPERTIES FOLDER "Misc")
add_custom_target(patchestry-headers)
set_target_properties(patchestry-headers PROPERTIES FOLDER "Misc")
add_dependencies(patchestry-headers patchestry-generic-headers)
add_custom_target(patchestry-tools)
set_target_properties(patchestry-tools PROPERTIES FOLDER "Misc")
add_custom_target(patchestry-doc)
define_property(GLOBAL PROPERTY VAST_INSTALL_TARGETS)
add_library(patchestry_settings INTERFACE)
target_include_directories(patchestry_settings INTERFACE
$<BUILD_INTERFACE:${PATCHESTRY_SOURCE_DIR}/include>
$<BUILD_INTERFACE:${PATCHESTRY_BINARY_DIR}/include>
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
)
include(compiler_warnings)
set_project_warnings(patchestry_settings)
# sanitizer options if supported by compiler
include(sanitizers)
enable_sanitizers(patchestry_settings)
add_subdirectory(include)
add_subdirectory(lib)
add_subdirectory(tools)
option(PATCHESTRY_ENABLE_TESTING "Enable Test Builds" ON)
if (PATCHESTRY_ENABLE_TESTING)
enable_testing()
add_subdirectory(test)
endif()
#
# install settings
#
option(PATCHESTRY_INSTALL "Generate the install target." ${PATCHESTRY_MASTER_PROJECT})
#
# packaging support
#
if (PATCHESTRY_INSTALL)
set(PATCHESTRY_CMAKE_INSTALL_DIR ${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME})
get_property(PATCHESTRY_INSTALL_TARGETS GLOBAL PROPERTY PATCHESTRY_INSTALL_TARGETS)
install(DIRECTORY ${PATCHESTRY_INCLUDE_DIR}/patchestry
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
COMPONENT patchestry-headers
FILES_MATCHING
PATTERN "*.h"
PATTERN "*.hpp"
PATTERN "*.h.inc"
PATTERN "*.hpp.inc"
PATTERN "CMakeFiles" EXCLUDE
)
install(DIRECTORY ${PATCHESTRY_SOURCE_DIR}/include/patchestry
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
COMPONENT patchestry-headers
FILES_MATCHING
PATTERN "*.h"
PATTERN "*.hpp"
PATTERN "CMakeFiles" EXCLUDE
)
install(TARGETS patchestry_settings EXPORT PATCHESTRYTargets)
set(PATCHESTRY_EXPORT_NAME PATCHESTRYTargets)
install(EXPORT PATCHESTRYTargets
FILE ${PATCHESTRY_EXPORT_NAME}.cmake
NAMESPACE PATCHESTRY::
DESTINATION ${PATCHESTRY_CMAKE_INSTALL_DIR}
)
set(CPACK_PACKAGE_VENDOR "Trail of Bits")
set(CPACK_PACKAGE_DESCRIPTION_SUMMARY
"Patchestry: an MLIR-based binary patching framework."
)
set(CPACK_PACKAGE_VERSION_MAJOR ${CMAKE_PROJECT_VERSION_MAJOR})
set(CPACK_PACKAGE_VERSION_MINOR ${CMAKE_PROJECT_VERSION_MINOR})
set(CPACK_PACKAGE_VERSION_PATCH ${CMAKE_PROJECT_VERSION_PATCH})
set(CPACK_RESOURCE_FILE_LICENSE "${CMAKE_CURRENT_SOURCE_DIR}/LICENSE")
set(CPACK_RESOURCE_FILE_README "${CMAKE_CURRENT_SOURCE_DIR}/README.md")
set(CPACK_OUTPUT_FILE_PREFIX "${CMAKE_CURRENT_BINARY_DIR}/package")
set(CPACK_PACKAGE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
include(CPack)
set(PATCHESTRY_CONFIG_NAME "${PROJECT_NAME}Config")
set(PATCHESTRY_PACKAGE_CONFIG_FILE "${PATCHESTRY_CONFIG_NAME}.cmake")
set(PATCHESTRY_PACKAGE_CONFIG_VERSION_FILE "${PATCHESTRY_CONFIG_NAME}Version.cmake")
include(CMakePackageConfigHelpers)
configure_package_config_file(
"${CMAKE_CURRENT_SOURCE_DIR}/cmake/${PATCHESTRY_CONFIG_NAME}.cmake.in"
"${CMAKE_CURRENT_BINARY_DIR}/${PATCHESTRY_PACKAGE_CONFIG_FILE}"
INSTALL_DESTINATION ${PATCHESTRY_CMAKE_INSTALL_DIR}
)
write_basic_package_version_file(
${PATCHESTRY_PACKAGE_CONFIG_VERSION_FILE}
VERSION ${CPACK_PACKAGE_VERSION}
COMPATIBILITY SameMajorVersion
)
install(FILES
"${CMAKE_CURRENT_BINARY_DIR}/${PATCHESTRY_PACKAGE_CONFIG_FILE}"
"${CMAKE_CURRENT_BINARY_DIR}/${PATCHESTRY_PACKAGE_CONFIG_VERSION_FILE}"
DESTINATION ${PATCHESTRY_CMAKE_INSTALL_DIR}
)
endif()