From 48ac5abc76808991d40b1f2bc2f845ceb062dd31 Mon Sep 17 00:00:00 2001 From: Brentley Jones Date: Wed, 26 Apr 2023 11:26:54 -0500 Subject: [PATCH] Remove use of `rfind` in `_modern_get_unprocessed_cc_compiler_opts` (#2116) --- xcodeproj/internal/input_files.bzl | 21 ++++--- xcodeproj/internal/library_targets.bzl | 4 +- xcodeproj/internal/memory_efficiency.bzl | 1 + xcodeproj/internal/opts.bzl | 77 ++++++++++-------------- xcodeproj/internal/top_level_targets.bzl | 4 +- xcodeproj/internal/xcode_targets.bzl | 4 +- 6 files changed, 49 insertions(+), 62 deletions(-) diff --git a/xcodeproj/internal/input_files.bzl b/xcodeproj/internal/input_files.bzl index 7fb8e63196..927b9f9de1 100644 --- a/xcodeproj/internal/input_files.bzl +++ b/xcodeproj/internal/input_files.bzl @@ -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", ) @@ -165,8 +166,8 @@ def _collect_input_files( attribute. """ entitlements = [] - c_srcs = [] - cxx_srcs = [] + c_sources = {} + cxx_sources = {} hdrs = [] non_arc_srcs = [] pch = [] @@ -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): @@ -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, @@ -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, diff --git a/xcodeproj/internal/library_targets.bzl b/xcodeproj/internal/library_targets.bzl index a1b6e5df47..190113b045 100644 --- a/xcodeproj/internal/library_targets.bzl +++ b/xcodeproj/internal/library_targets.bzl @@ -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, diff --git a/xcodeproj/internal/memory_efficiency.bzl b/xcodeproj/internal/memory_efficiency.bzl index ac63da9035..7c6476e40f 100644 --- a/xcodeproj/internal/memory_efficiency.bzl +++ b/xcodeproj/internal/memory_efficiency.bzl @@ -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() diff --git a/xcodeproj/internal/opts.bzl b/xcodeproj/internal/opts.bzl index 222accbd79..308f04a0f9 100644 --- a/xcodeproj/internal/opts.bzl +++ b/xcodeproj/internal/opts.bzl @@ -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. @@ -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). @@ -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) @@ -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", @@ -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", @@ -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, @@ -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: @@ -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 @@ -253,8 +238,8 @@ 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. @@ -262,8 +247,8 @@ def _get_unprocessed_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`. @@ -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, @@ -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, @@ -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`. @@ -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, ) @@ -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, @@ -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 @@ -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, diff --git a/xcodeproj/internal/top_level_targets.bzl b/xcodeproj/internal/top_level_targets.bzl index 2072b4de0a..fe3a1fe121 100644 --- a/xcodeproj/internal/top_level_targets.bzl +++ b/xcodeproj/internal/top_level_targets.bzl @@ -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, diff --git a/xcodeproj/internal/xcode_targets.bzl b/xcodeproj/internal/xcode_targets.bzl index 91786f53d0..aa9d567914 100644 --- a/xcodeproj/internal/xcode_targets.bzl +++ b/xcodeproj/internal/xcode_targets.bzl @@ -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,