Brian O'Connor | e8468b5 | 2016-07-25 13:42:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016-present Open Networking Laboratory |
| 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 | package org.onosproject.onosjar; |
| 17 | |
| 18 | import com.facebook.buck.cli.BuckConfig; |
| 19 | import com.facebook.buck.jvm.java.CalculateAbi; |
| 20 | import com.facebook.buck.jvm.java.DefaultJavaLibrary; |
| 21 | import com.facebook.buck.jvm.java.JavaBuckConfig; |
Brian O'Connor | ee67495 | 2016-09-13 16:31:45 -0700 | [diff] [blame] | 22 | import com.facebook.buck.jvm.java.JavaLibrary; |
Brian O'Connor | e8468b5 | 2016-07-25 13:42:36 -0700 | [diff] [blame] | 23 | import com.facebook.buck.jvm.java.JavaLibraryDescription; |
| 24 | import com.facebook.buck.jvm.java.JavaOptions; |
Brian O'Connor | ee67495 | 2016-09-13 16:31:45 -0700 | [diff] [blame] | 25 | import com.facebook.buck.jvm.java.JavaSourceJar; |
Brian O'Connor | e8468b5 | 2016-07-25 13:42:36 -0700 | [diff] [blame] | 26 | import com.facebook.buck.jvm.java.JavacOptions; |
| 27 | import com.facebook.buck.jvm.java.JavacOptionsAmender; |
| 28 | import com.facebook.buck.jvm.java.JavacOptionsFactory; |
Brian O'Connor | ee67495 | 2016-09-13 16:31:45 -0700 | [diff] [blame] | 29 | import com.facebook.buck.jvm.java.JavacToJarStepFactory; |
| 30 | import com.facebook.buck.jvm.java.JavadocJar; |
| 31 | import com.facebook.buck.jvm.java.MavenUberJar; |
| 32 | import com.facebook.buck.maven.AetherUtil; |
Brian O'Connor | e8468b5 | 2016-07-25 13:42:36 -0700 | [diff] [blame] | 33 | import com.facebook.buck.model.BuildTarget; |
| 34 | import com.facebook.buck.model.Flavor; |
Brian O'Connor | ee67495 | 2016-09-13 16:31:45 -0700 | [diff] [blame] | 35 | import com.facebook.buck.model.Flavored; |
| 36 | import com.facebook.buck.model.ImmutableFlavor; |
Brian O'Connor | e8468b5 | 2016-07-25 13:42:36 -0700 | [diff] [blame] | 37 | import com.facebook.buck.parser.NoSuchBuildTargetException; |
| 38 | import com.facebook.buck.rules.BuildRule; |
| 39 | import com.facebook.buck.rules.BuildRuleParams; |
| 40 | import com.facebook.buck.rules.BuildRuleResolver; |
| 41 | import com.facebook.buck.rules.BuildRuleType; |
| 42 | import com.facebook.buck.rules.BuildRules; |
| 43 | import com.facebook.buck.rules.BuildTargetSourcePath; |
| 44 | import com.facebook.buck.rules.Description; |
| 45 | import com.facebook.buck.rules.SourcePathResolver; |
| 46 | import com.facebook.buck.rules.SourcePaths; |
| 47 | import com.facebook.buck.rules.TargetGraph; |
Brian O'Connor | ee67495 | 2016-09-13 16:31:45 -0700 | [diff] [blame] | 48 | import com.google.common.base.Function; |
Brian O'Connor | e8468b5 | 2016-07-25 13:42:36 -0700 | [diff] [blame] | 49 | import com.google.common.base.Optional; |
Brian O'Connor | ee67495 | 2016-09-13 16:31:45 -0700 | [diff] [blame] | 50 | import com.google.common.base.Preconditions; |
Brian O'Connor | e8468b5 | 2016-07-25 13:42:36 -0700 | [diff] [blame] | 51 | import com.google.common.collect.ImmutableSet; |
| 52 | import com.google.common.collect.ImmutableSortedSet; |
| 53 | import com.google.common.collect.Iterables; |
| 54 | |
| 55 | import java.nio.file.Path; |
| 56 | |
| 57 | import static com.facebook.buck.jvm.common.ResourceValidator.validateResources; |
| 58 | |
| 59 | /** |
| 60 | * Description for the onos_jar rules. |
| 61 | * |
| 62 | * Currently, this only does Swagger generation. |
| 63 | */ |
Brian O'Connor | ee67495 | 2016-09-13 16:31:45 -0700 | [diff] [blame] | 64 | public class OnosJarDescription implements Description<OnosJarDescription.Arg>, Flavored { |
Brian O'Connor | e8468b5 | 2016-07-25 13:42:36 -0700 | [diff] [blame] | 65 | public static final BuildRuleType TYPE = BuildRuleType.of("onos_jar"); |
Brian O'Connor | ee67495 | 2016-09-13 16:31:45 -0700 | [diff] [blame] | 66 | public static final Flavor NON_OSGI_JAR = ImmutableFlavor.of("non-osgi"); |
| 67 | |
| 68 | public static final ImmutableSet<Flavor> SUPPORTED_FLAVORS = ImmutableSet.of( |
| 69 | JavaLibrary.SRC_JAR, |
| 70 | JavaLibrary.MAVEN_JAR, |
| 71 | JavaLibrary.JAVADOC_JAR, |
| 72 | NON_OSGI_JAR); |
| 73 | |
Brian O'Connor | e8468b5 | 2016-07-25 13:42:36 -0700 | [diff] [blame] | 74 | private final JavacOptions defaultJavacOptions; |
| 75 | private final JavaOptions defaultJavaOptions; |
| 76 | |
| 77 | public OnosJarDescription(BuckConfig config) { |
| 78 | JavaBuckConfig javaConfig = new JavaBuckConfig(config); |
| 79 | defaultJavacOptions = javaConfig.getDefaultJavacOptions(); |
| 80 | defaultJavaOptions = javaConfig.getDefaultJavaOptions(); |
| 81 | } |
| 82 | |
| 83 | @Override |
| 84 | public BuildRuleType getBuildRuleType() { |
| 85 | return TYPE; |
| 86 | } |
| 87 | |
| 88 | @Override |
| 89 | public Arg createUnpopulatedConstructorArg() { |
| 90 | return new Arg(); |
| 91 | } |
| 92 | |
| 93 | @Override |
| 94 | public <A extends Arg> BuildRule createBuildRule(TargetGraph targetGraph, |
| 95 | BuildRuleParams params, |
| 96 | BuildRuleResolver resolver, |
| 97 | A args) |
| 98 | throws NoSuchBuildTargetException { |
| 99 | |
| 100 | |
| 101 | SourcePathResolver pathResolver = new SourcePathResolver(resolver); |
| 102 | BuildTarget target = params.getBuildTarget(); |
| 103 | |
| 104 | // We know that the flavour we're being asked to create is valid, since the check is done when |
| 105 | // creating the action graph from the target graph. |
| 106 | |
| 107 | ImmutableSortedSet<Flavor> flavors = target.getFlavors(); |
| 108 | BuildRuleParams paramsWithMavenFlavor = null; |
| 109 | |
Brian O'Connor | ee67495 | 2016-09-13 16:31:45 -0700 | [diff] [blame] | 110 | if (flavors.contains(JavaLibrary.MAVEN_JAR)) { |
| 111 | paramsWithMavenFlavor = params; |
| 112 | |
| 113 | // Maven rules will depend upon their vanilla versions, so the latter have to be constructed |
| 114 | // without the maven flavor to prevent output-path conflict |
| 115 | params = params.copyWithBuildTarget( |
| 116 | params.getBuildTarget().withoutFlavors(ImmutableSet.of(JavaLibrary.MAVEN_JAR))); |
| 117 | } |
| 118 | |
| 119 | if (flavors.contains(JavaLibrary.SRC_JAR)) { |
| 120 | args.mavenCoords = args.mavenCoords.transform( |
| 121 | new Function<String, String>() { |
| 122 | @Override |
| 123 | public String apply(String input) { |
| 124 | return AetherUtil.addClassifier(input, AetherUtil.CLASSIFIER_SOURCES); |
| 125 | } |
| 126 | }); |
| 127 | |
| 128 | if (!flavors.contains(JavaLibrary.MAVEN_JAR)) { |
| 129 | return new JavaSourceJar( |
| 130 | params, |
| 131 | pathResolver, |
| 132 | args.srcs.get(), |
| 133 | args.mavenCoords); |
| 134 | } else { |
| 135 | return MavenUberJar.SourceJar.create( |
| 136 | Preconditions.checkNotNull(paramsWithMavenFlavor), |
| 137 | pathResolver, |
| 138 | args.srcs.get(), |
| 139 | args.mavenCoords); |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | if (flavors.contains(JavaLibrary.JAVADOC_JAR)) { |
| 144 | args.mavenCoords = args.mavenCoords.transform( |
| 145 | new Function<String, String>() { |
| 146 | @Override |
| 147 | public String apply(String input) { |
| 148 | return AetherUtil.addClassifier(input, AetherUtil.CLASSIFIER_JAVADOC); |
| 149 | } |
| 150 | }); |
| 151 | |
| 152 | if (!flavors.contains(JavaLibrary.MAVEN_JAR)) { |
| 153 | return new JavadocJar( |
| 154 | params, |
| 155 | pathResolver, |
| 156 | args.srcs.get(), |
| 157 | args.mavenCoords); |
| 158 | } else { |
| 159 | return MavenUberJar.MavenJavadocJar.create( |
| 160 | Preconditions.checkNotNull(paramsWithMavenFlavor), |
| 161 | pathResolver, |
| 162 | args.srcs.get(), |
| 163 | args.mavenCoords); |
| 164 | } |
| 165 | } |
| 166 | |
Brian O'Connor | e8468b5 | 2016-07-25 13:42:36 -0700 | [diff] [blame] | 167 | JavacOptions javacOptions = JavacOptionsFactory.create( |
| 168 | defaultJavacOptions, |
| 169 | params, |
| 170 | resolver, |
| 171 | pathResolver, |
| 172 | args |
| 173 | ); |
| 174 | |
| 175 | BuildTarget abiJarTarget = params.getBuildTarget().withAppendedFlavors(CalculateAbi.FLAVOR); |
| 176 | |
| 177 | ImmutableSortedSet<BuildRule> exportedDeps = resolver.getAllRules(args.exportedDeps.get()); |
| 178 | |
Brian O'Connor | ee67495 | 2016-09-13 16:31:45 -0700 | [diff] [blame] | 179 | final DefaultJavaLibrary defaultJavaLibrary; |
| 180 | if (!flavors.contains(NON_OSGI_JAR)) { |
| 181 | defaultJavaLibrary = |
| 182 | resolver.addToIndex( |
| 183 | new OnosJar( |
| 184 | params.appendExtraDeps( |
| 185 | Iterables.concat( |
| 186 | BuildRules.getExportedRules( |
| 187 | Iterables.concat( |
| 188 | params.getDeclaredDeps().get(), |
| 189 | exportedDeps, |
| 190 | resolver.getAllRules(args.providedDeps.get()))), |
| 191 | pathResolver.filterBuildRuleInputs( |
| 192 | javacOptions.getInputs(pathResolver)))), |
| 193 | pathResolver, |
| 194 | args.srcs.get(), |
| 195 | validateResources( |
| 196 | pathResolver, |
| 197 | params.getProjectFilesystem(), |
| 198 | args.resources.get()), |
| 199 | javacOptions.getGeneratedSourceFolderName(), |
| 200 | args.proguardConfig.transform( |
| 201 | SourcePaths.toSourcePath(params.getProjectFilesystem())), |
| 202 | args.postprocessClassesCommands.get(), // FIXME this should be forbidden |
| 203 | exportedDeps, |
| 204 | resolver.getAllRules(args.providedDeps.get()), |
| 205 | new BuildTargetSourcePath(abiJarTarget), |
| 206 | javacOptions.trackClassUsage(), |
Brian O'Connor | e8468b5 | 2016-07-25 13:42:36 -0700 | [diff] [blame] | 207 | /* additionalClasspathEntries */ ImmutableSet.<Path>of(), |
Brian O'Connor | ee67495 | 2016-09-13 16:31:45 -0700 | [diff] [blame] | 208 | new OnosJarStepFactory(javacOptions, JavacOptionsAmender.IDENTITY, |
| 209 | args.webContext, args.apiTitle, args.apiVersion, |
| 210 | args.apiPackage, args.apiDescription, args.resources, |
| 211 | args.groupId, args.bundleName, args.bundleVersion, |
| 212 | args.bundleLicense, args.bundleDescription, args.importPackages, |
| 213 | args.exportPackages, args.includeResources, args.dynamicimportPackages), |
| 214 | args.resourcesRoot, |
| 215 | args.mavenCoords, |
| 216 | args.tests.get(), |
| 217 | javacOptions.getClassesToRemoveFromJar(), |
| 218 | args.webContext, |
| 219 | args.apiTitle, |
| 220 | args.apiVersion, |
| 221 | args.apiPackage, |
| 222 | args.apiDescription)); |
| 223 | } else { |
| 224 | defaultJavaLibrary = |
| 225 | resolver.addToIndex( |
| 226 | new DefaultJavaLibrary( |
| 227 | params.appendExtraDeps( |
| 228 | Iterables.concat( |
| 229 | BuildRules.getExportedRules( |
| 230 | Iterables.concat( |
| 231 | params.getDeclaredDeps().get(), |
| 232 | exportedDeps, |
| 233 | resolver.getAllRules(args.providedDeps.get()))), |
| 234 | pathResolver.filterBuildRuleInputs( |
| 235 | javacOptions.getInputs(pathResolver)))), |
| 236 | pathResolver, |
| 237 | args.srcs.get(), |
| 238 | validateResources( |
| 239 | pathResolver, |
| 240 | params.getProjectFilesystem(), |
| 241 | args.resources.get()), |
| 242 | javacOptions.getGeneratedSourceFolderName(), |
| 243 | args.proguardConfig.transform( |
| 244 | SourcePaths.toSourcePath(params.getProjectFilesystem())), |
| 245 | args.postprocessClassesCommands.get(), |
| 246 | exportedDeps, |
| 247 | resolver.getAllRules(args.providedDeps.get()), |
| 248 | new BuildTargetSourcePath(abiJarTarget), |
| 249 | javacOptions.trackClassUsage(), |
| 250 | /* additionalClasspathEntries */ ImmutableSet.<Path>of(), |
| 251 | new JavacToJarStepFactory(javacOptions, JavacOptionsAmender.IDENTITY), |
| 252 | args.resourcesRoot, |
| 253 | args.mavenCoords, |
| 254 | args.tests.get(), |
| 255 | javacOptions.getClassesToRemoveFromJar())); |
| 256 | } |
Brian O'Connor | e8468b5 | 2016-07-25 13:42:36 -0700 | [diff] [blame] | 257 | |
| 258 | resolver.addToIndex( |
| 259 | CalculateAbi.of( |
| 260 | abiJarTarget, |
| 261 | pathResolver, |
| 262 | params, |
| 263 | new BuildTargetSourcePath(defaultJavaLibrary.getBuildTarget()))); |
| 264 | |
| 265 | return defaultJavaLibrary; |
| 266 | } |
| 267 | |
Brian O'Connor | ee67495 | 2016-09-13 16:31:45 -0700 | [diff] [blame] | 268 | @Override |
| 269 | public boolean hasFlavors(ImmutableSet<Flavor> flavors) { |
| 270 | return SUPPORTED_FLAVORS.containsAll(flavors); |
| 271 | } |
Brian O'Connor | e8468b5 | 2016-07-25 13:42:36 -0700 | [diff] [blame] | 272 | |
| 273 | public static class Arg extends JavaLibraryDescription.Arg { |
| 274 | public Optional<String> webContext; |
| 275 | public Optional<String> apiTitle; |
| 276 | public Optional<String> apiVersion; |
| 277 | public Optional<String> apiPackage; |
| 278 | public Optional<String> apiDescription; |
Brian O'Connor | ee67495 | 2016-09-13 16:31:45 -0700 | [diff] [blame] | 279 | |
| 280 | public Optional<String> groupId; |
| 281 | public Optional<String> bundleName; |
| 282 | public Optional<String> bundleVersion; |
| 283 | public Optional<String> bundleLicense; |
| 284 | public Optional<String> bundleDescription; |
| 285 | |
| 286 | public Optional<String> importPackages; |
| 287 | public Optional<String> exportPackages; |
| 288 | public Optional<String> includeResources; |
| 289 | public Optional<String> dynamicimportPackages; |
Brian O'Connor | e8468b5 | 2016-07-25 13:42:36 -0700 | [diff] [blame] | 290 | } |
| 291 | } |