aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorGravatar pcloudy <pcloudy@google.com>2018-03-28 05:07:22 -0700
committerGravatar Copybara-Service <copybara-piper@google.com>2018-03-28 05:08:37 -0700
commit0070792c6061a71723346e4b384ed07bc56000c6 (patch)
tree0497e32093d10df4186aeca2ce3fcb96a73e05fa
parent1a042ce789214cfdb26000464d99fbe227dff301 (diff)
Inline ObjectFilePathHelper
Since it's not used anywhere else outside of CcCompilationHelper.java RELNOTES: PiperOrigin-RevId: 190755588
-rw-r--r--src/main/java/com/google/devtools/build/lib/rules/cpp/CcCompilationHelper.java79
-rw-r--r--src/main/java/com/google/devtools/build/lib/rules/cpp/ObjectFilePathHelper.java90
2 files changed, 64 insertions, 105 deletions
diff --git a/src/main/java/com/google/devtools/build/lib/rules/cpp/CcCompilationHelper.java b/src/main/java/com/google/devtools/build/lib/rules/cpp/CcCompilationHelper.java
index b50306fe21..985d5fcee2 100644
--- a/src/main/java/com/google/devtools/build/lib/rules/cpp/CcCompilationHelper.java
+++ b/src/main/java/com/google/devtools/build/lib/rules/cpp/CcCompilationHelper.java
@@ -64,6 +64,8 @@ import com.google.devtools.build.lib.vfs.PathFragment;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
+import java.util.HashMap;
+import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
@@ -1175,6 +1177,51 @@ public final class CcCompilationHelper {
}
/**
+ * Calculate the output names for object file paths from a set of source files.
+ *
+ * <p>The object file path is constructed in the following format:
+ * <bazel-bin>/<target_package_path>/_objs/<target_name>/<output_name>.<obj_extension>
+ * When there's no two source files having the same basename:
+ * <output_name> = <prefixDir>/<source_file_base_name>
+ * otherwise:
+ * <output_name> = <prefixDir>/N/<source_file_base_name>,
+ * N = the file’s order among the source files with the same basename, starts with 0
+ *
+ * <p>Examples: 1. Output names for ["lib1/foo.cc", "lib2/bar.cc"] are ["foo", "bar"]
+ * 2. Output names for ["foo.cc", "bar.cc", "foo.cpp", "lib/foo.cc"]
+ * are ["0/foo", "bar", "1/foo", "2/foo"]
+ */
+ private ImmutableMap<Artifact, String> calculateOutputNameMap(
+ Iterable<Artifact> sourceArtifacts, String prefixDir) {
+ ImmutableMap.Builder<Artifact, String> builder = ImmutableMap.builder();
+
+ HashMap<String, Integer> count = new LinkedHashMap<>();
+ HashMap<String, Integer> number = new LinkedHashMap<>();
+ for (Artifact source : sourceArtifacts) {
+ String outputName =
+ FileSystemUtils.removeExtension(source.getRootRelativePath()).getBaseName();
+ count.put(outputName, count.getOrDefault(outputName, 0) + 1);
+ }
+
+ for (Artifact source : sourceArtifacts) {
+ String outputName =
+ FileSystemUtils.removeExtension(source.getRootRelativePath()).getBaseName();
+ if (count.getOrDefault(outputName, 0) > 1) {
+ int num = number.getOrDefault(outputName, 0);
+ number.put(outputName, num + 1);
+ outputName = num + "/" + outputName;
+ }
+ // If prefixDir is set, prepend it to the outputName
+ if (prefixDir != null) {
+ outputName = prefixDir + "/" + outputName;
+ }
+ builder.put(source, outputName);
+ }
+
+ return builder.build();
+ }
+
+ /**
* Constructs the C++ compiler actions. It generally creates one action for every specified source
* file. It takes into account LIPO, fake-ness, coverage, and PIC, in addition to using the
* settings specified on the current object. This method should only be called once.
@@ -1204,22 +1251,22 @@ public final class CcCompilationHelper {
}
}
- String outputNamePrefixDir = null;
- // purpose is only used by objc rules, it ends with either "_non_objc_arc" or "_objc_arc".
- // Here we use it to distinguish arc and non-arc compilation.
- if (purpose != null) {
- outputNamePrefixDir = purpose.endsWith("_non_objc_arc") ? "non_arc" : "arc";
+ ImmutableMap<Artifact, String> outputNameMap = null;
+ if (cppConfiguration.shortenObjFilePath()) {
+ String outputNamePrefixDir = null;
+ // purpose is only used by objc rules, it ends with either "_non_objc_arc" or "_objc_arc".
+ // Here we use it to distinguish arc and non-arc compilation.
+ if (purpose != null) {
+ outputNamePrefixDir = purpose.endsWith("_non_objc_arc") ? "non_arc" : "arc";
+ }
+ outputNameMap = calculateOutputNameMap(
+ compilationUnitSources
+ .stream()
+ .map(source -> source.getSource())
+ .collect(Collectors.toList()),
+ outputNamePrefixDir);
}
- ObjectFilePathHelper objectFilePathHelper =
- new ObjectFilePathHelper(
- compilationUnitSources
- .stream()
- .map(source -> source.getSource())
- .collect(Collectors.toList()),
- cppConfiguration.shortenObjFilePath(),
- outputNamePrefixDir);
-
for (CppSource source : compilationUnitSources) {
Artifact sourceArtifact = source.getSource();
Label sourceLabel = source.getLabel();
@@ -1234,7 +1281,9 @@ public final class CcCompilationHelper {
featureConfiguration.isEnabled(CppRuleClasses.THIN_LTO)
&& CppFileTypes.LTO_SOURCE.matches(sourceArtifact.getFilename());
- String outputName = objectFilePathHelper.getOutputName(sourceArtifact);
+ String outputName = cppConfiguration.shortenObjFilePath()
+ ? outputNameMap.get(sourceArtifact)
+ : FileSystemUtils.removeExtension(sourceArtifact.getRootRelativePath()).getPathString();
if (!sourceArtifact.isTreeArtifact()) {
switch (source.getType()) {
diff --git a/src/main/java/com/google/devtools/build/lib/rules/cpp/ObjectFilePathHelper.java b/src/main/java/com/google/devtools/build/lib/rules/cpp/ObjectFilePathHelper.java
deleted file mode 100644
index 2ceda006cf..0000000000
--- a/src/main/java/com/google/devtools/build/lib/rules/cpp/ObjectFilePathHelper.java
+++ /dev/null
@@ -1,90 +0,0 @@
-// 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;
-
-import com.google.common.collect.ImmutableMap;
-import com.google.devtools.build.lib.actions.Artifact;
-import com.google.devtools.build.lib.vfs.FileSystemUtils;
-import java.util.HashMap;
-import java.util.LinkedHashMap;
-
-/**
- * A helper class for calculating the output names for object file paths from a set of source files.
- *
- * <p>If {@link shortenObjFilePath} is true, the object file path is constructed
- *
- * <p>The object file path is constructed in the following format:
- * <bazel-bin>/<target_package_path>/_objs/<target_name>/<output_name>.<obj_extension> When there's
- * no two source files having the same basename: <output_name> = <source_file_base_name> otherwise:
- * <output_name> = N/<source_file_base_name>, N = the file’s order among the source files with the
- * same basename
- *
- * <p>Examples: 1. Output names for ["lib1/foo.cc", "lib2/bar.cc"] are ["foo", "bar"] 2. Output
- * names for ["foo.cc", "bar.cc", "foo.cpp", "lib/foo.cc"] are ["0/foo", "bar", "1/foo", "2/foo"]
- *
- * <p>TODO(b/76143707): Inline this class when it's not used anywhere outside of
- * CcCompilationHelper.
- */
-public class ObjectFilePathHelper {
-
- private final ImmutableMap<Artifact, String> outputNameMap;
- private final boolean shortenObjFilePath;
-
- public ObjectFilePathHelper(
- Iterable<Artifact> sourceArtifacts, boolean shortenObjFilePath, String prefixDir) {
- // If legacy object file path is used, no need to calculate outputNameMap
- this.shortenObjFilePath = shortenObjFilePath;
- if (!shortenObjFilePath) {
- outputNameMap = null;
- return;
- }
-
- ImmutableMap.Builder<Artifact, String> builder = ImmutableMap.builder();
-
- HashMap<String, Integer> count = new LinkedHashMap<>();
- HashMap<String, Integer> number = new LinkedHashMap<>();
- for (Artifact source : sourceArtifacts) {
- String outputName =
- FileSystemUtils.removeExtension(source.getRootRelativePath()).getBaseName();
- count.put(outputName, count.getOrDefault(outputName, 0) + 1);
- }
-
- for (Artifact source : sourceArtifacts) {
- String outputName =
- FileSystemUtils.removeExtension(source.getRootRelativePath()).getBaseName();
- if (count.getOrDefault(outputName, 0) > 1) {
- int num = number.getOrDefault(outputName, 0);
- number.put(outputName, num + 1);
- outputName = num + "/" + outputName;
- }
- // If prefixDir is set, prepend it to the outputName
- if (prefixDir != null) {
- outputName = prefixDir + "/" + outputName;
- }
- builder.put(source, outputName);
- }
-
- outputNameMap = builder.build();
- }
-
- /** Return the output name for the object file path of a given source file. */
- public String getOutputName(Artifact source) {
- if (shortenObjFilePath) {
- return outputNameMap.get(source);
- } else {
- return FileSystemUtils.removeExtension(source.getRootRelativePath()).getPathString();
- }
- }
-}