-
Notifications
You must be signed in to change notification settings - Fork 2
/
CMakeLists.txt
590 lines (530 loc) · 23.3 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
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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
#
# UPX "CMake" build file; see https://cmake.org/
# Copyright (C) Markus Franz Xaver Johannes Oberhumer
#
# Build requirements:
# A C++ compiler that fully implements C++17: clang-5, gcc-8 or msvc-2019-16.11
# (older or other compilers may work but are unsupported, use at your own risk)
# Sections of this CMakeLists.txt:
# - options
# - init
# - common compilation flags
# - targets
# - target compilation flags
# - test
# - install
# - print summary
# CMake version check; using a somewhat current CMake version is highly recommended
file(REMOVE "${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/.upx_cmake_config_done.txt")
if(DEFINED UPX_CONFIG_CMAKE_MINIMUM_REQUIRED_VERSION)
cmake_minimum_required(VERSION "${UPX_CONFIG_CMAKE_MINIMUM_REQUIRED_VERSION}" FATAL_ERROR)
else()
cmake_minimum_required(VERSION "3.8" FATAL_ERROR) # CMake >= 3.8 is needed for CXX_STANDARD 17
endif()
# support functions and some utility
include("${CMAKE_CURRENT_SOURCE_DIR}/misc/cmake/functions.cmake")
upx_print_var(CMAKE_VERSION UPX_CONFIG_CMAKE_MINIMUM_REQUIRED_VERSION CMAKE_GENERATOR)
#***********************************************************************
# options
#***********************************************************************
upx_cmake_include_hook(1_options)
# compilation config options
if(NOT USE_STRICT_DEFAULTS)
# permissive config defaults when building from source code tarball
option(UPX_CONFIG_DISABLE_GITREV "Do not compile with Git version info." ON)
option(UPX_CONFIG_DISABLE_SANITIZE "Do not compile with sanitize options." ON)
option(UPX_CONFIG_DISABLE_WERROR "Do not compile with -Werror option." ON)
option(UPX_CONFIG_DISABLE_WSTRICT "Do not compile with strict compiler warnings." ON)
else()
# strict config defaults for Git developer builds
message(STATUS "===== UPX NOTE: strict developer config defaults enabled =====")
option(UPX_CONFIG_DISABLE_GITREV "Do not compile with Git version info." OFF)
option(UPX_CONFIG_DISABLE_SANITIZE "Do not compile with sanitize options." OFF)
option(UPX_CONFIG_DISABLE_WERROR "Do not compile with -Werror option." OFF)
option(UPX_CONFIG_DISABLE_WSTRICT "Do not compile with strict compiler warnings." OFF)
endif()
# test config options (see below)
# IMPORTANT NOTE: self-pack test can only work if the host executable format is supported by UPX!
option(UPX_CONFIG_DISABLE_SELF_PACK_TEST "Do not test packing UPX with itself" OFF)
option(UPX_CONFIG_DISABLE_EXHAUSTIVE_TESTS "Do not run exhaustive tests" OFF)
#***********************************************************************
# init
#***********************************************************************
set(UPX_VERSION_STRING "4.3.0") # this should match src/version.h
upx_cmake_include_hook(2_init)
# Disallow in-source build. Note that you will still have to manually
# clean up a few files if you accidentally try an in-source build.
upx_disallow_in_source_build()
# global settings
if(${CMAKE_VERSION} VERSION_GREATER "3.14.99" AND NOT DEFINED CMAKE_MSVC_RUNTIME_LIBRARY)
set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded")
cmake_policy(SET CMP0091 NEW)
endif()
# global CMake settings that default to ON
upx_cache_bool_vars(ON
CMAKE_C_STANDARD_REQUIRED CMAKE_CXX_STANDARD_REQUIRED
CMAKE_EXPORT_COMPILE_COMMANDS CMAKE_REQUIRED_QUIET
)
# internal UPX settings; useful for CI jobs
upx_cache_bool_vars(OFF
UPX_CONFIG_CMAKE_DISABLE_TEST UPX_CONFIG_CMAKE_DISABLE_INSTALL
UPX_CONFIG_CMAKE_DISABLE_PRINT_INFO UPX_CONFIG_CMAKE_DISABLE_PLATFORM_CHECK
UPX_CONFIG_DISABLE_C_STANDARD UPX_CONFIG_DISABLE_CXX_STANDARD
UPX_CONFIG_DISABLE_RUN_UNPACKED_TEST UPX_CONFIG_DISABLE_RUN_PACKED_TEST
UPX_CONFIG_DISABLE_SHARED_LIBS UPX_CONFIG_REQUIRE_THREADS
)
upx_cache_bool_vars(ON UPX_CONFIG_EXPECT_THREADS)
upx_print_env_var(CC CXX)
# determine Git revision
set(GITREV_SHORT "")
set(GITREV_PLUS "")
set(GIT_DESCRIBE "")
if(IS_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/.git" AND NOT UPX_CONFIG_DISABLE_GITREV)
find_package(Git)
if(Git_FOUND)
execute_process(
COMMAND "${GIT_EXECUTABLE}" rev-parse --short=12 HEAD
RESULT_VARIABLE result ERROR_QUIET
OUTPUT_VARIABLE GITREV_SHORT OUTPUT_STRIP_TRAILING_WHITESPACE
)
string(LENGTH "${GITREV_SHORT}" l)
if(${result} EQUAL 0 AND ${l} EQUAL 12)
execute_process(RESULT_VARIABLE result COMMAND "${GIT_EXECUTABLE}" diff --quiet)
if(NOT ${result} EQUAL 0)
set(GITREV_PLUS "+")
endif()
else()
set(GITREV_SHORT "")
endif()
execute_process(
COMMAND "${GIT_EXECUTABLE}" describe --match "v*.*.*" --tags --dirty
RESULT_VARIABLE result ERROR_QUIET
OUTPUT_VARIABLE GIT_DESCRIBE OUTPUT_STRIP_TRAILING_WHITESPACE
)
if(GIT_DESCRIBE MATCHES "^v?([0-9]+\\.[0-9]+\\.[0-9]+)-([0-9]+)-g(.+)$")
set(GIT_DESCRIBE "${CMAKE_MATCH_1}-devel.${CMAKE_MATCH_2}+git-${CMAKE_MATCH_3}")
endif()
endif()
endif()
if(GITREV_SHORT)
message(STATUS "UPX_VERSION_GITREV = \"${GITREV_SHORT}${GITREV_PLUS}\"")
if(GIT_DESCRIBE)
message(STATUS "UPX_VERSION_GIT_DESCRIBE = \"${GIT_DESCRIBE}\"")
endif()
elseif(UPX_CONFIG_DISABLE_GITREV)
message(STATUS "UPX_VERSION_GITREV: disabled")
else()
message(STATUS "UPX_VERSION_GITREV: not set")
endif()
# CMake init
upx_set_default_build_type(Release) # default is CMAKE_BUILD_TYPE=Release
project(upx VERSION "${UPX_VERSION_STRING}" LANGUAGES C CXX)
upx_apply_build_type()
upx_set_global_vars()
upx_check_working_build_rpath(UPX_CONFIG_HAVE_WORKING_BUILD_RPATH)
if(DEFINED UPX_CONFIG_CMAKE_EXECUTABLE_SUFFIX)
set(CMAKE_EXECUTABLE_SUFFIX "${UPX_CONFIG_CMAKE_EXECUTABLE_SUFFIX}")
endif()
if(NOT UPX_CONFIG_CMAKE_DISABLE_INSTALL AND CMAKE_INSTALL_PREFIX)
include(GNUInstallDirs)
endif()
add_subdirectory(vendor/acx)
if(${CMAKE_VERSION} VERSION_GREATER "3.4.99") # need cmake-3.5
add_subdirectory(vendor/cmt)
endif()
if(NOT DEFINED LZO_CONFIG_DISABLE_SHARED_LIBS AND UPX_CONFIG_DISABLE_SHARED_LIBS)
set(LZO_CONFIG_DISABLE_SHARED_LIBS ON)
endif()
if(NOT DEFINED LZO_CONFIG_HAVE_WORKING_BUILD_RPATH AND UPX_CONFIG_HAVE_WORKING_BUILD_RPATH)
set(LZO_CONFIG_HAVE_WORKING_BUILD_RPATH ON)
if(1)
# just for testing; TODO some time later: disable this
set(ENABLE_STATIC OFF CACHE INTERNAL "" FORCE)
set(ENABLE_SHARED ON CACHE INTERNAL "" FORCE)
endif()
endif()
if(${CMAKE_VERSION} VERSION_LESS "3.5") # need cmake-3.5
elseif(CMAKE_C_COMPILER_ARG1 MATCHES "sanitize=memory") # LZO is not MSAN safe
else()
add_subdirectory(vendor/lzo)
endif()
#***********************************************************************
# common compilation flags
#***********************************************************************
include(CheckCCompilerFlag)
include(CheckFunctionExists)
include(CheckIncludeFile)
include(CheckStructHasMember)
include(CheckSymbolExists)
include(CheckTypeSize)
upx_cmake_include_hook(3_common_compilation_flags)
# assert sane type sizes
check_type_size("size_t" C_SIZEOF_SIZE_T LANGUAGE C)
check_type_size("size_t" CXX_SIZEOF_SIZE_T LANGUAGE CXX)
if(NOT "${C_SIZEOF_SIZE_T}" MATCHES "^(4|8|16)$")
message(FATAL_ERROR "ERROR: unexpected C_SIZEOF_SIZE_T '${C_SIZEOF_SIZE_T}'")
endif()
if(NOT ",${C_SIZEOF_SIZE_T}," STREQUAL ",${CXX_SIZEOF_SIZE_T},")
message(FATAL_ERROR "FATAL ERROR: '${C_SIZEOF_SIZE_T}' '${CXX_SIZEOF_SIZE_T}' mismatch")
endif()
if(NOT DEFINED HAVE_UNISTD_H)
check_include_file("unistd.h" HAVE_UNISTD_H)
endif()
if(NOT DEFINED HAVE_UTIMENSAT)
# proper checking for utimensat() is somewhat messy
check_function_exists(utimensat HAVE_UTIMENSAT_FUNCTION__)
if(HAVE_UTIMENSAT_FUNCTION__)
check_symbol_exists(utimensat "sys/types.h;fcntl.h;sys/stat.h" HAVE_UTIMENSAT_SYMBOL__)
if(HAVE_UTIMENSAT_SYMBOL__)
CHECK_STRUCT_HAS_MEMBER("struct stat" "st_mtim.tv_nsec" "sys/types.h;fcntl.h;sys/stat.h" HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC) # POSIX.1-2008
if(NOT HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC)
CHECK_STRUCT_HAS_MEMBER("struct stat" "st_mtimespec.tv_nsec" "sys/types.h;fcntl.h;sys/stat.h" HAVE_STRUCT_STAT_ST_MTIMESPEC_TV_NSEC) # macOS
endif()
if(HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC OR HAVE_STRUCT_STAT_ST_MTIMESPEC_TV_NSEC)
set(HAVE_UTIMENSAT 1)
endif()
endif()
endif()
endif()
if(UPX_CONFIG_DISABLE_WSTRICT)
# enable all basic warnings
set(warn_Wall -Wall)
set(warn_WN -W3)
else()
# enable all basic warnings, and enable lots of strict warnings
set(warn_Wall -Wall -Wextra -Wcast-align -Wcast-qual -Wmissing-declarations -Wpointer-arith -Wshadow -Wvla -Wwrite-strings)
set(warn_WN -W4)
endif()
if(UPX_CONFIG_DISABLE_WERROR)
# warnings are just warnings
set(warn_Werror "")
set(warn_WX "")
else()
# warnings are fatal errors; annoy developers to keep the source code warning-free
set(warn_Werror -Werror)
set(warn_WX -WX)
endif()
if(MSVC_FRONTEND)
# disable warning C5105 which may get triggered by some older versions of <windows.h>
set(warn_WX -wd5105 ${warn_WX})
endif()
if(NOT CMAKE_C_COMPILER_ID MATCHES "^MSVC")
# use -O2 instead of -O3 to reduce code size
string(REGEX REPLACE "(^| )-O3( |$$)" "\\1-O2\\2" a "${CMAKE_C_FLAGS_RELEASE}")
string(REGEX REPLACE "(^| )-O3( |$$)" "\\1-O2\\2" b "${CMAKE_CXX_FLAGS_RELEASE}")
set(CMAKE_C_FLAGS_RELEASE "${a}" CACHE STRING "Flags used by the C compiler during RELEASE builds." FORCE)
set(CMAKE_CXX_FLAGS_RELEASE "${b}" CACHE STRING "Flags used by the CXX compiler during RELEASE builds." FORCE)
endif()
if(MSVC_FRONTEND OR WIN32 OR MINGW OR CYGWIN)
# disable silly warnings about using "deprecated" POSIX functions like fopen()
add_definitions(-D_CRT_NONSTDC_NO_DEPRECATE)
add_definitions(-D_CRT_NONSTDC_NO_WARNINGS)
add_definitions(-D_CRT_SECURE_NO_DEPRECATE)
add_definitions(-D_CRT_SECURE_NO_WARNINGS)
add_definitions(-D_SCL_SECURE_NO_DEPRECATE)
add_definitions(-D_SCL_SECURE_NO_WARNINGS)
add_definitions(-DWIN32_LEAN_AND_MEAN)
add_definitions(-D__USE_MINGW_ANSI_STDIO)
endif()
if(MSVC_FRONTEND)
# use -funsigned-char; set __cplusplus according to selected C++ standard
add_definitions(-J -Zc:__cplusplus)
if(CMAKE_C_COMPILER_ID MATCHES "^MSVC")
upx_add_definitions(-Zc:preprocessor) # use new preprocessor
endif()
endif()
if(NOT CMAKE_C_COMPILER_ID MATCHES "^MSVC")
# protect against security threats caused by misguided compiler "optimizations"
upx_add_definitions(-fno-delete-null-pointer-checks -fno-lifetime-dse)
upx_add_definitions(-fno-strict-aliasing -fno-strict-overflow -funsigned-char)
# disable overambitious auto-vectorization until this actually gains something
upx_add_definitions(-fno-tree-vectorize)
# disable annoying clang warnings which get added by the macOS Xcode cmake generator
if(CMAKE_GENERATOR MATCHES "Xcode")
upx_add_definitions(-Wno-shorten-64-to-32)
endif()
endif()
# examine compiler configuration
if(NOT UPX_CONFIG_CMAKE_DISABLE_PRINT_INFO)
upx_print_common_symbols()
upx_print_mingw_symbols()
endif()
#***********************************************************************
# targets
#***********************************************************************
# internal settings; these may change in a future versions
set(UPX_CONFIG_DISABLE_THREADS ON) # multithreading is currently not used; maybe in UPX version 5
set(UPX_CONFIG_DISABLE_BZIP2 OFF) # bzip2 is currently not used; we might need it to decompress linux kernels
set(UPX_CONFIG_DISABLE_ZSTD OFF) # zstd is currently not used; maybe in UPX version 5
upx_cmake_include_hook(4_targets)
if(NOT UPX_CONFIG_DISABLE_THREADS)
find_package(Threads)
endif()
# make sure that threads are indeed fully supported in C++
if(Threads_FOUND)
foreach(f std_lock_guard.cpp types_abi.cpp)
set(CMAKE_TRY_COMPILE_TARGET_TYPE "EXECUTABLE")
if(NOT UPX_CONFIG_DISABLE_CXX_STANDARD)
try_compile(result "${CMAKE_CURRENT_BINARY_DIR}"
"${CMAKE_CURRENT_SOURCE_DIR}/misc/cmake/try_compile/${f}"
OUTPUT_VARIABLE output CXX_STANDARD 17)
else()
try_compile(result "${CMAKE_CURRENT_BINARY_DIR}"
"${CMAKE_CURRENT_SOURCE_DIR}/misc/cmake/try_compile/${f}"
OUTPUT_VARIABLE output)
endif()
if(NOT result)
# failed; under MinGW be sure to use the posix-threads and NOT the win32-threads version
#message(STATUS "Threads FAILED ${f}: ${output}") # debug output from try_compile
set(Threads_FOUND OFF)
break()
endif()
endforeach()
endif()
if(NOT UPX_CONFIG_DISABLE_BZIP2)
upx_add_glob_files(bzip2_SOURCES "vendor/bzip2/*.c")
add_library(upx_vendor_bzip2 STATIC ${bzip2_SOURCES})
if(NOT UPX_CONFIG_DISABLE_C_STANDARD)
set_property(TARGET upx_vendor_bzip2 PROPERTY C_STANDARD 11)
endif()
endif() # UPX_CONFIG_DISABLE_BZIP2
upx_add_glob_files(ucl_SOURCES "vendor/ucl/src/*.c")
add_library(upx_vendor_ucl STATIC ${ucl_SOURCES})
if(NOT UPX_CONFIG_DISABLE_C_STANDARD)
set_property(TARGET upx_vendor_ucl PROPERTY C_STANDARD 11)
endif()
upx_add_glob_files(zlib_SOURCES "vendor/zlib/*.c")
add_library(upx_vendor_zlib STATIC ${zlib_SOURCES})
if(NOT UPX_CONFIG_DISABLE_C_STANDARD)
set_property(TARGET upx_vendor_zlib PROPERTY C_STANDARD 11)
endif()
if(NOT UPX_CONFIG_DISABLE_ZSTD)
upx_add_glob_files(zstd_SOURCES "vendor/zstd/lib/*/*.c")
add_library(upx_vendor_zstd STATIC ${zstd_SOURCES})
if(NOT UPX_CONFIG_DISABLE_C_STANDARD)
set_property(TARGET upx_vendor_zstd PROPERTY C_STANDARD 11)
endif()
endif() # UPX_CONFIG_DISABLE_ZSTD
upx_add_glob_files(upx_SOURCES "src/*.cpp" "src/[cfu]*/*.c*")
add_executable(upx ${upx_SOURCES})
if(NOT UPX_CONFIG_DISABLE_CXX_STANDARD)
set_property(TARGET upx PROPERTY CXX_STANDARD 17)
endif()
target_link_libraries(upx upx_vendor_ucl upx_vendor_zlib)
if(NOT UPX_CONFIG_DISABLE_BZIP2)
target_link_libraries(upx upx_vendor_bzip2)
endif()
if(NOT UPX_CONFIG_DISABLE_ZSTD)
target_link_libraries(upx upx_vendor_zstd)
endif()
if(Threads_FOUND)
target_link_libraries(upx Threads::Threads)
endif()
#***********************************************************************
# target compilation flags
#***********************************************************************
upx_cmake_include_hook(5_target_compilation_flags)
if(NOT UPX_CONFIG_DISABLE_BZIP2)
set(t upx_vendor_bzip2)
upx_compile_target_debug_with_O2(${t})
upx_sanitize_target(${t})
target_compile_definitions(${t} PRIVATE BZ_NO_STDIO=1)
if(MSVC_FRONTEND)
target_compile_options(${t} PRIVATE ${warn_WN} -wd4127 -wd4244 -wd4267 ${warn_WX})
elseif(GNU_FRONTEND)
target_compile_options(${t} PRIVATE ${warn_Wall} ${warn_Werror})
endif()
upx_add_target_extra_compile_options(${t} UPX_CONFIG_EXTRA_COMPILE_OPTIONS_BZIP2)
endif() # UPX_CONFIG_DISABLE_BZIP2
set(t upx_vendor_ucl)
target_include_directories(${t} PRIVATE vendor/ucl/include vendor/ucl)
upx_compile_target_debug_with_O2(${t})
upx_sanitize_target(${t})
if(MSVC_FRONTEND)
target_compile_options(${t} PRIVATE ${warn_WN} ${warn_WX})
elseif(GNU_FRONTEND)
target_compile_options(${t} PRIVATE ${warn_Wall} ${warn_Werror})
endif()
upx_add_target_extra_compile_options(${t} UPX_CONFIG_EXTRA_COMPILE_OPTIONS_UCL)
set(t upx_vendor_zlib)
upx_compile_target_debug_with_O2(${t})
upx_sanitize_target(${t})
target_compile_definitions(${t} PRIVATE HAVE_VSNPRINTF=1)
if(HAVE_UNISTD_H)
target_compile_definitions(${t} PRIVATE HAVE_UNISTD_H=1)
endif()
if(MSVC_FRONTEND)
target_compile_options(${t} PRIVATE -W3 ${warn_WX})
elseif(GNU_FRONTEND)
target_compile_options(${t} PRIVATE ${warn_Wall} -Wno-cast-align -Wno-cast-qual ${warn_Werror})
endif()
upx_add_target_extra_compile_options(${t} UPX_CONFIG_EXTRA_COMPILE_OPTIONS_ZLIB)
if(NOT UPX_CONFIG_DISABLE_ZSTD)
set(t upx_vendor_zstd)
upx_compile_target_debug_with_O2(${t})
upx_sanitize_target(${t})
target_compile_definitions(${t} PRIVATE DYNAMIC_BMI2=0 ZSTD_DISABLE_ASM=1)
if(MSVC_FRONTEND)
target_compile_options(${t} PRIVATE ${warn_WN} ${warn_WX})
elseif(GNU_FRONTEND)
target_compile_options(${t} PRIVATE ${warn_Wall} ${warn_Werror})
endif()
upx_add_target_extra_compile_options(${t} UPX_CONFIG_EXTRA_COMPILE_OPTIONS_ZSTD)
endif() # UPX_CONFIG_DISABLE_ZSTD
set(t upx)
target_include_directories(${t} PRIVATE vendor)
target_compile_definitions(${t} PRIVATE $<$<CONFIG:Debug>:DEBUG=1>)
if(GITREV_SHORT)
target_compile_definitions(${t} PRIVATE UPX_VERSION_GITREV="${GITREV_SHORT}${GITREV_PLUS}")
if(GIT_DESCRIBE)
target_compile_definitions(${t} PRIVATE UPX_VERSION_GIT_DESCRIBE="${GIT_DESCRIBE}")
endif()
endif()
if(Threads_FOUND)
target_compile_definitions(${t} PRIVATE WITH_THREADS=1)
endif()
if(NOT UPX_CONFIG_DISABLE_WSTRICT)
target_compile_definitions(${t} PRIVATE UPX_CONFIG_DISABLE_WSTRICT=0)
endif()
if(NOT UPX_CONFIG_DISABLE_WERROR)
target_compile_definitions(${t} PRIVATE UPX_CONFIG_DISABLE_WERROR=0)
endif()
if(NOT UPX_CONFIG_DISABLE_BZIP2)
target_compile_definitions(${t} PRIVATE WITH_BZIP2=1)
endif()
if(NOT UPX_CONFIG_DISABLE_ZSTD)
target_compile_definitions(${t} PRIVATE WITH_ZSTD=1)
endif()
if(HAVE_UTIMENSAT)
target_compile_definitions(${t} PRIVATE USE_UTIMENSAT=1)
if(HAVE_STRUCT_STAT_ST_MTIMESPEC_TV_NSEC)
target_compile_definitions(${t} PRIVATE HAVE_STRUCT_STAT_ST_MTIMESPEC_TV_NSEC=1)
endif()
endif()
# improve speed of the Debug versions
upx_compile_source_debug_with_O2(src/compress/compress_lzma.cpp)
upx_compile_source_debug_with_O2(src/filter/filter_impl.cpp)
#upx_compile_target_debug_with_O2(${t})
upx_sanitize_target(${t})
if(MSVC_FRONTEND)
target_compile_options(${t} PRIVATE -EHsc ${warn_WN} ${warn_WX})
elseif(GNU_FRONTEND)
target_compile_options(${t} PRIVATE ${warn_Wall} ${warn_Werror})
endif()
upx_add_target_extra_compile_options(${t} UPX_CONFIG_EXTRA_COMPILE_OPTIONS_UPX)
add_executable(acx_naked_c vendor/acx/test/naked_c.c)
add_executable(acx_naked_x vendor/acx/test/naked_x.cpp)
add_executable(acx_simple_c vendor/acx/test/simple_c.c)
add_executable(acx_simple_x vendor/acx/test/simple_x.cpp)
if(NOT UPX_CONFIG_DISABLE_CXX_STANDARD)
set_property(TARGET acx_naked_x PROPERTY CXX_STANDARD 17)
set_property(TARGET acx_simple_x PROPERTY CXX_STANDARD 17)
endif()
foreach(t IN ITEMS acx_naked_c acx_naked_x acx_simple_c acx_simple_x)
if(MSVC_FRONTEND)
target_compile_options(${t} PRIVATE ${warn_WN} ${warn_WX})
elseif(GNU_FRONTEND)
target_compile_options(${t} PRIVATE ${warn_Wall} -Wno-missing-declarations ${warn_Werror})
if(CMAKE_C_COMPILER_ID MATCHES "^Clang$" AND CMAKE_C_COMPILER_VERSION VERSION_LESS "3.7")
elseif(CMAKE_C_COMPILER_ID MATCHES "^GNU" AND CMAKE_C_COMPILER_VERSION VERSION_LESS "4.5")
else()
# -save-temps problem in parallel builds because
# - add_subdirectory(acx) also uses -save-temps
# - there is a basename clash with add_subdirectory(acx) files, e.g. simple_c
# - save-temps files are not put into the object-destination subdirectory
# => use -save-temps=obj
target_compile_options(${t} PRIVATE -save-temps=obj)
endif()
endif()
upx_sanitize_target(${t})
endforeach()
#***********************************************************************
# test
# ctest
# make test
# ninja test
#***********************************************************************
upx_cmake_include_hook(6_test)
if(NOT UPX_CONFIG_CMAKE_DISABLE_TEST)
add_custom_command(TARGET upx POST_BUILD COMMAND "${CMAKE_COMMAND}" -E make_directory "${CMAKE_CURRENT_BINARY_DIR}/XTesting/$<CONFIG>")
include(CTest)
if(NOT CMAKE_CROSSCOMPILING OR CMAKE_CROSSCOMPILING_EMULATOR)
upx_add_test(upx-version upx --version)
upx_add_test(upx-version-short upx --version-short)
upx_add_test(upx-license upx --license)
upx_add_test(upx-help-1 upx --help)
upx_add_test(upx-help-2 upx --help-short)
upx_add_test(upx-help-3 upx --help-verbose)
upx_add_test(upx-sysinfo-1 upx --sysinfo)
upx_add_test(upx-sysinfo-2 upx --sysinfo -v)
upx_add_test(upx-sysinfo-3 upx --sysinfo -vv)
if(NOT UPX_CONFIG_DISABLE_SELF_PACK_TEST)
# IMPORTANT NOTE: these tests can only work if the host executable format
# is supported by UPX!
include("${CMAKE_CURRENT_SOURCE_DIR}/misc/cmake/self_pack_test.cmake")
endif()
endif()
if(NOT CMAKE_CROSSCOMPILING OR CMAKE_CROSSCOMPILING_EMULATOR)
add_test(NAME acx_naked_c COMMAND acx_naked_c)
add_test(NAME acx_naked_x COMMAND acx_naked_x)
add_test(NAME acx_simple_c COMMAND acx_simple_c)
add_test(NAME acx_simple_x COMMAND acx_simple_x)
set_tests_properties(acx_naked_c PROPERTIES COST 1003)
set_tests_properties(acx_naked_x PROPERTIES COST 1002)
set_tests_properties(acx_simple_c PROPERTIES COST 1001)
set_tests_properties(acx_simple_x PROPERTIES COST 1000)
endif()
endif() # UPX_CONFIG_CMAKE_DISABLE_TEST
#***********************************************************************
# install
# cmake --install .
# make install
# ninja install
#***********************************************************************
upx_cmake_include_hook(7_install)
if(NOT UPX_CONFIG_CMAKE_DISABLE_INSTALL)
# installation prefix and directories
if(NOT CMAKE_INSTALL_PREFIX)
#message(FATAL_ERROR "ERROR: CMAKE_INSTALL_PREFIX is not defined")
message(WARNING "WARNING: CMAKE_INSTALL_PREFIX is not defined")
endif()
# install files
if(CMAKE_INSTALL_PREFIX AND DEFINED CMAKE_INSTALL_BINDIR)
install(TARGETS upx DESTINATION "${CMAKE_INSTALL_BINDIR}")
install(FILES
COPYING LICENSE NEWS README doc/THANKS.txt doc/upx-doc.html doc/upx-doc.txt
DESTINATION "${CMAKE_INSTALL_DOCDIR}"
)
install(FILES doc/upx.1 DESTINATION "${CMAKE_INSTALL_MANDIR}/man1")
install(TARGETS acx_naked_c acx_naked_x acx_simple_c acx_simple_x DESTINATION "${CMAKE_INSTALL_BINDIR}/acx")
endif()
endif() # UPX_CONFIG_CMAKE_DISABLE_INSTALL
#***********************************************************************
# summary
# print some info about the build configuration
#***********************************************************************
upx_cmake_include_hook(8_summary)
upx_print_var(CMAKE_VERSION UPX_CONFIG_CMAKE_MINIMUM_REQUIRED_VERSION CMAKE_GENERATOR)
if(NOT UPX_CONFIG_CMAKE_DISABLE_PRINT_INFO)
# print detailed info
include("${CMAKE_CURRENT_SOURCE_DIR}/misc/cmake/print_info.cmake")
upx_print_info()
endif()
upx_print_var(CMAKE_INSTALL_PREFIX CMAKE_CONFIGURATION_TYPES CMAKE_TRY_COMPILE_CONFIGURATION CMAKE_BUILD_TYPE)
if(Threads_FOUND)
message(STATUS "WITH_THREADS = 1")
elseif(UPX_CONFIG_REQUIRE_THREADS)
message(FATAL_ERROR "ERROR: WITH_THREADS required")
elseif(UPX_CONFIG_EXPECT_THREADS AND NOT UPX_CONFIG_DISABLE_THREADS)
message(FATAL_ERROR "ERROR: WITH_THREADS expected; set UPX_CONFIG_EXPECT_THREADS=OFF")
endif()
if(CMAKE_BUILD_TYPE AND NOT CMAKE_BUILD_TYPE MATCHES "^(Debug|None|Release)$")
message(WARNING "WARNING: unsupported CMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}; please use \"Debug\" or \"Release\"")
endif()
# extra sanity checks to detect incompatible C vs CXX settings
if(NOT UPX_CONFIG_CMAKE_DISABLE_PLATFORM_CHECK)
upx_platform_check_c_cxx_mismatch()
endif()
upx_cmake_include_hook(9_finish)
file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/.upx_cmake_config_done.txt" "")
# vim:set ft=cmake ts=4 sw=4 tw=0 et: