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

Removing dependency on Bash on Windows platform #314

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
28 changes: 21 additions & 7 deletions modules/core/internal/common.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,9 @@ def get_output_filename(src_file, pattern, proto_info):

return filename

def _is_windows(ctx):
return ctx.configuration.host_path_separator == ";"

def copy_file(ctx, src_file, dest_path, sibling = None):
"""
Copy a file to a new path destination
Expand All @@ -279,13 +282,24 @@ def copy_file(ctx, src_file, dest_path, sibling = None):

"""
dest_file = ctx.actions.declare_file(dest_path, sibling = sibling)
ctx.actions.run_shell(
mnemonic = "CopyFile",
inputs = [src_file],
outputs = [dest_file],
command = "cp '{}' '{}'".format(src_file.path, dest_file.path),
progress_message = "copying file {} to {}".format(src_file.path, dest_file.path),
)
if _is_windows(ctx):
ctx.actions.run(
mnemonic = "CopyFile",
inputs = [src_file],
outputs = [dest_file],
executable = "cmd.exe",
arguments = ["/C", "copy", src_file.path.replace("/", "\\"), dest_file.path.replace("/", "\\")],
progress_message = "copying file {} to {}".format(src_file.path, dest_file.path),
use_default_shell_env = True,
)
else:
ctx.actions.run_shell(
mnemonic = "CopyFile",
inputs = [src_file],
outputs = [dest_file],
command = "cp '{}' '{}'".format(src_file.path, dest_file.path),
progress_message = "copying file {} to {}".format(src_file.path, dest_file.path),
)
return dest_file

def descriptor_proto_path(proto, proto_info):
Expand Down
53 changes: 37 additions & 16 deletions modules/core/internal/compile.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ proto_compile_attrs = {
),
}

def _is_windows(ctx):
return ctx.configuration.host_path_separator == ";"

def proto_compile_impl(ctx):
"""
Common implementation function for lang_*_compile rules.
Expand Down Expand Up @@ -335,7 +338,7 @@ def proto_compile(ctx, options, extra_protoc_args, extra_protoc_files):

# $@ is replaced with args list and is quote wrapped to support paths with special chars
mnemonic = "ProtoCompile"
command = ("mkdir -p '{}' && ".format(premerge_root)) + protoc.path + ' "$@"'
command = ('mkdir -p "{}" && '.format(premerge_root)) + protoc.path + ' "$@"'
cmd_inputs += extra_protoc_files
tools = [protoc] + ([plugin.tool_executable] if plugin.tool_executable else [])

Expand Down Expand Up @@ -375,21 +378,39 @@ def proto_compile(ctx, options, extra_protoc_args, extra_protoc_files):
}

# Run protoc (https://bazel.build/rules/lib/actions#run_shell)
ctx.actions.run_shell(
mnemonic = mnemonic,
command = command,
arguments = [args],
inputs = cmd_inputs,
tools = tools,
outputs = plugin_protoc_outputs,
env = plugin_env,
use_default_shell_env = plugin.use_built_in_shell_environment,
input_manifests = cmd_input_manifests,
progress_message = "Compiling protoc outputs for {} plugin on target {}".format(
plugin.name,
ctx.label,
),
)
if _is_windows(ctx):
command = command.replace(' "$@"', "").replace("/", "\\").replace("-p ", "").replace("&&", "&")
ctx.actions.run(
mnemonic = mnemonic,
executable = "cmd.exe",
arguments = ["/C", command, args],
inputs = cmd_inputs,
tools = tools,
outputs = plugin_protoc_outputs,
env = plugin_env,
use_default_shell_env = plugin.use_built_in_shell_environment,
input_manifests = cmd_input_manifests,
progress_message = "Compiling protoc outputs for {} plugin on target {}".format(
plugin.name,
ctx.label,
),
)
else:
ctx.actions.run_shell(
mnemonic = mnemonic,
command = command,
arguments = [args],
inputs = cmd_inputs,
tools = tools,
outputs = plugin_protoc_outputs,
env = plugin_env,
use_default_shell_env = plugin.use_built_in_shell_environment,
input_manifests = cmd_input_manifests,
progress_message = "Compiling protoc outputs for {} plugin on target {}".format(
plugin.name,
ctx.label,
),
)

# Build final output defaults for merged locations
output_root = get_package_root(ctx) + "/" + ctx.label.name
Expand Down
Loading