blob: 4f12049412d3a191dea2acf19a665b5f8fcdfce1 [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
25This can then be used to build a C toolchain that builds for Smalltown.
26"""
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
37 musl_root = detect_root(ctx.attr.musl)
38 musl_files = ctx.files.musl
39
40 # This builds a tarball containing musl, musl headers and linux headers.
41 # This is done by some carefully crafted tar command line arguments that rewrite
42 # paths to ensure that everything lands in lib/ and include/ in the tarball.
43
44 # TODO(q3k): write nice, small static Go utility for this.
45
46 arguments = [tarball.path]
47 command = "tar -chJf $1"
48
49 arguments += [musl_headers_path]
50 command += " --transform 's|^'$2'|include|' $2"
51
52 arguments += [linux_headers_path]
53 command += " --transform 's|^'$3'|include|' $3"
54
55 arguments += [musl_root]
56 command += " --transform 's|^'$4'|lib|' $4"
57
58 ctx.actions.run_shell(
59 inputs = [musl_headers, linux_headers] + ctx.files.musl,
60 outputs = [tarball],
61 progress_message = "Building toolchain tarball",
62 mnemonic = "BuildToolchainTarball",
63 arguments = arguments,
64 use_default_shell_env = True,
65 command = command,
66 )
67 return [DefaultInfo(files=depset([tarball]))]
68
69musl_gcc_tarball = rule(
70 implementation = _musl_gcc_tarball,
71 attrs = {
72 "musl": attr.label(mandatory = True),
73 "musl_headers": attr.label(mandatory = True, allow_single_file = True),
74 "linux_headers": attr.label(mandatory = True, allow_single_file = True),
75 },
76)