Skip to content

Commit

Permalink
client_server_test find .exe on Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
aherrmann committed Jul 22, 2020
1 parent 0ad08cf commit 23f4a59
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 3 deletions.
6 changes: 3 additions & 3 deletions bazel_tools/client_server/client_server_test.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,10 @@ def client_server_test(
# Deduplicate in case any of runner, client, server are identical.
data = depset([runner, client, server]).to_list() + data,
cmd = """\
runner=$$(canonicalize_rlocation "$(rootpaths {runner})")
runner=$$(canonicalize_rlocation $$(get_exe $(rootpaths {runner})))
runner_args="{runner_args}"
client=$$(canonicalize_rlocation "$(rootpaths {client})")
server=$$(canonicalize_rlocation "$(rootpaths {server})")
client=$$(canonicalize_rlocation $$(get_exe $(rootpaths {client})))
server=$$(canonicalize_rlocation $$(get_exe $(rootpaths {server})))
server_args="{server_args}"
for file in {server_files}; do
server_args+=" $$(canonicalize_rlocation $$file)"
Expand Down
3 changes: 3 additions & 0 deletions bazel_tools/sh/sh.bzl
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# Copyright (c) 2020 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved.
# SPDX-License-Identifier: Apache-2.0

load("@os_info//:os_info.bzl", "os_name")

def _sh_inline_script_impl(ctx):
cmd = ctx.attr.cmd
cmd = ctx.expand_location(cmd, ctx.attr.data)
Expand All @@ -11,6 +13,7 @@ def _sh_inline_script_impl(ctx):
is_executable = True,
substitutions = {
"%cmd%": cmd,
"%os%": os_name,
},
)

Expand Down
32 changes: 32 additions & 0 deletions bazel_tools/sh/test.sh.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,38 @@ canonicalize_rlocation() {
# or rlocation will fail to find the path in the manifest file.
rlocation $(realpath -L -s -m --relative-to=$PWD $TEST_WORKSPACE/$1)
}
get_exe() {
# Usage: get_exe FILE...
#
# On Windows: Return the FILE ending on .exe or the first argument.
# On Unix: Return the first argument.

# Note (AH): Bazel `sh_binary` targets produce multiple outputs.
# On Unix `script`, and `script.sh`.
# On Windows `script`, `script.exe`, and `script.sh`.
# Assuming `srcs = ["script.sh"]`.
# This is an issue for macros like `client_server_test` which would like
# to determine the path to executables such as.
# `$$(canonicalize_rlocation $(rootpath {client}))`.
# For a `sh_binary` generated `{client}` this fails since `$(rootpath )` is
# not applicable to a target with multiple outputs and we have to use
# `$(rootpaths )` instead. This happens to work on Unix because the first
# item returned by `$(rootpaths )` is correctly executable. However, on
# Windows the required `.exe` wrapper is only the second item returned by
# `$(rootpaths )` and consequently the obtained `$client` path cannot be
# executed on Windows.
if [[ %os% = windows ]]; then
for arg in "$@"; do
if [[ $arg = *.exe ]]; then
echo "$arg"
return
fi
done
echo "$1"
else
echo "$1"
fi
}
set -e
%cmd%
# vim: ft=sh

0 comments on commit 23f4a59

Please sign in to comment.