Home | History | Annotate | Download | only in options
      1 /*
      2  * Copyright 2016 Google Inc. All Rights Reserved.
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *     http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.google.turbine.options;
     18 
     19 import static com.google.common.base.Preconditions.checkNotNull;
     20 
     21 import com.google.common.collect.ImmutableList;
     22 import com.google.common.collect.ImmutableSet;
     23 import java.util.Optional;
     24 import javax.annotation.Nullable;
     25 
     26 /** Header compilation options. */
     27 public class TurbineOptions {
     28 
     29   private final Optional<String> output;
     30   private final ImmutableList<String> classPath;
     31   private final ImmutableSet<String> bootClassPath;
     32   private final Optional<String> release;
     33   private final Optional<String> system;
     34   private final ImmutableList<String> sources;
     35   private final ImmutableList<String> processorPath;
     36   private final ImmutableSet<String> processors;
     37   private final ImmutableList<String> sourceJars;
     38   private final Optional<String> outputDeps;
     39   private final ImmutableSet<String> directJars;
     40   private final Optional<String> targetLabel;
     41   private final Optional<String> injectingRuleKind;
     42   private final ImmutableList<String> depsArtifacts;
     43   private final boolean javacFallback;
     44   private final boolean help;
     45   private final ImmutableList<String> javacOpts;
     46   private final boolean shouldReduceClassPath;
     47 
     48   private TurbineOptions(
     49       @Nullable String output,
     50       ImmutableList<String> classPath,
     51       ImmutableSet<String> bootClassPath,
     52       @Nullable String release,
     53       @Nullable String system,
     54       ImmutableList<String> sources,
     55       ImmutableList<String> processorPath,
     56       ImmutableSet<String> processors,
     57       ImmutableList<String> sourceJars,
     58       @Nullable String outputDeps,
     59       ImmutableSet<String> directJars,
     60       @Nullable String targetLabel,
     61       @Nullable String injectingRuleKind,
     62       ImmutableList<String> depsArtifacts,
     63       boolean javacFallback,
     64       boolean help,
     65       ImmutableList<String> javacOpts,
     66       boolean shouldReduceClassPath) {
     67     this.output = Optional.ofNullable(output);
     68     this.classPath = checkNotNull(classPath, "classPath must not be null");
     69     this.bootClassPath = checkNotNull(bootClassPath, "bootClassPath must not be null");
     70     this.release = Optional.ofNullable(release);
     71     this.system = Optional.ofNullable(system);
     72     this.sources = checkNotNull(sources, "sources must not be null");
     73     this.processorPath = checkNotNull(processorPath, "processorPath must not be null");
     74     this.processors = checkNotNull(processors, "processors must not be null");
     75     this.sourceJars = checkNotNull(sourceJars, "sourceJars must not be null");
     76     this.outputDeps = Optional.ofNullable(outputDeps);
     77     this.directJars = checkNotNull(directJars, "directJars must not be null");
     78     this.targetLabel = Optional.ofNullable(targetLabel);
     79     this.injectingRuleKind = Optional.ofNullable(injectingRuleKind);
     80     this.depsArtifacts = checkNotNull(depsArtifacts, "depsArtifacts must not be null");
     81     this.javacFallback = javacFallback;
     82     this.help = help;
     83     this.javacOpts = checkNotNull(javacOpts, "javacOpts must not be null");
     84     this.shouldReduceClassPath = shouldReduceClassPath;
     85   }
     86 
     87   /** Paths to the Java source files to compile. */
     88   public ImmutableList<String> sources() {
     89     return sources;
     90   }
     91 
     92   /** Paths to classpath artifacts. */
     93   public ImmutableList<String> classPath() {
     94     return classPath;
     95   }
     96 
     97   /** Paths to compilation bootclasspath artifacts. */
     98   public ImmutableSet<String> bootClassPath() {
     99     return bootClassPath;
    100   }
    101 
    102   /** The target platform version. */
    103   public Optional<String> release() {
    104     return release;
    105   }
    106 
    107   /** The target platform's system modules. */
    108   public Optional<String> system() {
    109     return system;
    110   }
    111 
    112   /** The output jar. */
    113   @Nullable
    114   public Optional<String> output() {
    115     return output;
    116   }
    117 
    118   /**
    119    * The output jar.
    120    *
    121    * @deprecated use {@link #output} instead.
    122    */
    123   @Deprecated
    124   @Nullable
    125   public String outputFile() {
    126     return output.orElse(null);
    127   }
    128 
    129   /** Paths to annotation processor artifacts. */
    130   public ImmutableList<String> processorPath() {
    131     return processorPath;
    132   }
    133 
    134   /** Annotation processor class names. */
    135   public ImmutableSet<String> processors() {
    136     return processors;
    137   }
    138 
    139   /** Source jars for compilation. */
    140   public ImmutableList<String> sourceJars() {
    141     return sourceJars;
    142   }
    143 
    144   /** Output jdeps file. */
    145   public Optional<String> outputDeps() {
    146     return outputDeps;
    147   }
    148 
    149   /** The direct dependencies. */
    150   public ImmutableSet<String> directJars() {
    151     return directJars;
    152   }
    153 
    154   /** The label of the target being compiled. */
    155   public Optional<String> targetLabel() {
    156     return targetLabel;
    157   }
    158 
    159   /**
    160    * If present, the name of the rule that injected an aspect that compiles this target.
    161    *
    162    * <p>Note that this rule will have a completely different label to {@link #targetLabel} above.
    163    */
    164   public Optional<String> injectingRuleKind() {
    165     return injectingRuleKind;
    166   }
    167 
    168   /** The .jdeps artifacts for direct dependencies. */
    169   public ImmutableList<String> depsArtifacts() {
    170     return depsArtifacts;
    171   }
    172 
    173   /** Fall back to javac-turbine for error reporting. */
    174   public boolean javacFallback() {
    175     return javacFallback;
    176   }
    177 
    178   /** Print usage information. */
    179   public boolean help() {
    180     return help;
    181   }
    182 
    183   /** Additional Java compiler flags. */
    184   public ImmutableList<String> javacOpts() {
    185     return javacOpts;
    186   }
    187 
    188   /** Returns true if the reduced classpath optimization is enabled. */
    189   public boolean shouldReduceClassPath() {
    190     return shouldReduceClassPath;
    191   }
    192 
    193   public static Builder builder() {
    194     return new Builder();
    195   }
    196 
    197   /** A {@link Builder} for {@link TurbineOptions}. */
    198   public static class Builder {
    199 
    200     private String output;
    201     private final ImmutableList.Builder<String> classPath = ImmutableList.builder();
    202     private final ImmutableList.Builder<String> sources = ImmutableList.builder();
    203     private final ImmutableList.Builder<String> processorPath = ImmutableList.builder();
    204     private final ImmutableSet.Builder<String> processors = ImmutableSet.builder();
    205     private final ImmutableList.Builder<String> sourceJars = ImmutableList.builder();
    206     private final ImmutableSet.Builder<String> bootClassPath = ImmutableSet.builder();
    207     @Nullable private String release;
    208     @Nullable private String system;
    209     private String outputDeps;
    210     private final ImmutableSet.Builder<String> directJars = ImmutableSet.builder();
    211     @Nullable private String targetLabel;
    212     @Nullable private String injectingRuleKind;
    213     private final ImmutableList.Builder<String> depsArtifacts = ImmutableList.builder();
    214     private boolean javacFallback = true;
    215     private boolean help = false;
    216     private final ImmutableList.Builder<String> javacOpts = ImmutableList.builder();
    217     private boolean shouldReduceClassPath = true;
    218 
    219     public TurbineOptions build() {
    220       return new TurbineOptions(
    221           output,
    222           classPath.build(),
    223           bootClassPath.build(),
    224           release,
    225           system,
    226           sources.build(),
    227           processorPath.build(),
    228           processors.build(),
    229           sourceJars.build(),
    230           outputDeps,
    231           directJars.build(),
    232           targetLabel,
    233           injectingRuleKind,
    234           depsArtifacts.build(),
    235           javacFallback,
    236           help,
    237           javacOpts.build(),
    238           shouldReduceClassPath);
    239     }
    240 
    241     public Builder setOutput(String output) {
    242       this.output = output;
    243       return this;
    244     }
    245 
    246     public Builder addClassPathEntries(Iterable<String> classPath) {
    247       this.classPath.addAll(classPath);
    248       return this;
    249     }
    250 
    251     public Builder addBootClassPathEntries(Iterable<String> bootClassPath) {
    252       this.bootClassPath.addAll(bootClassPath);
    253       return this;
    254     }
    255 
    256     public Builder setRelease(String release) {
    257       this.release = release;
    258       return this;
    259     }
    260 
    261     public Builder setSystem(String system) {
    262       this.system = system;
    263       return this;
    264     }
    265 
    266     public Builder addSources(Iterable<String> sources) {
    267       this.sources.addAll(sources);
    268       return this;
    269     }
    270 
    271     public Builder addProcessorPathEntries(Iterable<String> processorPath) {
    272       this.processorPath.addAll(processorPath);
    273       return this;
    274     }
    275 
    276     public Builder addProcessors(Iterable<String> processors) {
    277       this.processors.addAll(processors);
    278       return this;
    279     }
    280 
    281     // TODO(cushon): remove this when turbine dependency is updated
    282     public Builder setTempDir(String tempDir) {
    283       return this;
    284     }
    285 
    286     public Builder setSourceJars(Iterable<String> sourceJars) {
    287       this.sourceJars.addAll(sourceJars);
    288       return this;
    289     }
    290 
    291     public Builder setOutputDeps(String outputDeps) {
    292       this.outputDeps = outputDeps;
    293       return this;
    294     }
    295 
    296     public Builder setTargetLabel(String targetLabel) {
    297       this.targetLabel = targetLabel;
    298       return this;
    299     }
    300 
    301     public Builder setInjectingRuleKind(String injectingRuleKind) {
    302       this.injectingRuleKind = injectingRuleKind;
    303       return this;
    304     }
    305 
    306     public Builder addAllDepsArtifacts(Iterable<String> depsArtifacts) {
    307       this.depsArtifacts.addAll(depsArtifacts);
    308       return this;
    309     }
    310 
    311     public Builder setJavacFallback(boolean javacFallback) {
    312       this.javacFallback = javacFallback;
    313       return this;
    314     }
    315 
    316     public Builder setHelp(boolean help) {
    317       this.help = help;
    318       return this;
    319     }
    320 
    321     public Builder addAllJavacOpts(Iterable<String> javacOpts) {
    322       this.javacOpts.addAll(javacOpts);
    323       return this;
    324     }
    325 
    326     public Builder setShouldReduceClassPath(boolean shouldReduceClassPath) {
    327       this.shouldReduceClassPath = shouldReduceClassPath;
    328       return this;
    329     }
    330 
    331     public Builder addDirectJars(ImmutableList<String> jars) {
    332       this.directJars.addAll(jars);
    333       return this;
    334     }
    335   }
    336 }
    337