Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
build
build/
out/
.vs/
.cache/

# vcpkg manifest-mode install tree
vcpkg_installed/

# Per-user CMake presets
CMakeUserPresets.json

tests/execution/*_codegen.cpp
tests/execution_enums/*_codegen.cpp
tests/execution_luawrapper/*_codegen.cpp
Expand Down
4 changes: 2 additions & 2 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[submodule "cmake/common-compile-settings"]
path = cmake/common-compile-settings
[submodule "support/cmake/common-compile-settings"]
path = support/cmake/common-compile-settings
url = https://github.com/OpenSpace/common-compile-settings
71 changes: 67 additions & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,75 @@
# OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. #
##########################################################################################

list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake/common-compile-settings")
include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/common-compile-settings/common-compile-settings.cmake)
cmake_minimum_required(VERSION 4.0)

# The tests link openspace-core / openspace-module-collection and include
# <openspace/documentation/verifier.h>, none of which exist outside the OpenSpace tree, so
# a standalone codegen checkout cannot build them yet. The option is kept (default OFF) for
# a future decoupling; OpenSpace's own CMakeLists adds support/coding/codegen/tests
# directly and does not consult this option.
option(CODEGEN_BUILD_TESTS "Build the codegen unit tests (requires the OpenSpace tree)" OFF)

project(codegen VERSION 1.0.0 LANGUAGES CXX)

list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/support/cmake/common-compile-settings")
include(${CMAKE_CURRENT_SOURCE_DIR}/support/cmake/common-compile-settings/common-compile-settings.cmake)

# Redirecting the output would leak into a surrounding project that vendors codegen
if (PROJECT_IS_TOP_LEVEL)
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
endif ()

add_subdirectory(lib)

add_executable(codegen-tool main.cpp)
target_link_libraries(codegen-tool PUBLIC codegen-lib)

target_link_libraries(codegen-tool PRIVATE codegen-lib)
set_compile_settings(codegen-tool)

if (CODEGEN_BUILD_TESTS AND CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR)
enable_testing()
add_subdirectory(tests)
endif ()

# Install and export rules that make codegen consumable through
# `find_package(codegen CONFIG REQUIRED)`, both the `codegen::codegen-lib` library and the
# `codegen::codegen-tool` executable that a consumer runs as a build step
if (PROJECT_IS_TOP_LEVEL)
include(GNUInstallDirs)
include(CMakePackageConfigHelpers)

set(CODEGEN_INSTALL_CMAKEDIR "${CMAKE_INSTALL_DATADIR}/codegen")

install(TARGETS codegen-lib codegen-tool
EXPORT codegenTargets
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
FILE_SET HEADERS DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
)
install(
EXPORT codegenTargets
FILE codegenTargets.cmake
NAMESPACE codegen::
DESTINATION ${CODEGEN_INSTALL_CMAKEDIR}
)

configure_package_config_file(
${CMAKE_CURRENT_SOURCE_DIR}/support/cmake/codegenConfig.cmake.in
${CMAKE_CURRENT_BINARY_DIR}/codegenConfig.cmake
INSTALL_DESTINATION ${CODEGEN_INSTALL_CMAKEDIR}
)
write_basic_package_version_file(
${CMAKE_CURRENT_BINARY_DIR}/codegenConfigVersion.cmake
VERSION ${PROJECT_VERSION}
COMPATIBILITY SameMajorVersion
)
install(
FILES
${CMAKE_CURRENT_BINARY_DIR}/codegenConfig.cmake
${CMAKE_CURRENT_BINARY_DIR}/codegenConfigVersion.cmake
DESTINATION ${CODEGEN_INSTALL_CMAKEDIR}
)
endif ()
119 changes: 119 additions & 0 deletions CMakePresets.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
{
"version": 6,
"cmakeMinimumRequired": {
"major": 4,
"minor": 0,
"patch": 0
},
"configurePresets": [
{
"name": "vcpkg",
"hidden": true,
"binaryDir": "${sourceDir}/build/${presetName}",
"toolchainFile": "$env{VCPKG_ROOT}/scripts/buildsystems/vcpkg.cmake"
},
{
"name": "windows-base",
"hidden": true,
"inherits": "vcpkg",
"condition": {
"type": "equals",
"lhs": "${hostSystemName}",
"rhs": "Windows"
},
"architecture": {
"value": "x64",
"strategy": "set"
}
},
{
"name": "windows",
"inherits": "windows-base",
"displayName": "Windows x64",
"description": "Multi-config build against the dynamic vcpkg triplet",
"cacheVariables": {
"VCPKG_TARGET_TRIPLET": "x64-windows"
}
},
{
"name": "windows-static",
"inherits": "windows-base",
"displayName": "Windows x64 (static dependencies)",
"cacheVariables": {
"VCPKG_TARGET_TRIPLET": "x64-windows-static"
}
},
{
"name": "linux-base",
"hidden": true,
"inherits": "vcpkg",
"generator": "Ninja",
"cacheVariables": {
"VCPKG_TARGET_TRIPLET": "x64-linux"
},
"condition": {
"type": "equals",
"lhs": "${hostSystemName}",
"rhs": "Linux"
}
},
{
"name": "linux",
"inherits": "linux-base",
"displayName": "Linux x64 RelWithDebInfo",
"cacheVariables": {
"CMAKE_BUILD_TYPE": "RelWithDebInfo"
}
},
{
"name": "linux-debug",
"inherits": "linux-base",
"displayName": "Linux x64 Debug",
"cacheVariables": {
"CMAKE_BUILD_TYPE": "Debug"
}
},
{
"name": "linux-release",
"inherits": "linux-base",
"displayName": "Linux x64 Release",
"cacheVariables": {
"CMAKE_BUILD_TYPE": "Release"
}
}
],
"buildPresets": [
{
"name": "windows",
"configurePreset": "windows",
"configuration": "RelWithDebInfo"
},
{
"name": "windows-debug",
"configurePreset": "windows",
"configuration": "Debug"
},
{
"name": "windows-release",
"configurePreset": "windows",
"configuration": "Release"
},
{
"name": "windows-static",
"configurePreset": "windows-static",
"configuration": "RelWithDebInfo"
},
{
"name": "linux",
"configurePreset": "linux"
},
{
"name": "linux-debug",
"configurePreset": "linux-debug"
},
{
"name": "linux-release",
"configurePreset": "linux-release"
}
]
}
21 changes: 21 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# License
OpenSpace Codegen

Copyright (c) 2021-2026

Permission is hereby granted, free of charge, to any person obtaining a copy of this
software and associated documentation files (the "Software"), to deal in the Software
without restriction, including without limitation the rights to use, copy, modify,
merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to the following
conditions:

The above copyright notice and this permission notice shall be included in all copies
or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,28 @@ Execution:

Additionally, passing the `--verbose` parameter will cause CodeGen to emit extra information, including which files are currently being processed.

## Building
codegen configures through the [vcpkg](https://vcpkg.io) toolchain like Ghoul and SGCT, driven by `CMakePresets.json`. It has no third-party runtime dependencies; the only submodule is the shared `common-compile-settings` CMake helper.

```
git clone --recursive https://github.com/OpenSpace/codegen
cd codegen
cmake --preset windows # or: linux
cmake --build --preset windows
```

The unit tests under `tests/` link `openspace-core` and are only built from within the OpenSpace tree, not by this standalone build (`CODEGEN_BUILD_TESTS` stays `OFF`).

### Consuming codegen
A superproject can pull codegen in as a vcpkg overlay port (`support/vcpkg/ports`) instead of `add_subdirectory`:

```cmake
find_package(codegen CONFIG REQUIRED)
target_link_libraries(main PRIVATE codegen::codegen-lib) # and run codegen::codegen-tool as a build step
```

`support/vcpkg/check-manifest-sync.cmake` verifies the port's dependency list stays in sync with the root `vcpkg.json`, and `support/consumer-test/` is a minimal project that builds against the installed port.

## Generated functions
Running the codegen will create a number of functions in the generated `_codegen.cpp` file that can be used by including the file in the main `.cpp` file.

Expand Down
39 changes: 29 additions & 10 deletions lib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,26 +22,45 @@
# OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. #
##########################################################################################

add_library(codegen-lib)
# codegen-lib carries no symbol-export annotations, so it is always a static library
# regardless of BUILD_SHARED_LIBS (a vcpkg dynamic triplet would otherwise try to build it
# as a DLL with an empty export table)
add_library(codegen-lib STATIC)
target_sources(
codegen-lib
PRIVATE
codegen.h
codegen.cpp
keywords.h
parsing.h
parsing.cpp
settings.h
snippets.h
snippets.cpp
types.h
types.cpp
util.h
util.cpp
verifier.h
verifier.cpp
PUBLIC
FILE_SET HEADERS
BASE_DIRS "${CMAKE_CURRENT_SOURCE_DIR}"
FILES
codegen.h
keywords.h
parsing.h
settings.h
snippets.h
types.h
util.h
verifier.h
)

target_include_directories(codegen-lib PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}")
target_include_directories(codegen-lib
PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>
$<INSTALL_INTERFACE:include>
)

set_compile_settings(codegen-lib)
# set_compile_settings links the interface-only compile_settings target PRIVATE, which for
# a static library would otherwise leave a dangling $<LINK_ONLY:compile_settings> in the
# exported interface. Keep it for the in-tree build only, and carry the language-standard
# requirement (needed to compile the public headers: std::format, <filesystem>, ...) in
# the exported interface explicitly.
set_property(TARGET codegen-lib PROPERTY LINK_LIBRARIES "$<BUILD_INTERFACE:compile_settings>")
set_property(TARGET codegen-lib PROPERTY INTERFACE_LINK_LIBRARIES "")
target_compile_features(codegen-lib PUBLIC cxx_std_23)
6 changes: 6 additions & 0 deletions support/cmake/codegenConfig.cmake.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
@PACKAGE_INIT@

# codegen has no third-party dependencies, so there is nothing to find_dependency() here.
include("${CMAKE_CURRENT_LIST_DIR}/codegenTargets.cmake")

check_required_components(codegen)
51 changes: 51 additions & 0 deletions support/consumer-test/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
##########################################################################################
# #
# OpenSpace Codegen #
# #
# Copyright (c) 2021-2026 #
# #
# Permission is hereby granted, free of charge, to any person obtaining a copy of this #
# software and associated documentation files (the "Software"), to deal in the Software #
# without restriction, including without limitation the rights to use, copy, modify, #
# merge, publish, distribute, sublicense, and/or sell copies of the Software, and to #
# permit persons to whom the Software is furnished to do so, subject to the following #
# conditions: #
# #
# The above copyright notice and this permission notice shall be included in all copies #
# or substantial portions of the Software. #
# #
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, #
# INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A #
# PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT #
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF #
# CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE #
# OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. #
##########################################################################################

# Minimal stand-in for a third-party project. It consumes codegen the same way an external
# developer would - through the `codegen` overlay port and `find_package` - and therefore
# catches breakage in the port, the install rules, and the exported targets that building
# codegen in-tree cannot catch.
#
# cmake -S support/consumer-test -B build/consumer-test
# --toolchain $env:VCPKG_ROOT/scripts/buildsystems/vcpkg.cmake
# -DVCPKG_TARGET_TRIPLET=x64-windows
# cmake --build build/consumer-test
# ctest --test-dir build/consumer-test

cmake_minimum_required(VERSION 3.25)
project(CodegenConsumerTest LANGUAGES CXX)

find_package(codegen CONFIG REQUIRED)

add_executable(CodegenConsumerTest main.cpp)
target_compile_features(CodegenConsumerTest PRIVATE cxx_std_23)
target_link_libraries(CodegenConsumerTest PRIVATE codegen::codegen-lib)

enable_testing()
add_test(NAME CodegenConsumerTest COMMAND CodegenConsumerTest)

# The exported executable target has to be runnable for a superproject that drives codegen
# as a build step. Point it at an empty directory so it does a clean no-op run (exit 0)
file(MAKE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/emptyscan)
add_test(NAME CodegenTool COMMAND codegen::codegen-tool ${CMAKE_CURRENT_BINARY_DIR}/emptyscan)
Loading