Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: update codegen #23

Merged
merged 1 commit into from
Oct 24, 2023
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
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,6 @@
[submodule ".format"]
path = .format
url = https://github.com/black-desk/.format
[submodule "tools/codegen/quicktype"]
path = tools/codegen/quicktype
url = https://github.com/black-desk/quicktype
38 changes: 38 additions & 0 deletions tools/codegen/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# codegen

[codegen.sh](./codegen.sh) use [quicktype] to
generate json (de)serializing code in libs/runtime/include
from [runtime-spec].

[quicktype]: https://github.com/glideapps/quicktype
[runtime-spec]: https://github.com/opencontainers/runtime-spec

The generated code needs to apply a little [patch](./fix.patch) to compile.

You can recreate that patch follow these instructions:

1. Remove old patch

```bash
rm ./fix.patch
```

2. Run codegen.sh

```bash
./codegen.sh
```

3. Run create-patch.sh, it will start a new shell.

```bash
./create-patch.sh
```

4. Change the code as your wish and leave `libs/runtime/include.orig` untouched

5. Exit

```bash
exit
```
28 changes: 22 additions & 6 deletions tools/codegen/codegen.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,30 @@ set -o pipefail
GIT=${GIT:="git"}

repoRoot="$("$GIT" rev-parse --show-toplevel)"
cd "$repoRoot"/tools/codegen
cd "$repoRoot/tools/codegen"

QUICKTYPE=${QUICKTYPE:="npx quicktype"}
git submodule update --init --depth 1

if [ "$QUICKTYPE" == "npx quicktype" ]; then
npm install .
QUICKTYPE=${QUICKTYPE:=""}

if [ -z "$QUICKTYPE" ]; then
pushd quicktype
npm i
npm run build
popd
fi

quicktype() {
if [ -z "$QUICKTYPE" ]; then
pushd quicktype
npx quicktype "$@"
popd
else
exec $QUICKTYPE "$@"

fi
}

generate() {
schema="$1"
shift
Expand Down Expand Up @@ -65,7 +81,7 @@ generate() {
echo ""
echo "// clang-format off"
} >"$filename"
done < <($QUICKTYPE "$schema" \
done < <(quicktype "$schema" \
--lang c++ \
-s schema \
-t "$toplevel_type" \
Expand Down Expand Up @@ -108,7 +124,7 @@ generate \
"$include" \
"ocppi/runtime/features/types"

PATCH_FILE=${PATCH_FILE:="$repoRoot"/tools/codegen/fix-unknow-types.patch}
PATCH_FILE=${PATCH_FILE:="$repoRoot"/tools/codegen/fix.patch}

if [[ ! -f "$PATCH_FILE" ]]; then
exit
Expand Down
5 changes: 3 additions & 2 deletions tools/codegen/create-patch.sh
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@ cd "$repoRoot"

"$USER_SHELL"

! diff -ruN "${origin#"$repoRoot/"}" "${include#"$repoRoot/"}" \
>"$repoRoot"/tools/codegen/fix-unknow-types.patch
diff -ruN "${origin#"$repoRoot/"}" "${include#"$repoRoot/"}" \
>"$repoRoot"/tools/codegen/fix.patch &&
exit 1

chmod -R u+w "$origin"
rm -rf "$origin"
28 changes: 14 additions & 14 deletions tools/codegen/fix-unknow-types.patch → tools/codegen/fix.patch
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
diff -ruN libs/runtime/include.orig/ocppi/runtime/config/types/Config.hpp libs/runtime/include/ocppi/runtime/config/types/Config.hpp
--- libs/runtime/include.orig/ocppi/runtime/config/types/Config.hpp 2023-09-01 15:23:00.959394014 +0800
+++ libs/runtime/include/ocppi/runtime/config/types/Config.hpp 2023-09-01 15:23:04.327171730 +0800
@@ -54,7 +54,7 @@
--- libs/runtime/include.orig/ocppi/runtime/config/types/Config.hpp 2023-10-22 10:47:22.692355495 +0800
+++ libs/runtime/include/ocppi/runtime/config/types/Config.hpp 2023-10-22 10:47:28.552355069 +0800
@@ -45,7 +45,7 @@
std::optional<std::string> domainname;
std::optional<Hooks> hooks;
std::optional<std::string> hostname;
Expand All @@ -11,8 +11,8 @@ diff -ruN libs/runtime/include.orig/ocppi/runtime/config/types/Config.hpp libs/r
std::string ociVersion;
std::optional<Process> process;
diff -ruN libs/runtime/include.orig/ocppi/runtime/config/types/Generators.hpp libs/runtime/include/ocppi/runtime/config/types/Generators.hpp
--- libs/runtime/include.orig/ocppi/runtime/config/types/Generators.hpp 2023-09-01 15:23:00.959394014 +0800
+++ libs/runtime/include/ocppi/runtime/config/types/Generators.hpp 2023-09-01 15:23:04.331171466 +0800
--- libs/runtime/include.orig/ocppi/runtime/config/types/Generators.hpp 2023-10-22 10:47:22.692355495 +0800
+++ libs/runtime/include/ocppi/runtime/config/types/Generators.hpp 2023-10-22 10:47:28.552355069 +0800
@@ -683,7 +683,7 @@
x.network = get_stack_optional<ResourcesNetwork>(j, "network");
x.pids = get_stack_optional<Pids>(j, "pids");
Expand Down Expand Up @@ -52,8 +52,8 @@ diff -ruN libs/runtime/include.orig/ocppi/runtime/config/types/Generators.hpp li
if (x.mounts) {
j["mounts"] = x.mounts;
diff -ruN libs/runtime/include.orig/ocppi/runtime/config/types/Linux.hpp libs/runtime/include/ocppi/runtime/config/types/Linux.hpp
--- libs/runtime/include.orig/ocppi/runtime/config/types/Linux.hpp 2023-09-01 15:23:00.959394014 +0800
+++ libs/runtime/include/ocppi/runtime/config/types/Linux.hpp 2023-09-01 15:23:04.335171202 +0800
--- libs/runtime/include.orig/ocppi/runtime/config/types/Linux.hpp 2023-10-22 10:47:22.692355495 +0800
+++ libs/runtime/include/ocppi/runtime/config/types/Linux.hpp 2023-10-22 10:47:28.552355069 +0800
@@ -62,7 +62,7 @@
std::optional<LinuxResources> resources;
std::optional<RootfsPropagation> rootfsPropagation;
Expand All @@ -64,9 +64,9 @@ diff -ruN libs/runtime/include.orig/ocppi/runtime/config/types/Linux.hpp libs/ru
std::optional<std::vector<IdMapping>> uidMappings;
};
diff -ruN libs/runtime/include.orig/ocppi/runtime/config/types/LinuxResources.hpp libs/runtime/include/ocppi/runtime/config/types/LinuxResources.hpp
--- libs/runtime/include.orig/ocppi/runtime/config/types/LinuxResources.hpp 2023-09-01 15:23:00.959394014 +0800
+++ libs/runtime/include/ocppi/runtime/config/types/LinuxResources.hpp 2023-09-01 15:23:04.339170938 +0800
@@ -50,7 +50,7 @@
--- libs/runtime/include.orig/ocppi/runtime/config/types/LinuxResources.hpp 2023-10-22 10:47:22.692355495 +0800
+++ libs/runtime/include/ocppi/runtime/config/types/LinuxResources.hpp 2023-10-22 10:47:28.552355069 +0800
@@ -41,7 +41,7 @@
std::optional<ResourcesNetwork> network;
std::optional<Pids> pids;
std::optional<std::map<std::string, Rdma>> rdma;
Expand All @@ -76,8 +76,8 @@ diff -ruN libs/runtime/include.orig/ocppi/runtime/config/types/LinuxResources.hp
}
}
diff -ruN libs/runtime/include.orig/ocppi/runtime/features/types/Features.hpp libs/runtime/include/ocppi/runtime/features/types/Features.hpp
--- libs/runtime/include.orig/ocppi/runtime/features/types/Features.hpp 2023-09-01 15:23:00.959394014 +0800
+++ libs/runtime/include/ocppi/runtime/features/types/Features.hpp 2023-09-01 15:23:05.295107841 +0800
--- libs/runtime/include.orig/ocppi/runtime/features/types/Features.hpp 2023-10-22 10:47:22.692355495 +0800
+++ libs/runtime/include/ocppi/runtime/features/types/Features.hpp 2023-10-22 10:47:28.552355069 +0800
@@ -35,7 +35,7 @@
struct Features {
std::optional<std::map<std::string, nlohmann::json>> annotations;
Expand All @@ -88,8 +88,8 @@ diff -ruN libs/runtime/include.orig/ocppi/runtime/features/types/Features.hpp li
std::string ociVersionMax;
std::string ociVersionMin;
diff -ruN libs/runtime/include.orig/ocppi/runtime/features/types/Generators.hpp libs/runtime/include/ocppi/runtime/features/types/Generators.hpp
--- libs/runtime/include.orig/ocppi/runtime/features/types/Generators.hpp 2023-09-01 15:23:00.959394014 +0800
+++ libs/runtime/include/ocppi/runtime/features/types/Generators.hpp 2023-09-01 15:23:05.299107577 +0800
--- libs/runtime/include.orig/ocppi/runtime/features/types/Generators.hpp 2023-10-22 10:47:22.692355495 +0800
+++ libs/runtime/include/ocppi/runtime/features/types/Generators.hpp 2023-10-22 10:47:28.552355069 +0800
@@ -199,7 +199,7 @@
inline void from_json(const json & j, Features& x) {
x.annotations = get_stack_optional<std::map<std::string, nlohmann::json>>(j, "annotations");
Expand Down
Loading