blob: 7632ade0f92db19ec85986e2fd1a1b93b5cfa261 [file] [log] [blame]
Brian O'Connore8468b52016-07-25 13:42:36 -07001/*
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 */
16package org.onosproject.onosjar;
17
18import com.facebook.buck.cli.BuckConfig;
19import com.facebook.buck.jvm.java.CalculateAbi;
20import com.facebook.buck.jvm.java.DefaultJavaLibrary;
21import com.facebook.buck.jvm.java.JavaBuckConfig;
22import com.facebook.buck.jvm.java.JavaLibraryDescription;
23import com.facebook.buck.jvm.java.JavaOptions;
24import com.facebook.buck.jvm.java.JavacOptions;
25import com.facebook.buck.jvm.java.JavacOptionsAmender;
26import com.facebook.buck.jvm.java.JavacOptionsFactory;
27import com.facebook.buck.model.BuildTarget;
28import com.facebook.buck.model.Flavor;
29import com.facebook.buck.parser.NoSuchBuildTargetException;
30import com.facebook.buck.rules.BuildRule;
31import com.facebook.buck.rules.BuildRuleParams;
32import com.facebook.buck.rules.BuildRuleResolver;
33import com.facebook.buck.rules.BuildRuleType;
34import com.facebook.buck.rules.BuildRules;
35import com.facebook.buck.rules.BuildTargetSourcePath;
36import com.facebook.buck.rules.Description;
37import com.facebook.buck.rules.SourcePathResolver;
38import com.facebook.buck.rules.SourcePaths;
39import com.facebook.buck.rules.TargetGraph;
40import com.google.common.base.Optional;
41import com.google.common.collect.ImmutableSet;
42import com.google.common.collect.ImmutableSortedSet;
43import com.google.common.collect.Iterables;
44
45import java.nio.file.Path;
46
47import static com.facebook.buck.jvm.common.ResourceValidator.validateResources;
48
49/**
50 * Description for the onos_jar rules.
51 *
52 * Currently, this only does Swagger generation.
53 */
54public class OnosJarDescription implements Description<OnosJarDescription.Arg> {
55 public static final BuildRuleType TYPE = BuildRuleType.of("onos_jar");
56 private final JavacOptions defaultJavacOptions;
57 private final JavaOptions defaultJavaOptions;
58
59 public OnosJarDescription(BuckConfig config) {
60 JavaBuckConfig javaConfig = new JavaBuckConfig(config);
61 defaultJavacOptions = javaConfig.getDefaultJavacOptions();
62 defaultJavaOptions = javaConfig.getDefaultJavaOptions();
63 }
64
65 @Override
66 public BuildRuleType getBuildRuleType() {
67 return TYPE;
68 }
69
70 @Override
71 public Arg createUnpopulatedConstructorArg() {
72 return new Arg();
73 }
74
75 @Override
76 public <A extends Arg> BuildRule createBuildRule(TargetGraph targetGraph,
77 BuildRuleParams params,
78 BuildRuleResolver resolver,
79 A args)
80 throws NoSuchBuildTargetException {
81
82
83 SourcePathResolver pathResolver = new SourcePathResolver(resolver);
84 BuildTarget target = params.getBuildTarget();
85
86 // We know that the flavour we're being asked to create is valid, since the check is done when
87 // creating the action graph from the target graph.
88
89 ImmutableSortedSet<Flavor> flavors = target.getFlavors();
90 BuildRuleParams paramsWithMavenFlavor = null;
91
92 JavacOptions javacOptions = JavacOptionsFactory.create(
93 defaultJavacOptions,
94 params,
95 resolver,
96 pathResolver,
97 args
98 );
99
100 BuildTarget abiJarTarget = params.getBuildTarget().withAppendedFlavors(CalculateAbi.FLAVOR);
101
102 ImmutableSortedSet<BuildRule> exportedDeps = resolver.getAllRules(args.exportedDeps.get());
103
104 DefaultJavaLibrary defaultJavaLibrary =
105 resolver.addToIndex(
106 new OnosJar(
107 params.appendExtraDeps(
108 Iterables.concat(
109 BuildRules.getExportedRules(
110 Iterables.concat(
111 params.getDeclaredDeps().get(),
112 exportedDeps,
113 resolver.getAllRules(args.providedDeps.get()))),
114 pathResolver.filterBuildRuleInputs(
115 javacOptions.getInputs(pathResolver)))),
116 pathResolver,
117 args.srcs.get(),
118 validateResources(
119 pathResolver,
120 params.getProjectFilesystem(),
121 args.resources.get()),
122 javacOptions.getGeneratedSourceFolderName(),
123 args.proguardConfig.transform(
124 SourcePaths.toSourcePath(params.getProjectFilesystem())),
125 args.postprocessClassesCommands.get(), // FIXME this should be forbidden
126 exportedDeps,
127 resolver.getAllRules(args.providedDeps.get()),
128 new BuildTargetSourcePath(abiJarTarget),
129 javacOptions.trackClassUsage(),
130 /* additionalClasspathEntries */ ImmutableSet.<Path>of(),
131 new OnosJarStepFactory(javacOptions, JavacOptionsAmender.IDENTITY,
132 args.webContext, args.apiTitle, args.apiVersion,
133 args.apiPackage, args.apiDescription, args.resources),
134 args.resourcesRoot,
135 args.mavenCoords,
136 args.tests.get(),
137 javacOptions.getClassesToRemoveFromJar(),
138 args.webContext,
139 args.apiTitle,
140 args.apiVersion,
141 args.apiPackage,
142 args.apiDescription));
143
144 resolver.addToIndex(
145 CalculateAbi.of(
146 abiJarTarget,
147 pathResolver,
148 params,
149 new BuildTargetSourcePath(defaultJavaLibrary.getBuildTarget())));
150
151 return defaultJavaLibrary;
152 }
153
154
155 public static class Arg extends JavaLibraryDescription.Arg {
156 public Optional<String> webContext;
157 public Optional<String> apiTitle;
158 public Optional<String> apiVersion;
159 public Optional<String> apiPackage;
160 public Optional<String> apiDescription;
161 }
162}