forked from zyantific/zydis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCMakeLists.txt
229 lines (206 loc) · 9.38 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
cmake_minimum_required(VERSION 3.1 FATAL_ERROR)
include(GenerateExportHeader)
include(GNUInstallDirs)
project(Zydis VERSION 2.0)
# =============================================================================================== #
# Overridable options #
# =============================================================================================== #
# Features
option(ZYDIS_FEATURE_DECODER
"Enable instruction decoding functionality"
ON)
option(ZYDIS_FEATURE_FORMATTER
"Enable instruction formatting functionality"
ON)
option(ZYDIS_FEATURE_EVEX
"Enable support for EVEX instructions"
ON)
option(ZYDIS_FEATURE_MVEX
"Enable support for MVEX instructions"
ON)
# Build configuration
option(BUILD_SHARED_LIBS
"Build shared libraries"
OFF)
option(ZYDIS_WHOLE_PROGRAM_OPTIMIZATION
"Enable whole program optimization"
OFF)
option(ZYDIS_NO_LIBC
"Don't use any C standard library functions (for exotic build-envs like kernel drivers)"
OFF)
option(ZYDIS_BUILD_EXAMPLES
"Build examples"
ON)
option(ZYDIS_BUILD_TOOLS
"Build tools"
ON)
option(ZYDIS_FUZZ_AFL_FAST
"Enables AFL persistent mode and reduces prints in ZydisFuzzIn"
OFF)
option(ZYDIS_DEV_MODE
"Enable developer mode (-Wall, -Werror, ...)"
OFF)
# =============================================================================================== #
# Developer mode #
# =============================================================================================== #
# If in developer mode, hack global compiler flags.
if (ZYDIS_DEV_MODE)
set_property(GLOBAL PROPERTY USE_FOLDERS ON)
if ("${CMAKE_C_COMPILER_ID}" STREQUAL "GNU" OR
"${CMAKE_C_COMPILER_ID}" STREQUAL "Clang" OR
"${CMAKE_C_COMPILER_ID}" STREQUAL "AppleClang")
set(compiler_specific "-pedantic -Wextra -Werror")
elseif (MSVC)
set(compiler_specific "/WX /W4 /TC")
endif ()
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${compiler_specific}"
CACHE STRING "Flags used by the compiler during all build types." FORCE)
endif ()
# =============================================================================================== #
# Library configuration #
# =============================================================================================== #
add_library("Zydis")
target_include_directories("Zydis"
PUBLIC "include" ${PROJECT_BINARY_DIR}
PRIVATE "src")
target_compile_definitions("Zydis" PRIVATE "_CRT_SECURE_NO_WARNINGS" "ZYDIS_EXPORTS")
if (ZYDIS_WHOLE_PROGRAM_OPTIMIZATION AND MSVC)
set_target_properties("Zydis" PROPERTIES COMPILE_FLAGS "/GL")
set_target_properties("Zydis" PROPERTIES LINK_FLAGS_RELEASE "/LTCG")
set_target_properties("Zydis" PROPERTIES STATIC_LIBRARY_FLAGS_RELEASE "/LTCG")
endif ()
generate_export_header("Zydis" BASE_NAME "ZYDIS" EXPORT_FILE_NAME "ZydisExportConfig.h")
if (NOT ZYDIS_FEATURE_ENCODER AND NOT ZYDIS_FEATURE_DECODER)
message(
FATAL_ERROR
"\nIt's dangerous to go alone! Take at least one of these:\n"
"[ ] ZYDIS_FEATURE_ENCODER [ ] ZYDIS_FEATURE_DECODER"
)
endif ()
if (ZYDIS_FEATURE_FORMATTER AND NOT ZYDIS_FEATURE_DECODER)
message(
FATAL_ERROR
"\nZYDIS_FEATURE_FORMATTER requires ZYDIS_FEATURE_DECODER to be enabled"
)
endif ()
if (NOT ZYDIS_FEATURE_DECODER)
target_compile_definitions("Zydis" PUBLIC "ZYDIS_DISABLE_DECODER")
endif ()
if (NOT ZYDIS_FEATURE_FORMATTER)
target_compile_definitions("Zydis" PUBLIC "ZYDIS_DISABLE_FORMATTER")
endif ()
if (NOT ZYDIS_FEATURE_EVEX)
target_compile_definitions("Zydis" PUBLIC "ZYDIS_DISABLE_EVEX")
endif ()
if (NOT ZYDIS_FEATURE_MVEX)
target_compile_definitions("Zydis" PUBLIC "ZYDIS_DISABLE_MVEX")
endif ()
if (ZYDIS_NO_LIBC)
target_compile_definitions("Zydis" PUBLIC "ZYDIS_NO_LIBC")
endif ()
target_sources("Zydis"
PRIVATE
"${CMAKE_CURRENT_LIST_DIR}/include/Zydis/CommonTypes.h"
"${CMAKE_CURRENT_LIST_DIR}/include/Zydis/Defines.h"
"${CMAKE_CURRENT_LIST_DIR}/include/Zydis/MetaInfo.h"
"${CMAKE_CURRENT_LIST_DIR}/include/Zydis/Mnemonic.h"
"${CMAKE_CURRENT_LIST_DIR}/include/Zydis/Register.h"
"${CMAKE_CURRENT_LIST_DIR}/include/Zydis/SharedTypes.h"
"${CMAKE_CURRENT_LIST_DIR}/include/Zydis/Status.h"
"${CMAKE_CURRENT_LIST_DIR}/include/Zydis/String.h"
"${CMAKE_CURRENT_LIST_DIR}/include/Zydis/Utils.h"
"${CMAKE_CURRENT_LIST_DIR}/include/Zydis/Zydis.h"
"${CMAKE_CURRENT_LIST_DIR}/include/Zydis/Internal/LibC.h"
"${CMAKE_CURRENT_LIST_DIR}/include/Zydis/Internal/SharedData.h"
"src/MetaInfo.c"
"src/Mnemonic.c"
"src/Register.c"
"src/SharedData.c"
"src/String.c"
"src/Utils.c"
"src/Zydis.c")
if (ZYDIS_FEATURE_DECODER)
target_sources("Zydis"
PRIVATE
"${CMAKE_CURRENT_LIST_DIR}/include/Zydis/Decoder.h"
"${CMAKE_CURRENT_LIST_DIR}/include/Zydis/DecoderTypes.h"
"${CMAKE_CURRENT_LIST_DIR}/include/Zydis/Internal/DecoderData.h"
"src/Decoder.c"
"src/DecoderData.c")
if (ZYDIS_FEATURE_FORMATTER)
target_sources("Zydis"
PRIVATE
"${CMAKE_CURRENT_LIST_DIR}/include/Zydis/Formatter.h"
"src/Formatter.c")
endif ()
endif ()
if (BUILD_SHARED_LIBS AND WIN32)
target_sources("Zydis" PRIVATE "src/VersionInfo.rc")
endif ()
# TODO: Install CMake config.
install(TARGETS "Zydis"
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})
install(FILES
"${PROJECT_BINARY_DIR}/ZydisExportConfig.h"
DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}")
install(DIRECTORY "include/" DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
# =============================================================================================== #
# Examples #
# =============================================================================================== #
if (ZYDIS_BUILD_EXAMPLES)
if (ZYDIS_FEATURE_DECODER AND ZYDIS_FEATURE_FORMATTER)
add_executable("FormatterHooks" "examples/FormatterHooks.c")
target_link_libraries("FormatterHooks" "Zydis")
set_target_properties("FormatterHooks" PROPERTIES FOLDER "Examples/Formatter")
target_compile_definitions("FormatterHooks" PRIVATE "_CRT_SECURE_NO_WARNINGS")
add_executable("ZydisFuzzIn" "examples/ZydisFuzzIn.c")
target_link_libraries("ZydisFuzzIn" "Zydis")
set_target_properties("FormatterHooks" PROPERTIES FOLDER "Examples")
target_compile_definitions("ZydisFuzzIn" PRIVATE "_CRT_SECURE_NO_WARNINGS")
if (ZYDIS_WHOLE_PROGRAM_OPTIMIZATION AND MSVC)
set_target_properties("ZydisFuzzIn" PROPERTIES COMPILE_FLAGS "/GL")
set_target_properties("ZydisFuzzIn" PROPERTIES LINK_FLAGS_RELEASE "/LTCG")
endif ()
if (ZYDIS_FUZZ_AFL_FAST)
target_compile_definitions("ZydisFuzzIn" PRIVATE "ZYDIS_FUZZ_AFL_FAST")
endif ()
add_executable("ZydisPerfTest" "examples/ZydisPerfTest.c")
target_link_libraries("ZydisPerfTest" "Zydis")
set_target_properties("FormatterHooks" PROPERTIES FOLDER "Examples")
target_compile_definitions("ZydisPerfTest" PRIVATE "_CRT_SECURE_NO_WARNINGS")
if (ZYDIS_WHOLE_PROGRAM_OPTIMIZATION AND MSVC)
set_target_properties("ZydisPerfTest" PROPERTIES COMPILE_FLAGS "/GL")
set_target_properties("ZydisPerfTest" PROPERTIES LINK_FLAGS_RELEASE "/LTCG")
endif ()
if (${CMAKE_SYSTEM_NAME} STREQUAL "Linux")
target_compile_definitions("ZydisPerfTest" PRIVATE "_GNU_SOURCE")
find_package(Threads REQUIRED)
target_link_libraries("ZydisPerfTest" Threads::Threads)
endif ()
endif ()
endif ()
# =============================================================================================== #
# Tools #
# =============================================================================================== #
if (ZYDIS_BUILD_TOOLS)
if (ZYDIS_FEATURE_DECODER AND ZYDIS_FEATURE_FORMATTER)
add_executable("ZydisDisasm" "tools/ZydisDisasm.c")
target_link_libraries("ZydisDisasm" "Zydis")
set_target_properties ("ZydisDisasm" PROPERTIES FOLDER "Tools")
target_compile_definitions("ZydisDisasm" PRIVATE "_CRT_SECURE_NO_WARNINGS")
if (ZYDIS_WHOLE_PROGRAM_OPTIMIZATION AND MSVC)
set_target_properties("ZydisDisasm" PROPERTIES COMPILE_FLAGS "/GL")
set_target_properties("ZydisDisasm" PROPERTIES LINK_FLAGS_RELEASE "/LTCG")
endif ()
add_executable("ZydisInfo" "tools/ZydisInfo.c")
target_link_libraries("ZydisInfo" "Zydis")
set_target_properties ("ZydisInfo" PROPERTIES FOLDER "Tools")
target_compile_definitions("ZydisInfo" PRIVATE "_CRT_SECURE_NO_WARNINGS")
if (ZYDIS_WHOLE_PROGRAM_OPTIMIZATION AND MSVC)
set_target_properties("ZydisInfo" PROPERTIES COMPILE_FLAGS "/GL")
set_target_properties("ZydisInfo" PROPERTIES LINK_FLAGS_RELEASE "/LTCG")
endif ()
endif ()
endif ()