Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle bourne shell tokenization in objc_copts and linkopts when expanding xcconfigs #84

Merged
merged 2 commits into from
Jul 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 16 additions & 12 deletions rules/library/xcconfig.bzl
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
load("//data:xcspecs.bzl", "SETTINGS")
load("@bazel_skylib//lib:types.bzl", "types")
load("@bazel_skylib//lib:shell.bzl", "shell")

_CLANG = "com.apple.compilers.llvm.clang.1_0"
_SWIFT = "com.apple.xcode.tools.swift.compiler"
Expand All @@ -21,7 +22,10 @@ def _unknown_enum_value(option, value, fatal = False):
options = repr(option["Values"]),
))

def _add_copts_from_option(xcspec, option, value, copts, linkopts):
def _id(value):
return value

def _add_copts_from_option(xcspec, option, value, value_escaper, copts, linkopts):
_type = option["Type"]
name = option["Name"]

Expand Down Expand Up @@ -116,7 +120,7 @@ def _add_copts_from_option(xcspec, option, value, copts, linkopts):
))

copts += [
arg.replace("$(value)", v)
arg.replace("$(value)", value_escaper(v))
for v in (value if types.is_list(value) else [value])
for arg in new
]
Expand All @@ -130,23 +134,23 @@ def settings_from_xcconfig(xcconfig):
ibtool_copts = []
linkopts = []

id_map = {
_CLANG: objc_copts,
_SWIFT: swift_copts,
_LD: linkopts,
_MOMC: momc_copts,
_MAPC: mapc_copts,
_IBTOOL: ibtool_copts,
}
identifiers = [
(_CLANG, objc_copts, shell.quote),
(_SWIFT, swift_copts, _id),
(_LD, linkopts, shell.quote),
(_MOMC, momc_copts, _id),
(_MAPC, mapc_copts, _id),
(_IBTOOL, ibtool_copts, _id),
]

for (id, copts) in id_map.items():
for (id, copts, value_escaper) in identifiers:
settings = SETTINGS[id]["Options"]
for (setting, option) in settings.items():
if not setting in xcconfig:
continue

value = xcconfig[setting]
_add_copts_from_option(id, option, value, copts, linkopts)
_add_copts_from_option(id, option, value, value_escaper, copts, linkopts)

return struct(
objc_copts = objc_copts,
Expand Down
2 changes: 2 additions & 0 deletions tests/ios/frameworks/target-xib/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ apple_framework(
name = "TargetXIB",
srcs = glob(["TargetXIB/**/*.swift"]),
data = glob(["TargetXIB/**/*.xib"]),
platforms = {"ios": "11.0"},
visibility = ["//visibility:public"],
)

apple_framework(
name = "TargetXIBTestsLib",
srcs = glob(["TargetXIBTests/**/*.swift"]),
platforms = {"ios": "11.0"},
visibility = ["//visibility:public"],
deps = [":TargetXIB"],
)
Expand Down
1 change: 1 addition & 0 deletions tests/macos/xcconfig/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ apple_framework(
"GCC_PREPROCESSOR_DEFINITIONS": [
"FOO",
"BAR=1",
"ABCD=HAS SO MANY SPACES",
],
},
)
Expand Down
13 changes: 9 additions & 4 deletions tests/macos/xcconfig/tests.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ def xcconfig_unit_test_suite():
assert_xcconfig(
name = "otherwise_supported",
xcconfig = {"ALTERNATE_LINKER": "foo"},
expected = {"linkopts": ["-fuse-ld=foo"]},
expected = {"linkopts": ["-fuse-ld='foo'"]},
),
assert_xcconfig(
name = "empty_string_match",
Expand All @@ -145,12 +145,12 @@ def xcconfig_unit_test_suite():
assert_xcconfig(
name = "command_line_flag",
xcconfig = {"SYSTEM_FRAMEWORK_SEARCH_PATHS": ["foo", "/bar"]},
expected = {"linkopts": ["-iframework", "foo", "-iframework", "/bar"]},
expected = {"linkopts": ["-iframework", "'foo'", "-iframework", "'/bar'"]},
),
assert_xcconfig(
name = "command_line_prefix",
xcconfig = {"CLANG_MACRO_BACKTRACE_LIMIT": "12"},
expected = {"objc_copts": ["-fmacro-backtrace-limit=12"]},
expected = {"objc_copts": ["-fmacro-backtrace-limit='12'"]},
),
assert_xcconfig(
name = "command_line_flag_default_bool",
Expand All @@ -170,7 +170,12 @@ def xcconfig_unit_test_suite():
assert_xcconfig(
name = "option_with_inherited",
xcconfig = {"GCC_PREPROCESSOR_DEFINITIONS": ["$(inherited)", "ABC=1"]},
expected = {"objc_copts": ["-D$(inherited)", "-DABC=1"]},
expected = {"objc_copts": ["-D'$(inherited)'", "-D'ABC=1'"]},
),
assert_xcconfig(
name = "option_with_shell_metacharacters",
xcconfig = {"GCC_PREPROCESSOR_DEFINITIONS": ["DISPLAY_VERSION=1.0.0-beta.1", "SDK_NAME=WHY WOULD YOU ADD SPACES"]},
expected = {"objc_copts": ["-D'DISPLAY_VERSION=1.0.0-beta.1'", "-D'SDK_NAME=WHY WOULD YOU ADD SPACES'"]},
),
# TODO: we should eventually support conditioned vars somehow
assert_xcconfig(
Expand Down