35
35
# Includes all transitive dependencies of the libraries being merged.
36
36
#
37
37
38
- get_filename_component (LIBUTILS_SCRIPT_DIR ${CMAKE_CURRENT_LIST_FILE} PATH )
39
- set (LIBUTILS_SCRIPT_DIR "${LIBUTILS_SCRIPT_DIR} /libutils" )
38
+ # Include this script only once.
40
39
41
- #
42
- # Locate required tools.
43
- #
40
+ if ( COMMAND libutils_setup )
41
+ return ()
42
+ endif ()
44
43
45
- if (CMAKE_BUILD_TOOL MATCHES "MSBuild" )
46
44
47
- set ( MSBUILD ON )
45
+ macro ( libutils_setup )
48
46
49
- # Use lib.exe from the same location as other compiler tools
47
+ get_filename_component (LIBUTILS_SCRIPT_DIR ${CMAKE_CURRENT_LIST_FILE} PATH )
48
+ set (LIBUTILS_SCRIPT_DIR "${LIBUTILS_SCRIPT_DIR} /libutils" )
49
+ set (LIBUTILS_BIN_DIR "${CMAKE_CURRENT_BINARY_DIR} /libutils" CACHE INTERNAL "" )
50
50
51
- get_filename_component (path "${CMAKE_LINKER} " DIRECTORY )
52
- set (LIB_TOOL "${path} /lib.exe" )
51
+ #
52
+ # Locate required tools.
53
+ #
53
54
54
- endif ( )
55
+ if ( CMAKE_BUILD_TOOL MATCHES "MSBuild" )
55
56
57
+ set (MSBUILD ON )
56
58
57
- if ( APPLE )
59
+ # Use lib.exe from the same location as other compiler tools
58
60
59
- find_program (LIB_TOOL libtool )
61
+ get_filename_component (path "${CMAKE_LINKER} " DIRECTORY )
62
+ set (LIB_TOOL "${path} /lib.exe" )
60
63
61
- # We need install_name_tool to do rpath mangling (see below )
64
+ endif ( )
62
65
63
- find_program (INSTALL_NAME_TOOL install_name_tool )
64
66
65
- # If available, otool is used to show runtime dependencies for libraries we
66
- # build
67
+ if (APPLE )
67
68
68
- find_program (OTOOL otool )
69
+ find_program (LIB_TOOL libtool )
69
70
70
- endif ( )
71
+ # We need install_name_tool to do rpath mangling (see below )
71
72
73
+ find_program (INSTALL_NAME_TOOL install_name_tool )
72
74
73
- #
74
- # Infrastructure for merging static libraries
75
- # ===========================================
76
- #
77
- # It is used to merge a static library with all other static libraries
78
- # on which it depends, so that, when using the merged library, one does
79
- # not have to worry about dependencies.
80
- #
81
- # The main logic for mering static libraries on different platforms is
82
- # in the merge_archives.cmake script. Calling merge_static_library() on
83
- # a library target arranges for this script to be called with all required
84
- # parameters every time the library is (re-)built.
85
- #
86
- # Extra effort is needed to get the list of all dependencies of the library.
87
- # These dependencies are computed by cmake, but there is no easy way to
88
- # get them out of cmake. We use the trick with custom language linker. Hovewer,
89
- # it does not work with MSBuild generator where we do other tricks. In either
90
- # case the idea is to define a phony target that depends on the static library
91
- # and capture link options that cmake uses to build this phony target.
92
- #
75
+ # If available, otool is used to show runtime dependencies for libraries we
76
+ # build
93
77
94
- #
95
- # Create merge script from template, setting required internal variables in it.
96
- #
97
- # TODO: This will work only if this file is included in the same folder in which
98
- # static libraries are linked.
99
- #
78
+ find_program (OTOOL otool )
100
79
101
- configure_file (
102
- ${LIBUTILS_SCRIPT_DIR} /merge_archives.cmake.in
103
- ${CMAKE_CURRENT_BINARY_DIR} /merge_archives.cmake
104
- @ONLY
105
- )
80
+ endif ()
106
81
107
- #
108
- # This small program saves in a file all command line options that
109
- # were passed to it. It is used to capture linker invocation options.
110
- #
111
82
112
- if (NOT MSBUILD AND NOT TARGET save_linker_opts )
113
- add_executable (save_linker_opts ${LIBUTILS_SCRIPT_DIR} /save_linker_opts.cc )
114
- set_property (TARGET save_linker_opts PROPERTY
115
- OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}
83
+ #
84
+ # Infrastructure for merging static libraries
85
+ # ===========================================
86
+ #
87
+ # It is used to merge a static library with all other static libraries
88
+ # on which it depends, so that, when using the merged library, one does
89
+ # not have to worry about dependencies.
90
+ #
91
+ # The main logic for mering static libraries on different platforms is
92
+ # in the merge_archives.cmake script. Calling merge_static_library() on
93
+ # a library target arranges for this script to be called with all required
94
+ # parameters every time the library is (re-)built.
95
+ #
96
+ # Extra effort is needed to get the list of all dependencies of the library.
97
+ # These dependencies are computed by cmake, but there is no easy way to
98
+ # get them out of cmake. We use the trick with custom language linker. Hovewer,
99
+ # it does not work with MSBuild generator where we do other tricks. In either
100
+ # case the idea is to define a phony target that depends on the static library
101
+ # and capture link options that cmake uses to build this phony target.
102
+ #
103
+
104
+ #
105
+ # Create merge script from template, setting required internal variables in it.
106
+ #
107
+
108
+ configure_file (
109
+ ${LIBUTILS_SCRIPT_DIR} /merge_archives.cmake.in
110
+ ${LIBUTILS_BIN_DIR} /merge_archives.cmake
111
+ @ONLY
116
112
)
117
- endif ()
113
+
114
+ #
115
+ # This small program saves in a file all command line options that
116
+ # were passed to it. It is used to capture linker invocation options.
117
+ #
118
+
119
+ if (NOT MSBUILD AND NOT TARGET save_linker_opts )
120
+ add_executable (save_linker_opts ${LIBUTILS_SCRIPT_DIR} /save_linker_opts.cc )
121
+ set_property (TARGET save_linker_opts PROPERTY
122
+ RUNTIME_OUTPUT_DIRECTORY ${LIBUTILS_BIN_DIR}
123
+ )
124
+ endif ()
125
+
126
+ endmacro (libutils_setup )
127
+
128
+ libutils_setup ()
118
129
119
130
#
120
- # Merge static libraries into single static or shared library.
131
+ # Merge static libraries into a single static or shared library.
121
132
#
122
133
# Given a static library target, this function sets up an infrastructure
123
134
# for merging this static libraray with its dependencies. It creates
@@ -185,7 +196,7 @@ function(merge_libraries TARGET)
185
196
-DMSBUILD=${MSBUILD}
186
197
-DINFO=${INFO}
187
198
-DINFO_PREFIX=${INFO_PREFIX}
188
- -P merge_archives.cmake
199
+ -P ${LIBUTILS_BIN_DIR} / merge_archives.cmake
189
200
)
190
201
191
202
endif ()
@@ -218,7 +229,7 @@ function(merge_libraries TARGET)
218
229
219
230
add_dependencies (${TARGET} -deps save_linker_opts )
220
231
set_target_properties (${TARGET} -deps PROPERTIES
221
- RULE_LAUNCH_LINK "${CMAKE_BINARY_DIR } /save_linker_opts ${log_file} .STATIC "
232
+ RULE_LAUNCH_LINK "${LIBUTILS_BIN_DIR } /save_linker_opts ${log_file} .STATIC "
222
233
)
223
234
224
235
# Arrange for ${TARGET}-deps to be built before ${TARGET}
@@ -236,7 +247,7 @@ function(merge_libraries TARGET)
236
247
#
237
248
238
249
set_target_properties (${TARGET} PROPERTIES
239
- RULE_LAUNCH_LINK "${CMAKE_BINARY_DIR } /save_linker_opts ${log_file} .SHARED "
250
+ RULE_LAUNCH_LINK "${LIBUTILS_BIN_DIR } /save_linker_opts ${log_file} .SHARED "
240
251
)
241
252
242
253
else (NOT MSBUILD )
0 commit comments