aboutsummaryrefslogtreecommitdiffhomepage
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/main/java/com/google/devtools/build/lib/rules/python/PrecompilePythonMode.java25
-rw-r--r--src/main/java/com/google/devtools/common/options/BoolOrEnumConverter.java62
-rw-r--r--src/main/java/com/google/devtools/common/options/EnumConverter.java6
-rw-r--r--src/main/java/com/google/devtools/common/options/OptionsParserImpl.java12
-rw-r--r--src/test/java/com/google/devtools/common/options/BoolOrEnumConverterTest.java106
5 files changed, 203 insertions, 8 deletions
diff --git a/src/main/java/com/google/devtools/build/lib/rules/python/PrecompilePythonMode.java b/src/main/java/com/google/devtools/build/lib/rules/python/PrecompilePythonMode.java
new file mode 100644
index 0000000000..b77473e7e8
--- /dev/null
+++ b/src/main/java/com/google/devtools/build/lib/rules/python/PrecompilePythonMode.java
@@ -0,0 +1,25 @@
+// Copyright 2014 Google Inc. 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.python;
+
+/**
+ * Enumerates the different modes of Python precompilation.
+ *
+ * NONE denotes that no precompilation should take place, and PROTO causes
+ * only the generated _pb/_pb2.py files to be precompiled.
+ */
+public enum PrecompilePythonMode {
+ NONE, PROTO; // TODO(scottgw) add 'ALL' element.
+}
diff --git a/src/main/java/com/google/devtools/common/options/BoolOrEnumConverter.java b/src/main/java/com/google/devtools/common/options/BoolOrEnumConverter.java
new file mode 100644
index 0000000000..1a11ee8a18
--- /dev/null
+++ b/src/main/java/com/google/devtools/common/options/BoolOrEnumConverter.java
@@ -0,0 +1,62 @@
+// Copyright 2014 Google Inc. 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.common.options;
+
+import com.google.devtools.common.options.Converters.BooleanConverter;
+
+/**
+ * Converter that can also convert from booleans and enumerations.
+ *
+ * <p> This is able to additionally convert from the standard set of
+ * boolean string values. If there is an overlap in values, those from
+ * the underlying enumeration will be taken.
+ */
+public abstract class BoolOrEnumConverter<T extends Enum<T>> extends EnumConverter<T>{
+
+ private T falseValue;
+ private T trueValue;
+
+ /**
+ * You *must* implement a zero-argument constructor that delegates
+ * to this constructor, passing in the appropriate parameters. This
+ * comes from the base {@link EnumConverter} class.
+ *
+ * @param enumType The type of your enumeration; usually a class literal
+ * like MyEnum.class
+ * @param typeName The intuitive name of your enumeration, for example, the
+ * type name for CompilationMode might be "compilation mode".
+ * @param trueValue The enumeration value to associate with {@code true}.
+ * @param falseValue The enumeration value to associate with {@code false}.
+ */
+ protected BoolOrEnumConverter(Class<T> enumType, String typeName, T trueValue, T falseValue) {
+ super(enumType, typeName);
+ this.trueValue = trueValue;
+ this.falseValue = falseValue;
+ }
+
+ public T convert(String input) throws OptionsParsingException {
+ try {
+ return super.convert(input);
+ } catch (OptionsParsingException eEnum) {
+ try {
+ BooleanConverter booleanConverter = new BooleanConverter();
+ boolean value = booleanConverter.convert(input);
+ return value ? trueValue : falseValue;
+ } catch (OptionsParsingException eBoolean) {
+ throw eEnum;
+ }
+ }
+ }
+}
diff --git a/src/main/java/com/google/devtools/common/options/EnumConverter.java b/src/main/java/com/google/devtools/common/options/EnumConverter.java
index f65241a214..a1b72aed1a 100644
--- a/src/main/java/com/google/devtools/common/options/EnumConverter.java
+++ b/src/main/java/com/google/devtools/common/options/EnumConverter.java
@@ -19,10 +19,10 @@ import java.util.Arrays;
/**
* A converter superclass for converters that parse enums.
*
- * Just subclass this class, creating a zero aro argument constructor that
+ * <p>Just subclass this class, creating a zero aro argument constructor that
* calls {@link #EnumConverter(Class, String)}.
*
- * This class compares the input string to the string returned by the toString()
+ * <p>This class compares the input string to the string returned by the toString()
* method of each enum member in a case-insensitive way. Usually, this is the
* name of the symbol, but beware if you override toString()!
*/
@@ -51,7 +51,7 @@ public abstract class EnumConverter<T extends Enum<T>>
* Implements {@link #convert(String)}.
*/
@Override
- public final T convert(String input) throws OptionsParsingException {
+ public T convert(String input) throws OptionsParsingException {
for (T value : enumType.getEnumConstants()) {
if (value.toString().equalsIgnoreCase(input)) {
return value;
diff --git a/src/main/java/com/google/devtools/common/options/OptionsParserImpl.java b/src/main/java/com/google/devtools/common/options/OptionsParserImpl.java
index e339dcd7f2..4cd504b81e 100644
--- a/src/main/java/com/google/devtools/common/options/OptionsParserImpl.java
+++ b/src/main/java/com/google/devtools/common/options/OptionsParserImpl.java
@@ -421,8 +421,8 @@ class OptionsParserImpl {
}
}
- private void addListValue(Field field, String name, Object value,
- OptionPriority priority, String source, String implicitDependant, String expandedFrom) {
+ private void addListValue(Field field, Object value, OptionPriority priority, String source,
+ String implicitDependant, String expandedFrom) {
ParsedOptionEntry entry = parsedValues.get(field);
if (entry == null) {
entry = new ParsedOptionEntry(ArrayListMultimap.create(), priority, source,
@@ -609,8 +609,8 @@ class OptionsParserImpl {
// Note: The type of the list member is not known; Java introspection
// only makes it available in String form via the signature string
// for the field declaration.
- addListValue(field, originalName, convertedValue,
- priority, sourceFunction.apply(originalName), implicitDependant, expandedFrom);
+ addListValue(field, convertedValue, priority, sourceFunction.apply(originalName),
+ implicitDependant, expandedFrom);
}
}
@@ -683,7 +683,9 @@ class OptionsParserImpl {
}
static boolean isBooleanField(Field field) {
- return field.getType().equals(boolean.class) || field.getType().equals(TriState.class);
+ return field.getType().equals(boolean.class)
+ || field.getType().equals(TriState.class)
+ || findConverter(field) instanceof BoolOrEnumConverter;
}
static boolean isSpecialNullDefault(String defaultValueString, Field optionField) {
diff --git a/src/test/java/com/google/devtools/common/options/BoolOrEnumConverterTest.java b/src/test/java/com/google/devtools/common/options/BoolOrEnumConverterTest.java
new file mode 100644
index 0000000000..ca8df3be4f
--- /dev/null
+++ b/src/test/java/com/google/devtools/common/options/BoolOrEnumConverterTest.java
@@ -0,0 +1,106 @@
+// Copyright 2014 Google Inc. 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.common.options;
+
+import static com.google.devtools.common.options.OptionsParser.newOptionsParser;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.fail;
+
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.JUnit4;
+
+/**
+ * A test for {@link BoolOrEnumConverter}.
+ */
+@RunWith(JUnit4.class)
+public class BoolOrEnumConverterTest {
+
+ private enum CompilationMode {
+ DBG, OPT
+ }
+
+ private static class CompilationModeConverter
+ extends BoolOrEnumConverter<CompilationMode> {
+
+ public CompilationModeConverter() {
+ super(CompilationMode.class, "compilation mode",
+ CompilationMode.DBG, CompilationMode.OPT);
+ }
+ }
+
+ /**
+ * The test options for the CompilationMode hybrid converter.
+ */
+ public static class CompilationModeTestOptions extends OptionsBase {
+ @Option(name = "compile_mode",
+ converter = CompilationModeConverter.class,
+ defaultValue = "dbg")
+ public CompilationMode compileMode;
+ }
+
+ @Test
+ public void converterFromEnum() throws Exception {
+ CompilationModeConverter converter = new CompilationModeConverter();
+ assertEquals(converter.convert("dbg"), CompilationMode.DBG);
+ assertEquals(converter.convert("opt"), CompilationMode.OPT);
+
+ try {
+ converter.convert("none");
+ fail();
+ } catch (OptionsParsingException e) {
+ assertEquals(e.getMessage(),
+ "Not a valid compilation mode: 'none' (should be dbg or opt)");
+ }
+ assertEquals("dbg or opt", converter.getTypeDescription());
+ }
+
+ @Test
+ public void convertFromBooleanValues() throws Exception {
+ String[] falseValues = new String[]{"false", "0"};
+ String[] trueValues = new String[]{"true", "1"};
+ CompilationModeConverter converter = new CompilationModeConverter();
+
+ for (String falseValue : falseValues) {
+ assertEquals(CompilationMode.OPT, converter.convert(falseValue));
+ }
+
+ for (String trueValue : trueValues) {
+ assertEquals(CompilationMode.DBG, converter.convert(trueValue));
+ }
+ }
+
+ @Test
+ public void prefixedWithNo() throws OptionsParsingException {
+ OptionsParser parser = newOptionsParser(CompilationModeTestOptions.class);
+ parser.parse("--nocompile_mode");
+ CompilationModeTestOptions options =
+ parser.getOptions(CompilationModeTestOptions.class);
+ assertNotNull(options.compileMode);
+ assertEquals(CompilationMode.OPT, options.compileMode);
+ }
+
+ @Test
+ public void missingValueAsBoolConversion() throws OptionsParsingException {
+ OptionsParser parser = newOptionsParser(CompilationModeTestOptions.class);
+ parser.parse("--compile_mode");
+ CompilationModeTestOptions options =
+ parser.getOptions(CompilationModeTestOptions.class);
+ assertNotNull(options.compileMode);
+ assertEquals(CompilationMode.DBG, options.compileMode);
+ }
+
+}