Skip to content

Commit

Permalink
Use Blaze's CommandLine infrastructure to expand ThinLTO command line…
Browse files Browse the repository at this point in the history
…s only when necessary to avoid unnecessary memory consumption

RELNOTES: None.
PiperOrigin-RevId: 251819397
  • Loading branch information
scentini authored and irengrig committed Jun 18, 2019
1 parent 021906f commit 817c49e
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -289,18 +289,9 @@ private void scheduleLtoBackendAction(
builder.setExecutable(compiler);
}

List<String> 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));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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}.
*
* <p>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<String> arguments() throws CommandLineExpansionException {
ImmutableList.Builder<String> 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();
}
}

0 comments on commit 817c49e

Please sign in to comment.