From 83e8c2be1342b7adcf223e4206e96ba91033207c Mon Sep 17 00:00:00 2001 From: "K.Ohta" Date: Tue, 17 May 2016 01:23:14 +0900 Subject: [PATCH] [BUILD][CMAKE] Add macros. --- .../build-cmake/3rdparty/CompressExeWithUpx.cmake | 28 ++++++++++ source/build-cmake/3rdparty/SetMSVCDebugPath.cmake | 31 +++++++++++ .../3rdparty/SplitDebugInformation.cmake | 60 ++++++++++++++++++++++ source/build-cmake/cmake/CompressExeWithUpx.cmake | 1 + source/build-cmake/cmake/SetMSVCDebugPath.cmake | 1 + .../build-cmake/cmake/SplitDebugInformation.cmake | 1 + 6 files changed, 122 insertions(+) create mode 100644 source/build-cmake/3rdparty/CompressExeWithUpx.cmake create mode 100644 source/build-cmake/3rdparty/SetMSVCDebugPath.cmake create mode 100644 source/build-cmake/3rdparty/SplitDebugInformation.cmake create mode 120000 source/build-cmake/cmake/CompressExeWithUpx.cmake create mode 120000 source/build-cmake/cmake/SetMSVCDebugPath.cmake create mode 120000 source/build-cmake/cmake/SplitDebugInformation.cmake diff --git a/source/build-cmake/3rdparty/CompressExeWithUpx.cmake b/source/build-cmake/3rdparty/CompressExeWithUpx.cmake new file mode 100644 index 000000000..249c15e1a --- /dev/null +++ b/source/build-cmake/3rdparty/CompressExeWithUpx.cmake @@ -0,0 +1,28 @@ +# Compress an executable with UPX. +# COMPRESS_EXE_WITH_UPX(EXE_TARGET) +# + +FIND_PROGRAM(UPX upx) +IF(NOT UPX) + MESSAGE(WARNING "upx was not found; executables will not be compressed.") +ENDIF(NOT UPX) + +IF(WIN32) + SET(UPX_OPTIONS "--compress-icons=0") +ENDIF(WIN32) + +MACRO(COMPRESS_EXE_WITH_UPX EXE_TARGET) +IF(UPX) + # NOTE: $ is preferred, + # but this doesn't seem to work on Ubuntu 10.04. + # (cmake_2.8.0-5ubuntu1_i386) + GET_PROPERTY(UPX_EXE_LOCATION TARGET ${EXE_TARGET} PROPERTY LOCATION) + + ADD_CUSTOM_COMMAND(TARGET ${EXE_TARGET} POST_BUILD + COMMAND ${UPX} --best --lzma ${UPX_OPTIONS} + ${UPX_EXE_LOCATION} + ) + + UNSET(UPX_EXE_LOCATION) +ENDIF(UPX) +ENDMACRO(COMPRESS_EXE_WITH_UPX) diff --git a/source/build-cmake/3rdparty/SetMSVCDebugPath.cmake b/source/build-cmake/3rdparty/SetMSVCDebugPath.cmake new file mode 100644 index 000000000..b458779b7 --- /dev/null +++ b/source/build-cmake/3rdparty/SetMSVCDebugPath.cmake @@ -0,0 +1,31 @@ +# Split debug information from an executable into a separate file. +# SPLIT_DEBUG_INFORMATION(EXE_TARGET) +# +# References: +# - http://cmake.3232098.n2.nabble.com/Save-stripped-debugging-information-td6819195.html +# - http://sourceware.org/bugzilla/show_bug.cgi?id=14527 +# - If debug symbols are stripped before .gnu_debuglink is added, +# the section will be truncated to .gnu_deb, and hence won't +# be recognized by gdb. +# - FIXME: If the above .gnu_debuglink workaround is used, Windows XP +# and Windows 7 will claim that the executable isn't a valid Win32 +# executable. (Wine ignores it and works fine!) +# +MACRO(SET_MSVC_DEBUG_PATH _target) +IF(MSVC) + # CMake seems to use weird settings for the PDB file. + # (at least version 2.8.12.2; 3.0.0 might be different) + STRING(REGEX REPLACE + "/Fd/" + "/Fd" + CMAKE_C_COMPILE_OBJECT "${CMAKE_C_COMPILE_OBJECT}") + STRING(REGEX REPLACE + "/Fd/" + "/Fd" + CMAKE_CXX_COMPILE_OBJECT "${CMAKE_CXX_COMPILE_OBJECT}") + + # Set the target PDB filename. + SET_TARGET_PROPERTIES(${_target} + PROPERTIES PDB "${CMAKE_CURRENT_BINARY_DIRECTORY}/${_target}.pdb") +ENDIF(MSVC) +ENDMACRO(SET_MSVC_DEBUG_PATH) diff --git a/source/build-cmake/3rdparty/SplitDebugInformation.cmake b/source/build-cmake/3rdparty/SplitDebugInformation.cmake new file mode 100644 index 000000000..48e98ef8e --- /dev/null +++ b/source/build-cmake/3rdparty/SplitDebugInformation.cmake @@ -0,0 +1,60 @@ +# Split debug information from an executable into a separate file. +# SPLIT_DEBUG_INFORMATION(EXE_TARGET) +# +# References: +# - http://cmake.3232098.n2.nabble.com/Save-stripped-debugging-information-td6819195.html +# - http://sourceware.org/bugzilla/show_bug.cgi?id=14527 +# - If debug symbols are stripped before .gnu_debuglink is added, +# the section will be truncated to .gnu_deb, and hence won't +# be recognized by gdb. +# - FIXME: If the above .gnu_debuglink workaround is used, Windows XP +# and Windows 7 will claim that the executable isn't a valid Win32 +# executable. (Wine ignores it and works fine!) +# +IF(NOT MSVC) + # CMake automatically finds objcopy and strip as + # part of its toolchain initialization. + IF(NOT CMAKE_OBJCOPY) + MESSAGE(WARNING "'objcopy' was not found; debug information will not be split.") + ELSEIF(NOT CMAKE_STRIP) + MESSAGE(WARNING "'strip' was not found; debug information will not be split.") + ENDIF() +ENDIF(NOT MSVC) + +MACRO(SPLIT_DEBUG_INFORMATION EXE_TARGET) +SET(SPLIT_OK 1) +IF(MSVC) + # MSVC splits debug information by itself. + SET(SPLIT_OK 0) +ELSEIF(NOT CMAKE_OBJCOPY) + # 'objcopy' is missing. + SET(SPLIT_OK 0) +ELSEIF(NOT CMAKE_STRIP) + # 'strip' is missing. + SET(SPLIT_OK 0) +ENDIF() + +IF(SPLIT_OK) + # NOTE: $ is preferred, + # but this doesn't seem to work on Ubuntu 10.04. + # (cmake_2.8.0-5ubuntu1_i386) + GET_PROPERTY(SPLITDEBUG_EXE_LOCATION TARGET ${EXE_TARGET} PROPERTY LOCATION) + + # NOTE: objcopy --strip-debug does NOT fully + # strip the binary; two sections are left: + # - .symtab: Symbol table. + # - .strtab: String table. + # These sections are split into the .debug file, so there's + # no reason to keep them in the executable. + ADD_CUSTOM_COMMAND(TARGET ${EXE_TARGET} POST_BUILD + COMMAND ${CMAKE_OBJCOPY} --only-keep-debug + ${SPLITDEBUG_EXE_LOCATION} ${CMAKE_CURRENT_BINARY_DIR}/${EXE_TARGET}.debug + COMMAND ${CMAKE_STRIP} + ${SPLITDEBUG_EXE_LOCATION} + COMMAND ${CMAKE_OBJCOPY} --add-gnu-debuglink=${EXE_TARGET}.debug + ${SPLITDEBUG_EXE_LOCATION} + ) + + UNSET(SPLITDEBUG_EXE_LOCATION) +ENDIF(SPLIT_OK) +ENDMACRO(SPLIT_DEBUG_INFORMATION) diff --git a/source/build-cmake/cmake/CompressExeWithUpx.cmake b/source/build-cmake/cmake/CompressExeWithUpx.cmake new file mode 120000 index 000000000..8dab6e97a --- /dev/null +++ b/source/build-cmake/cmake/CompressExeWithUpx.cmake @@ -0,0 +1 @@ +../3rdparty/CompressExeWithUpx.cmake \ No newline at end of file diff --git a/source/build-cmake/cmake/SetMSVCDebugPath.cmake b/source/build-cmake/cmake/SetMSVCDebugPath.cmake new file mode 120000 index 000000000..ef8f3871e --- /dev/null +++ b/source/build-cmake/cmake/SetMSVCDebugPath.cmake @@ -0,0 +1 @@ +../3rdparty/SetMSVCDebugPath.cmake \ No newline at end of file diff --git a/source/build-cmake/cmake/SplitDebugInformation.cmake b/source/build-cmake/cmake/SplitDebugInformation.cmake new file mode 120000 index 000000000..160018bfa --- /dev/null +++ b/source/build-cmake/cmake/SplitDebugInformation.cmake @@ -0,0 +1 @@ +../3rdparty/SplitDebugInformation.cmake \ No newline at end of file -- 2.11.0