From 636cefb9f39c5cd9018f075d387b1eb5b8332f53 Mon Sep 17 00:00:00 2001 From: "Markus F.X.J. Oberhumer" Date: Wed, 20 Dec 2023 18:49:34 +0100 Subject: [PATCH] cmake update --- CMakeLists.txt | 47 +++++------------- misc/cmake/functions.cmake | 72 ++++++++++++++++++++++++---- misc/cmake/use_strict_defaults.cmake | 0 3 files changed, 74 insertions(+), 45 deletions(-) create mode 100644 misc/cmake/use_strict_defaults.cmake diff --git a/CMakeLists.txt b/CMakeLists.txt index 1f82fe73..8404e266 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -35,7 +35,7 @@ include("${CMAKE_CURRENT_SOURCE_DIR}/misc/cmake/functions.cmake") upx_cmake_include_hook(1_options) # compilation config options -if(NOT IS_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/.git") +if(NOT IS_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/.git" OR NOT EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/misc/cmake/use_strict_defaults.cmake") # permissive config defaults when building from source code tarball option(UPX_CONFIG_DISABLE_GITREV "Do not compile with default Git version info." ON) option(UPX_CONFIG_DISABLE_SANITIZE "Do not compile with default sanitize options." ON) @@ -62,14 +62,10 @@ set(UPX_VERSION_STRING "4.3.0") upx_cmake_include_hook(2_init) -# Disallow in-source builds. Note that you will still have to manually +# 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. if(IS_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/.git") - set(CMAKE_DISABLE_IN_SOURCE_BUILD ON) - set(CMAKE_DISABLE_SOURCE_CHANGES ON) - if(",${CMAKE_CURRENT_SOURCE_DIR}," STREQUAL ",${CMAKE_CURRENT_BINARY_DIR},") - message(FATAL_ERROR "ERROR: In-source builds are not allowed, please use an extra build dir.") - endif() + upx_disallow_in_source_build() endif() # global settings @@ -132,35 +128,10 @@ else() message(STATUS "UPX_VERSION_GITREV: not set") endif() -# set the default build type to "Release" -get_property(is_multi_config GLOBAL PROPERTY GENERATOR_IS_MULTI_CONFIG) -if(NOT is_multi_config AND NOT CMAKE_BUILD_TYPE) - set(CMAKE_BUILD_TYPE "Release" CACHE STRING "Choose the type of build." FORCE) -endif() - # CMake init +upx_default_build_type(Release) project(upx VERSION "${UPX_VERSION_STRING}" LANGUAGES C CXX) - -# set the default multi-config build type to "Release" -if(is_multi_config) - set(c "${CMAKE_CONFIGURATION_TYPES}") - list(INSERT c 0 "Release") - list(INSERT c 1 "Debug") - if(CMAKE_BUILD_TYPE) - list(INSERT c 0 "${CMAKE_BUILD_TYPE}") - endif() - list(REMOVE_DUPLICATES c) - set(CMAKE_CONFIGURATION_TYPES "${c}" CACHE STRING "List of supported configuration types." FORCE) -endif() - -# set the build type for use in try_compile() -if(NOT CMAKE_TRY_COMPILE_CONFIGURATION) - if(CMAKE_BUILD_TYPE) - set(CMAKE_TRY_COMPILE_CONFIGURATION "${CMAKE_BUILD_TYPE}") - else() - set(CMAKE_TRY_COMPILE_CONFIGURATION "Release") - endif() -endif() +upx_apply_build_type() # set MSVC_FRONTEND and MINGW if(NOT DEFINED MSVC_FRONTEND AND (MSVC OR CMAKE_C_COMPILER_FRONTEND_VARIANT MATCHES "^MSVC")) @@ -452,6 +423,10 @@ if(NOT CMAKE_CROSSCOMPILING OR CMAKE_CROSSCOMPILING_EMULATOR) add_test(NAME upx-sysinfo COMMAND upx --sysinfo -v) if(NOT UPX_CONFIG_DISABLE_SELF_PACK_TEST) # IMPORTANT NOTE: these tests can only work if the host executable format is supported by UPX! + set(emu "") + if(DEFINED CMAKE_CROSSCOMPILING_EMULATOR) + set(emu "${CMAKE_CROSSCOMPILING_EMULATOR}") + endif() set(exe "${CMAKE_EXECUTABLE_SUFFIX}") set(upx_self_exe "$") set(fo "--force-overwrite") @@ -464,8 +439,8 @@ if(NOT UPX_CONFIG_DISABLE_SELF_PACK_TEST) upx_add_serial_test(upx-fileinfo upx --fileinfo upx-packed${exe} upx-packed-n2b${exe} upx-packed-n2d${exe} upx-packed-n2e${exe} upx-packed-lzma${exe}) upx_add_serial_test(upx-test upx -t upx-packed${exe} upx-packed-n2b${exe} upx-packed-n2d${exe} upx-packed-n2e${exe} upx-packed-lzma${exe}) upx_add_serial_test(upx-unpack upx -d upx-packed${exe} ${fo} -o upx-unpacked${exe}) - upx_add_serial_test(upx-run-unpacked ${CMAKE_CROSSCOMPILING_EMULATOR} ./upx-unpacked${exe} --version-short) - upx_add_serial_test(upx-run-packed ${CMAKE_CROSSCOMPILING_EMULATOR} ./upx-packed${exe} --version-short) + upx_add_serial_test(upx-run-unpacked ${emu} ./upx-unpacked${exe} --version-short) + upx_add_serial_test(upx-run-packed ${emu} ./upx-packed${exe} --version-short) endif() # UPX_CONFIG_DISABLE_SELF_PACK_TEST endif() diff --git a/misc/cmake/functions.cmake b/misc/cmake/functions.cmake index b16d1196..7b4a23d5 100644 --- a/misc/cmake/functions.cmake +++ b/misc/cmake/functions.cmake @@ -4,7 +4,7 @@ # #*********************************************************************** -# util +# macros #*********************************************************************** # support config hooks; developer convenience @@ -13,11 +13,60 @@ macro(upx_cmake_include_hook section) include("${CMAKE_CURRENT_SOURCE_DIR}/maint/make/CMakeLists.${section}.txt" OPTIONAL) endmacro() +# 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. +macro(upx_disallow_in_source_build) + set(CMAKE_DISABLE_IN_SOURCE_BUILD ON) + set(CMAKE_DISABLE_SOURCE_CHANGES ON) + if(",${CMAKE_CURRENT_SOURCE_DIR}," STREQUAL ",${CMAKE_CURRENT_BINARY_DIR},") + # try to clean up (does not work) + #file(REMOVE "${CMAKE_CURRENT_BINARY_DIR}/CMakeCache.txt") + #file(REMOVE "${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/cmake.check_cache") + message(FATAL_ERROR "ERROR: In-source builds are not allowed, please use an extra build dir.") + endif() +endmacro() + +# set the default build type; must be called before project() cmake init +macro(upx_default_build_type type) + set(upx_global_default_build_type "${type}") + get_property(upx_global_is_multi_config GLOBAL PROPERTY GENERATOR_IS_MULTI_CONFIG) + if(NOT upx_global_is_multi_config AND NOT CMAKE_BUILD_TYPE) + set(CMAKE_BUILD_TYPE "${upx_global_default_build_type}" CACHE STRING "Choose the type of build." FORCE) + endif() +endmacro() + +# set the default multi-config build type; must be called after project() cmake init +macro(upx_apply_build_type) + if(upx_global_is_multi_config) + set(c "${CMAKE_CONFIGURATION_TYPES}") + list(INSERT c 0 "${upx_global_default_build_type}") + if(CMAKE_BUILD_TYPE) + list(INSERT c 0 "${CMAKE_BUILD_TYPE}") + endif() + list(REMOVE_DUPLICATES c) + set(CMAKE_CONFIGURATION_TYPES "${c}" CACHE STRING "List of supported configuration types." FORCE) + endif() + # now also set the build type for use in try_compile() + if(NOT CMAKE_TRY_COMPILE_CONFIGURATION) + if(CMAKE_BUILD_TYPE) + set(CMAKE_TRY_COMPILE_CONFIGURATION "${CMAKE_BUILD_TYPE}") + else() + set(CMAKE_TRY_COMPILE_CONFIGURATION "${upx_global_default_build_type}") + endif() + endif() +endmacro() + +#*********************************************************************** +# util +#*********************************************************************** + function(upx_print_var) # ARGV foreach(var_name ${ARGV}) - if(DEFINED ${var_name} AND NOT ",${${var_name}}," STREQUAL ",,") - if(${var_name}) - message(STATUS "${var_name} = ${${var_name}}") + if(DEFINED ${var_name}) + if(NOT ",${${var_name}}," STREQUAL ",,") + if(${var_name}) + message(STATUS "${var_name} = ${${var_name}}") + endif() endif() endif() endforeach() @@ -52,7 +101,10 @@ function(upx_add_glob_files) # ARGV set(var_name ${ARGV0}) list(REMOVE_AT ARGV 0) file(GLOB files ${ARGV}) - set(result "${${var_name}}") + set(result "") + if(DEFINED ${var_name}) + set(result "${${var_name}}") + endif() list(APPEND result "${files}") list(SORT result) list(REMOVE_DUPLICATES result) @@ -70,9 +122,11 @@ function(upx_cache_bool_vars) # ARGV set(value "${UPX_CACHE_VALUE_${var_name}}") elseif(DEFINED ${var_name}) # defined via "cmake -DXXX=YYY" set(value "${${var_name}}") - elseif("$ENV{${var_name}}" MATCHES "^(0|1|OFF|ON|FALSE|TRUE)$") # check environment - set(value "$ENV{${var_name}}") - set(UPX_CACHE_ORIGIN_FROM_ENV_${var_name} TRUE CACHE INTERNAL "" FORCE) # for info below + elseif(DEFINED ENV{${var_name}}) + if("$ENV{${var_name}}" MATCHES "^(0|1|OFF|ON|FALSE|TRUE)$") # check environment + set(value "$ENV{${var_name}}") + set(UPX_CACHE_ORIGIN_FROM_ENV_${var_name} TRUE CACHE INTERNAL "" FORCE) # for info below + endif() endif() # convert to bool if(value) @@ -141,7 +195,7 @@ function(upx_compile_source_debug_with_O2) # ARGV set(flags "$<$:-O2>") if(${CMAKE_VERSION} VERSION_LESS "3.8") # 3.8: The COMPILE_FLAGS source file property learned to support generator expressions - if(is_multi_config OR NOT CMAKE_BUILD_TYPE MATCHES "^Debug$") + if(upx_global_is_multi_config OR NOT CMAKE_BUILD_TYPE MATCHES "^Debug$") return() endif() set(flags "-O2") diff --git a/misc/cmake/use_strict_defaults.cmake b/misc/cmake/use_strict_defaults.cmake new file mode 100644 index 00000000..e69de29b