1*61c4878aSAndroid Build Coastguard Worker# Copyright 2024 The Pigweed Authors 2*61c4878aSAndroid Build Coastguard Worker# 3*61c4878aSAndroid Build Coastguard Worker# Licensed under the Apache License, Version 2.0 (the "License"); you may not 4*61c4878aSAndroid Build Coastguard Worker# use this file except in compliance with the License. You may obtain a copy of 5*61c4878aSAndroid Build Coastguard Worker# the License at 6*61c4878aSAndroid Build Coastguard Worker# 7*61c4878aSAndroid Build Coastguard Worker# https://www.apache.org/licenses/LICENSE-2.0 8*61c4878aSAndroid Build Coastguard Worker# 9*61c4878aSAndroid Build Coastguard Worker# Unless required by applicable law or agreed to in writing, software 10*61c4878aSAndroid Build Coastguard Worker# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 11*61c4878aSAndroid Build Coastguard Worker# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 12*61c4878aSAndroid Build Coastguard Worker# License for the specific language governing permissions and limitations under 13*61c4878aSAndroid Build Coastguard Worker# the License. 14*61c4878aSAndroid Build Coastguard Worker"""A basic extension for exposing a macOS host's local Xcode libraries.""" 15*61c4878aSAndroid Build Coastguard Worker 16*61c4878aSAndroid Build Coastguard Workerdef _get_xcode_sdk_root(extension_ctx): 17*61c4878aSAndroid Build Coastguard Worker xcrun_result = extension_ctx.execute(["/usr/bin/xcrun", "--show-sdk-path"]) 18*61c4878aSAndroid Build Coastguard Worker if xcrun_result.return_code != 0: 19*61c4878aSAndroid Build Coastguard Worker fail("Failed locating Xcode SDK: {}".format(xcrun_result.stderr)) 20*61c4878aSAndroid Build Coastguard Worker return xcrun_result.stdout.replace("\n", "") 21*61c4878aSAndroid Build Coastguard Worker 22*61c4878aSAndroid Build Coastguard Worker# If you squint hard enough, this looks like a new_local_repository, but 23*61c4878aSAndroid Build Coastguard Worker# we very intentionally avoid using new_local_repository here since the xcode 24*61c4878aSAndroid Build Coastguard Worker# SDK path gets baked into the MODULE.bazel.lock which breaks CI/CQ bots. 25*61c4878aSAndroid Build Coastguard Worker# 26*61c4878aSAndroid Build Coastguard Worker# See https://issues.pigweed.dev/issues/346388161#comment24 for more details. 27*61c4878aSAndroid Build Coastguard Workerdef _xcode_repository_impl(repository_ctx): 28*61c4878aSAndroid Build Coastguard Worker if repository_ctx.os.name != "mac os x": 29*61c4878aSAndroid Build Coastguard Worker fail( 30*61c4878aSAndroid Build Coastguard Worker "Cannot expose a local Xcode SDK on {} hosts. This repository " + 31*61c4878aSAndroid Build Coastguard Worker "rule only works on macOS hosts. It's possible a rule from one " + 32*61c4878aSAndroid Build Coastguard Worker "of your build files is referencing this repository and is not " + 33*61c4878aSAndroid Build Coastguard Worker "properly gated behind a `target_compatible_with` expression or " + 34*61c4878aSAndroid Build Coastguard Worker "`select()` statement." % (repository_ctx.os,), 35*61c4878aSAndroid Build Coastguard Worker ) 36*61c4878aSAndroid Build Coastguard Worker p = repository_ctx.path(_get_xcode_sdk_root(repository_ctx)) 37*61c4878aSAndroid Build Coastguard Worker children = p.readdir() 38*61c4878aSAndroid Build Coastguard Worker for child in children: 39*61c4878aSAndroid Build Coastguard Worker repository_ctx.symlink(child, child.basename) 40*61c4878aSAndroid Build Coastguard Worker repository_ctx.symlink(repository_ctx.attr.build_file, "BUILD") 41*61c4878aSAndroid Build Coastguard Worker 42*61c4878aSAndroid Build Coastguard Workerxcode_sdk_repository = repository_rule( 43*61c4878aSAndroid Build Coastguard Worker implementation = _xcode_repository_impl, 44*61c4878aSAndroid Build Coastguard Worker attrs = { 45*61c4878aSAndroid Build Coastguard Worker "build_file": attr.label( 46*61c4878aSAndroid Build Coastguard Worker allow_single_file = True, 47*61c4878aSAndroid Build Coastguard Worker ), 48*61c4878aSAndroid Build Coastguard Worker }, 49*61c4878aSAndroid Build Coastguard Worker local = True, 50*61c4878aSAndroid Build Coastguard Worker configure = True, 51*61c4878aSAndroid Build Coastguard Worker) 52