Skip to content

Commit

Permalink
Removing dependency on Bash on Windows platform
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexander Golovlev authored and Alexander Golovlev committed May 28, 2024
1 parent 851143f commit e546332
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 23 deletions.
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

0 comments on commit e546332

Please sign in to comment.