-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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
api: add go proto generation script #8155
Changes from 10 commits
7296975
494ecd1
6bea765
55cd052
41e2254
4ab0339
0b8c0df
b692406
7745cca
6a40cc0
cd50630
07bba3b
790fa31
d3bdc13
85923b3
e415f56
cf27aa1
91dacaa
bdf73b3
beee205
f0e736e
21f10e7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
#!/usr/bin/env python3 | ||
from subprocess import check_output | ||
kyessenov marked this conversation as resolved.
Show resolved
Hide resolved
|
||
from subprocess import call | ||
|
||
import glob | ||
import os | ||
import shutil | ||
import sys | ||
|
||
# Find the locations of the workspace root and the generated files directory. | ||
workspace = check_output(['bazel', 'info', 'workspace']).decode().strip() | ||
bazel_bin = check_output(['bazel', 'info', 'bazel-bin']).decode().strip() | ||
targets = '@envoy_api//...' | ||
import_base = 'github.com/envoyproxy/go-control-plane' | ||
output_base = 'build_go' | ||
kyessenov marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
go_protos = check_output([ | ||
'bazel', | ||
'query', | ||
'kind("go_proto_library", %s)' % targets, | ||
]).split() | ||
|
||
# Each rule has the form @envoy_api//foo/bar:baz_go_proto. | ||
# First build all the rules to ensure we have the output files. | ||
if call(['bazel', 'build', '-c', 'fastbuild'] + go_protos) != 0: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. just use subprocess.check_call? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
print('Build failed') | ||
sys.exit(1) | ||
|
||
shutil.rmtree(os.path.join(workspace, output_base, 'envoy'), ignore_errors=True) | ||
for rule in go_protos: | ||
# Example rule: | ||
# @envoy_api//envoy/config/bootstrap/v2:pkg_go_proto | ||
# | ||
# Example generated directory: | ||
# bazel-bin/external/envoy_api/envoy/config/bootstrap/v2/linux_amd64_stripped/pkg_go_proto%/github.com/envoyproxy/go-control-plane/envoy/config/bootstrap/v2/ | ||
# | ||
# Example output directory: | ||
# go_out/envoy/config/bootstrap/v2 | ||
rule_dir, proto = rule.decode()[len('@envoy_api//'):].rsplit(':', 1) | ||
|
||
input_dir = os.path.join(bazel_bin, 'external', 'envoy_api', rule_dir, 'linux_amd64_stripped', | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This code is quite similar to https://github.com/envoyproxy/envoy/blob/master/docs/build.sh#L66. Is there a way to refactor to share, or maybe you can avoid reaching into There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. linux_amd64_stripped is specific to rules_go I think. It's not coming from bazel itself. I've tried to figure out how to get generated files with bazel query but eventually gave up. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, it's due to compilation modes in go. It statically links everything, so it has to keep outputs in sub-directories https://github.com/bazelbuild/rules_go/blob/0303b3a69695e35940b09ddbf7a444bcc7fbefd4/go/private/mode.bzl. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you specify There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe it's better to recommend running this script with empty There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yeah an empty bazelrc works too but I think that ignores workspace .bazelrc There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ok, -c fastbuild is better than ignoring workspace bazel rc. |
||
proto + '%', import_base, rule_dir) | ||
input_files = glob.glob(os.path.join(input_dir, '*.go')) | ||
output_dir = os.path.join(workspace, output_base, rule_dir) | ||
|
||
# Ensure the output directory exists | ||
os.makedirs(output_dir, 0o755, exist_ok=True) | ||
for generated_file in input_files: | ||
shutil.copy(generated_file, output_dir) | ||
os.chmod(os.path.join(output_dir, generated_file), 0o644) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You could optionally just call |
||
print('Go artifacts placed into: ' + output_base) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How does this impact local development flows?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This aligns the generated/bazel-internal build with the public bazel-less build, so I think it's an improvement.