//build/toolchain/musl-host-gcc: implement

This is a cc_toolchain which runs on x86 systems with Linux/gcc and
targets Smalltown via static musl builds.

It is currently unused, but can be tested by trying to build any
cc_binary with
--crosstool_top=//build/toolchain/musl-host-gcc:musl_host_cc_suite .

Test Plan: This has been tested manually by running it against a simple cc_binary. Another revision on top of this will attempt to build mkfs.xfs with it.

X-Origin-Diff: phab/D623
GitOrigin-RevId: ebdf51ee76d9d5a7fd94725c66ef53783f787df7
diff --git a/WORKSPACE b/WORKSPACE
index 56f5bba..9ba620d 100644
--- a/WORKSPACE
+++ b/WORKSPACE
@@ -214,3 +214,7 @@
     strip_prefix = "qboot-a5300c4949b8d4de2d34bedfaed66793f48ec948",
     urls = ["https://github.com/bonzini/qboot/archive/a5300c4949b8d4de2d34bedfaed66793f48ec948.tar.gz"],
 )
+
+# Load musl toolchain Smalltown sysroot tarball into external repository.
+load("//build/toolchain/musl-host-gcc:sysroot.bzl", "musl_sysroot_repositories")
+musl_sysroot_repositories()
diff --git a/build/toolchain/BUILD b/build/toolchain/BUILD
index 5bf53d2..78c4ae6 100644
--- a/build/toolchain/BUILD
+++ b/build/toolchain/BUILD
@@ -2,7 +2,17 @@
 
 # Toolchain definitions.
 #
-# We currently define a single custom toolchain: the host_cc toolchain suite.
+# We currently define two toolchains:
+#
+#  - //build/toolchain:host_cc_suite , which is a fully unhermetic host toolchain,
+#    that can be used to build tools for the host.
+#  - //build/toolchain/musl-host-gcc:musl_host_cc_suite , which combines the host's
+#    gcc compiler with a sysroot tarball that targets Smalltown. This can be used to
+#    build C libraries/tools for Smalltown.
+#
+
+# This file defines //build/toolchain:host_cc_suite.
+#
 # This is a C++ toolchain that uses GCC from the host at hardcoded paths. We
 # can get away with this, as currently the entire build is performed in a known
 # container (see: //scripts:create_container.sh). We define this toolchain so
diff --git a/build/toolchain/cc_toolchain_config.bzl b/build/toolchain/cc_toolchain_config.bzl
index b69e06f..11c7736 100644
--- a/build/toolchain/cc_toolchain_config.bzl
+++ b/build/toolchain/cc_toolchain_config.bzl
@@ -14,17 +14,16 @@
 #  See the License for the specific language governing permissions and
 #  limitations under the License.
 
-load("@bazel_tools//tools/cpp:cc_toolchain_config_lib.bzl", "tool_path")
+load("@bazel_tools//tools/cpp:cc_toolchain_config_lib.bzl", "tool", "tool_path")
 
-# This defines a minimal, non-parametrized toolchain configuration rule that
-# uses the host GCC. For background on why we do this, see
-# //build/toolchain/BUILD.
+# This defines a minimal, barely parametrized toolchain configuration rule that
+# uses the host GCC with some possible overrides.
 
 def _host_cc_toolchain_impl(ctx):
     tool_paths = [
         tool_path(
             name = "gcc",
-            path = "/usr/bin/gcc",
+            path = ctx.attr.gcc,
         ),
         tool_path(
             name = "ld",
@@ -57,10 +56,7 @@
     ]
     return cc_common.create_cc_toolchain_config_info(
         ctx = ctx,
-        cxx_builtin_include_directories = [
-          "/usr/lib/gcc/x86_64-redhat-linux/10/include/",
-          "/usr/include",
-        ],
+        cxx_builtin_include_directories = ctx.attr.host_includes,
         toolchain_identifier = "k8-toolchain",
         host_system_name = "local",
         target_system_name = "local",
@@ -70,10 +66,24 @@
         abi_version = "unknown",
         abi_libc_version = "unknown",
         tool_paths = tool_paths,
+        builtin_sysroot = ctx.attr.sysroot,
     )
 
 host_cc_toolchain_config = rule(
     implementation = _host_cc_toolchain_impl,
-    attrs = {},
+    attrs = {
+        "gcc": attr.string(
+            default = "/usr/bin/gcc",
+        ),
+        "host_includes": attr.string_list(
+            default = [
+                "/usr/lib/gcc/x86_64-redhat-linux/10/include/",
+                "/usr/include",
+            ],
+        ),
+        "sysroot": attr.string(
+            default = "",
+        ),
+    },
     provides = [CcToolchainConfigInfo],
 )
diff --git a/build/toolchain/musl-host-gcc/BUILD b/build/toolchain/musl-host-gcc/BUILD
new file mode 100644
index 0000000..95a59f6
--- /dev/null
+++ b/build/toolchain/musl-host-gcc/BUILD
@@ -0,0 +1,45 @@
+load("//build/toolchain:cc_toolchain_config.bzl", "host_cc_toolchain_config")
+
+# This file defines //build/toolchain/musl-host-gcc:musl_host_cc_suite.
+#
+# This is a C++ toolchain that uses GCC from the host at hardcoded paths, with
+# a pre-built sysroot tarball that targets Smalltown with musl and Linux headers.
+# It's a superset of //build/toolchain:host_cc_suite.
+# For more information, see README.md.
+
+cc_toolchain_suite(
+    name = "musl_host_cc_suite",
+    toolchains = {
+        "k8": ":musl_host_cc_k8_toolchain",
+    },
+    visibility = ["//visibility:public"],
+)
+
+cc_toolchain(
+    name = "musl_host_cc_k8_toolchain",
+    all_files = ":musl_toolchain_files",
+    compiler_files = ":musl_toolchain_files",
+    dwp_files = ":musl_toolchain_files",
+    linker_files = ":musl_toolchain_files",
+    objcopy_files = ":musl_toolchain_files",
+    strip_files = ":musl_toolchain_files",
+    supports_param_files = 0,
+    toolchain_config = ":musl_host_cc_k8_toolchain_config",
+    toolchain_identifier = "host-musl-k8-toolchain",
+)
+
+host_cc_toolchain_config(
+    name = "musl_host_cc_k8_toolchain_config",
+    gcc = "gcc-wrapper.sh",
+    host_includes = [],
+    sysroot = "external/musl_sysroot",
+)
+
+filegroup(
+    name = "musl_toolchain_files",
+    srcs = [
+        ":gcc-wrapper.sh",
+        ":musl.spec",
+        "@musl_sysroot//:all",
+    ],
+)
diff --git a/build/toolchain/musl-host-gcc/README.md b/build/toolchain/musl-host-gcc/README.md
new file mode 100644
index 0000000..585bac2
--- /dev/null
+++ b/build/toolchain/musl-host-gcc/README.md
@@ -0,0 +1,42 @@
+musl-host-gcc
+=============
+
+musl-host-gcc is a Bazel C++ toolchain that uses the machine's host gcc in combination with a pre-built musl, musl headers, and Linux headers.
+
+It is currently used to build the few C binaries we need in Smalltown' runtime.
+
+At some point, this toolchain should be replaced by a fully hermetic toolchain that doesn't depend on the host environment.
+
+Usage
+-----
+
+To use this toolchain explicitely while building a `cc_binary`, do:
+
+    bazel build --crosstool_top=//build/toolchain/musl-host-gcc:musl_host_cc_suite //foo/bar
+
+During an actual build however, the right toolchain should be selected using aspects or other Bazel configurability features, instead of a hardcoded `--crosstool_top`.
+
+Building Toolchain Sysroot Tarball
+----------------------------------
+
+The toolchain's musl/linux components are currently built ahead of time and committed to this repository as `//build/toolchain/musl-host-gcc/toolchain.tar.xz`. This is the 'sysroot' tarball, that contains all headers and libraries required to build against Smalltown.
+
+To build this tarball, run the following commands:
+
+    bazel build //build/toolchain/musl-host-gcc/sysroot
+    cp -f bazel-bin/build/toolchain/musl-host-gcc/sysroot/sysroot.tar.xz build/toolchain/musl-host-gcc/sysroot.tar.xz
+
+Internals
+---------
+
+The toolchain is implemented in the following way:
+
+1. `//build/toolchain/musl-host-gcc/sysroot` is used to build `//build/toolchain/musl-host-gcc/sysroot.tar.xz` which is a tarball that contains all include and binary library files for building against musl for Smalltown (x86\_64 / k8) - thes are musl headers, musl libraries, and linux headers. This tarball is commited to source control.
+1. When building a target that uses the toolchain, the `sysroot.tar.xz` tarball is extracted into an external repository `@musl_sysroot`, via `sysroot.bzl` and `sysroot_repository.bzl`.
+1. A toolchain config is built using `//build/toolchain:cc_toolchain_config.bzl`, which points at `gcc-wrapper.sh` as its gcc entrypoint. `gcc-wrapper.sh` expects to be able to call the host gcc with `musl.spec`.
+1. A toolchain is built in `//build/toolchain/musl-host-gcc:musl_host_cc_suite`, which uses the previously mentioned config, and builds it to contain `gcc-wrapper.sh`, `musl.spec`, and the sysroot tarball.
+
+Quirks
+------
+
+As mentioned above, the musl sysroot is kept in a tarball in this repository. This is obviously suboptimal, but on the other hand gives us an effectively pre-built part of a toolchain. In the future, once we have a hermetic toolchain, a similar tarball might actually contain a fully hermetic toolchain pre-built for k8.
diff --git a/build/toolchain/musl-host-gcc/gcc-wrapper.sh b/build/toolchain/musl-host-gcc/gcc-wrapper.sh
new file mode 100755
index 0000000..a430e75
--- /dev/null
+++ b/build/toolchain/musl-host-gcc/gcc-wrapper.sh
@@ -0,0 +1,2 @@
+#!/usr/bin/env bash
+exec /usr/bin/gcc "$@" -specs build/toolchain/musl-host-gcc/musl.spec
diff --git a/build/toolchain/musl-host-gcc/musl.spec b/build/toolchain/musl-host-gcc/musl.spec
new file mode 100644
index 0000000..376d0d9
--- /dev/null
+++ b/build/toolchain/musl-host-gcc/musl.spec
@@ -0,0 +1,32 @@
+%rename cpp_options old_cpp_options
+
+*cpp_options:
+-nostdinc %(old_cpp_options) -isystem external/musl_sysroot/include
+
+*cc1:
+%(cc1_cpu) -nostdinc -isystem external/musl_sysroot/include
+
+*link_libgcc:
+-L .%s -L external/musl_sysroot/lib
+
+*libgcc:
+libgcc.a%s %:if-exists(libgcc_eh.a%s)
+
+*startfile:
+%{!shared: external/musl_sysroot/lib/Scrt1.o} external/musl_sysroot/lib/crti.o crtbeginS.o%s
+
+*endfile:
+crtendS.o%s external/musl_sysroot/lib/crtn.o
+
+*link:
+-nostdlib -no-dynamic-linker -static %{rdynamic:-export-dynamic}
+
+*esp_link:
+
+
+*esp_options:
+
+
+*esp_cpp_options:
+
+
diff --git a/build/toolchain/musl-host-gcc/sysroot.bzl b/build/toolchain/musl-host-gcc/sysroot.bzl
new file mode 100644
index 0000000..2f54ced
--- /dev/null
+++ b/build/toolchain/musl-host-gcc/sysroot.bzl
@@ -0,0 +1,26 @@
+#  Copyright 2020 The Monogon Project Authors.
+#
+#  SPDX-License-Identifier: Apache-2.0
+#
+#  Licensed under the Apache License, Version 2.0 (the "License");
+#  you may not use this file except in compliance with the License.
+#  You may obtain a copy of the License at
+#
+#      http://www.apache.org/licenses/LICENSE-2.0
+#
+#  Unless required by applicable law or agreed to in writing, software
+#  distributed under the License is distributed on an "AS IS" BASIS,
+#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+#  See the License for the specific language governing permissions and
+#  limitations under the License.
+
+load("//build/toolchain/musl-host-gcc:sysroot_repository.bzl", "musl_sysroot_rule")
+
+def musl_sysroot_repositories():
+    """
+    Provides an external repository that contains the extracted musl/linux sysroot.
+    """
+    musl_sysroot_rule(
+        name = "musl_sysroot",
+        snapshot = "//build/toolchain/musl-host-gcc:sysroot.tar.xz",
+    )
diff --git a/build/toolchain/musl-host-gcc/sysroot.tar.xz b/build/toolchain/musl-host-gcc/sysroot.tar.xz
new file mode 100755
index 0000000..e61dba6
--- /dev/null
+++ b/build/toolchain/musl-host-gcc/sysroot.tar.xz
Binary files differ
diff --git a/build/toolchain/musl-host-gcc/sysroot/BUILD b/build/toolchain/musl-host-gcc/sysroot/BUILD
new file mode 100644
index 0000000..62260ae
--- /dev/null
+++ b/build/toolchain/musl-host-gcc/sysroot/BUILD
@@ -0,0 +1,24 @@
+load(":musl.bzl", "musl_headers")
+load(":linux.bzl", "linux_headers")
+load(":tarball.bzl", "musl_gcc_tarball")
+
+linux_headers(
+    name = "linux_headers",
+    src = "@linux//:all",
+    arch = "x86_64",
+    visibility = ["//visibility:public"],
+)
+
+musl_headers(
+    name = "musl_headers",
+    src = "@musl//:all",
+    arch = "x86_64",
+    visibility = ["//visibility:public"],
+)
+
+musl_gcc_tarball(
+    name = "sysroot",
+    musl = "//third_party/musl",
+    musl_headers = ":musl_headers",
+    linux_headers = ":linux_headers",
+)
diff --git a/build/toolchain/musl-host-gcc/sysroot/linux.bzl b/build/toolchain/musl-host-gcc/sysroot/linux.bzl
new file mode 100644
index 0000000..e9cf40a
--- /dev/null
+++ b/build/toolchain/musl-host-gcc/sysroot/linux.bzl
@@ -0,0 +1,44 @@
+#  Copyright 2020 The Monogon Project Authors.
+#
+#  SPDX-License-Identifier: Apache-2.0
+#
+#  Licensed under the Apache License, Version 2.0 (the "License");
+#  you may not use this file except in compliance with the License.
+#  You may obtain a copy of the License at
+#
+#      http://www.apache.org/licenses/LICENSE-2.0
+#
+#  Unless required by applicable law or agreed to in writing, software
+#  distributed under the License is distributed on an "AS IS" BASIS,
+#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+#  See the License for the specific language governing permissions and
+#  limitations under the License.
+
+load(
+    "//build/utils:detect_root.bzl",
+    "detect_root",
+)
+
+def _linux_headers(ctx):
+    hdrs_name = ctx.attr.name + "_headers"
+    hdrs_dir = ctx.actions.declare_directory(hdrs_name)
+
+    root = detect_root(ctx.attr.src)
+    ctx.actions.run_shell(
+        inputs = ctx.files.src,
+        outputs = [hdrs_dir],
+        progress_message = "Generating Linux Kernel Headers",
+        mnemonic = "LinuxCollectHeaders",
+        arguments = [root, ctx.attr.arch, hdrs_dir.path],
+        use_default_shell_env = True,
+        command = "make -C \"$1\" headers_install ARCH=\"$2\" INSTALL_HDR_PATH=\"$(pwd)/$3\" > /dev/null && mv \"$3/include/\"* \"$3/\" && rmdir \"$3/include\"",
+    )
+    return [DefaultInfo(files=depset([hdrs_dir]))]
+
+linux_headers = rule(
+    implementation = _linux_headers,
+    attrs = {
+        "src": attr.label(mandatory = True),
+        "arch": attr.string(mandatory = True),
+    },
+)
diff --git a/build/toolchain/musl-host-gcc/sysroot/musl.bzl b/build/toolchain/musl-host-gcc/sysroot/musl.bzl
new file mode 100644
index 0000000..5055b83
--- /dev/null
+++ b/build/toolchain/musl-host-gcc/sysroot/musl.bzl
@@ -0,0 +1,44 @@
+#  Copyright 2020 The Monogon Project Authors.
+#
+#  SPDX-License-Identifier: Apache-2.0
+#
+#  Licensed under the Apache License, Version 2.0 (the "License");
+#  you may not use this file except in compliance with the License.
+#  You may obtain a copy of the License at
+#
+#      http://www.apache.org/licenses/LICENSE-2.0
+#
+#  Unless required by applicable law or agreed to in writing, software
+#  distributed under the License is distributed on an "AS IS" BASIS,
+#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+#  See the License for the specific language governing permissions and
+#  limitations under the License.
+
+load(
+    "//build/utils:detect_root.bzl",
+    "detect_root",
+)
+
+def _musl_headers(ctx):
+    hdrs_name = ctx.attr.name + "_headers"
+    hdrs_dir = ctx.actions.declare_directory(hdrs_name)
+
+    root = detect_root(ctx.attr.src)
+    ctx.actions.run_shell(
+        inputs = ctx.files.src,
+        outputs = [hdrs_dir],
+        progress_message = "Collecting musl headers",
+        mnemonic = "MuslCollectHeaders",
+        arguments = [root, ctx.attr.arch, hdrs_dir.path],
+        use_default_shell_env = True,
+        command = "make -C \"$1\" install-headers ARCH=\"$2\" includedir=\"$(pwd)/$3\" > /dev/null",
+    )
+    return [DefaultInfo(files=depset([hdrs_dir]))]
+
+musl_headers = rule(
+    implementation = _musl_headers,
+    attrs = {
+        "src": attr.label(mandatory = True),
+        "arch": attr.string(mandatory = True),
+    },
+)
diff --git a/build/toolchain/musl-host-gcc/sysroot/tarball.bzl b/build/toolchain/musl-host-gcc/sysroot/tarball.bzl
new file mode 100644
index 0000000..4f12049
--- /dev/null
+++ b/build/toolchain/musl-host-gcc/sysroot/tarball.bzl
@@ -0,0 +1,76 @@
+#  Copyright 2020 The Monogon Project Authors.
+#
+#  SPDX-License-Identifier: Apache-2.0
+#
+#  Licensed under the Apache License, Version 2.0 (the "License");
+#  you may not use this file except in compliance with the License.
+#  You may obtain a copy of the License at
+#
+#      http://www.apache.org/licenses/LICENSE-2.0
+#
+#  Unless required by applicable law or agreed to in writing, software
+#  distributed under the License is distributed on an "AS IS" BASIS,
+#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+#  See the License for the specific language governing permissions and
+#  limitations under the License.
+
+load(
+    "//build/utils:detect_root.bzl",
+    "detect_root",
+)
+
+"""
+Build a sysroot-style tarball containing musl/linux headers and libraries.
+
+This can then be used to build a C toolchain that builds for Smalltown.
+"""
+
+def _musl_gcc_tarball(ctx):
+    tarball_name = ctx.attr.name + ".tar.xz"
+    tarball = ctx.actions.declare_file(tarball_name)
+
+    musl_headers = ctx.file.musl_headers
+    musl_headers_path = musl_headers.path
+    linux_headers = ctx.file.linux_headers
+    linux_headers_path = linux_headers.path
+
+    musl_root = detect_root(ctx.attr.musl)
+    musl_files = ctx.files.musl
+
+    # This builds a tarball containing musl, musl headers and linux headers.
+    # This is done by some carefully crafted tar command line arguments that rewrite
+    # paths to ensure that everything lands in lib/ and include/ in the tarball.
+
+    # TODO(q3k): write nice, small static Go utility for this.
+
+    arguments = [tarball.path]
+    command = "tar -chJf $1"
+
+    arguments += [musl_headers_path]
+    command += " --transform 's|^'$2'|include|' $2"
+
+    arguments += [linux_headers_path]
+    command += " --transform 's|^'$3'|include|' $3"
+
+    arguments += [musl_root]
+    command += " --transform 's|^'$4'|lib|' $4"
+
+    ctx.actions.run_shell(
+        inputs = [musl_headers, linux_headers] + ctx.files.musl,
+        outputs = [tarball],
+        progress_message = "Building toolchain tarball",
+        mnemonic = "BuildToolchainTarball",
+        arguments = arguments,
+        use_default_shell_env = True,
+        command = command,
+    )
+    return [DefaultInfo(files=depset([tarball]))]
+
+musl_gcc_tarball = rule(
+    implementation = _musl_gcc_tarball,
+    attrs = {
+        "musl": attr.label(mandatory = True),
+        "musl_headers": attr.label(mandatory = True, allow_single_file = True),
+        "linux_headers": attr.label(mandatory = True, allow_single_file = True),
+    },
+)
diff --git a/build/toolchain/musl-host-gcc/sysroot_repository.bzl b/build/toolchain/musl-host-gcc/sysroot_repository.bzl
new file mode 100644
index 0000000..253abbf
--- /dev/null
+++ b/build/toolchain/musl-host-gcc/sysroot_repository.bzl
@@ -0,0 +1,42 @@
+#  Copyright 2020 The Monogon Project Authors.
+#
+#  SPDX-License-Identifier: Apache-2.0
+#
+#  Licensed under the Apache License, Version 2.0 (the "License");
+#  you may not use this file except in compliance with the License.
+#  You may obtain a copy of the License at
+#
+#      http://www.apache.org/licenses/LICENSE-2.0
+#
+#  Unless required by applicable law or agreed to in writing, software
+#  distributed under the License is distributed on an "AS IS" BASIS,
+#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+#  See the License for the specific language governing permissions and
+#  limitations under the License.
+
+"""
+A generic workspace rule that extracts some subpaths from a tarball.
+
+TODO(q3k): This should maybe be moved to //build/utils and called differently.
+"""
+
+def _musl_sysroot_rule_impl(rctx):
+    rctx.extract(rctx.attr.snapshot)
+    rctx.file("BUILD.bazel", """
+filegroup(
+    name = "all",
+    srcs = glob(["include/**", "lib/**"]),
+    visibility = ["//visibility:public"],
+)
+""")
+
+
+musl_sysroot_rule = repository_rule(
+    implementation = _musl_sysroot_rule_impl,
+    attrs = {
+        "snapshot": attr.label(
+            default = Label("//build/toolchain/musl-host-gcc:sysroot.tar.xz"),
+            allow_single_file = True,
+        ),
+    },
+)
diff --git a/build/utils/BUILD.bazel b/build/utils/BUILD.bazel
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/build/utils/BUILD.bazel
diff --git a/build/utils/detect_root.bzl b/build/utils/detect_root.bzl
new file mode 100644
index 0000000..50c9574
--- /dev/null
+++ b/build/utils/detect_root.bzl
@@ -0,0 +1,66 @@
+#  Copyright 2020 The Monogon Project Authors.
+#
+#  SPDX-License-Identifier: Apache-2.0
+#
+#  Licensed under the Apache License, Version 2.0 (the "License");
+#  you may not use this file except in compliance with the License.
+#  You may obtain a copy of the License at
+#
+#      http://www.apache.org/licenses/LICENSE-2.0
+#
+#  Unless required by applicable law or agreed to in writing, software
+#  distributed under the License is distributed on an "AS IS" BASIS,
+#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+#  See the License for the specific language governing permissions and
+#  limitations under the License.
+
+# Copyright Google Inc.
+# This file's contents are licensed under the Apache License, Version 2.0.
+# See third_party/licenses/LICENSE.APACHE20 file in this repository for a copy
+# of the License.
+
+# This file contains code adapted from github.com/bazelbuiled/rules_foreign_cc:
+# Files:
+#  - tools/build_defs/detect_root.bzl
+
+def detect_root(source):
+    """Detects the path to the topmost directory of the 'source' outputs.
+    To be used with external build systems to point to the source code/tools directories.
+"""
+
+    root = ""
+    sources = source.files.to_list()
+    if (root and len(root) > 0) or len(sources) == 0:
+        return root
+
+    root = ""
+    level = -1
+    num_at_level = 0
+
+    # find topmost directory
+    for file in sources:
+        file_level = _get_level(file.path)
+        if level == -1 or level > file_level:
+            root = file.path
+            level = file_level
+            num_at_level = 1
+        elif level == file_level:
+            num_at_level += 1
+
+    if num_at_level == 1:
+        return root
+
+    (before, sep, after) = root.rpartition("/")
+    if before and sep and after:
+        return before
+    return root
+
+def _get_level(path):
+    normalized = path
+    for i in range(len(path)):
+        new_normalized = normalized.replace("//", "/")
+        if len(new_normalized) == len(normalized):
+            break
+        normalized = new_normalized
+
+    return normalized.count("/")
diff --git a/third_party/licenses/LICENSE.APACHE20 b/third_party/licenses/LICENSE.APACHE20
new file mode 100644
index 0000000..d645695
--- /dev/null
+++ b/third_party/licenses/LICENSE.APACHE20
@@ -0,0 +1,202 @@
+
+                                 Apache License
+                           Version 2.0, January 2004
+                        http://www.apache.org/licenses/
+
+   TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
+
+   1. Definitions.
+
+      "License" shall mean the terms and conditions for use, reproduction,
+      and distribution as defined by Sections 1 through 9 of this document.
+
+      "Licensor" shall mean the copyright owner or entity authorized by
+      the copyright owner that is granting the License.
+
+      "Legal Entity" shall mean the union of the acting entity and all
+      other entities that control, are controlled by, or are under common
+      control with that entity. For the purposes of this definition,
+      "control" means (i) the power, direct or indirect, to cause the
+      direction or management of such entity, whether by contract or
+      otherwise, or (ii) ownership of fifty percent (50%) or more of the
+      outstanding shares, or (iii) beneficial ownership of such entity.
+
+      "You" (or "Your") shall mean an individual or Legal Entity
+      exercising permissions granted by this License.
+
+      "Source" form shall mean the preferred form for making modifications,
+      including but not limited to software source code, documentation
+      source, and configuration files.
+
+      "Object" form shall mean any form resulting from mechanical
+      transformation or translation of a Source form, including but
+      not limited to compiled object code, generated documentation,
+      and conversions to other media types.
+
+      "Work" shall mean the work of authorship, whether in Source or
+      Object form, made available under the License, as indicated by a
+      copyright notice that is included in or attached to the work
+      (an example is provided in the Appendix below).
+
+      "Derivative Works" shall mean any work, whether in Source or Object
+      form, that is based on (or derived from) the Work and for which the
+      editorial revisions, annotations, elaborations, or other modifications
+      represent, as a whole, an original work of authorship. For the purposes
+      of this License, Derivative Works shall not include works that remain
+      separable from, or merely link (or bind by name) to the interfaces of,
+      the Work and Derivative Works thereof.
+
+      "Contribution" shall mean any work of authorship, including
+      the original version of the Work and any modifications or additions
+      to that Work or Derivative Works thereof, that is intentionally
+      submitted to Licensor for inclusion in the Work by the copyright owner
+      or by an individual or Legal Entity authorized to submit on behalf of
+      the copyright owner. For the purposes of this definition, "submitted"
+      means any form of electronic, verbal, or written communication sent
+      to the Licensor or its representatives, including but not limited to
+      communication on electronic mailing lists, source code control systems,
+      and issue tracking systems that are managed by, or on behalf of, the
+      Licensor for the purpose of discussing and improving the Work, but
+      excluding communication that is conspicuously marked or otherwise
+      designated in writing by the copyright owner as "Not a Contribution."
+
+      "Contributor" shall mean Licensor and any individual or Legal Entity
+      on behalf of whom a Contribution has been received by Licensor and
+      subsequently incorporated within the Work.
+
+   2. Grant of Copyright License. Subject to the terms and conditions of
+      this License, each Contributor hereby grants to You a perpetual,
+      worldwide, non-exclusive, no-charge, royalty-free, irrevocable
+      copyright license to reproduce, prepare Derivative Works of,
+      publicly display, publicly perform, sublicense, and distribute the
+      Work and such Derivative Works in Source or Object form.
+
+   3. Grant of Patent License. Subject to the terms and conditions of
+      this License, each Contributor hereby grants to You a perpetual,
+      worldwide, non-exclusive, no-charge, royalty-free, irrevocable
+      (except as stated in this section) patent license to make, have made,
+      use, offer to sell, sell, import, and otherwise transfer the Work,
+      where such license applies only to those patent claims licensable
+      by such Contributor that are necessarily infringed by their
+      Contribution(s) alone or by combination of their Contribution(s)
+      with the Work to which such Contribution(s) was submitted. If You
+      institute patent litigation against any entity (including a
+      cross-claim or counterclaim in a lawsuit) alleging that the Work
+      or a Contribution incorporated within the Work constitutes direct
+      or contributory patent infringement, then any patent licenses
+      granted to You under this License for that Work shall terminate
+      as of the date such litigation is filed.
+
+   4. Redistribution. You may reproduce and distribute copies of the
+      Work or Derivative Works thereof in any medium, with or without
+      modifications, and in Source or Object form, provided that You
+      meet the following conditions:
+
+      (a) You must give any other recipients of the Work or
+          Derivative Works a copy of this License; and
+
+      (b) You must cause any modified files to carry prominent notices
+          stating that You changed the files; and
+
+      (c) You must retain, in the Source form of any Derivative Works
+          that You distribute, all copyright, patent, trademark, and
+          attribution notices from the Source form of the Work,
+          excluding those notices that do not pertain to any part of
+          the Derivative Works; and
+
+      (d) If the Work includes a "NOTICE" text file as part of its
+          distribution, then any Derivative Works that You distribute must
+          include a readable copy of the attribution notices contained
+          within such NOTICE file, excluding those notices that do not
+          pertain to any part of the Derivative Works, in at least one
+          of the following places: within a NOTICE text file distributed
+          as part of the Derivative Works; within the Source form or
+          documentation, if provided along with the Derivative Works; or,
+          within a display generated by the Derivative Works, if and
+          wherever such third-party notices normally appear. The contents
+          of the NOTICE file are for informational purposes only and
+          do not modify the License. You may add Your own attribution
+          notices within Derivative Works that You distribute, alongside
+          or as an addendum to the NOTICE text from the Work, provided
+          that such additional attribution notices cannot be construed
+          as modifying the License.
+
+      You may add Your own copyright statement to Your modifications and
+      may provide additional or different license terms and conditions
+      for use, reproduction, or distribution of Your modifications, or
+      for any such Derivative Works as a whole, provided Your use,
+      reproduction, and distribution of the Work otherwise complies with
+      the conditions stated in this License.
+
+   5. Submission of Contributions. Unless You explicitly state otherwise,
+      any Contribution intentionally submitted for inclusion in the Work
+      by You to the Licensor shall be under the terms and conditions of
+      this License, without any additional terms or conditions.
+      Notwithstanding the above, nothing herein shall supersede or modify
+      the terms of any separate license agreement you may have executed
+      with Licensor regarding such Contributions.
+
+   6. Trademarks. This License does not grant permission to use the trade
+      names, trademarks, service marks, or product names of the Licensor,
+      except as required for reasonable and customary use in describing the
+      origin of the Work and reproducing the content of the NOTICE file.
+
+   7. Disclaimer of Warranty. Unless required by applicable law or
+      agreed to in writing, Licensor provides the Work (and each
+      Contributor provides its Contributions) on an "AS IS" BASIS,
+      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
+      implied, including, without limitation, any warranties or conditions
+      of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
+      PARTICULAR PURPOSE. You are solely responsible for determining the
+      appropriateness of using or redistributing the Work and assume any
+      risks associated with Your exercise of permissions under this License.
+
+   8. Limitation of Liability. In no event and under no legal theory,
+      whether in tort (including negligence), contract, or otherwise,
+      unless required by applicable law (such as deliberate and grossly
+      negligent acts) or agreed to in writing, shall any Contributor be
+      liable to You for damages, including any direct, indirect, special,
+      incidental, or consequential damages of any character arising as a
+      result of this License or out of the use or inability to use the
+      Work (including but not limited to damages for loss of goodwill,
+      work stoppage, computer failure or malfunction, or any and all
+      other commercial damages or losses), even if such Contributor
+      has been advised of the possibility of such damages.
+
+   9. Accepting Warranty or Additional Liability. While redistributing
+      the Work or Derivative Works thereof, You may choose to offer,
+      and charge a fee for, acceptance of support, warranty, indemnity,
+      or other liability obligations and/or rights consistent with this
+      License. However, in accepting such obligations, You may act only
+      on Your own behalf and on Your sole responsibility, not on behalf
+      of any other Contributor, and only if You agree to indemnify,
+      defend, and hold each Contributor harmless for any liability
+      incurred by, or claims asserted against, such Contributor by reason
+      of your accepting any such warranty or additional liability.
+
+   END OF TERMS AND CONDITIONS
+
+   APPENDIX: How to apply the Apache License to your work.
+
+      To apply the Apache License to your work, attach the following
+      boilerplate notice, with the fields enclosed by brackets "[]"
+      replaced with your own identifying information. (Don't include
+      the brackets!)  The text should be enclosed in the appropriate
+      comment syntax for the file format. We also recommend that a
+      file or class name and description of purpose be included on the
+      same "printed page" as the copyright notice for easier
+      identification within third-party archives.
+
+   Copyright [yyyy] [name of copyright owner]
+
+   Licensed under the Apache License, Version 2.0 (the "License");
+   you may not use this file except in compliance with the License.
+   You may obtain a copy of the License at
+
+       http://www.apache.org/licenses/LICENSE-2.0
+
+   Unless required by applicable law or agreed to in writing, software
+   distributed under the License is distributed on an "AS IS" BASIS,
+   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+   See the License for the specific language governing permissions and
+   limitations under the License.
diff --git a/third_party/musl/BUILD.bazel b/third_party/musl/BUILD.bazel
index e69de29..afd1212 100644
--- a/third_party/musl/BUILD.bazel
+++ b/third_party/musl/BUILD.bazel
@@ -0,0 +1,36 @@
+genrule(
+    name = "musl",
+    srcs = [
+        "@musl//:all",
+    ],
+    outs = [
+        # C Runtimes
+        "crt1.o",
+        "crti.o",
+        "crtn.o",
+        "rcrt1.o",
+        "Scrt1.o",
+
+        # Static musl libc
+        "libc.a",
+
+        # Placeholder archives
+        "libcrypt.a",
+        "libdl.a",
+        "libm.a",
+        "libpthread.a",
+        "libresolv.a",
+        "librt.a",
+        "libutil.a",
+        "libxnet.a",
+    ],
+    cmd = """
+    OUT=$$PWD/$(RULEDIR)
+    (
+        cd external/musl
+        ./configure --prefix=$$OUT --syslibdir=$$OUT --libdir=$$OUT
+        make -j $$(nproc) install-libs
+    ) > /dev/null
+    """,
+    visibility = ["//visibility:public"],
+)