blob: c24c3f27e5f108266170428d0e27fd21612e2788 [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
17load(
18 "//build/utils:detect_root.bzl",
19 "detect_root",
20)
21
22"""
23Build a sysroot-style tarball containing musl/linux headers and libraries.
24
Serge Bazanski662b5b32020-12-21 13:49:00 +010025This can then be used to build a C toolchain that builds C/C++ binaries for Metropolis nodes.
Serge Bazanski9e861a82020-09-16 13:46:41 +020026"""
27
28def _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
Leopold7fbf1042023-01-06 19:57:37 +010037 compiler_headers_path = "lib/gcc/x86_64-redhat-linux/12/include"
Lorenz Brunbb95ebd2021-03-16 15:06:51 +010038
Serge Bazanski9e861a82020-09-16 13:46:41 +020039 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 Brunbb95ebd2021-03-16 15:06:51 +010051 # 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 Bazanski9e861a82020-09-16 13:46:41 +020055
Lorenz Brunbb95ebd2021-03-16 15:06:51 +010056 arguments += [musl_headers_path]
Serge Bazanski9e861a82020-09-16 13:46:41 +020057 command += " --transform 's|^'$3'|include|' $3"
58
Lorenz Brunbb95ebd2021-03-16 15:06:51 +010059 arguments += [linux_headers_path]
60 command += " --transform 's|^'$4'|include|' $4"
61
Serge Bazanski9e861a82020-09-16 13:46:41 +020062 arguments += [musl_root]
Lorenz Brunbb95ebd2021-03-16 15:06:51 +010063 command += " --transform 's|^'$5'|lib|' $5"
Serge Bazanski9e861a82020-09-16 13:46:41 +020064
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 Brunbb95ebd2021-03-16 15:06:51 +010074 return [DefaultInfo(files = depset([tarball]))]
Serge Bazanski9e861a82020-09-16 13:46:41 +020075
76musl_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)