bx: Add bin2c and cmake macro

This commit is contained in:
Sandy Carter
2023-01-08 09:40:51 -05:00
committed by Sandy
parent e9b0bb3b6c
commit 99537daccd
7 changed files with 128 additions and 1 deletions

View File

@@ -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,

View File

@@ -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()

View File

@@ -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 <image.png.h>
```
### `bgfx_compile_shader_to_header`
Add a build rule for a `*.sc` shader to the generated build system using shaderc.
```cmake

View File

@@ -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)

View File

@@ -1,3 +1,64 @@
# _bgfx_bin2c_parse(
# INPUT_FILE filename
# OUTPUT_FILE filename
# ARRAY_NAME name
# )
# Usage: bin2c -f <in> -o <out> -n <name>
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

View File

@@ -9,3 +9,7 @@
# this software. If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.
include(bx.cmake)
if(BGFX_BUILD_TOOLS)
include(bin2c.cmake)
endif()

21
cmake/bx/bin2c.cmake Normal file
View File

@@ -0,0 +1,21 @@
# bgfx.cmake - bgfx building in cmake
# Written in 2017 by Joshua Brookover <joshua.al.brookover@gmail.com>
#
# 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 <http://creativecommons.org/publicdomain/zero/1.0/>.
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")