Skip to content
Snippets Groups Projects
Commit 7175e4f2 authored by Steffen Müthing's avatar Steffen Müthing
Browse files

Merge branch 'feature/FS1535-cmake-sourcedir-to-builddir-utilities'

This branch adds CMake macros for copying / symlinking files from the source
directory to the build directory and for adding a symlink back to the corresponding
directory in the source tree into every directory in the build tree.

* feature/FS1535-cmake-sourcedir-to-builddir-utilities:
  [CMake] Do copies as a fall-back if symlinks are not available on the platform
  [Cmake] Add macros for copying files from the source tree to the build tree.
  [CMake] add macro dune_symlink_to_source_files
  [CMake] Add macro dune_symlink_to_source_tree
parents f2d36eef 7f43465d
No related branches found
No related tags found
No related merge requests found
......@@ -1168,3 +1168,85 @@ macro(add_dune_all_flags targets)
target_link_libraries(${target} ${DUNE_LIBS} ${libs})
endforeach()
endmacro(add_dune_all_flags targets)
# This macro adds a file-copy command.
# The file_name is the name of a file that exists
# in the source tree. This file will be copied
# to the build tree when executing this command.
# Notice that this does not create a top-level
# target. In order to do this you have to additionally
# call add_custom_target(...) with dependency
# on the file.
macro(dune_add_copy_command file_name)
add_custom_command(
OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/${file_name}"
COMMAND ${CMAKE_COMMAND}
ARGS -E copy "${CMAKE_CURRENT_SOURCE_DIR}/${file_name}" "${file_name}"
DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/${file_name}"
)
endmacro(dune_add_copy_command file_name)
# This macro adds a file-copy target under given target_name.
# The file_name is the name of a file that exists
# in the source tree. This file will be copied
# to the build tree.
macro(dune_add_copy_target target_name file_name)
dune_add_copy_command(${file_name})
add_custom_target("${target_name}" ALL DEPENDS "${file_name}")
endmacro(dune_fufem_add_copy_target target_name file_name)
# This macro adds a copy-dependecy to a target
# The file_name is the name of a file that exists
# in the source tree. This file will be copied
# to the build tree.
macro(dune_add_copy_dependency target file_name)
message(STATUS "Adding copy-to-build-dir dependency for ${file_name} to target ${target}")
dune_add_copy_target("${target}_copy_${file_name}" "${file_name}")
add_dependencies(${target} "${target}_copy_${file_name}")
endmacro(dune_add_copy_dependency)
# add a symlink called src_dir to all directories in the build tree.
# That symlink points to the corresponding directory in the source tree.
# Call the macro from the toplevel CMakeLists.txt file of your project.
# You can also call it from some other directory, creating only symlinks
# in that directory and all directories below. A warning is issued on
# Windows systems.
macro(dune_symlink_to_source_tree)
# check for Windows to issue a warning
if(${CMAKE_SYSTEM_NAME} STREQUAL "Windows")
if(NOT DEFINED DUNE_WINDOWS_SYMLINK_WARNING)
message(WARNING "Your module wanted to create symlinks, but you cannot do that on your platform.")
set(DUNE_WINDOWS_SYMLINK_WARNING)
endif()
else()
# get a list of all files in the current source directory and below.
file(GLOB_RECURSE files RELATIVE ${CMAKE_SOURCE_DIR} "*")
# iterate over all files, extract the directory name and write a symlink in the corresponding build directory
foreach(f ${files})
get_filename_component(dir ${f} DIRECTORY)
execute_process(COMMAND ${CMAKE_COMMAND} "-E" "create_symlink" "${CMAKE_SOURCE_DIR}/${dir}" "${CMAKE_BINARY_DIR}/${dir}/src_dir")
endforeach()
endif()
endmacro(dune_symlink_to_source_tree)
# add symlinks to the build tree, which point to files in the source tree.
# Foreach file given in "files", a symlink of that name is created in the
# corresponding build directory. Use for ini files, grid files etc. A warning
# is issued on Windows systems.
macro(dune_symlink_to_source_files files)
# create symlinks for all given files
foreach(f ${files})
# check for Windows to issue a warning
if(${CMAKE_SYSTEM_NAME} STREQUAL "Windows")
if(NOT DEFINED DUNE_WINDOWS_SYMLINK_WARNING)
message(WARNING "Your module wanted to create symlinks, but you cannot do that on your platform.")
set(DUNE_WINDOWS_SYMLINK_WARNING)
endif()
dune_add_copy_command(${f})
else()
# create symlink
execute_process(COMMAND ${CMAKE_COMMAND} "-E" "create_symlink" "${CMAKE_CURRENT_SOURCE_DIR}/${f}" "${CMAKE_CURRENT_BINARY_DIR}/${f}")
endif()
endforeach()
endmacro(dune_symlink_to_source_files files)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment