diff options
author | Googler <noreply@google.com> | 2015-05-11 15:20:37 +0000 |
---|---|---|
committer | Han-Wen Nienhuys <hanwen@google.com> | 2015-05-11 16:25:28 +0000 |
commit | 6a5d68692348a6cbfd8217457a041173d45b2aaa (patch) | |
tree | 39105e265061d56d82b59ebf789e4d628f547116 /src/main | |
parent | 2dc7f24b5180f3144c0a0041dd075dd22e3d493c (diff) |
Open-source J2ObjcConfiguration and J2ObjcCommandLineOptions.
--
MOS_MIGRATED_REVID=93308663
Diffstat (limited to 'src/main')
3 files changed, 153 insertions, 1 deletions
diff --git a/src/main/java/com/google/devtools/build/lib/bazel/rules/BazelRuleClassProvider.java b/src/main/java/com/google/devtools/build/lib/bazel/rules/BazelRuleClassProvider.java index 75f1428145..b7c95dcece 100644 --- a/src/main/java/com/google/devtools/build/lib/bazel/rules/BazelRuleClassProvider.java +++ b/src/main/java/com/google/devtools/build/lib/bazel/rules/BazelRuleClassProvider.java @@ -81,6 +81,8 @@ import com.google.devtools.build.lib.rules.objc.IosApplicationRule; import com.google.devtools.build.lib.rules.objc.IosDeviceRule; import com.google.devtools.build.lib.rules.objc.IosExtensionBinaryRule; import com.google.devtools.build.lib.rules.objc.IosExtensionRule; +import com.google.devtools.build.lib.rules.objc.J2ObjcCommandLineOptions; +import com.google.devtools.build.lib.rules.objc.J2ObjcConfiguration; import com.google.devtools.build.lib.rules.objc.ObjcBinaryRule; import com.google.devtools.build.lib.rules.objc.ObjcBuildInfoFactory; import com.google.devtools.build.lib.rules.objc.ObjcBundleLibraryRule; @@ -180,7 +182,8 @@ public class BazelRuleClassProvider { CppOptions.class, JavaOptions.class, PythonOptions.class, - ObjcCommandLineOptions.class + ObjcCommandLineOptions.class, + J2ObjcCommandLineOptions.class ); /** @@ -306,5 +309,6 @@ public class BazelRuleClassProvider { builder.addConfigurationFragment(new JvmConfigurationLoader(JAVA_CPU_SUPPLIER)); builder.addConfigurationFragment(new JavaConfigurationLoader(JAVA_CPU_SUPPLIER)); builder.addConfigurationFragment(new ObjcConfigurationLoader()); + builder.addConfigurationFragment(new J2ObjcConfiguration.Loader()); } } diff --git a/src/main/java/com/google/devtools/build/lib/rules/objc/J2ObjcCommandLineOptions.java b/src/main/java/com/google/devtools/build/lib/rules/objc/J2ObjcCommandLineOptions.java new file mode 100644 index 0000000000..d6d9cad47f --- /dev/null +++ b/src/main/java/com/google/devtools/build/lib/rules/objc/J2ObjcCommandLineOptions.java @@ -0,0 +1,40 @@ +// Copyright 2015 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.objc; + +import com.google.common.collect.Multimap; +import com.google.devtools.build.lib.analysis.config.FragmentOptions; +import com.google.devtools.build.lib.syntax.Label; +import com.google.devtools.common.options.Converters; +import com.google.devtools.common.options.Option; + +import java.util.List; + +/** + * Command-line Options for J2ObjC translation of Java source code to ObjC. + */ +public class J2ObjcCommandLineOptions extends FragmentOptions { + @Option(name = "j2objc_translation_flags", + converter = Converters.CommaSeparatedOptionListConverter.class, + allowMultiple = true, + defaultValue = "", + category = "undocumented", + help = "Specifies the translation flags for the J2ObjC transpiler." + ) + public List<String> translationFlags; + + @Override + public void addAllLabels(Multimap<String, Label> labelMap) {} +} diff --git a/src/main/java/com/google/devtools/build/lib/rules/objc/J2ObjcConfiguration.java b/src/main/java/com/google/devtools/build/lib/rules/objc/J2ObjcConfiguration.java new file mode 100644 index 0000000000..35b24d1ce9 --- /dev/null +++ b/src/main/java/com/google/devtools/build/lib/rules/objc/J2ObjcConfiguration.java @@ -0,0 +1,108 @@ +// Copyright 2015 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.objc; + +import com.google.common.base.Joiner; +import com.google.common.collect.ImmutableSet; +import com.google.devtools.build.lib.analysis.config.BuildConfiguration.Fragment; +import com.google.devtools.build.lib.analysis.config.BuildOptions; +import com.google.devtools.build.lib.analysis.config.ConfigurationEnvironment; +import com.google.devtools.build.lib.analysis.config.ConfigurationFragmentFactory; +import com.google.devtools.build.lib.analysis.config.InvalidConfigurationException; + +import java.util.Collections; +import java.util.Set; + +/** + * A J2ObjC transpiler configuration containing J2ObjC translation flags. + */ +public class J2ObjcConfiguration extends Fragment { + /** + * Always-on flags for J2ObjC translation. These flags will always be used for invoking the J2ObjC + * transpiler. See https://github.com/google/j2objc/wiki/j2objc for flag documentation. + */ + private static final Set<String> J2OBJC_ALWAYS_ON_TRANSLATION_FLAGS = ImmutableSet.of( + "--doc-comments", + "--extract-unsequenced", + "--final-methods-as-functions", + "--hide-private-members", + "--segmented-headers", + "-XcombineJars"); + + /** + * Allowed flags for J2ObjC translation. See https://github.com/google/j2objc/wiki/j2objc for flag + * documentation. + */ + static final Set<String> J2OBJC_BLACKLISTED_TRANSLATION_FLAGS = + ImmutableSet.of("--prefixes", "--prefix", "-x"); + + static final String INVALID_TRANSLATION_FLAGS_MSG_TEMPLATE = + "J2Objc translation flags: %s not supported. Unsupported flags are: %s"; + + /** + * Configuration loader for {@link J2ObjcConfiguration}. + */ + public static class Loader implements ConfigurationFragmentFactory { + @Override + public Fragment create(ConfigurationEnvironment env, BuildOptions buildOptions) + throws InvalidConfigurationException { + return new J2ObjcConfiguration(buildOptions.get(J2ObjcCommandLineOptions.class)); + } + + @Override + public Class<? extends Fragment> creates() { + return J2ObjcConfiguration.class; + } + } + + private Iterable<String> translationFlags; + private String cacheKey; + + J2ObjcConfiguration(J2ObjcCommandLineOptions j2ObjcOptions) throws InvalidConfigurationException { + Set<String> translationFlags = ImmutableSet.<String>builder() + .addAll(j2ObjcOptions.translationFlags) + .addAll(J2OBJC_ALWAYS_ON_TRANSLATION_FLAGS) + .build(); + + if (Collections.disjoint(translationFlags, J2OBJC_BLACKLISTED_TRANSLATION_FLAGS)) { + this.translationFlags = translationFlags; + this.cacheKey = Joiner.on(" ").join(this.translationFlags); + } else { + throw new InvalidConfigurationException(String.format(INVALID_TRANSLATION_FLAGS_MSG_TEMPLATE, + Joiner.on(",").join(translationFlags), + Joiner.on(",").join(J2OBJC_BLACKLISTED_TRANSLATION_FLAGS))); + } + } + + /** + * Returns the translation flags used to invoke the J2ObjC transpiler. The returned flags contain + * both the always-on flags from {@link #J2OBJC_ALWAYS_ON_TRANSLATION_FLAGS} and user-specified + * flags from {@link J2ObjcCommandLineOptions}. The set of valid flags can be found at + * {@link #J2OBJC_BLACKLISTED_TRANSLATION_FLAGS}. + */ + public Iterable<String> getTranslationFlags() { + return translationFlags; + } + + @Override + public String getName() { + return "J2ObjC"; + } + + @Override + public String cacheKey() { + return cacheKey; + } +} |