From a1293e74b8c0b82a570fe68d964d4a5ab603fc56 Mon Sep 17 00:00:00 2001 From: zadig Date: Sun, 10 Mar 2024 18:36:51 +0100 Subject: [PATCH] Initial commit. --- .buckconfig | 18 +++ .buckroot | 0 .github/workflows/integration.yml | 47 +++++++ .github/workflows/linter.yml | 27 ++++ .github/workflows/linux.yml | 35 ++++++ .github/workflows/macos.yml | 32 +++++ .gitignore | 5 + BUCK | 26 ++++ LICENSE | 202 ++++++++++++++++++++++++++++++ README.md | 116 +++++++++++++++++ docs/BUCK | 4 + docs/generate.sh | 27 ++++ docs/root/openssl/rules.bzl.md | 59 +++++++++ openssl/BUCK | 0 openssl/providers/archive.bzl | 29 +++++ openssl/providers/build.bzl | 48 +++++++ openssl/rules.bzl | 21 ++++ openssl/rules/archive.bzl | 171 +++++++++++++++++++++++++ openssl/rules/build.bzl | 175 ++++++++++++++++++++++++++ toolchains/BUCK | 26 ++++ 20 files changed, 1068 insertions(+) create mode 100644 .buckconfig create mode 100644 .buckroot create mode 100644 .github/workflows/integration.yml create mode 100644 .github/workflows/linter.yml create mode 100644 .github/workflows/linux.yml create mode 100644 .github/workflows/macos.yml create mode 100644 .gitignore create mode 100644 BUCK create mode 100644 LICENSE create mode 100644 README.md create mode 100644 docs/BUCK create mode 100755 docs/generate.sh create mode 100644 docs/root/openssl/rules.bzl.md create mode 100644 openssl/BUCK create mode 100644 openssl/providers/archive.bzl create mode 100644 openssl/providers/build.bzl create mode 100644 openssl/rules.bzl create mode 100644 openssl/rules/archive.bzl create mode 100644 openssl/rules/build.bzl create mode 100644 toolchains/BUCK diff --git a/.buckconfig b/.buckconfig new file mode 100644 index 0000000..7539ade --- /dev/null +++ b/.buckconfig @@ -0,0 +1,18 @@ +[repositories] +root = . +rules_openssl = . +prelude = prelude +toolchains = toolchains +none = none + +[repository_aliases] +config = prelude +ovr_config = prelude +fbcode = none +fbsource = none +fbcode_macros = none +buck = none + +[parser] +target_platform_detector_spec = target:root//...->prelude//platforms:default +target_platform_detector_spec = target:rules_openssl//...->prelude//platforms:default diff --git a/.buckroot b/.buckroot new file mode 100644 index 0000000..e69de29 diff --git a/.github/workflows/integration.yml b/.github/workflows/integration.yml new file mode 100644 index 0000000..291b9ca --- /dev/null +++ b/.github/workflows/integration.yml @@ -0,0 +1,47 @@ +name: Integration tests + +on: + push: + branches: + - "*" + - "!sapling-pr-archive-*" + +jobs: + test_integration_linux_x86_64: + runs-on: ubuntu-latest + strategy: + fail-fast: false + steps: + - name: Install dependencies + run: | + sudo apt-get update && sudo apt-get install -y gcc + + - name: Fetch Buck2 + uses: zadlg/buck2-github-composite-action@v2 + + - name: Initialize an empty buck2 project + run: | + mkdir example/ && cd example + buck2 init --git + mkdir rules_openssl + + - name: Checkout code + uses: actions/checkout@v4 + with: + path: example/rules_openssl + + - name: Create the `.buckconfig` file + working-directory: example/ + run: | + mv rules_openssl/.buckconfig . + sed -i 's/rules_openssl = ./rules_openssl = rules_openssl/g' .buckconfig + touch .buckroot + + - name: Import `openssl_archive` symbol in main BUCK file. + working-directory: example/ + run: | + echo -ne 'load("@rules_openssl//openssl:rules.bzl", "openssl_archive")' > BUCK + + - name: Compile OpenSSL + working-directory: example/ + run: buck2 build -- @rules_openssl//... diff --git a/.github/workflows/linter.yml b/.github/workflows/linter.yml new file mode 100644 index 0000000..654d9cb --- /dev/null +++ b/.github/workflows/linter.yml @@ -0,0 +1,27 @@ +name: Linter tests + +on: + push: + branches: + - "*" + - "!sapling-pr-archive-*" + +jobs: + buildifier: + runs-on: ubuntu-latest + strategy: + fail-fast: false + steps: + - name: Setup Go environment + uses: actions/setup-go@v5.0.0 + + - name: Install buildifier + run: go install github.com/bazelbuild/buildtools/buildifier@latest + + - name: Checkout code + uses: actions/checkout@v2 + + - name: Run buildifier + run: | + buildifier -r -mode check . + find . -name BUCK -exec buildifier -mode check {} \; diff --git a/.github/workflows/linux.yml b/.github/workflows/linux.yml new file mode 100644 index 0000000..76020c8 --- /dev/null +++ b/.github/workflows/linux.yml @@ -0,0 +1,35 @@ +name: Linux tests + +on: + push: + branches: + - "*" + - "!sapling-pr-archive-*" + +jobs: + build_test_ubuntu: + runs-on: ubuntu-latest + strategy: + fail-fast: false + steps: + - name: Install dependencies + run: | + sudo apt-get update && sudo apt-get install -y gcc + + - name: Fetch Buck2 + uses: zadlg/buck2-github-composite-action@v2 + + - name: Checkout code + uses: actions/checkout@v2 + + - name: Fetch Buck2 prelude cell and init Buck2 workspace. + run: | + git clone --depth=1 'https://github.com/facebook/buck2-prelude.git' prelude + + - name: Run full build + run: | + buck2 build ... + + - name: Check that documentation is synced + run: | + buck2 run docs:generate diff --git a/.github/workflows/macos.yml b/.github/workflows/macos.yml new file mode 100644 index 0000000..9946f43 --- /dev/null +++ b/.github/workflows/macos.yml @@ -0,0 +1,32 @@ +name: macOS tests + +on: + push: + branches: + - "*" + - "!sapling-pr-archive-*" + +jobs: + build_test_macos: + runs-on: macos-13 + strategy: + fail-fast: false + steps: + - name: Fetch Buck2 + uses: zadlg/buck2-github-composite-action@v3 + with: + arch: x86_64 + platform: apple-darwin + sha384: 230272fa57843a024759187e5606859755250e612a288122ea91ce54f5e602186dfc8cd06c9e895c7d66954b16c9f92 + output-path: /usr/local/bin/buck2 + + - name: Checkout code + uses: actions/checkout@v2 + + - name: Fetch Buck2 prelude cell and init Buck2 workspace. + run: | + git clone --depth=1 'https://github.com/facebook/buck2-prelude.git' prelude + + - name: Run full build + run: | + /usr/local/bin/buck2 build ... diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b4487f6 --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +buck-out/ +.buckconfig.local +.buckd/ +.buckversion +.fakebuckversion diff --git a/BUCK b/BUCK new file mode 100644 index 0000000..0eebe4c --- /dev/null +++ b/BUCK @@ -0,0 +1,26 @@ +# Copyright 2024 github.com/zadlg +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http:#www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +load("@rules_openssl//openssl:rules.bzl", "openssl_archive", "openssl_build") + +openssl_archive( + name = "openssl-srcs", + version = "3.2.1", + sha256 = "83c7329fe52c850677d75e5d0b0ca245309b97e8ecbcfdc1dfdc4ab9fac35b39", +) + +openssl_build( + name = "openssl", + archive = ":openssl-srcs", +) diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..d645695 --- /dev/null +++ b/LICENSE @@ -0,0 +1,202 @@ + + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright [yyyy] [name of copyright owner] + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. diff --git a/README.md b/README.md new file mode 100644 index 0000000..e224e7f --- /dev/null +++ b/README.md @@ -0,0 +1,116 @@ +# [OpenSSL] toolchain and rules for [Buck2]. + +This repository implements a [Buck2] toolchain for using [OpenSSL]. + +It builds [OpenSSL] from source, using by default archives from the +[OpenSSL GitHub release page]. + +_TL;DR_: See [`openssl_archive`] and [`openssl_build`]. + +## Dependencies + +### `python_bootstrap` and `cxx` toolchains. + +`python_bootstrap` and `cxx` toolcains must be enabled. + +To do so, users may add the following to their `toolchains` cell: + +```starlark +load("@prelude//toolchains:cxx.bzl", "system_cxx_toolchain") +load("@prelude//toolchains:python.bzl", "system_python_bootstrap_toolchain") + +system_python_bootstrap_toolchain( + name = "python_bootstrap", + visibility = ["PUBLIC"], +) + +system_cxx_toolchain( + name = "cxx", + visibility = ["PUBLIC"], +) +``` + +`python_bootstrap` is used by the [`http_archive`] rule. + +`cxx` is used to compile [OpenSSL] from source. + +### GNU Make + +[GNU Make] is also needed, since the [OpenSSL] build stage is using Make. + +## Installation + +### Adding the cell + +In order to start using the rules and the toolchain implemented by this repository, +users first need to clone the repo under their [Buck2] project. + +Then, a new cell call `rules_openssl` must be declared in their `.buckconfig` file: + +``` +# .buckconfig +[repositories] +rules_openssl = rules_openssl/ +``` + +If one wishes to place this repository under another directory than root, the +following can be done: + +``` +# .buckconfig +[repositories] +rules_openssl = path/to/this/repo/cloned +``` + +## Building OpenSSL + +Two rules have to be used together in order to build [OpenSSL]. + +### [`openssl_archive`] + +First, users must declare a new [OpenSSL] archive. Usually, this archive +comes from the [OpenSSl GitHub release page]: + +```starlark +# BUCK +load("@rules_openssl//openssl:rules.bzl", "openssl_archive") + +openssl_archive( + name = "openssl-srcs", + version = "3.2.1", + sha256 = "83c7329fe52c850677d75e5d0b0ca245309b97e8ecbcfdc1dfdc4ab9fac35b39", +) +``` + +In the example above, [`openssl_archive`] determines the URL to the archive +using the `version` argument: + +```starlark + return "https://github.com/openssl/openssl/releases/download/openssl-{version}/openssl-{version}.tar.gz".format(version = version) +``` + +### [`openssl_build`] + +Then, [OpenSSL] can be built from the output of an [`openssl_archive`]: + +```starlark +# BUCK +load("@rules_openssl//openssl:rules.bzl", "openssl_build") + +openssl_build( + name = "openssl", + archive = ":openssl-srcs", +) +``` + +## License + +See [License](LICENSE). + +[OpenSSL]: https://openssl.org/ +[Buck2]: https://buck2.build/ +[OpenSSL GitHub release page]: https://github.com/openssl/openssl/releases +[`http_archive`]: https://buck2.build/docs/api/rules/#http_archive +[GNU Make]: https://www.gnu.org/software/make/ +[`openssl_archive`]: docs/root/openssl/rules.bzl.md#openssl_archive +[`openssl_build`]: docs/root/openssl/rules.bzl.md#openssl_build diff --git a/docs/BUCK b/docs/BUCK new file mode 100644 index 0000000..e01d376 --- /dev/null +++ b/docs/BUCK @@ -0,0 +1,4 @@ +sh_binary( + name = "generate", + main = "generate.sh", +) diff --git a/docs/generate.sh b/docs/generate.sh new file mode 100755 index 0000000..7ba077e --- /dev/null +++ b/docs/generate.sh @@ -0,0 +1,27 @@ +#!/bin/sh + +set -e + +buck2 docs starlark \ + --format markdown_files \ + --markdown-files-starlark-subdir "" \ + --markdown-files-destination-dir "${PWD}/docs/" \ + -- @rules_openssl//openssl:rules.bzl + +if ! [ "$1" = "generate" ]; then + if [ -d ".git" ]; then + if ! git diff --exit-code; then + echo "Documentation is out of sync." + exit 1 + fi + elif [ -d ".sl" ]; then + if [ -n "$(sl diff)" ]; then + echo "Documentation is out of sync." + exit 1 + fi + else + echo "Unknown vcs." + exit 1 + fi +fi +exit 0 diff --git a/docs/root/openssl/rules.bzl.md b/docs/root/openssl/rules.bzl.md new file mode 100644 index 0000000..aed27e5 --- /dev/null +++ b/docs/root/openssl/rules.bzl.md @@ -0,0 +1,59 @@ +## native + +```python +native: struct(..) +``` + +--- +## openssl\_archive + +```python +def openssl_archive( + name: str, + version: str, + configure_file: str = "Configure", + *args, + **kwargs +) +``` + +Defines an OpenSSL source code archive. + +name: + Target name. + version: + OpenSSL version. + +--- +## openssl\_build + +```python +def openssl_build( + *, + name: str, + default_target_platform: None | str = _, + target_compatible_with: list[str] = _, + compatible_with: list[str] = _, + exec_compatible_with: list[str] = _, + visibility: list[str] = _, + within_view: list[str] = _, + metadata: opaque_metadata = _, + tests: list[str] = _, + archive: str, + configure_flags: list[str] = _ +) -> None +``` + +#### Parameters + +* `name`: name of the target +* `default_target_platform`: specifies the default target platform, used when no platforms are specified on the command line +* `target_compatible_with`: a list of constraints that are required to be satisfied for this target to be compatible with a configuration +* `compatible_with`: a list of constraints that are required to be satisfied for this target to be compatible with a configuration +* `exec_compatible_with`: a list of constraints that are required to be satisfied for this target to be compatible with an execution platform +* `visibility`: a list of visibility patterns restricting what targets can depend on this one +* `within_view`: a list of visibility patterns restricting what this target can depend on +* `metadata`: a key-value map of metadata associated with this target +* `tests`: a list of targets that provide tests for this one +* `archive`: Archive returned by the `openssl_archive` macro. +* `configure_flags`: Configure flags. diff --git a/openssl/BUCK b/openssl/BUCK new file mode 100644 index 0000000..e69de29 diff --git a/openssl/providers/archive.bzl b/openssl/providers/archive.bzl new file mode 100644 index 0000000..a27b065 --- /dev/null +++ b/openssl/providers/archive.bzl @@ -0,0 +1,29 @@ +# Copyright 2024 github.com/zadlg +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http:#www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +ArchiveInfo = provider( + doc = """ + An archive containing the source code of OpenSSL. + """, + fields = { + # The actual archive. + "ar": provider_field(Artifact), + + # The configure file. + "configure_file": provider_field(Artifact), + + # OpenSSL version. + "version": provider_field(str), + }, +) diff --git a/openssl/providers/build.bzl b/openssl/providers/build.bzl new file mode 100644 index 0000000..39bb03b --- /dev/null +++ b/openssl/providers/build.bzl @@ -0,0 +1,48 @@ +# Copyright 2024 github.com/zadlg +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http:#www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +load("@rules_openssl//openssl/providers:archive.bzl", "ArchiveInfo") + +ConfigureInfo = provider( + doc = """ + Info about the configuration stage.""", + fields = { + # Configure flags. List of `resolved_macro`. + "flags": provider_field(list), + + # Configure and build directory. + "dir": provider_field(Artifact), + + # Configure command line. + "cmd_args": provider_field(cmd_args), + }, +) + +BuildInfo = provider( + doc = """ + Info about the build.""", + fields = { + # The archive configured. + "archive_info": provider_field(ArchiveInfo), + + # The configuration stage. + "configure_info": provider_field(ConfigureInfo), + + # Install directory. + "install_dir": provider_field(Artifact), + + # Build and install command line. + "cmd_args": provider_field(cmd_args), + }, +) diff --git a/openssl/rules.bzl b/openssl/rules.bzl new file mode 100644 index 0000000..fc7ae6e --- /dev/null +++ b/openssl/rules.bzl @@ -0,0 +1,21 @@ +# Copyright 2024 github.com/zadlg +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http:#www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +load("@rules_openssl//openssl/rules:archive.bzl", _openssl_archive = "openssl_archive") +load("@rules_openssl//openssl/rules:build.bzl", _openssl_build = "openssl_build") + +load_symbols({ + "openssl_archive": _openssl_archive, + "openssl_build": _openssl_build, +}) diff --git a/openssl/rules/archive.bzl b/openssl/rules/archive.bzl new file mode 100644 index 0000000..20e6ae5 --- /dev/null +++ b/openssl/rules/archive.bzl @@ -0,0 +1,171 @@ +# Copyright 2024 github.com/zadlg +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http:#www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +load("@rules_openssl//openssl/providers:archive.bzl", "ArchiveInfo") + +def _archive_impl(ctx: AnalysisContext) -> list[Provider]: + """Implementation of rule `archive`. + + Args: + ctx: + Analysis context. + + Returns: + A list of providers. + """ + return [ + DefaultInfo(), + ArchiveInfo( + ar = ctx.attrs.http_archive, + configure_file = ctx.attrs.configure_file, + version = ctx.attrs.version, + ), + ] + +archive = rule( + impl = _archive_impl, + attrs = { + "http_archive": attrs.source(doc = """ + Archive returned by `http_archive` rule. + """), + "configure_file": attrs.source(doc = """ + Configure file. + """), + "version": attrs.string(doc = """ + OpenSSL version. + """), + }, +) + +def _build_url(kwargs: dict, version: str) -> str: + """Builds the URL. + + If `url` or `urls` is specified in `kwargs`, it will be picked. Otherwise, + the `url` will be built using the `version` argument. + + Args: + kwargs: + Named arguments. + version: + OpenSSL version. + + Returns: + The URL to the source code archive. + """ + url = kwargs.get("url") + if url != None: + kwargs.pop("url") + return url + urls = kwargs.get("urls") + if urls != None: + if type(urls) != list: + fail("`urls` is not a list.") + if len(urls) != 1: + fail("`urls` must contain exactly one value") + kwargs.pop("urls") + return urls[0] + return "https://github.com/openssl/openssl/releases/download/openssl-{version}/openssl-{version}.tar.gz".format(version = version) + +def _strip_prefix(kwargs: dict, version: str) -> str: + """Builds the `strip_prefix` argument for `http_archive` rule. + + If `strip_prefix` is specified in `kwargs`, it will be picked. Otherwise, + it will be built using the `version` argument. + + Args: + kwargs: + Named arguments. + version: + OpenSSL version. + + Returns: + The value of the `strip_prefix` argument to `http_archive` rule. + """ + strip_prefix = kwargs.get("strip_prefix") + if strip_prefix != None: + kwargs.pop("strip_prefix") + return strip_prefix + return "openssl-{version}".format(version = version) + +def _build_sub_targets(kwargs: dict, configure_file: str) -> list[str]: + """Builds the `sub_targets` argument for `http_archive` rule. + + Args: + kwargs: + Named arguments. + configure_file: + Name of the configure script file. + + Returns: + The list of subtargets for `http_archive` rule. + """ + sub_targets = kwargs.get("sub_targets") + if sub_targets != None: + if type(sub_targets) != list: + fail("`sub_targets` must be a list of strings.") + kwargs.pop("sub_targets") + else: + sub_targets = [] + sub_targets.append(configure_file) + return sub_targets + +def openssl_archive( + name: str, + version: str, + configure_file: str = "Configure", + *kargs, + **kwargs): + """Defines an OpenSSL source code archive. + + Args: + name: + Target name. + version: + OpenSSL version. + """ + ar_name = "{name}.ar".format( + name = name, + ) + url = _build_url( + kwargs = kwargs, + version = version, + ) + strip_prefix = _strip_prefix( + kwargs = kwargs, + version = version, + ) + sub_targets = _build_sub_targets( + kwargs = kwargs, + configure_file = configure_file, + ) + native.http_archive( + name = ar_name, + urls = [url], + strip_prefix = strip_prefix, + sub_targets = sub_targets, + *kargs, + **kwargs + ) + archive( + name = name, + http_archive = ":{ar_name}".format( + ar_name = ar_name, + ), + configure_file = ":{ar_name}[{configure_file}]".format( + ar_name = ar_name, + configure_file = configure_file, + ), + version = version, + visibility = kwargs.get("visibility"), + ) diff --git a/openssl/rules/build.bzl b/openssl/rules/build.bzl new file mode 100644 index 0000000..e0d9c0d --- /dev/null +++ b/openssl/rules/build.bzl @@ -0,0 +1,175 @@ +# Copyright 2024 github.com/zadlg +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http:#www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +load("@rules_openssl//openssl/providers:archive.bzl", "ArchiveInfo") +load("@rules_openssl//openssl/providers:build.bzl", "BuildInfo", "ConfigureInfo") + +def _write_configure_script(ctx: AnalysisContext) -> Artifact: + """Writes the script for configuring OpenSSL. + + Args: + ctx: + Analysis context. + + Returns: + The Artifact corresponding to the configure script. + """ + return ctx.actions.write( + "__configure__.sh", + """#!/usr/bin/env sh +set -e +_configure_dir="$1" +mkdir -p "${_configure_dir}" +shift 1 +_configure_file="$1" +shift 1 +_install_dir="$(realpath "$1")" +shift 1 +_configure_args="$@" +cd "${_configure_dir}" +set -x +"${_configure_file}" \ + --prefix="${_install_dir}" \ + --openssldir="${_install_dir}" ${_configure_args} +""", + is_executable = True, + ) + +def _write_build_script(ctx: AnalysisContext) -> Artifact: + """Writes the script for building OpenSSL. + + Args: + ctx: + Analysis context. + + Returns: + The Artifact corresponding to the build script. + """ + return ctx.actions.write( + "__build_install__.sh", + """#!/usr/bin/env sh +set -e +_configure_dir="$1" +shift 1 +_install_dir="$1" +shift 1 +mkdir -p "${_install_dir}" +cd "${_configure_dir}" +make install_sw +""", + is_executable = True, + ) + +def _configure_impl(ctx: AnalysisContext) -> list[Provider]: + """Implementation of rule `configure`. + + Args: + ctx: + Analysis context. + + Returns: + List of providers. + """ + + archive_info = ctx.attrs.archive[ArchiveInfo] + + configure_dir = ctx.actions.declare_output( + "configure", + dir = True, + ) + + install_dir = ctx.actions.declare_output( + "install", + dir = True, + ) + configure_script = _write_configure_script( + ctx = ctx, + ) + configure_args = cmd_args([ + configure_dir.as_output(), + cmd_args(archive_info.configure_file).relative_to(configure_dir), + cmd_args(install_dir).ignore_artifacts(), + ] + ctx.attrs.configure_flags) + configure_args.hidden(archive_info.ar) + + ctx.actions.run( + configure_args, + category = "openssl_configure", + exe = RunInfo(cmd_args(configure_script)), + ) + + build_script = _write_build_script( + ctx = ctx, + ) + + build_args = cmd_args([ + configure_dir, + install_dir.as_output(), + cmd_args(archive_info.ar).hidden(), + ]) + + ctx.actions.run( + build_args, + category = "openssl_build", + exe = RunInfo(cmd_args(build_script)), + ) + + return [ + DefaultInfo(default_outputs = [configure_dir, install_dir]), + BuildInfo( + archive_info = archive_info, + configure_info = ConfigureInfo( + flags = ctx.attrs.configure_flags, + dir = configure_dir, + cmd_args = configure_args, + ), + install_dir = install_dir, + cmd_args = build_args, + ), + ] + +openssl_build = rule( + impl = _configure_impl, + attrs = { + "archive": attrs.dep(doc = """ + Archive returned by the `openssl_archive` macro. + """), + "configure_flags": attrs.list( + attrs.arg(), + doc = """ + Configure flags. + """, + default = [ + "no-async", + "no-autoerrinit", + "no-cmp", + "no-cms", + "no-comp", + "no-ct", + "no-deprecated", + "no-dgram", + "no-dynamic-engine", + "no-filenames", + "no-legacy", + "no-module", + "no-shared", + "no-sock", + "no-ssl-trace", + "no-ssl", + "no-dtls", + "no-md4", + ], + ), + }, +) diff --git a/toolchains/BUCK b/toolchains/BUCK new file mode 100644 index 0000000..d6fa6ac --- /dev/null +++ b/toolchains/BUCK @@ -0,0 +1,26 @@ +# Copyright 2024 github.com/zadlg +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http:#www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +load("@prelude//toolchains:cxx.bzl", "system_cxx_toolchain") +load("@prelude//toolchains:python.bzl", "system_python_bootstrap_toolchain") + +system_python_bootstrap_toolchain( + name = "python_bootstrap", + visibility = ["PUBLIC"], +) + +system_cxx_toolchain( + name = "cxx", + visibility = ["PUBLIC"], +)