Code generation utility that integrates with CMake
ccg generates files from inja templates supplied with json data that are rendered during a CMake configure via a function ccg_generate with a and input files
First, add the package to the vcpkg.json project manifest file:
{
"name": "myproject",
"version": "0.0.1",
"dependencies": [
"ccg"
]
}Then, add the package with find_package(ccg CONFIG REQUIRED) and call ccg_generate in CMake:
cmake_minimum_required(VERSION 3.25)
project(
my-example
LANGUAGES CXX
)
find_package(ccg CONFIG REQUIRED)
ccg_generate(
"${CMAKE_SOURCE_DIR}/input.json"
"${CMAKE_SOURCE_DIR}/config.json"
)
add_executable(my-example)
set_target_properties(
my-example
PROPERTIES
CXX_STANDARD 20
CXX_STANDARD_RQUIRED ON
)
file(GLOB_RECURSE SOURCE_FILES CONFIGURE_DEPENDS
"${CMAKE_SOURCE_DIR}/src/*.cpp"
)
target_sources(my-example PRIVATE ${SOURCE_FILES})
target_compile_options(my-example PRIVATE "/std:c++20")
target_include_directories(my-example PRIVATE "${CMAKE_SOURCE_DIR}/src")ccg_generate takes two parameters; paths to an input json file, and a config json file. Under the hood cmake builds a command-line executable that is executed with the supplied config and input files
Specifies global configuration settings
{
"templates": "${sourceDir}/templates/"
}templates- supplies the path to theinjatemplates directory
Specifies the input data that is used during generation
{
"out": "${sourceDir}/src/",
"items": [
{
"name": "Verbosity",
"outputs": [
{
"template": "enum.hpp.jinja",
"output": "Verbosity.hpp"
},
{
"template": "enum.cpp.jinja",
"output": "Verbosity.cpp"
}
],
"data": {
"description": "Represents the verbosity of logging output",
"namespace": "example",
"underlyingType": "int8_t",
"default": "Normal",
"includePath": "",
"items": [
{
"name": "Quiet",
"value": 0,
"string": "Quite",
"alts": [ "quite", "QUIET" ]
},
{
"name": "Minimal",
"value": 1,
"string": "Minimal"
},
{
"name": "Normal",
"value": 2,
"string": "Normal",
"alts": [ "NORMAL", "normal" ]
},
{
"name": "Detailed",
"value": 3,
"string": "Detailed"
},
{
"name": "Diagnostic",
"value": 4,
"string": "Diagnostic"
}
]
}
}
]
}out- supplies a base path for generated filesitems- array of data that is iteratedname- identifying name for the item. This value is added to the data available inside the template renderingoutputs- array of render targets to enable generating multiple files for eachitemtemplate- path to theinjatemplate that is rendered. The path is relative toconfig.templatesin the supplied configuration fileoutput- path of the rendered file. The path is relative toout, or can be specified with an absolute path using a macro
data- general purpose bag of data passed to template rendering
Json files are able to use cmake macro-like substitutions in non-data fields
| Macro | Description | Example |
|---|---|---|
${sourceDir} |
Substitutes to ${CMAKE_SOURCE_DIR} |
"templates": "${sourceDir}/templates/" |
${binaryDir} |
Substitutes to ${CMAKE_BINARY_DIR} |
"out": "${binaryDir}/include/MyFile.hpp" |