Brian O'Connor | e8468b5 | 2016-07-25 13:42:36 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | ce2a03d | 2017-08-03 19:21:03 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Brian O'Connor | e8468b5 | 2016-07-25 13:42:36 -0700 | [diff] [blame] | 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; |
Brian O'Connor | 3d59f71 | 2016-10-05 21:42:36 -0700 | [diff] [blame] | 45 | import com.facebook.buck.rules.SourcePath; |
Brian O'Connor | e8468b5 | 2016-07-25 13:42:36 -0700 | [diff] [blame] | 46 | import com.facebook.buck.rules.SourcePathResolver; |
| 47 | import com.facebook.buck.rules.SourcePaths; |
| 48 | import com.facebook.buck.rules.TargetGraph; |
Brian O'Connor | ee67495 | 2016-09-13 16:31:45 -0700 | [diff] [blame] | 49 | import com.google.common.base.Function; |
Brian O'Connor | e8468b5 | 2016-07-25 13:42:36 -0700 | [diff] [blame] | 50 | import com.google.common.base.Optional; |
Brian O'Connor | ee67495 | 2016-09-13 16:31:45 -0700 | [diff] [blame] | 51 | import com.google.common.base.Preconditions; |
Brian O'Connor | e8468b5 | 2016-07-25 13:42:36 -0700 | [diff] [blame] | 52 | import com.google.common.collect.ImmutableSet; |
Brian O'Connor | 3d59f71 | 2016-10-05 21:42:36 -0700 | [diff] [blame] | 53 | import com.google.common.collect.ImmutableSortedMap; |
Brian O'Connor | e8468b5 | 2016-07-25 13:42:36 -0700 | [diff] [blame] | 54 | import com.google.common.collect.ImmutableSortedSet; |
| 55 | import com.google.common.collect.Iterables; |
Brian O'Connor | 79b7067 | 2016-10-20 13:44:52 -0700 | [diff] [blame] | 56 | import com.google.common.collect.Lists; |
Brian O'Connor | e8468b5 | 2016-07-25 13:42:36 -0700 | [diff] [blame] | 57 | |
| 58 | import java.nio.file.Path; |
Brian O'Connor | 79b7067 | 2016-10-20 13:44:52 -0700 | [diff] [blame] | 59 | import java.util.List; |
Brian O'Connor | e8468b5 | 2016-07-25 13:42:36 -0700 | [diff] [blame] | 60 | |
| 61 | import static com.facebook.buck.jvm.common.ResourceValidator.validateResources; |
| 62 | |
| 63 | /** |
| 64 | * Description for the onos_jar rules. |
| 65 | * |
| 66 | * Currently, this only does Swagger generation. |
| 67 | */ |
Brian O'Connor | ee67495 | 2016-09-13 16:31:45 -0700 | [diff] [blame] | 68 | public class OnosJarDescription implements Description<OnosJarDescription.Arg>, Flavored { |
Brian O'Connor | e8468b5 | 2016-07-25 13:42:36 -0700 | [diff] [blame] | 69 | public static final BuildRuleType TYPE = BuildRuleType.of("onos_jar"); |
Brian O'Connor | ee67495 | 2016-09-13 16:31:45 -0700 | [diff] [blame] | 70 | public static final Flavor NON_OSGI_JAR = ImmutableFlavor.of("non-osgi"); |
| 71 | |
| 72 | public static final ImmutableSet<Flavor> SUPPORTED_FLAVORS = ImmutableSet.of( |
| 73 | JavaLibrary.SRC_JAR, |
| 74 | JavaLibrary.MAVEN_JAR, |
| 75 | JavaLibrary.JAVADOC_JAR, |
| 76 | NON_OSGI_JAR); |
| 77 | |
Brian O'Connor | e8468b5 | 2016-07-25 13:42:36 -0700 | [diff] [blame] | 78 | private final JavacOptions defaultJavacOptions; |
| 79 | private final JavaOptions defaultJavaOptions; |
| 80 | |
| 81 | public OnosJarDescription(BuckConfig config) { |
| 82 | JavaBuckConfig javaConfig = new JavaBuckConfig(config); |
| 83 | defaultJavacOptions = javaConfig.getDefaultJavacOptions(); |
| 84 | defaultJavaOptions = javaConfig.getDefaultJavaOptions(); |
| 85 | } |
| 86 | |
| 87 | @Override |
| 88 | public BuildRuleType getBuildRuleType() { |
| 89 | return TYPE; |
| 90 | } |
| 91 | |
| 92 | @Override |
| 93 | public Arg createUnpopulatedConstructorArg() { |
| 94 | return new Arg(); |
| 95 | } |
| 96 | |
| 97 | @Override |
| 98 | public <A extends Arg> BuildRule createBuildRule(TargetGraph targetGraph, |
| 99 | BuildRuleParams params, |
| 100 | BuildRuleResolver resolver, |
| 101 | A args) |
| 102 | throws NoSuchBuildTargetException { |
| 103 | |
| 104 | |
| 105 | SourcePathResolver pathResolver = new SourcePathResolver(resolver); |
| 106 | BuildTarget target = params.getBuildTarget(); |
| 107 | |
| 108 | // We know that the flavour we're being asked to create is valid, since the check is done when |
| 109 | // creating the action graph from the target graph. |
| 110 | |
| 111 | ImmutableSortedSet<Flavor> flavors = target.getFlavors(); |
| 112 | BuildRuleParams paramsWithMavenFlavor = null; |
| 113 | |
Brian O'Connor | ee67495 | 2016-09-13 16:31:45 -0700 | [diff] [blame] | 114 | if (flavors.contains(JavaLibrary.MAVEN_JAR)) { |
| 115 | paramsWithMavenFlavor = params; |
| 116 | |
| 117 | // Maven rules will depend upon their vanilla versions, so the latter have to be constructed |
| 118 | // without the maven flavor to prevent output-path conflict |
| 119 | params = params.copyWithBuildTarget( |
| 120 | params.getBuildTarget().withoutFlavors(ImmutableSet.of(JavaLibrary.MAVEN_JAR))); |
| 121 | } |
| 122 | |
| 123 | if (flavors.contains(JavaLibrary.SRC_JAR)) { |
| 124 | args.mavenCoords = args.mavenCoords.transform( |
| 125 | new Function<String, String>() { |
| 126 | @Override |
| 127 | public String apply(String input) { |
| 128 | return AetherUtil.addClassifier(input, AetherUtil.CLASSIFIER_SOURCES); |
| 129 | } |
| 130 | }); |
| 131 | |
| 132 | if (!flavors.contains(JavaLibrary.MAVEN_JAR)) { |
| 133 | return new JavaSourceJar( |
| 134 | params, |
| 135 | pathResolver, |
| 136 | args.srcs.get(), |
| 137 | args.mavenCoords); |
| 138 | } else { |
| 139 | return MavenUberJar.SourceJar.create( |
| 140 | Preconditions.checkNotNull(paramsWithMavenFlavor), |
| 141 | pathResolver, |
| 142 | args.srcs.get(), |
Brian O'Connor | 69e37d9 | 2016-10-12 15:05:09 -0700 | [diff] [blame] | 143 | args.mavenCoords, |
| 144 | Optional.absent()); //FIXME |
Brian O'Connor | ee67495 | 2016-09-13 16:31:45 -0700 | [diff] [blame] | 145 | } |
| 146 | } |
| 147 | |
| 148 | if (flavors.contains(JavaLibrary.JAVADOC_JAR)) { |
| 149 | args.mavenCoords = args.mavenCoords.transform( |
| 150 | new Function<String, String>() { |
| 151 | @Override |
| 152 | public String apply(String input) { |
| 153 | return AetherUtil.addClassifier(input, AetherUtil.CLASSIFIER_JAVADOC); |
| 154 | } |
| 155 | }); |
| 156 | |
Brian O'Connor | 3d59f71 | 2016-10-05 21:42:36 -0700 | [diff] [blame] | 157 | JavadocJar.JavadocArgs.Builder javadocArgs = JavadocJar.JavadocArgs.builder() |
| 158 | .addArg("-windowtitle", target.getShortName()) |
| 159 | .addArg("-link", "http://docs.oracle.com/javase/8/docs/api") |
| 160 | .addArg("-tag", "onos.rsModel:a:\"onos model\""); //FIXME from buckconfig + rule |
| 161 | |
| 162 | final ImmutableSortedMap.Builder<SourcePath, Path> javadocFiles = ImmutableSortedMap.naturalOrder(); |
| 163 | if (args.javadocFiles.isPresent()) { |
| 164 | for (SourcePath path : args.javadocFiles.get()) { |
| 165 | javadocFiles.put(path, |
| 166 | JavadocJar.getDocfileWithPath(pathResolver, path, args.javadocFilesRoot.orNull())); |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | |
Brian O'Connor | ee67495 | 2016-09-13 16:31:45 -0700 | [diff] [blame] | 171 | if (!flavors.contains(JavaLibrary.MAVEN_JAR)) { |
| 172 | return new JavadocJar( |
| 173 | params, |
| 174 | pathResolver, |
| 175 | args.srcs.get(), |
Brian O'Connor | 3d59f71 | 2016-10-05 21:42:36 -0700 | [diff] [blame] | 176 | javadocFiles.build(), |
| 177 | javadocArgs.build(), |
Brian O'Connor | ee67495 | 2016-09-13 16:31:45 -0700 | [diff] [blame] | 178 | args.mavenCoords); |
| 179 | } else { |
| 180 | return MavenUberJar.MavenJavadocJar.create( |
| 181 | Preconditions.checkNotNull(paramsWithMavenFlavor), |
| 182 | pathResolver, |
| 183 | args.srcs.get(), |
Brian O'Connor | 3d59f71 | 2016-10-05 21:42:36 -0700 | [diff] [blame] | 184 | javadocFiles.build(), |
| 185 | javadocArgs.build(), |
Brian O'Connor | 69e37d9 | 2016-10-12 15:05:09 -0700 | [diff] [blame] | 186 | args.mavenCoords, |
| 187 | Optional.absent()); //FIXME |
Brian O'Connor | ee67495 | 2016-09-13 16:31:45 -0700 | [diff] [blame] | 188 | } |
| 189 | } |
| 190 | |
Brian O'Connor | e8468b5 | 2016-07-25 13:42:36 -0700 | [diff] [blame] | 191 | JavacOptions javacOptions = JavacOptionsFactory.create( |
| 192 | defaultJavacOptions, |
| 193 | params, |
| 194 | resolver, |
| 195 | pathResolver, |
| 196 | args |
| 197 | ); |
| 198 | |
| 199 | BuildTarget abiJarTarget = params.getBuildTarget().withAppendedFlavors(CalculateAbi.FLAVOR); |
| 200 | |
| 201 | ImmutableSortedSet<BuildRule> exportedDeps = resolver.getAllRules(args.exportedDeps.get()); |
| 202 | |
Brian O'Connor | 79b7067 | 2016-10-20 13:44:52 -0700 | [diff] [blame] | 203 | // Build the resources string |
| 204 | List<String> resourceMappings = Lists.newArrayList(); |
| 205 | |
| 206 | if (args.includeResources.isPresent()) { |
| 207 | args.includeResources.get().entrySet().forEach(p -> |
| 208 | resourceMappings.add(String.format("%s=%s", p.getKey(), p.getValue()))); |
| 209 | } |
| 210 | |
| 211 | if (args.apiTitle.isPresent()) { |
| 212 | resourceMappings.add("WEB-INF/classes/apidoc/swagger.json=swagger.json"); |
| 213 | } |
| 214 | |
| 215 | Optional<String> includedResourcesString = Optional.of(String.join(",", resourceMappings)); |
Brian O'Connor | ee67495 | 2016-09-13 16:31:45 -0700 | [diff] [blame] | 216 | final DefaultJavaLibrary defaultJavaLibrary; |
| 217 | if (!flavors.contains(NON_OSGI_JAR)) { |
| 218 | defaultJavaLibrary = |
| 219 | resolver.addToIndex( |
| 220 | new OnosJar( |
| 221 | params.appendExtraDeps( |
| 222 | Iterables.concat( |
| 223 | BuildRules.getExportedRules( |
| 224 | Iterables.concat( |
| 225 | params.getDeclaredDeps().get(), |
| 226 | exportedDeps, |
| 227 | resolver.getAllRules(args.providedDeps.get()))), |
| 228 | pathResolver.filterBuildRuleInputs( |
| 229 | javacOptions.getInputs(pathResolver)))), |
| 230 | pathResolver, |
| 231 | args.srcs.get(), |
| 232 | validateResources( |
| 233 | pathResolver, |
| 234 | params.getProjectFilesystem(), |
| 235 | args.resources.get()), |
| 236 | javacOptions.getGeneratedSourceFolderName(), |
| 237 | args.proguardConfig.transform( |
| 238 | SourcePaths.toSourcePath(params.getProjectFilesystem())), |
| 239 | args.postprocessClassesCommands.get(), // FIXME this should be forbidden |
| 240 | exportedDeps, |
| 241 | resolver.getAllRules(args.providedDeps.get()), |
| 242 | new BuildTargetSourcePath(abiJarTarget), |
| 243 | javacOptions.trackClassUsage(), |
Brian O'Connor | e8468b5 | 2016-07-25 13:42:36 -0700 | [diff] [blame] | 244 | /* additionalClasspathEntries */ ImmutableSet.<Path>of(), |
Brian O'Connor | ee67495 | 2016-09-13 16:31:45 -0700 | [diff] [blame] | 245 | new OnosJarStepFactory(javacOptions, JavacOptionsAmender.IDENTITY, |
| 246 | args.webContext, args.apiTitle, args.apiVersion, |
| 247 | args.apiPackage, args.apiDescription, args.resources, |
| 248 | args.groupId, args.bundleName, args.bundleVersion, |
| 249 | args.bundleLicense, args.bundleDescription, args.importPackages, |
Bharat saraswal | a899a21 | 2017-02-28 13:19:57 +0530 | [diff] [blame] | 250 | args.exportPackages, includedResourcesString, |
| 251 | args.dynamicimportPackages, args.privatePackages), |
Brian O'Connor | ee67495 | 2016-09-13 16:31:45 -0700 | [diff] [blame] | 252 | args.resourcesRoot, |
Brian O'Connor | 69e37d9 | 2016-10-12 15:05:09 -0700 | [diff] [blame] | 253 | args.manifestFile, |
Brian O'Connor | ee67495 | 2016-09-13 16:31:45 -0700 | [diff] [blame] | 254 | args.mavenCoords, |
| 255 | args.tests.get(), |
| 256 | javacOptions.getClassesToRemoveFromJar(), |
| 257 | args.webContext, |
| 258 | args.apiTitle, |
| 259 | args.apiVersion, |
| 260 | args.apiPackage, |
Brian O'Connor | 79b7067 | 2016-10-20 13:44:52 -0700 | [diff] [blame] | 261 | args.apiDescription, |
| 262 | args.includeResources)); |
Brian O'Connor | ee67495 | 2016-09-13 16:31:45 -0700 | [diff] [blame] | 263 | } else { |
| 264 | defaultJavaLibrary = |
| 265 | resolver.addToIndex( |
| 266 | new DefaultJavaLibrary( |
| 267 | params.appendExtraDeps( |
| 268 | Iterables.concat( |
| 269 | BuildRules.getExportedRules( |
| 270 | Iterables.concat( |
| 271 | params.getDeclaredDeps().get(), |
| 272 | exportedDeps, |
| 273 | resolver.getAllRules(args.providedDeps.get()))), |
| 274 | pathResolver.filterBuildRuleInputs( |
| 275 | javacOptions.getInputs(pathResolver)))), |
| 276 | pathResolver, |
| 277 | args.srcs.get(), |
| 278 | validateResources( |
| 279 | pathResolver, |
| 280 | params.getProjectFilesystem(), |
| 281 | args.resources.get()), |
| 282 | javacOptions.getGeneratedSourceFolderName(), |
| 283 | args.proguardConfig.transform( |
| 284 | SourcePaths.toSourcePath(params.getProjectFilesystem())), |
| 285 | args.postprocessClassesCommands.get(), |
| 286 | exportedDeps, |
| 287 | resolver.getAllRules(args.providedDeps.get()), |
| 288 | new BuildTargetSourcePath(abiJarTarget), |
| 289 | javacOptions.trackClassUsage(), |
| 290 | /* additionalClasspathEntries */ ImmutableSet.<Path>of(), |
| 291 | new JavacToJarStepFactory(javacOptions, JavacOptionsAmender.IDENTITY), |
| 292 | args.resourcesRoot, |
Brian O'Connor | 69e37d9 | 2016-10-12 15:05:09 -0700 | [diff] [blame] | 293 | args.manifestFile, |
Brian O'Connor | ee67495 | 2016-09-13 16:31:45 -0700 | [diff] [blame] | 294 | args.mavenCoords, |
| 295 | args.tests.get(), |
| 296 | javacOptions.getClassesToRemoveFromJar())); |
| 297 | } |
Brian O'Connor | e8468b5 | 2016-07-25 13:42:36 -0700 | [diff] [blame] | 298 | |
| 299 | resolver.addToIndex( |
| 300 | CalculateAbi.of( |
| 301 | abiJarTarget, |
| 302 | pathResolver, |
| 303 | params, |
| 304 | new BuildTargetSourcePath(defaultJavaLibrary.getBuildTarget()))); |
| 305 | |
| 306 | return defaultJavaLibrary; |
| 307 | } |
| 308 | |
Brian O'Connor | ee67495 | 2016-09-13 16:31:45 -0700 | [diff] [blame] | 309 | @Override |
| 310 | public boolean hasFlavors(ImmutableSet<Flavor> flavors) { |
| 311 | return SUPPORTED_FLAVORS.containsAll(flavors); |
| 312 | } |
Brian O'Connor | e8468b5 | 2016-07-25 13:42:36 -0700 | [diff] [blame] | 313 | |
| 314 | public static class Arg extends JavaLibraryDescription.Arg { |
| 315 | public Optional<String> webContext; |
| 316 | public Optional<String> apiTitle; |
| 317 | public Optional<String> apiVersion; |
| 318 | public Optional<String> apiPackage; |
| 319 | public Optional<String> apiDescription; |
Brian O'Connor | ee67495 | 2016-09-13 16:31:45 -0700 | [diff] [blame] | 320 | |
| 321 | public Optional<String> groupId; |
| 322 | public Optional<String> bundleName; |
| 323 | public Optional<String> bundleVersion; |
| 324 | public Optional<String> bundleLicense; |
| 325 | public Optional<String> bundleDescription; |
| 326 | |
| 327 | public Optional<String> importPackages; |
Bharat saraswal | a899a21 | 2017-02-28 13:19:57 +0530 | [diff] [blame] | 328 | public Optional<String> privatePackages; |
Brian O'Connor | ee67495 | 2016-09-13 16:31:45 -0700 | [diff] [blame] | 329 | public Optional<String> exportPackages; |
Brian O'Connor | 79b7067 | 2016-10-20 13:44:52 -0700 | [diff] [blame] | 330 | public Optional<ImmutableSortedMap<String, SourcePath>> includeResources; |
Brian O'Connor | ee67495 | 2016-09-13 16:31:45 -0700 | [diff] [blame] | 331 | public Optional<String> dynamicimportPackages; |
Brian O'Connor | e8468b5 | 2016-07-25 13:42:36 -0700 | [diff] [blame] | 332 | } |
| 333 | } |