forked from Slicer/Slicer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSlicerFunctionAddPythonQtResources.cmake
88 lines (76 loc) · 3 KB
/
SlicerFunctionAddPythonQtResources.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
set_property(GLOBAL PROPERTY _SLICER_PYTHON_RESOURCE_TARGETS)
function(slicerFunctionAddPythonQtResources RESOURCE_NAMES)
set(out_paths)
# Generate compiler resource scripts
foreach(in_path ${ARGN})
set(rc_depends)
if(IS_ABSOLUTE ${in_path})
file(RELATIVE_PATH out_dir ${CMAKE_CURRENT_SOURCE_DIR} ${in_path})
get_filename_component(out_dir ${CMAKE_CURRENT_BINARY_DIR}/${out_dir} PATH)
else()
get_filename_component(out_dir ${CMAKE_CURRENT_BINARY_DIR}/${in_path} PATH)
get_filename_component(in_path ${in_path} ABSOLUTE)
endif()
get_filename_component(out_name ${in_path} NAME_WE)
get_filename_component(rc_path ${in_path} PATH)
set(out_path ${out_dir}/${out_name}Resources.py)
if(EXISTS ${in_path})
# Parse file for dependencies
file(READ ${in_path} rc_file_contents)
string(REGEX MATCHALL "<file[^<]+" rc_files "${rc_file_contents}")
foreach(rc_file ${rc_files})
string(REGEX REPLACE "^<file[^>]*>" "" rc_file ${rc_file})
if(NOT IS_ABSOLUTE ${rc_file})
set(rc_file ${rc_path}/${rc_file})
endif()
list(APPEND rc_depends ${rc_file})
endforeach()
# Copy the input qrc script to enforce recalculation of dependencies on
# changes to the same
configure_file(
${in_path}
${CMAKE_CURRENT_BINARY_DIR}/.${out_name}Resources.py.deps
COPYONLY
)
endif()
if(NOT DEFINED QT_RCC_EXECUTABLE AND TARGET Qt5::rcc)
get_target_property(QT_RCC_EXECUTABLE Qt5::rcc IMPORTED_LOCATION)
endif()
if(NOT DEFINED Slicer_QRCC_SCRIPT)
message(FATAL_ERROR "Slicer_QRCC_SCRIPT is expected to be set to /path/to/qrcc.py")
endif()
if(NOT EXISTS ${Slicer_QRCC_SCRIPT})
message(FATAL_ERROR "Slicer_QRCC_SCRIPT set to ${Slicer_QRCC_SCRIPT} corresponds to an nonexistent file.")
endif()
# Create command to generate the compiled resource script
add_custom_command(
OUTPUT ${out_path}
COMMAND ${CMAKE_COMMAND} -E make_directory ${out_dir}
COMMAND ${PYTHON_EXECUTABLE}
${Slicer_QRCC_SCRIPT}
--rcc ${QT_RCC_EXECUTABLE}
-o ${out_path}
${in_path}
VERBATIM
WORKING_DIRECTORY ${rc_path}
MAIN_DEPENDENCY ${in_path}
DEPENDS ${Slicer_QRCC_SCRIPT} ${rc_depends}
)
list(APPEND out_paths ${out_path})
endforeach()
# Create target to generate resource files
get_filename_component(target ${CMAKE_CURRENT_BINARY_DIR}-Resources NAME)
add_custom_target(${target} DEPENDS ${out_paths})
get_property(resource_targets
GLOBAL PROPERTY _SLICER_PYTHON_RESOURCE_TARGETS
)
list(APPEND resource_targets ${target})
set_property(GLOBAL PROPERTY _SLICER_PYTHON_RESOURCE_TARGETS
${resource_targets}
)
set(${RESOURCE_NAMES} ${out_paths} PARENT_SCOPE)
endfunction()
function(slicerFunctionAddPythonQtResourcesTargets NAME)
get_property(resource_files GLOBAL PROPERTY _SLICER_PYTHON_RESOURCE_TARGETS)
add_custom_target(${NAME} DEPENDS ${resource_files})
endfunction()