blob: 109b7dd897126c62a528016540bafa9f86753fbb [file] [log] [blame]
Brian O'Connorb86c9202016-04-05 20:15:04 -07001import random
Brian O'Connor42c38cf2016-04-05 17:05:57 -07002
3DEBUG_ARG='JAVA_TOOL_OPTIONS="-Xdebug -Xrunjdwp:server=y,transport=dt_socket,address=5005,suspend=y"'
Brian O'Connore5817c92016-04-06 15:41:48 -07004FORCE_INSTALL=True
5NONE='NONE'
Brian O'Connor42c38cf2016-04-05 17:05:57 -07006
Brian O'Connore4da59d2016-04-08 00:32:18 -07007SRC = 'src/main/java/**/'
8TEST = 'src/test/java/**/'
Brian O'Connor0f6677d2016-04-06 23:26:51 -07009RESOURCES_ROOT = 'src/main/resources/'
Brian O'Connore4da59d2016-04-08 00:32:18 -070010TEST_RESOURCES_ROOT = 'src/test/resources/'
11
Brian O'Connor0f6677d2016-04-06 23:26:51 -070012
Brian O'Connor1f165982016-04-06 21:36:09 -070013ONOS_GROUP_ID = 'org.onosproject'
Brian O'Connore642f7c2016-05-23 18:33:04 -070014ONOS_VERSION = '1.7.0-SNAPSHOT'
Brian O'Connore4da59d2016-04-08 00:32:18 -070015ONOS_ARTIFACT_BASE = 'onos-'
16
17def _get_name():
18 base_path = get_base_path()
19 return ONOS_ARTIFACT_BASE + base_path.replace('/', '-') #TODO Unix-separator
Brian O'Connor1f165982016-04-06 21:36:09 -070020
Brian O'Connor40a3fbd2016-06-08 13:54:46 -070021def checkstyle(
22 name,
23 srcs = None,
24 jar_target = None,
25 ):
26
27 if srcs and jar_target:
28 base = get_base_path()
29 files = '%s\n%s\n' % (name, base) + '\n'.join(['%s/%s' % (base, s) for s in srcs])
30
31 genrule(
32 name = name + '-checkstyle-files',
33 bash = "echo '%s' > $OUT" % files,
34 srcs = srcs,
35 out = 'checkstyle-files.txt',
36 )
37
38 sh_test(
39 name = name + '-checkstyle',
Thomas Vachuska275d2e82016-07-14 17:41:34 -070040 test = '//tools/build/conf:start-buck-daemon',
Brian O'Connor40a3fbd2016-06-08 13:54:46 -070041 deps = [ jar_target ],
42 args = [
Thomas Vachuska275d2e82016-07-14 17:41:34 -070043 '$(location //tools/build/conf:buck-daemon-jar)',
44 'checkstyle',
Brian O'Connor40a3fbd2016-06-08 13:54:46 -070045 '$(location :' + name + '-checkstyle-files)',
46 '$(location //tools/build/conf:checkstyle-xml)',
47 '$(location //tools/build/conf:suppressions-xml)',
48 ],
49 test_rule_timeout_ms = 20000,
50 labels = [ 'checkstyle' ],
51 )
52 else:
53 print 'Not generating checkstyle rule for %s because there are no sources.' % name
54
Ray Milkeyc340e282016-06-30 14:45:42 -070055def java_doc(
56 name,
57 title,
58 pkgs,
59 paths,
60 srcs = [],
61 deps = [],
62 visibility = [],
63 do_it_wrong = False,
64 ):
65 if do_it_wrong:
66 sourcepath = paths
67 else:
68 sourcepath = ['$SRCDIR/' + n for n in paths]
69 genrule(
70 name = name,
71 cmd = ' '.join([
72 'while ! test -f .buckconfig; do cd ..; done;',
73 'javadoc',
74 '-quiet',
75 '-protected',
76 '-encoding UTF-8',
77 '-charset UTF-8',
78 '-notimestamp',
79 '-windowtitle "' + title + '"',
80 '-link http://docs.oracle.com/javase/7/docs/api',
81 '-subpackages ',
82 ':'.join(pkgs),
83 '-sourcepath ',
84 ':'.join(sourcepath),
85 ' -classpath ',
86 ':'.join(['$(classpath %s)' % n for n in deps]),
87 '-d $TMP',
88 ]) + ';jar cf $OUT -C $TMP .',
89 srcs = srcs,
90 out = name + '.jar',
91 visibility = visibility,
92)
93
94
Brian O'Connor42c38cf2016-04-05 17:05:57 -070095def osgi_jar(
Brian O'Connore4da59d2016-04-08 00:32:18 -070096 name = None,
97 srcs = None,
Brian O'Connor1f165982016-04-06 21:36:09 -070098 group_id = ONOS_GROUP_ID,
99 version = ONOS_VERSION,
Brian O'Connor42c38cf2016-04-05 17:05:57 -0700100 deps = [],
101 visibility = ['PUBLIC'],
102 license = 'NONE',
103 description = '',
104 debug = False,
Brian O'Connore5817c92016-04-06 15:41:48 -0700105 import_packages = '*',
106 export_packages = '*',
107 include_resources = NONE,
108 web_context = NONE,
Brian O'Connore4da59d2016-04-08 00:32:18 -0700109 resources = NONE,
Brian O'Connor0f6677d2016-04-06 23:26:51 -0700110 resources_root = None,
Brian O'Connor42c38cf2016-04-05 17:05:57 -0700111 **kwargs
112 ):
113
Brian O'Connore4da59d2016-04-08 00:32:18 -0700114 # if name and _get_name() != name:
115 # print _get_name(), '!=', name
116 if name is None:
117 name = _get_name()
118
119 if srcs is None:
120 srcs = glob([SRC + '/*.java'])
121
122 if resources == NONE and resources_root is not None:
123 resources = glob([resources_root + '**'])
124 elif resources == NONE:
125 resources = glob([RESOURCES_ROOT + '**'])
126
127 if resources and not resources_root:
128 resources_root = RESOURCES_ROOT
129
Brian O'Connor42c38cf2016-04-05 17:05:57 -0700130 bare_jar_name = name + '-jar'
131 osgi_jar_name = name + '-osgi'
132 mvn_coords = group_id + ':' + name + ':' + version
133
Brian O'Connor0f6677d2016-04-06 23:26:51 -0700134
Brian O'Connor42c38cf2016-04-05 17:05:57 -0700135 java_library(
136 name = bare_jar_name,
137 srcs = srcs,
138 deps = deps,
Brian O'Connor0f6677d2016-04-06 23:26:51 -0700139 visibility = [], #intentially, not visible
140 resources = resources,
141 resources_root = resources_root,
Brian O'Connor42c38cf2016-04-05 17:05:57 -0700142 **kwargs
143 )
144
Brian O'Connor4847ea32016-04-29 16:33:06 -0700145 cp = ':'.join(['$(classpath %s)' % c for c in deps]) if deps else '""'
Brian O'Connor42c38cf2016-04-05 17:05:57 -0700146
147 args = ( '$(location :%s)' % bare_jar_name, #input jar
148 '$OUT', #output jar
149 cp, #classpath
150 name, #bundle name
151 group_id, #group id
152 version, #version
153 license, #license url
Brian O'Connore5817c92016-04-06 15:41:48 -0700154 "'%s'" % import_packages, #packages to import
155 "'%s'" % export_packages, #packages to export
156 include_resources, #custom includes to classpath
Brian O'Connor42c38cf2016-04-05 17:05:57 -0700157 web_context, #web context (REST API only)
158 description, #description
159 )
160
161 #TODO stage_jar is a horrendous hack
162 stage_jar = 'pushd $SRCDIR; mkdir bin; cd bin; jar xf $(location :%s); ls; popd; ' % bare_jar_name
163 wrap_jar = '$(exe //utils/osgiwrap:osgi-jar) ' + ' '.join(args)
164 bash = stage_jar + wrap_jar
165 if debug:
166 bash = stage_jar + DEBUG_ARG + ' ' + wrap_jar
167 print bash
Brian O'Connor42c38cf2016-04-05 17:05:57 -0700168
169 genrule(
170 name = osgi_jar_name,
171 bash = bash,
Brian O'Connor1f165982016-04-06 21:36:09 -0700172 out = '%s-%s.jar' % (name, version), #FIXME add version to jar file
Brian O'Connore5817c92016-04-06 15:41:48 -0700173 srcs = glob(['src/main/webapp/**']),
Brian O'Connor42c38cf2016-04-05 17:05:57 -0700174 visibility = [], #intentially, not visible
175 )
176
177 # TODO we really should shade the jar with maven flavor
178 prebuilt_jar(
179 name = name,
180 maven_coords = mvn_coords,
181 binary_jar = ':' + osgi_jar_name,
182 visibility = visibility,
183 )
184
Brian O'Connor42c38cf2016-04-05 17:05:57 -0700185 ### Checkstyle
Brian O'Connor40a3fbd2016-06-08 13:54:46 -0700186 checkstyle(
Brian O'Connorbe95f682016-05-18 15:40:19 -0700187 name = name + '-checkstyle-files',
Brian O'Connor4847ea32016-04-29 16:33:06 -0700188 srcs = srcs,
Brian O'Connor40a3fbd2016-06-08 13:54:46 -0700189 jar_target = ':'+ bare_jar_name,
190 )
Brian O'Connorb3cc6042016-04-25 11:55:51 -0700191
Ray Milkeyc340e282016-06-30 14:45:42 -0700192 java_doc(
193 name = name + '-javadoc',
194 title = 'Java Docs',
195 pkgs = [ 'org.onosproject' ],
196 paths = [ 'src/main/java' ],
197 srcs = srcs,
198 deps = deps,
199 visibility = visibility,
200 do_it_wrong = False,
201 )
202
Brian O'Connorb3cc6042016-04-25 11:55:51 -0700203 # TODO add project config for intellij
204 # project_config(
205 # src_target = ':' + bare_jar_name,
206 # src_roots = [ 'src/main/java' ],
207 # test_target = ':' + name + '-tests',
208 # test_roots = [ 'src/test/java' ],
209 # )
210
Brian O'Connor42c38cf2016-04-05 17:05:57 -0700211 ### .m2 Install
Brian O'Connorb86c9202016-04-05 20:15:04 -0700212 mvn_cmd = ' '.join(( 'mvn install:install-file',
213 '-Dfile=$(location :%s)' % name,
214 '-DgroupId=%s' % group_id,
215 '-DartifactId=%s' % name,
216 '-Dversion=%s' % version,
217 '-Dpackaging=jar' ))
Brian O'Connore5817c92016-04-06 15:41:48 -0700218 cmd = mvn_cmd + ' > $OUT'
219 if FORCE_INSTALL:
220 # Add a random number to the command to force this rule to run.
221 # TODO We should make this configurable from CLI, perhaps with a flag.
222 cmd = 'FOO=%s ' % random.random() + cmd
Brian O'Connor42c38cf2016-04-05 17:05:57 -0700223 genrule(
224 name = name + '-install',
Brian O'Connorb86c9202016-04-05 20:15:04 -0700225 bash = cmd,
Brian O'Connor42c38cf2016-04-05 17:05:57 -0700226 out = 'install.log',
227 visibility = visibility,
228 )
229
Brian O'Connore4da59d2016-04-08 00:32:18 -0700230def osgi_jar_with_tests(
231 name = None,
232 deps = [],
233 test_srcs = None,
234 test_deps = [ '//lib:TEST' ],
235 test_resources = None,
236 test_resources_root = None,
237 visibility = [ 'PUBLIC' ],
238 **kwargs
239 ):
240
241 if name is None:
242 name = _get_name()
243
244 osgi_jar(name = name,
245 deps = deps,
246 visibility = visibility,
247 **kwargs)
Brian O'Connor0f6677d2016-04-06 23:26:51 -0700248
249 if test_resources and not test_resources_root:
Brian O'Connore4da59d2016-04-08 00:32:18 -0700250 test_resources_root = TEST_RESOURCES_ROOT
251 if test_resources_root and not test_resources:
252 test_resources = glob([test_resources_root + '**'])
253 if not test_resources and not test_resources_root:
254 test_resources = glob([TEST_RESOURCES_ROOT + '**'])
255 if test_resources:
256 test_resources_root = TEST_RESOURCES_ROOT
Brian O'Connor0f6677d2016-04-06 23:26:51 -0700257
Brian O'Connore4da59d2016-04-08 00:32:18 -0700258 if test_srcs is None:
259 test_srcs = glob([TEST + '/*.java'])
260
261 if not test_srcs:
262 print "Generating test rule for %s, but there are no tests." % name
263
264 java_test(
265 name = name + '-tests',
266 srcs = test_srcs,
267 deps = deps +
268 test_deps +
269 [':' + name + '-jar'],
270 source_under_test = [':' + name + '-jar'],
271 resources = test_resources,
272 resources_root = test_resources_root,
273 visibility = visibility
Brian O'Connorbe95f682016-05-18 15:40:19 -0700274 )
275
Brian O'Connor40a3fbd2016-06-08 13:54:46 -0700276 checkstyle(
277 name = name + '-tests',
278 srcs = test_srcs,
279 jar_target = ':' + name + '-tests',
280 )
281
Brian O'Connore642f7c2016-05-23 18:33:04 -0700282 #FIXME need to run checkstyle on test sources
Ray Milkeyc340e282016-06-30 14:45:42 -0700283
284
285