Skip to content

Commit

Permalink
Add pbxproj_partials.write_targets
Browse files Browse the repository at this point in the history
Signed-off-by: Brentley Jones <github@brentleyjones.com>
  • Loading branch information
brentleyjones committed Dec 18, 2023
1 parent 875f147 commit 77e515b
Showing 1 changed file with 174 additions and 0 deletions.
174 changes: 174 additions & 0 deletions xcodeproj/internal/pbxproj_partials.bzl
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Actions for creating `PBXProj` partials."""

load(":collections.bzl", "uniq")
load(
":memory_efficiency.bzl",
"EMPTY_LIST",
Expand All @@ -9,6 +10,8 @@ load(
)
load(":platforms.bzl", "PLATFORM_NAME")

_UNIT_TEST_PRODUCT_TYPE = "u" # com.apple.product-type.bundle.unit-test

# Utility

def _apple_platform_to_platform_name(platform):
Expand All @@ -17,6 +20,18 @@ def _apple_platform_to_platform_name(platform):
def _depset_len(depset):
return str(len(depset.to_list()))

def _dsym_files_to_string(dsym_files):
dsym_paths = []
for file in dsym_files.to_list():
file_path = file.path

# dSYM files contain plist and DWARF.
if not file_path.endswith("Info.plist"):
# ../Product.dSYM/Contents/Resources/DWARF/Product
dsym_path = "/".join(file_path.split("/")[:-4])
dsym_paths.append("\"{}\"".format(dsym_path))
return " ".join(dsym_paths)

def _dynamic_framework_path(file_and_is_framework):
file, is_framework = file_and_is_framework
if is_framework:
Expand Down Expand Up @@ -250,6 +265,19 @@ def _write_files_and_groups(
resolved_repositories_file,
)

def _write_generated_xcfilelist(*, actions, generator_name, infoplist_paths):
args = actions.args()
args.set_param_file_format("multiline")

args.add_all(infoplist_paths)

xcfilelist = actions.declare_file(
"{}-generated.xcfilelist".format(generator_name),
)
actions.write(xcfilelist, args)

return xcfilelist

def _write_pbxproj_prefix(
*,
actions,
Expand Down Expand Up @@ -876,10 +904,156 @@ def _write_target_build_settings(

return build_settings_output, debug_settings_output, params

def _write_targets(
*,
actions,
colorize,
consolidation_maps,
default_xcode_configuration,
generator_name,
install_path,
tool,
xcode_target_configurations,
xcode_targets,
xcode_targets_by_label):
"""Creates `File`s representing targets in a `PBXProj` element.
Args:
actions: `ctx.actions`.
colorize: Whether to colorize the output.
consolidation_maps: A `dict` mapping `File`s containing target
consolidation maps to a `list` of `Label`s of the targets included
in the map.
default_xcode_configuration: The name of the the Xcode configuration to
use when building, if not overridden by custom schemes.
generator_name: The name of the `xcodeproj` generator target.
install_path: The workspace relative path to where the final
`.xcodeproj` will be written.
tool: The executable that will generate the output files.
xcode_target_configurations: A `dict` mapping `xcode_target.id` to a
`list` of Xcode configuration names that the target is present in.
xcode_targets: A `dict` mapping `xcode_target.id` to `xcode_target`s.
xcode_targets_by_label: A `dict` mapping `xcode_target.label` to a
`dict` mapping `xcode_target.id` to `xcode_target`s.
Returns:
A tuple with two elements:
* `pbxnativetargets`: A `list` of `File`s for the `PBNativeTarget`
`PBXProj` partials.
* `buildfile_subidentifiers_files`: A `list` of `File`s that contain
serialized `[Identifiers.BuildFile.SubIdentifier]`s.
"""
pbxnativetargets = []
buildfile_subidentifiers_files = []
for consolidation_map, labels in consolidation_maps.items():
(
label_pbxnativetargets,
label_buildfile_subidentifiers,
) = _write_consolidation_map_targets(
actions = actions,
colorize = colorize,
consolidation_map = consolidation_map,
default_xcode_configuration = default_xcode_configuration,
generator_name = generator_name,
idx = consolidation_map.basename,
install_path = install_path,
labels = labels,
tool = tool,
xcode_target_configurations = xcode_target_configurations,
xcode_targets = xcode_targets,
xcode_targets_by_label = xcode_targets_by_label,
)

pbxnativetargets.append(label_pbxnativetargets)
buildfile_subidentifiers_files.append(label_buildfile_subidentifiers)

return (
pbxnativetargets,
buildfile_subidentifiers_files,
)

# `project.pbxproj`

def _write_project_pbxproj(
*,
actions,
files_and_groups,
generator_name,
pbxproj_prefix,
pbxproject_targets,
pbxproject_known_regions,
pbxproject_target_attributes,
pbxtargetdependencies,
targets):
"""Creates a `project.pbxproj` `File`.
Args:
actions: `ctx.actions`.
files_and_groups: The `files_and_groups` `File` returned from
`pbxproj_partials.write_files_and_groups`.
generator_name: The name of the `xcodeproj` generator target.
pbxproj_prefix: The `File` returned from
`pbxproj_partials.write_pbxproj_prefix`.
pbxproject_known_regions: The `known_regions` `File` returned from
`pbxproj_partials.write_known_regions`.
pbxproject_target_attributes: The `pbxproject_target_attributes` `File`
returned from `pbxproj_partials.write_pbxproject_targets`.
pbxproject_targets: The `pbxproject_targets` `File` returned from
`pbxproj_partials.write_pbxproject_targets`.
pbxtargetdependencies: The `pbxtargetdependencies` `Files` returned from
`pbxproj_partials.write_pbxproject_targets`.
targets: The `targets` `list` of `Files` returned from
`pbxproj_partials.write_targets`.
Returns:
A `project.pbxproj` `File`.
"""
output = actions.declare_file("{}.project.pbxproj".format(generator_name))

inputs = [
pbxproj_prefix,
pbxproject_target_attributes,
pbxproject_known_regions,
pbxproject_targets,
] + targets + [
pbxtargetdependencies,
files_and_groups,
]

args = actions.args()
args.use_param_file("%s")
args.set_param_file_format("multiline")
args.add_all(inputs)

actions.run_shell(
arguments = [args],
inputs = inputs,
outputs = [output],
command = """\
cat "$@" > "{output}"
""".format(output = output.path),
mnemonic = "WriteXcodeProjPBXProj",
progress_message = "Generating %{output}",
execution_requirements = {
# Running `cat` is faster than looking up and copying from cache
"no-cache": "1",
# Absolute paths
"no-remote": "1",
# Each file is directly referenced, so lets have some speed
"no-sandbox": "1",
},
)

return output

pbxproj_partials = struct(
write_files_and_groups = _write_files_and_groups,
write_generated_xcfilelist = _write_generated_xcfilelist,
write_pbxproj_prefix = _write_pbxproj_prefix,
write_pbxtargetdependencies = _write_pbxtargetdependencies,
write_project_pbxproj = _write_project_pbxproj,
write_swift_debug_settings = _write_swift_debug_settings,
write_target_build_settings = _write_target_build_settings,
write_targets = _write_targets,
)

0 comments on commit 77e515b

Please sign in to comment.