How to avoid CMake writting full path of objects to the static library?

82 Views Asked by At

The original issue is here.

After a few days, I find the problem is here. I tried to use the following CMakeLists.txt to build tcmalloc_minimal_static.lib:

#...
# https://github.com/gperftools/gperftools/blob/master/CMakeLists.txt#L593
if(MINGW OR MSVC)
  set(WINDOWS_INCLUDES
#       ...
          src/windows/preamble_patcher.h)
  set(libwindows_la_SOURCES ${WINDOWS_INCLUDES}
#       ...
          src/windows/preamble_patcher_with_stub.cc)
  add_library(windows_object OBJECT ${libwindows_la_SOURCES})
  add_library(windows INTERFACE)
  target_sources(windows INTERFACE $<TARGET_OBJECTS:windows_object>)
  # patch_functions.cc uses Psapi.lib.  MSVC has a #pragma for that, but not us.
  target_link_libraries(windows INTERFACE psapi)
#...

# https://github.com/gperftools/gperftools/blob/master/CMakeLists.txt#L811
set(libtcmalloc_minimal_internal_la_SOURCES src/common.cc
#       ...
        ${TCMALLOC_MINIMAL_INCLUDES})
> add_library(tcmalloc_minimal_internal_object OBJECT ${libtcmalloc_minimal_internal_la_SOURCES})
# We #define NO_TCMALLOC_SAMPLES, since sampling is turned off for _minimal.
target_compile_definitions(tcmalloc_minimal_internal_object PRIVATE NO_TCMALLOC_SAMPLES NO_HEAP_CHECK NDEBUG)
add_library(tcmalloc_minimal_internal INTERFACE)
target_link_libraries(tcmalloc_minimal_internal INTERFACE ${LIBSPINLOCK} ${maybe_threads_lib})
target_sources(tcmalloc_minimal_internal INTERFACE $<TARGET_OBJECTS:tcmalloc_minimal_internal_object>)
#...

# https://github.com/gperftools/gperftools/blob/master/CMakeLists.txt#L847
if(GPERFTOOLS_BUILD_STATIC)
    add_library(tcmalloc_minimal_static STATIC ${libtcmalloc_minimal_internal_la_SOURCES})
    target_compile_definitions(tcmalloc_minimal_static PRIVATE NO_TCMALLOC_SAMPLES NDEBUG)
    target_link_libraries(tcmalloc_minimal_static PRIVATE tcmalloc_minimal_internal Threads::Threads)
    if(MINGW)
      target_link_libraries(tcmalloc_minimal_static PRIVATE stacktrace)
    endif()
    if(NOT MSVC)
      set_target_properties(tcmalloc_minimal_static PROPERTIES
              OUTPUT_NAME tcmalloc_minimal)
    endif()
    weaken_object(tcmalloc_minimal_static)
    install(TARGETS tcmalloc_minimal_static)
endif()

Then I got a wrong tcmalloc_minimal_static.lib. Opening by 7z.exe:

tcmalloc_minimal_static.lib
 - F:/
   - *full/path/to/my/project/*
     - build/Release/
       - _deps/
         - gperftools-build/
           - tcmalloc_minimal_internal_object.dir/
              - Release/
                - central_freelist.obj
                - ...
           - windows_object.dir/
              - Release/
                - ia32_modrm_map.obj
                - ...
 - tcmalloc_minimal_static.dir/
   - Release/
     - central_freelist.obj
     - ...
 - 1.txt
 - 2.txt

which is ridiculous. Then objcopy.exe can't read this library at all.

I tried to add -DCMAKE_SKIP_RPATH to cmake launching arguments. But it didn't work at all. Too many other issues are about shared library, but I'm facing to two static libraries. I just want to remove those long long absolute path in the library so that objcopy.exe could read it.

0

There are 0 best solutions below