From ad2ff81e06a5a48b26fc3f178c8423051da24d4c Mon Sep 17 00:00:00 2001 From: Julio Merino Date: Wed, 1 Nov 2023 05:40:37 -0700 Subject: [PATCH] Add basic implementation of rules_shtk for Bazel This change brings a new implementation of shtk build rules for Bazel, namely rules_shtk. It does so by: adding a new bazel_rules subdirectory, which contains the code of the rules_shtk; adding a new bazel_examples subdirectory, which provides end-to-end examples for usage of rules_shtk; and adding a new integration test (inttest) that leverages the bazel_examples to verify that the rules work as expected. --- .github/workflows/bazel.yml | 37 ++++++ README.md | 12 ++ bazel_examples/.bazelrc | 1 + bazel_examples/.bazelversion | 1 + bazel_examples/.gitignore | 3 + bazel_examples/BUILD.bazel | 27 +++++ bazel_examples/MODULE.bazel | 36 ++++++ bazel_examples/Makefile | 46 ++++++++ bazel_examples/WORKSPACE.bazel | 32 +++++ bazel_examples/binary/BUILD.bazel | 34 ++++++ bazel_examples/binary/hello.sh | 34 ++++++ bazel_examples/inttest.sh | 87 ++++++++++++++ .../minimum_shtk_version/BUILD.bazel | 53 +++++++++ bazel_examples/minimum_shtk_version/dual.sh | 39 ++++++ bazel_examples/test/BUILD.bazel | 40 +++++++ bazel_examples/test/adder.c | 43 +++++++ bazel_examples/test/adder_test.sh | 35 ++++++ bazel_rules/.bazelrc | 1 + bazel_rules/.bazelversion | 1 + bazel_rules/.gitignore | 1 + bazel_rules/BUILD.bazel | 60 ++++++++++ bazel_rules/MODULE.bazel | 34 ++++++ bazel_rules/WORKSPACE.bazel | 27 +++++ bazel_rules/repositories.bzl | 107 +++++++++++++++++ bazel_rules/rules.bzl | 111 ++++++++++++++++++ bazel_rules/toolchain.BUILD.tpl | 43 +++++++ bazel_rules/toolchains.bzl | 56 +++++++++ bazel_rules/versions.bzl | 49 ++++++++ 28 files changed, 1050 insertions(+) create mode 100644 .github/workflows/bazel.yml create mode 100644 bazel_examples/.bazelrc create mode 100644 bazel_examples/.bazelversion create mode 100644 bazel_examples/.gitignore create mode 100644 bazel_examples/BUILD.bazel create mode 100644 bazel_examples/MODULE.bazel create mode 100644 bazel_examples/Makefile create mode 100644 bazel_examples/WORKSPACE.bazel create mode 100644 bazel_examples/binary/BUILD.bazel create mode 100644 bazel_examples/binary/hello.sh create mode 100644 bazel_examples/inttest.sh create mode 100644 bazel_examples/minimum_shtk_version/BUILD.bazel create mode 100644 bazel_examples/minimum_shtk_version/dual.sh create mode 100644 bazel_examples/test/BUILD.bazel create mode 100644 bazel_examples/test/adder.c create mode 100644 bazel_examples/test/adder_test.sh create mode 100644 bazel_rules/.bazelrc create mode 100644 bazel_rules/.bazelversion create mode 100644 bazel_rules/.gitignore create mode 100644 bazel_rules/BUILD.bazel create mode 100644 bazel_rules/MODULE.bazel create mode 100644 bazel_rules/WORKSPACE.bazel create mode 100644 bazel_rules/repositories.bzl create mode 100644 bazel_rules/rules.bzl create mode 100644 bazel_rules/toolchain.BUILD.tpl create mode 100644 bazel_rules/toolchains.bzl create mode 100644 bazel_rules/versions.bzl diff --git a/.github/workflows/bazel.yml b/.github/workflows/bazel.yml new file mode 100644 index 0000000..00dd6a1 --- /dev/null +++ b/.github/workflows/bazel.yml @@ -0,0 +1,37 @@ +name: Bazel integration + +on: [push, pull_request] + +jobs: + build-rules: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - uses: actions/cache@v3 + with: + path: ~/.cache/bazelisk + key: ${{ runner.os }}--${{ hashFiles('**/.bazelversion') }} + - uses: bazelbuild/setup-bazelisk@v2 + - run: cd bazel_rules && bazel build //... + + build-examples: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - uses: actions/cache@v3 + with: + path: ~/.cache/bazelisk + key: ${{ runner.os }}--${{ hashFiles('**/.bazelversion') }} + - uses: bazelbuild/setup-bazelisk@v2 + - run: cd bazel_examples && bazel build //... + + test-examples: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - uses: actions/cache@v3 + with: + path: ~/.cache/bazelisk + key: ${{ runner.os }}--${{ hashFiles('**/.bazelversion') }} + - uses: bazelbuild/setup-bazelisk@v2 + - run: make -C bazel_examples diff --git a/README.md b/README.md index aa2806e..b8f000c 100644 --- a/README.md +++ b/README.md @@ -46,6 +46,18 @@ this repository, follow the instructions in the [INSTALL.md file](INSTALL.md). +Bazel integration +----------------- + +The `bazel_rules` directory provides the implementation of `rules_shtk`, a +collection of build rules for the [Bazel build system](https://bazel.build/) +to build and run `shtk` scripts. See their [`README.md`](bazel_rules/README.md) +for more details on usage. + +The `bazel_examples` directory provides sample usage of the rules. See their +[`README.md`](bazel_examples/README.md) for more details about them. + + Documentation ------------- diff --git a/bazel_examples/.bazelrc b/bazel_examples/.bazelrc new file mode 100644 index 0000000..3ce91d2 --- /dev/null +++ b/bazel_examples/.bazelrc @@ -0,0 +1 @@ +common --enable_bzlmod diff --git a/bazel_examples/.bazelversion b/bazel_examples/.bazelversion new file mode 100644 index 0000000..19b860c --- /dev/null +++ b/bazel_examples/.bazelversion @@ -0,0 +1 @@ +6.4.0 diff --git a/bazel_examples/.gitignore b/bazel_examples/.gitignore new file mode 100644 index 0000000..fe9d788 --- /dev/null +++ b/bazel_examples/.gitignore @@ -0,0 +1,3 @@ +bazel-* +inttest +!Makefile diff --git a/bazel_examples/BUILD.bazel b/bazel_examples/BUILD.bazel new file mode 100644 index 0000000..4c24019 --- /dev/null +++ b/bazel_examples/BUILD.bazel @@ -0,0 +1,27 @@ +# Copyright 2023 Julio Merino +# All rights reserved. +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions are +# met: +# +# * Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# * Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in the +# documentation and/or other materials provided with the distribution. +# * Neither the name of Google Inc. nor the names of its contributors +# may be used to endorse or promote products derived from this software +# without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/bazel_examples/MODULE.bazel b/bazel_examples/MODULE.bazel new file mode 100644 index 0000000..95ed17a --- /dev/null +++ b/bazel_examples/MODULE.bazel @@ -0,0 +1,36 @@ +# Copyright 2023 Julio Merino +# All rights reserved. +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions are +# met: +# +# * Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# * Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in the +# documentation and/or other materials provided with the distribution. +# * Neither the name of Google Inc. nor the names of its contributors +# may be used to endorse or promote products derived from this software +# without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +module( + name = "shtk_examples", + version = "0.0.0", +) + +bazel_dep(name = "rules_shtk", version = "1.8.0") + +local_path_override(module_name = "rules_shtk", path = "../bazel_rules") diff --git a/bazel_examples/Makefile b/bazel_examples/Makefile new file mode 100644 index 0000000..5b785de --- /dev/null +++ b/bazel_examples/Makefile @@ -0,0 +1,46 @@ +# Copyright 2023 Julio Merino +# All rights reserved. +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions are +# met: +# +# * Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# * Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in the +# documentation and/or other materials provided with the distribution. +# * Neither the name of Google Inc. nor the names of its contributors +# may be used to endorse or promote products derived from this software +# without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +# +# Rules to run the integration tests for the examples. +# +# We could use Bazel itself to run these tests, but doing so is difficult +# because we need to run Bazel within Bazel and that's always tricky. +# That said, while those issues are surmountable, the integration tests +# themselves would rely on the shtk rules being functional and we cannot +# trust that they are. +# + +.PHONY: check +check: inttest + @rm -rf tmp && mkdir tmp + cd tmp && REAL_HOME=$$HOME WORKSPACE=$$(pwd)/.. ../inttest + @rm -rf tmp + +inttest: inttest.sh + shtk build -m shtk_unittest_main -o $@ $< diff --git a/bazel_examples/WORKSPACE.bazel b/bazel_examples/WORKSPACE.bazel new file mode 100644 index 0000000..9655145 --- /dev/null +++ b/bazel_examples/WORKSPACE.bazel @@ -0,0 +1,32 @@ +# Copyright 2023 Julio Merino +# All rights reserved. +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions are +# met: +# +# * Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# * Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in the +# documentation and/or other materials provided with the distribution. +# * Neither the name of Google Inc. nor the names of its contributors +# may be used to endorse or promote products derived from this software +# without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +load("@rules_shtk//:repositories.bzl", "shtk_configure", "shtk_dist") + +shtk_configure() # Auto-discover installed shtk. +shtk_dist() # Download an shtk release. diff --git a/bazel_examples/binary/BUILD.bazel b/bazel_examples/binary/BUILD.bazel new file mode 100644 index 0000000..a8c7a48 --- /dev/null +++ b/bazel_examples/binary/BUILD.bazel @@ -0,0 +1,34 @@ +# Copyright 2023 Julio Merino +# All rights reserved. +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions are +# met: +# +# * Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# * Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in the +# documentation and/or other materials provided with the distribution. +# * Neither the name of Google Inc. nor the names of its contributors +# may be used to endorse or promote products derived from this software +# without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +load("@rules_shtk//:rules.bzl", "shtk_binary") + +shtk_binary( + name = "simple", + src = "hello.sh", +) diff --git a/bazel_examples/binary/hello.sh b/bazel_examples/binary/hello.sh new file mode 100644 index 0000000..1b59eeb --- /dev/null +++ b/bazel_examples/binary/hello.sh @@ -0,0 +1,34 @@ +# Copyright 2023 Julio Merino +# All rights reserved. +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions are +# met: +# +# * Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# * Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in the +# documentation and/or other materials provided with the distribution. +# * Neither the name of Google Inc. nor the names of its contributors +# may be used to endorse or promote products derived from this software +# without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +shtk_import cli + + +main() { + shtk_cli_info "Hello, world!" +} diff --git a/bazel_examples/inttest.sh b/bazel_examples/inttest.sh new file mode 100644 index 0000000..6a67c7e --- /dev/null +++ b/bazel_examples/inttest.sh @@ -0,0 +1,87 @@ +# Copyright 2023 Julio Merino +# All rights reserved. +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions are +# met: +# +# * Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# * Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in the +# documentation and/or other materials provided with the distribution. +# * Neither the name of Google Inc. nor the names of its contributors +# may be used to endorse or promote products derived from this software +# without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +shtk_import cli +shtk_import unittest + + +one_time_setup() { + # REAL_HOME provides access to the Bazel and Bazelisk caches from within this + # test program. While not strictly necessary, this makes these tests much + # faster than they would otherwise and minimizes network traffic. + [ "${REAL_HOME-unset}" != unset ] \ + || shtk_cli_error "REAL_HOME must point to the user's home directory" + + # WORKSPACE points to the root of the bazel_examples directory so that we can + # run Bazel on it. + [ "${WORKSPACE-unset}" != unset ] \ + || shtk_cli_error "WORKSPACE must point to the bazel_examples directory" + + # Create a fake Bazel binary that is aware of our configuration variables + # above. We do this so that we can invoke this wrapper via assert_command. + local real_bazel="$(which bazel)" + cat >run_bazel <= 1.50 but the toolchain provides 1." \ + ../run_bazel build "//minimum_shtk_version:${kind}_request_future" + done + + assert_command \ + -e match:"Hello, world" \ + "${WORKSPACE}/bazel-bin/minimum_shtk_version/binary_request_older" + + assert_command \ + -e match:"No tests" \ + "${WORKSPACE}/bazel-bin/minimum_shtk_version/test_request_older" +} diff --git a/bazel_examples/minimum_shtk_version/BUILD.bazel b/bazel_examples/minimum_shtk_version/BUILD.bazel new file mode 100644 index 0000000..de5aabb --- /dev/null +++ b/bazel_examples/minimum_shtk_version/BUILD.bazel @@ -0,0 +1,53 @@ +# Copyright 2023 Julio Merino +# All rights reserved. +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions are +# met: +# +# * Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# * Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in the +# documentation and/or other materials provided with the distribution. +# * Neither the name of Google Inc. nor the names of its contributors +# may be used to endorse or promote products derived from this software +# without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +load("@rules_shtk//:rules.bzl", "shtk_binary", "shtk_test") + +shtk_binary( + name = "binary_request_older", + src = "dual.sh", + minimum_shtk_version = "1.6", +) + +shtk_binary( + name = "binary_request_future", + src = "dual.sh", + minimum_shtk_version = "1.50", +) + +shtk_test( + name = "test_request_older", + src = "dual.sh", + minimum_shtk_version = "1.6", +) + +shtk_test( + name = "test_request_future", + src = "dual.sh", + minimum_shtk_version = "1.50", +) diff --git a/bazel_examples/minimum_shtk_version/dual.sh b/bazel_examples/minimum_shtk_version/dual.sh new file mode 100644 index 0000000..744d6ed --- /dev/null +++ b/bazel_examples/minimum_shtk_version/dual.sh @@ -0,0 +1,39 @@ +# Copyright 2023 Julio Merino +# All rights reserved. +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions are +# met: +# +# * Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# * Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in the +# documentation and/or other materials provided with the distribution. +# * Neither the name of Google Inc. nor the names of its contributors +# may be used to endorse or promote products derived from this software +# without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +shtk_import cli + + +main() { + shtk_cli_info "Hello, world!" +} + + +shtk_unittest_main() { + shtk_cli_info "No tests" +} diff --git a/bazel_examples/test/BUILD.bazel b/bazel_examples/test/BUILD.bazel new file mode 100644 index 0000000..a0ca91b --- /dev/null +++ b/bazel_examples/test/BUILD.bazel @@ -0,0 +1,40 @@ +# Copyright 2023 Julio Merino +# All rights reserved. +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions are +# met: +# +# * Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# * Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in the +# documentation and/or other materials provided with the distribution. +# * Neither the name of Google Inc. nor the names of its contributors +# may be used to endorse or promote products derived from this software +# without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +load("@rules_shtk//:rules.bzl", "shtk_test") + +cc_binary( + name = "adder", + srcs = ["adder.c"], +) + +shtk_test( + name = "adder_test", + src = "adder_test.sh", + data = [":adder"], +) diff --git a/bazel_examples/test/adder.c b/bazel_examples/test/adder.c new file mode 100644 index 0000000..d0dd351 --- /dev/null +++ b/bazel_examples/test/adder.c @@ -0,0 +1,43 @@ +// Copyright 2023 Julio Merino +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above copyright +// notice, this list of conditions and the following disclaimer in the +// documentation and/or other materials provided with the distribution. +// * Neither the name of Google Inc. nor the names of its contributors +// may be used to endorse or promote products derived from this software +// without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +#include +#include +#include + +int main(int argc, char** argv) { + if (argc != 3) { + errx(EXIT_FAILURE, "Requires two integer arguments"); + } + int x1 = atoi(argv[1]); + int x2 = atoi(argv[2]); + + printf("The sum of %d and %d is %d\n", x1, x2, x1 + x2); + + return EXIT_SUCCESS; +} diff --git a/bazel_examples/test/adder_test.sh b/bazel_examples/test/adder_test.sh new file mode 100644 index 0000000..e707bfd --- /dev/null +++ b/bazel_examples/test/adder_test.sh @@ -0,0 +1,35 @@ +# Copyright 2023 Julio Merino +# All rights reserved. +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions are +# met: +# +# * Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# * Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in the +# documentation and/or other materials provided with the distribution. +# * Neither the name of Google Inc. nor the names of its contributors +# may be used to endorse or promote products derived from this software +# without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +shtk_import unittest + + +shtk_unittest_add_test simple +simple_test() { + assert_command -o inline:"The sum of 2 and 3 is 5\n" ../test/adder 2 3 +} diff --git a/bazel_rules/.bazelrc b/bazel_rules/.bazelrc new file mode 100644 index 0000000..3ce91d2 --- /dev/null +++ b/bazel_rules/.bazelrc @@ -0,0 +1 @@ +common --enable_bzlmod diff --git a/bazel_rules/.bazelversion b/bazel_rules/.bazelversion new file mode 100644 index 0000000..19b860c --- /dev/null +++ b/bazel_rules/.bazelversion @@ -0,0 +1 @@ +6.4.0 diff --git a/bazel_rules/.gitignore b/bazel_rules/.gitignore new file mode 100644 index 0000000..ac51a05 --- /dev/null +++ b/bazel_rules/.gitignore @@ -0,0 +1 @@ +bazel-* diff --git a/bazel_rules/BUILD.bazel b/bazel_rules/BUILD.bazel new file mode 100644 index 0000000..e203a4c --- /dev/null +++ b/bazel_rules/BUILD.bazel @@ -0,0 +1,60 @@ +# Copyright 2023 Julio Merino +# All rights reserved. +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions are +# met: +# +# * Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# * Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in the +# documentation and/or other materials provided with the distribution. +# * Neither the name of Google Inc. nor the names of its contributors +# may be used to endorse or promote products derived from this software +# without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +load("@bazel_skylib//rules:common_settings.bzl", "string_setting") +load(":repositories.bzl", "DISTS") +load(":toolchains.bzl", "shtk_toolchain") +load(":versions.bzl", "versions") + +string_setting( + name = "shtk_version", + build_setting_default = "1.7", + visibility = ["//visibility:public"], +) + +[ + config_setting( + name = "shtk_{}".format(versions.canonicalize(version)), + flag_values = {":shtk_version": version}, + visibility = ["//visibility:public"], + ) + for version in DISTS.keys() + ["1.8"] +] + +shtk_toolchain(name = "shtk_system_toolchain") + +toolchain_type( + name = "toolchain_type", + visibility = ["//visibility:public"], +) + +toolchain( + name = "system_toolchain", + toolchain = ":shtk_system_toolchain", + toolchain_type = ":toolchain_type", +) diff --git a/bazel_rules/MODULE.bazel b/bazel_rules/MODULE.bazel new file mode 100644 index 0000000..18432a1 --- /dev/null +++ b/bazel_rules/MODULE.bazel @@ -0,0 +1,34 @@ +# Copyright 2023 Julio Merino +# All rights reserved. +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions are +# met: +# +# * Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# * Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in the +# documentation and/or other materials provided with the distribution. +# * Neither the name of Google Inc. nor the names of its contributors +# may be used to endorse or promote products derived from this software +# without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +module( + name = "rules_shtk", + version = "1.8.0", +) + +bazel_dep(name = "bazel_skylib", version = "1.4.2") diff --git a/bazel_rules/WORKSPACE.bazel b/bazel_rules/WORKSPACE.bazel new file mode 100644 index 0000000..4c24019 --- /dev/null +++ b/bazel_rules/WORKSPACE.bazel @@ -0,0 +1,27 @@ +# Copyright 2023 Julio Merino +# All rights reserved. +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions are +# met: +# +# * Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# * Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in the +# documentation and/or other materials provided with the distribution. +# * Neither the name of Google Inc. nor the names of its contributors +# may be used to endorse or promote products derived from this software +# without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/bazel_rules/repositories.bzl b/bazel_rules/repositories.bzl new file mode 100644 index 0000000..fedfbd3 --- /dev/null +++ b/bazel_rules/repositories.bzl @@ -0,0 +1,107 @@ +# Copyright 2023 Julio Merino +# All rights reserved. +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions are +# met: +# +# * Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# * Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in the +# documentation and/or other materials provided with the distribution. +# * Neither the name of Google Inc. nor the names of its contributors +# may be used to endorse or promote products derived from this software +# without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +load(":toolchains.bzl", "shtk_toolchain") +load(":versions.bzl", "versions") + +DISTS = { + "1.7": "9386b7dbf1a28361eaf56dd1712385abdbdf3a02f03e17ccbf5579bf22695e27", +} + +DEFAULT_DIST = "1.7" + +def _shtk_autoconf_toolchain_impl(repository_ctx): + shtk_path = repository_ctx.which("shtk") + + result = repository_ctx.execute([shtk_path, "version"]) + if result.return_code != 0: + fail("failed to query version: " + result.stdout + result.stderr) + version = result.stdout.rstrip().split(" ")[1] + + repository_ctx.template( + "BUILD.bazel", + repository_ctx.path(Label("@rules_shtk//:toolchain.BUILD.tpl")), + { + "%{shtk_path}": str(shtk_path), + "%{version}": version, + }, + ) + +shtk_autoconf_toolchain = repository_rule( + implementation = _shtk_autoconf_toolchain_impl, + configure = True, +) + +def shtk_configure(): + shtk_autoconf_toolchain(name = "shtk_autoconf") + native.register_toolchains( + "@shtk_autoconf//:toolchain", + ) + +def _shtk_dist_toolchain_impl(repository_ctx): + version = repository_ctx.attr.version + url = "https://github.com/jmmv/shtk/releases/download/shtk-{}/shtk-{}.tar.gz".format(version, version) + + repository_ctx.download_and_extract( + url, + "shtk.tar.gz", + sha256 = repository_ctx.attr.sha256, + ) + + result = repository_ctx.execute( + [ + repository_ctx.which("sh"), + "-c", + "prefix=\"$(pwd)/local\" && cd " + str(repository_ctx.path("shtk.tar.gz/shtk-" + version)) + " && ./configure --prefix \"${prefix}\" && make install" + ], + ) + if result.return_code != 0: + fail("build failed: " + result.stdout + result.stderr) + + repository_ctx.template( + "BUILD.bazel", + repository_ctx.path(Label("@rules_shtk//:toolchain.BUILD.tpl")), + { + "%{shtk_path}": str(repository_ctx.path("local")) + "/bin/shtk", + "%{version}": version, + }, + ) + +shtk_dist_toolchain = repository_rule( + implementation = _shtk_dist_toolchain_impl, + attrs = { + "version": attr.string(), + "sha256": attr.string(), + }, +) + +def shtk_dist(version = DEFAULT_DIST): + repository = "shtk_dist_" + versions.canonicalize(version) + sha256 = DISTS[version] + shtk_dist_toolchain(name = repository, version = version, sha256 = sha256) + native.register_toolchains("@{}//:toolchain".format(repository)) diff --git a/bazel_rules/rules.bzl b/bazel_rules/rules.bzl new file mode 100644 index 0000000..c6272c0 --- /dev/null +++ b/bazel_rules/rules.bzl @@ -0,0 +1,111 @@ +# Copyright 2023 Julio Merino +# All rights reserved. +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions are +# met: +# +# * Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# * Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in the +# documentation and/or other materials provided with the distribution. +# * Neither the name of Google Inc. nor the names of its contributors +# may be used to endorse or promote products derived from this software +# without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +load(":versions.bzl", "versions") + + +_SHTK_ATTRS = { + "src": attr.label( + mandatory = True, + allow_single_file = True, + ), + "minimum_shtk_version": attr.string(default = "1.0"), +} + + +def _shtk_build(ctx, mnemonic, main): + src = ctx.attr.src[DefaultInfo].files.to_list()[0] + out = ctx.actions.declare_file(ctx.attr.name) + info = ctx.toolchains["//:toolchain_type"].shtkinfo + + if versions.lt(info.version, ctx.attr.minimum_shtk_version): + fail("Target requires shtk >= {} but the toolchain provides {}".format( + ctx.attr.minimum_shtk_version, info.version)) + + ctx.actions.run( + mnemonic = mnemonic, + executable = info.path, + arguments = ["build", "-m", main, "-o", out.path, src.short_path], + inputs = [src], + outputs = [out], + ) + + return out + + +def _shtk_binary_impl(ctx): + out = _shtk_build(ctx, "ShtkBinary", "main") + + return [DefaultInfo( + files = depset([out]), + executable = out, + )] + + +shtk_binary = rule( + doc = "Rule for creating an shtk binary.", + implementation = _shtk_binary_impl, + attrs = _SHTK_ATTRS, + executable = True, + toolchains = ["//:toolchain_type"], +) + + +def _shtk_test_impl(ctx): + out = _shtk_build(ctx, "ShtkTest", "shtk_unittest_main") + + runfiles = ctx.runfiles(files = ctx.files.data) + transitive_runfiles = [] + for target in ctx.attr.data: + transitive_runfiles.append(target[DefaultInfo].default_runfiles) + runfiles = runfiles.merge_all(transitive_runfiles) + + return [ + DefaultInfo( + files = depset([out]), + runfiles = runfiles, + executable = out, + ), + RunEnvironmentInfo( + environment = ctx.attr.env, + inherited_environment = ctx.attr.env_inherit, + ), + ] + + +shtk_test = rule( + doc = "Rule for creating an shtk test.", + implementation = _shtk_test_impl, + attrs = _SHTK_ATTRS | { + "data": attr.label_list(), + "env": attr.string_dict(), + "env_inherit": attr.string_list(), + }, + toolchains = ["//:toolchain_type"], + test = True, +) diff --git a/bazel_rules/toolchain.BUILD.tpl b/bazel_rules/toolchain.BUILD.tpl new file mode 100644 index 0000000..9bc542b --- /dev/null +++ b/bazel_rules/toolchain.BUILD.tpl @@ -0,0 +1,43 @@ +# Copyright 2023 Julio Merino +# All rights reserved. +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions are +# met: +# +# * Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# * Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in the +# documentation and/or other materials provided with the distribution. +# * Neither the name of Google Inc. nor the names of its contributors +# may be used to endorse or promote products derived from this software +# without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +load("@rules_shtk//:toolchains.bzl", "shtk_toolchain") +load("@rules_shtk//:versions.bzl", "versions") + +shtk_toolchain( + name = "shtk_toolchain", + shtk_path = "%{shtk_path}", + shtk_version = "%{version}", +) + +toolchain( + name = "toolchain", + toolchain = ":shtk_toolchain", + toolchain_type = "@rules_shtk//:toolchain_type", + visibility = ["//visibility:public"], +) diff --git a/bazel_rules/toolchains.bzl b/bazel_rules/toolchains.bzl new file mode 100644 index 0000000..4552cc2 --- /dev/null +++ b/bazel_rules/toolchains.bzl @@ -0,0 +1,56 @@ +# Copyright 2023 Julio Merino +# All rights reserved. +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions are +# met: +# +# * Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# * Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in the +# documentation and/or other materials provided with the distribution. +# * Neither the name of Google Inc. nor the names of its contributors +# may be used to endorse or promote products derived from this software +# without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +ShtkInfo = provider( + doc = "Details about the shtk(1) tool.", + fields = { + "path": "The path to the shtk(1) tool", + "version": "The version of the shtk(3) libraries", + # TODO(jmmv): Propagate what shell they use and allow selection. + # TODO(jmmv): Propagate provenance and allow selection. + }, +) + + +def _shtk_toolchain_impl(ctx): + toolchain_info = platform_common.ToolchainInfo( + shtkinfo = ShtkInfo( + path = ctx.attr.shtk_path, + version = ctx.attr.shtk_version, + ), + ) + return [toolchain_info] + + +shtk_toolchain = rule( + implementation = _shtk_toolchain_impl, + attrs = { + "shtk_path": attr.string(), + "shtk_version": attr.string(), + }, +) diff --git a/bazel_rules/versions.bzl b/bazel_rules/versions.bzl new file mode 100644 index 0000000..e602a0d --- /dev/null +++ b/bazel_rules/versions.bzl @@ -0,0 +1,49 @@ +# Copyright 2023 Julio Merino +# All rights reserved. +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions are +# met: +# +# * Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# * Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in the +# documentation and/or other materials provided with the distribution. +# * Neither the name of Google Inc. nor the names of its contributors +# may be used to endorse or promote products derived from this software +# without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +"""Utilities to manipulate shtk version numbers.""" + + +def _lt(dotted_lhs, dotted_rhs): + """Given two dotted version numbers, returns true if the first one is + strictly before the second one.""" + lhs = [int(s) for s in dotted_lhs.split(".")] + rhs = [int(s) for s in dotted_rhs.split(".")] + return lhs < rhs + + +def _canonicalize(dotted_version): + """Converts a dotted version number into its canonical form to appear in a + Bazel target name.""" + return dotted_version.replace(".", "_") + + +versions = struct( + canonicalize = _canonicalize, + lt = _lt, +)