1 # Copyright (C) 2012 Modelon AB
\r
3 # This program is free software: you can redistribute it and/or modify
\r
4 # it under the terms of the BSD style license.
\r
6 # This program is distributed in the hope that it will be useful,
\r
7 # but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
8 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
9 # FMILIB_License.txt file for more details.
\r
11 # You should have received a copy of the FMILIB_License.txt file
\r
12 # along with this program. If not, contact Modelon AB <http://www.modelon.com>.
\r
14 # Merge_static_libs(outlib lib1 lib2 ... libn) merges a number of static
\r
15 # libs into a single static library
\r
16 function(merge_static_libs outlib )
\r
18 list(REMOVE_AT libs 0)
\r
19 # Create a dummy file that the target will depend on
\r
20 set(dummyfile ${CMAKE_CURRENT_BINARY_DIR}/${outlib}_dummy.c)
\r
21 file(WRITE ${dummyfile} "const char * dummy = \"${dummyfile}\";")
\r
23 add_library(${outlib} STATIC ${dummyfile})
\r
25 if("${CMAKE_CFG_INTDIR}" STREQUAL ".")
\r
26 set(multiconfig FALSE)
\r
28 set(multiconfig TRUE)
\r
31 # First get the file names of the libraries to be merged
\r
32 foreach(lib ${libs})
\r
33 get_target_property(libtype ${lib} TYPE)
\r
34 if(NOT libtype STREQUAL "STATIC_LIBRARY")
\r
35 message(FATAL_ERROR "Merge_static_libs can only process static libraries")
\r
38 foreach(CONFIG_TYPE ${CMAKE_CONFIGURATION_TYPES})
\r
39 get_target_property("libfile_${CONFIG_TYPE}" ${lib} "LOCATION_${CONFIG_TYPE}")
\r
40 list(APPEND libfiles_${CONFIG_TYPE} ${libfile_${CONFIG_TYPE}})
\r
43 get_target_property(libfile ${lib} LOCATION)
\r
44 list(APPEND libfiles "${libfile}")
\r
47 message(STATUS "will be merging ${libfiles}")
\r
48 # Just to be sure: cleanup from duplicates
\r
50 foreach(CONFIG_TYPE ${CMAKE_CONFIGURATION_TYPES})
\r
51 list(REMOVE_DUPLICATES libfiles_${CONFIG_TYPE})
\r
52 set(libfiles ${libfiles} ${libfiles_${CONFIG_TYPE}})
\r
55 list(REMOVE_DUPLICATES libfiles)
\r
57 # Now the easy part for MSVC and for MAC
\r
59 # lib.exe does the merging of libraries just need to conver the list into string
\r
60 foreach(CONFIG_TYPE ${CMAKE_CONFIGURATION_TYPES})
\r
62 foreach(lib ${libfiles_${CONFIG_TYPE}})
\r
63 set(flags "${flags} ${lib}")
\r
65 string(TOUPPER "STATIC_LIBRARY_FLAGS_${CONFIG_TYPE}" PROPNAME)
\r
66 set_target_properties(${outlib} PROPERTIES ${PROPNAME} "${flags}")
\r
70 # Use OSX's libtool to merge archives
\r
72 message(FATAL_ERROR "Multiple configurations are not supported")
\r
74 get_target_property(outfile ${outlib} LOCATION)
\r
75 add_custom_command(TARGET ${outlib} POST_BUILD
\r
76 COMMAND rm ${outfile}
\r
77 COMMAND /usr/bin/libtool -static -o ${outfile}
\r
81 # general UNIX - need to "ar -x" and then "ar -ru"
\r
83 message(FATAL_ERROR "Multiple configurations are not supported")
\r
85 get_target_property(outfile ${outlib} LOCATION)
\r
86 message(STATUS "outfile location is ${outfile}")
\r
87 foreach(lib ${libfiles})
\r
88 # objlistfile will contain the list of object files for the library
\r
89 set(objlistfile ${lib}.objlist)
\r
90 set(objdir ${lib}.objdir)
\r
91 set(objlistcmake ${objlistfile}.cmake)
\r
92 # we only need to extract files once
\r
93 if(${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/cmake.check_cache IS_NEWER_THAN ${objlistcmake})
\r
94 #---------------------------------
\r
95 FILE(WRITE ${objlistcmake}
\r
96 "# Extract object files from the library
\r
97 message(STATUS \"Extracting object files from ${lib}\")
\r
98 EXECUTE_PROCESS(COMMAND ${CMAKE_AR} -x ${lib}
\r
99 WORKING_DIRECTORY ${objdir})
\r
100 # save the list of object files
\r
101 EXECUTE_PROCESS(COMMAND ls .
\r
102 OUTPUT_FILE ${objlistfile}
\r
103 WORKING_DIRECTORY ${objdir})")
\r
104 #---------------------------------
\r
105 file(MAKE_DIRECTORY ${objdir})
\r
106 add_custom_command(
\r
107 OUTPUT ${objlistfile}
\r
108 COMMAND ${CMAKE_COMMAND} -P ${objlistcmake}
\r
111 list(APPEND extrafiles "${objlistfile}")
\r
112 # relative path is needed by ar under MSYS
\r
113 file(RELATIVE_PATH objlistfilerpath ${objdir} ${objlistfile})
\r
114 add_custom_command(TARGET ${outlib} POST_BUILD
\r
115 COMMAND ${CMAKE_COMMAND} -E echo "Running: ${CMAKE_AR} ru ${outfile} @${objlistfilerpath}"
\r
116 COMMAND ${CMAKE_AR} ru "${outfile}" @"${objlistfilerpath}"
\r
117 WORKING_DIRECTORY ${objdir})
\r
119 add_custom_command(TARGET ${outlib} POST_BUILD
\r
120 COMMAND ${CMAKE_COMMAND} -E echo "Running: ${CMAKE_RANLIB} ${outfile}"
\r
121 COMMAND ${CMAKE_RANLIB} ${outfile})
\r
123 file(WRITE ${dummyfile}.base "const char* ${outlib}_sublibs=\"${libs}\";")
\r
124 add_custom_command(
\r
125 OUTPUT ${dummyfile}
\r
126 COMMAND ${CMAKE_COMMAND} -E copy ${dummyfile}.base ${dummyfile}
\r
127 DEPENDS ${libs} ${extrafiles})
\r