OSDN Git Service

[BUILD][CMAKE] .
[csp-qt/common_source_project-fm7.git] / source / build-cmake / 3rdparty / SplitDebugInformation.cmake
1 # Split debug information from an executable into a separate file.
2 # SPLIT_DEBUG_INFORMATION(EXE_TARGET)
3 #
4 # References:
5 # - http://cmake.3232098.n2.nabble.com/Save-stripped-debugging-information-td6819195.html
6 # - http://sourceware.org/bugzilla/show_bug.cgi?id=14527
7 #   - If debug symbols are stripped before .gnu_debuglink is added,
8 #     the section will be truncated to .gnu_deb, and hence won't
9 #     be recognized by gdb.
10 # - FIXME: If the above .gnu_debuglink workaround is used, Windows XP
11 #   and Windows 7 will claim that the executable isn't a valid Win32
12 #   executable. (Wine ignores it and works fine!)
13 #
14 IF(NOT MSVC)
15         # CMake automatically finds objcopy and strip as
16         # part of its toolchain initialization.
17         IF(NOT CMAKE_OBJCOPY)
18                 MESSAGE(WARNING "'objcopy' was not found; debug information will not be split.")
19         ELSEIF(NOT CMAKE_STRIP)
20                 MESSAGE(WARNING "'strip' was not found; debug information will not be split.")
21         ENDIF()
22 ENDIF(NOT MSVC)
23
24 MACRO(SPLIT_DEBUG_INFORMATION EXE_TARGET)
25 SET(SPLIT_OK 1)
26 IF(MSVC)
27         # MSVC splits debug information by itself.
28         SET(SPLIT_OK 0)
29 ELSEIF(NOT CMAKE_OBJCOPY)
30         # 'objcopy' is missing.
31         SET(SPLIT_OK 0)
32 ELSEIF(NOT CMAKE_STRIP)
33         # 'strip' is missing.
34         SET(SPLIT_OK 0)
35 ENDIF()
36
37 IF(SPLIT_OK)
38         # NOTE: $<TARGET_FILE:gcbanner> is preferred,
39         # but this doesn't seem to work on Ubuntu 10.04.
40         # (cmake_2.8.0-5ubuntu1_i386)
41         GET_PROPERTY(SPLITDEBUG_EXE_LOCATION TARGET ${EXE_TARGET} PROPERTY LOCATION)
42
43         # NOTE: objcopy --strip-debug does NOT fully
44         # strip the binary; two sections are left:
45         # - .symtab: Symbol table.
46         # - .strtab: String table.
47         # These sections are split into the .debug file, so there's
48         # no reason to keep them in the executable.
49         ADD_CUSTOM_COMMAND(TARGET ${EXE_TARGET} POST_BUILD
50                 COMMAND ${CMAKE_OBJCOPY} --only-keep-debug
51                         ${SPLITDEBUG_EXE_LOCATION} ${CMAKE_CURRENT_BINARY_DIR}/${EXE_TARGET}.debug
52                 COMMAND ${CMAKE_STRIP}
53                         ${SPLITDEBUG_EXE_LOCATION}
54                 COMMAND ${CMAKE_OBJCOPY} --add-gnu-debuglink=${EXE_TARGET}.debug
55                         ${SPLITDEBUG_EXE_LOCATION}
56                 )
57
58         UNSET(SPLITDEBUG_EXE_LOCATION)
59 ENDIF(SPLIT_OK)
60 ENDMACRO(SPLIT_DEBUG_INFORMATION)