• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2023 The Pigweed Authors
2#
3# Licensed under the Apache License, Version 2.0 (the "License"); you may not
4# use this file except in compliance with the License. You may obtain a copy of
5# the License at
6#
7#     https://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12# License for the specific language governing permissions and limitations under
13# the License.
14"""Xcode configuration for Bazel build.
15
16This replaces xcode_configure, but only intends to work with macOS host builds,
17and exclusively attempts to support xcode command-line tools.
18"""
19
20def _pw_xcode_repository_impl(repository_ctx):
21    """Generated repository containing a pw_xcode_info target.
22
23    Args:
24        repository_ctx: The context of the current repository.
25
26    Returns:
27        None
28    """
29
30    build_file_contents = [
31        "package(default_visibility = [\"//visibility:public\"])",
32        "",
33        "filegroup(",
34        "    name = \"default\",",
35        "    visibility = [\"//visibility:private\"],",
36        ")",
37    ]
38    repository_ctx.file("BUILD", "\n".join(build_file_contents))
39
40    # Generate a stub and early return if not on macOS.
41    if repository_ctx.os.name != "mac os x":
42        # Generate the constant, but make it empty.
43        defs_file_contents = [
44            "XCODE_SDK_PATH = \"\"",
45        ]
46        repository_ctx.file("defs.bzl", "\n".join(defs_file_contents))
47        return
48
49    xcrun_result = repository_ctx.execute(["/usr/bin/xcrun", "--show-sdk-path"])
50    if xcrun_result.return_code != 0:
51        fail("Failed locating Xcode SDK: {}".format(xcrun_result.stderr))
52
53    sdk_path = xcrun_result.stdout.replace("\n", "")
54    defs_file_contents = [
55        "XCODE_SDK_PATH = \"{}\"".format(sdk_path),
56    ]
57    repository_ctx.file("defs.bzl", "\n".join(defs_file_contents))
58
59pw_xcode_repository = repository_rule(
60    _pw_xcode_repository_impl,
61    attrs = {},
62    doc = "Initializes a macOS SDK repository",
63)
64