blob: 89976f5afbcfadc4d3d32ae11244006ec5b76b60 [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
Ray Milkey171b89a2016-07-28 15:22:26 -070012include_defs('//onos.defs')
Brian O'Connore4da59d2016-04-08 00:32:18 -070013
14def _get_name():
15 base_path = get_base_path()
16 return ONOS_ARTIFACT_BASE + base_path.replace('/', '-') #TODO Unix-separator
Brian O'Connor1f165982016-04-06 21:36:09 -070017
Brian O'Connor40a3fbd2016-06-08 13:54:46 -070018def checkstyle(
19 name,
20 srcs = None,
21 jar_target = None,
22 ):
23
24 if srcs and jar_target:
25 base = get_base_path()
26 files = '%s\n%s\n' % (name, base) + '\n'.join(['%s/%s' % (base, s) for s in srcs])
27
28 genrule(
29 name = name + '-checkstyle-files',
30 bash = "echo '%s' > $OUT" % files,
31 srcs = srcs,
32 out = 'checkstyle-files.txt',
33 )
34
35 sh_test(
36 name = name + '-checkstyle',
Thomas Vachuska275d2e82016-07-14 17:41:34 -070037 test = '//tools/build/conf:start-buck-daemon',
Brian O'Connor40a3fbd2016-06-08 13:54:46 -070038 deps = [ jar_target ],
39 args = [
Thomas Vachuska275d2e82016-07-14 17:41:34 -070040 '$(location //tools/build/conf:buck-daemon-jar)',
41 'checkstyle',
Brian O'Connor40a3fbd2016-06-08 13:54:46 -070042 '$(location :' + name + '-checkstyle-files)',
43 '$(location //tools/build/conf:checkstyle-xml)',
44 '$(location //tools/build/conf:suppressions-xml)',
45 ],
46 test_rule_timeout_ms = 20000,
47 labels = [ 'checkstyle' ],
48 )
49 else:
50 print 'Not generating checkstyle rule for %s because there are no sources.' % name
51
Ray Milkeyc340e282016-06-30 14:45:42 -070052def java_doc(
53 name,
54 title,
55 pkgs,
56 paths,
57 srcs = [],
58 deps = [],
59 visibility = [],
60 do_it_wrong = False,
61 ):
62 if do_it_wrong:
63 sourcepath = paths
64 else:
65 sourcepath = ['$SRCDIR/' + n for n in paths]
66 genrule(
67 name = name,
68 cmd = ' '.join([
69 'while ! test -f .buckconfig; do cd ..; done;',
70 'javadoc',
71 '-quiet',
72 '-protected',
73 '-encoding UTF-8',
74 '-charset UTF-8',
75 '-notimestamp',
76 '-windowtitle "' + title + '"',
77 '-link http://docs.oracle.com/javase/7/docs/api',
78 '-subpackages ',
79 ':'.join(pkgs),
80 '-sourcepath ',
81 ':'.join(sourcepath),
82 ' -classpath ',
83 ':'.join(['$(classpath %s)' % n for n in deps]),
84 '-d $TMP',
85 ]) + ';jar cf $OUT -C $TMP .',
86 srcs = srcs,
87 out = name + '.jar',
88 visibility = visibility,
89)
90
91
Brian O'Connor42c38cf2016-04-05 17:05:57 -070092def osgi_jar(
Brian O'Connore4da59d2016-04-08 00:32:18 -070093 name = None,
94 srcs = None,
Brian O'Connor1f165982016-04-06 21:36:09 -070095 group_id = ONOS_GROUP_ID,
96 version = ONOS_VERSION,
Brian O'Connor42c38cf2016-04-05 17:05:57 -070097 deps = [],
98 visibility = ['PUBLIC'],
99 license = 'NONE',
100 description = '',
101 debug = False,
Brian O'Connore5817c92016-04-06 15:41:48 -0700102 import_packages = '*',
103 export_packages = '*',
104 include_resources = NONE,
105 web_context = NONE,
Brian O'Connore4da59d2016-04-08 00:32:18 -0700106 resources = NONE,
Brian O'Connor0f6677d2016-04-06 23:26:51 -0700107 resources_root = None,
Brian O'Connor42c38cf2016-04-05 17:05:57 -0700108 **kwargs
109 ):
110
Brian O'Connore4da59d2016-04-08 00:32:18 -0700111 # if name and _get_name() != name:
112 # print _get_name(), '!=', name
113 if name is None:
114 name = _get_name()
115
116 if srcs is None:
117 srcs = glob([SRC + '/*.java'])
118
119 if resources == NONE and resources_root is not None:
120 resources = glob([resources_root + '**'])
121 elif resources == NONE:
122 resources = glob([RESOURCES_ROOT + '**'])
123
124 if resources and not resources_root:
125 resources_root = RESOURCES_ROOT
126
Brian O'Connor42c38cf2016-04-05 17:05:57 -0700127 bare_jar_name = name + '-jar'
128 osgi_jar_name = name + '-osgi'
129 mvn_coords = group_id + ':' + name + ':' + version
130
Brian O'Connor0f6677d2016-04-06 23:26:51 -0700131
Brian O'Connor42c38cf2016-04-05 17:05:57 -0700132 java_library(
133 name = bare_jar_name,
134 srcs = srcs,
135 deps = deps,
Brian O'Connor0f6677d2016-04-06 23:26:51 -0700136 visibility = [], #intentially, not visible
137 resources = resources,
138 resources_root = resources_root,
Brian O'Connor42c38cf2016-04-05 17:05:57 -0700139 **kwargs
140 )
141
Brian O'Connor4847ea32016-04-29 16:33:06 -0700142 cp = ':'.join(['$(classpath %s)' % c for c in deps]) if deps else '""'
Brian O'Connor42c38cf2016-04-05 17:05:57 -0700143
144 args = ( '$(location :%s)' % bare_jar_name, #input jar
145 '$OUT', #output jar
146 cp, #classpath
147 name, #bundle name
148 group_id, #group id
149 version, #version
150 license, #license url
Brian O'Connore5817c92016-04-06 15:41:48 -0700151 "'%s'" % import_packages, #packages to import
152 "'%s'" % export_packages, #packages to export
153 include_resources, #custom includes to classpath
Brian O'Connor42c38cf2016-04-05 17:05:57 -0700154 web_context, #web context (REST API only)
155 description, #description
156 )
157
158 #TODO stage_jar is a horrendous hack
159 stage_jar = 'pushd $SRCDIR; mkdir bin; cd bin; jar xf $(location :%s); ls; popd; ' % bare_jar_name
160 wrap_jar = '$(exe //utils/osgiwrap:osgi-jar) ' + ' '.join(args)
161 bash = stage_jar + wrap_jar
162 if debug:
163 bash = stage_jar + DEBUG_ARG + ' ' + wrap_jar
164 print bash
Brian O'Connor42c38cf2016-04-05 17:05:57 -0700165
166 genrule(
167 name = osgi_jar_name,
168 bash = bash,
Brian O'Connor1f165982016-04-06 21:36:09 -0700169 out = '%s-%s.jar' % (name, version), #FIXME add version to jar file
Brian O'Connore5817c92016-04-06 15:41:48 -0700170 srcs = glob(['src/main/webapp/**']),
Brian O'Connor42c38cf2016-04-05 17:05:57 -0700171 visibility = [], #intentially, not visible
172 )
173
174 # TODO we really should shade the jar with maven flavor
175 prebuilt_jar(
176 name = name,
177 maven_coords = mvn_coords,
178 binary_jar = ':' + osgi_jar_name,
179 visibility = visibility,
180 )
181
Brian O'Connor42c38cf2016-04-05 17:05:57 -0700182 ### Checkstyle
Brian O'Connor40a3fbd2016-06-08 13:54:46 -0700183 checkstyle(
Brian O'Connorbe95f682016-05-18 15:40:19 -0700184 name = name + '-checkstyle-files',
Brian O'Connor4847ea32016-04-29 16:33:06 -0700185 srcs = srcs,
Brian O'Connor40a3fbd2016-06-08 13:54:46 -0700186 jar_target = ':'+ bare_jar_name,
187 )
Brian O'Connorb3cc6042016-04-25 11:55:51 -0700188
Ray Milkeyc340e282016-06-30 14:45:42 -0700189 java_doc(
190 name = name + '-javadoc',
191 title = 'Java Docs',
192 pkgs = [ 'org.onosproject' ],
193 paths = [ 'src/main/java' ],
194 srcs = srcs,
195 deps = deps,
196 visibility = visibility,
197 do_it_wrong = False,
198 )
199
Brian O'Connorb3cc6042016-04-25 11:55:51 -0700200 # TODO add project config for intellij
201 # project_config(
202 # src_target = ':' + bare_jar_name,
203 # src_roots = [ 'src/main/java' ],
204 # test_target = ':' + name + '-tests',
205 # test_roots = [ 'src/test/java' ],
206 # )
207
Brian O'Connor42c38cf2016-04-05 17:05:57 -0700208 ### .m2 Install
Brian O'Connorb86c9202016-04-05 20:15:04 -0700209 mvn_cmd = ' '.join(( 'mvn install:install-file',
210 '-Dfile=$(location :%s)' % name,
211 '-DgroupId=%s' % group_id,
212 '-DartifactId=%s' % name,
213 '-Dversion=%s' % version,
214 '-Dpackaging=jar' ))
Brian O'Connore5817c92016-04-06 15:41:48 -0700215 cmd = mvn_cmd + ' > $OUT'
216 if FORCE_INSTALL:
217 # Add a random number to the command to force this rule to run.
218 # TODO We should make this configurable from CLI, perhaps with a flag.
219 cmd = 'FOO=%s ' % random.random() + cmd
Brian O'Connor42c38cf2016-04-05 17:05:57 -0700220 genrule(
221 name = name + '-install',
Brian O'Connorb86c9202016-04-05 20:15:04 -0700222 bash = cmd,
Brian O'Connor42c38cf2016-04-05 17:05:57 -0700223 out = 'install.log',
224 visibility = visibility,
225 )
226
Brian O'Connore4da59d2016-04-08 00:32:18 -0700227def osgi_jar_with_tests(
228 name = None,
229 deps = [],
230 test_srcs = None,
231 test_deps = [ '//lib:TEST' ],
232 test_resources = None,
233 test_resources_root = None,
234 visibility = [ 'PUBLIC' ],
235 **kwargs
236 ):
237
238 if name is None:
239 name = _get_name()
240
241 osgi_jar(name = name,
242 deps = deps,
243 visibility = visibility,
244 **kwargs)
Brian O'Connor0f6677d2016-04-06 23:26:51 -0700245
246 if test_resources and not test_resources_root:
Brian O'Connore4da59d2016-04-08 00:32:18 -0700247 test_resources_root = TEST_RESOURCES_ROOT
248 if test_resources_root and not test_resources:
249 test_resources = glob([test_resources_root + '**'])
250 if not test_resources and not test_resources_root:
251 test_resources = glob([TEST_RESOURCES_ROOT + '**'])
252 if test_resources:
253 test_resources_root = TEST_RESOURCES_ROOT
Brian O'Connor0f6677d2016-04-06 23:26:51 -0700254
Brian O'Connore4da59d2016-04-08 00:32:18 -0700255 if test_srcs is None:
256 test_srcs = glob([TEST + '/*.java'])
257
258 if not test_srcs:
259 print "Generating test rule for %s, but there are no tests." % name
260
261 java_test(
262 name = name + '-tests',
263 srcs = test_srcs,
264 deps = deps +
265 test_deps +
266 [':' + name + '-jar'],
267 source_under_test = [':' + name + '-jar'],
268 resources = test_resources,
269 resources_root = test_resources_root,
270 visibility = visibility
Brian O'Connorbe95f682016-05-18 15:40:19 -0700271 )
272
Brian O'Connor40a3fbd2016-06-08 13:54:46 -0700273 checkstyle(
274 name = name + '-tests',
275 srcs = test_srcs,
276 jar_target = ':' + name + '-tests',
277 )
278
Brian O'Connore642f7c2016-05-23 18:33:04 -0700279 #FIXME need to run checkstyle on test sources
Ray Milkeyc340e282016-06-30 14:45:42 -0700280
281
282