File: expand_command.cmake

package info (click to toggle)
vecgeom 1.2.8%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 24,016 kB
  • sloc: cpp: 88,803; ansic: 6,888; python: 1,035; sh: 582; sql: 538; makefile: 23
file content (59 lines) | stat: -rw-r--r-- 2,276 bytes parent folder | download | duplicates (2)
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
# This file contains a generic workaround to cause a list generated by
# a generator expression to be expanded when used as the COMMAND to
# add_custom_command() or add_custom_target().
# Using this workaround:
#  - include this file in CMakeLists.txt
#  - call expandable_command(<retvar> <command> [arg1] [arg2] ...)
#  - use the <retvar> from above as the COMMAND to add_custom_target()
#       or add_custom_command().
# Example to execute
#  echo a generated list
# code:
#  include(/path/to/expand_command.cmake)
#  expandable_command(cmdline echo $<TARGET_PROPERTY:echo,MSGLIST>)
#  add_custom_target(echo ALL COMMAND ${cmdline} VERBATIM)
#  set_property(TARGET echo PROPERTY MSGLIST a generated list)
if(NOT EXPAND_COMMAND_AS_SCRIPT)
    #==== This section is processed at configuration time when included
    if(COMMAND expandable_command)
        return()
    endif()

    set(EXPAND_COMMAND_SCRIPT "${CMAKE_CURRENT_LIST_FILE}")

    # Check if we are a case like VecGeom + VecCore where CUDA is
    # initialized by a sub-project (VecCore) but is used also in the
    # top project (VecGeom)
    get_directory_property(hasParent PARENT_DIRECTORY)
    if(hasParent)
      set(EXPAND_COMMAND_SCRIPT "${CMAKE_CURRENT_LIST_FILE}" PARENT_SCOPE)
    endif()

    function(expandable_command retvar)
        set(cmdline ${ARGN})
        string(REPLACE ";" "$<SEMICOLON>" cmdline "${cmdline}")
        set(${retvar} "${CMAKE_COMMAND}" "-D" "EXPAND_COMMAND_AS_SCRIPT=TRUE"
            "-D" "EXPAND_COMMAND=${cmdline}" "-P" "${EXPAND_COMMAND_SCRIPT}"
            PARENT_SCOPE)
    endfunction(expandable_command)

else()
    #==== This section is the command wrapper run at build time
    FOREACH(VAL ${EXPAND_COMMAND})
       STRING(REGEX REPLACE "^\"(.*)\"$" "\\1" VAL2 ${VAL})
       STRING(REGEX REPLACE "^,\"(.*)\"$" ",\\1" VAL3 ${VAL2})
       STRING(REGEX REPLACE "\",\"" "," VAL4 ${VAL3})
       LIST(APPEND real_cmd "${VAL4}")
    ENDFOREACH(VAL ${EXPAND_COMMAND})
    execute_process(COMMAND ${real_cmd}
                    ERROR_VARIABLE error
                    RESULT_VARIABLE result)
    set(stderr_type "")
    if(result)
        set(stderr_type FATAL_ERROR)
    endif()
    if(result OR error)
        message(${stderr_type} "${error}")
    endif()
endif()