From 83dd9bb266d3784e5459d37bb16b287af7e6887d Mon Sep 17 00:00:00 2001 From: Gavin Howard Date: Thu, 7 Dec 2017 18:56:57 -0700 Subject: [PATCH] Use the lib create funcs to help with merging We'll see if this removes the problem. --- functions.cmake | 92 +++++++++++++++++++++++++++++++++---------------- 1 file changed, 62 insertions(+), 30 deletions(-) diff --git a/functions.cmake b/functions.cmake index e636884..76320eb 100644 --- a/functions.cmake +++ b/functions.cmake @@ -40,6 +40,7 @@ # Make sure we save this. set(SCRIPT_DIR "${CMAKE_CURRENT_SOURCE_DIR}" CACHE STRING "Directory of the merge script") +set(MERGE_LIBS "" CACHE STRING "List of libraries to merge") # For some reason, we get stupid CMake warnings. cmake_policy(SET CMP0026 NEW) @@ -56,40 +57,45 @@ function(merge_static_libs outlib) file(WRITE ${dummyfile} "// ${dummyfile}") add_library(${outlib} STATIC ${dummyfile}) + get_target_property(target_prefix "${outlib}" PREFIX) + get_target_property(target_suffix "${outlib}" SUFFIX) + set(outfile "${CMAKE_CURRENT_BINARY_DIR}/${target_prefix}${outlib}${target_suffix}") - if("${CMAKE_CFG_INTDIR}" STREQUAL ".") - set(multiconfig FALSE) - else() - set(multiconfig TRUE) - endif() - - # First get the file names of the libraries to be merged - foreach(lib ${libs}) + # Make sure all libs are static. + foreach(lib ${MERGE_LIBS}) get_target_property(libtype ${lib} TYPE) if(NOT libtype STREQUAL "STATIC_LIBRARY") message(FATAL_ERROR "Merge_static_libs can only process static libraries") endif() - if(multiconfig) + endforeach() + + # If we are on a multiconfig build system... + if(NOT"${CMAKE_CFG_INTDIR}" STREQUAL ".") + + # Generate a list for each config type. + foreach(lib ${MERGE_LIBS}) foreach(CONFIG_TYPE ${CMAKE_CONFIGURATION_TYPES}) get_target_property("libfile_${CONFIG_TYPE}" ${lib} "LOCATION_${CONFIG_TYPE}") list(APPEND libfiles_${CONFIG_TYPE} ${libfile_${CONFIG_TYPE}}) endforeach() - else() - set(libfile "$") - list(APPEND libfiles "${libfile}") - endif(multiconfig) - endforeach() - message(STATUS "will be merging ${libfiles}") + endforeach() - # Just to be sure: cleanup from duplicates - if(multiconfig) + # Dedup the lists. foreach(CONFIG_TYPE ${CMAKE_CONFIGURATION_TYPES}) list(REMOVE_DUPLICATES libfiles_${CONFIG_TYPE}) set(libfiles ${libfiles} ${libfiles_${CONFIG_TYPE}}) endforeach() + endif() + + # Rename. + set(libfiles "${MERGE_LIBS}") + + # Just to be sure: cleanup from duplicates list(REMOVE_DUPLICATES libfiles) + message(STATUS "will be merging ${libfiles}") + # Now the easy part for MSVC and for MAC if(MSVC) # lib.exe does the merging of libraries just need to conver the list into string @@ -103,26 +109,35 @@ function(merge_static_libs outlib) endforeach() elseif(APPLE) - # Use OSX's libtool to merge archives + + # Use OSX's libtool to merge archives. + + # We can't handle multiconfigs. if(multiconfig) message(FATAL_ERROR "Multiple configurations are not supported") endif() + get_target_property(outfile ${outlib} LOCATION) add_custom_command(TARGET ${outlib} POST_BUILD - COMMAND rm $ - COMMAND /usr/bin/libtool -static -o $ - ${libfiles}) + COMMAND rm ${outfile} + COMMAND /usr/bin/libtool -static -o ${outfile} ${libfiles}) + else() # general UNIX - need to "ar -x" and then "ar -ru" + + # We can't handle multiconfigs. if(multiconfig) message(FATAL_ERROR "Multiple configurations are not supported") endif() - set(outfile $) + + # Loop over the files. foreach(lib ${libfiles}) - # objlistfile will contain the list of object files for the library + + # objlistfile will contain the list of object files for the library. set(objlistfile ${lib}.objlist) set(objdir ${lib}.objdir) set(objlistcmake ${objlistfile}.cmake) + # we only need to extract files once if(${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/cmake.check_cache IS_NEWER_THAN ${objlistcmake}) #--------------------------------- @@ -144,17 +159,18 @@ function(merge_static_libs outlib) DEPENDS ${lib}) endif() list(APPEND extrafiles "${objlistfile}") + # relative path is needed by ar under MSYS file(RELATIVE_PATH objlistfilerpath ${objdir} ${objlistfile}) add_custom_command(TARGET ${outlib} POST_BUILD - COMMAND ${CMAKE_COMMAND} -E echo "Running: ${CMAKE_AR} ru $ @${objlistfilerpath}" - COMMAND ${CMAKE_AR} ru "$" @"${objlistfilerpath}" + COMMAND ${CMAKE_COMMAND} -E echo "Running: ${CMAKE_AR} ru ${outfile} @${objlistfilerpath}" + COMMAND ${CMAKE_AR} ru "${outfile}" @"${objlistfilerpath}" WORKING_DIRECTORY ${objdir}) endforeach() add_custom_command(TARGET ${outlib} POST_BUILD - COMMAND ${CMAKE_COMMAND} -E echo "Running: ${CMAKE_RANLIB} $" - COMMAND ${CMAKE_RANLIB} $) + COMMAND ${CMAKE_COMMAND} -E echo "Running: ${CMAKE_RANLIB} ${outfile}" + COMMAND ${CMAKE_RANLIB} ${outfile}) endif() file(WRITE ${dummyfile}.base "const char* ${outlib}_sublibs=\"${libs}\";") add_custom_command( @@ -164,6 +180,14 @@ function(merge_static_libs outlib) endfunction() +function(merge_lib name output) + + get_target_property(target_prefix "${name}" PREFIX) + get_target_property(target_suffix "${name}" SUFFIX) + list(APPEND MERGE_LIBS + "${CMAKE_CURRENT_BINARY_DIR}/${target_prefix}${output}${target_suffix}") + +endfunction(merge_lib) function(create_test target) @@ -188,7 +212,7 @@ function(create_shared_library name output_name src doinstall) endfunction(create_shared_library) -function(create_static_library name output_name src doinstall) +function(create_static_library name output_name src doinstall domerge) add_library("${name}" STATIC "${src}") target_link_libraries("${name}" "${ARGN}") @@ -201,6 +225,10 @@ function(create_static_library name output_name src doinstall) install(TARGETS "${name}" ARCHIVE DESTINATION lib/) endif(doinstall) + if(domerge) + merge_lib("${name}" "${output_name}") + endif(domerge) + endfunction(create_static_library) function(create_pic_library name output_name src doinstall) @@ -217,13 +245,17 @@ function(create_pic_library name output_name src doinstall) install(TARGETS "${name}" ARCHIVE DESTINATION lib/) endif(doinstall) + if(domerge) + merge_lib("${name}" "${output_name}") + endif(domerge) + endfunction(create_pic_library) function(create_all_libraries shared_name static_name pic_name output_name src doinstall) create_shared_library("${shared_name}" "${output_name}" "${src}" "${doinstall}" "${ARGN}") - create_static_library("${static_name}" "${output_name}" "${src}" "${doinstall}" "${ARGN}") - create_pic_library("${pic_name}" "${output_name}_pic" "${src}" "${doinstall}" "${ARGN}") + create_static_library("${static_name}" "${output_name}" "${src}" "${doinstall}" NO "${ARGN}") + create_pic_library("${pic_name}" "${output_name}_pic" "${src}" "${doinstall}" NO "${ARGN}") endfunction(create_all_libraries)