-
Notifications
You must be signed in to change notification settings - Fork 97
/
CMakeLists.txt
276 lines (242 loc) · 7.82 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
cmake_minimum_required(VERSION 3.11)
project(funchook VERSION 2.0.0 LANGUAGES C ASM)
include("GNUInstallDirs")
include(FetchContent)
if(NOT FUNCHOOK_CPU)
set(FUNCHOOK_CPU ${CMAKE_SYSTEM_PROCESSOR})
if (FUNCHOOK_CPU MATCHES "x86_64" OR FUNCHOOK_CPU MATCHES "i.86" OR FUNCHOOK_CPU MATCHES "AMD64")
set(FUNCHOOK_CPU x86)
endif ()
if (FUNCHOOK_CPU MATCHES "aarch64")
set(FUNCHOOK_CPU arm64)
endif ()
if (FUNCHOOK_CPU MATCHES "ARM64")
set(FUNCHOOK_CPU arm64)
endif ()
endif ()
if (FUNCHOOK_CPU MATCHES "arm64")
set(FUNCHOOK_DEFAULT_DISASM "capstone")
if (MSVC)
cmake_minimum_required(VERSION 3.26)
endif ()
else ()
set(FUNCHOOK_DEFAULT_DISASM "distorm")
endif ()
option(FUNCHOOK_BUILD_SHARED "build shared library" ON)
option(FUNCHOOK_BUILD_STATIC "build static library" ON)
option(FUNCHOOK_BUILD_TESTS "Build tests" ON)
option(FUNCHOOK_INSTALL "Install Funchook" ON)
set(FUNCHOOK_DISASM ${FUNCHOOK_DEFAULT_DISASM} CACHE STRING "disassembler engine")
if (CMAKE_SYSTEM_NAME STREQUAL "Linux")
set(_GNU_SOURCE 1)
include(CheckCSourceCompiles)
# musl libc doesn't provide the GNU-specific version of strerror_r
# even when _GNU_SOURCE is defined.
check_c_source_compiles(
"#define _GNU_SOURCE 1
#include <string.h>
int main()
{
char dummy[128];
return *strerror_r(0, dummy, sizeof(dummy));
}
"
GNU_SPECIFIC_STRERROR_R
)
endif ()
include(CheckCCompilerFlag)
check_c_compiler_flag(-fvisibility=hidden HAVE_FVISIBILITY_HIDDEN)
if (FUNCHOOK_DISASM STREQUAL capstone)
set(DISASM_CAPSTONE 1)
elseif (FUNCHOOK_DISASM STREQUAL distorm)
set(DISASM_DISTORM 1)
elseif (FUNCHOOK_DISASM STREQUAL zydis)
set(DISASM_ZYDIS 1)
else ()
message(FATAL_ERROR "Unknown FUNCHOOK_DISASM type: ${FUNCHOOK_DISASM}")
endif ()
if (CMAKE_TOOLCHAIN_FILE)
get_filename_component(TOOLCHAIN_FILE "${CMAKE_TOOLCHAIN_FILE}" ABSOLUTE BASE_DIR "${CMAKE_BINARY_DIR}")
else ()
set(TOOLCHAIN_FILE "")
endif ()
if (WIN32)
set(FUNCHOOK_OS windows)
set(FUNCHOOK_DEPS psapi)
else ()
set(FUNCHOOK_OS unix)
set(FUNCHOOK_DEPS dl)
endif ()
function(add_subdirectory_pic source_dir binary_dir)
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
add_subdirectory(${source_dir} ${binary_dir})
endfunction(add_subdirectory_pic)
#
# capstone
#
if (DISASM_CAPSTONE)
FetchContent_Declare(
capstone
GIT_REPOSITORY https://github.com/capstone-engine/capstone.git
GIT_TAG 5.0.1
GIT_SHALLOW TRUE
)
FetchContent_GetProperties(capstone)
if(NOT capstone_POPULATED)
FetchContent_Populate(capstone)
string(TOUPPER ${FUNCHOOK_CPU} FUNCHOOK_CPU_UPPER)
set(CAPSTONE_BUILD_SHARED OFF CACHE BOOL "")
set(CAPSTONE_BUILD_STATIC_RUNTIME OFF CACHE BOOL "")
set(CAPSTONE_BUILD_TESTS OFF CACHE BOOL "")
set(CAPSTONE_BUILD_CSTOOL OFF CACHE BOOL "")
set(CAPSTONE_ARCHITECTURE_DEFAULT OFF CACHE BOOL "")
set(CAPSTONE_${FUNCHOOK_CPU_UPPER}_SUPPORT ON CACHE BOOL "")
add_subdirectory_pic(${capstone_SOURCE_DIR} ${capstone_BINARY_DIR})
endif()
list(APPEND FUNCHOOK_DEPS capstone)
set(DISASM capstone)
endif ()
#
# distorm
#
if (DISASM_DISTORM)
FetchContent_Declare(
distorm
GIT_REPOSITORY https://github.com/gdabah/distorm.git
GIT_TAG 3.5.2b
GIT_SHALLOW TRUE
)
FetchContent_GetProperties(distorm)
if(NOT distorm_POPULATED)
FetchContent_Populate(distorm)
file(GLOB distorm_SRC_FILES ${distorm_SOURCE_DIR}/src/*.c)
add_library(distorm STATIC ${distorm_SRC_FILES})
set_target_properties(distorm PROPERTIES POSITION_INDEPENDENT_CODE ON)
target_include_directories(distorm PUBLIC ${distorm_SOURCE_DIR}/include)
if (HAVE_FVISIBILITY_HIDDEN)
target_compile_options(distorm PRIVATE -fvisibility=hidden)
endif ()
if (MSVC)
# workaround for https://github.com/gdabah/distorm/pull/179
set_source_files_properties(${distorm_SOURCE_DIR}/src/prefix.c PROPERTIES COMPILE_FLAGS /wd4819)
endif ()
endif()
list(APPEND FUNCHOOK_DEPS distorm)
set(DISASM distorm)
endif ()
#
# zydis
#
if (DISASM_ZYDIS)
FetchContent_Declare(
Zydis
GIT_REPOSITORY https://github.com/zyantific/zydis.git
GIT_TAG v4.1.0
GIT_SHALLOW TRUE
)
FetchContent_GetProperties(Zydis)
if(NOT zydis_POPULATED)
FetchContent_Populate(Zydis)
set(ZYDIS_BUILD_SHARED_LIB OFF CACHE BOOL "")
set(ZYDIS_BUILD_EXAMPLES OFF CACHE BOOL "")
set(ZYDIS_BUILD_TOOLS OFF CACHE BOOL "")
add_subdirectory_pic(${zydis_SOURCE_DIR} ${zydis_BINARY_DIR})
endif()
list(APPEND FUNCHOOK_DEPS Zydis)
set(DISASM Zydis)
endif ()
#
# funchook
#
set(FUNCHOOK_SOURCES src/funchook.c src/arch_${FUNCHOOK_CPU}.c src/os_${FUNCHOOK_OS}.c src/disasm_${DISASM}.c)
if ((FUNCHOOK_CPU STREQUAL x86) AND (CMAKE_SIZEOF_VOID_P EQUAL "8"))
if (MSVC)
# Microsoft x64 calling convention (Microsoft Visual C++)
enable_language(ASM_MASM)
set(FUNCHOOK_SOURCES ${FUNCHOOK_SOURCES} src/prehook-x86_64-ms.asm)
elseif (WIN32)
# Microsoft x64 calling convention (Mingw-w64)
set(FUNCHOOK_SOURCES ${FUNCHOOK_SOURCES} src/prehook-x86_64-ms.S)
else ()
# System V ABI (Linux, macOS)
set(FUNCHOOK_SOURCES ${FUNCHOOK_SOURCES} src/prehook-x86_64-sysv.S)
endif ()
endif ()
if ((FUNCHOOK_CPU STREQUAL x86) AND (CMAKE_SIZEOF_VOID_P EQUAL "4"))
if (MSVC)
# Windows (Microsoft Visual C++)
enable_language(ASM_MASM)
set(FUNCHOOK_SOURCES ${FUNCHOOK_SOURCES} src/prehook-i686-ms.asm)
set_source_files_properties(src/prehook-i686-ms.asm PROPERTIES COMPILE_OPTIONS "/safeseh")
else ()
# Windows and Linux (gcc)
set(FUNCHOOK_SOURCES ${FUNCHOOK_SOURCES} src/prehook-i686-gas.S)
endif ()
endif ()
if (FUNCHOOK_CPU STREQUAL "arm64")
if (MSVC)
enable_language(ASM_MARMASM)
set(FUNCHOOK_SOURCES ${FUNCHOOK_SOURCES} src/prehook-arm64-ms.asm)
else ()
set(FUNCHOOK_SOURCES ${FUNCHOOK_SOURCES} src/prehook-arm64-gas.S)
endif ()
endif ()
set(FUNCHOOK_PROPERTIES
OUTPUT_NAME funchook
VERSION ${PROJECT_VERSION}
SOVERSION ${PROJECT_VERSION_MAJOR}
DEFINE_SYMBOL FUNCHOOK_EXPORTS)
configure_file(src/cmake_config.h.in config.h)
function (add_funchook_library target_name target_type)
add_library(${target_name} ${target_type} ${FUNCHOOK_SOURCES})
set_target_properties(${target_name} PROPERTIES ${FUNCHOOK_PROPERTIES})
target_include_directories(${target_name} PUBLIC include)
target_include_directories(${target_name} PRIVATE ${CMAKE_CURRENT_BINARY_DIR}) # to include config.h
target_link_libraries(${target_name} PRIVATE ${FUNCHOOK_DEPS})
if (HAVE_FVISIBILITY_HIDDEN)
target_compile_options(${target_name} PRIVATE -fvisibility=hidden)
endif ()
if (CMAKE_COMPILER_IS_GNUCC)
target_compile_options(${target_name} PRIVATE -Wall)
endif ()
endfunction ()
if (FUNCHOOK_BUILD_SHARED)
add_funchook_library(funchook-shared SHARED)
if (MINGW)
set_target_properties(funchook-shared PROPERTIES PREFIX "")
endif ()
if (MSVC)
set_target_properties(funchook-shared PROPERTIES IMPORT_SUFFIX _dll.lib)
endif ()
endif ()
if (FUNCHOOK_BUILD_STATIC)
add_funchook_library(funchook-static STATIC)
endif ()
#
# tests
#
enable_testing()
if (FUNCHOOK_BUILD_TESTS)
add_subdirectory(test)
endif ()
#
# install
#
if (FUNCHOOK_INSTALL)
install(FILES include/funchook.h DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
if (FUNCHOOK_BUILD_SHARED)
install(TARGETS funchook-shared
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR})
if (MSVC)
install(FILES $<TARGET_PDB_FILE:funchook-shared> DESTINATION ${CMAKE_INSTALL_BINDIR} OPTIONAL)
endif ()
endif ()
if (FUNCHOOK_BUILD_STATIC)
install(TARGETS funchook-static
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR})
endif ()
endif ()