Strip symbols on release builds into separate binaries
authorMike McLaughlin <mikem@microsoft.com>
Tue, 15 Mar 2016 23:04:06 +0000 (16:04 -0700)
committerMike McLaughlin <mikem@microsoft.com>
Tue, 22 Mar 2016 21:24:03 +0000 (14:24 -0700)
Issue #3669

Created a common cmake strip_symbols function that all the modules and programs
use to strip the symbols out of the main into a separate .dbg (Linux) or .dSYM (OSX)
file.

Added an install_clr cmake function to encapsulate the install logic.

Changed all the library module cmake install lines from a TARGETS to a FILES one. The
TARGETS based install directives caused cmake to relink the binary and copy the unstripped
version to the install path. Left the all programs like corerun or ildasm as TARGETS
installs because on OSX FILES type installs don't get marked as executable.

Need to use "get_property(strip_source_file TARGET ${targetName} PROPERTY LOCATION)" for
the older versions of cmake and "set(strip_source_file $<TARGET_FILE:${targetName}>)" on
newer versions (v3 or greater).

19 files changed:
CMakeLists.txt
src/ToolBox/SOS/Strike/CMakeLists.txt
src/ToolBox/SOS/lldbplugin/CMakeLists.txt
src/coreclr/hosts/coreconsole/CMakeLists.txt
src/coreclr/hosts/corerun/CMakeLists.txt
src/coreclr/hosts/osxbundlerun/CMakeLists.txt
src/coreclr/hosts/unixcoreconsole/CMakeLists.txt
src/coreclr/hosts/unixcorerun/CMakeLists.txt
src/corefx/System.Globalization.Native/CMakeLists.txt
src/dlls/clretwrc/CMakeLists.txt
src/dlls/dbgshim/CMakeLists.txt
src/dlls/mscordac/CMakeLists.txt
src/dlls/mscordbi/CMakeLists.txt
src/dlls/mscoree/coreclr/CMakeLists.txt
src/ilasm/CMakeLists.txt
src/ildasm/exe/CMakeLists.txt
src/jit/standalone/CMakeLists.txt
src/scripts/genXplatLttng.py
src/tools/crossgen/CMakeLists.txt

index ef0220d9a7861888a758778383293f3825d8e78e..ba328f48bea1f5daf7e51cdfa684cb922b1e1d78 100644 (file)
@@ -154,6 +154,27 @@ else()
     if (AWK STREQUAL "AWK-NOTFOUND")
         message(FATAL_ERROR "AWK not found")
     endif()
+    if (CMAKE_SYSTEM_NAME STREQUAL Darwin)
+
+      # Ensure that dsymutil and strip is present
+      find_program(DSYMUTIL dsymutil)
+      if (DSYMUTIL STREQUAL "DSYMUTIL-NOTFOUND")
+          message(FATAL_ERROR "dsymutil not found")
+      endif()
+      find_program(STRIP strip)
+      if (STRIP STREQUAL "STRIP-NOTFOUND")
+          message(FATAL_ERROR "strip not found")
+      endif()
+
+    else (CMAKE_SYSTEM_NAME STREQUAL Darwin)
+
+      # Ensure that objcopy is present
+      find_program(OBJCOPY objcopy)
+      if (OBJCOPY STREQUAL "OBJCOPY-NOTFOUND")
+          message(FATAL_ERROR "objcopy not found")
+      endif()
+    endif (CMAKE_SYSTEM_NAME STREQUAL Darwin)
 endif(WIN32)
 
 # Build a list of compiler definitions by putting -D in front of each define.
@@ -242,6 +263,69 @@ function(add_precompiled_header header cppFile targetSources)
   endif(MSVC)
 endfunction()
 
+function(strip_symbols targetName outputFilename)
+  if(CLR_CMAKE_PLATFORM_UNIX)
+    if(UPPERCASE_CMAKE_BUILD_TYPE STREQUAL RELEASE)
+
+      # On the older version of cmake (2.8.12) used on Ubuntu 14.04 the TARGET_FILE
+      # generator expression doesn't work correctly returning the wrong path and on
+      # the newer cmake versions the LOCATION property isn't supported anymore.
+      if(CMAKE_VERSION VERSION_EQUAL 3.0 OR CMAKE_VERSION VERSION_GREATER 3.0)
+          set(strip_source_file $<TARGET_FILE:${targetName}>)
+      else()
+          get_property(strip_source_file TARGET ${targetName} PROPERTY LOCATION)
+      endif()
+
+      if(CMAKE_SYSTEM_NAME STREQUAL Darwin)
+        set(strip_destination_file ${strip_source_file}.dwarf)
+
+        add_custom_command(
+          TARGET ${targetName}
+          POST_BUILD
+          VERBATIM 
+          COMMAND ${DSYMUTIL} --flat --minimize ${strip_source_file}
+          COMMAND ${STRIP} -u -r ${strip_source_file}
+          COMMENT Stripping symbols from ${strip_source_file} into file ${strip_destination_file}
+        )
+      else(CMAKE_SYSTEM_NAME STREQUAL Darwin)
+        set(strip_destination_file ${strip_source_file}.dbg)
+
+        add_custom_command(
+          TARGET ${targetName}
+          POST_BUILD
+          VERBATIM 
+          COMMAND ${OBJCOPY} --only-keep-debug ${strip_source_file} ${strip_destination_file}
+          COMMAND ${OBJCOPY} --strip-unneeded ${strip_source_file}
+          COMMAND ${OBJCOPY} --add-gnu-debuglink=${strip_destination_file} ${strip_source_file}
+          COMMENT Stripping symbols from ${strip_source_file} into file ${strip_destination_file}
+        )
+      endif(CMAKE_SYSTEM_NAME STREQUAL Darwin)
+
+      set(${outputFilename} ${strip_destination_file} PARENT_SCOPE)
+    endif(UPPERCASE_CMAKE_BUILD_TYPE STREQUAL RELEASE)
+  endif(CLR_CMAKE_PLATFORM_UNIX)
+endfunction()
+
+function(install_clr targetName)
+  strip_symbols(${targetName} strip_destination_file)
+
+  # On the older version of cmake (2.8.12) used on Ubuntu 14.04 the TARGET_FILE
+  # generator expression doesn't work correctly returning the wrong path and on
+  # the newer cmake versions the LOCATION property isn't supported anymore.
+  if(CMAKE_VERSION VERSION_EQUAL 3.0 OR CMAKE_VERSION VERSION_GREATER 3.0)
+      set(install_source_file $<TARGET_FILE:${targetName}>)
+  else()
+      get_property(install_source_file TARGET ${targetName} PROPERTY LOCATION)
+  endif()
+
+  install(PROGRAMS ${install_source_file} DESTINATION .)
+  if(WIN32)
+      install(FILES ${CMAKE_CURRENT_BINARY_DIR}/$<CONFIG>/${targetName}.pdb DESTINATION PDB)
+  else()
+      install(FILES ${strip_destination_file} DESTINATION .)
+  endif()
+endfunction()
+
 # Includes
 
 if (CMAKE_CONFIGURATION_TYPES) # multi-configuration generator?
index f4b157b2c71a50fe7012f379cfee7da42d9c3aa5..77d929d69b19f25fed7922e9f37085162865a20f 100644 (file)
@@ -146,9 +146,8 @@ add_dependencies(sos mscordaccore)
 target_link_libraries(sos ${SOS_LIBRARY})
 
 # add the install targets
-install (TARGETS sos DESTINATION .)
-if(WIN32)
-  install (FILES ${CMAKE_CURRENT_BINARY_DIR}/$<CONFIG>/sos.pdb DESTINATION PDB)
-else(WIN32)
-  install (FILES sosdocsunix.txt DESTINATION .)
-endif(WIN32)
+install_clr(sos)
+
+if(NOT WIN32)
+  install(FILES sosdocsunix.txt DESTINATION .)
+endif(NOT WIN32)
index 90fbcbd4e117eb8a9b8428cd9e1a2f94233e13fa..438ae33478ed3a7c29cca98cd747f2e33052da82 100644 (file)
@@ -93,4 +93,4 @@ if (CLR_CMAKE_PLATFORM_UNIX)
 endif()
 
 # add the install targets
-install (TARGETS sosplugin DESTINATION .)
+install_clr(sosplugin)
\ No newline at end of file
index 8848ff7d1a3b21a5e1b6e80dca7f94c0d919232e..634fdb77d640ed2b107a5e4b5ab818c898a74e5c 100644 (file)
@@ -27,8 +27,6 @@ else()
     )
 
     # Can't compile on linux yet so only add for windows
-    # add the install targets
-    install (TARGETS CoreConsole DESTINATION .)
-    install (FILES ${CMAKE_CURRENT_BINARY_DIR}/$ENV{__BuildType}/CoreConsole.pdb DESTINATION PDB)
+    install_clr(CoreConsole)
 
 endif(CLR_CMAKE_PLATFORM_UNIX)
\ No newline at end of file
index 3a992ebcf58212f68c240eb6daa1a4aa4ea0b08d..7b25c12d6b306ad1d3810c54a2d8eb3c1a4aa090 100644 (file)
@@ -32,10 +32,6 @@ else()
     )
 
     # Can't compile on linux yet so only add for windows
-    # add the install targets
-    install (TARGETS CoreRun DESTINATION .)
+    install_clr(CoreRun)
     
-    # We will generate PDB only for the debug configuration
-    install (FILES ${CMAKE_CURRENT_BINARY_DIR}/$<CONFIG>/CoreRun.pdb DESTINATION PDB)
-
 endif(CLR_CMAKE_PLATFORM_UNIX)
\ No newline at end of file
index 9c8fd7a2756ef67c1f06c0e72761f0aadf80dd66..2ccc881dbeccdac0d77fa5844d3375d404b1506e 100644 (file)
@@ -21,4 +21,4 @@ add_dependencies(osxbundlerun
     coreclr
 )
 
-install (TARGETS osxbundlerun DESTINATION .)
+install_clr(osxbundlerun)
index f4840edf38cec48d3e0266a0ff5c5ebcf5905d31..cb18e82776ac0e8000d5e92c333ad4628740adc9 100644 (file)
@@ -30,4 +30,4 @@ add_dependencies(coreconsole
     coreclr
 )
 
-install (TARGETS coreconsole DESTINATION .)
+install_clr(coreconsole)
\ No newline at end of file
index 4563ba9938fc29f794d94abaaa9ea3ed0ffcef7a..1f0c75995e964196efdfae7d18e539b9eb671ac8 100644 (file)
@@ -30,4 +30,4 @@ add_dependencies(corerun
     coreclr
 )
 
-install (TARGETS corerun DESTINATION .)
+install_clr(corerun)
\ No newline at end of file
index 879dc5bc185ca42680bf7a7793a4e4d5957d530c..bf279efe6aad97a928bc0f2e2b3522d47b6ca475 100644 (file)
@@ -73,4 +73,5 @@ else()
     add_definitions(-DU_DISABLE_RENAMING=1)
 endif()
 
-install (TARGETS System.Globalization.Native DESTINATION .)
+# add the install targets
+install_clr(System.Globalization.Native)
\ No newline at end of file
index 36205b5fe12a90225dea37bc2ad9543707c88e34..b1f7a490888e04f5fef5f249c4deaf149706eea9 100644 (file)
@@ -20,8 +20,4 @@ add_library_clr(clretwrc SHARED
 )
 
 # add the install targets
-install (TARGETS clretwrc DESTINATION .)
-
-if(WIN32)
-    install (FILES ${CMAKE_CURRENT_BINARY_DIR}/$<CONFIG>/clretwrc.pdb DESTINATION PDB)
-endif(WIN32)
+install_clr(clretwrc)
\ No newline at end of file
index 655cb0a28be0bad7e796f161d0393a4539a06e1e..c3ebaf5d062253274eb508493bf7b500fee07d42 100644 (file)
@@ -71,7 +71,4 @@ endif(WIN32)
 target_link_libraries(dbgshim ${DBGSHIM_LIBRARIES})
 
 # add the install targets
-install (TARGETS dbgshim DESTINATION .)
-if(WIN32)
-    install (FILES ${CMAKE_CURRENT_BINARY_DIR}/$<CONFIG>/dbgshim.pdb DESTINATION PDB)
-endif(WIN32)
+install_clr(dbgshim)
index ea655e77b82cf49269c7d4e3683a69af6e557ed0..9a28479fedad6b5bedd6fc19947641ce09507c33 100644 (file)
@@ -106,7 +106,4 @@ endif(WIN32)
 target_link_libraries(mscordaccore ${COREDAC_LIBRARIES})
 
 # add the install targets
-install (TARGETS mscordaccore DESTINATION .)
-if(WIN32)
-    install (FILES ${CMAKE_CURRENT_BINARY_DIR}/$<CONFIG>/mscordaccore.pdb DESTINATION PDB)
-endif(WIN32)
+install_clr(mscordaccore)
\ No newline at end of file
index 0c8a685b76689c53bca349225bf82da6ac7aea4b..2748bdf4a28a3d2cf304c06e25938ed7aea58e14 100644 (file)
@@ -95,7 +95,4 @@ elseif(CLR_CMAKE_PLATFORM_UNIX)
 endif(WIN32)
 
 # add the install targets
-install (TARGETS mscordbi DESTINATION .)
-if(WIN32)
-    install (FILES ${CMAKE_CURRENT_BINARY_DIR}/$<CONFIG>/mscordbi.pdb DESTINATION PDB)
-endif(WIN32)
+install_clr(mscordbi)
\ No newline at end of file
index 2c57fb630eacc154e153d28acaa63fc2de95b1f3..78ec166065dad49fb1e31e52e17243ec52b88506 100644 (file)
@@ -140,7 +140,8 @@ if(WIN32)
         clr_unknown_arch()
     endif()
 
-    add_custom_command(TARGET coreclr
+    add_custom_command(
+        TARGET coreclr
         POST_BUILD
         COMMAND ${CMAKE_CXX_COMPILER} /P /EP /TP ${PREPROCESS_DEFINITIONS} ${INC_DIR} /Fi${CMAKE_CURRENT_BINARY_DIR}/daccess.i  ${CLR_DIR}/src/debug/daccess/daccess.cpp
         COMMAND ${BuildToolsDir}/dactablegen.exe /dac:${CMAKE_CURRENT_BINARY_DIR}/daccess.i /pdb:${CMAKE_CURRENT_BINARY_DIR}/$<CONFIG>/coreclr.pdb /dll:$<TARGET_FILE:coreclr> /bin:${CMAKE_CURRENT_BINARY_DIR}/wks.bin
@@ -160,7 +161,4 @@ else()
 endif(WIN32)
 
 # add the install targets
-install (TARGETS coreclr DESTINATION .)
-if(WIN32)
-    install (FILES ${CMAKE_CURRENT_BINARY_DIR}/$<CONFIG>/coreclr.pdb DESTINATION PDB)
-endif(WIN32)
+install_clr(coreclr)
\ No newline at end of file
index 4c5a7f63a969c81f5dadc0b9af13eed167bffa86..e6828e7a49bcb68775b2c26c1b8f69d5879b37db 100644 (file)
@@ -66,7 +66,6 @@ if(CLR_CMAKE_PLATFORM_UNIX)
       dl
     )
   endif(NOT CMAKE_SYSTEM_NAME STREQUAL FreeBSD AND NOT CMAKE_SYSTEM_NAME STREQUAL NetBSD)
-
 else()
   target_link_libraries(ilasm
     ${ILASM_LINK_LIBRARIES}
@@ -76,8 +75,6 @@ else()
     oleaut32
     shell32
   )
-
-  install (FILES ${CMAKE_CURRENT_BINARY_DIR}/$<CONFIG>/ilasm.pdb DESTINATION PDB)
 endif(CLR_CMAKE_PLATFORM_UNIX)
 
-install (TARGETS ilasm DESTINATION .)
+install_clr(ilasm)
\ No newline at end of file
index 8a57044ce6fa4faa8a4210f726df1414f61d8b10..8b468f07a41df3807e11b58ee78050d700311e50 100644 (file)
@@ -69,10 +69,6 @@ else()
         oleaut32
         shell32
     )
-
-    # We will generate PDB only for the debug configuration
-    install (FILES ${CMAKE_CURRENT_BINARY_DIR}/$<CONFIG>/ildasm.pdb DESTINATION PDB)
-
 endif(CLR_CMAKE_PLATFORM_UNIX)
 
-install (TARGETS ildasm DESTINATION .)
+install_clr(ildasm)
\ No newline at end of file
index ba987b409b5d560f78dc3f89ad7c5b145284981a..80e9a0e8e9167aa9cfb9c36b3bcfe4c0505127f5 100644 (file)
@@ -47,7 +47,4 @@ target_link_libraries(ryujit
 )
 
 # add the install targets
-install (TARGETS ryujit DESTINATION .)
-if(WIN32)
-    install (FILES ${CMAKE_CURRENT_BINARY_DIR}/$<CONFIG>/ryujit.pdb DESTINATION PDB)
-endif(WIN32)
+install_clr(ryujit)
\ No newline at end of file
index 7befa6136ed868aa01b38e8dfe4a8fe2c9c6bd92..d343b122ff756fcd1a1173cbc71202885b144b5f 100644 (file)
@@ -444,7 +444,7 @@ def generateLttngFiles(etwmanifest,eventprovider_directory):
     add_subdirectory(tracepointprovider)
 
     # Install the static eventprovider library
-    install (TARGETS eventprovider DESTINATION lib)
+    install(TARGETS eventprovider DESTINATION lib)
     """)
     topCmake.close()
 
@@ -481,11 +481,11 @@ def generateLttngFiles(etwmanifest,eventprovider_directory):
     tracepointprovider_Cmake.write("""    )
 
     target_link_libraries(coreclrtraceptprovider
-                         -llttng-ust
+                          -llttng-ust
     )
 
-   #Install the static coreclrtraceptprovider library
-   install (TARGETS coreclrtraceptprovider DESTINATION .)
+    # Install the static coreclrtraceptprovider library
+    install_clr(coreclrtraceptprovider)
    """)
     tracepointprovider_Cmake.close()
 
index 9bbf37334a001cb55f57a0e03f0a0c327c409450..d5e956620f132049ab0bf5074061e884a0f38ee4 100644 (file)
@@ -64,14 +64,11 @@ else()
     if (NOT CLR_CMAKE_PLATFORM_ARCH_ARM64)
         target_link_libraries(crossgen ${STATIC_MT_VCRT_LIB})
     endif()
-
-    # We will generate PDB only for the debug configuration
-    install (FILES ${CMAKE_CURRENT_BINARY_DIR}/$<CONFIG>/crossgen.pdb DESTINATION PDB)
-
 endif(CLR_CMAKE_PLATFORM_UNIX)
 
-install (TARGETS crossgen DESTINATION .)
-
 add_subdirectory(../../zap/crossgen ../../zap/crossgen)
 add_subdirectory(../../vm/crossgen ../../vm/crossgen)
 add_subdirectory(../../vm/crossgen_mscorlib ../../vm/crossgen_mscorlib)
+
+# add the install targets
+install_clr(crossgen)
\ No newline at end of file