1# Copyright (c) 2009 Sun Microsystems, Inc.
2# Use is subject to license terms.
3#
4# This program is free software; you can redistribute it and/or modify
5# it under the terms of the GNU General Public License as published by
6# the Free Software Foundation; version 2 of the License.
7#
8# This program is distributed in the hope that it will be useful,
9# but WITHOUT ANY WARRANTY; without even the implied warranty of
10# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11# GNU General Public License for more details.
12#
13# You should have received a copy of the GNU General Public License
14# along with this program; if not, write to the Free Software
15# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1335  USA
16
17FILE(REMOVE "${TARGET_LOCATION}")
18
19SET(TEMP_DIR ${CMAKE_CURRENT_BINARY_DIR}/merge_archives_${TARGET})
20MAKE_DIRECTORY(${TEMP_DIR})
21# Extract each archive to its own subdirectory(avoid object filename clashes)
22SEPARATE_ARGUMENTS(STATIC_LIBS UNIX_COMMAND "${STATIC_LIBS}")
23FOREACH(LIB ${STATIC_LIBS})
24  GET_FILENAME_COMPONENT(NAME_NO_EXT ${LIB} NAME_WE)
25  SET(TEMP_SUBDIR ${TEMP_DIR}/${NAME_NO_EXT})
26  MAKE_DIRECTORY(${TEMP_SUBDIR})
27  EXECUTE_PROCESS(
28    COMMAND ${CMAKE_AR} -x ${LIB}
29    WORKING_DIRECTORY ${TEMP_SUBDIR}
30  )
31
32  FILE(GLOB_RECURSE LIB_OBJECTS "${TEMP_SUBDIR}/*")
33  SET(OBJECTS ${OBJECTS} ${LIB_OBJECTS})
34ENDFOREACH()
35
36# Use relative paths, makes command line shorter.
37GET_FILENAME_COMPONENT(ABS_TEMP_DIR ${TEMP_DIR} ABSOLUTE)
38FOREACH(OBJ ${OBJECTS})
39  FILE(RELATIVE_PATH OBJ ${ABS_TEMP_DIR} ${OBJ})
40  FILE(TO_NATIVE_PATH ${OBJ} OBJ)
41  SET(ALL_OBJECTS ${ALL_OBJECTS} ${OBJ})
42ENDFOREACH()
43
44FILE(TO_NATIVE_PATH ${TARGET_LOCATION} ${TARGET_LOCATION})
45# Now pack the objects into library with ar.
46EXECUTE_PROCESS(
47  COMMAND ${CMAKE_AR} -r ${TARGET_LOCATION} ${ALL_OBJECTS}
48  WORKING_DIRECTORY ${TEMP_DIR}
49)
50EXECUTE_PROCESS(
51  COMMAND ${CMAKE_RANLIB} ${TARGET_LOCATION}
52  WORKING_DIRECTORY ${TEMP_DIR}
53)
54
55# Cleanup
56FILE(REMOVE_RECURSE ${TEMP_DIR})
57