blob: b9ec3d68353a466e5bd99ce0d16bd9522c2b1189 [file] [log] [blame]
Brian O'Connore8468b52016-07-25 13:42:36 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Brian O'Connore8468b52016-07-25 13:42:36 -07003 *
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.jvm.java.CompileToJarStepFactory;
19import com.facebook.buck.jvm.java.DefaultJavaLibrary;
Brian O'Connoree674952016-09-13 16:31:45 -070020import com.facebook.buck.jvm.java.HasMavenCoordinates;
21import com.facebook.buck.jvm.java.JavaLibrary;
22import com.facebook.buck.jvm.java.MavenPublishable;
Brian O'Connore8468b52016-07-25 13:42:36 -070023import com.facebook.buck.model.BuildTarget;
24import com.facebook.buck.rules.AddToRuleKey;
25import com.facebook.buck.rules.BuildRule;
26import com.facebook.buck.rules.BuildRuleParams;
27import com.facebook.buck.rules.SourcePath;
28import com.facebook.buck.rules.SourcePathResolver;
29import com.google.common.base.Optional;
30import com.google.common.collect.ImmutableList;
31import com.google.common.collect.ImmutableSet;
Brian O'Connor79b70672016-10-20 13:44:52 -070032import com.google.common.collect.ImmutableSortedMap;
Brian O'Connore8468b52016-07-25 13:42:36 -070033import com.google.common.collect.ImmutableSortedSet;
34
Brian O'Connor921593c2017-04-28 00:42:37 -070035import java.io.IOException;
36import java.math.BigInteger;
37import java.net.URL;
Brian O'Connore8468b52016-07-25 13:42:36 -070038import java.nio.file.Path;
Brian O'Connor921593c2017-04-28 00:42:37 -070039import java.security.DigestInputStream;
40import java.security.MessageDigest;
41import java.security.NoSuchAlgorithmException;
Brian O'Connore8468b52016-07-25 13:42:36 -070042import java.util.Set;
43import java.util.regex.Pattern;
44
45/**
46 * Implementation of a build rule that generates a onosjar.json file for a set
47 * of Java sources.
48 */
Brian O'Connoree674952016-09-13 16:31:45 -070049public class OnosJar extends DefaultJavaLibrary
50 implements MavenPublishable{
Brian O'Connore8468b52016-07-25 13:42:36 -070051
Brian O'Connor921593c2017-04-28 00:42:37 -070052 // Inject the SHA of this rule's jar into the rule key
53 private static String RULE_VERSION;
54 static {
55 URL pluginJarLocation = OnosJar.class.getProtectionDomain().getCodeSource().getLocation();
56 try {
57 MessageDigest md = MessageDigest.getInstance("SHA");
58 DigestInputStream dis = new DigestInputStream(pluginJarLocation.openStream(), md);
59 // Consume the InputStream...
60 while (dis.read() != -1);
61 RULE_VERSION = String.format("%032x", new BigInteger(1, md.digest()));
62 } catch (NoSuchAlgorithmException | IOException e) {
63 System.err.println("Failed to compute hash for OnosJar rule");
64 RULE_VERSION = "nil";
65 //TODO consider bailing here instead
66 }
67 }
68 @AddToRuleKey
69 private final String ruleVersion = RULE_VERSION;
70
Brian O'Connore8468b52016-07-25 13:42:36 -070071 @AddToRuleKey
72 final Optional<String> webContext;
73
74 @AddToRuleKey
75 final Optional<String> apiTitle;
76
77 @AddToRuleKey
78 final Optional<String> apiVersion;
79
80 @AddToRuleKey
81 final Optional<String> apiPackage;
82
83 @AddToRuleKey
84 final Optional<String> apiDescription;
85
Brian O'Connor79b70672016-10-20 13:44:52 -070086 @AddToRuleKey
87 final Optional<ImmutableSortedMap<String, SourcePath>> includedResources;
88
Brian O'Connoree674952016-09-13 16:31:45 -070089 private final ImmutableSortedSet<HasMavenCoordinates> mavenDeps;
90
Brian O'Connore8468b52016-07-25 13:42:36 -070091 public OnosJar(BuildRuleParams params,
92 SourcePathResolver resolver,
93 Set<? extends SourcePath> srcs,
94 Set<? extends SourcePath> resources,
95 Optional<Path> generatedSourceFolder,
96 Optional<SourcePath> proguardConfig,
97 ImmutableList<String> postprocessClassesCommands,
98 ImmutableSortedSet<BuildRule> exportedDeps,
99 ImmutableSortedSet<BuildRule> providedDeps,
100 SourcePath abiJar, boolean trackClassUsage,
101 ImmutableSet<Path> additionalClasspathEntries,
102 CompileToJarStepFactory compileStepFactory,
103 Optional<Path> resourcesRoot,
Brian O'Connor69e37d92016-10-12 15:05:09 -0700104 Optional<SourcePath> manifestFile,
Brian O'Connore8468b52016-07-25 13:42:36 -0700105 Optional<String> mavenCoords,
106 ImmutableSortedSet<BuildTarget> tests,
107 ImmutableSet<Pattern> classesToRemoveFromJar,
108 Optional<String> webContext,
109 Optional<String> apiTitle,
110 Optional<String> apiVersion,
111 Optional<String> apiPackage,
Brian O'Connor79b70672016-10-20 13:44:52 -0700112 Optional<String> apiDescription,
113 Optional<ImmutableSortedMap<String, SourcePath>> includedResources) {
Brian O'Connore8468b52016-07-25 13:42:36 -0700114 super(params, resolver, srcs, resources, generatedSourceFolder,
115 proguardConfig, postprocessClassesCommands, exportedDeps,
116 providedDeps, abiJar, trackClassUsage, additionalClasspathEntries,
Brian O'Connor69e37d92016-10-12 15:05:09 -0700117 compileStepFactory, resourcesRoot, manifestFile, mavenCoords,
118 tests, classesToRemoveFromJar);
Brian O'Connore8468b52016-07-25 13:42:36 -0700119 this.webContext = webContext;
120 this.apiTitle = apiTitle;
121 this.apiVersion = apiVersion;
122 this.apiPackage = apiPackage;
123 this.apiDescription = apiDescription;
Brian O'Connor79b70672016-10-20 13:44:52 -0700124 this.includedResources = includedResources;
Brian O'Connoree674952016-09-13 16:31:45 -0700125 this.mavenDeps = computeMavenDeps();
126 }
127
128 private ImmutableSortedSet<HasMavenCoordinates> computeMavenDeps() {
129 ImmutableSortedSet.Builder<HasMavenCoordinates> mavenDeps = ImmutableSortedSet.naturalOrder();
130 for (JavaLibrary javaLibrary : getTransitiveClasspathDeps()) {
131 if (this.equals(javaLibrary)) {
132 // no need to include ourself
133 continue;
134 } else if (HasMavenCoordinates.MAVEN_COORDS_PRESENT_PREDICATE.apply(javaLibrary)) {
135 mavenDeps.add(javaLibrary);
136 //FIXME BOC do we always want to exclude all of a maven jar's dependencies? probably.
137 mavenDeps.addAll(javaLibrary.getTransitiveClasspathDeps());
138 }
139 }
140 return mavenDeps.build();
141 }
142
143 @Override
144 public Iterable<HasMavenCoordinates> getMavenDeps() {
145 return mavenDeps;
146 }
147
148 @Override
149 public Iterable<BuildRule> getPackagedDependencies() {
150 //FIXME this is not supported at the moment
151 return ImmutableList.of();
Brian O'Connore8468b52016-07-25 13:42:36 -0700152 }
Brian O'Connor69e37d92016-10-12 15:05:09 -0700153
154 @Override
155 public Optional<Path> getPomTemplate() {
156 //FIXME we should consider supporting this
157 return Optional.absent();
158 }
Brian O'Connore8468b52016-07-25 13:42:36 -0700159}