Skip to content

Commit

Permalink
Remove use of rfind in _modern_get_unprocessed_cc_compiler_opts (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
brentleyjones authored Apr 26, 2023
1 parent 4ddee48 commit 48ac5ab
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 62 deletions.
21 changes: 11 additions & 10 deletions xcodeproj/internal/input_files.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ load(":linker_input_files.bzl", "linker_input_files")
load(
":memory_efficiency.bzl",
"EMPTY_DEPSET",
"EMPTY_DICT",
"EMPTY_LIST",
"memory_efficient_depset",
)
Expand Down Expand Up @@ -165,8 +166,8 @@ def _collect_input_files(
attribute.
"""
entitlements = []
c_srcs = []
cxx_srcs = []
c_sources = {}
cxx_sources = {}
hdrs = []
non_arc_srcs = []
pch = []
Expand All @@ -185,18 +186,18 @@ def _collect_input_files(
srcs.append(file)
extension = file.extension
if extension in C_EXTENSIONS:
c_srcs.append(file)
c_sources[file.path] = None
elif extension in CXX_EXTENSIONS:
cxx_srcs.append(file)
cxx_sources[file.path] = None

# buildifier: disable=uninitialized
def _handle_non_arc_srcs_file(file):
non_arc_srcs.append(file)
extension = file.extension
if extension in C_EXTENSIONS:
c_srcs.append(file)
c_sources[file.path] = None
elif extension in CXX_EXTENSIONS:
cxx_srcs.append(file)
cxx_sources[file.path] = None

# buildifier: disable=uninitialized
def _handle_hdrs_file(file):
Expand Down Expand Up @@ -687,8 +688,8 @@ def _collect_input_files(
entitlements = entitlements[0] if entitlements else None,
folder_resources = folder_resources,
generated = generated_depset,
has_c_sources = bool(c_srcs),
has_cxx_sources = bool(cxx_srcs),
c_sources = c_sources,
cxx_sources = cxx_sources,
hdrs = hdrs,
indexstores = indexstores_depset,
indexstores_output_group_name = indexstores_output_group_name,
Expand Down Expand Up @@ -765,8 +766,8 @@ def _from_resource_bundle(bundle):
entitlements = None,
folder_resources = depset(bundle.folder_resources),
generated = EMPTY_DEPSET,
has_c_sources = False,
has_cxx_sources = False,
c_sources = EMPTY_DICT,
cxx_sources = EMPTY_DICT,
hdrs = EMPTY_LIST,
indexstores = EMPTY_DEPSET,
indexstores_output_group_name = None,
Expand Down
4 changes: 2 additions & 2 deletions xcodeproj/internal/library_targets.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,8 @@ def process_library_target(
) = process_opts(
ctx = ctx,
build_mode = build_mode,
has_c_sources = target_inputs.has_c_sources,
has_cxx_sources = target_inputs.has_cxx_sources,
c_sources = target_inputs.c_sources,
cxx_sources = target_inputs.cxx_sources,
target = target,
implementation_compilation_context = implementation_compilation_context,
package_bin_dir = package_bin_dir,
Expand Down
1 change: 1 addition & 0 deletions xcodeproj/internal/memory_efficiency.bzl
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"Commonly used constant expressions. Used to avoid creating new objects in memory."

EMPTY_DICT = {}
EMPTY_DEPSET = depset()
EMPTY_LIST = []
EMPTY_TUPLE = tuple()
Expand Down
77 changes: 31 additions & 46 deletions xcodeproj/internal/opts.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

load("@bazel_tools//tools/cpp:toolchain_utils.bzl", "find_cpp_toolchain")
load(":files.bzl", "is_relative_path")
load(":input_files.bzl", "CXX_EXTENSIONS", "C_EXTENSIONS")
load(":memory_efficiency.bzl", "EMPTY_LIST")

# Swift compiler flags that we don't want to propagate to Xcode.
Expand Down Expand Up @@ -74,20 +73,6 @@ _CC_COMPILE_ACTIONS = {
"ObjcCompile": None,
}

def _is_c_file(filename):
last_dot_in_basename = filename.rfind(".")
if last_dot_in_basename <= 0:
return False
ext_distance_from_end = len(filename) - last_dot_in_basename - 1
return filename[-ext_distance_from_end:] in C_EXTENSIONS

def _is_cxx_file(filename):
last_dot_in_basename = filename.rfind(".")
if last_dot_in_basename <= 0:
return False
ext_distance_from_end = len(filename) - last_dot_in_basename - 1
return filename[-ext_distance_from_end:] in CXX_EXTENSIONS

# Defensive list of features that can appear in the CC toolchain, but that we
# definitely don't want to enable (meaning we don't want them to contribute
# command line flags).
Expand All @@ -104,15 +89,15 @@ _UNSUPPORTED_CC_FEATURES = [
def _legacy_get_unprocessed_cc_compiler_opts(
*,
ctx,
has_c_sources,
has_cxx_sources,
c_sources,
cxx_sources,
has_swift_opts,
target,
implementation_compilation_context):
if (has_swift_opts or
not implementation_compilation_context or
not (has_c_sources or has_cxx_sources)):
return ([], [], [], [])
not (c_sources or cxx_sources)):
return (EMPTY_LIST, EMPTY_LIST, EMPTY_LIST, EMPTY_LIST)

cc_toolchain = find_cpp_toolchain(ctx)

Expand Down Expand Up @@ -168,7 +153,7 @@ def _legacy_get_unprocessed_cc_compiler_opts(

cpp = ctx.fragments.cpp

if has_c_sources:
if c_sources:
base_copts = cc_common.get_memory_inefficient_command_line(
feature_configuration = feature_configuration,
action_name = "objc-compile" if is_objc else "c-compile",
Expand All @@ -183,7 +168,7 @@ def _legacy_get_unprocessed_cc_compiler_opts(
conlyopts = []
conly_args = []

if has_cxx_sources:
if cxx_sources:
base_cxxopts = cc_common.get_memory_inefficient_command_line(
feature_configuration = feature_configuration,
action_name = "objc++-compile" if is_objc else "c++-compile",
Expand All @@ -204,8 +189,8 @@ def _modern_get_unprocessed_cc_compiler_opts(
*,
# buildifier: disable=unused-variable
ctx,
has_c_sources,
has_cxx_sources,
c_sources,
cxx_sources,
# buildifier: disable=unused-variable
has_swift_opts,
target,
Expand All @@ -216,7 +201,7 @@ def _modern_get_unprocessed_cc_compiler_opts(
cxxopts = EMPTY_LIST
cxx_args = EMPTY_LIST

if not has_c_sources and not has_cxx_sources:
if not c_sources and not cxx_sources:
return (conlyopts, conly_args, cxxopts, cxx_args)

for action in target.actions:
Expand All @@ -226,19 +211,19 @@ def _modern_get_unprocessed_cc_compiler_opts(
previous_arg = None
for arg in action.argv:
if previous_arg == "-c":
if not conly_args and _is_c_file(arg):
if not conly_args and arg in c_sources:
# First argument is "wrapped_clang"
conlyopts = action.argv[1:]
conly_args = action.args
elif not cxx_args and _is_cxx_file(arg):
elif not cxx_args and arg in cxx_sources:
# First argument is "wrapped_clang_pp"
cxxopts = action.argv[1:]
cxx_args = action.args
break
previous_arg = arg

if ((not has_c_sources or conly_args) and
(not has_cxx_sources or cxx_args)):
if ((not c_sources or conly_args) and
(not cxx_sources or cxx_args)):
# We've found all the args we are looking for
break

Expand All @@ -253,17 +238,17 @@ def _get_unprocessed_compiler_opts(
*,
ctx,
build_mode,
has_c_sources,
has_cxx_sources,
c_sources,
cxx_sources,
target,
implementation_compilation_context):
"""Returns the unprocessed compiler options for the given target.
Args:
ctx: The aspect context.
build_mode: See `xcodeproj.build_mode`.
has_c_sources: `True` if `target` has C sources.
has_cxx_sources: `True` if `target` has C++ sources.
c_sources: A `dict` of C source paths.
cxx_sources: A `dict` of C++ source paths.
target: The `Target` that the compiler options will be retrieved from.
implementation_compilation_context: The implementation deps aware
`CcCompilationContext` for `target`.
Expand All @@ -285,8 +270,8 @@ def _get_unprocessed_compiler_opts(

conlyopts, conly_args, cxxopts, cxxargs = _get_unprocessed_cc_compiler_opts(
ctx = ctx,
has_c_sources = has_c_sources,
has_cxx_sources = has_cxx_sources,
c_sources = c_sources,
cxx_sources = cxx_sources,
has_swift_opts = bool(swiftcopts),
target = target,
implementation_compilation_context = implementation_compilation_context,
Expand Down Expand Up @@ -824,8 +809,8 @@ def _process_target_compiler_opts(
*,
ctx,
build_mode,
has_c_sources,
has_cxx_sources,
c_sources,
cxx_sources,
target,
implementation_compilation_context,
package_bin_dir,
Expand All @@ -835,8 +820,8 @@ def _process_target_compiler_opts(
Args:
ctx: The aspect context.
build_mode: See `xcodeproj.build_mode`.
has_c_sources: `True` if `target` has C sources.
has_cxx_sources: `True` if `target` has C++ sources.
c_sources: A `dict` of C source paths.
cxx_sources: A `dict` of C++ source paths.
target: The `Target` that the compiler options will be retrieved from.
implementation_compilation_context: The implementation deps aware
`CcCompilationContext` for `target`.
Expand Down Expand Up @@ -866,8 +851,8 @@ def _process_target_compiler_opts(
) = _get_unprocessed_compiler_opts(
ctx = ctx,
build_mode = build_mode,
has_c_sources = has_c_sources,
has_cxx_sources = has_cxx_sources,
c_sources = c_sources,
cxx_sources = cxx_sources,
target = target,
implementation_compilation_context = implementation_compilation_context,
)
Expand Down Expand Up @@ -931,8 +916,8 @@ def process_opts(
*,
ctx,
build_mode,
has_c_sources,
has_cxx_sources,
c_sources,
cxx_sources,
target,
implementation_compilation_context,
package_bin_dir,
Expand All @@ -942,8 +927,8 @@ def process_opts(
Args:
ctx: The aspect context.
build_mode: See `xcodeproj.build_mode`.
has_c_sources: `True` if `target` has C sources.
has_cxx_sources: `True` if `target` has C++ sources.
c_sources: A `dict` of C source paths.
cxx_sources: A `dict` of C++ source paths.
target: The `Target` that the compiler and linker options will be
retrieved from.
implementation_compilation_context: The implementation deps aware
Expand All @@ -968,8 +953,8 @@ def process_opts(
return _process_target_compiler_opts(
ctx = ctx,
build_mode = build_mode,
has_c_sources = has_c_sources,
has_cxx_sources = has_cxx_sources,
c_sources = c_sources,
cxx_sources = cxx_sources,
target = target,
implementation_compilation_context = implementation_compilation_context,
package_bin_dir = package_bin_dir,
Expand Down
4 changes: 2 additions & 2 deletions xcodeproj/internal/top_level_targets.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -434,8 +434,8 @@ def process_top_level_target(
) = process_opts(
ctx = ctx,
build_mode = build_mode,
has_c_sources = target_inputs.has_c_sources,
has_cxx_sources = target_inputs.has_cxx_sources,
c_sources = target_inputs.c_sources,
cxx_sources = target_inputs.cxx_sources,
target = target,
implementation_compilation_context = implementation_compilation_context,
package_bin_dir = package_bin_dir,
Expand Down
4 changes: 2 additions & 2 deletions xcodeproj/internal/xcode_targets.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -189,8 +189,8 @@ def _to_xcode_target_inputs(inputs):
srcs = tuple(inputs.srcs),
non_arc_srcs = tuple(inputs.non_arc_srcs),
hdrs = tuple(inputs.hdrs),
has_c_sources = inputs.has_c_sources,
has_cxx_sources = inputs.has_cxx_sources,
has_c_sources = bool(inputs.c_sources),
has_cxx_sources = bool(inputs.cxx_sources),
resources = inputs.resources,
folder_resources = inputs.folder_resources,
resource_bundle_dependencies = inputs.resource_bundle_dependencies,
Expand Down

0 comments on commit 48ac5ab

Please sign in to comment.