all: misc cleanups

This commit is contained in:
Markus F.X.J. Oberhumer 2024-07-23 12:24:09 +02:00
parent 0a82280f20
commit 03c4184067
11 changed files with 118 additions and 92 deletions

View File

@ -68,7 +68,7 @@ upx_cmake_include_hook(2_init)
# clean up a few files if you accidentally try an in-source build.
upx_disallow_in_source_build()
# global settings part 1
# 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)
@ -136,21 +136,6 @@ 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()
# global settings part 2
if(CMAKE_GENERATOR MATCHES "Ninja" AND NOT CMAKE_EXECUTABLE_FORMAT MATCHES "^ELF")
set(CMAKE_BUILD_WITH_INSTALL_RPATH ON)
endif()
# set MSVC_FRONTEND, GNU_FRONTEND and MINGW
if(NOT DEFINED MSVC_FRONTEND AND (MSVC OR CMAKE_C_COMPILER_FRONTEND_VARIANT MATCHES "^MSVC"))
set(MSVC_FRONTEND 1)
elseif (NOT DEFINED GNU_FRONTEND AND (CMAKE_C_COMPILER_FRONTEND_VARIANT MATCHES "^GNU" OR CMAKE_C_COMPILER_ID MATCHES "(Clang|GNU)"))
set(GNU_FRONTEND 1)
endif()
if(NOT DEFINED MINGW AND CMAKE_C_PLATFORM_ID MATCHES "^MinGW")
set(MINGW 1)
endif()
#***********************************************************************
# common compilation flags
#***********************************************************************
@ -514,6 +499,7 @@ upx_print_var(CMAKE_VERSION UPX_CONFIG_CMAKE_MINIMUM_REQUIRED_VERSION CMAKE_GENE
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)

View File

@ -29,7 +29,7 @@ endif
run_cmake_config = $(CMAKE) -S . -B $1 $(UPX_CMAKE_CONFIG_FLAGS) -DCMAKE_BUILD_TYPE=$2
run_cmake_build = $(CMAKE) --build $1 $(UPX_CMAKE_BUILD_FLAGS) --config $2
# avoid re-running run_cmake_config if .upx_cmake_config_done.txt already exists
run_config = $(if $(wildcard $1/CMakeFiles/.upx_cmake_config_done.txt),,$(call run_cmake_config,$1,$2))
run_config = $(if $(wildcard $1/CMakeFiles/.*_cmake_config_done.txt),,$(call run_cmake_config,$1,$2))
run_build = $(call run_cmake_build,$1,$2)
build/debug: PHONY
@ -77,11 +77,12 @@ test: $$(patsubst %+test,%,$$(.DEFAULT_GOAL))+test PHONY
#
# extra pre-defined build configurations and some utility; optional
include ./misc/make/Makefile-extra.mk
-include ./Makevars-local.mk
-include ./misc/make/Makefile-extra.mk
# developer convenience
ifneq ($(wildcard /usr/bin/env),) # need Unix utils like bash, perl, sed, xargs, etc.
ifneq ($(wildcard ./misc/scripts/.),)
ifneq ($(and $(wildcard ./misc/scripts/.),$(wildcard ./doc/upx.pod)),)
check-whitespace clang-format run-testsuite run-testsuite-all run-testsuite-debug run-testsuite-release: src/Makefile PHONY
$(MAKE) -C src $@
endif

View File

@ -42,6 +42,10 @@ macro(upx_set_default_build_type type)
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()
# also enable CMAKE_EXPORT_COMPILE_COMMANDS by default
if(NOT DEFINED CMAKE_EXPORT_COMPILE_COMMANDS)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
endif()
endmacro()
# set the default multi-config build type; must be called after project() cmake init
@ -63,6 +67,22 @@ macro(upx_apply_build_type)
set(CMAKE_TRY_COMPILE_CONFIGURATION "${upx_global_default_build_type}")
endif()
endif()
# handle CMAKE_BUILD_WITH_INSTALL_RPATH
if(NOT DEFINED CMAKE_BUILD_WITH_INSTALL_RPATH)
if(CMAKE_GENERATOR MATCHES "Ninja" AND NOT CMAKE_EXECUTABLE_FORMAT MATCHES "^ELF")
# info: needed by Ninja generator unless on an ELF-based or XCOFF-based platform
set(CMAKE_BUILD_WITH_INSTALL_RPATH ON)
endif()
endif()
# and also set MSVC_FRONTEND, GNU_FRONTEND and MINGW
if(NOT DEFINED MSVC_FRONTEND AND (MSVC OR CMAKE_C_COMPILER_FRONTEND_VARIANT MATCHES "^MSVC"))
set(MSVC_FRONTEND 1)
elseif(NOT DEFINED GNU_FRONTEND AND (CMAKE_C_COMPILER_FRONTEND_VARIANT MATCHES "^GNU" OR CMAKE_C_COMPILER_ID MATCHES "(Clang|GNU|LLVM)"))
set(GNU_FRONTEND 1)
endif()
if(NOT DEFINED MINGW AND CMAKE_C_PLATFORM_ID MATCHES "^MinGW")
set(MINGW 1)
endif()
endmacro()
# ternary conditional operator
@ -97,7 +117,7 @@ function(upx_make_bool_var result_var_name var_name default_value)
set(${result_var_name} "${result}" PARENT_SCOPE) # return value
endfunction()
function(upx_unused_var) # ARGV
function(upx_maybe_unused_var) # ARGV
foreach(var_name ${ARGV})
if(DEFINED ${var_name})
set(dummy "${${var_name}}")
@ -339,6 +359,15 @@ endfunction()
# sanitize a target; note that this may require special run-time support libs from your toolchain
function(upx_sanitize_target) # ARGV
# default sanitizer for Debug builds
if(NOT DEFINED upx_sanitize_flags_debug)
set(upx_sanitize_flags_debug -fsanitize=undefined -fsanitize-undefined-trap-on-error -fstack-protector-all)
endif()
# default sanitizer for Release builds
if(NOT DEFINED upx_sanitize_flags_release)
set(upx_sanitize_flags_release -fstack-protector)
endif()
foreach(t ${ARGV})
if(UPX_CONFIG_DISABLE_SANITIZE)
# no-op
@ -356,12 +385,10 @@ function(upx_sanitize_target) # ARGV
# unsupported compiler; unreliable/broken sanitize implementation before gcc-8 (May 2018)
message(WARNING "WARNING: ignoring SANITIZE for target '${t}'")
else()
# default sanitizer for Debug builds
target_compile_options(${t} PRIVATE $<$<CONFIG:Debug>:-fsanitize=undefined -fsanitize-undefined-trap-on-error -fstack-protector-all>)
# default sanitizer for Release builds
target_compile_options(${t} PRIVATE $<$<CONFIG:MinSizeRel>:-fstack-protector>)
target_compile_options(${t} PRIVATE $<$<CONFIG:Release>:-fstack-protector>)
target_compile_options(${t} PRIVATE $<$<CONFIG:RelWithDebInfo>:-fstack-protector>)
target_compile_options(${t} PRIVATE $<$<CONFIG:Debug>:${upx_sanitize_flags_debug}>)
target_compile_options(${t} PRIVATE $<$<CONFIG:MinSizeRel>:${upx_sanitize_flags_release}>)
target_compile_options(${t} PRIVATE $<$<CONFIG:Release>:${upx_sanitize_flags_release}>)
target_compile_options(${t} PRIVATE $<$<CONFIG:RelWithDebInfo>:${upx_sanitize_flags_release}>)
endif()
endforeach()
endfunction()

View File

@ -8,6 +8,7 @@
# print some info about the build configuration
#***********************************************************************
function(upx_print_info) # ARGV
get_property(PROPERTY_GENERATOR_IS_MULTI_CONFIG GLOBAL PROPERTY GENERATOR_IS_MULTI_CONFIG)
get_property(PROPERTY_TARGET_SUPPORTS_SHARED_LIBS GLOBAL PROPERTY TARGET_SUPPORTS_SHARED_LIBS)
@ -27,6 +28,7 @@ upx_print_var(CMAKE_ANDROID_NDK CMAKE_ANDROID_NDK_VERSION CMAKE_ANDROID_STANDALO
upx_print_var(CMAKE_APPLE_SILICON_PROCESSOR CMAKE_OSX_DEPLOYMENT_TARGET CMAKE_OSX_SYSROOT)
upx_print_var(CMAKE_VS_PLATFORM_TOOLSET CMAKE_VS_PLATFORM_TOOLSET_HOST_ARCHITECTURE CMAKE_VS_PLATFORM_TOOLSET_VERSION)
upx_print_var(CMAKE_CROSSCOMPILING CMAKE_CROSSCOMPILING_EMULATOR)
upx_print_var(CMAKE_SIZEOF_VOID_P)
# binutils
upx_print_var(CMAKE_EXECUTABLE_FORMAT CMAKE_AR CMAKE_RANLIB)
@ -51,5 +53,6 @@ upx_print_var(PROPERTY_TARGET_SUPPORTS_SHARED_LIBS)
# shortcuts
upx_print_var(APPLE CLANG CYGWIN GNU_FRONTEND GNUC MINGW MSVC MSVC_FRONTEND MSVC_IDE MSVC_TOOLSET_VERSION MSVC_VERSION MSYS UNIX WIN32 WIN64)
endfunction()
# vim:set ft=cmake ts=4 sw=4 tw=0 et:

View File

@ -70,18 +70,21 @@ build/extra/clang-static/debug: PHONY; $(call run_config_and_build,$@,Debug)
build/extra/clang-static/release: PHONY; $(call run_config_and_build,$@,Release)
build/extra/clang-static/%: export CC = clang -static
build/extra/clang-static/%: export CXX = clang++ -static
build/extra/clang-static/%: export UPX_CONFIG_DISABLE_SHARED_LIBS = ON
# force building with clang/clang++ -static-pie
build/extra/clang-static-pie/debug: PHONY; $(call run_config_and_build,$@,Debug)
build/extra/clang-static-pie/release: PHONY; $(call run_config_and_build,$@,Release)
build/extra/clang-static-pie/%: export CC = clang -static-pie -fPIE -Wno-unused-command-line-argument
build/extra/clang-static-pie/%: export CXX = clang++ -static-pie -fPIE -Wno-unused-command-line-argument
build/extra/clang-static-pie/%: export UPX_CONFIG_DISABLE_SHARED_LIBS = ON
# force building with clang/clang++ -static -flto
build/extra/clang-static-lto/debug: PHONY; $(call run_config_and_build,$@,Debug)
build/extra/clang-static-lto/release: PHONY; $(call run_config_and_build,$@,Release)
build/extra/clang-static-lto/%: export CC = clang -static -flto
build/extra/clang-static-lto/%: export CXX = clang++ -static -flto
build/extra/clang-static-lto/%: export UPX_CONFIG_DISABLE_SHARED_LIBS = ON
# force building with clang/clang++ C++20 (and C17)
build/extra/clang-std-cxx20/debug: PHONY; $(call run_config_and_build,$@,Debug)
@ -134,18 +137,21 @@ build/extra/gcc-static/debug: PHONY; $(call run_config_and_build,$@,Debug)
build/extra/gcc-static/release: PHONY; $(call run_config_and_build,$@,Release)
build/extra/gcc-static/%: export CC = gcc -static
build/extra/gcc-static/%: export CXX = g++ -static
build/extra/gcc-static/%: export UPX_CONFIG_DISABLE_SHARED_LIBS = ON
# force building with gcc/g++ -static-pie
build/extra/gcc-static-pie/debug: PHONY; $(call run_config_and_build,$@,Debug)
build/extra/gcc-static-pie/release: PHONY; $(call run_config_and_build,$@,Release)
build/extra/gcc-static-pie/%: export CC = gcc -static-pie -fPIE
build/extra/gcc-static-pie/%: export CXX = g++ -static-pie -fPIE
build/extra/gcc-static-pie/%: export UPX_CONFIG_DISABLE_SHARED_LIBS = ON
# force building with gcc/g++ -static -flto
build/extra/gcc-static-lto/debug: PHONY; $(call run_config_and_build,$@,Debug)
build/extra/gcc-static-lto/release: PHONY; $(call run_config_and_build,$@,Release)
build/extra/gcc-static-lto/%: export CC = gcc -static -flto
build/extra/gcc-static-lto/%: export CXX = g++ -static -flto
build/extra/gcc-static-lto/%: export UPX_CONFIG_DISABLE_SHARED_LIBS = ON
# force building with gcc/g++ C++20 (and C17)
build/extra/gcc-std-cxx20/debug: PHONY; $(call run_config_and_build,$@,Debug)

View File

@ -6,8 +6,9 @@ argv0=$0; argv0abs=$(readlink -fn "$argv0"); argv0dir=$(dirname "$argv0abs")
#
# Copyright (C) Markus Franz Xaver Johannes Oberhumer
#
# mimic running "ctest", i.e. the "test" section of CMakeLists.txt; does not redirect stdout
# (allows freely setting $upx_exe_runner, while CMake is restricted to configure-time settings)
# mimic running "ctest", i.e. the "test" section of CMakeLists.txt
# - does not redirect stdout
# - allows freely setting $upx_exe_runner, while CMake is restricted to configure-time settings
#
# requires:
# $upx_exe (required, but with convenience fallback "./upx")
@ -48,7 +49,7 @@ if ! "${run_upx[@]}" --help >/dev/null; then echo "UPX-ERROR: FATAL: upx --help
#***********************************************************************
# see CMakeLists.txt
#
# IDEA: create a Makefile and use "make -j" so that these tests can
# IDEA: create a Makefile and use "make -j8" so that these tests can
# run in parallel much like "ctest --parallel 8"
#***********************************************************************

View File

@ -256,7 +256,7 @@ def compress_stub(method, idata):
# encode upx stub header
odata = encode_compressed_stub_header(method, idata, odata) + odata
else:
raise Exception, ("invalid method", method, opts.methods)
raise Exception("invalid method", method, opts.methods)
if 1 and len(odata) >= len(idata):
# not compressible
return 0, idata

View File

@ -58,29 +58,29 @@ def do_file(fn):
def brand_arm(s):
if e_ident[4:7] != s:
raise Exception, "%s is not %s" % (fn, opts.bfdname)
raise Exception("%s is not %s" % (fn, opts.bfdname))
write("\x61") # ELFOSABI_ARM
def brand_freebsd(s):
if e_ident[4:7] != s:
raise Exception, "%s is not %s" % (fn, opts.bfdname)
raise Exception("%s is not %s" % (fn, opts.bfdname))
write("\x09")
def brand_linux(s):
if e_ident[4:7] != s:
raise Exception, "%s is not %s" % (fn, opts.bfdname)
raise Exception("%s is not %s" % (fn, opts.bfdname))
##write("\x00Linux\x00\x00\x00")
write("\x00" * 9)
def brand_netbsd(s):
if e_ident[4:7] != s:
raise Exception, "%s is not %s" % (fn, opts.bfdname)
raise Exception("%s is not %s" % (fn, opts.bfdname))
write("\x02")
def brand_openbsd(s):
if e_ident[4:7] != s:
raise Exception, "%s is not %s" % (fn, opts.bfdname)
raise Exception("%s is not %s" % (fn, opts.bfdname))
write("\x0c")
if opts.bfdname[:3] == "elf":
if e_ident[:4] != "\x7f\x45\x4c\x46":
raise Exception, "%s is not %s" % (fn, "ELF")
raise Exception("%s is not %s" % (fn, "ELF"))
fp.seek(7, 0)
if opts.bfdname == "elf32-bigarm" and opts.elfosabi == "arm":
brand_arm("\x01\x02\x01")
@ -106,7 +106,7 @@ def do_file(fn):
done = 0
fp.close()
if not done:
raise Exception, ("error: invalid args", opts.__dict__)
raise Exception("error: invalid args", opts.__dict__)
def main(argv):
@ -127,7 +127,7 @@ def main(argv):
else: assert 0, ("getopt problem:", opt, optarg, xopts, args)
# process arguments
if not args:
raise Exception, "error: no arguments given"
raise Exception("error: no arguments given")
for arg in args:
do_file(arg)
return 0

View File

@ -63,7 +63,7 @@ def not_found(state, l, inc, fatal=None):
if fatal is None:
fatal = opts.fatal
if fatal:
raise Exception, "%s:%d: include file %s not found" % (state[0], state[2], inc)
raise Exception("%s:%d: include file %s not found" % (state[0], state[2], inc))
return l
@ -78,7 +78,7 @@ def parse_comment(state, l, comment):
if f == "ignore=0": cf["fatal"] = 1
elif f == "ignore=1": cf["fatal"] = 0
else:
raise Exception, "%s:%d: bad flags %s %s" % (state[0], state[2], f, str(flags))
raise Exception("%s:%d: bad flags %s %s" % (state[0], state[2], f, str(flags)))
return cf
@ -93,7 +93,7 @@ def handle_inc_c(state, l, ofp):
elif q1 == '"' and q2 == '"':
dirs = [state[1]] + opts.includes
else:
raise Exception, "syntax error: include line " + l
raise Exception("syntax error: include line " + l)
for dir in dirs:
fn = os.path.join(dir, inc)
if os.path.isfile(fn):
@ -114,7 +114,7 @@ def handle_inc_nasm(state, l, ofp):
elif q1 == '"' and q2 == '"':
pass
else:
raise Exception, "syntax error: include line " + l
raise Exception("syntax error: include line " + l)
# info: nasm simply does concat the includes
for prefix in opts.includes + [""]:
fn = prefix + inc

View File

@ -73,6 +73,8 @@ def check_dump(dump_fn):
psections = d.find("Sections:\n")
psymbols = d.find("SYMBOL TABLE:\n")
prelocs = d.find("RELOCATION RECORDS FOR ");
if prelocs == -1: # no relocations
prelocs = len(d)
assert 0 <= psections < psymbols < prelocs
# preprocessSections
sections = []
@ -139,7 +141,7 @@ def do_file(fn):
fp.seek(0, 0)
idata = fp.read()
if idata[:4] != "\x7f\x45\x4c\x46":
raise Exception, "%s is not %s" % (fn, "ELF")
raise Exception("%s is not %s" % (fn, "ELF"))
if idata[4:7] == "\x01\x01\x01":
# ELF32 LE
eh, idata = idata[:52], idata[52:]
@ -162,7 +164,7 @@ def do_file(fn):
e_shnum, e_shstrndx = struct.unpack(">HH", eh[60:64])
assert e_shstrndx + 3 == e_shnum
else:
raise Exception, "%s is not %s" % (fn, "ELF")
raise Exception("%s is not %s" % (fn, "ELF"))
odata = None
pos = idata.find("\0.symtab\0.strtab\0.shstrtab\0")
@ -207,7 +209,7 @@ def main(argv):
elif opt in ["--with-dump"]: opts.with_dump = optarg
else: assert 0, ("getopt problem:", opt, optarg, xopts, args)
if not args:
raise Exception, "error: no arguments given"
raise Exception("error: no arguments given")
if opts.with_dump or opts.bindump:
assert len(args) == 1, "need exactly one file"
# process arguments