Skip to content

Commit a0e1a09

Browse files
Use CcInfo for defines and local_defines (#276)
This also fixes an oddity(?) where `swift_library` sets defines for cc dependents.
1 parent 1e0ae01 commit a0e1a09

File tree

6 files changed

+43
-91
lines changed

6 files changed

+43
-91
lines changed

examples/ios_app/test/fixtures/project.xcodeproj/project.pbxproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1322,6 +1322,7 @@
13221322
"__DATE__=\"redacted\"",
13231323
"__TIMESTAMP__=\"redacted\"",
13241324
"__TIME__=\"redacted\"",
1325+
AWESOME,
13251326
);
13261327
HEADER_SEARCH_PATHS = (
13271328
"$(PROJECT_DIR)/CoreUtilsObjC",
@@ -1407,6 +1408,7 @@
14071408
"__DATE__=\"redacted\"",
14081409
"__TIMESTAMP__=\"redacted\"",
14091410
"__TIME__=\"redacted\"",
1411+
AWESOME,
14101412
);
14111413
GENERATE_INFOPLIST_FILE = YES;
14121414
IPHONEOS_DEPLOYMENT_TARGET = 15.0;
@@ -1630,6 +1632,7 @@
16301632
"__DATE__=\"redacted\"",
16311633
"__TIMESTAMP__=\"redacted\"",
16321634
"__TIME__=\"redacted\"",
1635+
AWESOME,
16331636
);
16341637
HEADER_SEARCH_PATHS = (
16351638
"$(PROJECT_DIR)/CoreUtilsObjC",

examples/ios_app/test/fixtures/spec.json

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -656,7 +656,8 @@
656656
"DEBUG",
657657
"__DATE__=\"redacted\"",
658658
"__TIMESTAMP__=\"redacted\"",
659-
"__TIME__=\"redacted\""
659+
"__TIME__=\"redacted\"",
660+
"AWESOME"
660661
],
661662
"IPHONEOS_DEPLOYMENT_TARGET": "15.0",
662663
"OTHER_CFLAGS": [
@@ -910,7 +911,8 @@
910911
"DEBUG",
911912
"__DATE__=\"redacted\"",
912913
"__TIMESTAMP__=\"redacted\"",
913-
"__TIME__=\"redacted\""
914+
"__TIME__=\"redacted\"",
915+
"AWESOME"
914916
],
915917
"IPHONEOS_DEPLOYMENT_TARGET": "15.0",
916918
"OTHER_CFLAGS": [
@@ -1239,7 +1241,8 @@
12391241
"DEBUG",
12401242
"__DATE__=\"redacted\"",
12411243
"__TIMESTAMP__=\"redacted\"",
1242-
"__TIME__=\"redacted\""
1244+
"__TIME__=\"redacted\"",
1245+
"AWESOME"
12431246
],
12441247
"IPHONEOS_DEPLOYMENT_TARGET": "15.0",
12451248
"OTHER_CFLAGS": [

test/fixtures/command_line/spec.json

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,7 @@
5858
"DEBUG",
5959
"__DATE__=\"redacted\"",
6060
"__TIMESTAMP__=\"redacted\"",
61-
"__TIME__=\"redacted\"",
62-
"SECRET_3=\\\"Hello\\\"",
63-
"SECRET_2=\\\"World!\\\""
61+
"__TIME__=\"redacted\""
6462
],
6563
"MACOSX_DEPLOYMENT_TARGET": "11.0",
6664
"OTHER_CFLAGS": [
@@ -646,9 +644,7 @@
646644
"DEBUG",
647645
"__DATE__=\"redacted\"",
648646
"__TIMESTAMP__=\"redacted\"",
649-
"__TIME__=\"redacted\"",
650-
"SECRET_3=\\\"Hello\\\"",
651-
"SECRET_2=\\\"World!\\\""
647+
"__TIME__=\"redacted\""
652648
],
653649
"MACOSX_DEPLOYMENT_TARGET": "12.0",
654650
"OTHER_CFLAGS": [

tools/generator/src/Generator+ProcessTargetMerges.swift

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,12 @@ exist
5555
merged.isSwift = merging.isSwift
5656

5757
// Merge build settings
58-
merged.buildSettings["PRODUCT_MODULE_NAME"]
59-
= merging.buildSettings["PRODUCT_MODULE_NAME"]
58+
merged.buildSettings["PRODUCT_MODULE_NAME"] =
59+
merging.buildSettings["PRODUCT_MODULE_NAME"]
60+
merged.buildSettings["GCC_PREPROCESSOR_DEFINITIONS"] =
61+
merging.buildSettings["GCC_PREPROCESSOR_DEFINITIONS"]
62+
merged.buildSettings["SWIFT_ACTIVE_COMPILATION_CONDITIONS"] =
63+
merging.buildSettings["SWIFT_ACTIVE_COMPILATION_CONDITIONS"]
6064
merged.buildSettings.merge(merging.buildSettings) { l, _ in l }
6165

6266
// Update search paths

xcodeproj/internal/providers.bzl

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,6 @@ target_type = struct(
5757
XcodeProjInfo = provider(
5858
"Provides information needed to generate an Xcode project.",
5959
fields = {
60-
"defines": """\
61-
A value returned from `_process_defines()` that contains the defines set by
62-
this target that should be propagated to dependent targets.
63-
""",
6460
"dependencies": """\
6561
A `list` of target ids (see the `target` `struct`) that this target directly
6662
depends on.

xcodeproj/internal/target.bzl

Lines changed: 26 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,6 @@ def _product_to_dto(product):
163163
def _processed_target(
164164
*,
165165
attrs_info,
166-
defines,
167166
dependencies,
168167
inputs,
169168
linker_inputs,
@@ -178,7 +177,6 @@ def _processed_target(
178177
179178
Args:
180179
attrs_info: The `InputFileAttributesInfo` for the target.
181-
defines: The value returned from `_process_defines()`.
182180
dependencies: A `list` of target ids of direct dependencies of this
183181
target.
184182
inputs: A value as returned from `input_files.collect()` that will
@@ -202,7 +200,6 @@ def _processed_target(
202200
"""
203201
return struct(
204202
attrs_info = attrs_info,
205-
defines = defines,
206203
dependencies = dependencies,
207204
inputs = inputs,
208205
linker_inputs = linker_inputs,
@@ -600,21 +597,18 @@ The xcodeproj rule requires {} rules to have a single library dep. {} has {}.\
600597
transitive_infos = transitive_infos,
601598
)
602599

600+
cc_info = target[CcInfo] if CcInfo in target else None
601+
_process_defines(
602+
cc_info = cc_info,
603+
build_settings = build_settings,
604+
)
603605
search_paths = _process_search_paths(
604-
cc_info = target[CcInfo] if CcInfo in target else None,
606+
cc_info = cc_info,
605607
opts_search_paths = opts_search_paths,
606608
)
607609

608610
return _processed_target(
609611
attrs_info = attrs_info,
610-
defines = _process_defines(
611-
attrs_info = attrs_info,
612-
is_swift = is_swift,
613-
defines = getattr(ctx.rule.attr, "defines", []),
614-
local_defines = getattr(ctx.rule.attr, "local_defines", []),
615-
transitive_infos = transitive_infos,
616-
build_settings = build_settings,
617-
),
618612
dependencies = dependencies,
619613
inputs = inputs,
620614
linker_inputs = linker_inputs,
@@ -766,21 +760,18 @@ def _process_library_target(*, ctx, target, transitive_infos):
766760
transitive_infos = transitive_infos,
767761
)
768762

763+
cc_info = target[CcInfo] if CcInfo in target else None
764+
_process_defines(
765+
cc_info = cc_info,
766+
build_settings = build_settings,
767+
)
769768
search_paths = _process_search_paths(
770-
cc_info = target[CcInfo] if CcInfo in target else None,
769+
cc_info = cc_info,
771770
opts_search_paths = opts_search_paths,
772771
)
773772

774773
return _processed_target(
775774
attrs_info = attrs_info,
776-
defines = _process_defines(
777-
attrs_info = attrs_info,
778-
is_swift = SwiftInfo in target,
779-
defines = getattr(ctx.rule.attr, "defines", []),
780-
local_defines = getattr(ctx.rule.attr, "local_defines", []),
781-
transitive_infos = transitive_infos,
782-
build_settings = build_settings,
783-
),
784775
dependencies = dependencies,
785776
inputs = inputs,
786777
linker_inputs = linker_inputs,
@@ -912,14 +903,6 @@ def _process_resource_target(*, ctx, target, transitive_infos):
912903

913904
return _processed_target(
914905
attrs_info = attrs_info,
915-
defines = _process_defines(
916-
attrs_info = attrs_info,
917-
is_swift = False,
918-
defines = [],
919-
local_defines = [],
920-
transitive_infos = transitive_infos,
921-
build_settings = build_settings,
922-
),
923906
dependencies = dependencies,
924907
inputs = inputs,
925908
linker_inputs = linker_inputs,
@@ -994,14 +977,6 @@ def _process_non_xcode_target(*, ctx, target, transitive_infos):
994977

995978
return _processed_target(
996979
attrs_info = attrs_info,
997-
defines = _process_defines(
998-
attrs_info = attrs_info,
999-
is_swift = SwiftInfo in target,
1000-
defines = getattr(ctx.rule.attr, "defines", []),
1001-
local_defines = getattr(ctx.rule.attr, "local_defines", []),
1002-
transitive_infos = transitive_infos,
1003-
build_settings = None,
1004-
),
1005980
dependencies = _process_dependencies(
1006981
attrs_info = attrs_info,
1007982
transitive_infos = transitive_infos,
@@ -1052,7 +1027,6 @@ def _should_skip_target(*, ctx, target):
10521027

10531028
def _target_info_fields(
10541029
*,
1055-
defines,
10561030
dependencies,
10571031
inputs,
10581032
linker_inputs,
@@ -1069,7 +1043,6 @@ def _target_info_fields(
10691043
This should be merged with other fields to fully create an `XcodeProjInfo`.
10701044
10711045
Args:
1072-
defines: Maps to `XcodeProjInfo.defines`.
10731046
dependencies: Maps to the `XcodeProjInfo.dependencies` field.
10741047
inputs: Maps to the `XcodeProjInfo.inputs` field.
10751048
linker_inputs: Maps to the `XcodeProjInfo.linker_inputs` field.
@@ -1087,7 +1060,6 @@ def _target_info_fields(
10871060
Returns:
10881061
A `dict` containing the following fields:
10891062
1090-
* `defines`
10911063
* `dependencies`
10921064
* `generated_inputs`
10931065
* `inputs`
@@ -1102,7 +1074,6 @@ def _target_info_fields(
11021074
* `xcode_targets`
11031075
"""
11041076
return {
1105-
"defines": defines,
11061077
"dependencies": dependencies,
11071078
"inputs": inputs,
11081079
"linker_inputs": linker_inputs,
@@ -1132,14 +1103,6 @@ def _skip_target(*, target, transitive_infos):
11321103
`transitive_infos`.
11331104
"""
11341105
return _target_info_fields(
1135-
defines = _process_defines(
1136-
attrs_info = None,
1137-
is_swift = False,
1138-
defines = [],
1139-
local_defines = [],
1140-
transitive_infos = transitive_infos,
1141-
build_settings = None,
1142-
),
11431106
dependencies = _process_dependencies(
11441107
attrs_info = None,
11451108
transitive_infos = transitive_infos,
@@ -1204,28 +1167,9 @@ def _process_dependencies(*, attrs_info, transitive_infos):
12041167

12051168
def _process_defines(
12061169
*,
1207-
attrs_info,
1208-
is_swift,
1209-
defines,
1210-
local_defines,
1211-
transitive_infos,
1170+
cc_info,
12121171
build_settings):
1213-
transitive_cc_defines = []
1214-
for attr, info in transitive_infos:
1215-
if (attrs_info and
1216-
attrs_info.xcode_targets.get(attr) != info.target_type):
1217-
continue
1218-
transitive_defines = info.defines
1219-
transitive_cc_defines.extend(transitive_defines.cc_defines)
1220-
1221-
# We only want to use this target's defines if it's a Swift target
1222-
if is_swift:
1223-
cc_defines = transitive_cc_defines
1224-
else:
1225-
transitive_cc_defines.extend(defines)
1226-
cc_defines = transitive_cc_defines + local_defines
1227-
1228-
if build_settings:
1172+
if cc_info and build_settings != None:
12291173
# We don't set `SWIFT_ACTIVE_COMPILATION_CONDITIONS` because the way we
12301174
# process Swift compile options already accounts for `defines`
12311175

@@ -1241,20 +1185,27 @@ def _process_defines(
12411185
# becomes an issue in practice, we can refactor `process_copts` to
12421186
# support this better.
12431187

1188+
defines = depset(
1189+
transitive = [
1190+
cc_info.compilation_context.defines,
1191+
cc_info.compilation_context.local_defines,
1192+
],
1193+
)
1194+
escaped_defines = [
1195+
define.replace("\\", "\\\\").replace('"', '\\"')
1196+
for define in defines.to_list()
1197+
]
1198+
12441199
setting = build_settings.get(
12451200
"GCC_PREPROCESSOR_DEFINITIONS",
12461201
[],
1247-
) + cc_defines
1202+
) + escaped_defines
12481203

12491204
# Remove duplicates
12501205
setting = reversed(uniq(reversed(setting)))
12511206

12521207
set_if_true(build_settings, "GCC_PREPROCESSOR_DEFINITIONS", setting)
12531208

1254-
return struct(
1255-
cc_defines = transitive_cc_defines,
1256-
)
1257-
12581209
# TODO: Refactor this into a search_paths module
12591210
def _process_search_paths(
12601211
*,
@@ -1437,7 +1388,6 @@ def _process_target(*, ctx, target, transitive_infos):
14371388
)
14381389

14391390
return _target_info_fields(
1440-
defines = processed_target.defines,
14411391
dependencies = processed_target.dependencies,
14421392
inputs = processed_target.inputs,
14431393
linker_inputs = processed_target.linker_inputs,

0 commit comments

Comments
 (0)