| Serge Bazanski | 9e861a8 | 2020-09-16 13:46:41 +0200 | [diff] [blame] | 1 | # Copyright 2020 The Monogon Project Authors. |
| 2 | # |
| 3 | # SPDX-License-Identifier: Apache-2.0 |
| 4 | # |
| 5 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | # you may not use this file except in compliance with the License. |
| 7 | # You may obtain a copy of the License at |
| 8 | # |
| 9 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | # |
| 11 | # Unless required by applicable law or agreed to in writing, software |
| 12 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | # See the License for the specific language governing permissions and |
| 15 | # limitations under the License. |
| 16 | |
| Serge Bazanski | 9e861a8 | 2020-09-16 13:46:41 +0200 | [diff] [blame] | 17 | """ |
| 18 | Build a sysroot-style tarball containing musl/linux headers and libraries. |
| 19 | |
| Serge Bazanski | 662b5b3 | 2020-12-21 13:49:00 +0100 | [diff] [blame] | 20 | This can then be used to build a C toolchain that builds C/C++ binaries for Metropolis nodes. |
| Serge Bazanski | 9e861a8 | 2020-09-16 13:46:41 +0200 | [diff] [blame] | 21 | """ |
| 22 | |
| Tim Windelschmidt | 156248b | 2025-01-10 00:27:45 +0100 | [diff] [blame] | 23 | load( |
| 24 | "//build/utils:detect_root.bzl", |
| 25 | "detect_root", |
| 26 | ) |
| 27 | |
| Serge Bazanski | 9e861a8 | 2020-09-16 13:46:41 +0200 | [diff] [blame] | 28 | def _musl_gcc_tarball(ctx): |
| 29 | tarball_name = ctx.attr.name + ".tar.xz" |
| 30 | tarball = ctx.actions.declare_file(tarball_name) |
| 31 | |
| 32 | musl_headers = ctx.file.musl_headers |
| 33 | musl_headers_path = musl_headers.path |
| 34 | linux_headers = ctx.file.linux_headers |
| 35 | linux_headers_path = linux_headers.path |
| 36 | |
| Tim Windelschmidt | f69d84b | 2024-07-03 20:32:19 +0200 | [diff] [blame] | 37 | compiler_headers_path = "lib/gcc/x86_64-redhat-linux/14/include" |
| Lorenz Brun | bb95ebd | 2021-03-16 15:06:51 +0100 | [diff] [blame] | 38 | |
| Serge Bazanski | 9e861a8 | 2020-09-16 13:46:41 +0200 | [diff] [blame] | 39 | musl_root = detect_root(ctx.attr.musl) |
| Serge Bazanski | 9e861a8 | 2020-09-16 13:46:41 +0200 | [diff] [blame] | 40 | |
| 41 | # This builds a tarball containing musl, musl headers and linux headers. |
| 42 | # This is done by some carefully crafted tar command line arguments that rewrite |
| 43 | # paths to ensure that everything lands in lib/ and include/ in the tarball. |
| 44 | |
| 45 | # TODO(q3k): write nice, small static Go utility for this. |
| 46 | |
| 47 | arguments = [tarball.path] |
| 48 | command = "tar -chJf $1" |
| 49 | |
| Lorenz Brun | bb95ebd | 2021-03-16 15:06:51 +0100 | [diff] [blame] | 50 | # Order is important here as this is a terrible hack producing a tar file with duplicate files. The decompressor |
| 51 | # will then overwrite the wrong one with the correct one for us. |
| Tim Windelschmidt | 156248b | 2025-01-10 00:27:45 +0100 | [diff] [blame] | 52 | arguments.append(compiler_headers_path) |
| Lorenz Brun | bb95ebd | 2021-03-16 15:06:51 +0100 | [diff] [blame] | 53 | command += " --transform 's|^'$2'|include|' /$2" |
| Serge Bazanski | 9e861a8 | 2020-09-16 13:46:41 +0200 | [diff] [blame] | 54 | |
| Tim Windelschmidt | 156248b | 2025-01-10 00:27:45 +0100 | [diff] [blame] | 55 | arguments.append(musl_headers_path) |
| Serge Bazanski | 9e861a8 | 2020-09-16 13:46:41 +0200 | [diff] [blame] | 56 | command += " --transform 's|^'$3'|include|' $3" |
| 57 | |
| Tim Windelschmidt | 156248b | 2025-01-10 00:27:45 +0100 | [diff] [blame] | 58 | arguments.append(linux_headers_path) |
| Lorenz Brun | bb95ebd | 2021-03-16 15:06:51 +0100 | [diff] [blame] | 59 | command += " --transform 's|^'$4'|include|' $4" |
| 60 | |
| Tim Windelschmidt | 156248b | 2025-01-10 00:27:45 +0100 | [diff] [blame] | 61 | arguments.append(musl_root) |
| Lorenz Brun | bb95ebd | 2021-03-16 15:06:51 +0100 | [diff] [blame] | 62 | command += " --transform 's|^'$5'|lib|' $5" |
| Serge Bazanski | 9e861a8 | 2020-09-16 13:46:41 +0200 | [diff] [blame] | 63 | |
| 64 | ctx.actions.run_shell( |
| 65 | inputs = [musl_headers, linux_headers] + ctx.files.musl, |
| 66 | outputs = [tarball], |
| 67 | progress_message = "Building toolchain tarball", |
| 68 | mnemonic = "BuildToolchainTarball", |
| 69 | arguments = arguments, |
| 70 | use_default_shell_env = True, |
| 71 | command = command, |
| 72 | ) |
| Lorenz Brun | bb95ebd | 2021-03-16 15:06:51 +0100 | [diff] [blame] | 73 | return [DefaultInfo(files = depset([tarball]))] |
| Serge Bazanski | 9e861a8 | 2020-09-16 13:46:41 +0200 | [diff] [blame] | 74 | |
| 75 | musl_gcc_tarball = rule( |
| 76 | implementation = _musl_gcc_tarball, |
| 77 | attrs = { |
| 78 | "musl": attr.label(mandatory = True), |
| 79 | "musl_headers": attr.label(mandatory = True, allow_single_file = True), |
| 80 | "linux_headers": attr.label(mandatory = True, allow_single_file = True), |
| 81 | }, |
| 82 | ) |