Skip to content

Commit

Permalink
Properly handle generated processed resources
Browse files Browse the repository at this point in the history
In f20bd24 I forgot to account for generated resource files.

Signed-off-by: Brentley Jones <[email protected]>
  • Loading branch information
brentleyjones committed Oct 28, 2024
1 parent f20bd24 commit cfaa3b4
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 26 deletions.
21 changes: 21 additions & 0 deletions test/internal/pbxproj_partials/write_files_and_groups_tests.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,13 @@ def _write_files_and_groups_test_impl(ctx):
)
for (path, owner) in ctx.attr.generated_files.items()
]
generated_file_paths = [
struct(
path = path,
owner = mock_actions.mock_label(owner),
)
for (path, owner) in ctx.attr.generated_file_paths.items()
]
generated_folders = [
struct(
path = path,
Expand All @@ -97,6 +104,7 @@ def _write_files_and_groups_test_impl(ctx):
files = depset(files),
file_paths = depset(ctx.attr.file_paths),
folders = depset(ctx.attr.folders),
generated_file_paths = depset(generated_file_paths),
generated_folders = depset(generated_folders),
generator_name = "a_generator_name",
install_path = ctx.attr.install_path,
Expand Down Expand Up @@ -197,6 +205,7 @@ write_files_and_groups_test = unittest.make(
"file_paths": attr.string_list(mandatory = True),
"files": attr.string_list(mandatory = True),
"folders": attr.string_list(mandatory = True),
"generated_file_paths": attr.string_dict(mandatory = True),
"generated_files": attr.string_dict(mandatory = True),
"generated_folders": attr.string_dict(mandatory = True),
"install_path": attr.string(mandatory = True),
Expand Down Expand Up @@ -231,6 +240,7 @@ def write_files_and_groups_test_suite(name):
files = [],
file_paths = [],
folders = [],
generated_file_paths = {},
generated_files = {},
generated_folders = {},
install_path,
Expand All @@ -253,6 +263,7 @@ def write_files_and_groups_test_suite(name):
files = files,
file_paths = file_paths,
folders = folders,
generated_file_paths = generated_file_paths,
generated_files = generated_files,
generated_folders = generated_folders,
install_path = install_path,
Expand Down Expand Up @@ -358,6 +369,10 @@ def write_files_and_groups_test_suite(name):
"a/path/to/a/folder",
"another/path/to/another/folder",
],
generated_file_paths = {
"bazel-out/ios-sim-config/bin/a/path/to/a/generated/file_as_file_path": "//a/path/to/a/generated",
"bazel-out/ios-sim-config/bin/another/path/to/another/generated/file_as_file_path": "//another/path/to/another/generated",
},
generated_files = {
"bazel-out/ios-sim-config/bin/a/path/to/a/generated/file": "//a/path/to/a/generated",
"bazel-out/ios-sim-config/bin/another/path/to/another/generated/file": "//another/path/to/another/generated",
Expand Down Expand Up @@ -437,6 +452,12 @@ ios-sim-config
file
another/path/to/another/generated
ios-sim-config
file_as_file_path
a/path/to/a/generated
ios-sim-config
file_as_file_path
another/path/to/another/generated
ios-sim-config
""",
_GENERATED_FOLDER_PATHS_FILE: """\
folder
Expand Down
6 changes: 6 additions & 0 deletions xcodeproj/internal/files/incremental_input_files.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -490,6 +490,9 @@ def _collect_incremental_input_files(
if label not in bundle_labels
],
)
extra_generated_file_paths = memory_efficient_depset(
resources_result.generated_folder_resources,
)
extra_generated_folders = memory_efficient_depset(
resources_result.generated_folder_resources,
transitive = [
Expand All @@ -505,6 +508,7 @@ def _collect_incremental_input_files(
)
else:
extra_folders = EMPTY_DEPSET
extra_generated_file_paths = EMPTY_DEPSET
extra_generated_folders = EMPTY_DEPSET
resource_bundle_labels = memory_efficient_depset(
transitive = [
Expand Down Expand Up @@ -552,6 +556,7 @@ def _collect_incremental_input_files(
transitive = transitive_extra_files,
),
extra_folders = extra_folders,
extra_generated_file_paths = extra_generated_file_paths,
extra_generated_folders = extra_generated_folders,
infoplist = infoplist,
non_arc_srcs = memory_efficient_depset(non_arc_srcs),
Expand Down Expand Up @@ -660,6 +665,7 @@ def _collect_mixed_language_input_files(
extra_file_paths = mergeable_info.extra_file_paths,
extra_files = mergeable_info.extra_files,
extra_folders = EMPTY_DEPSET,
extra_generated_file_paths = EMPTY_DEPSET,
extra_generated_folders = EMPTY_DEPSET,
infoplist = None,
non_arc_srcs = mergeable_info.non_arc_srcs,
Expand Down
41 changes: 28 additions & 13 deletions xcodeproj/internal/files/incremental_resources.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ def _create_bundle(name = None):
name = name,
resources = [],
resource_file_paths = [],
generated_resource_file_paths = [],
folder_resources = [],
generated_folder_resources = [],
dependency_paths = [],
Expand Down Expand Up @@ -73,19 +74,14 @@ def _handle_processed_resource(
processed_origins,
)

file_paths = []
owner = file.owner
for short_path in origin_short_paths:
file_path = _handle_processed_resource_origin(
_handle_processed_resource_origin(
bundle = bundle,
short_path = short_path,
focused_resource_short_paths = focused_resource_short_paths,
owner = owner,
)
if file_path:
file_paths.append(file_path)

return file_paths

def _handle_processed_resource_origin(
*,
Expand All @@ -94,18 +90,20 @@ def _handle_processed_resource_origin(
focused_resource_short_paths,
owner):
if short_path not in focused_resource_short_paths:
return None
return

if short_path.startswith("../"):
file_path = "external" + short_path[2:]
else:
file_path = short_path

is_generated = file_path.startswith("bazel-out/")

# If a file is a child of a folder-type file, the parent folder-type file
# should be added to the bundle instead of the child file
folder_type_prefix = _path_folder_type_prefix(file_path)
if folder_type_prefix:
if file_path.startswith("bazel-out/"):
if is_generated:
bundle.generated_folder_resources.append(
struct(
owner = owner,
Expand All @@ -114,9 +112,15 @@ def _handle_processed_resource_origin(
)
else:
bundle.folder_resources.append(folder_type_prefix)
return None

return file_path
elif is_generated:
bundle.generated_resource_file_paths.append(
struct(
owner = owner,
path = file_path,
),
)
else:
bundle.resource_file_paths.append(file_path)

def _handle_unprocessed_resource(
*,
Expand Down Expand Up @@ -173,15 +177,14 @@ def _add_processed_resources_to_bundle(
focused_resource_short_paths,
processed_origins):
for file in files.to_list():
file_paths = _handle_processed_resource(
_handle_processed_resource(
bundle = bundle,
bundle_metadata = bundle_metadata,
bundle_path = bundle_path,
file = file,
focused_resource_short_paths = focused_resource_short_paths,
processed_origins = processed_origins,
)
bundle.resource_file_paths.extend(file_paths)

def _add_unprocessed_resources_to_bundle(
*,
Expand Down Expand Up @@ -439,6 +442,12 @@ def _collect_incremental_resources(
* `resources`: A `list` of two element `tuple`s. The first
element is the label of the target that owns the resource. The
second element is a `File` for a resource.
* `resource_file_paths`: A `list` of two element `tuple`s. The first
element is the label of the target that owns the resource. The
second element is a file path string of a non-generated resource.
* `generated_resource_file_paths`: A `list` of two element `tuple`s.
The first element is the label of the target that owns the resource.
The second element is a file path string of a generated resource.
* `xccurrentversions`: A `list` of `.xccurrentversion` `File`s.
"""
root_bundle = _create_bundle()
Expand Down Expand Up @@ -522,6 +531,8 @@ def _collect_incremental_resources(
for child_bundle_path in parent_bundle_paths:
bundle = resource_bundle_targets[child_bundle_path]
if (not bundle.resources and
not bundle.resource_file_paths and
not bundle.generated_resource_file_paths and
not bundle.folder_resources and
not bundle.generated_folder_resources and
not bundle.dependency_paths):
Expand Down Expand Up @@ -554,6 +565,9 @@ def _collect_incremental_resources(
resource_file_paths = memory_efficient_depset(
bundle.resource_file_paths,
),
generated_resource_file_paths = memory_efficient_depset(
bundle.generated_resource_file_paths,
),
folder_resources = memory_efficient_depset(
bundle.folder_resources,
),
Expand All @@ -567,6 +581,7 @@ def _collect_incremental_resources(
bundles = frozen_bundles,
resources = root_bundle.resources,
resource_file_paths = root_bundle.resource_file_paths,
generated_resource_file_paths = root_bundle.generated_resource_file_paths,
folder_resources = root_bundle.folder_resources,
generated_folder_resources = root_bundle.generated_folder_resources,
xccurrentversions = xccurrentversions,
Expand Down
10 changes: 6 additions & 4 deletions xcodeproj/internal/incremental_xcode_targets.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@ def _from_resource_bundle(bundle):
compile_stub_needed = False,
inputs = struct(
entitlements = EMPTY_DEPSET,
extra_files = bundle.resources,
extra_file_paths = bundle.resource_file_paths,
extra_files = bundle.resources,
extra_folders = bundle.folder_resources,
extra_generated_file_paths = bundle.generated_resource_file_paths,
extra_generated_folders = bundle.generated_folder_resources,
infoplist = None,
non_arc_srcs = EMPTY_DEPSET,
Expand Down Expand Up @@ -172,16 +173,17 @@ def _make_incremental_xcode_target(

def _merge_xcode_inputs(*, dest_inputs, mergeable_info):
return struct(
extra_files = memory_efficient_depset(
transitive = [dest_inputs.extra_files, mergeable_info.extra_files],
),
extra_file_paths = memory_efficient_depset(
transitive = [
dest_inputs.extra_file_paths,
mergeable_info.extra_file_paths,
],
),
extra_files = memory_efficient_depset(
transitive = [dest_inputs.extra_files, mergeable_info.extra_files],
),
extra_folders = dest_inputs.extra_folders,
extra_generated_file_paths = dest_inputs.extra_generated_file_paths,
extra_generated_folders = dest_inputs.extra_generated_folders,
infoplist = dest_inputs.infoplist,
non_arc_srcs = mergeable_info.non_arc_srcs,
Expand Down
25 changes: 16 additions & 9 deletions xcodeproj/internal/pbxproj_partials.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,14 @@ def _generated_dirname(file):

return file.dirname

def _always_generated_file_path(file):
def _xcfilelist_always_generated_file_path(file):
return "$(BAZEL_OUT){}".format(file.path[9:])

def _generated_file_path(file):
def _xcfilelist_generated_file_path(file):
if file.is_source:
return None

return _always_generated_file_path(file)
return _xcfilelist_always_generated_file_path(file)

def _source_file(file):
if not file.is_source:
Expand Down Expand Up @@ -108,10 +108,10 @@ def _generated_file(file):

return _generated_file_or_folder(file.path, file.owner)

def _generated_folder(generated_folder):
def _generated_file_path(generated_file_path):
return _generated_file_or_folder(
generated_folder.path,
generated_folder.owner,
generated_file_path.path,
generated_file_path.owner,
)

# Partials
Expand Down Expand Up @@ -405,6 +405,7 @@ def _write_files_and_groups(
files,
file_paths,
folders,
generated_file_paths,
generated_folders,
generator_name,
install_path,
Expand All @@ -429,6 +430,8 @@ def _write_files_and_groups(
file paths.
folders: A `depset` of paths to non-generated folders to include in the
project.
generated_file_paths: A `depset` of file paths to generated files to
include in the project.
generated_folders: A `depset` of paths to generated folders to include
in the project.
generator_name: The name of the `xcodeproj` generator target.
Expand Down Expand Up @@ -517,6 +520,10 @@ def _write_files_and_groups(
generated_file_paths_args.set_param_file_format("multiline")

generated_file_paths_args.add_all(files, map_each = _generated_file)
generated_file_paths_args.add_all(
generated_file_paths,
map_each = _generated_file_path,
)

actions.write(generated_file_paths_file, generated_file_paths_args)

Expand All @@ -533,7 +540,7 @@ def _write_files_and_groups(

generated_folder_paths_args.add_all(
generated_folders,
map_each = _generated_folder,
map_each = _generated_file_path,
)

actions.write(generated_folder_paths_file, generated_folder_paths_args)
Expand Down Expand Up @@ -677,12 +684,12 @@ def _write_generated_xcfilelist(

# Info.plists are tracked as build files by Xcode, so top-level targets
# will fail the first time they are built if we don't track them
args.add_all(infoplists, map_each = _always_generated_file_path)
args.add_all(infoplists, map_each = _xcfilelist_always_generated_file_path)

# Source files are tracked as build files by Xcode, so building targets that
# directly use generated source files will fail the first time they are
# built if we don't track them
args.add_all(srcs, map_each = _generated_file_path)
args.add_all(srcs, map_each = _xcfilelist_generated_file_path)

xcfilelist = actions.declare_file(
"{}-generated.xcfilelist".format(generator_name),
Expand Down
8 changes: 8 additions & 0 deletions xcodeproj/internal/xcodeproj_incremental_rule.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -135,12 +135,16 @@ def _collect_files(
transitive_file_paths = []
transitive_files = [unsupported_extra_files]
transitive_folders = []
transitive_generated_file_paths = []
transitive_generated_folders = []
transitive_srcs = []
for xcode_target in all_targets:
transitive_file_paths.append(xcode_target.inputs.extra_file_paths)
transitive_files.append(xcode_target.inputs.extra_files)
transitive_folders.append(xcode_target.inputs.extra_folders)
transitive_generated_file_paths.append(
xcode_target.inputs.extra_generated_file_paths,
)
transitive_generated_folders.append(
xcode_target.inputs.extra_generated_folders,
)
Expand Down Expand Up @@ -169,13 +173,15 @@ def _collect_files(
transitive = transitive_files,
)
folders = depset(transitive = transitive_folders)
generated_file_paths = depset(transitive = transitive_generated_file_paths)
generated_folders = depset(transitive = transitive_generated_folders)

return (
compile_stub_needed,
file_paths,
files,
folders,
generated_file_paths,
generated_folders,
infoplists,
srcs,
Expand Down Expand Up @@ -376,6 +382,7 @@ def _write_project_contents(
file_paths,
files,
folders,
generated_file_paths,
generated_folders,
infoplists,
srcs,
Expand Down Expand Up @@ -436,6 +443,7 @@ def _write_project_contents(
files = files,
file_paths = file_paths,
folders = folders,
generated_file_paths = generated_file_paths,
generated_folders = generated_folders,
generator_name = name,
install_path = install_path,
Expand Down

0 comments on commit cfaa3b4

Please sign in to comment.