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 | |
| 17 | load( |
| 18 | "//build/utils:detect_root.bzl", |
| 19 | "detect_root", |
| 20 | ) |
| 21 | |
| 22 | """ |
| 23 | Build a sysroot-style tarball containing musl/linux headers and libraries. |
| 24 | |
Serge Bazanski | 662b5b3 | 2020-12-21 13:49:00 +0100 | [diff] [blame] | 25 | 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] | 26 | """ |
| 27 | |
| 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 | |
Lorenz Brun | 942f5e2 | 2022-01-27 15:03:10 +0100 | [diff] [blame] | 37 | compiler_headers_path = "lib/gcc/x86_64-redhat-linux/11/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) |
| 40 | musl_files = ctx.files.musl |
| 41 | |
| 42 | # This builds a tarball containing musl, musl headers and linux headers. |
| 43 | # This is done by some carefully crafted tar command line arguments that rewrite |
| 44 | # paths to ensure that everything lands in lib/ and include/ in the tarball. |
| 45 | |
| 46 | # TODO(q3k): write nice, small static Go utility for this. |
| 47 | |
| 48 | arguments = [tarball.path] |
| 49 | command = "tar -chJf $1" |
| 50 | |
Lorenz Brun | bb95ebd | 2021-03-16 15:06:51 +0100 | [diff] [blame] | 51 | # Order is important here as this is a terrible hack producing a tar file with duplicate files. The decompressor |
| 52 | # will then overwrite the wrong one with the correct one for us. |
| 53 | arguments += [compiler_headers_path] |
| 54 | command += " --transform 's|^'$2'|include|' /$2" |
Serge Bazanski | 9e861a8 | 2020-09-16 13:46:41 +0200 | [diff] [blame] | 55 | |
Lorenz Brun | bb95ebd | 2021-03-16 15:06:51 +0100 | [diff] [blame] | 56 | arguments += [musl_headers_path] |
Serge Bazanski | 9e861a8 | 2020-09-16 13:46:41 +0200 | [diff] [blame] | 57 | command += " --transform 's|^'$3'|include|' $3" |
| 58 | |
Lorenz Brun | bb95ebd | 2021-03-16 15:06:51 +0100 | [diff] [blame] | 59 | arguments += [linux_headers_path] |
| 60 | command += " --transform 's|^'$4'|include|' $4" |
| 61 | |
Serge Bazanski | 9e861a8 | 2020-09-16 13:46:41 +0200 | [diff] [blame] | 62 | arguments += [musl_root] |
Lorenz Brun | bb95ebd | 2021-03-16 15:06:51 +0100 | [diff] [blame] | 63 | command += " --transform 's|^'$5'|lib|' $5" |
Serge Bazanski | 9e861a8 | 2020-09-16 13:46:41 +0200 | [diff] [blame] | 64 | |
| 65 | ctx.actions.run_shell( |
| 66 | inputs = [musl_headers, linux_headers] + ctx.files.musl, |
| 67 | outputs = [tarball], |
| 68 | progress_message = "Building toolchain tarball", |
| 69 | mnemonic = "BuildToolchainTarball", |
| 70 | arguments = arguments, |
| 71 | use_default_shell_env = True, |
| 72 | command = command, |
| 73 | ) |
Lorenz Brun | bb95ebd | 2021-03-16 15:06:51 +0100 | [diff] [blame] | 74 | return [DefaultInfo(files = depset([tarball]))] |
Serge Bazanski | 9e861a8 | 2020-09-16 13:46:41 +0200 | [diff] [blame] | 75 | |
| 76 | musl_gcc_tarball = rule( |
| 77 | implementation = _musl_gcc_tarball, |
| 78 | attrs = { |
| 79 | "musl": attr.label(mandatory = True), |
| 80 | "musl_headers": attr.label(mandatory = True, allow_single_file = True), |
| 81 | "linux_headers": attr.label(mandatory = True, allow_single_file = True), |
| 82 | }, |
| 83 | ) |