summaryrefslogtreecommitdiffhomepage
path: root/cmake/modules/AddDatafile.cmake
blob: f68ad048c7e33f836f5f924e841df24996935ca2 (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
# Adds a target that creates a datafile archive containing all files under selected source directory tree. For example:
#
#     add_datafile(
#         shatter
#         SOURCE shatter
#         OUTPUT shatter.dat
#         RESOURCES resource_shatter
#     )
#
# Will create 'shatter.dat' in the respective binary directory containing all files in 'shatter' subdirectory of current
# source directory. SOURCE option's argument defaults to the target name. OUTPUT option's argument defaults to the
# target name with ".dat" suffix.
#
# Optionally, RESOURCES can be used to specify binary resource targets from AddDownloadableResource that will provide
# additional directories with files that will also get included in the final datafile. In case of conflicts, files from
# source tree will be used. This is to allow working with binary assets located in source directory.
#
# To create the archive the Datafile.exe is used - for non-Windows platforms an emulator that can run it is required.

function(add_datafile)
	set(DATAFILE_TARGET ${ARGV0})
	cmake_parse_arguments(
		PARSE_ARGV 1
		DATAFILE
		""
		"SOURCE;OUTPUT"
		"RESOURCES"
		)
	if(NOT DEFINED DATAFILE_SOURCE)
		set(DATAFILE_SOURCE ${DATAFILE_TARGET})
	endif()
	if(NOT DEFINED DATAFILE_OUTPUT)
		set(DATAFILE_OUTPUT "${DATAFILE_TARGET}.dat")
	endif()
	cmake_path(ABSOLUTE_PATH DATAFILE_SOURCE NORMALIZE BASE_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
	cmake_path(ABSOLUTE_PATH DATAFILE_OUTPUT NORMALIZE BASE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
	if(CMAKE_HOST_SYSTEM_NAME MATCHES Windows)
		set(DATAFILE_VAR PATH)
		set(DATAFILE_PATHS $ENV{PATH})
		list(PREPEND DATAFILE_PATHS $<TARGET_FILE_DIR:Zlib::zlib>)
	else()
		set(DATAFILE_VAR WINEPATH)
		set(DATAFILE_PATHS $ENV{WINEPATH})
		list(PREPEND DATAFILE_PATHS $<TARGET_FILE_DIR:Zlib::zlib>)
		list(PREPEND DATAFILE_PATHS $<SHELL_PATH:${CMAKE_FIND_ROOT_PATH}/bin>)
	endif()
	add_custom_command(
		OUTPUT ${DATAFILE_OUTPUT}
		COMMAND ${CMAKE_COMMAND} -E rm -f ${DATAFILE_OUTPUT}
		COMMENT "Creating datafile ${DATAFILE_TARGET}"
		)
	foreach(RESOURCE_TARGET IN ITEMS ${DATAFILE_RESOURCES})
		get_property(RESOURCE_DIRECTORY TARGET ${RESOURCE_TARGET} PROPERTY RESOURCE_DIRECTORY)
		add_custom_command(
			OUTPUT ${DATAFILE_OUTPUT} APPEND
			DEPENDS ${RESOURCE_TARGET}
			COMMAND
				${CMAKE_COMMAND} -E chdir ${RESOURCE_DIRECTORY}
				${CMAKE_COMMAND} -E env "${DATAFILE_VAR}='${DATAFILE_PATHS}'"
				${CMAKE_CORSSCOMPILING_EMULATOR}
				$<TARGET_FILE:Datafile> $<SHELL_PATH:${DATAFILE_OUTPUT}> -mak
			)
	endforeach()
	file(
		GLOB_RECURSE DATAFILE_DEPENDENCIES
		LIST_DIRECTORIES true
		CONFIGURE_DEPENDS
		"${DATAFILE_SOURCE}/*"
		)
	add_custom_command(
		OUTPUT ${DATAFILE_OUTPUT} APPEND
		DEPENDS ${DATAFILE_DEPENDENCIES}
		COMMAND
			${CMAKE_COMMAND} -E chdir ${DATAFILE_SOURCE}
			${CMAKE_COMMAND} -E env "${DATAFILE_VAR}='${DATAFILE_PATHS}'"
			${CMAKE_CORSSCOMPILING_EMULATOR}
			$<TARGET_FILE:Datafile> $<SHELL_PATH:${DATAFILE_OUTPUT}> -mak
		)
	add_custom_target(${DATAFILE_TARGET} ALL DEPENDS ${DATAFILE_OUTPUT})
endfunction()