From 0a2e4191b34fa80e1bcdcd66564677f7c7fc5403 Mon Sep 17 00:00:00 2001 From: reuk Date: Sat, 25 Apr 2020 16:54:31 +0100 Subject: [PATCH] CMake: Allow custom header names for BinaryData targets This change helps to avoid ambiguity in includes when building projects which contain more than one "BinaryData.h". --- examples/CMake/readme.md | 18 +++++++++++++----- extras/Build/CMake/JUCEUtils.cmake | 22 ++++++++++++++++------ extras/Build/juceaide/Main.cpp | 26 ++++++++++---------------- 3 files changed, 39 insertions(+), 27 deletions(-) diff --git a/examples/CMake/readme.md b/examples/CMake/readme.md index d542aebc0c..adfd9b71ae 100644 --- a/examples/CMake/readme.md +++ b/examples/CMake/readme.md @@ -418,17 +418,25 @@ attributes directly to these creation functions, rather than adding them later. ``` juce_add_binary_data( + [HEADER_NAME ...] [NAMESPACE ...] SOURCES ...) ``` Create a static library that embeds the contents of the files passed as arguments to this function. Adds a library target called `` which can be linked into other targets using -`target_link_libraries`. The `NAMESPACE` argument is optional. If not provided, the generated files -will use the default namespace `BinaryData`. Each of the files located at the paths following -`SOURCES` will be encoded and embedded in the resulting static library. This library can be linked -as normal using `target_link_libraries( PRIVATE )`, and the header can be -included using `#include `. +`target_link_libraries`. + +The `HEADER_NAME` argument is optional. If provided, the generated header will be given the +requested name, otherwise the generated header will be named "BinaryData.h". In completely new +projects, you should provide a unique name here, so that projects containing more than one binary +data target are able to include the binary data headers without ambiguity. + +The `NAMESPACE` argument is also optional. If not provided, the generated files will use the default +namespace `BinaryData`. Each of the files located at the paths following `SOURCES` will be encoded +and embedded in the resulting static library. This library can be linked as normal using +`target_link_libraries( PRIVATE )`, and the header can be included using +`#include `. ### `juce_add_bundle_resources_directory` diff --git a/extras/Build/CMake/JUCEUtils.cmake b/extras/Build/CMake/JUCEUtils.cmake index 604fd06640..891db46fac 100644 --- a/extras/Build/CMake/JUCEUtils.cmake +++ b/extras/Build/CMake/JUCEUtils.cmake @@ -694,7 +694,7 @@ endfunction() # ================================================================================================== function(juce_add_binary_data target) - set(one_value_args NAMESPACE) + set(one_value_args NAMESPACE HEADER_NAME) set(multi_value_args SOURCES) cmake_parse_arguments(JUCE_ARG "" "${one_value_args}" "${multi_value_args}" ${ARGN}) @@ -716,19 +716,27 @@ function(juce_add_binary_data target) file(MAKE_DIRECTORY ${juce_binary_data_folder}) - if(JUCE_ARG_NAMESPACE) - set(namespace_opt --namespace=${JUCE_ARG_NAMESPACE}) + if(NOT JUCE_ARG_NAMESPACE) + set(JUCE_ARG_NAMESPACE BinaryData) + endif() + + if(NOT JUCE_ARG_HEADER_NAME) + set(JUCE_ARG_HEADER_NAME BinaryData.h) endif() add_custom_command(OUTPUT ${binary_file_names} - COMMAND juce::juceaide binarydata ${namespace_opt} ${juce_binary_data_folder} ${JUCE_ARG_SOURCES} + COMMAND juce::juceaide binarydata "${JUCE_ARG_NAMESPACE}" "${JUCE_ARG_HEADER_NAME}" + ${juce_binary_data_folder} ${JUCE_ARG_SOURCES} WORKING_DIRECTORY ${CMAKE_CURRENT_LIST_DIR} VERBATIM) target_sources(${target} PRIVATE "${binary_file_names}") - target_compile_definitions(${target} INTERFACE JUCE_TARGET_HAS_BINARY_DATA=1) target_include_directories(${target} INTERFACE ${juce_binary_data_folder}) target_compile_features(${target} PRIVATE cxx_std_11) + + if(JUCE_ARG_HEADER_NAME STREQUAL "BinaryData.h") + target_compile_definitions(${target} INTERFACE JUCE_TARGET_HAS_BINARY_DATA=1) + endif() endfunction() # ================================================================================================== @@ -805,8 +813,10 @@ function(juce_generate_juce_header target) set(defs_file $>) + set(extra_args) + add_custom_command(OUTPUT "${juce_header}" - COMMAND juce::juceaide header "${defs_file}" "${juce_header}" + COMMAND juce::juceaide header "${defs_file}" "${juce_header}" ${extra_args} DEPENDS "${defs_file}" VERBATIM) endfunction() diff --git a/extras/Build/juceaide/Main.cpp b/extras/Build/juceaide/Main.cpp index 37f0d4c78d..50dbcfba35 100644 --- a/extras/Build/juceaide/Main.cpp +++ b/extras/Build/juceaide/Main.cpp @@ -40,7 +40,7 @@ constexpr auto headerTemplate = R"(/* ${JUCE_INCLUDES} #if JUCE_TARGET_HAS_BINARY_DATA -#include "BinaryData.h" + #include "BinaryData.h" #endif #if ! DONT_SET_USING_JUCE_NAMESPACE @@ -60,30 +60,24 @@ namespace ProjectInfo #endif )"; -juce::String getValueOr (juce::ArgumentList& args, juce::String option, juce::String fallback) +int writeBinaryData (juce::ArgumentList&& args) { - const auto opt = args.removeValueForOption (option); - return opt.isNotEmpty() ? opt : fallback; -} + args.checkMinNumArguments (3); + const auto namespaceName = args.arguments.removeAndReturn (0); + const auto headerName = args.arguments.removeAndReturn (0); + const auto outFolder = args.arguments.removeAndReturn (0).resolveAsExistingFolder(); -int writeBinaryData (juce::ArgumentList&& argumentList) -{ juce::build_tools::ResourceFile resourceFile; - resourceFile.setClassName (getValueOr (argumentList, "--namespace", "BinaryData")); - const auto lineEndings = argumentList.removeOptionIfFound ("--windows") ? "\r\n" : "\n"; - - if (argumentList.arguments.isEmpty()) - juce::ConsoleApplication::fail ("No destination folder specified for binary data files", 1); - - const auto outFolder = argumentList.arguments.removeAndReturn (0).resolveAsExistingFolder(); + resourceFile.setClassName (namespaceName.text); + const auto lineEndings = args.removeOptionIfFound ("--windows") ? "\r\n" : "\n"; - for (const auto& arg : argumentList.arguments) + for (const auto& arg : args.arguments) resourceFile.addFile (arg.resolveAsExistingFile()); const auto result = resourceFile.write (0, lineEndings, - outFolder.getChildFile ("./BinaryData.h"), + outFolder.getChildFile (headerName.text), [&outFolder] (int index) { return outFolder.getChildFile ("./BinaryData" + juce::String { index + 1 } + ".cpp");