-
Notifications
You must be signed in to change notification settings - Fork 81
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
Linkstatic shell tests #488
Merged
+223
−38
Merged
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
dd2f5f7
specify `provides` for binary/library rules
Profpatsch dbfeb1e
skylark: add sh_inline_test
Profpatsch fc3bb83
tests/library-static-only: add sh tests to check linking
Profpatsch bc1ce90
tests: rename library-static-only to library-linkstatic-flag
Profpatsch 100073a
remove `provides` from rule(), because skydoc barfs
Profpatsch 4778b74
move sh_inline_test.bzl to tests/
Profpatsch File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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") | ||
Profpatsch marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
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 | ||
) |
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("//skylark: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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Necessary?
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.
Sadly yes, to navigate around bazelbuild/bazel#3115
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.
Could you add a note inline about this at the top of
providers.bzl
about this?