//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/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("/")