forked from Slicer/Slicer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSlicerExtensionCPackBundleFixup.cmake.in
406 lines (348 loc) · 15.3 KB
/
SlicerExtensionCPackBundleFixup.cmake.in
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
include(@Slicer_CMAKE_DIR@/BundleUtilitiesWithRPath.cmake)
#include(BundleUtilities)
#
# Snanity checks
#
if(NOT EXISTS "${CMAKE_INSTALL_PREFIX}")
# If you are invoking that script from the command line, make sure to pass -DCMAKE_INSTALL_PREFIX:PATH=/path/to/installed/extension
message(FATAL_ERROR "CMAKE_INSTALL_PREFIX is set to a nonexistent directory :(")
endif()
#-----------------------------------------------------------------------------
# gp_log_message - Convenient function allowing to both display and log a message.
# Log file: $ENV{DESTDIR}/../complete-bundles-log.txt
#-----------------------------------------------------------------------------
function(gp_log_message text)
message(${text})
file(APPEND "@EXTENSION_BINARY_DIR@/complete-bundles-log.txt" "${text}
")
endfunction()
#-----------------------------------------------------------------------------
# gp_clear_log - Clear the log file
#-----------------------------------------------------------------------------
function(gp_clear_log)
file(WRITE "@EXTENSION_BINARY_DIR@/complete-bundles-log.txt" "")
gp_log_message("Log file: @EXTENSION_BINARY_DIR@/complete-bundles-log.txt")
endfunction()
#-----------------------------------------------------------------------------
# gp_item_default_embedded_path_override item default_embedded_path_var
#-----------------------------------------------------------------------------
# Return the path that others should refer to the item by when the item
# is embedded inside a bundle.
#
# This is a project-specific override of BundleUtilities.cmake's
# gp_item_default_embedded_path
#
function(gp_item_default_embedded_path_override item default_embedded_path_var)
# By default, embed items as set by gp_item_default_embedded_path:
set(path "${${default_embedded_path_var}}")
if(item MATCHES "[^/]+\\.framework/")
set(path "@fixup_path@/Frameworks")
endif()
if(item MATCHES "@CMAKE_BINARY_DIR@/.+\\.(dylib|so)$")
set(path "@fixup_path@/@Slicer_BUNDLE_EXTENSIONS_LOCATION@@Slicer_LIB_DIR@")
elseif(item MATCHES "\\.(dylib|so)$")
set(path "@fixup_path@/@Slicer_LIB_DIR@")
endif()
set(Slicer_USE_PYTHONQT "@Slicer_USE_PYTHONQT@")
if(Slicer_USE_PYTHONQT)
# Python library
if(item MATCHES "libpython[^/]+\\.dylib$")
set(path "@fixup_path@/lib/Python/lib")
endif()
# VTK python modules
if(item MATCHES "@CMAKE_BINARY_DIR@/.+/vtkmodules/[^/]+\\.so$")
set(path "@fixup_path@/@Slicer_BUNDLE_EXTENSIONS_LOCATION@bin/Python/vtkmodules")
elseif(item MATCHES "vtkmodules/[^/]+\\.so$")
set(path "@fixup_path@/bin/Python/vtkmodules")
endif()
endif()
set(Slicer_BUILD_CLI_SUPPORT "@Slicer_BUILD_CLI_SUPPORT@")
if(Slicer_BUILD_CLI_SUPPORT)
if(item MATCHES "@Slicer_BINARY_INNER_SUBDIR@/@Slicer_CLIMODULES_LIB_DIR@/[^/]+\\.dylib$")
set(path "@fixup_path@/@Slicer_CLIMODULES_LIB_DIR@")
elseif(item MATCHES "@Slicer_CLIMODULES_LIB_DIR@/[^/]+\\.dylib$")
set(path "@fixup_path@/@Slicer_BUNDLE_EXTENSIONS_LOCATION@@Slicer_CLIMODULES_LIB_DIR@")
endif()
endif()
set(Slicer_BUILD_QTLOADABLEMODULES "@Slicer_BUILD_QTLOADABLEMODULES@")
if(Slicer_BUILD_QTLOADABLEMODULES)
if(item MATCHES "@Slicer_BINARY_INNER_SUBDIR@/@Slicer_QTLOADABLEMODULES_LIB_DIR@/[^/]+\\.(so|dylib)$")
set(path "@fixup_path@/@Slicer_QTLOADABLEMODULES_LIB_DIR@")
elseif(item MATCHES "@Slicer_QTLOADABLEMODULES_LIB_DIR@/[^/]+\\.(so|dylib)$")
set(path "@fixup_path@/@Slicer_BUNDLE_EXTENSIONS_LOCATION@@Slicer_QTLOADABLEMODULES_LIB_DIR@")
endif()
endif()
if(item MATCHES "@Slicer_BINARY_INNER_SUBDIR@/@Slicer_THIRDPARTY_LIB_DIR@/[^/]+\\.(so|dylib)$")
# Ignore Slicer libraries. They are already expected to be fixed up.
elseif(item MATCHES "@Slicer_THIRDPARTY_LIB_DIR@/[^/]+\\.(so|dylib)$")
set(path "@fixup_path@/@Slicer_BUNDLE_EXTENSIONS_LOCATION@@Slicer_THIRDPARTY_LIB_DIR@")
endif()
set(extension_build_dir "@CMAKE_BINARY_DIR@")
set(_is_superbuild_extension @_is_superbuild_extension@)
if(_is_superbuild_extension)
get_filename_component(extension_build_dir ${extension_build_dir}/.. ABSOLUTE)
endif()
# Libraries that are not part of the Slicer build tree and not part of the extension
# build tree are considered as third-party libraries.
if(NOT item MATCHES "@Slicer_SUPERBUILD_DIR@"
AND NOT item MATCHES "${extension_build_dir}"
AND item MATCHES "\\.(so|dylib)$"
)
set(path "@fixup_path@/@Slicer_BUNDLE_EXTENSIONS_LOCATION@@Slicer_THIRDPARTY_LIB_DIR@")
endif()
math(EXPR lib_current $ENV{FIXUP_LIB_CURRENT}+1)
set(ENV{FIXUP_LIB_CURRENT} ${lib_current})
#gp_log_message("${lib_current}/$ENV{FIXUP_LIBS_COUNT} - fixing item ${item} with ${path}")
set(${default_embedded_path_var} "${path}" PARENT_SCOPE)
endfunction()
#-----------------------------------------------------------------------------
# get_extension_keys
#-----------------------------------------------------------------------------
function(get_extension_keys ext_dir libs dirs keys_var)
set(${keys_var} PARENT_SCOPE)
#get_bundle_and_executable("${app}" bundle executable valid)
#if(valid)
# Always use the exepath of the main bundle executable for <fixup_path>
# replacements:
#
#get_filename_component(exepath "${executable}" PATH)
set(exepath "${ext_dir}/Contents/MacOS")
set(bundle "${ext_dir}/Contents")
# But do fixups on all executables in the bundle:
#
get_bundle_all_executables("${bundle}" exes)
message(STATUS " exes='${exes}'")
# For each extra lib, accumulate a key as well and then also accumulate
# any of its prerequisites. (Extra libs are typically dynamically loaded
# plugins: libraries that are prerequisites for full runtime functionality
# but that do not show up in otool -L output...)
#
foreach(lib ${libs})
set_bundle_key_values(${keys_var} "${lib}" "${lib}" "${exepath}" "${dirs}" 0)
set(prereqs "")
get_prerequisites("${lib}" prereqs 1 0 "${exepath}" "${dirs}")
foreach(pr ${prereqs})
set_bundle_key_values(${keys_var} "${lib}" "${pr}" "${exepath}" "${dirs}" 1)
endforeach()
endforeach()
# For each executable found in the bundle, accumulate keys as we go.
# The list of keys should be complete when all prerequisites of all
# binaries in the bundle have been analyzed.
#
foreach(exe ${exes})
# Add the exe itself to the keys:
#
set_bundle_key_values(${keys_var} "${exe}" "${exe}" "${exepath}" "${dirs}" 0)
# Add each prerequisite to the keys:
#
set(prereqs "")
get_prerequisites("${exe}" prereqs 1 0 "${exepath}" "${dirs}")
foreach(pr ${prereqs})
set_bundle_key_values(${keys_var} "${exe}" "${pr}" "${exepath}" "${dirs}" 1)
endforeach()
endforeach()
# Propagate values to caller's scope:
#
set(${keys_var} ${${keys_var}} PARENT_SCOPE)
foreach(key ${${keys_var}})
set(${key}_ITEM "${${key}_ITEM}" PARENT_SCOPE)
set(${key}_RESOLVED_ITEM "${${key}_RESOLVED_ITEM}" PARENT_SCOPE)
set(${key}_DEFAULT_EMBEDDED_PATH "${${key}_DEFAULT_EMBEDDED_PATH}" PARENT_SCOPE)
set(${key}_EMBEDDED_ITEM "${${key}_EMBEDDED_ITEM}" PARENT_SCOPE)
set(${key}_RESOLVED_EMBEDDED_ITEM "${${key}_RESOLVED_EMBEDDED_ITEM}" PARENT_SCOPE)
set(${key}_COPYFLAG "${${key}_COPYFLAG}" PARENT_SCOPE)
endforeach()
#endif()
endfunction()
#-----------------------------------------------------------------------------
# fixup_extension
#-----------------------------------------------------------------------------
function(fixup_extension ext libs dirs)
message(STATUS "fixup_extension")
message(STATUS " ext='${ext}'")
message(STATUS " libs='${libs}'")
message(STATUS " dirs='${dirs}'")
set(extension_build_dir "@CMAKE_BINARY_DIR@")
set(_is_superbuild_extension @_is_superbuild_extension@)
if(_is_superbuild_extension)
get_filename_component(extension_build_dir ${extension_build_dir}/.. ABSOLUTE)
endif()
#get_bundle_and_executable("${ext}" bundle executable valid)
set(bundle "${ext}")
message(STATUS " bundle='${bundle}'")
set(valid 1)
if(valid)
# get_filename_component(exepath "${executable}" PATH)
set(exepath "${bundle}/Contents/MacOS")
# TODO: Extract list of rpath dirs automatically. On macOS, the following could be
# done: otool -l path/to/executable | grep -A 3 LC_RPATH | grep path
# See https://www.mikeash.com/pyblog/friday-qa-2009-11-06-linking-and-install-names.html#comment-87ea054b4839586412727dcfc94c79d2
set(GP_RPATH_DIR ${bundle}/Contents)
message(STATUS " GP_RPATH_DIR='${GP_RPATH_DIR}'")
message(STATUS "fixup_extension: preparing...")
get_extension_keys("${ext}" "${libs}" "${dirs}" keys)
message(STATUS "fixup_extension: copying...")
list(LENGTH keys n)
math(EXPR n ${n}*2)
set(i 0)
foreach(key ${keys})
math(EXPR i ${i}+1)
set(show_status 0)
if(show_status)
message(STATUS "key='${key}'")
message(STATUS "item='${${key}_ITEM}'")
message(STATUS "resolved_item='${${key}_RESOLVED_ITEM}'")
message(STATUS "default_embedded_path='${${key}_DEFAULT_EMBEDDED_PATH}'")
message(STATUS "embedded_item='${${key}_EMBEDDED_ITEM}'")
message(STATUS "resolved_embedded_item='${${key}_RESOLVED_EMBEDDED_ITEM}'")
message(STATUS "copyflag='${${key}_COPYFLAG}'")
message(STATUS "")
endif()
set(key_skip 0)
# Check if the item belong to an extension package itself built within a
# "Slicer" build tree. This may be done in custom application (e.g SlicerSALT)
# where bundling a specific extension (through -DSlicer_EXTENSION_SOURCE_DIRS=...)
# is not possible and instead the extension is packaged first and its content
# is then copied into the custom application package.
set(in_extension_build_dir_within_slicer_build_dir 0)
string(FIND "${extension_build_dir}" "@Slicer_SUPERBUILD_DIR@" pos)
if(${pos} EQUAL 0)
string(FIND "${${key}_ITEM}" "${extension_build_dir}" pos)
if(${pos} EQUAL 0)
set(in_extension_build_dir_within_slicer_build_dir 1)
endif()
endif()
string(FIND "${${key}_ITEM}" "@Slicer_SUPERBUILD_DIR@" pos)
if(${pos} EQUAL 0 AND NOT in_extension_build_dir_within_slicer_build_dir)
message(STATUS "${i}/${n}: skipping '${${key}_ITEM}'")
set(key_skip 1)
endif()
if("${${key}_ITEM}" MATCHES "[^/]+\\.framework/")
set(key_skip 1)
endif()
if(NOT ${key_skip})
message(STATUS "${i}/${n}: considering '${key}'")
if(${${key}_COPYFLAG})
message(STATUS "${i}/${n}: copying '${${key}_RESOLVED_ITEM}'")
else()
message(STATUS "${i}/${n}: *NOT* copying '${${key}_RESOLVED_ITEM}'")
endif()
if(${${key}_COPYFLAG})
set(item "${${key}_ITEM}")
if(item MATCHES "[^/]+\\.framework/")
copy_resolved_framework_into_bundle("${${key}_RESOLVED_ITEM}"
"${${key}_RESOLVED_EMBEDDED_ITEM}")
else()
copy_resolved_item_into_bundle("${${key}_RESOLVED_ITEM}"
"${${key}_RESOLVED_EMBEDDED_ITEM}")
endif()
endif()
endif()
set(${key}_SKIP ${key_skip})
endforeach()
message(STATUS "fixup_extension: fixing...")
foreach(key ${keys})
math(EXPR i ${i}+1)
if(NOT ${${key}_SKIP})
if(APPLE)
message(STATUS "${i}/${n}: fixing up '${${key}_RESOLVED_EMBEDDED_ITEM}'")
fixup_bundle_item("${${key}_RESOLVED_EMBEDDED_ITEM}" "${exepath}" "${dirs}")
else()
message(STATUS "${i}/${n}: fix-up not required on this platform '${${key}_RESOLVED_EMBEDDED_ITEM}'")
endif()
endif()
endforeach()
message(STATUS "fixup_extension: cleaning up...")
clear_bundle_keys(keys)
#message(STATUS "fixup_extension: verifying...")
#verify_app("${app}")
else()
message(SEND_ERROR "error: fixup_extension: not a valid bundle")
endif()
message(STATUS "fixup_extension: done")
endfunction()
#-----------------------------------------------------------------------------
# Fixup the .app bundles in the install tree:
#-----------------------------------------------------------------------------
function(fixup_slicer_extension)
set(app "@[email protected]")
set(ext_dir "$ENV{DESTDIR}${CMAKE_INSTALL_PREFIX}/${app}")
set(suffix "@CMAKE_SHARED_LIBRARY_SUFFIX@")
set(exepath "${ext_dir}/Contents/MacOS")
set(candidates_pattern
"${ext_dir}/Contents/@Slicer_BUNDLE_EXTENSIONS_LOCATION@@Slicer_THIRDPARTY_BIN_DIR@/[^/]+"
"${ext_dir}/Contents/@Slicer_BUNDLE_EXTENSIONS_LOCATION@@Slicer_THIRDPARTY_LIB_DIR@/[^/]+"
)
set(Slicer_USE_PYTHONQT "@Slicer_USE_PYTHONQT@")
if(Slicer_USE_PYTHONQT)
list(APPEND candidates_pattern
"${ext_dir}/Contents/@Slicer_BUNDLE_EXTENSIONS_LOCATION@bin/Python/vtkmodules/*.so"
)
endif()
set(Slicer_BUILD_QTLOADABLEMODULES "@Slicer_BUILD_QTLOADABLEMODULES@")
if(Slicer_BUILD_QTLOADABLEMODULES)
list(APPEND candidates_pattern
"${ext_dir}/Contents/@Slicer_BUNDLE_EXTENSIONS_LOCATION@@Slicer_QTLOADABLEMODULES_LIB_DIR@/libq*Module${suffix}"
"${ext_dir}/Contents/@Slicer_BUNDLE_EXTENSIONS_LOCATION@@Slicer_QTLOADABLEMODULES_LIB_DIR@/*Python.so"
)
if(Slicer_USE_PYTHONQT)
list(APPEND candidates_pattern
"${ext_dir}/Contents/@Slicer_BUNDLE_EXTENSIONS_LOCATION@@Slicer_QTLOADABLEMODULES_LIB_DIR@/qSlicer*PythonQt.so"
)
endif()
endif()
set(libs "")
file(GLOB_RECURSE candidates ${candidates_pattern})
foreach(lib ${candidates})
if(NOT lib MATCHES "(_debug|d[0-9])${suffix}$")
set(libs ${libs} "${lib}")
endif()
endforeach()
list(REMOVE_DUPLICATES libs)
# Additional libs may be found in:
set(libs_path )
list(APPEND libs_path "@EXTENSION_BINARY_DIR@/@Slicer_LIB_DIR@")
if(Slicer_BUILD_QTLOADABLEMODULES)
list(APPEND libs_path "@EXTENSION_BINARY_DIR@/@Slicer_QTLOADABLEMODULES_LIB_DIR@")
endif()
set(Slicer_BUILD_CLI_SUPPORT "@Slicer_BUILD_CLI_SUPPORT@")
if(Slicer_BUILD_CLI_SUPPORT)
list(APPEND libs_path "@EXTENSION_BINARY_DIR@/@Slicer_CLIMODULES_LIB_DIR@")
endif()
set(extension_libs_path "@EXTENSION_FIXUP_BUNDLE_LIBRARY_DIRECTORIES@")
if(extension_libs_path)
list(APPEND libs_path ${extension_libs_path})
endif()
list(REMOVE_DUPLICATES libs_path)
set(EXTENSION_SUPERBUILD_DIR "@EXTENSION_SUPERBUILD_DIR@")
gp_clear_log()
gp_log_message("Calling fixup_extension with")
gp_log_message("app=${ext_dir}")
gp_log_message("<EXTENSION_SUPERBUILD_DIR>=${EXTENSION_SUPERBUILD_DIR}")
gp_log_message("libs=")
foreach(lib ${libs})
file(RELATIVE_PATH relative_lib ${EXTENSION_SUPERBUILD_DIR} ${lib})
if(NOT "${relative_lib}" STREQUAL "${lib}")
set(lib "<EXTENSION_SUPERBUILD_DIR>/${relative_lib}")
endif()
gp_log_message(" ${lib}")
endforeach()
gp_log_message("libs_path=")
foreach(path ${libs_path})
file(RELATIVE_PATH relative_path ${EXTENSION_SUPERBUILD_DIR} ${path})
if(NOT "${relative_path}" STREQUAL "${path}")
set(path "<EXTENSION_SUPERBUILD_DIR>/${relative_path}")
endif()
gp_log_message(" ${path}")
endforeach()
# Keep track of libs count and current lib being fixed
list(LENGTH libs libs_count)
set(ENV{FIXUP_LIBS_COUNT} ${libs_count})
set(ENV{FIXUP_LIB_CURRENT} 0)
fixup_extension(
"${ext_dir}"
"${libs}"
"${libs_path}"
)
endfunction()
fixup_slicer_extension()