use of com.google.devtools.common.options.OptionsParser in project bazel by bazelbuild.
the class ConfigSetting method matchesConfig.
/**
* Given a list of [flagName, flagValue] pairs, returns true if flagName == flagValue for
* every item in the list under this configuration, false otherwise.
*/
private boolean matchesConfig(Map<String, String> expectedSettings, BuildConfiguration config) throws OptionsParsingException {
// Rather than returning fast when we find a mismatch, continue looking at the other flags
// to check that they're indeed valid flag specifications.
boolean foundMismatch = false;
// Since OptionsParser instantiation involves reflection, let's try to minimize that happening.
Map<Class<? extends OptionsBase>, OptionsParser> parserCache = new HashMap<>();
for (Map.Entry<String, String> setting : expectedSettings.entrySet()) {
String optionName = setting.getKey();
String expectedRawValue = setting.getValue();
Class<? extends OptionsBase> optionClass = config.getOptionClass(optionName);
if (optionClass == null) {
throw new OptionsParsingException("unknown option: '" + optionName + "'");
}
OptionsParser parser = parserCache.get(optionClass);
if (parser == null) {
parser = OptionsParser.newOptionsParser(optionClass);
parserCache.put(optionClass, parser);
}
parser.parse("--" + optionName + "=" + expectedRawValue);
Object expectedParsedValue = parser.getOptions(optionClass).asMap().get(optionName);
if (!optionMatches(config, optionName, expectedParsedValue)) {
foundMismatch = true;
}
}
return !foundMismatch;
}
use of com.google.devtools.common.options.OptionsParser in project bazel by bazelbuild.
the class BuildOptions method of.
/**
* Creates a BuildOptions class by taking the option values from command-line arguments and
* applying the specified original options.
*/
@VisibleForTesting
static BuildOptions of(List<Class<? extends FragmentOptions>> optionsList, BuildOptions originalOptions, String... args) throws OptionsParsingException {
Builder builder = builder();
OptionsParser parser = OptionsParser.newOptionsParser(ImmutableList.<Class<? extends OptionsBase>>copyOf(optionsList));
parser.parse(args);
for (Class<? extends FragmentOptions> optionsClass : optionsList) {
builder.add(parser.getOptions(optionsClass));
}
builder.setOriginalOptions(originalOptions);
return builder.build();
}
use of com.google.devtools.common.options.OptionsParser in project bazel by bazelbuild.
the class XcodeGen method main.
public static void main(String[] args) throws IOException, OptionsParsingException {
OptionsParser parser = OptionsParser.newOptionsParser(XcodeGenOptions.class);
parser.parse(args);
XcodeGenOptions options = parser.getOptions(XcodeGenOptions.class);
if (options.control == null) {
throw new IllegalArgumentException("--control must be specified\n" + Options.getUsage(XcodeGenOptions.class));
}
FileSystem fileSystem = FileSystems.getDefault();
Control controlPb;
try (InputStream in = Files.newInputStream(fileSystem.getPath(options.control))) {
controlPb = Control.parseFrom(in);
}
Path pbxprojPath = fileSystem.getPath(controlPb.getPbxproj());
Iterator<String> srcList = allSourceFilePaths(controlPb).iterator();
Path workspaceRoot;
// TODO(bazel-team): Remove this if-else clause once Bazel passes in the workspace root.
if (controlPb.hasWorkspaceRoot()) {
workspaceRoot = fileSystem.getPath(controlPb.getWorkspaceRoot());
} else if (!srcList.hasNext()) {
workspaceRoot = XcodeprojGeneration.relativeWorkspaceRoot(pbxprojPath);
} else {
// Get the absolute path to the workspace root.
// TODO(bazel-team): Remove this hack, possibly by converting Xcodegen to be run with
// "bazel run" and using RUNFILES to get the workspace root. For now, this is needed to work
// around Xcode's handling of symlinks not playing nicely with how Bazel stores output
// artifacts in /private/var/tmp. This means a relative path from .xcodeproj in bazel-out to
// the workspace root in .xcodeproj will not work properly at certain times during
// Xcode/xcodebuild execution. Walking up the path of a known source file prevents having
// to reason about a file that might only be accessible through a symlink, like a tools jar.
Path relSourceFilePath = fileSystem.getPath(srcList.next());
Path absSourceFilePath = relSourceFilePath.toAbsolutePath();
workspaceRoot = absSourceFilePath;
for (int i = 0; i < relSourceFilePath.getNameCount(); i++) {
workspaceRoot = workspaceRoot.getParent();
}
}
try (OutputStream out = Files.newOutputStream(pbxprojPath)) {
// This workspace root here is relative to the PWD, so that the .xccurrentversion
// files can actually be read. The other workspaceRoot is relative to the .xcodeproj
// root or is absolute.
Path relativeWorkspaceRoot = fileSystem.getPath(".");
PBXProject project = XcodeprojGeneration.xcodeproj(workspaceRoot, controlPb, ImmutableList.of(new CurrentVersionSetter(relativeWorkspaceRoot), new PbxReferencesGrouper(fileSystem)));
XcodeprojGeneration.write(out, project);
}
}
use of com.google.devtools.common.options.OptionsParser in project bazel by bazelbuild.
the class GenerateWorkspace method main.
public static void main(String[] args) throws InterruptedException {
OptionsParser parser = OptionsParser.newOptionsParser(GenerateWorkspaceOptions.class);
parser.parseAndExitUponError(args);
GenerateWorkspaceOptions options = parser.getOptions(GenerateWorkspaceOptions.class);
if (options.mavenProjects.isEmpty() && options.bazelProjects.isEmpty() && options.artifacts.isEmpty()) {
printUsage(parser);
return;
}
GenerateWorkspace workspaceFileGenerator = new GenerateWorkspace(options.outputDir);
workspaceFileGenerator.generateFromWorkspace(options.bazelProjects);
workspaceFileGenerator.generateFromPom(options.mavenProjects);
workspaceFileGenerator.generateFromArtifacts(options.artifacts);
if (!workspaceFileGenerator.hasErrors()) {
workspaceFileGenerator.writeResults();
}
workspaceFileGenerator.cleanup();
if (workspaceFileGenerator.hasErrors()) {
System.exit(1);
}
}
use of com.google.devtools.common.options.OptionsParser in project bazel by bazelbuild.
the class DexMapper method main.
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
OptionsParser optionsParser = OptionsParser.newOptionsParser(Options.class);
optionsParser.parseAndExitUponError(args);
Options options = optionsParser.getOptions(Options.class);
List<String> inputs = options.inputJars;
List<String> outputs = options.outputJars;
String filterFile = options.mainDexFilter;
String resourceFile = options.outputResources;
try {
Predicate<String> inputFilter = Predicates.alwaysTrue();
if (options.inclusionFilterJar != null) {
inputFilter = SplitZipFilters.entriesIn(options.inclusionFilterJar);
}
new SplitZip().setVerbose(false).useDefaultEntryDate().setSplitDexedClasses(options.splitDexedClasses).addInputs(inputs).addOutputs(outputs).setInputFilter(inputFilter).setMainClassListFile(filterFile).setResourceFile(resourceFile).run().close();
} catch (Exception ex) {
System.err.println("Caught exception" + ex.getMessage());
ex.printStackTrace(System.out);
System.exit(1);
}
}
Aggregations