From 99537daccdfd6130a64808780e850ddb5a38dcc8 Mon Sep 17 00:00:00 2001 From: Sandy Carter Date: Sun, 8 Jan 2023 09:40:51 -0500 Subject: [PATCH] bx: Add bin2c and cmake macro --- .cmake-format.py | 10 ++++++- CMakeLists.txt | 4 +++ README.md | 28 ++++++++++++++++++ cmake/Config.cmake.in | 1 + cmake/bgfxToolUtils.cmake | 61 +++++++++++++++++++++++++++++++++++++++ cmake/bx/CMakeLists.txt | 4 +++ cmake/bx/bin2c.cmake | 21 ++++++++++++++ 7 files changed, 128 insertions(+), 1 deletion(-) create mode 100644 cmake/bx/bin2c.cmake diff --git a/.cmake-format.py b/.cmake-format.py index 47d52a5..48ea7eb 100644 --- a/.cmake-format.py +++ b/.cmake-format.py @@ -4,7 +4,11 @@ with section("parse"): # Specify structure for custom cmake functions - additional_commands = { '_bgfx_get_profile_ext': {'pargs': {'nargs': 2}}, + additional_commands = { '_bgfx_bin2c_parse': { 'kwargs': { 'ARRAY_NAME': 1, + 'INPUT_FILE': 1, + 'OUTPUT_FILE': 1}, + 'pargs': {'flags': [], 'nargs': '*'}}, + '_bgfx_get_profile_ext': {'pargs': {'nargs': 2}}, '_bgfx_shaderc_parse': { 'kwargs': { 'BIN2C': 1, 'DEFINES': '+', 'FILE': 1, @@ -31,6 +35,10 @@ with section("parse"): 'DISASM', 'WERROR'], 'nargs': '1+'}}, + 'bgfx_compile_binary_to_header': { 'kwargs': { 'ARRAY_NAME': 1, + 'INPUT_FILE': 1, + 'OUTPUT_FILE': 1}, + 'pargs': {'flags': [], 'nargs': '*'}}, 'bgfx_compile_shader_to_header': { 'kwargs': { 'OUTPUT_DIR': 1, 'SHADERS': '+', 'TYPE': 1, diff --git a/CMakeLists.txt b/CMakeLists.txt index df45eb2..2369d66 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -30,6 +30,7 @@ option(BX_AMALGAMATED "Amalgamated bx build for faster compilation" OFF) option(BX_CONFIG_DEBUG "Log debug messages (default: on in debug)" OFF) option(BGFX_AMALGAMATED "Amalgamated bgfx build for faster compilation" OFF) option(BGFX_BUILD_TOOLS "Build bgfx tools." ON) +option(BGFX_BUILD_TOOLS_BIN2C "Build bx binary to c converter." ON) option(BGFX_BUILD_TOOLS_SHADER "Build bgfx shader tools." ON) option(BGFX_BUILD_TOOLS_GEOMETRY "Build bgfx geometry tools." ON) option(BGFX_BUILD_TOOLS_TEXTURE "Build bgfx texture tools." ON) @@ -194,6 +195,9 @@ if(BGFX_INSTALL) # install tools install(FILES ${CMAKE_CURRENT_SOURCE_DIR}/cmake/bgfxToolUtils.cmake DESTINATION "${config_install_dir}") if(BGFX_BUILD_TOOLS) + if(BGFX_BUILD_TOOLS_BIN2C) + install(TARGETS bin2c EXPORT "${TARGETS_EXPORT_NAME}" DESTINATION "${CMAKE_INSTALL_BINDIR}") + endif() if(BGFX_BUILD_TOOLS_SHADER) install(TARGETS shaderc EXPORT "${TARGETS_EXPORT_NAME}" DESTINATION "${CMAKE_INSTALL_BINDIR}") endif() diff --git a/README.md b/README.md index b3e99a6..c4af266 100644 --- a/README.md +++ b/README.md @@ -32,6 +32,34 @@ This project is setup to be included a few different ways. To include bgfx sourc ## Added cmake commands bgfx.cmake will install `bgfxToolUtils.cmake` which has useful cmake functions for using bgfx's tools: +### `bgfx_compile_binary_to_header` +Add a build rule for a binary file to the generated build system using bin2c. +```cmake +bgfx_compile_binary_to_header( + INPUT_FILE filename + OUTPUT_FILE filename + ARRAY_NAME name +) +``` +This defines a bin2c command to generate a specified `OUTPUT_FILE` header with an array `ARRAY_NAME` with the binary representation of a `INPUT_FILE` file. + +Adding these `INPUT_FILE` as source files to a target will run `bin2c` at build time and they will rebuild if either the contents of the `INPUT_FILE` change. + +#### Examples: Generating an image as a header +```cmake +bgfx_compile_binary_to_header( + INPUT_FILE image.png + OUTPUT_FILE ${CMAKE_BINARY_DIR}/include/generated/images/image.png.h +) +add_library(myLib image.png) +target_include_directories(myLib ${CMAKE_BINARY_DIR}/include/generated/images) +``` + +```cpp +// main.cpp +#include +``` + ### `bgfx_compile_shader_to_header` Add a build rule for a `*.sc` shader to the generated build system using shaderc. ```cmake diff --git a/cmake/Config.cmake.in b/cmake/Config.cmake.in index 5abc324..d1eb0c0 100644 --- a/cmake/Config.cmake.in +++ b/cmake/Config.cmake.in @@ -22,6 +22,7 @@ if(@CMAKE_CROSSCOMPILING@) endif() endmacro() + _bgfx_crosscompile_use_host_tool(bin2c) _bgfx_crosscompile_use_host_tool(shaderc) _bgfx_crosscompile_use_host_tool(texturec) _bgfx_crosscompile_use_host_tool(texturev) diff --git a/cmake/bgfxToolUtils.cmake b/cmake/bgfxToolUtils.cmake index 13f2656..b676fe2 100644 --- a/cmake/bgfxToolUtils.cmake +++ b/cmake/bgfxToolUtils.cmake @@ -1,3 +1,64 @@ +# _bgfx_bin2c_parse( +# INPUT_FILE filename +# OUTPUT_FILE filename +# ARRAY_NAME name +# ) +# Usage: bin2c -f -o -n +function(_bgfx_bin2c_parse ARG_OUT) + set(options "") + set(oneValueArgs INPUT_FILE;OUTPUT_FILE;ARRAY_NAME) + set(multiValueArgs "") + cmake_parse_arguments(ARG "${options}" "${oneValueArgs}" "${multiValueArgs}" "${ARGN}") + set(CLI "") + + # -f + if(ARG_INPUT_FILE) + list(APPEND CLI "-f" "${ARG_INPUT_FILE}") + else() + message(SEND_ERROR "Call to _bgfx_bin2c_parse() must have an INPUT_FILE") + endif() + + # -o + if(ARG_OUTPUT_FILE) + list(APPEND CLI "-o" "${ARG_OUTPUT_FILE}") + else() + message(SEND_ERROR "Call to _bgfx_bin2c_parse() must have an OUTPUT_FILE") + endif() + + # -n + if(ARG_ARRAY_NAME) + list(APPEND CLI "-n" "${ARG_ARRAY_NAME}") + else() + message(SEND_ERROR "Call to _bgfx_bin2c_parse() must have an ARRAY_NAME") + endif() + + set(${ARG_OUT} ${CLI} PARENT_SCOPE) +endfunction() + +# bgfx_compile_binary_to_header( +# INPUT_FILE filename +# OUTPUT_FILE filename +# ARRAY_NAME name +# ) +# +function(bgfx_compile_binary_to_header) + set(options "") + set(oneValueArgs INPUT_FILE;OUTPUT_FILE;ARRAY_NAME) + set(multiValueArgs "") + cmake_parse_arguments(ARG "${options}" "${oneValueArgs}" "${multiValueArgs}" "${ARGN}") + _bgfx_bin2c_parse( + CLI + INPUT_FILE ${ARG_INPUT_FILE} + OUTPUT_FILE ${ARG_OUTPUT_FILE} + ARRAY_NAME ${ARG_ARRAY_NAME} + ) + add_custom_command( + OUTPUT ${ARG_OUTPUT_FILE} # + COMMAND bgfx::bin2c ${CLI} # + MAIN_DEPENDENCY ${ARG_INPUT_FILE} # + ) +endfunction() + # _bgfx_shaderc_parse( # FILE filename # OUTPUT filename diff --git a/cmake/bx/CMakeLists.txt b/cmake/bx/CMakeLists.txt index cf8babd..8b3933e 100644 --- a/cmake/bx/CMakeLists.txt +++ b/cmake/bx/CMakeLists.txt @@ -9,3 +9,7 @@ # this software. If not, see . include(bx.cmake) + +if(BGFX_BUILD_TOOLS) + include(bin2c.cmake) +endif() diff --git a/cmake/bx/bin2c.cmake b/cmake/bx/bin2c.cmake new file mode 100644 index 0000000..12c4e12 --- /dev/null +++ b/cmake/bx/bin2c.cmake @@ -0,0 +1,21 @@ +# bgfx.cmake - bgfx building in cmake +# Written in 2017 by Joshua Brookover +# +# To the extent possible under law, the author(s) have dedicated all copyright +# and related and neighboring rights to this software to the public domain +# worldwide. This software is distributed without any warranty. +# +# You should have received a copy of the CC0 Public Domain Dedication along with +# this software. If not, see . + +add_executable(bin2c) + +# Grab the bin2c source files +file(GLOB_RECURSE BIN2C_SOURCES # + ${BX_DIR}/tools/bin2c/*.cpp # + ${BX_DIR}/tools/bin2c/*.h # +) + +target_sources(bin2c PRIVATE ${BIN2C_SOURCES}) +target_link_libraries(bin2c PRIVATE bx) +set_target_properties(bin2c PROPERTIES FOLDER "bgfx/tools")