Skip to content

Commit

Permalink
Upgrade to buildtools 4.2.0 and reformat (#285)
Browse files Browse the repository at this point in the history
This moves us to a modern version of buildtools (buildifier), which
powers `make format`.

Also, update build file formatting across the repository.
  • Loading branch information
jparise authored Sep 27, 2021
1 parent 4fbc056 commit f2c1a43
Show file tree
Hide file tree
Showing 31 changed files with 1,323 additions and 1,276 deletions.
82 changes: 45 additions & 37 deletions BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ load(
)
load(
"@build_bazel_rules_apple//apple:resources.bzl",
"apple_resource_group",
"apple_resource_bundle",
"apple_resource_group",
)
load(
"@build_bazel_rules_swift//swift:swift.bzl",
Expand Down Expand Up @@ -44,7 +44,6 @@ apple_resource_group(
]),
)


# Build system support. Currently, we use the example xcbuildkit
# to simply show the progress bar.
# Stick this into a bundle to group files together
Expand All @@ -54,15 +53,14 @@ apple_resource_bundle(
"@xcbuildkit//:BazelBuildServiceInstaller",
"@xcbuildkit//:BuildInfo",
# We use this to check if it's installed into each Xcode
"@bazel_tools//tools/osx:xcode-locator-genrule"
]
"@bazel_tools//tools/osx:xcode-locator-genrule",
],
)

load(
"//:BazelExtensions/xcode_configuration_provider.bzl",
"declare_target_config",
)

load(
"@build_bazel_rules_apple//apple:resources.bzl",
"apple_resource_group",
Expand All @@ -72,33 +70,37 @@ load(
# - $(SPAWN_OPTS) is determined at build time by tools/XCHammerXcodeRunscript.sh
# XCHammer's `bazel`, tools/bazelwrapper subs out make variable
xchammer_xcode_target_config = target_config(
build_bazel_template="tools/XCHammerXcodeRunscript.sh",
build_bazel_options="$(SPAWN_OPTS)"
build_bazel_options = "$(SPAWN_OPTS)",
build_bazel_template = "tools/XCHammerXcodeRunscript.sh",
)

# This is an example of declaring a target config.
declare_target_config(name="XCHammerSourcesXcodeConfig",
config=xchammer_xcode_target_config)
declare_target_config(
name = "XCHammerSourcesXcodeConfig",
config = xchammer_xcode_target_config,
)

# This BUILD file is not actually imported into XCHammer
# There is already a BUILD file placed in there by another dep.
apple_resource_group(
name = "BazelExtensions",
structured_resources=glob([
structured_resources = glob([
"BazelExtensions/*.bzl",
]),
)


swift_library(
name = "XCHammerSources",
srcs = glob(["Sources/**/*.swift"]),
copts = [
"-swift-version",
"4.2",
],
data = [
":XCHammerAssets",
":BazelExtensions",
] + [
":XCBuildKit",

],
deps = ["@" + namespaced_name(x) for x in [
"AEXML//:AEXML",
Expand All @@ -114,11 +116,7 @@ swift_library(
"XcodeGen//:ProjectSpec",
"XcodeProj//:XcodeProj",
"Yams//:Yams",
]] + [":XCHammerSourcesXcodeConfig" ],
copts = [
"-swift-version",
"4.2"
],
]] + [":XCHammerSourcesXcodeConfig"],
)

apple_bundle_version(
Expand All @@ -132,7 +130,10 @@ macos_application(
infoplists = ["Info.plist"],
minimum_os_version = "10.14",
version = ":XCHammerVersion",
deps = [":XCHammerSources", ":XCHammerSourcesXcodeConfig"],
deps = [
":XCHammerSources",
":XCHammerSourcesXcodeConfig",
],
)

buildifier(
Expand Down Expand Up @@ -169,41 +170,47 @@ gen_xchammer_config(
},
target_config = {
"//:xchammer": xchammer_xcode_target_config,
"//tools/XCConfigExporter:xcconfig-exporter": xchammer_xcode_target_config
"//tools/XCConfigExporter:xcconfig-exporter": xchammer_xcode_target_config,
},
targets = ["//:xchammer", "//tools/XCConfigExporter:xcconfig-exporter"],
targets = [
"//:xchammer",
"//tools/XCConfigExporter:xcconfig-exporter",
],
),
)

# Xcode project for Bazel built project
load(
"//:BazelExtensions/xcodeproject.bzl",
"xcode_project"
"xcode_project",
)

# The xcode_project for Bazel built Xcode project
# Note: the target_config is declared in :XCHammerSourcesXcodeConfig
xcode_project(
name="workspace_v2",
bazel="tools/bazelwrapper",
targets=["//:xchammer", "//tools/XCConfigExporter:xcconfig-exporter"],
project_config=project_config(
name = "workspace_v2",
bazel = "tools/bazelwrapper",
project_config = project_config(
generate_xcode_schemes = False,
paths = ["**"],
generate_xcode_schemes=False,
xcconfig_overrides = {
"Release": "tools/BazelToolchain.xcconfig",
"Debug": "tools/BazelToolchain.xcconfig",
"Profile": "tools/BazelToolchain.xcconfig",
}
)
},
),
targets = [
"//:xchammer",
"//tools/XCConfigExporter:xcconfig-exporter",
],
)

# Development / Distribution
genrule(
name = "xchammer_unzip",
srcs = [ ":xchammer" ],
srcs = [":xchammer"],
outs = ["xchammer_unzip"],
cmd = "unzip -q $(SRCS) -d $(OUTS)",
outs = [ "xchammer_unzip" ]
)

# This produces the same interface as the XCHammer repository, so a person
Expand Down Expand Up @@ -232,23 +239,24 @@ gen_workspace_cmd = """
touch WORKSPACE
echo 'sh_binary(name="xchammer", srcs=["xchammer.app/Contents/MacOS/xchammer"], visibility=["//visibility:public"])' > BUILD
"""

genrule(
name = "xchammer_dist",
srcs = [ ":xchammer_unzip" ],
srcs = [":xchammer_unzip"],
outs = ["xchammer_dist_repo.zip"],
cmd = """
mkdir zipdir/
cp -r $(SRCS)/xchammer.app zipdir/xchammer.app
cd zipdir
{gen_workspace_cmd}
zip -q ../$(OUTS) -r .
""".format(gen_workspace_cmd=gen_workspace_cmd),
outs = [ "xchammer_dist_repo.zip" ]
""".format(gen_workspace_cmd = gen_workspace_cmd),
)

# Builds an xchammer WORKSPACE use for local development. By creating a symlink
# to the build artifact to ensure the upstream repository is using the latest
# artifacts.
#
#
# If we were to just use `xchammer_dist` Bazel will not check a `.zip` of an
# http_archive, so this symlinks the files instead.
#
Expand All @@ -262,12 +270,12 @@ genrule(
# --override_repository=xchammer=/path/to/xchammer/bazel-bin/xchammer_dev_repo/
genrule(
name = "xchammer_dev",
srcs = [ ":xchammer" ],
srcs = [":xchammer"],
outs = ["xchammer_dev_repo"],
cmd = """
mkdir $(OUTS)
ln -s $$(dirname $$PWD/$(SRCS))/xchammer_archive-root/xchammer.app $(OUTS)/xchammer.app
cd $(OUTS)
{gen_workspace_cmd}
""".format(gen_workspace_cmd=gen_workspace_cmd),
outs = [ "xchammer_dev_repo" ],
""".format(gen_workspace_cmd = gen_workspace_cmd),
)
3 changes: 1 addition & 2 deletions BazelExtensions/tulsi.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@ load(
"@xchammer_tulsi_aspects//:tulsi/tulsi_aspects_paths.bzl",
_SwiftInfo = "SwiftInfo",
)

load(
"@xchammer_tulsi_aspects//:tulsi/tulsi_aspects.bzl",
_tulsi_sources_aspect = "tulsi_sources_aspect",
_TulsiSourcesAspectInfo = "TulsiSourcesAspectInfo",
_tulsi_sources_aspect = "tulsi_sources_aspect",
)

SwiftInfo = _SwiftInfo
Expand Down
1 change: 0 additions & 1 deletion BazelExtensions/xchammerconfig.bzl
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# Experimental XCHammer DSL.


def _gen_dsl_impl(ctx):
ctx.actions.write(
content = ctx.attr.ast,
Expand Down
70 changes: 32 additions & 38 deletions BazelExtensions/xcode_configuration_provider.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -4,45 +4,40 @@ load(
)

XcodeProjectTargetInfo = provider(
fields={
fields = {
"target_config_json_str": """
JSON string of target_config
Note: this must be a string as it's a rule input
"""
}
""",
},
)


def _declare_target_config_impl(ctx):
return struct(
providers=[XcodeProjectTargetInfo(target_config_json_str=ctx.attr.json)],
objc=apple_common.new_objc_provider(),
providers = [XcodeProjectTargetInfo(target_config_json_str = ctx.attr.json)],
objc = apple_common.new_objc_provider(),
)


_declare_target_config = rule(
implementation=_declare_target_config_impl,
output_to_genfiles=True,
attrs={"json": attr.string(mandatory=True)},
implementation = _declare_target_config_impl,
output_to_genfiles = True,
attrs = {"json": attr.string(mandatory = True)},
)


def declare_target_config(name, config, **kwargs):
""" Declare a target configuration for an Xcode project
This rule takes a `target_config` from XCHammerConfig
and aggregates it onto the depgraph
"""
_declare_target_config(name=name, json=config.to_json().replace("\\", ""), **kwargs)

_declare_target_config(name = name, json = config.to_json().replace("\\", ""), **kwargs)

XcodeConfigurationAspectInfo = provider(
fields={
fields = {
"values": """This is the value of the JSON
"""
}
""",
},
)


def _target_config_aspect_impl(itarget, ctx):
infos = []
if ctx.rule.kind == "_declare_target_config":
Expand All @@ -61,19 +56,18 @@ def _target_config_aspect_impl(itarget, ctx):
elif XcodeProjectTargetInfo in target:
info_map[str(itarget.label)] = target[XcodeProjectTargetInfo].target_config_json_str

return XcodeConfigurationAspectInfo(values=info_map)

return XcodeConfigurationAspectInfo(values = info_map)

target_config_aspect = aspect(
implementation=_target_config_aspect_impl, attr_aspects=["*"]
implementation = _target_config_aspect_impl,
attr_aspects = ["*"],
)


XcodeBuildSourceInfo = provider(
fields={
fields = {
"values": """The values of source files
"""
}
""",
},
)

def _extract_generated_sources(target, ctx):
Expand Down Expand Up @@ -103,10 +97,10 @@ def _extract_generated_sources(target, ctx):
files.append(objc.module_map)

trans_files = depset(transitive = files)
return [f for f in trans_files.to_list() if not f.is_source]
return [f for f in trans_files.to_list() if not f.is_source]

get_srcroot = "\"$(cat ../../DO_NOT_BUILD_HERE)/\""
non_hermetic_execution_requirements = { "no-cache": "1", "no-remote": "1", "local": "1", "no-sandbox": "1" }
non_hermetic_execution_requirements = {"no-cache": "1", "no-remote": "1", "local": "1", "no-sandbox": "1"}

def _install_action(ctx, infos, itarget):
inputs = []
Expand All @@ -124,19 +118,19 @@ def _install_action(ctx, infos, itarget):
inputs.append(info)
last = parts[len(parts) - 1]
cmd.append(
"target_dir=\"$SRCROOT/xchammer-includes/x/x/" + target_dir + "\""
"target_dir=\"$SRCROOT/xchammer-includes/x/x/" + target_dir + "\"",
)
cmd.append("mkdir -p \"$target_dir\"")
cmd.append("ditto " + info.path + " \"$target_dir\"")

output = ctx.actions.declare_file(itarget.label.name + "_outputs.dummy")
cmd.append("touch " + output.path)
ctx.actions.run_shell(
inputs=inputs,
command="\n".join(cmd),
use_default_shell_env=True,
outputs=[output],
execution_requirements = non_hermetic_execution_requirements
inputs = inputs,
command = "\n".join(cmd),
use_default_shell_env = True,
outputs = [output],
execution_requirements = non_hermetic_execution_requirements,
)
return [output]

Expand Down Expand Up @@ -167,16 +161,16 @@ def _xcode_build_sources_aspect_impl(itarget, ctx):
XcodeBuildSourceInfo(values = infos),
]


# Note, that for "pure" Xcode builds we build swiftmodules with Xcode, so we
# don't need to pre-compile them with Bazel
pure_xcode_build_sources_aspect = aspect(
implementation=_xcode_build_sources_aspect_impl, attr_aspects=["*"],
attrs = { "include_swift_outputs": attr.string(values=["false","true"], default="false") }
implementation = _xcode_build_sources_aspect_impl,
attr_aspects = ["*"],
attrs = {"include_swift_outputs": attr.string(values = ["false", "true"], default = "false")},
)

xcode_build_sources_aspect = aspect(
implementation=_xcode_build_sources_aspect_impl, attr_aspects=["*"],
attrs = { "include_swift_outputs": attr.string(values=["false", "true"], default="true") }
implementation = _xcode_build_sources_aspect_impl,
attr_aspects = ["*"],
attrs = {"include_swift_outputs": attr.string(values = ["false", "true"], default = "true")},
)

Loading

0 comments on commit f2c1a43

Please sign in to comment.