1# - Add a target for files that just need to be copied
2#
3#  include(FileCopyTargets)
4#  add_file_copy_target(<target_name> <directory to copy to> <filename> [<filename>...])
5#    Creates a custom target that copies the files to a directory, if needed.
6#    Relative paths for the destination directory are considered with
7#    with respect to CMAKE_CURRENT_BINARY_DIR
8#    You can use this target in all the usual ways, including
9#    add_dependencies(some_other_target this_target) to specify that another
10#    target depends on this.
11#
12#  install_file_copy_target(<target_name> [arguments to INSTALL(PROGRAMS ...) ])
13#    Works just the same as INSTALL(PROGRAMS ...) because it wraps it to install
14#    the files you specified in add_file_copy_target
15#
16#
17# Requires CMake 2.6 or newer (uses the 'function' command)
18#
19# Original Author:
20# 2009-2010 Ryan Pavlik <rpavlik@iastate.edu> <abiryan@ryand.net>
21# http://academic.cleardefinition.com
22# Iowa State University HCI Graduate Program/VRAC
23#
24# Copyright Iowa State University 2009-2010.
25# Distributed under the Boost Software License, Version 1.0.
26# (See accompanying file LICENSE_1_0.txt or copy at
27# http://www.boost.org/LICENSE_1_0.txt)
28
29if(__add_file_copy_target)
30	return()
31endif()
32set(__add_file_copy_target YES)
33
34define_property(TARGET
35	PROPERTY
36	FILE_COPY_TARGET
37	BRIEF_DOCS
38	"File Copy target"
39	FULL_DOCS
40	"Is this a target created by add_file_copy_target?")
41
42function(add_file_copy_target _target _dest)
43	if(NOT ARGN)
44		message(WARNING
45			"In add_file_copy_target call for target ${_target}, no source files were specified!")
46		return()
47	endif()
48
49	set(ALLFILES)
50	set(SOURCES)
51	foreach(fn ${ARGN})
52		# Produce an absolute path to the input file
53		if(IS_ABSOLUTE "${fn}")
54			get_filename_component(fullpath "${fn}" ABSOLUTE)
55			get_filename_component(fn "${fn}" NAME)
56		else()
57			get_filename_component(fullpath
58				"${CMAKE_CURRENT_SOURCE_DIR}/${fn}"
59				ABSOLUTE)
60		endif()
61
62		# Clean up output file name
63		get_filename_component(absout "${_dest}/${fn}" ABSOLUTE)
64
65		add_custom_command(OUTPUT "${absout}"
66			COMMAND
67			${CMAKE_COMMAND}
68			ARGS -E make_directory "${_dest}"
69			COMMAND
70			${CMAKE_COMMAND}
71			ARGS -E copy "${fullpath}" "${_dest}"
72			MAIN_DEPENDENCY "${fullpath}"
73			VERBATIM
74			COMMENT "Copying ${fn} to ${absout}")
75		list(APPEND SOURCES "${fullpath}")
76		list(APPEND ALLFILES "${absout}")
77	endforeach()
78
79	# Custom target depending on all the file copy commands
80	add_custom_target(${_target}
81		SOURCES ${SOURCES}
82		DEPENDS ${ALLFILES})
83
84	set_property(TARGET ${_target} PROPERTY FILE_COPY_TARGET YES)
85endfunction()
86
87function(install_file_copy_target _target)
88	get_target_property(_isFCT ${_target} FILE_COPY_TARGET)
89	if(NOT _isFCT)
90		message(WARNING
91			"install_file_copy_target called on a target not created with add_file_copy_target!")
92		return()
93	endif()
94
95	# Get sources
96	get_target_property(_srcs ${_target} SOURCES)
97
98	# Remove the "fake" file forcing build
99	list(REMOVE_AT _srcs 0)
100
101	# Forward the call to install
102	install(PROGRAMS ${_srcs} ${ARGN})
103endfunction()
104