reland "set up GL sample app to build through Bazel Mac toolchain"

https://skia-review.googlesource.com/c/skia/+/549897

G3 error fixed with:
https://critique.corp.google.com/cl/456234733

Introduces tracking bug:
https://bugs.chromium.org/p/skia/issues/detail?id=13452

Suggested review order.
1) tools/sk_app/* and src/gpu/ganesh/*
sets up the actual target to be built by the toolchain
2) toolchain/* and .bazelrc
changes to the mac hermetic toolchain, including support for framework dependencies, objc compilation, and dynamic lib dependency resolution

Change-Id: Id31e0adb134d385cbb4af6818f2c25c4fdae9598
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/551881
Reviewed-by: Kevin Lubick <kjlubick@google.com>
Commit-Queue: Jorge Betancourt <jmbetancourt@google.com>
This commit is contained in:
Jorge Betancourt 2022-06-21 14:38:54 -04:00 committed by SkCQ
parent 3bd3b5dfae
commit c327d1054a
9 changed files with 203 additions and 53 deletions

View File

@ -4,7 +4,9 @@ build:clang_linux --crosstool_top=//toolchain:clang_suite_linux
build:clang_linux --compiler=host_is_linux_amd64
build:clang_mac --crosstool_top=//toolchain:clang_suite_mac
build:clang_mac --apple_crosstool_top=//toolchain:clang_suite_mac
build:clang_mac --compiler=host_is_mac_m1
build:clang_mac --apple_platform_type=macos
# =============================================================================
# Alias to build configurations below. This makes configuring things from

View File

@ -69,6 +69,11 @@ def cc_library(**kwargs):
"""A shim around cc_library that lets us tweak settings for G3 if necessary."""
native.cc_library(**kwargs)
# buildifier: disable=unnamed-macro
def objc_library(**kwargs):
"""A shim around objc_library that lets us tweak settings for G3 if necessary."""
native.objc_library(**kwargs)
# buildifier: disable=unnamed-macro
def exports_files_legacy(label_list = None, visibility = None):
"""A self-annotating macro to export all files in this package for legacy G3 rules.

View File

@ -29,8 +29,11 @@ cc_binary_with_flags(
},
deps = [
"//:skia_public",
"//tools/sk_app",
],
] + select({
"@platforms//os:macos": ["//tools/sk_app:sk_app_objc"],
"@platforms//os:linux": ["//tools/sk_app:sk_app"],
"//conditions:default": [],
}),
)
cc_binary_with_flags(

View File

@ -65,6 +65,9 @@ use system headers and Mac-specific includes when compiling. Our Bazel toolchain
`xcode-select` in your path so that we may symlink the user's current Xcode directory in the
toolchain's cache. Make sure `xcode-select -p` returns a valid path.
Your Xcode path should resemble `/Applications/Xcode.app/Contents/Developer/`. Either move your
Xcode or use `xcode-select` to use the Xcode in this location.
## .bazelrc Tips
You should make a [.bazelrc file](https://bazel.build/docs/bazelrc) in your home directory where
you can specify settings that apply only to you. These can augment or replace the ones we define

View File

@ -3,3 +3,11 @@ load("//bazel:macros.bzl", "exports_files_legacy")
licenses(["notice"])
exports_files_legacy()
filegroup(
name = "srcs",
srcs = [
"GrGLMakeNativeInterface_mac.cpp",
],
visibility = ["//src/gpu/ganesh/gl:__pkg__"],
)

View File

@ -29,6 +29,7 @@ cc_toolchain_suite(
"arm64|host_is_mac_m1": ":mac_m1_host",
"darwin|host_is_mac_m1": ":mac_m1_host",
"darwin_arm64|host_is_mac_m1": ":mac_m1_host",
"darwin_x86_64": ":mac_m1_host",
},
)

View File

@ -94,6 +94,14 @@ def _make_action_configs():
action_name = ACTION_NAMES.cpp_compile,
tools = [clang_tool],
)
objc_compile_action = action_config(
action_name = ACTION_NAMES.objc_compile,
tools = [clang_tool],
)
objcpp_compile_action = action_config(
action_name = ACTION_NAMES.objcpp_compile,
tools = [clang_tool],
)
linkstamp_compile_action = action_config(
action_name = ACTION_NAMES.linkstamp_compile,
tools = [clang_tool],
@ -118,22 +126,16 @@ def _make_action_configs():
tools = [lld_tool],
)
# This is the same rule as
# https://github.com/emscripten-core/emsdk/blob/7f39d100d8cd207094decea907121df72065517e/bazel/emscripten_toolchain/crosstool.bzl#L143
# By default, there are no flags or libraries passed to the llvm-ar tool, so
# we need to specify them. The variables mentioned by expand_if_available are defined
# https://docs.bazel.build/versions/main/cc-toolchain-config-reference.html#cctoolchainconfiginfo-build-variables
cpp_link_static_library_action = action_config(
action_name = ACTION_NAMES.cpp_link_static_library,
flag_sets = [
# objc archiver and cpp archiver actions use the same base flags
common_archive_flags = [
flag_set(
flag_groups = [
flag_group(
# https://llvm.org/docs/CommandGuide/llvm-ar.html
# replace existing files or insert them if they already exist,
# create the file if it doesn't already exist
# symbol table should be added
# Deterministic timestamps should be used
# [r]eplace existing files or insert them if they already exist,
# [c]reate the file if it doesn't already exist
# [s]ymbol table should be added
# [D]eterministic timestamps should be used
flags = ["rcsD", "%{output_execpath}"],
# Despite the name, output_execpath just refers to linker output,
# e.g. libFoo.a
@ -174,7 +176,22 @@ def _make_action_configs():
),
],
),
],
]
# This is the same rule as
# https://github.com/emscripten-core/emsdk/blob/7f39d100d8cd207094decea907121df72065517e/bazel/emscripten_toolchain/crosstool.bzl#L143
# By default, there are no flags or libraries passed to the llvm-ar tool, so
# we need to specify them. The variables mentioned by expand_if_available are defined
# https://docs.bazel.build/versions/main/cc-toolchain-config-reference.html#cctoolchainconfiginfo-build-variables
cpp_link_static_library_action = action_config(
action_name = ACTION_NAMES.cpp_link_static_library,
flag_sets = common_archive_flags,
tools = [ar_tool],
)
objc_archive_action = action_config(
action_name = ACTION_NAMES.objc_archive,
flag_sets = common_archive_flags,
tools = [ar_tool],
)
@ -187,16 +204,25 @@ def _make_action_configs():
cpp_link_nodeps_dynamic_library_action,
cpp_link_static_library_action,
linkstamp_compile_action,
objc_archive_action,
objc_compile_action,
objcpp_compile_action,
preprocess_assemble_action,
]
return action_configs
# In addition to pointing the c and cpp compile actions to our toolchain, we also need to set objc
# and objcpp action flags as well. We build .m and .mm files with the objc_library rule, which
# will use the default toolchain if not specified here.
# https://docs.bazel.build/versions/3.3.0/be/objective-c.html#objc_library
def _make_default_flags():
"""Here we define the flags for certain actions that are always applied."""
cxx_compile_includes = flag_set(
actions = [
ACTION_NAMES.c_compile,
ACTION_NAMES.cpp_compile,
ACTION_NAMES.objc_compile,
ACTION_NAMES.objcpp_compile,
],
flag_groups = [
flag_group(
@ -211,6 +237,13 @@ def _make_default_flags():
XCODE_SYMLINK + "/include",
"-isystem",
EXTERNAL_TOOLCHAIN + "/lib/clang/13.0.0/include",
# Set the framework path to the Mac SDK framework directory. We can't include it
# through XCODE_SYMLINK because of infinite symlink recursion introduced in the
# framework folder.
# TODO(jmbetancourt): feed this path similarly to how we used xcode-select
# idea: set this in the trampoline script
"-F",
"/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/System/Library/Frameworks",
# We do not want clang to search in absolute paths for files. This makes
# Bazel think we are using an outside resource and fail the compile.
"-no-canonical-prefixes",
@ -222,6 +255,8 @@ def _make_default_flags():
cpp_compile_includes = flag_set(
actions = [
ACTION_NAMES.cpp_compile,
ACTION_NAMES.objc_compile,
ACTION_NAMES.objcpp_compile,
],
flag_groups = [
flag_group(
@ -238,6 +273,14 @@ def _make_default_flags():
flag_groups = [
flag_group(
flags = [
# lld goes through dynamic library dependencies for dylib and tbh files through
# absolute paths (/System/Library/Frameworks). However, the dependencies live in
# [Xcode dir]/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/System/Library/Frameworks
# -Wl tells clang to forward the next flag to the linker.
# -syslibroot appends to the beginning of the dylib dependency path.
# https://github.com/llvm/llvm-project/blob/d61341768cf0cff7ceeaddecc2f769b5c1b901c4/lld/MachO/InputFiles.cpp#L1418-L1420
"-Wl,-syslibroot",
"/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/",
"-fuse-ld=lld",
# We chose to use the llvm runtime, not the gcc one because it is already
# included in the clang binary
@ -259,6 +302,7 @@ def _make_default_flags():
),
],
)
return [feature(
"default_flags",
enabled = True,

View File

@ -1,4 +1,4 @@
load("//bazel:macros.bzl", "cc_library", "exports_files_legacy", "select_multi", "selects")
load("//bazel:macros.bzl", "cc_library", "exports_files_legacy", "objc_library", "select_multi", "selects")
licenses(["notice"])
@ -20,6 +20,14 @@ selects.config_setting_group(
],
)
selects.config_setting_group(
name = "gl_mac",
match_all = [
"//bazel/common_config_settings:gl_backend",
"@platforms//os:macos",
],
)
selects.config_setting_group(
name = "vulkan_unix",
match_all = [
@ -72,3 +80,49 @@ cc_library(
"//conditions:default": [],
}),
)
objc_library(
name = "sk_app_objc",
testonly = True,
srcs = [
"Window.cpp",
"WindowContext.cpp",
"RasterWindowContext.h",
] + select_multi(
{
"//bazel/common_config_settings:gl_backend": [
"GLWindowContext.cpp",
"GLWindowContext.h",
],
#TODO dawn and metal backend
},
default = [],
) + select({
"@platforms//os:macos": ["//tools/sk_app/mac:srcs"],
"//conditions:default": [],
# TODO ios support
}),
hdrs = [
"Application.h",
"DisplayParams.h",
"Window.h",
"WindowContext.h",
],
copts = [
"-Wno-deprecated-declarations",
],
sdk_frameworks = [
"QuartzCore",
"Cocoa",
"Foundation",
],
visibility = ["//:__subpackages__"],
deps = [
"//:skia_internal",
"//tools/skui",
"//tools/timer",
] + select({
"@platforms//os:macos": ["//tools/sk_app/mac:deps"],
"//conditions:default": [],
}),
)

View File

@ -0,0 +1,30 @@
load("//bazel:macros.bzl", "cc_library", "exports_files_legacy")
licenses(["notice"])
exports_files_legacy()
filegroup(
name = "srcs",
testonly = True,
srcs = [
"RasterWindowContext_mac.mm",
"WindowContextFactory_mac.h",
"Window_mac.mm",
"Window_mac.h",
"main_mac.mm",
] + select({
#TODO Metal and Dawn
"//bazel/common_config_settings:gl_backend": ["GLWindowContext_mac.mm"],
"//conditions:default": [],
}),
visibility = ["//tools/sk_app:__pkg__"],
)
cc_library(
name = "deps",
testonly = True,
linkopts = [],
visibility = ["//tools/sk_app:__pkg__"],
deps = ["//tools:tool_utils"],
)