blob: a7e407de09b4dbad341e8fbb67fcc89a966db52c [file] [log] [blame]
Serge Bazanski9e861a82020-09-16 13:46:41 +02001# 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 Bazanski9e861a82020-09-16 13:46:41 +020017"""
18Build a sysroot-style tarball containing musl/linux headers and libraries.
19
Serge Bazanski662b5b32020-12-21 13:49:00 +010020This can then be used to build a C toolchain that builds C/C++ binaries for Metropolis nodes.
Serge Bazanski9e861a82020-09-16 13:46:41 +020021"""
22
Tim Windelschmidt156248b2025-01-10 00:27:45 +010023load(
24 "//build/utils:detect_root.bzl",
25 "detect_root",
26)
27
Serge Bazanski9e861a82020-09-16 13:46:41 +020028def _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 Windelschmidtf69d84b2024-07-03 20:32:19 +020037 compiler_headers_path = "lib/gcc/x86_64-redhat-linux/14/include"
Lorenz Brunbb95ebd2021-03-16 15:06:51 +010038
Serge Bazanski9e861a82020-09-16 13:46:41 +020039 musl_root = detect_root(ctx.attr.musl)
Serge Bazanski9e861a82020-09-16 13:46:41 +020040
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 Brunbb95ebd2021-03-16 15:06:51 +010050 # 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 Windelschmidt156248b2025-01-10 00:27:45 +010052 arguments.append(compiler_headers_path)
Lorenz Brunbb95ebd2021-03-16 15:06:51 +010053 command += " --transform 's|^'$2'|include|' /$2"
Serge Bazanski9e861a82020-09-16 13:46:41 +020054
Tim Windelschmidt156248b2025-01-10 00:27:45 +010055 arguments.append(musl_headers_path)
Serge Bazanski9e861a82020-09-16 13:46:41 +020056 command += " --transform 's|^'$3'|include|' $3"
57
Tim Windelschmidt156248b2025-01-10 00:27:45 +010058 arguments.append(linux_headers_path)
Lorenz Brunbb95ebd2021-03-16 15:06:51 +010059 command += " --transform 's|^'$4'|include|' $4"
60
Tim Windelschmidt156248b2025-01-10 00:27:45 +010061 arguments.append(musl_root)
Lorenz Brunbb95ebd2021-03-16 15:06:51 +010062 command += " --transform 's|^'$5'|lib|' $5"
Serge Bazanski9e861a82020-09-16 13:46:41 +020063
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 Brunbb95ebd2021-03-16 15:06:51 +010073 return [DefaultInfo(files = depset([tarball]))]
Serge Bazanski9e861a82020-09-16 13:46:41 +020074
75musl_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)