From 817c49ebc77b509fde995e914ded3141b417865d Mon Sep 17 00:00:00 2001 From: rosica Date: Thu, 6 Jun 2019 03:20:13 -0700 Subject: [PATCH] Use Blaze's CommandLine infrastructure to expand ThinLTO command lines only when necessary to avoid unnecessary memory consumption RELNOTES: None. PiperOrigin-RevId: 251819397 --- .../lib/rules/cpp/LtoBackendArtifacts.java | 17 ++---- .../lib/rules/cpp/LtoBackendCommandLine.java | 60 +++++++++++++++++++ 2 files changed, 64 insertions(+), 13 deletions(-) create mode 100644 src/main/java/com/google/devtools/build/lib/rules/cpp/LtoBackendCommandLine.java diff --git a/src/main/java/com/google/devtools/build/lib/rules/cpp/LtoBackendArtifacts.java b/src/main/java/com/google/devtools/build/lib/rules/cpp/LtoBackendArtifacts.java index a1fdec92af1464..29f3021efd9aa2 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/cpp/LtoBackendArtifacts.java +++ b/src/main/java/com/google/devtools/build/lib/rules/cpp/LtoBackendArtifacts.java @@ -17,6 +17,7 @@ import com.google.common.base.Preconditions; import com.google.common.collect.ImmutableSet; import com.google.devtools.build.lib.actions.Artifact; +import com.google.devtools.build.lib.actions.CommandLine; import com.google.devtools.build.lib.analysis.actions.ActionConstructionContext; import com.google.devtools.build.lib.analysis.config.BuildConfiguration; import com.google.devtools.build.lib.analysis.config.BuildOptions; @@ -29,7 +30,6 @@ import com.google.devtools.build.lib.rules.cpp.CppLinkAction.LinkArtifactFactory; import com.google.devtools.build.lib.vfs.FileSystemUtils; import com.google.devtools.build.lib.vfs.PathFragment; -import java.util.ArrayList; import java.util.List; import java.util.Objects; import javax.annotation.Nullable; @@ -289,18 +289,9 @@ private void scheduleLtoBackendAction( builder.setExecutable(compiler); } - List execArgs = new ArrayList<>(); - execArgs.addAll( - CppHelper.getCommandLine( - ruleErrorConsumer, featureConfiguration, buildVariables, CppActionNames.LTO_BACKEND)); - // If this is a PIC compile (set based on the CppConfiguration), the PIC - // option should be added after the rest of the command line so that it - // cannot be overridden. This is consistent with the ordering in the - // CppCompileAction's compiler options. - if (usePic) { - execArgs.add("-fPIC"); - } - builder.addExecutableArguments(execArgs); + CommandLine ltoCommandLine = + new LtoBackendCommandLine(featureConfiguration, buildVariables, usePic); + builder.addCommandLine(ltoCommandLine); actionConstructionContext.registerAction(builder.build(actionConstructionContext)); } diff --git a/src/main/java/com/google/devtools/build/lib/rules/cpp/LtoBackendCommandLine.java b/src/main/java/com/google/devtools/build/lib/rules/cpp/LtoBackendCommandLine.java new file mode 100644 index 00000000000000..874a0928fc5ee5 --- /dev/null +++ b/src/main/java/com/google/devtools/build/lib/rules/cpp/LtoBackendCommandLine.java @@ -0,0 +1,60 @@ +// Copyright 2019 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.ImmutableList; +import com.google.devtools.build.lib.actions.CommandLine; +import com.google.devtools.build.lib.actions.CommandLineExpansionException; +import com.google.devtools.build.lib.rules.cpp.CcToolchainFeatures.ExpansionException; +import com.google.devtools.build.lib.rules.cpp.CcToolchainFeatures.FeatureConfiguration; + +/** + * A representation of command line for {@link LtoBackendAction}. + * + *

We have this class so we're able to defer construction of the command line to execution phase. + */ +public class LtoBackendCommandLine extends CommandLine { + + private final FeatureConfiguration featureConfiguration; + private final CcToolchainVariables buildVariables; + private final boolean usePic; + + LtoBackendCommandLine( + FeatureConfiguration featureConfiguration, + CcToolchainVariables ccToolchainVariables, + boolean usePic) { + this.featureConfiguration = featureConfiguration; + this.buildVariables = ccToolchainVariables; + this.usePic = usePic; + } + + @Override + public Iterable arguments() throws CommandLineExpansionException { + ImmutableList.Builder args = ImmutableList.builder(); + try { + args.addAll(featureConfiguration.getCommandLine(CppActionNames.LTO_BACKEND, buildVariables)); + } catch (ExpansionException e) { + throw new CommandLineExpansionException(e.getMessage()); + } + // If this is a PIC compile (set based on the CppConfiguration), the PIC + // option should be added after the rest of the command line so that it + // cannot be overridden. This is consistent with the ordering in the + // CppCompileAction's compiler options. + if (usePic) { + args.add("-fPIC"); + } + return args.build(); + } +}