aboutsummaryrefslogtreecommitdiffhomepage
path: root/tools/build_defs/apple/shared.bzl
blob: 440b1798f158a06a2ef3e0df1459856b41dc22f6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# Copyright 2016 The Bazel Authors. All rights reserved.
#
# 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.
"""Common definitions for Apple rules."""

APPLE_SIMULATOR_ARCHITECTURES = ["i386", "x86_64"]
"""Architectures that are used by the simulator (iOS, tvOS and watchOS)."""

IOS_DEVICE_ARCHITECTURES = ["armv7", "arm64"]
"""Architectures that are used by iOS devices."""

TVOS_DEVICE_ARCHITECTURES = ["arm64"]
"""Architectures that are used by tvOS devices."""

WATCHOS_DEVICE_ARCHITECTURES = ["armv7k"]
"""Architectures that are used by watchOS devices."""

APPLE_DEFAULT_ARCHITECTURES = (APPLE_SIMULATOR_ARCHITECTURES +
                               IOS_DEVICE_ARCHITECTURES +
                               WATCHOS_DEVICE_ARCHITECTURES)
"""Architectures commonly used for building/testing on simulators/devices."""

APPLE_FRAGMENTS = ["apple"]
"""Configuration fragments containing Apple specific information."""

DARWIN_EXECUTION_REQUIREMENTS = {"requires-darwin": ""}
"""Standard execution requirements to force building on Mac.

See :func:`apple_action`."""

XCRUNWRAPPER_LABEL = "//external:xcrunwrapper"
"""The label for xcrunwrapper tool."""

def label_scoped_path(ctx, path):
    """Return the path scoped to target's label."""
    return ctx.label.name + "/" + path.lstrip("/")

def module_cache_path(ctx):
    """Returns the Clang module cache path to use for this rule."""
    return ctx.genfiles_dir.path + "/_objc_module_cache"

def apple_action(ctx, **kw):
    """Creates an action that only runs on MacOS/Darwin.

    Call it similar to how you would call ctx.action:
      apple_action(ctx, outputs=[...], inputs=[...],...)
    """
    execution_requirements = dict(kw.get("execution_requirements", {}))
    execution_requirements.update(DARWIN_EXECUTION_REQUIREMENTS)

    no_sandbox = kw.pop("no_sandbox", False)
    if no_sandbox:
        execution_requirements["nosandbox"] = "1"

    kw["execution_requirements"] = execution_requirements

    ctx.action(**kw)

def xcrun_env(ctx):
    """Returns the environment dictionary necessary to use xcrunwrapper."""
    platform = ctx.fragments.apple.single_arch_platform

    if hasattr(apple_common, "apple_host_system_env"):
        xcode_config = ctx.attr._xcode_config[apple_common.XcodeVersionConfig]
        env = apple_common.target_apple_env(xcode_config, platform)
        env.update(apple_common.apple_host_system_env(xcode_config))
    else:
        env = ctx.fragments.apple.target_apple_env(platform)
        env.update(ctx.fragments.apple.apple_host_system_env())

    return env

def xcrun_action(ctx, **kw):
    """Creates an apple action that executes xcrunwrapper.

    args:
      ctx: The context of the rule that owns this action.

    This method takes the same keyword arguments as ctx.action, however you don't
    need to specify the executable.
    """
    kw["env"] = dict(kw.get("env", {}))
    kw["env"].update(xcrun_env(ctx))

    apple_action(ctx, executable = ctx.executable._xcrunwrapper, **kw)