use of com.google.devtools.build.lib.runtime.BlazeRuntime in project bazel by bazelbuild.
the class CoverageCommand method setDefaultInstrumentationFilter.
/**
* Method implements a heuristic used to set default value of the
* --instrumentation_filter option. Following algorithm is used:
* 1) Identify all test targets on the command line.
* 2) Expand all test suites into the individual test targets
* 3) Calculate list of package names containing all test targets above.
* 4) Replace all "javatests/" substrings in package names with "java/".
* 5) If two packages reside in the same directory, use filter based on
* the parent directory name instead. Doing so significantly simplifies
* instrumentation filter in majority of real-life scenarios (in
* particular when dealing with my/package/... wildcards).
* 6) Set --instrumentation_filter default value to instrument everything
* in those packages.
*/
private void setDefaultInstrumentationFilter(CommandEnvironment env, OptionsParser optionsProvider) throws OptionsParsingException, AbruptExitException {
try {
BlazeRuntime runtime = env.getRuntime();
// Initialize package cache, since it is used by the TargetPatternEvaluator.
// TODO(bazel-team): Don't allow commands to setup the package cache more than once per build.
// We'll have to move it earlier in the process to allow this. Possibly: Move it to
// the command dispatcher and allow commands to annotate "need-packages".
env.setupPackageCache(optionsProvider, runtime.getDefaultsPackageContent(optionsProvider));
// Collect all possible test targets. We don't really care whether there will be parsing
// errors here - they will be reported during actual build.
TargetPatternEvaluator targetPatternEvaluator = env.newTargetPatternEvaluator();
Set<Target> testTargets = targetPatternEvaluator.parseTargetPatternList(env.getReporter(), optionsProvider.getResidue(), FilteringPolicies.FILTER_TESTS, /*keep_going=*/
true).getTargets();
SortedSet<String> packageFilters = Sets.newTreeSet();
collectInstrumentedPackages(env, testTargets, packageFilters);
optimizeFilterSet(packageFilters);
String instrumentationFilter = "//" + Joiner.on(",//").join(packageFilters);
final String instrumentationFilterOptionName = "instrumentation_filter";
if (!packageFilters.isEmpty()) {
env.getReporter().handle(Event.info("Using default value for --instrumentation_filter: \"" + instrumentationFilter + "\"."));
env.getReporter().handle(Event.info("Override the above default with --" + instrumentationFilterOptionName));
optionsProvider.parse(OptionPriority.COMPUTED_DEFAULT, "Instrumentation filter heuristic", ImmutableList.of("--" + instrumentationFilterOptionName + "=" + instrumentationFilter));
}
} catch (TargetParsingException e) {
// We can't compute heuristic - just use default filter.
} catch (InterruptedException e) {
// We cannot quit now because AbstractCommand does not have the
// infrastructure to do that. Just set a flag and return from exec() as
// early as possible. We can do this because there is always an exec()
// after an editOptions().
wasInterrupted = true;
}
}
use of com.google.devtools.build.lib.runtime.BlazeRuntime in project bazel by bazelbuild.
the class DumpCommand method exec.
@Override
public ExitCode exec(CommandEnvironment env, OptionsProvider options) {
BlazeRuntime runtime = env.getRuntime();
DumpOptions dumpOptions = options.getOptions(DumpOptions.class);
boolean anyOutput = dumpOptions.dumpPackages || dumpOptions.dumpVfs || dumpOptions.dumpActionCache || dumpOptions.dumpRuleClasses || (dumpOptions.dumpSkyframe != SkyframeDumpOption.OFF);
if (!anyOutput) {
Map<String, String> categories = new HashMap<>();
categories.put("verbosity", "Options that control what internal state is dumped");
Collection<Class<? extends OptionsBase>> optionList = new ArrayList<>();
optionList.add(DumpOptions.class);
env.getReporter().getOutErr().printErrLn(BlazeCommandUtils.expandHelpTopic(getClass().getAnnotation(Command.class).name(), getClass().getAnnotation(Command.class).help(), getClass(), optionList, categories, OptionsParser.HelpVerbosity.LONG, runtime.getProductName()));
return ExitCode.ANALYSIS_FAILURE;
}
PrintStream out = new PrintStream(env.getReporter().getOutErr().getOutputStream());
try {
out.println("Warning: this information is intended for consumption by developers");
out.println("only, and may change at any time. Script against it at your own risk!");
out.println();
boolean success = true;
if (dumpOptions.dumpPackages) {
env.getPackageManager().dump(out);
out.println();
}
if (dumpOptions.dumpVfs) {
out.println("Filesystem cache");
FileSystemUtils.dump(env.getOutputBase().getFileSystem(), out);
out.println();
}
if (dumpOptions.dumpActionCache) {
success &= dumpActionCache(env, out);
out.println();
}
if (dumpOptions.dumpRuleClasses) {
dumpRuleClasses(runtime, out);
out.println();
}
if (dumpOptions.dumpSkyframe != SkyframeDumpOption.OFF) {
success &= dumpSkyframe(env.getSkyframeExecutor(), dumpOptions.dumpSkyframe == SkyframeDumpOption.SUMMARY, out);
out.println();
}
return success ? ExitCode.SUCCESS : ExitCode.ANALYSIS_FAILURE;
} finally {
out.flush();
}
}
use of com.google.devtools.build.lib.runtime.BlazeRuntime in project bazel by bazelbuild.
the class HelpCommand method exec.
@Override
public ExitCode exec(CommandEnvironment env, OptionsProvider options) {
env.getEventBus().post(new NoBuildEvent());
BlazeRuntime runtime = env.getRuntime();
OutErr outErr = env.getReporter().getOutErr();
Options helpOptions = options.getOptions(Options.class);
if (options.getResidue().isEmpty()) {
emitBlazeVersionInfo(outErr, runtime.getProductName());
emitGenericHelp(outErr, runtime);
return ExitCode.SUCCESS;
}
if (options.getResidue().size() != 1) {
env.getReporter().handle(Event.error("You must specify exactly one command"));
return ExitCode.COMMAND_LINE_ERROR;
}
String helpSubject = options.getResidue().get(0);
if (helpSubject.equals("startup_options")) {
emitBlazeVersionInfo(outErr, runtime.getProductName());
emitStartupOptions(outErr, helpOptions.helpVerbosity, runtime, getOptionCategories(runtime));
return ExitCode.SUCCESS;
} else if (helpSubject.equals("target-syntax")) {
emitBlazeVersionInfo(outErr, runtime.getProductName());
emitTargetSyntaxHelp(outErr, getOptionCategories(runtime), runtime.getProductName());
return ExitCode.SUCCESS;
} else if (helpSubject.equals("info-keys")) {
emitInfoKeysHelp(env, outErr);
return ExitCode.SUCCESS;
} else if (helpSubject.equals("completion")) {
emitCompletionHelp(runtime, outErr);
return ExitCode.SUCCESS;
} else if (helpSubject.equals("everything-as-html")) {
new HtmlEmitter(runtime).emit(outErr);
return ExitCode.SUCCESS;
}
BlazeCommand command = runtime.getCommandMap().get(helpSubject);
if (command == null) {
ConfiguredRuleClassProvider provider = runtime.getRuleClassProvider();
RuleClass ruleClass = provider.getRuleClassMap().get(helpSubject);
if (ruleClass != null && ruleClass.isDocumented()) {
// There is a rule with a corresponding name
outErr.printOut(BlazeRuleHelpPrinter.getRuleDoc(helpSubject, provider));
return ExitCode.SUCCESS;
} else {
env.getReporter().handle(Event.error(null, "'" + helpSubject + "' is neither a command nor a build rule"));
return ExitCode.COMMAND_LINE_ERROR;
}
}
emitBlazeVersionInfo(outErr, runtime.getProductName());
outErr.printOut(BlazeCommandUtils.getUsage(command.getClass(), getOptionCategories(runtime), helpOptions.helpVerbosity, runtime.getBlazeModules(), runtime.getRuleClassProvider(), runtime.getProductName()));
return ExitCode.SUCCESS;
}
use of com.google.devtools.build.lib.runtime.BlazeRuntime in project bazel by bazelbuild.
the class FetchCommand method exec.
@Override
public ExitCode exec(CommandEnvironment env, OptionsProvider options) {
BlazeRuntime runtime = env.getRuntime();
if (options.getResidue().isEmpty()) {
env.getReporter().handle(Event.error(String.format("missing fetch expression. Type '%s help fetch' for syntax and help", env.getRuntime().getProductName())));
return ExitCode.COMMAND_LINE_ERROR;
}
try {
env.setupPackageCache(options, runtime.getDefaultsPackageContent());
} catch (InterruptedException e) {
env.getReporter().handle(Event.error("fetch interrupted"));
return ExitCode.INTERRUPTED;
} catch (AbruptExitException e) {
env.getReporter().handle(Event.error(null, "Unknown error: " + e.getMessage()));
return e.getExitCode();
}
PackageCacheOptions pkgOptions = options.getOptions(PackageCacheOptions.class);
if (!pkgOptions.fetch) {
env.getReporter().handle(Event.error(null, "You cannot run fetch with --fetch=false"));
return ExitCode.COMMAND_LINE_ERROR;
}
// Querying for all of the dependencies of the targets has the side-effect of populating the
// Skyframe graph for external targets, which requires downloading them. The JDK is required to
// build everything but isn't counted as a dep in the build graph so we add it manually.
ImmutableList.Builder<String> labelsToLoad = new ImmutableList.Builder<String>().addAll(options.getResidue());
String query = Joiner.on(" union ").join(labelsToLoad.build());
query = "deps(" + query + ")";
AbstractBlazeQueryEnvironment<Target> queryEnv = QueryCommand.newQueryEnvironment(env, options.getOptions(FetchOptions.class).keepGoing, false, Lists.<String>newArrayList(), 200, Sets.<Setting>newHashSet());
// 1. Parse query:
QueryExpression expr;
try {
expr = QueryExpression.parse(query, queryEnv);
} catch (QueryException e) {
env.getReporter().handle(Event.error(null, "Error while parsing '" + query + "': " + e.getMessage()));
return ExitCode.COMMAND_LINE_ERROR;
}
// 2. Evaluate expression:
try {
queryEnv.evaluateQuery(expr, new ThreadSafeOutputFormatterCallback<Target>() {
@Override
public void processOutput(Iterable<Target> partialResult) {
// Throw away the result.
}
});
} catch (InterruptedException e) {
return ExitCode.COMMAND_LINE_ERROR;
} catch (QueryException e) {
// Keep consistent with reportBuildFileError()
env.getReporter().handle(Event.error(e.getMessage()));
return ExitCode.COMMAND_LINE_ERROR;
} catch (IOException e) {
// Should be impossible since our OutputFormatterCallback doesn't throw IOException.
throw new IllegalStateException(e);
}
env.getReporter().handle(Event.progress("All external dependencies fetched successfully."));
return ExitCode.SUCCESS;
}
use of com.google.devtools.build.lib.runtime.BlazeRuntime in project bazel by bazelbuild.
the class InfoCommand method exec.
@Override
public ExitCode exec(final CommandEnvironment env, final OptionsProvider optionsProvider) {
final BlazeRuntime runtime = env.getRuntime();
env.getReporter().switchToAnsiAllowingHandler();
Options infoOptions = optionsProvider.getOptions(Options.class);
OutErr outErr = env.getReporter().getOutErr();
// Creating a BuildConfiguration is expensive and often unnecessary. Delay the creation until
// it is needed.
Supplier<BuildConfiguration> configurationSupplier = new Supplier<BuildConfiguration>() {
private BuildConfiguration configuration;
@Override
public BuildConfiguration get() {
if (configuration != null) {
return configuration;
}
try {
// In order to be able to answer configuration-specific queries, we need to setup the
// package path. Since info inherits all the build options, all the necessary information
// is available here.
env.setupPackageCache(optionsProvider, runtime.getDefaultsPackageContent(optionsProvider));
// TODO(bazel-team): What if there are multiple configurations? [multi-config]
configuration = env.getConfigurations(optionsProvider).getTargetConfigurations().get(0);
return configuration;
} catch (InvalidConfigurationException e) {
env.getReporter().handle(Event.error(e.getMessage()));
throw new ExitCausingRuntimeException(ExitCode.COMMAND_LINE_ERROR);
} catch (AbruptExitException e) {
throw new ExitCausingRuntimeException("unknown error: " + e.getMessage(), e.getExitCode());
} catch (InterruptedException e) {
env.getReporter().handle(Event.error("interrupted"));
throw new ExitCausingRuntimeException(ExitCode.INTERRUPTED);
}
}
};
Map<String, InfoItem> items = getInfoItemMap(env, optionsProvider);
try {
if (infoOptions.showMakeEnvironment) {
Map<String, String> makeEnv = configurationSupplier.get().getMakeEnvironment();
for (Map.Entry<String, String> entry : makeEnv.entrySet()) {
InfoItem item = new InfoItem.MakeInfoItem(entry.getKey(), entry.getValue());
items.put(item.getName(), item);
}
}
List<String> residue = optionsProvider.getResidue();
if (residue.size() > 1) {
env.getReporter().handle(Event.error("at most one key may be specified"));
return ExitCode.COMMAND_LINE_ERROR;
}
String key = residue.size() == 1 ? residue.get(0) : null;
env.getEventBus().post(new NoBuildEvent());
if (key != null) {
// print just the value for the specified key:
byte[] value;
if (items.containsKey(key)) {
value = items.get(key).get(configurationSupplier, env);
} else {
env.getReporter().handle(Event.error("unknown key: '" + key + "'"));
return ExitCode.COMMAND_LINE_ERROR;
}
try {
outErr.getOutputStream().write(value);
outErr.getOutputStream().flush();
} catch (IOException e) {
env.getReporter().handle(Event.error("Cannot write info block: " + e.getMessage()));
return ExitCode.ANALYSIS_FAILURE;
}
} else {
// print them all
// We'll need this later anyway
configurationSupplier.get();
for (InfoItem infoItem : items.values()) {
if (infoItem.isHidden()) {
continue;
}
outErr.getOutputStream().write((infoItem.getName() + ": ").getBytes(StandardCharsets.UTF_8));
outErr.getOutputStream().write(infoItem.get(configurationSupplier, env));
}
}
} catch (AbruptExitException e) {
return e.getExitCode();
} catch (ExitCausingRuntimeException e) {
return e.getExitCode();
} catch (IOException e) {
return ExitCode.LOCAL_ENVIRONMENTAL_ERROR;
} catch (InterruptedException e) {
return ExitCode.INTERRUPTED;
}
return ExitCode.SUCCESS;
}
Aggregations