Skip to content

Commit

Permalink
Add tests for common toolchain operations.
Browse files Browse the repository at this point in the history
Part of bazelbuild#2219.

Change-Id: I4b6358742e614f3f447abf0dabf3c9344a64a236
  • Loading branch information
katre committed Jul 13, 2017
1 parent 23570a7 commit 30b7805
Showing 1 changed file with 219 additions and 26 deletions.
245 changes: 219 additions & 26 deletions src/test/shell/bazel/toolchain_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,7 @@ source "${CURRENT_DIR}/../integration_test_setup.sh" \
function set_up() {
create_new_workspace

# Create shared constraints.
mkdir -p constraints
cat >>constraints/BUILD <<EOF
package(default_visibility = ['//visibility:public'])
constraint_setting(name = 'os')
constraint_value(name = 'linux',
constraint_setting = ':os')
constraint_value(name = 'mac',
constraint_setting = ':os')
EOF

# Create shared report rule for printing info.
# Create shared report rule for printing toolchain info.
mkdir report
touch report/BUILD
cat >>report/report.bzl <<EOF
Expand All @@ -49,28 +38,27 @@ def _report_impl(ctx):
print('%s = "%s"' % (field, value))
report_toolchain = rule(
_report_impl,
attrs = {
'fields': attr.string_list(),
'toolchain': attr.label(providers = [platform_common.ToolchainInfo]),
}
implementation = _report_impl,
attrs = {
'fields': attr.string_list(),
'toolchain': attr.label(providers = [platform_common.ToolchainInfo]),
}
)
EOF
}

function test_toolchain_rule() {

function write_test_toolchain() {
mkdir -p toolchain
cat >> toolchain/toolchain.bzl <<EOF
def _test_toolchain_impl(ctx):
toolchain = platform_common.ToolchainInfo(
type = Label('//toolchain:test_toolchain_type'),
type = Label('//toolchain:test_toolchain'),
extra_label = ctx.attr.extra_label,
extra_str = ctx.attr.extra_str)
return [toolchain]
test_toolchain = rule(
_test_toolchain_impl,
implementation = _test_toolchain_impl,
attrs = {
'extra_label': attr.label(),
'extra_str': attr.string(),
Expand All @@ -79,10 +67,83 @@ test_toolchain = rule(
EOF

cat >> toolchain/BUILD <<EOF
toolchain_type(name = 'test_toolchain')
EOF
}

function write_test_rule() {
mkdir -p toolchain
cat >> toolchain/rule.bzl <<EOF
def _impl(ctx):
toolchain = ctx.toolchains['//toolchain:test_toolchain']
message = ctx.attr.message
print(
'Using toolchain: rule message: "%s", toolchain extra_str: "%s"' %
(message, toolchain.extra_str))
return []
use_toolchain = rule(
implementation = _impl,
attrs = {
'message': attr.string(),
},
toolchains = ['//toolchain:test_toolchain'],
)
EOF
}

function write_test_aspect() {
mkdir -p toolchain
cat >> toolchain/aspect.bzl <<EOF
def _impl(target, ctx):
toolchain = ctx.toolchains['//toolchain:test_toolchain']
message = ctx.rule.attr.message
print(
'Using toolchain in aspect: rule message: "%s", toolchain extra_str: "%s"' %
(message, toolchain.extra_str))
return []
use_toolchain = aspect(
implementation = _impl,
attrs = {},
toolchains = ['//toolchain:test_toolchain'],
)
EOF
}

function write_toolchains() {
cat >> WORKSPACE <<EOF
register_toolchains('//:toolchain_1')
EOF

cat >> BUILD <<EOF
load('//toolchain:toolchain.bzl', 'test_toolchain')
# Define the toolchain.
filegroup(name = 'dep_rule')
test_toolchain(
name = 'toolchain_impl_1',
extra_label = ':dep_rule',
extra_str = 'foo from 1')
# Declare the toolchain.
toolchain(
name = 'toolchain_1',
toolchain_type = '//toolchain:test_toolchain',
exec_compatible_with = [],
target_compatible_with = [],
toolchain = ':toolchain_impl_1')
EOF
}

function test_toolchain_provider() {
write_test_toolchain

cat >> BUILD <<EOF
load('//toolchain:toolchain.bzl', 'test_toolchain')
load('//report:report.bzl', 'report_toolchain')
load(':toolchain.bzl', 'test_toolchain')
filegroup(name = 'dep_rule')
toolchain_type(name = 'test_toolchain_type')
test_toolchain(
name = 'linux_toolchain',
extra_label = ':dep_rule',
Expand All @@ -95,10 +156,142 @@ report_toolchain(
)
EOF

bazel build //toolchain:report &> $TEST_log || fail "Build failed"
expect_log 'type = "//toolchain:test_toolchain_type"'
expect_log 'extra_label = "//toolchain:dep_rule"'
bazel build //:report &> $TEST_log || fail "Build failed"
expect_log 'type = "//toolchain:test_toolchain"'
expect_log 'extra_label = "//:dep_rule"'
expect_log 'extra_str = "bar"'
}

function test_toolchain_use_in_rule {
write_test_toolchain
write_test_rule
write_toolchains

mkdir -p demo
cat >> demo/BUILD <<EOF
load('//toolchain:rule.bzl', 'use_toolchain')
# Use the toolchain.
use_toolchain(
name = 'use',
message = 'this is the rule')
EOF

bazel build //demo:use &> $TEST_log || fail "Build failed"
expect_log 'Using toolchain: rule message: "this is the rule", toolchain extra_str: "foo from 1"'
}

function test_toolchain_use_in_aspect {
write_test_toolchain
write_test_aspect
write_toolchains

mkdir -p demo
cat >> demo/demo.bzl <<EOF
def _impl(ctx):
return []
demo = rule(
implementation = _impl,
attrs = {
'message': attr.string(),
}
)
EOF
cat >> demo/BUILD <<EOF
load(':demo.bzl', 'demo')
demo(
name = 'demo',
message = 'bar from demo')
EOF

bazel build \
--aspects //toolchain:aspect.bzl%use_toolchain \
//demo:demo &> $TEST_log || fail "Build failed"
expect_log 'Using toolchain in aspect: rule message: "bar from demo", toolchain extra_str: "foo from 1"'
}

function test_toolchain_constraints() {
write_test_toolchain
write_test_rule

cat >> WORKSPACE <<EOF
register_toolchains('//:toolchain_1')
register_toolchains('//:toolchain_2')
EOF

cat >> BUILD <<EOF
load('//toolchain:toolchain.bzl', 'test_toolchain')
# Define constraints.
constraint_setting(name = 'setting')
constraint_value(name = 'value1', constraint_setting = ':setting')
constraint_value(name = 'value2', constraint_setting = ':setting')
platform(
name = 'platform1',
constraint_values = [':value1'],
visibility = ['//visibility:public'])
platform(
name = 'platform2',
constraint_values = [':value2'],
visibility = ['//visibility:public'])
# Define the toolchain.
filegroup(name = 'dep_rule')
test_toolchain(
name = 'toolchain_impl_1',
extra_label = ':dep_rule',
extra_str = 'foo from 1')
test_toolchain(
name = 'toolchain_impl_2',
extra_label = ':dep_rule',
extra_str = 'foo from 2')
# Declare the toolchain.
toolchain(
name = 'toolchain_1',
toolchain_type = '//toolchain:test_toolchain',
exec_compatible_with = [':value1'],
target_compatible_with = [':value2'],
toolchain = ':toolchain_impl_1')
toolchain(
name = 'toolchain_2',
toolchain_type = '//toolchain:test_toolchain',
exec_compatible_with = [':value2'],
target_compatible_with = [':value1'],
toolchain = ':toolchain_impl_2')
EOF

mkdir -p demo
cat >> demo/BUILD <<EOF
load('//toolchain:rule.bzl', 'use_toolchain')
# Use the toolchain.
use_toolchain(
name = 'use',
message = 'this is the rule')
EOF

# This should use toolchain_1.
bazel build \
--experimental_host_platform=//:platform1 \
--experimental_platforms=//:platform2 \
//demo:use &> $TEST_log || fail "Build failed"
expect_log 'Using toolchain: rule message: "this is the rule", toolchain extra_str: "foo from 1"'

# This should use toolchain_2.
bazel build \
--experimental_host_platform=//:platform2 \
--experimental_platforms=//:platform1 \
//demo:use &> $TEST_log || fail "Build failed"
expect_log 'Using toolchain: rule message: "this is the rule", toolchain extra_str: "foo from 2"'

# This should not match any toolchains.
bazel build \
--experimental_host_platform=//:platform1 \
--experimental_platforms=//:platform1 \
//demo:use &> $TEST_log && fail "Build failure expected"
expect_log 'no matching toolchains found for types //toolchain:test_toolchain for target //demo:use'
expect_not_log 'Using toolchain: rule message:'
}

run_suite "toolchain tests"

0 comments on commit 30b7805

Please sign in to comment.