Skip to content

Commit

Permalink
Expose C++ action names to Skylark
Browse files Browse the repository at this point in the history
This cl adds Skylark constants allowing users to specify which C++ action they
want for the feature configuration Skylark API.  This is done by exposing a
Skylark file at @bazel_tools//tools/cpp:action_names.bzl.

Skylark api to the C++ toolchain doc:
https://docs.google.com/document/d/1g91BWJITcYw_X-VxsDC0VgUn5E9g0kRBGoBSpoO41gA/edit#.

Progress on bazelbuild#4571.

RELNOTES: None.
PiperOrigin-RevId: 199596778
  • Loading branch information
hlopko authored and George Gensure committed Aug 2, 2018
1 parent 998ff16 commit d3fe943
Show file tree
Hide file tree
Showing 16 changed files with 245 additions and 172 deletions.
29 changes: 14 additions & 15 deletions src/main/java/com/google/devtools/build/lib/rules/cpp/CcCommon.java
Original file line number Diff line number Diff line change
Expand Up @@ -110,17 +110,17 @@ public void collectMetadataArtifacts(Iterable<Artifact> objectFiles,

public static final ImmutableSet<String> ALL_COMPILE_ACTIONS =
ImmutableSet.of(
CppCompileAction.C_COMPILE,
CppCompileAction.CPP_COMPILE,
CppCompileAction.CPP_HEADER_PARSING,
CppCompileAction.CPP_HEADER_PREPROCESSING,
CppCompileAction.CPP_MODULE_COMPILE,
CppCompileAction.CPP_MODULE_CODEGEN,
CppCompileAction.ASSEMBLE,
CppCompileAction.PREPROCESS_ASSEMBLE,
CppCompileAction.CLIF_MATCH,
CppCompileAction.LINKSTAMP_COMPILE,
CppCompileAction.CC_FLAGS_MAKE_VARIABLE_ACTION_NAME);
CppActionNames.C_COMPILE,
CppActionNames.CPP_COMPILE,
CppActionNames.CPP_HEADER_PARSING,
CppActionNames.CPP_HEADER_PREPROCESSING,
CppActionNames.CPP_MODULE_COMPILE,
CppActionNames.CPP_MODULE_CODEGEN,
CppActionNames.ASSEMBLE,
CppActionNames.PREPROCESS_ASSEMBLE,
CppActionNames.CLIF_MATCH,
CppActionNames.LINKSTAMP_COMPILE,
CppActionNames.CC_FLAGS_MAKE_VARIABLE);

public static final ImmutableSet<String> ALL_LINK_ACTIONS =
ImmutableSet.of(
Expand All @@ -132,7 +132,7 @@ public void collectMetadataArtifacts(Iterable<Artifact> objectFiles,
ImmutableSet.of(Link.LinkTargetType.STATIC_LIBRARY.getActionName());

public static final ImmutableSet<String> ALL_OTHER_ACTIONS =
ImmutableSet.of(CppCompileAction.STRIP_ACTION_NAME);
ImmutableSet.of(CppActionNames.STRIP);

/** Action configs we request to enable. */
public static final ImmutableSet<String> DEFAULT_ACTION_CONFIGS =
Expand Down Expand Up @@ -971,8 +971,7 @@ public static String computeCcFlags(RuleContext ruleContext, TransitiveInfoColle
(CcToolchainProvider) toolchain.get(ToolchainInfo.PROVIDER);
FeatureConfiguration featureConfiguration =
CcCommon.configureFeaturesOrReportRuleError(ruleContext, toolchainProvider);
if (!featureConfiguration.actionIsConfigured(
CppCompileAction.CC_FLAGS_MAKE_VARIABLE_ACTION_NAME)) {
if (!featureConfiguration.actionIsConfigured(CppActionNames.CC_FLAGS_MAKE_VARIABLE)) {
return null;
}

Expand All @@ -981,7 +980,7 @@ public static String computeCcFlags(RuleContext ruleContext, TransitiveInfoColle
Joiner.on(" ")
.join(
featureConfiguration.getCommandLine(
CppCompileAction.CC_FLAGS_MAKE_VARIABLE_ACTION_NAME, buildVariables));
CppActionNames.CC_FLAGS_MAKE_VARIABLE, buildVariables));
String oldCcFlags = "";
TemplateVariableInfo templateVariableInfo =
toolchain.get(TemplateVariableInfo.PROVIDER);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
// Copyright 2018 The Bazel Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.google.devtools.build.lib.rules.cpp;

/** Class holding constants for all C++ action names */
public class CppActionNames {

/** A string constant used to compute CC_FLAGS make variable value */
public static final String CC_FLAGS_MAKE_VARIABLE = "cc-flags-make-variable";
/** A string constant for the strip action name. */
public static final String STRIP = "strip";
/** A string constant for the linkstamp-compile action. */
public static final String LINKSTAMP_COMPILE = "linkstamp-compile";
/** A string constant for the c compilation action. */
public static final String C_COMPILE = "c-compile";
/** A string constant for the c++ compilation action. */
public static final String CPP_COMPILE = "c++-compile";
/** A string constant for the c++ module compile action. */
public static final String CPP_MODULE_CODEGEN = "c++-module-codegen";
/** A string constant for the objc compilation action. */
public static final String OBJC_COMPILE = "objc-compile";
/** A string constant for the objc++ compile action. */
public static final String OBJCPP_COMPILE = "objc++-compile";
/** A string constant for the c++ header parsing. */
public static final String CPP_HEADER_PARSING = "c++-header-parsing";
/** A string constant for the c++ header preprocessing. */
public static final String CPP_HEADER_PREPROCESSING = "c++-header-preprocessing";
/**
* A string constant for the c++ module compilation action. Note: currently we don't support C
* module compilation.
*/
public static final String CPP_MODULE_COMPILE = "c++-module-compile";
/** A string constant for the assembler actions. */
public static final String ASSEMBLE = "assemble";

public static final String PREPROCESS_ASSEMBLE = "preprocess-assemble";
/**
* A string constant for the clif actions. Bazel enables different features of the toolchain based
* on the name of the action. This name enables the clif_matcher feature, which switches the
* "compiler" to the clif_matcher and adds some additional arguments as described in the CROSSTOOL
* file.
*/
public static final String CLIF_MATCH = "clif-match";

/** Name of the action producing static library. */
public static final String CPP_LINK_STATIC_LIBRARY = "c++-link-static-library";
/** Name of the action producing dynamic library from cc_library. */
public static final String CPP_LINK_NODEPS_DYNAMIC_LIBRARY = "c++-link-nodeps-dynamic-library";
/** Name of the action producing dynamic library from cc_binary. */
public static final String CPP_LINK_DYNAMIC_LIBRARY = "c++-link-dynamic-library";
/** Name of the action producing executable binary. */
public static final String CPP_LINK_EXECUTABLE = "c++-link-executable";
/** Name of the objc action producing static library */
public static final String OBJC_ARCHIVE = "objc-archive";
/** Name of the objc action producing dynamic library */
public static final String OBJC_FULLY_LINK = "objc-fully-link";
/** Name of the objc action producing objc executable binary */
public static final String OBJC_EXECUTABLE = "objc-executable";
/** Name of the objc action producing objc++ executable binary */
public static final String OBJCPP_EXECUTABLE = "objc++-executable";

public static final String LTO_INDEXING = "lto-indexing";
public static final String LTO_BACKEND = "lto-backend";
}
Original file line number Diff line number Diff line change
Expand Up @@ -93,69 +93,6 @@ public class CppCompileAction extends AbstractAction
private static final int VALIDATION_DEBUG = 0; // 0==none, 1==warns/errors, 2==all
private static final boolean VALIDATION_DEBUG_WARN = VALIDATION_DEBUG >= 1;

/** A string constant used to compute CC_FLAGS make variable value */
public static final java.lang.String CC_FLAGS_MAKE_VARIABLE_ACTION_NAME =
"cc-flags-make-variable";

/** A string constant for the strip action name. */
public static final String STRIP_ACTION_NAME = "strip";

/** A string constant for the linkstamp-compile action. */
public static final String LINKSTAMP_COMPILE = "linkstamp-compile";

/**
* A string constant for the c compilation action.
*/
public static final String C_COMPILE = "c-compile";

/**
* A string constant for the c++ compilation action.
*/
public static final String CPP_COMPILE = "c++-compile";

/** A string constant for the c++ module compile action. */
public static final String CPP_MODULE_CODEGEN = "c++-module-codegen";

/**
* A string constant for the objc compilation action.
*/
public static final String OBJC_COMPILE = "objc-compile";

/**
* A string constant for the objc++ compile action.
*/
public static final String OBJCPP_COMPILE = "objc++-compile";

/**
* A string constant for the c++ header parsing.
*/
public static final String CPP_HEADER_PARSING = "c++-header-parsing";

/**
* A string constant for the c++ header preprocessing.
*/
public static final String CPP_HEADER_PREPROCESSING = "c++-header-preprocessing";

/**
* A string constant for the c++ module compilation action.
* Note: currently we don't support C module compilation.
*/
public static final String CPP_MODULE_COMPILE = "c++-module-compile";

/**
* A string constant for the assembler actions.
*/
public static final String ASSEMBLE = "assemble";
public static final String PREPROCESS_ASSEMBLE = "preprocess-assemble";

/**
* A string constant for the clif actions. Bazel enables different features of the toolchain based
* on the name of the action. This name enables the clif_matcher feature, which switches the
* "compiler" to the clif_matcher and adds some additional arguments as described in the CROSSTOOL
* file.
*/
public static final String CLIF_MATCH = "clif-match";

protected final Artifact outputFile;
private final Artifact sourceFile;
private final NestedSet<Artifact> mandatoryInputs;
Expand Down Expand Up @@ -1327,11 +1264,11 @@ public Iterable<Artifact> getInputFilesForExtraAction(
@Override
public String getMnemonic() {
switch (actionName) {
case OBJC_COMPILE:
case OBJCPP_COMPILE:
case CppActionNames.OBJC_COMPILE:
case CppActionNames.OBJCPP_COMPILE:
return "ObjcCompile";

case LINKSTAMP_COMPILE:
case CppActionNames.LINKSTAMP_COMPILE:
// When compiling shared native deps, e.g. when two java_binary rules have the same set of
// native dependencies, the CppCompileAction for link stamp data is shared also. This means
// that out of two CppCompileAction instances, only one is actually executed, which means
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -232,37 +232,37 @@ private String getActionName() {
}
PathFragment sourcePath = sourceFile.getExecPath();
if (CppFileTypes.CPP_MODULE_MAP.matches(sourcePath)) {
return CppCompileAction.CPP_MODULE_COMPILE;
return CppActionNames.CPP_MODULE_COMPILE;
} else if (CppFileTypes.CPP_HEADER.matches(sourcePath)) {
// TODO(bazel-team): Handle C headers that probably don't work in C++ mode.
if (!cppConfiguration.getParseHeadersVerifiesModules()
&& featureConfiguration.isEnabled(CppRuleClasses.PARSE_HEADERS)) {
return CppCompileAction.CPP_HEADER_PARSING;
return CppActionNames.CPP_HEADER_PARSING;
} else if (!cppConfiguration.getParseHeadersVerifiesModules()
&& featureConfiguration.isEnabled(CppRuleClasses.PREPROCESS_HEADERS)) {
return CppCompileAction.CPP_HEADER_PREPROCESSING;
return CppActionNames.CPP_HEADER_PREPROCESSING;
} else {
// CcCommon.collectCAndCppSources() ensures we do not add headers to
// the compilation artifacts unless either 'parse_headers' or
// 'preprocess_headers' is set.
throw new IllegalStateException();
}
} else if (CppFileTypes.C_SOURCE.matches(sourcePath)) {
return CppCompileAction.C_COMPILE;
return CppActionNames.C_COMPILE;
} else if (CppFileTypes.CPP_SOURCE.matches(sourcePath)) {
return CppCompileAction.CPP_COMPILE;
return CppActionNames.CPP_COMPILE;
} else if (CppFileTypes.OBJC_SOURCE.matches(sourcePath)) {
return CppCompileAction.OBJC_COMPILE;
return CppActionNames.OBJC_COMPILE;
} else if (CppFileTypes.OBJCPP_SOURCE.matches(sourcePath)) {
return CppCompileAction.OBJCPP_COMPILE;
return CppActionNames.OBJCPP_COMPILE;
} else if (CppFileTypes.ASSEMBLER.matches(sourcePath)) {
return CppCompileAction.ASSEMBLE;
return CppActionNames.ASSEMBLE;
} else if (CppFileTypes.ASSEMBLER_WITH_C_PREPROCESSOR.matches(sourcePath)) {
return CppCompileAction.PREPROCESS_ASSEMBLE;
return CppActionNames.PREPROCESS_ASSEMBLE;
} else if (CppFileTypes.CLIF_INPUT_PROTO.matches(sourcePath)) {
return CppCompileAction.CLIF_MATCH;
return CppActionNames.CLIF_MATCH;
} else if (CppFileTypes.CPP_MODULE.matches(sourcePath)) {
return CppCompileAction.CPP_MODULE_CODEGEN;
return CppActionNames.CPP_MODULE_CODEGEN;
}
// CcCompilationHelper ensures CppCompileAction only gets instantiated for supported file types.
throw new IllegalStateException();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -902,14 +902,13 @@ public static void createStripAction(
return;
}

if (!featureConfiguration.actionIsConfigured(CppCompileAction.STRIP_ACTION_NAME)) {
if (!featureConfiguration.actionIsConfigured(CppActionNames.STRIP)) {
context.ruleError("Expected action_config for 'strip' to be configured.");
return;
}

Tool stripTool =
Preconditions.checkNotNull(
featureConfiguration.getToolForAction(CppCompileAction.STRIP_ACTION_NAME));
Preconditions.checkNotNull(featureConfiguration.getToolForAction(CppActionNames.STRIP));
CcToolchainVariables variables =
new CcToolchainVariables.Builder(toolchain.getBuildVariables())
.addStringVariable(
Expand All @@ -919,8 +918,7 @@ public static void createStripAction(
.addStringVariable(CcCommon.INPUT_FILE_VARIABLE_NAME, input.getExecPathString())
.build();
ImmutableList<String> commandLine =
ImmutableList.copyOf(
featureConfiguration.getCommandLine(CppCompileAction.STRIP_ACTION_NAME, variables));
ImmutableList.copyOf(featureConfiguration.getCommandLine(CppActionNames.STRIP, variables));
ImmutableMap.Builder<String, String> executionInfoBuilder = ImmutableMap.builder();
for (String executionRequirement : stripTool.getExecutionRequirements()) {
executionInfoBuilder.put(executionRequirement, "");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public static CppCompileAction createLinkstampCompileAction(
.setBuiltinIncludeFiles(buildInfoHeaderArtifacts)
.addMandatoryInputs(nonCodeInputs)
.setCppConfiguration(cppConfiguration)
.setActionName(CppCompileAction.LINKSTAMP_COMPILE);
.setActionName(CppActionNames.LINKSTAMP_COMPILE);
semantics.finalizeCompileActionBuilder(ruleContext, builder);
return builder.buildOrThrowIllegalStateException();
}
Expand Down Expand Up @@ -140,7 +140,7 @@ private static CcToolchainVariables getVariables(
boolean codeCoverageEnabled) {
// TODO(b/34761650): Remove all this hardcoding by separating a full blown compile action.
Preconditions.checkArgument(
featureConfiguration.actionIsConfigured(CppCompileAction.LINKSTAMP_COMPILE));
featureConfiguration.actionIsConfigured(CppActionNames.LINKSTAMP_COMPILE));

return CompileBuildVariables.setupVariablesOrReportRuleError(
ruleContext,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ public class FakeCppCompileAction extends CppCompileAction {
/* additionalIncludeScanningRoots=*/ ImmutableList.of(),
GUID,
executionInfo,
CppCompileAction.CPP_COMPILE,
CppActionNames.CPP_COMPILE,
cppSemantics,
cppProvider,
grepIncludes);
Expand Down
Loading

0 comments on commit d3fe943

Please sign in to comment.