Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
usFunctionAddResources.cmake
Go to the documentation of this file.
1 #! \ingroup MicroServicesCMake
2 #! \brief Add resources to a library or executable.
3 #!
4 #! This CMake function uses an external command line program to generate a ZIP archive
5 #! containing data from external resources such as text files or images or other ZIP
6 #! archives. The created archive file can be appended or linked into the target file
7 #! using the usFunctionEmbedResources macro.
8 #!
9 #! Each module can call this function to add resources and make them available at
10 #! runtime through the Module class. Multiple calls to this function append the
11 #! input files.
12 #!
13 #! In the case of linking static modules which contain resources to the target module,
14 #! adding the static module target name to the ZIP_ARCHIVES list will merge its
15 #! resources into the target module.
16 #!
17 #! Example usage:
18 #! \code{.cmake}
19 #! set(module_srcs )
20 #! usFunctionAddResources(TARGET mylib
21 #! MODULE_NAME org_me_mylib
22 #! FILES config.properties logo.png
23 #! )
24 #! \endcode
25 #!
26 #! \param TARGET (required) The target to which the resource files are added.
27 #! \param MODULE_NAME (required/optional) The module name of the target, as specified in
28 #! the \c US_MODULE_NAME pre-processor definition of that target. This parameter
29 #! is optional if a target property with the name US_MODULE_NAME exists, containing
30 #! the required module name.
31 #! \param COMPRESSION_LEVEL (optional) The zip compression level (0-9). Defaults to the default zip
32 #! level. Level 0 disables compression.
33 #! \param WORKING_DIRECTORY (optional) The root path for all resource files listed after the
34 #! FILES argument. If no or a relative path is given, it is considered relativ to the
35 #! current CMake source directory.
36 #! \param FILES (optional) A list of resource files (paths to external files in the file system)
37 #! relative to the current working directory.
38 #! \param ZIP_ARCHIVES (optional) A list of zip archives (relative to the current working directory
39 #! or absolute file paths) whose contents is merged into the target module. If a list entry
40 #! is a valid target name and that target is a static library, its absolute file path is
41 #! used instead.
42 #!
43 #! \sa usFunctionEmbedResources
44 #! \sa \ref MicroServices_Resources
45 #!
46 function(usFunctionAddResources)
47 
48  cmake_parse_arguments(US_RESOURCE "" "TARGET;MODULE_NAME;WORKING_DIRECTORY;COMPRESSION_LEVEL" "FILES;ZIP_ARCHIVES" ${ARGN})
49 
50  if(NOT US_RESOURCE_TARGET)
51  message(SEND_ERROR "TARGET argument not specified.")
52  endif()
53 
54  if(NOT US_RESOURCE_MODULE_NAME)
55  get_target_property(US_RESOURCE_MODULE_NAME ${US_RESOURCE_TARGET} US_MODULE_NAME)
56  if(NOT US_RESOURCE_MODULE_NAME)
57  message(SEND_ERROR "Either the MODULE_NAME argument or the US_MODULE_NAME target property is required.")
58  endif()
59  endif()
60 
61  if(NOT US_RESOURCE_FILES AND NOT US_RESOURCE_ZIP_ARCHIVES)
62  message(WARNING "No resources specified. Skipping resource processing.")
63  return()
64  endif()
65 
66  if(NOT US_RESOURCE_WORKING_DIRECTORY)
67  set(US_RESOURCE_WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
68  endif()
69  if(NOT IS_ABSOLUTE ${US_RESOURCE_WORKING_DIRECTORY})
70  set(US_RESOURCE_WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/${US_RESOURCE_WORKING_DIRECTORY}")
71  endif()
72 
73  if(US_RESOURCE_COMPRESSION_LEVEL)
74  set(cmd_line_args -${US_RESOURCE_COMPRESSION_LEVEL})
75  endif()
76 
77  set(resource_compiler ${US_RCC_EXECUTABLE})
78  if(TARGET ${US_RCC_EXECUTABLE_NAME})
79  set(resource_compiler ${US_RCC_EXECUTABLE_NAME})
80  elseif(NOT resource_compiler)
81  message(FATAL_ERROR "The CppMicroServices resource compiler was not found. Check the US_RCC_EXECUTABLE CMake variable.")
82  endif()
83 
84  set(_cmd_deps )
85  foreach(_file ${US_RESOURCE_FILES})
86  if(IS_ABSOLUTE ${_file})
87  list(APPEND _cmd_deps ${_file})
88  else()
89  list(APPEND _cmd_deps ${US_RESOURCE_WORKING_DIRECTORY}/${_file})
90  endif()
91  endforeach()
92 
93  set(_zip_args )
94  if(US_RESOURCE_ZIP_ARCHIVES)
95  foreach(_zip_archive ${US_RESOURCE_ZIP_ARCHIVES})
96  if(TARGET ${_zip_archive})
97  get_target_property(_is_static_lib ${_zip_archive} TYPE)
98  if(_is_static_lib STREQUAL "STATIC_LIBRARY")
99  list(APPEND _cmd_deps ${_zip_archive})
100  list(APPEND _zip_args $<TARGET_FILE:${_zip_archive}>)
101  endif()
102  else()
103  if(IS_ABSOLUTE ${_zip_archive})
104  list(APPEND _cmd_deps ${_zip_archive})
105  else()
106  list(APPEND _cmd_deps ${US_RESOURCE_WORKING_DIRECTORY}/${_zip_archive})
107  endif()
108  list(APPEND _zip_args ${_zip_archive})
109  endif()
110  endforeach()
111  endif()
112 
113  if(NOT US_RESOURCE_FILES AND NOT _zip_args)
114  return()
115  endif()
116 
117  if(US_RESOURCE_FILES)
118  set(_file_args -a ${US_RESOURCE_FILES})
119  endif()
120  if(_zip_args)
121  set(_zip_args -m ${_zip_args})
122  endif()
123 
124  get_target_property(_counter ${US_RESOURCE_TARGET} _us_resource_counter)
125  if((NOT ${_counter} EQUAL 0) AND NOT _counter)
126  set(_counter 0)
127  else()
128  math(EXPR _counter "${_counter} + 1")
129  endif()
130 
131  set(_res_zip "${CMAKE_CURRENT_BINARY_DIR}/us_${US_RESOURCE_TARGET}/res_${_counter}.zip")
132 
133  add_custom_command(
134  OUTPUT ${_res_zip}
135  COMMAND ${CMAKE_COMMAND} -E make_directory "${CMAKE_CURRENT_BINARY_DIR}/us_${US_RESOURCE_TARGET}"
136  COMMAND ${resource_compiler} ${cmd_line_args} ${_res_zip} ${US_RESOURCE_MODULE_NAME} ${_file_args} ${_zip_args}
137  WORKING_DIRECTORY ${US_RESOURCE_WORKING_DIRECTORY}
138  DEPENDS ${_cmd_deps} ${resource_compiler}
139  COMMENT "Checking resource dependencies for ${US_RESOURCE_TARGET}"
140  VERBATIM
141  )
142 
143  get_target_property(_res_zips ${US_RESOURCE_TARGET} _us_resource_zips)
144  if(NOT _res_zips)
145  set(_res_zips )
146  endif()
147  list(APPEND _res_zips ${_res_zip})
148 
149  set_target_properties(${US_RESOURCE_TARGET} PROPERTIES
150  _us_resource_counter "${_counter}"
151  _us_resource_zips "${_res_zips}"
152  )
153 
154 endfunction()
usFunctionAddResources()
Add resources to a library or executable.
const std::string TARGET