Skip to content

Commit d5249b5

Browse files
authored
Adding a C API (#66)
Adding a C API. **New Features** - `rego_c.h` contains a C API for the library - `examples/c` contains an example that builds an interpreter using the C API **Breaking Change** The project was incorrectly configured to set the PUBLIC CMake build directory to `include/rego` instead of `include`. This meant that projects consuming the library using CMake FetchContent would include header files without a `rego/` prefix, which would cause problems long term. This has been fixed, but as a result any projects using the library via CMake FetchContent will need to update their `#include` statements to include the `rego/` prefix. --------- Signed-off-by: Matthew A Johnson <[email protected]>
1 parent dde8e12 commit d5249b5

39 files changed

+1309
-210
lines changed

CHANGELOG

+15
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,20 @@
11
# Changelog
22

3+
## 2023-09-07 - Version 0.3.3
4+
Adding a C API.
5+
6+
**New Features**
7+
- `rego_c.h` contains a C API for the library
8+
- `examples/c` contains an example that builds an interpreter using the C API
9+
10+
**Breaking Change**
11+
The project was incorrectly configured to set the PUBLIC CMake build directory
12+
to `include/rego` instead of `include`. This meant that projects consuming the
13+
library using CMake FetchContent would include header files without a `rego/`
14+
prefix, which would cause problems long term. This has been fixed, but as a result
15+
any projects using the library via CMake FetchContent will need to update their
16+
`#include` statements to include the `rego/` prefix.
17+
318
## 2023-09-03 - Version 0.3.2
419
Adding tests based upon the Confidential ACI Rego framework.
520

CMakeLists.txt

+32-2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@ set ( REGOCPP_OPA_VERSION 0.55.0 )
1919

2020
set( REGOCPP_VERSION ${REGOCPP_VERSION_MAJOR}.${REGOCPP_VERSION_MINOR}.${REGOCPP_VERSION_REVISION} )
2121

22+
message("Configure REGOCPP_VERSION at ${REGOCPP_VERSION}")
23+
24+
project( regocpp VERSION ${REGOCPP_VERSION} LANGUAGES CXX)
25+
26+
# -------------------- Get version info --------------------------
27+
2228
find_package(Git REQUIRED)
2329

2430
execute_process(
@@ -28,9 +34,28 @@ execute_process(
2834
OUTPUT_STRIP_TRAILING_WHITESPACE
2935
)
3036

31-
message("Configure REGOCPP_VERSION at ${REGOCPP_VERSION}")
37+
execute_process(
38+
COMMAND git rev-parse --abbrev-ref HEAD
39+
WORKING_DIRECTORY ${CMAKE_CURRENT_LIST_DIR}
40+
OUTPUT_VARIABLE REGOCPP_GIT_BRANCH
41+
OUTPUT_STRIP_TRAILING_WHITESPACE
42+
)
3243

33-
project( regocpp VERSION ${REGOCPP_VERSION} LANGUAGES CXX)
44+
execute_process(
45+
COMMAND git log -1 --format=%h
46+
WORKING_DIRECTORY ${CMAKE_CURRENT_LIST_DIR}
47+
OUTPUT_VARIABLE REGOCPP_GIT_SHORT_HASH
48+
OUTPUT_STRIP_TRAILING_WHITESPACE
49+
)
50+
51+
execute_process(
52+
COMMAND git log -1 --format=%aD
53+
WORKING_DIRECTORY ${CMAKE_CURRENT_LIST_DIR}
54+
OUTPUT_VARIABLE REGOCPP_BUILD_DATE
55+
OUTPUT_STRIP_TRAILING_WHITESPACE
56+
)
57+
58+
set(REGOCPP_BUILD_TOOLCHAIN "${CMAKE_CXX_COMPILER_ID} ${CMAKE_CXX_COMPILER_VERSION}")
3459

3560
set(CMAKE_CXX_STANDARD 20)
3661
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
@@ -41,6 +66,7 @@ option(REGOCPP_BUILD_TOOLS "Specifies whether to build the command line tools" O
4166
option(REGOCPP_BUILD_TESTS "Specifies whether to build the tests" OFF)
4267
option(REGOCPP_OPA_TESTS "Specifies whether to load and run the OPA built-ins tests" OFF)
4368
option(REGOCPP_SPECULATIVE_LOAD_HARDENING "Specifies whether to enable speculative load hardening, only supported with Clang" OFF)
69+
option(REGOCPP_COPY_EXAMPLES "Specifies whether to copy the examples to the install directory" OFF)
4470

4571
# -------------------- Find packages --------------------------
4672

@@ -87,6 +113,7 @@ else()
87113
tools/*.cc
88114
tests/*.cc
89115
tests/*.h
116+
examples/c/main.c
90117
)
91118

92119
add_custom_target(regocpp_format
@@ -157,6 +184,9 @@ install(FILES ${PROJECT_BINARY_DIR}/${PROJECT_NAME}Config.cmake
157184

158185
if (REGOCPP_BUILD_TOOLS)
159186
install(TARGETS rego_interpreter rego_trieste RUNTIME)
187+
endif()
188+
189+
if (REGOCPP_COPY_EXAMPLES)
160190
install(DIRECTORY tools/examples DESTINATION .)
161191
endif()
162192

CMakePresets.json

+12-4
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
"CMAKE_INSTALL_PREFIX": "${sourceDir}/build/dist",
1212
"CMAKE_CXX_COMPILER": "clang++",
1313
"REGOCPP_BUILD_TESTS": "ON",
14-
"REGOCPP_BUILD_TOOLS": "ON"
14+
"REGOCPP_BUILD_TOOLS": "ON",
15+
"REGOCPP_COPY_EXAMPLES": "ON"
1516
}
1617
},
1718
{
@@ -25,6 +26,7 @@
2526
"CMAKE_CXX_COMPILER": "clang++",
2627
"REGOCPP_BUILD_TESTS": "ON",
2728
"REGOCPP_BUILD_TOOLS": "ON",
29+
"REGOCPP_COPY_EXAMPLES": "ON",
2830
"REGOCPP_OPA_TESTS": "ON"
2931
}
3032
},
@@ -36,7 +38,8 @@
3638
"CMAKE_BUILD_TYPE": "Debug",
3739
"CMAKE_INSTALL_PREFIX": "${sourceDir}/build/dist",
3840
"REGOCPP_BUILD_TESTS": "ON",
39-
"REGOCPP_BUILD_TOOLS": "ON"
41+
"REGOCPP_BUILD_TOOLS": "ON",
42+
"REGOCPP_COPY_EXAMPLES": "ON"
4043
}
4144
},
4245
{
@@ -48,6 +51,7 @@
4851
"CMAKE_INSTALL_PREFIX": "${sourceDir}/build/dist",
4952
"REGOCPP_BUILD_TESTS": "ON",
5053
"REGOCPP_BUILD_TOOLS": "ON",
54+
"REGOCPP_COPY_EXAMPLES": "ON",
5155
"REGOCPP_OPA_TESTS": "ON"
5256
}
5357
},
@@ -61,7 +65,8 @@
6165
"CMAKE_INSTALL_PREFIX": "${sourceDir}/build/dist",
6266
"CMAKE_CXX_COMPILER": "clang++",
6367
"REGOCPP_BUILD_TESTS": "ON",
64-
"REGOCPP_BUILD_TOOLS": "ON"
68+
"REGOCPP_BUILD_TOOLS": "ON",
69+
"REGOCPP_COPY_EXAMPLES": "ON"
6570
}
6671
},
6772
{
@@ -75,6 +80,7 @@
7580
"CMAKE_CXX_COMPILER": "clang++",
7681
"REGOCPP_BUILD_TESTS": "ON",
7782
"REGOCPP_BUILD_TOOLS": "ON",
83+
"REGOCPP_COPY_EXAMPLES": "ON",
7884
"REGOCPP_OPA_TESTS": "ON"
7985
}
8086
},
@@ -86,7 +92,8 @@
8692
"CMAKE_BUILD_TYPE": "Release",
8793
"CMAKE_INSTALL_PREFIX": "${sourceDir}/build/dist",
8894
"REGOCPP_BUILD_TESTS": "ON",
89-
"REGOCPP_BUILD_TOOLS": "ON"
95+
"REGOCPP_BUILD_TOOLS": "ON",
96+
"REGOCPP_COPY_EXAMPLES": "ON"
9097
}
9198
},
9299
{
@@ -98,6 +105,7 @@
98105
"CMAKE_INSTALL_PREFIX": "${sourceDir}/build/dist",
99106
"REGOCPP_BUILD_TESTS": "ON",
100107
"REGOCPP_BUILD_TOOLS": "ON",
108+
"REGOCPP_COPY_EXAMPLES": "ON",
101109
"REGOCPP_OPA_TESTS": "ON"
102110
}
103111
}

VERSION

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
0.3.2
1+
0.3.3

examples/README.md

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# Examples
2+
3+
This directory contains examples of how to use the library from the different
4+
languages we support (other than C++. For C++ examples see the
5+
[tests](../tests) or [tools](../tools) directories).
6+
7+
At the moment we support C and C++ only, but we plan to support Rust, Python,
8+
and .NET wrappers, with more as demand dictates.
9+
10+
## C
11+
The C example is a [simple command line tool](c/main.c) that takes one or more
12+
Rego data, module, and input files and evaluates a query against them.
13+
14+
To build it on Linux:
15+
```bash
16+
cd examples/c
17+
mkdir build
18+
cd build
19+
cmake .. --preset release-clang
20+
ninja install
21+
```
22+
23+
and on Windows:
24+
25+
```cmd
26+
cd examples\c
27+
mkdir build
28+
cd build
29+
cmake .. --preset release
30+
cmake --build . --config Release --target INSTALL
31+
```
32+
33+
The resulting executable can be called from within the `dist` directory:
34+
35+
```bash
36+
cd dist
37+
./bin/regoc -d examples/scalars.rego -q data.scalars.greeting
38+
```

examples/c/CMakeLists.txt

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Copyright (c) Microsoft Corporation.
2+
# Licensed under the MIT License.
3+
4+
cmake_minimum_required(VERSION 3.15)
5+
6+
include(FetchContent)
7+
8+
project( regoc VERSION 0.1.0 LANGUAGES C CXX )
9+
10+
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
11+
12+
FetchContent_Declare(
13+
regocpp
14+
GIT_REPOSITORY https://github.com/matajoh/rego-cpp
15+
GIT_TAG 46101f7e89ca36b8b2c79f12ea411cb74827bb72
16+
)
17+
18+
FetchContent_Declare(
19+
cargs
20+
GIT_REPOSITORY https://github.com/likle/cargs
21+
GIT_TAG v1.0.3
22+
)
23+
24+
set(CMAKE_POLICY_DEFAULT_CMP0077 NEW)
25+
26+
FetchContent_MakeAvailable(regocpp)
27+
FetchContent_MakeAvailable(cargs)
28+
29+
add_executable(regoc main.c)
30+
target_link_libraries(regoc PRIVATE regocpp::rego cargs)
31+
32+
install(TARGETS regoc DESTINATION bin)

examples/c/CMakePresets.json

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
{
2+
"version": 3,
3+
"configurePresets": [
4+
{
5+
"name": "debug-clang",
6+
"displayName": "Debug Build using clang",
7+
"description": "Sets up a debug build that uses Clang",
8+
"generator": "Ninja",
9+
"cacheVariables": {
10+
"CMAKE_BUILD_TYPE": "Debug",
11+
"CMAKE_INSTALL_PREFIX": "${sourceDir}/build/dist",
12+
"CMAKE_C_COMPILER": "clang",
13+
"CMAKE_CXX_COMPILER": "clang++",
14+
"REGOCPP_COPY_EXAMPLES": "ON"
15+
}
16+
},
17+
{
18+
"name": "debug",
19+
"displayName": "Debug Build",
20+
"description": "Sets up a debug build that uses the default compiler and generator",
21+
"cacheVariables": {
22+
"CMAKE_BUILD_TYPE": "Debug",
23+
"CMAKE_INSTALL_PREFIX": "${sourceDir}/build/dist",
24+
"REGOCPP_COPY_EXAMPLES": "ON"
25+
}
26+
},
27+
{
28+
"name": "release-clang",
29+
"displayName": "Release Build using clang",
30+
"description": "Sets up a release build that uses Clang",
31+
"generator": "Ninja",
32+
"cacheVariables": {
33+
"CMAKE_BUILD_TYPE": "Release",
34+
"CMAKE_INSTALL_PREFIX": "${sourceDir}/build/dist",
35+
"CMAKE_C_COMPILER": "clang",
36+
"CMAKE_CXX_COMPILER": "clang++",
37+
"REGOCPP_COPY_EXAMPLES": "ON"
38+
}
39+
},
40+
{
41+
"name": "release",
42+
"displayName": "Release Build",
43+
"description": "Sets up a release build that uses the default compiler and generator",
44+
"cacheVariables": {
45+
"CMAKE_BUILD_TYPE": "Release",
46+
"CMAKE_INSTALL_PREFIX": "${sourceDir}/build/dist",
47+
"REGOCPP_COPY_EXAMPLES": "ON"
48+
}
49+
}
50+
]
51+
}

0 commit comments

Comments
 (0)