-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
crit(proto-gen): move to Python script
Previously, CRIT was using an overengineered Makefile stunt to generate protobuf bindings for the CLI. The problem of statically linking all the bindings while using the library as a dependency cannot be resolved in a reasonable manner in the Makefile. The logic has been moved out into a Python script. Signed-off-by: Prajwal S N <prajwalnadig21@gmail.com>
- Loading branch information
Showing
3 changed files
with
89 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
PY ?= python3 | ||
PROTO_PATH ?= ../../crit/images | ||
PB_PATH ?= ../../crit/images | ||
GIT_BRANCH ?= master | ||
|
||
pb-gen: clean-pb | ||
@$(PY) protogen.py $(PROTO_PATH) $(PB_PATH) | ||
|
||
proto-update: clean-proto | ||
git clone --depth 1 --branch $(GIT_BRANCH) https://github.com/checkpoint-restore/criu criu-temp | ||
cp criu-temp/images/*.proto $(PROTO_PATH)/ | ||
# rpc.proto is not an image and it is used only to communicate criu-service and swrk. | ||
rm -rf criu-temp $(PROTO_PATH)/rpc.proto | ||
# To prevent namespace conflict with proto files | ||
# in github.com/letsencrypt/boulder, we prepend | ||
# a prefix to the filenames. | ||
mv $(PROTO_PATH)/sa.proto $(PROTO_PATH)/criu-sa.proto | ||
sed -i 's/sa\.proto/criu-sa\.proto/g' $(PROTO_PATH)/*.proto | ||
mv $(PROTO_PATH)/core.proto $(PROTO_PATH)/criu-core.proto | ||
sed -i 's/core\.proto/criu-core\.proto/g' $(PROTO_PATH)/*.proto | ||
|
||
clean-proto: | ||
@echo "Removing existing .proto files..." | ||
rm $(PROTO_PATH)/*.proto || true | ||
|
||
clean-pb: | ||
@echo "Removing existing .pb.go files..." | ||
find $(PB_PATH) -type f -name '*.pb.go' -delete | ||
|
||
.PHONY: pb-gen proto-update clean-proto clean-pb |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import os | ||
import shutil | ||
import argparse | ||
import subprocess | ||
|
||
parser = argparse.ArgumentParser( | ||
description='A script to generate Go bindings for the protobuf definitions provided by CRIU') | ||
parser.add_argument( | ||
'src', help='Path to the definitions directory', type=str) | ||
parser.add_argument( | ||
'dest', help='Path to the destination directory', type=str) | ||
|
||
args = parser.parse_args() | ||
|
||
# The import paths for each package passed to --go_opt | ||
pkg_opts = '' | ||
# The names of the .proto files without the extension | ||
names = [] | ||
|
||
# Loop over the files in the src dir | ||
for file in os.listdir(args.src): | ||
if file.endswith('.proto'): | ||
# Strip the .proto extension | ||
name = os.path.splitext(file)[0] | ||
names.append(name) | ||
# Add the import path for the protoc file | ||
pkg_opts += ',M{0}.proto=github.com/checkpoint-restore/go-criu/v6/crit/images/{0}'.format( | ||
name) | ||
|
||
# Create the dest dir | ||
if not os.path.exists(args.dest): | ||
os.makedirs(args.dest) | ||
# Generate the .pb.go files | ||
command = 'protoc -I {} --go_opt=paths=source_relative{} --go_out={} {}'.format( | ||
args.src, pkg_opts, args.dest, ' '.join(map(lambda s: os.path.join(args.src, s + '.proto'), names))) | ||
result = subprocess.run(command, shell=True) | ||
|
||
# Move the files to the respective dirs | ||
for name in names: | ||
# Create dir with the same name as the file | ||
dir_name = os.path.join(args.dest, name) | ||
os.makedirs(dir_name, exist_ok=True) | ||
# Move the generated .pb.go file from the dest dir | ||
shutil.move(os.path.join(args.dest, name + '.pb.go'), | ||
os.path.join(dir_name, name + '.pb.go')) |