Skip to content
Merged
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
13 changes: 13 additions & 0 deletions .github/workflows/build_and_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -108,3 +108,16 @@ jobs:
run: |
cd examples/bazel
bazelisk build --lockfile_mode=off //...

cmake_example:
name: Build CMake example
runs-on: "ubuntu-22.04"
steps:
- uses: actions/checkout@v4
- name: Build
run: |
cd examples/cmake
BUILD_DIR="$(mktemp -d)"
cmake -GNinja -DCMAKE_BUILD_TYPE=Debug -S . -B "${BUILD_DIR}" && \
cmake --build "${BUILD_DIR}" -- -v && \
${BUILD_DIR}/jsonnet_cmake_example
19 changes: 19 additions & 0 deletions examples/cmake/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
cmake_minimum_required(VERSION 3.15...4.1)

project(jsonnet_cmake_example VERSION 1.0 LANGUAGES CXX)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

# Can use add_subdirectory to point to the jsonnet source.
# Disable tests (force BUILD_TEST OFF) so that jsonnet doesn't pull in GoogleTest.
# Use EXCLUDE_FROM_ALL so that targets won't be built if nothing depends on them.
set(BUILD_TESTS OFF CACHE BOOL "" FORCE)
add_subdirectory(
${CMAKE_CURRENT_SOURCE_DIR}/../..
${CMAKE_CURRENT_BINARY_DIR}/jsonnet
EXCLUDE_FROM_ALL
)

add_executable(jsonnet_cmake_example main.cpp)
target_link_libraries(jsonnet_cmake_example PRIVATE jsonnet_cpp_static)
18 changes: 18 additions & 0 deletions examples/cmake/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#include <iostream>
#include <string>
#include <libjsonnet++.h>

int main(int argc, char **argv) {
jsonnet::Jsonnet vm;
vm.init();
std::string code = "local c=3;{a:1,b:2,c:c}";
std::string output;
std::cout << "evaluating " << code << std::endl;
if (vm.evaluateSnippet("-", code, &output)) {
std::cout << "result:\n" << output << std::endl;
return 0;
} else {
std::cout << "FAILURE:\n" << vm.lastError() << std::endl;
return 1;
}
}