Code Monkey home page Code Monkey logo

apple_support's Introduction

Apple Support for Bazel

This repository contains the Apple CC toolchain, Apple related platforms and constraints definitions, and small helper functions for rules authors targeting Apple platforms.

If you want to build iOS, tvOS, visionOS, watchOS, or macOS apps, use rules_apple.

If you want to build Swift use rules_swift.

See the documentation for the helper rules provided by this repository.

Installation

Copy the MODULE.bazel or WORKSPACE snippets from the releases page into your project.

Toolchain setup

The Apple CC toolchain in this repository provides toolchains for building for Apple platforms besides macOS. Since Bazel 7 this toolchain is required when targeting those platforms but the toolchain also supports Bazel 6.

NOTE: This toolchain requires a full Xcode installation, not just the Xcode Command Line Tools. If you only need to build for macOS and don't want to require a full Xcode installation in your build, use the builtin Unix toolchain provided by Bazel.

Bazel 7+ Setup

For Bazel 7+ the only setup that is required is to have apple_support in your MODULE.bazel (even if you're not referencing it directly) or WORKSPACE, which you can copy from the releases page into your project.

If you also depend on rules_cc, apple_support must come above rules_cc in your MODULE.bazel or WORKSPACE file because Bazel selects toolchains based on which is registered first.

Bazel 6 Setup

For Bazel 6, pull this repository into your build and add this to your .bazelrc:

build --enable_platform_specific_config
build:macos --apple_crosstool_top=@local_config_apple_cc//:toolchain
build:macos --crosstool_top=@local_config_apple_cc//:toolchain
build:macos --host_crosstool_top=@local_config_apple_cc//:toolchain

This ensures that all rules provided by rules_apple, as well as other rules like cc_binary, all use the toolchain provided by this repository when building on macOS.

If you're using bzlmod with Bazel 6 and the --crosstool_top configurations you must expose the local_config_apple_cc repository to your project by putting this in your MODULE.bazel:

apple_cc_configure = use_extension("@build_bazel_apple_support//crosstool:setup.bzl", "apple_cc_configure_extension")
use_repo(apple_cc_configure, "local_config_apple_cc")

Toolchain configuration

There are many different flags you can flip to configure how the toolchain works. Here are some of the more commonly useful ones:

  • Setting DEVELOPER_DIR in the environment. This is recommended so that the toolchain can be invalidated when the DEVELOPER_DIR changes, which ensures that toolchain binaries will be rebuilt with the new version of Xcode so that caches are correctly shared across machines.
  • Setting BAZEL_ALLOW_NON_APPLICATIONS_XCODE=1 in the environment (or using --repo_env) allows the toolchain to discover Xcode versions outside of the /Applications directory to avoid header inclusion errors from bazel. This is not enabled by default because /Applications is the standard directory, and this improves toolchain setup performance.

apple_support's People

Contributors

allevato avatar balestrapatrick avatar brentleyjones avatar cgrindel avatar comius avatar dflems avatar dslomov avatar erikkerber avatar fmeum avatar fweikert avatar github-actions[bot] avatar illicitonion avatar kaylathar avatar keith avatar kersson avatar luispadron avatar nglevin avatar sergiocampama avatar stravinskii avatar thii avatar thomasvl avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

apple_support's Issues

Provide a mechanism to disable `-dead_strip` in builds

I've recently come across a failure to build gazelle that was ultimately traced down to commit a000b23. I'm not a strong enough C++ developer to understand why that's happening, but I noticed that the commit message for this stated that:

This should be the right default for everyone, if not we can look at adding a way to disable it.

Having a flag to disable this feature would be most helpful.

Does not work with Bazel 2.1

Error:

$ bazel test :all


Traceback (most recent call last):
	File "/.../external/build_bazel_rules_apple/tools/realpath/BUILD", line 9
		_apple_genrule_inner(name = 'realpath_genrule')
	File "/.../external/build_bazel_apple_support/rules/apple_genrule.bzl", line 78, in _apple_genrule_impl
		apple_support.run_shell(ctx, <8 more arguments>)
	File "/.../external/build_bazel_apple_support/lib/apple_support.bzl", line 326, in apple_support.run_shell
		ctx.actions.run_shell(<1 more arguments>)
'command' must be of type string. passing a sequence of strings as 'command' is deprecated. To temporarily disable this check, set --incompatible_objc_framework_cleanup=false.

However, --incompatible_objc_framework_cleanup=false doesn't exist:

$ bazel test --noincompatible_objc_framework_cleanup :all
ERROR: Unrecognized option: --noincompatible_objc_framework_cleanup

It was removed here.

Here's the code with the error.

Getting rid of apple_cc_toolchain

We would like to get rid of apple_cc_toolchain. There has been recent efforts to refector it so that it is more like cc_toolchain, to the point where I believe the only remaining difference is the three crosstool variables used by cc_toolchain_config to set these environment variables: XCODE_VERSION_OVERRIDE, APPLE_SDK_VERSION_OVERRIDE, APPLE_SDK_PLATFORM. This information is readily available in xcode_config, which is already a dependency of cc_toolchain_config, so I believe it should be straightforward to migrate the bazel apple cc toolchain to that.

Once that is done, we should be able to replace all instances of apple_cc_toolchain with cc_toolchain.

This change also removes the use on APPLE_SDK_VERSION_OVERRIDE, so that can eventually be dropped also.

apple_support.run doesn't seem to work when executable is a FilesToRunProvider

I'm still pretty new to bazel, so sorry if I'm doing something weird! I'm trying to get bazel_clang_tidy working, but patching it to connect it to our copy of LLVM. AIUI, it uses an aspect to generate additional actions for each cc rule. This is almost working, but I started getting errors because the sysroot contained __BAZEL_XCODE_SDKROOT__, which I think was being added due to our use of rules_xcodeproj.

I've modified the invocation of ctx.actions.run here to call apple_support.run instead, and injected the "apple" fragment and appropriate attrs too.

But now I'm seeing bazel file with the following error (truncated):

exec: com.google.devtools.build.lib.analysis.FilesToRunProvider@680cb74e: not found

It looks like this may be because apple_support.run doesn't expect the executable to be a FilesToRunProvider.

Is there a good workaround for this, or am I holding it wrong? :-)

CC toolchain error occurs when building on linux with targets that are only compatible with MacOS

Reproduction

The following error occurs on linux if a target is marked as only being compatible with MacOS and the targets that depend on that target are not marked as such.

ERROR: /home/chuck/.cache/bazel/_bazel_chuck/1356082df8f6e9e7706552f3fc65582d/external/bazel_tools/tools/cpp/BUILD:58:19: in cc_toolchain_alias rule @bazel_tools//tools/cpp:current_cc_toolchain: 
Traceback (most recent call last):
        File "/virtual_builtins_bzl/common/cc/cc_toolchain_alias.bzl", line 26, column 48, in _impl
        File "/virtual_builtins_bzl/common/cc/cc_helper.bzl", line 219, column 17, in _find_cpp_toolchain
Error in fail: Unable to find a CC toolchain using toolchain resolution. Target: @@bazel_tools//tools/cpp:current_cc_toolchain, Platform: @@apple_support~1.11.1//platforms:darwin_x86_64, Exec platform: @@loca
l_config_platform//:host
ERROR: /home/chuck/.cache/bazel/_bazel_chuck/1356082df8f6e9e7706552f3fc65582d/external/bazel_tools/tools/cpp/BUILD:58:19: Analysis of target '@bazel_tools//tools/cpp:current_cc_toolchain' failed
ERROR: Analysis of target '//Sources/PrintObjcVersion:PrintObjcVersion' failed; build aborted: Analysis failed

Precompile toolchain binaries to avoid issues

Sometimes compiling the toolchain binaries causes timeouts on slower CI machines. One way we could avoid this is by vendoring the binaries for the few files we need instead of causing them to be compiled on the fly. This would also solve hermeticity issues with them

bazelbuild/bazel#17437

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.