forked from rpavlik/cmake-modules
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CreateLaunchers.cmake
410 lines (362 loc) · 14 KB
/
CreateLaunchers.cmake
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
407
408
409
410
# - Create launchers to set working directory, env. vars, etc.
#
# include(CreateLaunchers) - to make these available
# guess_runtime_library_dirs(<outputvarname> [<extralibrary> ...])
# create_default_target_launcher(<targetname>
# [COMMAND <target command>]
# [ARGS <args...>]
# [FORWARD_ARGS]
# [RUNTIME_LIBRARY_DIRS <dir...>]
# [WORKING_DIRECTORY <dir>]
# [ENVIRONMENT <VAR=value> [<VAR=value>...]])
#
# create_target_launcher(<targetname>
# [COMMAND <target command>]
# [ARGS <args...>]
# [FORWARD_ARGS]
# [RUNTIME_LIBRARY_DIRS <dir...>]
# [WORKING_DIRECTORY <dir>]
# [ENVIRONMENT <VAR=value> [<VAR=value>...]])
#
# create_generic_launcher(<launchername>
# [COMMAND <target command>]
# [RUNTIME_LIBRARY_DIRS <dir...>]
# [WORKING_DIRECTORY <dir>]
# [ENVIRONMENT <VAR=value> [<VAR=value>...]])
# - sets GENERIC_LAUNCHER_COMMAND and GENERIC_LAUNCHER_FAIL_REGULAR_EXPRESSION
#
# Requires these CMake modules:
# CleanDirectoryList
#
# Requires CMake 2.6 or newer (uses the 'function' command)
#
# Original Author:
# 2009-2020 Ryan Pavlik <[email protected]> <[email protected]>
# http://academic.cleardefinition.com
#
# Copyright 2009-2013, Iowa State University.
# Copyright 2018-2019, caseymcc
# Copyright 2020, Ryan Pavlik
# Copyright 2014-2019, Contributors
# SPDX-License-Identifier: BSL-1.0
# Distributed under the Boost Software License, Version 1.0.
# (See accompanying file LICENSE_1_0.txt or copy at
# http://www.boost.org/LICENSE_1_0.txt)
if(__create_launchers)
return()
endif()
set(__create_launchers YES)
cmake_policy(PUSH)
cmake_policy(SET CMP0053 NEW)
include(CleanDirectoryList)
# We must run the following at "include" time, not at function call time,
# to find the path to this module rather than the path to a calling list file
get_filename_component(_launchermoddir ${CMAKE_CURRENT_LIST_FILE} PATH)
set(_launchermoddir "${_launchermoddir}/launcher-templates")
macro(_launcher_system_settings)
if(CMAKE_SIZEOF_VOID_P EQUAL 8)
set(BITS 64)
else()
set(BITS 32)
endif()
if(WIN32)
# Find user and system name
set(SYSTEM_NAME $ENV{USERDOMAIN})
set(USER_NAME $ENV{USERNAME})
set(VCPROJ_TYPE vcproj)
set(USERFILE_EXTENSION ${SYSTEM_NAME}.${USER_NAME}.user)
set(LAUNCHER_LINESEP "
")
if(MSVC90)
set(USERFILE_VC_VERSION 9.00)
elseif(MSVC80)
set(USERFILE_VC_VERSION 8.00)
elseif(MSVC71)
set(USERFILE_VC_VERSION 7.10)
elseif(MSVC10 OR (MSVC AND MSVC_VERSION GREATER 1600)) # 2010 or newer
set(LAUNCHER_LINESEP "\n")
set(USERFILE_VC_VERSION 10.00)
set(USERFILE_EXTENSION user)
set(VCPROJ_TYPE vcxproj)
endif()
if(BITS EQUAL 64)
set(USERFILE_PLATFORM x64)
else()
set(USERFILE_PLATFORM Win${BITS})
endif()
set(_pathdelim "$<SEMICOLON>")
set(_suffix "cmd")
else()
set(_pathdelim ":")
set(USERFILE_PLATFORM ${CMAKE_SYSTEM_NAME}${BITS})
set(_suffix "sh")
find_package(GDB QUIET)
if(GDB_FOUND)
set(LAUNCHERS_GOT_GDB YES)
if(GDB_HAS_RETURN_CHILD_RESULT)
set(LAUNCHERS_GDB_ARG --return-child-result)
endif()
else()
set(LAUNCHERS_GOT_GDB)
endif()
endif()
if(WIN32 AND NOT USERFILE_REMOTE_MACHINE)
site_name(USERFILE_REMOTE_MACHINE)
mark_as_advanced(USERFILE_REMOTE_MACHINE)
endif()
endmacro()
macro(_launcher_process_args)
set(_nowhere)
set(_curdest _nowhere)
set(_val_args ARGS COMMAND RUNTIME_LIBRARY_DIRS WORKING_DIRECTORY
ENVIRONMENT TARGET_PLATFORM)
set(_bool_args FORWARD_ARGS)
foreach(_arg ${_val_args} ${_bool_args})
set(${_arg})
endforeach()
foreach(_element ${ARGN})
string(REPLACE ";" "\\;" _element "${_element}")
list(FIND _val_args "${_element}" _val_arg_find)
list(FIND _bool_args "${_element}" _bool_arg_find)
if("${_val_arg_find}" GREATER "-1")
set(_curdest "${_element}")
elseif("${_bool_arg_find}" GREATER "-1")
set("${_element}" ON)
set(_curdest _nowhere)
else()
list(APPEND ${_curdest} "${_element}")
endif()
endforeach()
if(_nowhere)
message(
FATAL_ERROR "Syntax error in use of a function in CreateLaunchers!")
endif()
# Turn into a list of native paths
set(_runtime_lib_dirs)
foreach(_dlldir ${RUNTIME_LIBRARY_DIRS})
file(TO_NATIVE_PATH "${_dlldir}" _path)
if(NOT EXISTS "${_path}") #this is not a file so lets leave it as is
set(_path ${_dlldir})
endif()
set(_runtime_lib_dirs "${_runtime_lib_dirs}${_path}${_pathdelim}")
endforeach()
if(NOT WORKING_DIRECTORY)
set(WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}")
endif()
if(NOT COMMAND)
set(COMMAND "$<TARGET_FILE:${_targetname}>")
endif()
if(FORWARD_ARGS)
if(WIN32)
set(FWD_ARGS %*)
else()
set(FWD_ARGS $*)
endif()
else()
set(FWD_ARGS)
endif()
if(TARGET_PLATFORM)
set(USERFILE_PLATFORM ${TARGET_PLATFORM})
endif()
set(USERFILE_WORKING_DIRECTORY "${WORKING_DIRECTORY}")
set(USERFILE_COMMAND "${COMMAND}")
set(USERFILE_COMMAND_ARGUMENTS "${ARGS}")
set(LAUNCHERSCRIPT_COMMAND_ARGUMENTS "${ARGS} ${FWD_ARGS}")
if(WIN32)
if(_runtime_lib_dirs)
set(RUNTIME_LIBRARIES_ENVIRONMENT
"PATH=${_runtime_lib_dirs}$<SEMICOLON>%PATH%")
endif()
file(READ "${_launchermoddir}/launcher.env.cmd.in" _cmdenv)
else()
if(_runtime_lib_dirs)
if(APPLE)
set(RUNTIME_LIBRARIES_ENVIRONMENT
"DYLD_LIBRARY_PATH=${_runtime_lib_dirs}:$DYLD_LIBRARY_PATH")
else()
set(RUNTIME_LIBRARIES_ENVIRONMENT
"LD_LIBRARY_PATH=${_runtime_lib_dirs}:$LD_LIBRARY_PATH")
endif()
endif()
file(READ "${_launchermoddir}/launcher.env.sh.in" _cmdenv)
endif()
set(USERFILE_ENVIRONMENT)
set(USERFILE_ENV_COMMANDS)
foreach(_arg "${RUNTIME_LIBRARIES_ENVIRONMENT}" ${ENVIRONMENT})
if(_arg)
string(CONFIGURE "@USERFILE_ENVIRONMENT@@LAUNCHER_LINESEP@@_arg@"
USERFILE_ENVIRONMENT @ONLY)
endif()
string(CONFIGURE "@USERFILE_ENV_COMMANDS@${_cmdenv}"
USERFILE_ENV_COMMANDS @ONLY)
endforeach()
endmacro()
#ok, we have gone very hackish on this function as the file(GENERATE) is very hard to work with as the cmake folks really don't
#want you writing generator files yourself, yet the user file is not planning to ever be supported
macro(_launcher_produce_vcproj_user)
if(MSVC)
set(CMAKEFILES_PATH "${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles")
set(TARGET_CMAKE_FILES "${CMAKEFILES_PATH}/${_targetname}")
file(READ "${_launchermoddir}/perconfig.${VCPROJ_TYPE}.user.in"
_perconfig)
#generator expressions do not play well with ">"
string(REPLACE ">" "$<ANGLE-R>" _perconfig ${_perconfig})
set(config_types)
if(CMAKE_CONFIGURATION_TYPES)
foreach(config_type ${CMAKE_CONFIGURATION_TYPES})
set(config_types ${config_types} ${config_type})
endforeach()
else()
set(config_types ${CMAKE_BUILD_TYPE})
endif()
set(USERFILE_CONFIGSECTIONS)
foreach(USERFILE_CONFIGNAME ${config_types})
string(CONFIGURE "${_perconfig}" _temp @ONLY ESCAPE_QUOTES)
#we are building the per config info with genertator expressions so that when generating the files only the config currently generated is being correctly filled in
set(_temp
"$<IF:$<CONFIG:${USERFILE_CONFIGNAME}>,\n${_temp},$<TARGET_PROPERTY:${_targetname},LAUNCHER_USER_ELSE_${USERFILE_CONFIGNAME}>>\n"
)
#we are putting this info into the target properties so it can be recuresively added back during the generator calls
set_target_properties(
${_targetname}
PROPERTIES LAUNCHER_USER_ELSE_${USERFILE_CONFIGNAME} ${_temp})
string(CONFIGURE "${USERFILE_CONFIGSECTIONS}${_temp}"
USERFILE_CONFIGSECTIONS ESCAPE_QUOTES)
endforeach()
configure_file(
"${_launchermoddir}/${VCPROJ_TYPE}.user.in"
${TARGET_CMAKE_FILES}.${VCPROJ_TYPE}.${USERFILE_EXTENSION}.config
@ONLY)
#now we are looping thtough each config type loading the previous ones output, hopefully execution order stays the same as the generation request
set(launcher_last_config)
foreach(USERFILE_CONFIGNAME ${config_types})
if(NOT launcher_last_config)
file(
GENERATE
OUTPUT
${TARGET_CMAKE_FILES}.${VCPROJ_TYPE}.${USERFILE_CONFIGNAME}.usergen
INPUT
${TARGET_CMAKE_FILES}.${VCPROJ_TYPE}.${USERFILE_EXTENSION}.config
CONDITION $<CONFIG:${USERFILE_CONFIGNAME}>)
else()
file(
GENERATE
OUTPUT
${TARGET_CMAKE_FILES}.${VCPROJ_TYPE}.${USERFILE_CONFIGNAME}.usergen
INPUT
${TARGET_CMAKE_FILES}.${VCPROJ_TYPE}.${launcher_last_config}.usergen
CONDITION $<CONFIG:${USERFILE_CONFIGNAME}>)
endif()
set(launcher_last_config ${USERFILE_CONFIGNAME})
endforeach()
#build the final output from the last generation output
file(
GENERATE
OUTPUT ${VCPROJNAME}.${VCPROJ_TYPE}.${USERFILE_EXTENSION}
INPUT
${TARGET_CMAKE_FILES}.${VCPROJ_TYPE}.${launcher_last_config}.usergen
)
endif()
endmacro()
macro(_launcher_configure_executable _src _tmp _target _config)
configure_file("${_src}" "${_tmp}" @ONLY)
#used to convert generator expressions
file(
GENERATE
OUTPUT "${_target}"
INPUT "${_tmp}"
CONDITION $<CONFIG:${_config}>)
#we lose the ability to change the file permisions as there is no support there in file(GENERATE) (although it has been requested)
#and nothing runs after file(GENERATE) durning the cmake call
# file(COPY "${_tmp}"
# DESTINATION "${_targetpath}"
# FILE_PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ GROUP_WRITE GROUP_EXECUTE WORLD_READ WORLD_EXECUTE)
endmacro()
macro(_launcher_create_target_launcher)
if(CMAKE_CONFIGURATION_TYPES)
# Multi-config generator - multiple launchers
foreach(_config ${CMAKE_CONFIGURATION_TYPES})
set(_fn "launch-${_targetname}-${_config}.${_suffix}")
_launcher_configure_executable(
"${_launchermoddir}/targetlauncher.${_suffix}.in"
"${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/${_fn}"
"${CMAKE_CURRENT_BINARY_DIR}/${_fn}" ${_config})
endforeach()
else()
# Single-config generator - single launcher
_launcher_configure_executable(
"${_launchermoddir}/targetlauncher.${_suffix}.in"
"${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/launch-${_targetname}.${_suffix}"
"${CMAKE_CURRENT_BINARY_DIR}/launch-${_targetname}.${_suffix}"
${CMAKE_BUILD_TYPE})
endif()
endmacro()
function(create_default_target_launcher _targetname)
_launcher_system_settings()
_launcher_process_args(${ARGN})
set(VCPROJNAME "${CMAKE_BINARY_DIR}/ALL_BUILD")
_launcher_produce_vcproj_user()
_launcher_create_target_launcher()
endfunction()
function(create_target_launcher _targetname)
_launcher_system_settings()
_launcher_process_args(${ARGN})
set(VCPROJNAME "${CMAKE_CURRENT_BINARY_DIR}/${_targetname}")
_launcher_produce_vcproj_user()
_launcher_create_target_launcher()
endfunction()
function(create_generic_launcher _launchername)
_launcher_system_settings()
_launcher_process_args(${ARGN})
if(NOT IS_ABSOLUTE _launchername)
set(_launchername
"${CMAKE_CURRENT_BINARY_DIR}/${_launchername}.${_suffix}")
else()
set(_launchername "${_launchername}.${_suffix}")
endif()
if(WIN32)
set(GENERIC_LAUNCHER_COMMAND
"${_launchername}"
PARENT_SCOPE)
set(GENERIC_LAUNCHER_FAIL_REGULAR_EXPRESSION)
else()
set(GENERIC_LAUNCHER_COMMAND
sh "${_launchername}"
PARENT_SCOPE)
set(GENERIC_LAUNCHER_FAIL_REGULAR_EXPRESSION
"Program terminated with signal")
endif()
if(CMAKE_CONFIGURATION_TYPES)
set(_launcher_build_type "${CMAKE_BUILD_TYPE}")
else()
unset(_launcher_build_type)
endif()
_launcher_configure_executable(
"${_launchermoddir}/genericlauncher.${_suffix}.in"
"${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/genericlauncher.${_suffix}.in"
"${_launchername}" ${_launcher_build_type})
endfunction()
function(guess_runtime_library_dirs _var)
# Start off with the link directories of the calling listfile's directory
get_directory_property(_libdirs LINK_DIRECTORIES)
# Add additional libraries passed to the function
foreach(_lib ${ARGN})
get_filename_component(_libdir "${_lib}" PATH)
list(APPEND _libdirs "${_libdir}")
endforeach()
# Now, build a list of potential dll directories
set(_dlldirs)
foreach(_libdir ${_libdirs})
# Add the libdir itself
list(APPEND _dlldirs "${_libdir}")
# Look also in libdir/../bin since the dll might not be with the lib
get_filename_component(_libdir "${_libdir}/../bin" ABSOLUTE)
list(APPEND _dlldirs "${_libdir}")
endforeach()
# Only keep the valid, unique directories
clean_directory_list(_dlldirs)
# Return _dlldirs
set(${_var}
"${_dlldirs}"
PARENT_SCOPE)
endfunction()
cmake_policy(POP)