-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCMakeLists.txt
330 lines (273 loc) · 13.4 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
###################################################################################
# #
# CMakeLists.txt #
# #
###################################################################################
# #
# Scorpion Anti-malware is a free Open Source AI-powered Anti-malware #
# framework for Researchers. #
# #
# Copyright (c) 2024-present (see AUTHORS.md). #
# #
# This program is free software: you can redistribute it and/or modify #
# it under the terms of the GNU General Public License as published by #
# the Free Software Foundation, either version 3 of the License, or #
# (at your option) any later version. #
# #
# This program is distributed in the hope that it will be useful, #
# but WITHOUT ANY WARRANTY; without even the implied warranty of #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
# GNU General Public License for more details. #
# #
# You should have received a copy of the GNU General Public License #
# along with this program. If not, see <https://www.gnu.org/licenses/>. #
# #
###################################################################################
# TODO: We can use an older version of CMake.
cmake_minimum_required(VERSION 3.5)
# Set default build type
if(NOT CMAKE_BUILD_TYPE)
message(STATUS "Build type not set - defaulting to Debug")
set(
CMAKE_BUILD_TYPE "Debug"
CACHE
STRING
"Choose the type of build from: Debug Release MinSizeRel RelWithDebInfo."
FORCE)
endif()
set(CMAKE_DEBUG_POSTFIX "d")
set(CMAKE_RELEASE_POSTFIX "")
set(CMAKE_MINSIZEREL_POSTFIX "s")
set(CMAKE_RELWITHDEBINFO_POSTFIX "rd")
set(CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake" ${CMAKE_MODULE_PATH})
set(SAM_ORG_NAME "Scorpion Anti-malware")
set(SAM_PROJECT_NAME "Scorpion Anti-malware")
set(SAM_PROJECT_VERSION "1.0.0")
set(SAM_PROJECT_DESCRIPTION "Scorpion Anti-malware Engine.")
set(SAM_EXECUTABLE_NAME "scorpion-antimalware")
project(${SAM_EXECUTABLE_NAME} VERSION ${SAM_PROJECT_VERSION} LANGUAGES CXX)
if (WIN32)
string(APPEND CMAKE_CXX_FLAGS " /W4")
else()
string(APPEND CMAKE_CXX_FLAGS " -Wall -Wextra")
string(APPEND CMAKE_C_FLAGS " -Wall -Wextra")
# SAM engine require a Windows build environment
message(WARNING "Scorpion Anti-malware engine requires a Windows build environment.")
endif()
option(SAM_BUILD_PYTHON "Build Python binaries" OFF)
option(SAM_BUILD_TESTS "Builds Scorpion Anti-malware console and links the engine." OFF)
option(SAM_BUILD_CLI_TOOLS "Builds Scorpion Anti-malware CLI tools." OFF)
option(SAM_LINK_CONSOLE "Builds Scorpion Anti-malware console and links the engine." OFF)
option(SAM_STATIC_ANN_ONNX_MODELPATH "Static ANN onnx model path.")
option(SAM_STATIC_ANN_STANDARDIZE_PARAMS "Static ANN standardize parameters.")
option(SAM_STATIC_CNN_ONNX_MODELPATH "Static CNN onnx model path.")
option(SAM_STATIC_CNN_COLORMAP "Static CNN colormap.")
option(SAM_DYNAMIC_CNN_BIGRU_ONNX_MODELPATH "Dynamic CNN BiGRU onnx model path.")
# onnxruntime providers
option(SAM_onnxruntime_USE_CUDA "Build onnx with CUDA support." OFF)
option(SAM_onnxruntime_USE_TENSORRT "Build with TensorRT support." OFF)
option(SAM_ONNXRUNTIME_ROOTDIR "onnxruntime root dir.")
# Safety check to ensure that builds are not performed in the source directory.
if(${CMAKE_CURRENT_SOURCE_DIR} STREQUAL ${CMAKE_CURRENT_BINARY_DIR})
message(
FATAL_ERROR
"In-source builds not allowed. Please make a new directory (called '.build' directory) and run CMake from there.\n"
)
endif()
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
# Disable compiler-specific extensions for cross-platform compatibility.
set(CMAKE_CXX_EXTENSIONS OFF)
set(SAM_ROOTDIR ${PROJECT_SOURCE_DIR})
if(MSVC)
string(APPEND CMAKE_CXX_FLAGS " /EHsc /wd26812")
string(APPEND CMAKE_C_FLAGS " /EHsc /wd26812")
endif()
# Disable C++ exceptions.
if(MSVC)
string(REGEX REPLACE "/EHsc" "/EHs-c-" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
add_definitions(-D_HAS_EXCEPTIONS=0)
else()
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-exceptions -fno-unwind-tables -fno-asynchronous-unwind-tables")
endif()
# Check https://stackoverflow.com/q/33062728/14629018 for more information.
if(MSVC)
set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS TRUE)
endif()
#################################################
message(STATUS "Building Scorpion Anti-malware engine as a library...")
set(SAM_ENGINE_ROOTDIR "${SAM_ROOTDIR}/sam-engine")
add_subdirectory(${SAM_ENGINE_ROOTDIR})
# If we are not linking the console, then we don't need the UI thread.
if (NOT SAM_LINK_CONSOLE)
target_compile_definitions(${SAM_ENGINE} PRIVATE
-USAM_ENGINE_ENABLE_UI_THREAD
)
endif()
#################################################
#
# Scorpion Anti-malware CLI Tools
#
#################################################
if (SAM_BUILD_CLI_TOOLS)
message(STATUS "Building Scorpion Anti-malware CLI tools...")
set(SAM_CLI_TOOLS_ROOTDIR "${SAM_ROOTDIR}/sam-cli-tools")
add_subdirectory(${SAM_CLI_TOOLS_ROOTDIR})
endif()
#################################################
#
# Scorpion Anti-malware Tests
#
#################################################
if (SAM_BUILD_TESTS)
message(STATUS "Building Scorpion Anti-malware tests...")
set(SAM_TESTS_ROOTDIR "${SAM_ROOTDIR}/tests")
add_subdirectory(${SAM_TESTS_ROOTDIR})
endif()
#################################################
#
# Scorpion Anti-malware Console
#
#################################################
if (SAM_LINK_CONSOLE)
# Enable the UI thread for the engine.
target_compile_definitions(${SAM_ENGINE} PRIVATE
SAM_ENGINE_ENABLE_UI_THREAD
)
message(STATUS "Building Scorpion Anti-malware console...")
set(SAM_CONSOLE_ROOTDIR "${SAM_ROOTDIR}/sam-console")
add_subdirectory(${SAM_CONSOLE_ROOTDIR})
# Find Qt
# TODO: Remove this from here.
find_package(Qt${SAM_CONSOLE_QT_VERSION} CONFIG REQUIRED COMPONENTS Widgets SvgWidgets)
add_executable(${SAM_EXECUTABLE_NAME} WIN32
${SAM_CONSOLE_MAIN_ENTRY}
${SAM_CONSOLE_CPP_FILES}
)
target_compile_definitions(${SAM_EXECUTABLE_NAME} PRIVATE
SAM_NO_EXCEPTIONS
SAM_CONSOLE_ROOTDIR="${SAM_CONSOLE_ROOTDIR}"
)
# Enable AUTOMOC for the SAM_EXECUTABLE_NAME target only.
# TODO: Remove this from here.
set_target_properties(${SAM_EXECUTABLE_NAME} PROPERTIES
AUTOMOC ON
)
message(STATUS "Linking Scorpion Anti-malware engine to the console...")
target_link_libraries(${SAM_EXECUTABLE_NAME} PRIVATE
Qt${SAM_CONSOLE_QT_VERSION}::Widgets
Qt${SAM_CONSOLE_QT_VERSION}::SvgWidgets
${SAM_ENGINE}
)
target_include_directories(${SAM_EXECUTABLE_NAME} PRIVATE
${${SAM_CONSOLE_EXECUTABLE_NAME}_INCLUDE_DIRECTORIES}
)
target_compile_definitions(${SAM_EXECUTABLE_NAME} PRIVATE
SAM_ORG_NAME="${SAM_ORG_NAME}"
SAM_CONSOLE_PROJECT_NAME="${SAM_CONSOLE_PROJECT_NAME}"
SAM_CONSOLE_PROJECT_VERSION="${SAM_CONSOLE_PROJECT_VERSION}"
)
# Specify the directory containing the shared libraries.
set(SAM_PE_ENGINE_BUILD_ROOTDIR "${PROJECT_BINARY_DIR}/sam-engine/PE-engine")
set(SAM_BYTE_PIXEL_ENGINE_BUILD_ROOTDIR "${PROJECT_BINARY_DIR}/sam-engine/byte-pixel-engine")
set(SAM_PE_PATHLS_GENERATOR_BUILD_ROOTDIR "${PROJECT_BINARY_DIR}/sam-engine/PE-pathls-generator")
set(SAM_MODELS_BUILD_ROOTDIR "${PROJECT_BINARY_DIR}/sam-engine/models")
set(SAM_SCAN_AREAS_PROCESSOR_BUILD_ROOTDIR "${PROJECT_BINARY_DIR}/sam-engine/scan-areas-processor")
set(SAM_ENGINE_BUILD_ROOTDIR "${PROJECT_BINARY_DIR}/sam-engine")
set(SAM_DYNAMICS_ENGINE_BUILD_ROOTDIR "${PROJECT_BINARY_DIR}/sam-engine/dynamics-engine")
set(SAM_SCAN_ANALYZER_BUILD_ROOTDIR "${PROJECT_BINARY_DIR}/sam-engine/scan-analyzer")
set(SAM_FIM_BUILD_ROOTDIR "${PROJECT_BINARY_DIR}/sam-engine/FIM")
# TODO: Why copy_if_different isn't working?
# Copy the shared libraries to the executable directory.
file(GLOB SAM_ENGINE_DLLS
"${SAM_PE_ENGINE_BUILD_ROOTDIR}/Debug/${SAM_PE_ENGINE}.dll"
"${SAM_BYTE_PIXEL_ENGINE_BUILD_ROOTDIR}/Debug/${SAM_BYTE_PIXEL_ENGINE}.dll"
"${SAM_PE_PATHLS_GENERATOR_BUILD_ROOTDIR}/Debug/${SAM_PE_PATHLS_GENERATOR}.dll"
"${SAM_MODELS_BUILD_ROOTDIR}/Debug/${SAM_MODELS}.dll"
"${SAM_ENGINE_BUILD_ROOTDIR}/Debug/${SAM_ENGINE}.dll"
"${SAM_SCAN_AREAS_PROCESSOR_BUILD_ROOTDIR}/Debug/${SAM_SCAN_AREAS_PROCESSOR}.dll"
"${SAM_DYNAMICS_ENGINE_BUILD_ROOTDIR}/Debug/${SAM_DYNAMICS_ENGINE}.dll"
"${SAM_SCAN_ANALYZER_BUILD_ROOTDIR}/Debug/${SAM_SCAN_ANALYZER}.dll"
"${SAM_FIM_BUILD_ROOTDIR}/Debug/${SAM_FIM}.dll"
)
# Safety check: Ensure that all the files exist.
foreach(file ${SAM_ENGINE_DLLS})
if(NOT EXISTS "${file}")
message(FATAL_ERROR "File '${file}' does not exist!")
endif()
endforeach()
foreach(SAM_ENGINE_DLL ${SAM_ENGINE_DLLS})
add_custom_command(TARGET ${SAM_EXECUTABLE_NAME} POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy ${SAM_ENGINE_DLL} $<TARGET_FILE_DIR:${SAM_EXECUTABLE_NAME}>
COMMENT "Copying ${SAM_ENGINE_DLL} to the executable directory."
)
endforeach()
# Specify the directory containing the shared libraries.
set(SAM_CONSOLE_BUILD_ROOTDIR "${PROJECT_BINARY_DIR}/sam-console")
# TODO: Why copy_if_different isn't working?
# Copy the shared libraries to the executable directory.
file(GLOB SAM_CONSOLE_DLLS
"${SAM_CONSOLE_BUILD_ROOTDIR}/Debug/${SAM_CONSOLE}.dll"
)
# Safety check: Ensure that all the files exist.
foreach(file ${SAM_CONSOLE_DLLS})
if(NOT EXISTS "${file}")
message(FATAL_ERROR "File '${file}' does not exist!")
endif()
endforeach()
# Copy the shared libraries to the executable directory.
foreach(SAM_CONSOLE_DLL ${SAM_CONSOLE_DLLS})
add_custom_command(TARGET ${SAM_EXECUTABLE_NAME} POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy ${SAM_CONSOLE_DLL} $<TARGET_FILE_DIR:${SAM_EXECUTABLE_NAME}>
COMMENT "Copying ${SAM_CONSOLE_DLL} to the executable directory."
)
endforeach()
# Copy the onnx dlls to the executable directory.
if (MSVC)
file(GLOB SAM_ORT_DLLS
"${SAM_ONNXRUNTIME_ROOTDIR}/lib/onnxruntime.dll"
)
# Safety check: Ensure that all the files exist.
foreach(file ${SAM_ORT_DLLS})
if(NOT EXISTS "${file}")
message(FATAL_ERROR "File '${file}' does not exist!")
endif()
endforeach()
foreach(SAM_ORT_DLL ${SAM_ORT_DLLS})
add_custom_command(TARGET ${SAM_EXECUTABLE_NAME} POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy ${SAM_ORT_DLL} $<TARGET_FILE_DIR:${SAM_EXECUTABLE_NAME}>
COMMENT "Copying ${SAM_ORT_DLL} to the executable directory."
)
endforeach()
# Install the onnx dlls to the executable directory.
install(FILES ${SAM_ORT_DLLS} DESTINATION .)
endif()
# Install the library and the header files.
# This is for CPack to be able to package the library.
set(SAM_LICENSE_FILE "${SAM_ROOTDIR}/LICENSE")
install(TARGETS ${SAM_EXECUTABLE_NAME} RUNTIME DESTINATION .)
install(FILES "${SAM_LICENSE_FILE}" DESTINATION .)
# Include the icon file in the executable (Windows-specific)
if(WIN32)
set_target_properties(${SAM_EXECUTABLE_NAME} PROPERTIES WIN32_EXECUTABLE TRUE)
set(favicon_rc "${CMAKE_BINARY_DIR}/favicon.rc")
file(GENERATE OUTPUT ${favicon_rc} CONTENT "IDI_FAVICON ICON DISCARDABLE \"${SAM_ROOTDIR}/resources/platforms/windows/favicon.ico\"")
target_sources(${SAM_EXECUTABLE_NAME} PRIVATE ${favicon_rc})
endif()
#################################################
#
# Packaging
#
#################################################
include(InstallRequiredSystemLibraries)
set(CPACK_PACKAGE_NAME ${SAM_PROJECT_NAME})
# set(CPACK_PACKAGE_VERSION ${SAM_PROJECT_VERSION})
set(CPACK_PACKAGE_VENDOR ${SAM_ORG_NAME})
set(CPACK_PACKAGE_DESCRIPTION_SUMMARY ${SAM_PROJECT_DESCRIPTION})
set(CPACK_RESOURCE_FILE_LICENSE "${SAM_LICENSE_FILE}")
set(CPACK_PACKAGE_VERSION_MAJOR "${${SAM_EXECUTABLE_NAME}_VERSION_MAJOR}")
set(CPACK_PACKAGE_VERSION_MINOR "${${SAM_EXECUTABLE_NAME}_VERSION_MINOR}")
# Specify the icon file for CPack (NSIS specific)
set(CPACK_NSIS_MUI_ICON "${SAM_ROOTDIR}/resources/platforms/windows/favicon.ico")
include(CPack)
endif()