ParaViewMacros.cmake 21.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
# File defining miscellaneous macros

#------------------------------------------------------------------------------
# GENERATE_QT_RESOURCE_FROM_FILES can be used to generate a Qt resource file
# from a given set of files.
# ARGUMENTS:
# resource_file: IN : full pathname of the qrc file to generate. 
# resource_prefix: IN : the name used in the "prefix" attribute for the
#                       generated qrc file.
# file_list: IN : list of files to be added into the resource file.
#------------------------------------------------------------------------------
12
FUNCTION(GENERATE_QT_RESOURCE_FROM_FILES resource_file resource_prefix file_list)
13 14 15 16
  SET (pq_resource_file_contents "<RCC>\n  <qresource prefix=\"${resource_prefix}\">\n")
  FOREACH (resource ${file_list})
    GET_FILENAME_COMPONENT(alias ${resource} NAME)
    GET_FILENAME_COMPONENT(resource ${resource} ABSOLUTE)
17
    GET_FILENAME_COMPONENT(resource ${resource} REALPATH)
18 19 20
    FILE(TO_NATIVE_PATH "${resource}" resource)
    SET (pq_resource_file_contents
      "${pq_resource_file_contents}    <file alias=\"${alias}\">${resource}</file>\n")
21
  ENDFOREACH ()
22 23 24 25
  SET (pq_resource_file_contents
    "${pq_resource_file_contents}  </qresource>\n</RCC>\n")

  # Generate the resource file.
26 27 28 29
  set (CMAKE_CONFIGURABLE_FILE_CONTENT "${pq_resource_file_contents}")
  configure_file(${CMAKE_ROOT}/Modules/CMakeConfigurableFile.in
                 "${resource_file}")
  unset (CMAKE_CONFIGURABLE_FILE_CONTENT)
30
ENDFUNCTION()
31 32 33 34 35 36 37 38 39

#----------------------------------------------------------------------------
# PV_PARSE_ARGUMENTS is a macro useful for writing macros that take a key-word
# style arguments.
#----------------------------------------------------------------------------
MACRO(PV_PARSE_ARGUMENTS prefix arg_names option_names)
  SET(DEFAULT_ARGS)
  FOREACH(arg_name ${arg_names})    
    SET(${prefix}_${arg_name})
40
  ENDFOREACH()
41 42
  FOREACH(option ${option_names})
    SET(${prefix}_${option} FALSE)
43
  ENDFOREACH()
44 45 46 47 48 49 50 51 52 53

  SET(current_arg_name DEFAULT_ARGS)
  SET(current_arg_list)
  FOREACH(arg ${ARGN})            
    SET(larg_names ${arg_names})    
    LIST(FIND larg_names "${arg}" is_arg_name)                   
    IF (is_arg_name GREATER -1)
      SET(${prefix}_${current_arg_name} ${current_arg_list})
      SET(current_arg_name ${arg})
      SET(current_arg_list)
54
    ELSE ()
55 56 57 58
      SET(loption_names ${option_names})    
      LIST(FIND loption_names "${arg}" is_option)            
      IF (is_option GREATER -1)
       SET(${prefix}_${arg} TRUE)
59
      ELSE ()
60
       SET(current_arg_list ${current_arg_list} ${arg})
61 62 63
      ENDIF ()
    ENDIF ()
  ENDFOREACH()
64
  SET(${prefix}_${current_arg_name} ${current_arg_list})
65
ENDMACRO()
66

67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
#----------------------------------------------------------------------------
# Macro for extracting Plugin path and name from arguments
#----------------------------------------------------------------------------
MACRO(PV_EXTRACT_CLIENT_SERVER_ARGS)
  set(options)
  set(oneValueArgs LOAD_PLUGIN PLUGIN_PATH)
  set(multiValueArgs )
  cmake_parse_arguments(PV "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
  set(CLIENT_SERVER_ARGS)
  if(PV_PLUGIN_PATH)
    set(CLIENT_SERVER_ARGS ${CLIENT_SERVER_ARGS} "--test-plugin-path=${PV_PLUGIN_PATH}")
  endif()
  if(PV_LOAD_PLUGIN)
    set(CLIENT_SERVER_ARGS ${CLIENT_SERVER_ARGS} "--test-plugin=${PV_LOAD_PLUGIN}")
  endif()
82
ENDMACRO()
83

84 85 86 87 88 89
#----------------------------------------------------------------------------
# Macro for setting values if a user did not overwrite them
#----------------------------------------------------------------------------
MACRO(pv_set_if_not_set name value)
  IF(NOT DEFINED "${name}")
    SET(${name} "${value}")
90 91
  ENDIF()
ENDMACRO()
92

93 94 95 96 97 98 99 100
#----------------------------------------------------------------------------
# When installing system libraries, on non-windows machines, the CMake variable
# pointing to the library may be a sym-link, in which case we don't simply want
# to install the symlink, but the actual library. This macro takes care of that.
# Use it for installing system libraries. Call this only on unix boxes.
FUNCTION (pv_install_library libpath dest component)
  IF (NOT WIN32)
    GET_FILENAME_COMPONENT(dir_tmp ${libpath} PATH)
101 102 103 104 105 106
    SET(name_tmp)
    # libs symlinks are always named lib.*.dylib on mac
    # libs symlinks are always named lib.so.* on linux
    IF (APPLE)
      GET_FILENAME_COMPONENT(name_tmp ${libpath} NAME_WE)
      FILE(GLOB lib_list "${dir_tmp}/${name_tmp}*")
107
    ELSE ()
108 109 110
      GET_FILENAME_COMPONENT(dir_tmp ${libpath} PATH)
      GET_FILENAME_COMPONENT(name_tmp ${libpath} NAME)
      FILE(GLOB lib_list RELATIVE "${dir_tmp}" "${libpath}*")
111
    ENDIF ()
112 113 114 115 116 117
    INSTALL(CODE "
          MESSAGE(STATUS \"Installing ${name_tmp}\")
          EXECUTE_PROCESS (WORKING_DIRECTORY ${dir_tmp}
               COMMAND tar c ${lib_list}
               COMMAND tar -xC \${CMAKE_INSTALL_PREFIX}/${dest})
               " COMPONENT ${component})
118 119
  ENDIF ()
ENDFUNCTION ()
120

121 122 123 124 125 126 127 128 129 130
#########################################################################
# Function to compile a proto file to generate a .h and .cc file
# Arguments:
# out_cpp_file_variable: variable that gets set with the full path to output file
# in_proto_file: full path to input file (e.g. ${CMAKE_CURRENT_SOURCE_DIR}/foo.pb)

FUNCTION (protobuf_generate out_cpp_file in_proto_file)
  GET_FILENAME_COMPONENT(basename ${in_proto_file} NAME_WE)
  GET_FILENAME_COMPONENT(absolute ${in_proto_file} ABSOLUTE)
  GET_FILENAME_COMPONENT(path ${absolute} PATH)
131
  SET (out_file ${CMAKE_CURRENT_BINARY_DIR}/${basename}.pb.h)
132
  SET(${out_cpp_file}  ${out_file} PARENT_SCOPE)
133
  ADD_CUSTOM_COMMAND(
134
    OUTPUT ${out_file}
135
    COMMAND protoc_compiler
136
      --cpp_out=dllexport_decl=VTK_PROTOBUF_EXPORT:${CMAKE_CURRENT_BINARY_DIR}
137
      --proto_path ${path} ${absolute}
138 139
    DEPENDS ${in_proto_file} protoc_compiler
  )
140
ENDFUNCTION ()
141

142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
#########################################################################
# Function to generate header file from any file(s). Support ASCII as well as
# binary files.
# Usage:
# generate_header(name
#                 [PREFIX prefix_text]
#                 [SUFFIX suffix_text]
#                 [VARIABLE variablename]
#                 [BINARY]
#                 FILES <list-of-files>
# name :- name of the header file e.g. ${CMAKE_CURRENT_BINARY_DIR}/FooBar.h
# PREFIX :- (optional) when specified, used as the prefix for the generated
#           function/variable names.
# SUFFIX :- (optional) when specified, used as the suffix for the generated
#           function/variable names.
# BINARY :-(optional) when specified, all files are treated as binary and
#           encoded using base64.
# VARIABLE :- (optional) when specified, all the generate functions used to
#             access the compiled files are listed.
# FILES   :- list of files to compile in.
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
function(generate_header name)
  pv_parse_arguments(arg
    "PREFIX;SUFFIX;VARIABLE;FILES"
    "BINARY"
    ${ARGN}
    )

  set (function_names)
  set (input_files)
  set (have_xmls)
  foreach (input_file ${arg_FILES})
    get_filename_component(absolute_file "${input_file}" ABSOLUTE)
    get_filename_component(file_name "${absolute_file}" NAME_WE)
    list (APPEND function_names "${arg_PREFIX}${file_name}${arg_SUFFIX}")
    list (APPEND input_files "${absolute_file}")
    set (have_xmls TRUE)
  endforeach()

  set (base_64)
  if (arg_BINARY)
    set (base_64 "-base64")
  endif()

  if (have_xmls)
    add_custom_command(
      OUTPUT "${name}"
189
      COMMAND kwProcessXML
190 191 192 193 194 195 196
              ${base_64}
              ${name}
              \"${arg_PREFIX}\" 
              \"${arg_SUFFIX}\"
              \"${arg_SUFFIX}\"
              ${input_files}
      DEPENDS ${arg_FILES}
197
              kwProcessXML 
198 199 200 201 202 203 204 205 206
     ) 
  endif ()

  if (DEFINED arg_VARIABLE)
    set (${arg_VARIABLE} ${function_names} PARENT_SCOPE)
  endif()
endfunction()


207 208 209 210 211 212 213
# NOTE: coded-separator lists
#
# Workaround for inability to pass ';'-separated lists via command-line.
# From caller: replace '_'  -> '_u' and ';'  -> '_s'
# On receiver: replace '_s' -> ';'  and '_u' -> '_'


Utkarsh Ayachit's avatar
Utkarsh Ayachit committed
214
# GENERATE_HTMLS_FROM_XMLS can be used to generate HTML files for
215 216
# from a given list of xml files that correspond to server manager xmls.
# ARGUMENTS:
Utkarsh Ayachit's avatar
Utkarsh Ayachit committed
217
# output_files: OUT: variables set to the output files
218 219 220
# xmls: IN : full pathnames to xml files.
# output_dir : IN : full path to output directory where to generate the htmls.
#------------------------------------------------------------------------------
221
function (generate_htmls_from_xmls output_files xmls gui_xmls output_dir)
222
  # create a string from the xmls list to pass
223
  # since this list needs to be passed as an argument, we cannot escape the ";".
224 225
  # generate_proxydocumentation.cmake and generate_qhp.cmake have code to convert
  # these strings back to lists.
226
  set (xmls_string "")
227
  foreach (xml ${xmls})
228
    get_filename_component(xml "${xml}" ABSOLUTE)
229
    set (xmls_string "${xmls_string}${xml};")
230 231
  endforeach()
  
232
  set (gui_xmls_string "")
233
  foreach (gui_xml ${gui_xmls})
234
    get_filename_component(gui_xml "${gui_xml}" ABSOLUTE)
235
    set (gui_xmls_string "${gui_xmls_string}${gui_xml};")
236
  endforeach()
237

238 239 240 241 242 243
  # Escape ';' in lists
  string(REPLACE "_" "_u"  xmls_string "${xmls_string}")
  string(REPLACE ";" "_s"  xmls_string "${xmls_string}")
  string(REPLACE "_" "_u"  gui_xmls_string "${gui_xmls_string}")
  string(REPLACE ";" "_s"  gui_xmls_string "${gui_xmls_string}")

244 245 246 247 248
  set (all_xmls ${xmls} ${gui_xmls})
  list (GET all_xmls 0 first_xml)
  if (NOT first_xml)
    message(FATAL_ERROR "No xml specified!!!")
  endif()
249 250 251 252
  
  # extract the name from the first xml file. This is the name for temporary
  # file we use.
  get_filename_component(first_xml "${first_xml}" NAME)
253 254 255

  find_program(QT_XMLPATTERNS_EXECUTABLE
    xmlpatterns
256
    HINTS "${QT_BINARY_DIR}"
257 258 259 260 261
    DOC "xmlpatterns used to generate html from Proxy documentation.")
  mark_as_advanced(QT_XMLPATTERNS_EXECUTABLE)

  if (NOT EXISTS ${QT_XMLPATTERNS_EXECUTABLE})
    message(WARNING "Valid QT_XMLPATTERNS_EXECUTABLE not specified.")
262
  else()
263

264 265
    add_custom_command(
      OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/${first_xml}.xml"
266

267 268 269 270 271 272 273 274 275 276 277 278
      # process each html file to separate it out into files for each proxy.
      COMMAND ${CMAKE_COMMAND}
              -Dxmlpatterns:FILEPATH=${QT_XMLPATTERNS_EXECUTABLE}
              -Dxml_to_xml_xsl:FILEPATH=${ParaView_CMAKE_DIR}/smxml_to_xml.xsl
              -Dgenerate_category_rw_xsl:FILEPATH=${ParaView_CMAKE_DIR}/generate_category_rw.xsl
              -Dxml_to_html_xsl:FILEPATH=${ParaView_CMAKE_DIR}/xml_to_html.xsl
              -Dxml_to_wiki_xsl:FILEPATH=${ParaView_CMAKE_DIR}/xml_to_wiki.xsl.in
              -Dinput_xmls:STRING=${xmls_string}
              -Dinput_gui_xmls:STRING=${gui_xmls_string}
              -Doutput_dir:PATH=${output_dir}
              -Doutput_file:FILEPATH=${CMAKE_CURRENT_BINARY_DIR}/${first_xml}.xml
              -P ${ParaView_CMAKE_DIR}/generate_proxydocumentation.cmake
279

280 281 282 283
      DEPENDS ${xmls}
              ${ParaView_CMAKE_DIR}/smxml_to_xml.xsl
              ${ParaView_CMAKE_DIR}/xml_to_html.xsl
              ${ParaView_CMAKE_DIR}/generate_proxydocumentation.cmake
284

285
      WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}"
286

287
      COMMENT "Generating Documentation HTMLs from xmls")
288

289
    set (dependencies ${dependencies}
290 291 292
         "${CMAKE_CURRENT_BINARY_DIR}/${first_xml}.xml")
    set (${output_files} ${dependencies} PARENT_SCOPE)
  endif()
293
endfunction()
294

295
#------------------------------------------------------------------------------
296 297
# Function used to build a qhp (and qch) file. Adds a custom command to generate
# a ${DESTINATION_DIRECTORY}/${name}.qch.
298
# build_help_project(name 
299 300
#                    DESTINATION_DIRECTORY directory
#                    [DOCUMENTATION_SOURCE_DIR directory]
301 302
#                    [NAMESPACE namespacename (default:${name}.org)]
#                    [FOLDER virtualfoldername (default:${name})]
303 304 305 306
#                    [TABLE_OF_CONTENTS toc]
#                    [TABLE_OF_CONTENTS_FILE toc_file_name]
#                    [FILES relative filenames/wildcard-expressions]
#                   )
307
# name :- specifies the name for the qhp. The generated qhp file will be
308 309 310 311 312
#         ${DESTINATION_DIRECTORY}/${name}.qhp
# DESTINATION_DIRECTORY :- output-directory for the qhp file.
# DOCUMENTATION_SOURCE_DIR :- (optional) when specified, all files in this
#                             directory are copied over to the
#                             DESTINATION_DIRECTORY.
313 314 315 316 317 318
# NAMESPACE :- (optional; default=${name}.org") Namespace to use in qhp file.
# FOLDER :- (optional; default=${name}") virtual folder in qhp file.
# TABLE_OF_CONTENTS :- (optional) XML string <toc>..</toc> (see qhp file
#                      documentation). Used only when TABLE_OF_CONTENTS_FILE is
#                      not specified.
# TABLE_OF_CONTENTS_FILE :- file to read in to obtain the TABLE_OF_CONTENTS
319
# FILEPATTERNS :- (optional: default="*.*") list of files (names or wildcards) to list
320
#          in the qhp file. Note that these files/paths are relative to the
321 322
#          DESTINATION_DIRECTORY.
# DEPENDS :- (optional) targets or files that the qch generation target depends on.
323 324 325
#
# If neither TABLE_OF_CONTENTS or TABLE_OF_CONTENTS_FILE is specified, then the
# TOC is auto-generated.
326
#------------------------------------------------------------------------------
327 328
function(build_help_project name)
  pv_parse_arguments(arg
329
    "DESTINATION_DIRECTORY;DOCUMENTATION_SOURCE_DIR;NAMESPACE;FOLDER;TABLE_OF_CONTENTS;TABLE_OF_CONTENTS_FILE;FILEPATTERNS;DEPENDS"
330 331 332 333
    ""
    ${ARGN}
    )

334 335 336 337
  if (NOT PARAVIEW_ENABLE_EMBEDDED_DOCUMENTATION)
    return()
  endif()

338 339
  if (NOT DEFINED arg_DESTINATION_DIRECTORY)
    message(FATAL_ERROR "No DESTINATION_DIRECTORY specified in build_help_project()")
340 341
  endif()

342 343
  find_program(QT_HELP_GENERATOR
    qhelpgenerator
344
    HINTS "${QT_BINARY_DIR}"
345 346 347 348 349 350 351
    DOC "qhelpgenerator used to compile Qt help project files")
  mark_as_advanced(QT_HELP_GENERATOR)

  if (NOT EXISTS ${QT_HELP_GENERATOR})
    message(WARNING "Valid QT_HELP_GENERATOR not specified.")
  endif()

352 353
  # set default values for optional arguments.
  pv_set_if_not_set(arg_FILEPATTERNS "*.*")
354 355
  pv_set_if_not_set(arg_NAMESPACE "${name}.org")
  pv_set_if_not_set(arg_FOLDER "${name}")
356
  pv_set_if_not_set(arg_DEPENDS "")
357 358 359 360 361 362 363

  # if filename is specified, it takes precendence.
  # setup toc variable to refer to the TOC xml dom.
  if (DEFINED arg_TABLE_OF_CONTENTS_FILE)
    file(READ ${arg_TABLE_OF_CONTENTS_FILE} arg_TABLE_OF_CONTENTS)
  endif()

364
  set (qhp_filename ${arg_DESTINATION_DIRECTORY}/${name}.qhp)
365 366

  set (extra_args)
367
  if (arg_DOCUMENTATION_SOURCE_DIR)
368 369 370 371 372 373 374 375 376
    set (extra_args
      # copy all htmls from source to destination directory (same location where the
      # qhp file is present.
      COMMAND ${CMAKE_COMMAND} -E copy_directory
              "${arg_DOCUMENTATION_SOURCE_DIR}"
              "${arg_DESTINATION_DIRECTORY}"
      )
  endif()

377 378
  if (NOT DEFINED arg_TABLE_OF_CONTENTS)
    # sanitize arg_FILEPATTERNS since we pass it as a command line argument.
379 380 381 382
    # Escape ';' in lists
    string(REPLACE "_" "_u"  arg_FILEPATTERNS "${arg_FILEPATTERNS}")
    string(REPLACE ";" "_s"  arg_FILEPATTERNS "${arg_FILEPATTERNS}")

383 384 385 386
    set (extra_args ${extra_args}

    # generate the toc at run-time.
    COMMAND ${CMAKE_COMMAND}
387 388 389 390 391
            -Doutput_file:FILEPATH=${qhp_filename}
            -Dfile_patterns:STRING="${arg_FILEPATTERNS}"
            -Dnamespace:STRING="${arg_NAMESPACE}"
            -Dfolder:PATH=${arg_FOLDER}
            -Dname:STRING="${name}"
392
            -P "${ParaView_CMAKE_DIR}/generate_qhp.cmake"
393 394 395 396 397 398 399 400
    )
  else ()
    # toc is provided, we'll just configure the file.
    set (files)
    foreach(filename ${arg_FILEPATTERNS})
      set (files "${files}<file>${filename}</file>\n")
    endforeach()

401
    configure_file(${ParaView_CMAKE_DIR}/build_help_project.qhp.in
402 403 404 405
      ${qhp_filename})
    list (APPEND arg_DEPENDS ${qhp_filename})
  endif()

406
  ADD_CUSTOM_COMMAND(
407
    OUTPUT ${arg_DESTINATION_DIRECTORY}/${name}.qch
408
    DEPENDS ${arg_DEPENDS}
409
            ${ParaView_CMAKE_DIR}/generate_qhp.cmake
410 411
  
    ${extra_args}
412

413 414
    # Now, compile the qhp file to generate the qch.
    COMMAND ${QT_HELP_GENERATOR}
415 416
            ${qhp_filename}
            -o ${arg_DESTINATION_DIRECTORY}/${name}.qch
417 418
  
    COMMENT "Compiling Qt help project ${name}.qhp"
419 420

    WORKING_DIRECTORY "${arg_DESTINATION_DIRECTORY}"
421
  )
422
endfunction()
423 424 425 426 427 428 429 430 431

macro(pv_set_link_interface_libs target)
  # if not lion then we need to set LINK_INTERFACE_LIBRARIES to reduce the number
  # of libraries we link against there is a limit of 253.
  if(CMAKE_SYSTEM_NAME STREQUAL "Darwin" AND CMAKE_SYSTEM_VERSION VERSION_LESS 11.0) 
    set_property(TARGET ${target}
      PROPERTY LINK_INTERFACE_LIBRARIES "${ARGN}")
  endif()
endmacro()
432

433 434 435
#------------------------------------------------------------------------------
# replacement for vtk-add executable that also adds the install rules.
#------------------------------------------------------------------------------
436
include(pvForwardingExecutable)
437 438 439 440

function(pv_add_executable name)
  set (VTK_EXE_SUFFIX)
  if(UNIX AND VTK_BUILD_FORWARDING_EXECUTABLES)
441 442
    set(PV_INSTALL_LIBRARY_DIR ${VTK_INSTALL_LIBRARY_DIR})
    pv_add_executable_with_forwarding(VTK_EXE_SUFFIX ${name} ${ARGN})
443 444 445 446 447
    set_property(GLOBAL APPEND PROPERTY VTK_TARGETS ${name})
  else()
    add_executable(${name} ${ARGN})
    set_property(GLOBAL APPEND PROPERTY VTK_TARGETS ${name})
  endif()
448 449 450
  if (PV_EXE_JOB_LINK_POOL)
    set_property(TARGET "${name}" PROPERTY JOB_POOL_LINK ${PV_EXE_JOB_LINK_POOL})
  endif ()
451 452 453 454
  if (APPLE AND NOT PARAVIEW_DO_UNIX_STYLE_INSTALLS)
    set_target_properties("${name}" PROPERTIES
      INSTALL_RPATH "@executable_path/../Libraries;@executable_path/../Plugins")
  endif ()
455 456 457
  pv_executable_install(${name} "${VTK_EXE_SUFFIX}")
endfunction()

458
#------------------------------------------------------------------------------
459
# Function used to add install rules for executables.
460 461
#------------------------------------------------------------------------------
function (pv_executable_install name exe_suffix)
462 463 464 465 466 467 468 469 470 471 472 473 474 475 476
  if (NOT VTK_INSTALL_NO_RUNTIME)
    if (exe_suffix)
      # we have two executables to install, one in the bin dir and another in the
      # lib dir

      # install the real-binary in the lib-dir
      install(TARGETS ${name}
              DESTINATION ${VTK_INSTALL_LIBRARY_DIR}
              COMPONENT Runtime)
    endif()

    # install the launcher binary in the binary dir. When exe_suffix is empty, the
    # launcher binary is same as the real binary.
    install(TARGETS ${name}${exe_suffix}
            DESTINATION ${VTK_INSTALL_RUNTIME_DIR}
477 478 479
            COMPONENT Runtime)
  endif()
endfunction()
480

481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500
#------------------------------------------------------------------------------
# Function used to copy arbitrary files matching certain patterns.
# Usage:
# copy_files_recursive(<source-dir>
#   DESTINATION <destination-dir>
#   [LABEL "<label to use>"]
#   [OUTPUT "<file generated to mark end of copying>"]
#   [REGEX <regex> [EXCLUDE]]
#   )
# One can specify multiple REGEX or REGEX <regex> EXCLUDE arguments.
#------------------------------------------------------------------------------
function(copy_files_recursive source-dir)
  set (dest-dir)
  set (patterns)
  set (exclude-patterns)
  set (output-file)
  set (label "Copying files")

  set (doing "")
  foreach (arg ${ARGN})
Utkarsh Ayachit's avatar
Utkarsh Ayachit committed
501
    if (arg MATCHES "^(DESTINATION|REGEX|OUTPUT|LABEL)$")
502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570
      set (doing "${arg}")
    elseif ("${doing}" STREQUAL "DESTINATION")
      set (doing "")
      set (dest-dir "${arg}")
    elseif ("${doing}" STREQUAL "REGEX")
      set (doing "SET")
      list (APPEND patterns "${arg}")
    elseif (("${arg}" STREQUAL "EXCLUDE") AND ("${doing}" STREQUAL "SET"))
      set (doing "")
      list (GET patterns -1 cur-pattern)
      list (REMOVE_AT patterns -1)
      list (APPEND exclude-patterns "${cur-pattern}")
    elseif ("${doing}" STREQUAL "OUTPUT")
      set (doing "")
      set (output-file "${arg}")
    elseif ("${doing}" STREQUAL "LABEL")
      set (doing "")
      set (label "${arg}")
    else()
      message(AUTHOR_WARNING "Unknown argument [${arg}]")
    endif()
  endforeach()

  set (match-regex)
  foreach (_item ${patterns})
    if (match-regex)
      set (match-regex "${match-regex}")
    endif()
    set (match-regex "${match-regex}${_item}")
  endforeach()

  set (exclude-regex)
  foreach (_item ${exclude-patterns})
    if (exclude-regex)
      set (exclude-regex "${exclude-regex}|")
    endif()
    set (exclude-regex "${exclude-regex}${_item}")
  endforeach()

  file(GLOB_RECURSE _all_files RELATIVE "${source-dir}" "${source-dir}/*")
  
  set (all_files)
  set (copy-commands)
  foreach (_file ${_all_files})
    if (exclude-regex AND ("${_file}" MATCHES "${exclude-regex}"))
      # skip
    elseif ("${_file}" MATCHES "${match-regex}")
      set (in-file "${source-dir}/${_file}")
      set (out-file "${dest-dir}/${_file}")
      get_filename_component(out-path ${out-file} PATH)
      list (APPEND all_files ${in-file})
      set (copy-commands "${copy-commands}
        file(COPY \"${in-file}\" DESTINATION \"${out-path}\")")
    endif()
  endforeach()

  get_filename_component(_name ${output-file} NAME)
  set(CMAKE_CONFIGURABLE_FILE_CONTENT ${copy-commands})
  configure_file(${CMAKE_ROOT}/Modules/CMakeConfigurableFile.in
    "${CMAKE_CURRENT_BINARY_DIR}/${_name}.cfr.cmake" @ONLY)
  unset(CMAKE_CONFIGURABLE_FILE_CONTENT)

  add_custom_command(OUTPUT ${output-file}
    COMMAND ${CMAKE_COMMAND} -P "${CMAKE_CURRENT_BINARY_DIR}/${_name}.cfr.cmake"
    COMMAND ${CMAKE_COMMAND} -E touch ${output-file}
    DEPENDS ${all_files}
            "${CMAKE_CURRENT_BINARY_DIR}/${_name}.cfr.cmake"
    COMMENT ${label})
endfunction()