-
Notifications
You must be signed in to change notification settings - Fork 81
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #488 from tweag/linkstatic-shell-tests
Linkstatic shell tests
- Loading branch information
Showing
21 changed files
with
223 additions
and
38 deletions.
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
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
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
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
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
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
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 |
---|---|---|
@@ -1,4 +1,6 @@ | ||
exports_files(["lint.bzl"]) | ||
exports_files([ | ||
"lint.bzl", | ||
]) | ||
|
||
sh_binary( | ||
name = "buildifier", | ||
|
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,109 @@ | ||
# test whether `linkstatic` works as expected | ||
package(default_testonly = 1) | ||
|
||
load("//tests:sh_inline_test.bzl", "sh_inline_test") | ||
load(":get_library_files.bzl", "get_libraries_as_runfiles") | ||
|
||
load( | ||
"@io_tweag_rules_haskell//haskell:haskell.bzl", | ||
"haskell_library", | ||
) | ||
|
||
# only .a files | ||
haskell_library( | ||
name = "library-static-only", | ||
srcs = ["Lib.hs"], | ||
visibility = ["//visibility:public"], | ||
deps = [ | ||
"@hackage//:base", | ||
"@hackage//:bytestring", | ||
], | ||
linkstatic = True, # <-- | ||
) | ||
|
||
# both .a and .so files | ||
haskell_library( | ||
name = "library-static-and-dynamic", | ||
srcs = ["Lib.hs"], | ||
visibility = ["//visibility:public"], | ||
deps = [ | ||
"@hackage//:base", | ||
"@hackage//:bytestring", | ||
], | ||
linkstatic = False, # <-- | ||
) | ||
|
||
# extract all libraries from the haskell_library | ||
get_libraries_as_runfiles( | ||
name = "library-static-only-libraries", | ||
library = ":library-static-only", | ||
) | ||
get_libraries_as_runfiles( | ||
name = "library-static-and-dynamic-libraries", | ||
library = ":library-static-and-dynamic", | ||
) | ||
|
||
# sh_test’s `data` doesn’t add stuff to runfiles :( | ||
# sh_library can bundle different targets as runfiles for sh_test | ||
# TODO(Profpatsch): add functionality to sh_inline_test by default? | ||
sh_library( | ||
name = "bundled-dependency-files-static-only", | ||
data = [":library-static-only-libraries"] | ||
) | ||
sh_library( | ||
name = "bundled-dependency-files-static-and-dynamic", | ||
data = [":library-static-and-dynamic-libraries"] | ||
) | ||
|
||
# ensure that linkstatic=True only creates only .a, no .so | ||
sh_inline_test( | ||
name = "test-linkstatic-static-only", | ||
script = """ | ||
set -euo pipefail | ||
for f in "$@"; do | ||
if ! [[ "$f" =~ .a$ ]]; then | ||
echo "not a static library: $f" | ||
exit 1 | ||
fi | ||
done | ||
""", | ||
# pass the file names as arguments | ||
args = ["$(rootpaths :library-static-only-libraries)"], | ||
data = [ | ||
# for rootpaths | ||
":library-static-only-libraries", | ||
# to actually get the files … | ||
":bundled-dependency-files-static-only"], | ||
size = "small", | ||
) | ||
|
||
# test whether .so is linked dynamically and .a statically | ||
sh_inline_test( | ||
name = "test-libraries-static-and-dynamic", | ||
script = """ | ||
set -euo pipefail | ||
is_dynamic () { | ||
# taken from https://github.com/NixOS/nixpkgs/blob/0b3f50f844e2a6b507b18d7c5259bb850b382f87/pkgs/build-support/setup-hooks/auto-patchelf.sh#L167-L170 | ||
readelf -l -- "$1" | grep -q "^ *INTERP\\>" | ||
} | ||
for f in "$@"; do | ||
if [[ "$f" =~ .a$ ]] && is_dynamic "$f"; then | ||
echo "should be a static executable: $f" | ||
exit 1 | ||
fi | ||
if [[ "$f" =~ .so$ ]] && ! is_dynamic "$f"; then | ||
echo "should be a dynamic executable: $f" | ||
exit 1 | ||
fi | ||
done | ||
""", | ||
# pass the file names as arguments | ||
args = ["$(rootpaths :library-static-and-dynamic-libraries)"], | ||
data = [ | ||
# for rootpaths | ||
":library-static-and-dynamic-libraries", | ||
# to actually get the files … | ||
":bundled-dependency-files-static-and-dynamic"], | ||
size = "small", | ||
) |
File renamed without changes.
File renamed without changes.
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,28 @@ | ||
load( | ||
"@io_tweag_rules_haskell//haskell:private/providers.bzl", | ||
"HaskellBuildInfo", | ||
"HaskellLibraryInfo", | ||
) | ||
load("//haskell:private/set.bzl", "set") | ||
|
||
def _get_libraries_as_runfiles_impl(ctx): | ||
"""Extract all library files from a haskell_library target | ||
and put them in this target’s files""" | ||
bi = ctx.attr.library[HaskellBuildInfo] | ||
return [DefaultInfo( | ||
# not necessarily complete | ||
files = depset( | ||
direct = bi.static_libraries, | ||
transitive = [set.to_depset(bi.dynamic_libraries)] | ||
), | ||
)] | ||
|
||
get_libraries_as_runfiles = rule( | ||
_get_libraries_as_runfiles_impl, | ||
attrs = { | ||
"library": attr.label( | ||
mandatory = True, | ||
providers = [HaskellBuildInfo, HaskellLibraryInfo], | ||
) | ||
} | ||
) |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,60 @@ | ||
load("@bazel_skylib//:lib.bzl", "shell") | ||
|
||
def quote_make_variables(s): | ||
"""Quote all genrule “Make” Variables in a string.""" | ||
return s.replace("$", "$$") | ||
|
||
def target_from_string(name, string): | ||
"""Write a skylark string to a target.""" | ||
native.genrule( | ||
name = name + "-file", | ||
outs = [name], | ||
# this is exceptionally ugly. | ||
cmd = """echo -n {quoted} > $(@)""".format( | ||
# but should at least be quoted right | ||
quoted = shell.quote(quote_make_variables(string)), | ||
), | ||
) | ||
|
||
bash_runfiles_boilerplate = """\ | ||
# Copy-pasted from Bazel's Bash runfiles library (tools/bash/runfiles/runfiles.bash). | ||
set -euo pipefail | ||
if [[ ! -d "${RUNFILES_DIR:-/dev/null}" && ! -f "${RUNFILES_MANIFEST_FILE:-/dev/null}" ]]; then | ||
if [[ -f "$0.runfiles_manifest" ]]; then | ||
export RUNFILES_MANIFEST_FILE="$0.runfiles_manifest" | ||
elif [[ -f "$0.runfiles/MANIFEST" ]]; then | ||
export RUNFILES_MANIFEST_FILE="$0.runfiles/MANIFEST" | ||
elif [[ -f "$0.runfiles/bazel_tools/tools/bash/runfiles/runfiles.bash" ]]; then | ||
export RUNFILES_DIR="$0.runfiles" | ||
fi | ||
fi | ||
if [[ -f "${RUNFILES_DIR:-/dev/null}/bazel_tools/tools/bash/runfiles/runfiles.bash" ]]; then | ||
source "${RUNFILES_DIR}/bazel_tools/tools/bash/runfiles/runfiles.bash" | ||
elif [[ -f "${RUNFILES_MANIFEST_FILE:-/dev/null}" ]]; then | ||
source "$(grep -m1 "^bazel_tools/tools/bash/runfiles/runfiles.bash " \ | ||
"$RUNFILES_MANIFEST_FILE" | cut -d ' ' -f 2-)" | ||
else | ||
echo >&2 "ERROR: cannot find @bazel_tools//tools/bash/runfiles:runfiles.bash" | ||
exit 1 | ||
fi | ||
# --- end runfiles.bash initialization --- | ||
""" | ||
|
||
def sh_inline_test(name, script, **kwargs): | ||
"""Like sh_test, but instead of srcs takes the shell script | ||
as verbatim bazel string. The bash runfiles are in scope, | ||
using `rlocation` works by default. | ||
""" | ||
script_name = name + ".sh" | ||
script = bash_runfiles_boilerplate + script | ||
|
||
target_from_string(script_name, script) | ||
|
||
deps = kwargs.pop("deps", []) | ||
|
||
native.sh_test( | ||
name = name, | ||
srcs = [script_name], | ||
deps = ["@bazel_tools//tools/bash/runfiles"] + deps, | ||
**kwargs | ||
) |