summaryrefslogtreecommitdiffhomepage
path: root/cmake/modules/AddDownloadableResource.cmake
blob: 9d00f586276ff7ff0447f60a47609609f0929a10 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# Adds a target that downloads an archive containing binary files that should be part of game distribution but are not
# part of the version control system. For example:
#
#     add_downloadable_resource(
#         resource_shatter
#         ARCHIVE shatter.tar.xz
#         DIRECTORY shatter.d
#         BASE_URL https://ftp.ignore.pl/starshatter/resource/latest
#         SHA1 bbed14649213a45499e0a158142f19fd619a54af
#     )
#
# Will download 'shatter.tar.xz' from 'https://ftp.ignore.pl/starshatter/resouce/latest' and unpack it into 'shatter.d'
# subdirectory under respective binary directory. The archive will be validated for correctness with SHA1.
#
# Selected options provide default arguments:
#  * ARCHIVE defaults to '<target_name>.tar.xz'
#  * DIRECTORY defaults to '<target_name>.d'
#  * BASE_URL defaults to 'https://ftp.ignore.pl/starshatter/resource/latest'
#
# Targets are designed to be used with AddDatafile targets via their RESOURCES option:
#
#     add_downloadable_resource(resource_shatter ...)
#     add_datafile(
#         shatter
#         RESOURCES resource_shatter
#     )
#
# This will automatically make the datafile generated by 'shatter' target to include files downloaded by
# 'resource_shatter'.
#
# Downloaded archives are not cleaned by default to avoid unnecessarily long build times. Instead they need to be
# cleaned explicitly either by removing them manually or by running 'clean_downloaded_<target_name>' target.

set(ADD_DOWNLOADABLE_RESOURCE_SCRIPT ${CMAKE_CURRENT_LIST_DIR}/download_resource.cmake.in)
function(add_downloadable_resource)
	set(RESOURCE_TARGET ${ARGV0})
	cmake_parse_arguments(
		PARSE_ARGV 1
		RESOURCE
		""
		"ARCHIVE;DIRECTORY;BASE_URL;SHA1"
		""
		)
	if(NOT DEFINED RESOURCE_ARCHIVE)
		set(RESOURCE_ARCHIVE ${RESOURCE_TARGET}.tar.xz)
	endif()
	if(NOT DEFINED RESOURCE_BASE_URL)
		set(RESOURCE_BASE_URL https://ftp.ignore.pl/starshatter/resources/latest)
	endif()
	if(NOT DEFINED RESOURCE_DIRECTORY)
		set(RESOURCE_DIRECTORY ${RESOURCE_TARGET}.d)
	endif()
	cmake_path(ABSOLUTE_PATH RESOURCE_ARCHIVE NORMALIZE BASE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
	cmake_path(ABSOLUTE_PATH RESOURCE_DIRECTORY NORMALIZE BASE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
	cmake_path(GET RESOURCE_ARCHIVE FILENAME RESOURCE_ARCHIVE_BASENAME)
	string(REGEX REPLACE "/$" "" RESOURCE_BASE_URL "${RESOURCE_BASE_URL}")
	set(DOWNLOAD_SCRIPT ${CMAKE_CURRENT_BINARY_DIR}/download_${RESOURCE_TARGET}.cmake)
	configure_file(${ADD_DOWNLOADABLE_RESOURCE_SCRIPT} ${DOWNLOAD_SCRIPT} @ONLY)
	add_custom_target(
		download_${RESOURCE_TARGET}
		COMMENT "Downloading ${RESOURCE_TARGET}"
		COMMAND ${CMAKE_COMMAND} -P ${DOWNLOAD_SCRIPT}
		)
	add_custom_target(
		clean_downloaded_${RESOURCE_TARGET}
		COMMENT "Removing downloaded ${RESOURCE_TARGET}"
		COMMAND ${CMAKE_COMMAND} -E rm -f ${RESOURCE_ARCHIVE}
		COMMAND ${CMAKE_COMMAND} -E remove_directory ${RESOURCE_DIRECTORY}
		)
	add_custom_command(
		OUTPUT ${RESOURCE_DIRECTORY}
		DEPENDS download_${RESOURCE_TARGET} ${RESOURCE_ARCHIVE}
		COMMENT "Unpacking ${RESOURCE_TARGET}"
		COMMAND ${CMAKE_COMMAND} -E make_directory ${RESOURCE_DIRECTORY}
		COMMAND
			${CMAKE_COMMAND} -E chdir ${RESOURCE_DIRECTORY}
			${CMAKE_COMMAND} -E tar xf ${RESOURCE_ARCHIVE}
		)
	add_custom_target(${RESOURCE_TARGET} DEPENDS ${RESOURCE_DIRECTORY})
	set_property(
		TARGET ${RESOURCE_TARGET}
		PROPERTY RESOURCE_DIRECTORY ${RESOURCE_DIRECTORY}
		)
endfunction()