1# - Copy the resources your app needs to the build tree.
2#
3#  copy_resources_to_build_tree(<target_name>)
4#
5# Requires CMake 2.6 or newer (uses the 'function' command)
6#
7# Original Author:
8# 2009-2010 Ryan Pavlik <rpavlik@iastate.edu> <abiryan@ryand.net>
9# http://academic.cleardefinition.com
10# Iowa State University HCI Graduate Program/VRAC
11#
12# Copyright Iowa State University 2009-2010.
13# Distributed under the Boost Software License, Version 1.0.
14# (See accompanying file LICENSE_1_0.txt or copy at
15# http://www.boost.org/LICENSE_1_0.txt)
16
17if(__copy_resources_to_build_tree)
18	return()
19endif()
20set(__copy_resources_to_build_tree YES)
21
22function(copy_resources_to_build_tree _target)
23	get_target_property(_resources ${_target} RESOURCE)
24	if(NOT _resources)
25		# Bail if no resources
26		message(STATUS
27			"Told to copy resources for target ${_target}, but "
28			"no resources are set!")
29		return()
30	endif()
31
32	get_target_property(_path ${_target} LOCATION)
33	get_filename_component(_path "${_path}" PATH)
34
35	if(NOT MSVC AND NOT "${CMAKE_GENERATOR}" MATCHES "Makefiles")
36		foreach(_config ${CMAKE_CONFIGURATION_TYPES})
37			get_target_property(_path${_config} ${_target} LOCATION_${_config})
38			get_filename_component(_path${_config} "${_path${_config}}" PATH)
39			add_custom_command(TARGET ${_target}
40						POST_BUILD
41						COMMAND
42						${CMAKE_COMMAND}
43						ARGS -E make_directory "${_path${_config}}/"
44						COMMENT "Creating directory ${_path${_config}}/")
45		endforeach()
46	endif()
47
48	foreach(_res ${_resources})
49		if(NOT IS_ABSOLUTE "${_res}")
50			get_filename_component(_res "${_res}" ABSOLUTE)
51		endif()
52		get_filename_component(_name "${_res}" NAME)
53
54		if(MSVC)
55			# Working dir is solution file dir, not exe file dir.
56			add_custom_command(TARGET ${_target}
57				POST_BUILD
58				COMMAND
59				${CMAKE_COMMAND}
60				ARGS -E copy "${_res}" "${CMAKE_BINARY_DIR}/"
61				COMMENT "Copying ${_name} to ${CMAKE_BINARY_DIR}/ for MSVC")
62		else()
63			if("${CMAKE_GENERATOR}" MATCHES "Makefiles")
64				add_custom_command(TARGET ${_target}
65					POST_BUILD
66					COMMAND
67					${CMAKE_COMMAND}
68					ARGS -E copy "${_res}" "${_path}/"
69					COMMENT "Copying ${_name} to ${_path}/")
70			else()
71				foreach(_config ${CMAKE_CONFIGURATION_TYPES})
72					add_custom_command(TARGET ${_target}
73						POST_BUILD
74						COMMAND
75						${CMAKE_COMMAND}
76						ARGS -E copy "${_res}" "${_path${_config}}"
77						COMMENT "Copying ${_name} to ${_path${_config}}")
78				endforeach()
79
80			endif()
81		endif()
82	endforeach()
83endfunction()
84