Skip to content

Commit

Permalink
Add a partial Starlark implementation of common.copts_filter.
Browse files Browse the repository at this point in the history
Variable expansion and some checks will be done in Starlark, however CoptsFilter constructor will be exposed to construct an object which is then passed to CcCompilationHelper.

PiperOrigin-RevId: 507725876
Change-Id: Ifae7fc54db6e9ea20b1c11b0b47f8ffb078e7456
  • Loading branch information
buildbreaker2021 authored and copybara-github committed Feb 7, 2023
1 parent 39c7bd5 commit 7a9a2f8
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 8 deletions.
3 changes: 0 additions & 3 deletions src/main/java/com/google/devtools/build/lib/rules/cpp/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ java_library(
"//src/main/java/com/google/devtools/build/lib/analysis:make_variable_supplier",
"//src/main/java/com/google/devtools/build/lib/analysis:package_specification_provider",
"//src/main/java/com/google/devtools/build/lib/analysis:platform_configuration",
"//src/main/java/com/google/devtools/build/lib/analysis:resolved_toolchain_context",
"//src/main/java/com/google/devtools/build/lib/analysis:rule_definition_environment",
"//src/main/java/com/google/devtools/build/lib/analysis:starlark/args",
"//src/main/java/com/google/devtools/build/lib/analysis:starlark/starlark_api_provider",
Expand Down Expand Up @@ -95,14 +94,12 @@ java_library(
"//src/main/java/com/google/devtools/build/lib/rules/apple",
"//src/main/java/com/google/devtools/build/lib/shell",
"//src/main/java/com/google/devtools/build/lib/skyframe:action_execution_value",
"//src/main/java/com/google/devtools/build/lib/skyframe:configured_target_and_data",
"//src/main/java/com/google/devtools/build/lib/skyframe:tree_artifact_value",
"//src/main/java/com/google/devtools/build/lib/skyframe/serialization/autocodec",
"//src/main/java/com/google/devtools/build/lib/skyframe/serialization/autocodec:serialization-constant",
"//src/main/java/com/google/devtools/build/lib/starlarkbuildapi",
"//src/main/java/com/google/devtools/build/lib/starlarkbuildapi/core",
"//src/main/java/com/google/devtools/build/lib/starlarkbuildapi/cpp",
"//src/main/java/com/google/devtools/build/lib/starlarkbuildapi/go",
"//src/main/java/com/google/devtools/build/lib/starlarkbuildapi/platform",
"//src/main/java/com/google/devtools/build/lib/util",
"//src/main/java/com/google/devtools/build/lib/util:detailed_exit_code",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,7 @@ public ImmutableMap<String, String> getAllMakeVariables() throws ExpansionExcept
}

/** A filter that removes copts from a c++ compile action according to a nocopts regex. */
public static final class CoptsFilter implements StarlarkValue {
public static final class CoptsFilter {
private final Pattern noCoptsPattern;
private final boolean allPasses;

Expand Down Expand Up @@ -469,7 +469,6 @@ public boolean passesFilter(String flag) {
}

/** Returns copts filter built from the make variable expanded nocopts attribute. */
@StarlarkMethod(name = "copts_filter", structField = true, documented = false)
public CoptsFilter getCoptsFilter() {
return getCoptsFilter(ruleContext);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@
import com.google.devtools.build.lib.view.config.crosstool.CrosstoolConfig.CToolchain;
import com.google.errorprone.annotations.FormatMethod;
import java.util.List;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;
import javax.annotation.Nullable;
import net.starlark.java.annot.Param;
import net.starlark.java.annot.ParamType;
Expand Down Expand Up @@ -2280,6 +2282,7 @@ private static boolean checkObjectsBound(Object... objects) {
documented = false,
positional = false,
named = true,
allowedTypes = {@ParamType(type = String.class), @ParamType(type = NoneType.class)},
defaultValue = "unbound"),
@Param(
name = "separate_module_headers",
Expand Down Expand Up @@ -2376,7 +2379,18 @@ public Tuple compile(
ImmutableList<CppModuleMap> additionalModuleMaps =
asClassImmutableList(additionalModuleMapsNoneable);

CoptsFilter coptsFilter = convertFromNoneable(coptsFilterObject, /* defaultValue= */ null);
String coptsFilterRegex = convertFromNoneable(coptsFilterObject, /* defaultValue= */ null);
CoptsFilter coptsFilter = null;
if (Strings.isNullOrEmpty(coptsFilterRegex)) {
coptsFilter = CoptsFilter.alwaysPasses();
} else {
try {
coptsFilter = CoptsFilter.fromRegex(Pattern.compile(coptsFilterRegex));
} catch (PatternSyntaxException e) {
throw Starlark.errorf(
"invalid regular expression '%s': %s", coptsFilterRegex, e.getMessage());
}
}

Object textualHeadersObject =
asClassImmutableListOrNestedSet(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -786,6 +786,12 @@ public boolean disableNoCopts() {
return cppOptions.disableNoCopts;
}

@Override
public boolean disableNocoptsStarlark(StarlarkThread thread) throws EvalException {
CcModule.checkPrivateStarlarkificationAllowlist(thread);
return disableNoCopts();
}

public boolean loadCcRulesFromBzl() {
return cppOptions.loadCcRulesFromBzl;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,4 +203,7 @@ boolean fissionActiveForCurrentCompilationModeStarlark(StarlarkThread thread)

@StarlarkMethod(name = "share_native_deps", documented = false, useStarlarkThread = true)
boolean shareNativeDepsStarlark(StarlarkThread thread) throws EvalException;

@StarlarkMethod(name = "disable_nocopts", documented = false, useStarlarkThread = true)
boolean disableNocoptsStarlark(StarlarkThread thread) throws EvalException;
}
2 changes: 1 addition & 1 deletion src/main/starlark/builtins_bzl/common/cc/cc_binary.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -651,7 +651,7 @@ def cc_binary_impl(ctx, additional_linkopts):
system_includes = cc_helper.system_include_dirs(ctx, additional_make_variable_substitutions),
private_hdrs = cc_helper.get_private_hdrs(ctx),
public_hdrs = cc_helper.get_public_hdrs(ctx),
copts_filter = common.copts_filter,
copts_filter = cc_helper.copts_filter(ctx, additional_make_variable_substitutions),
srcs = cc_helper.get_srcs(ctx),
compilation_contexts = compilation_context_deps,
grep_includes = cc_helper.grep_includes_executable(ctx.attr._grep_includes),
Expand Down
14 changes: 14 additions & 0 deletions src/main/starlark/builtins_bzl/common/cc/cc_helper.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -1193,6 +1193,19 @@ def _linker_scripts(ctx):
result.append(f)
return result

def _copts_filter(ctx, additional_make_variable_substitutions):
nocopts = getattr(ctx.attr, "nocopts", None)

if nocopts == None or len(nocopts) == 0:
return nocopts

# Check if nocopts is disabled.
if ctx.fragments.cpp.disable_nocopts():
fail("This attribute was removed. See https://github.com/bazelbuild/bazel/issues/8706 for details.", attr = "nocopts")

# Expand nocopts and create CoptsFilter.
return _expand(ctx, nocopts, additional_make_variable_substitutions)

cc_helper = struct(
merge_cc_debug_contexts = _merge_cc_debug_contexts,
is_code_coverage_enabled = _is_code_coverage_enabled,
Expand Down Expand Up @@ -1252,4 +1265,5 @@ cc_helper = struct(
defines = _defines,
local_defines = _local_defines,
linker_scripts = _linker_scripts,
copts_filter = _copts_filter,
)
2 changes: 1 addition & 1 deletion src/main/starlark/builtins_bzl/common/cc/cc_library.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def _cc_library_impl(ctx):
local_defines = cc_helper.local_defines(ctx, additional_make_variable_substitutions) + cc_helper.get_local_defines_for_runfiles_lookup(ctx),
loose_includes = common.loose_include_dirs,
system_includes = cc_helper.system_include_dirs(ctx, additional_make_variable_substitutions),
copts_filter = common.copts_filter,
copts_filter = cc_helper.copts_filter(ctx, additional_make_variable_substitutions),
purpose = "cc_library-compile",
srcs = cc_helper.get_srcs(ctx),
private_hdrs = cc_helper.get_private_hdrs(ctx),
Expand Down

0 comments on commit 7a9a2f8

Please sign in to comment.