changeset 553:cb4508f45048

cmake: rework assets output directory
author David Demelier <markand@malikania.fr>
date Tue, 07 Mar 2023 20:58:00 +0100
parents ffd972a3d084
children cdbc13ceff85
files cmake/MlkBcc.cmake cmake/MlkMap.cmake cmake/MlkTileset.cmake examples/example-tileset/example-tileset.c libmlk-example/CMakeLists.txt
diffstat 5 files changed, 68 insertions(+), 29 deletions(-) [+]
line wrap: on
line diff
--- a/cmake/MlkBcc.cmake	Tue Mar 07 20:45:00 2023 +0100
+++ b/cmake/MlkBcc.cmake	Tue Mar 07 20:58:00 2023 +0100
@@ -16,9 +16,38 @@
 # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 #
 
+#
+# mlk_bcc(
+#   OUTPUT_VAR variable
+#   ASSETS files...
+#   [OUTPUT_DIRECTORY directory]
+# )
+#
+# Convert binary files to header files using mlk-bcc utility.
+#
+# This macro will generate all files under the OUTPUT_DIRECTORY variable which
+# defaults to ${CMAKE_CURRENT_BINARY_DIR}/assets if unset.
+#
+#
+# It will also keep the last parent directory name under the output directory
+# because most users will arrange more files under a specific file hierarchy,
+# this also avoid a file name conflict.
+#
+# Example, given the following assets files:
+#
+# - assets/sprites/foo.png
+# - assets/sounds/volume-up.wav
+# - assets/sounds/volume-down.wav
+#
+# The following files will be generated:
+#
+# - <output-directory>/assets/sprites/foo.h
+# - <output-directory>/assets/sounds/volume-up.h
+# - <output-directory>/assets/sounds/volume-down.h
+#
 macro(mlk_bcc)
 	set(options "")
-	set(oneValueArgs OUTPUTS_VAR)
+	set(oneValueArgs "OUTPUT_DIRECTORY;OUTPUTS_VAR")
 	set(multiValueArgs ASSETS)
 
 	cmake_parse_arguments(_bcc "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
@@ -29,36 +58,44 @@
 		message(FATAL_ERROR "Missing ASSETS")
 	endif ()
 
+	if (NOT _bcc_OUTPUT_DIRECTORY)
+		set(_bcc_base_directory ${CMAKE_CURRENT_BINARY_DIR}/assets)
+	else ()
+		set(_bcc_base_directory ${_bcc_OUTPUT_DIRECTORY})
+	endif ()
+
 	foreach (a ${_bcc_ASSETS})
-		cmake_path(
-			RELATIVE_PATH a
-			BASE_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
-			OUTPUT_VARIABLE output
-		)
-		cmake_path(GET output PARENT_PATH outputdir)
-		cmake_path(GET output EXTENSION extension)
-		cmake_path(REPLACE_EXTENSION output .h)
-		cmake_path(REMOVE_EXTENSION output OUTPUT_VARIABLE outputname)
+		# Determine output parent directory name.
+		get_filename_component(_bcc_dirname ${a} DIRECTORY)
+		get_filename_component(_bcc_dirname ${_bcc_dirname} NAME)
 
-		if (extension MATCHES ".sql")
-			set(args "-0cs")
+		# Determine output file name.
+		cmake_path(GET a FILENAME _bcc_filename)
+		cmake_path(REPLACE_EXTENSION _bcc_filename .h)
+		set(_bcc_output_file ${_bcc_base_directory}/${_bcc_dirname}/${_bcc_filename})
+
+		# Text files are better NUL terminated.
+		cmake_path(GET a EXTENSION _bcc_extension)
+
+		if (_bcc_extension MATCHES "\\.sql")
+			set(_bcc_args "-0cs")
 		else ()
-			set(args "-cs")
+			set(_bcc_args "-cs")
 		endif ()
 
-		message("===> ${output}")
-		set(outputfile ${CMAKE_CURRENT_BINARY_DIR}/${output})
+		# This is the mlk-bcc variable to generate the C identifier.
+		set(_bcc_var "assets_${_bcc_dirname}_${_bcc_filename}")
 
 		add_custom_command(
-			OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${output}
+			OUTPUT ${_bcc_output_file}
 			COMMAND
-				${CMAKE_COMMAND} -E make_directory ${CMAKE_CURRENT_BINARY_DIR}/${outputdir}
+				${CMAKE_COMMAND} -E make_directory ${_bcc_base_directory}/${_bcc_dirname}
 			COMMAND
-				$<TARGET_FILE:mlk-bcc> ${args} ${a} ${outputname} > ${outputfile}
-			COMMENT "Generating ${output}"
+				$<TARGET_FILE:mlk-bcc> ${_bcc_args} ${a} ${_bcc_var} > ${_bcc_output_file}
+			COMMENT "Generating asset ${_bcc_dirname}/${_bcc_filename}"
 			DEPENDS $<TARGET_FILE:mlk-bcc> ${a}
 		)
 
-		list(APPEND ${_bcc_OUTPUTS_VAR} ${CMAKE_CURRENT_BINARY_DIR}/${output})
+		list(APPEND ${_bcc_OUTPUTS_VAR} ${_bcc_output_file})
 	endforeach ()
 endmacro()
--- a/cmake/MlkMap.cmake	Tue Mar 07 20:45:00 2023 +0100
+++ b/cmake/MlkMap.cmake	Tue Mar 07 20:58:00 2023 +0100
@@ -18,12 +18,13 @@
 
 function(mlk_map input output)
 	set(cmd COMMAND $<TARGET_FILE:mlk-map> < ${input} > ${output})
+	get_filename_component(filename ${output} NAME)
 
 	add_custom_command(
 		OUTPUT ${output}
 		COMMAND ${cmd}
-		DEPENDS $<TARGET_FILE:mlk-map>
-		COMMENT "Generating map ${output}"
+		DEPENDS $<TARGET_FILE:mlk-map> ${input}
+		COMMENT "Generating map ${filename}"
 	)
 endfunction()
 
--- a/cmake/MlkTileset.cmake	Tue Mar 07 20:45:00 2023 +0100
+++ b/cmake/MlkTileset.cmake	Tue Mar 07 20:58:00 2023 +0100
@@ -18,12 +18,13 @@
 
 function(mlk_tileset input output)
 	set(cmd COMMAND $<TARGET_FILE:mlk-tileset> < ${input} > ${output})
+	get_filename_component(filename ${output} NAME)
 
 	add_custom_command(
 		OUTPUT ${output}
 		COMMAND ${cmd}
-		DEPENDS $<TARGET_FILE:mlk-tileset>
-		COMMENT "Generating tileset ${output}"
+		DEPENDS $<TARGET_FILE:mlk-tileset> ${input}
+		COMMENT "Generating tileset ${filename}"
 	)
 endfunction()
 
--- a/examples/example-tileset/example-tileset.c	Tue Mar 07 20:45:00 2023 +0100
+++ b/examples/example-tileset/example-tileset.c	Tue Mar 07 20:58:00 2023 +0100
@@ -45,6 +45,8 @@
 #include <mlk/example/example.h>
 #include <mlk/example/registry.h>
 
+#include <assets/tilesets/world.h>
+
 static struct mlk_tileset_loader_file loader_file;
 static struct mlk_tileset_loader loader;
 static struct mlk_tileset tileset;
@@ -113,9 +115,6 @@
 	if (mlk_example_init("example-tileset") < 0)
 		mlk_panic();
 
-	// TODO: temporary.
-	const char *path = "/home/markand/dev/molko/build/libmlk-example/tilesets/world.tileset";
-
 	/*
 	 * Demonstrate how we can override functions to use different resources
 	 * and/or allocations.
@@ -123,12 +122,12 @@
 	 * Images are loaded from the libmlk-example registry from RAM and
 	 * sprites animations are statically allocated.
 	 */
-	mlk_tileset_loader_file_init(&loader_file, &loader, path);
+	mlk_tileset_loader_file_init(&loader_file, &loader, "");
 	loader.init_texture = init_texture;
 	loader.init_sprite = init_sprite;
 	loader.init_animation = init_animation;
 
-	if (mlk_tileset_loader_open(&loader, &tileset, path) < 0)
+	if (mlk_tileset_loader_openmem(&loader, &tileset, assets_tilesets_world, sizeof (assets_tilesets_world)) < 0)
 		mlk_panic();
 }
 
--- a/libmlk-example/CMakeLists.txt	Tue Mar 07 20:45:00 2023 +0100
+++ b/libmlk-example/CMakeLists.txt	Tue Mar 07 20:58:00 2023 +0100
@@ -60,6 +60,7 @@
 	${libmlk-example_SOURCE_DIR}/assets/tilesets/world.json
 )
 
+# As they are quite small, we generate tilesets and maps through mlk-bcc too.
 mlk_maps("${MAPS}" ${libmlk-example_BINARY_DIR}/maps maps)
 mlk_tilesets("${TILESETS}" ${libmlk-example_BINARY_DIR}/tilesets tilesets)