blob: b415fa884e16011494d601413c1e36484c04da4b [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 )
Brian O'Connor40a3fbd2016-06-08 13:54:46 -070049
Ray Milkeyc340e282016-06-30 14:45:42 -070050def java_doc(
51 name,
52 title,
53 pkgs,
54 paths,
55 srcs = [],
56 deps = [],
57 visibility = [],
58 do_it_wrong = False,
59 ):
60 if do_it_wrong:
61 sourcepath = paths
62 else:
63 sourcepath = ['$SRCDIR/' + n for n in paths]
64 genrule(
65 name = name,
66 cmd = ' '.join([
67 'while ! test -f .buckconfig; do cd ..; done;',
68 'javadoc',
69 '-quiet',
70 '-protected',
71 '-encoding UTF-8',
72 '-charset UTF-8',
73 '-notimestamp',
74 '-windowtitle "' + title + '"',
75 '-link http://docs.oracle.com/javase/7/docs/api',
76 '-subpackages ',
77 ':'.join(pkgs),
78 '-sourcepath ',
79 ':'.join(sourcepath),
80 ' -classpath ',
81 ':'.join(['$(classpath %s)' % n for n in deps]),
82 '-d $TMP',
83 ]) + ';jar cf $OUT -C $TMP .',
84 srcs = srcs,
85 out = name + '.jar',
86 visibility = visibility,
87)
88
89
Brian O'Connor42c38cf2016-04-05 17:05:57 -070090def osgi_jar(
Brian O'Connore4da59d2016-04-08 00:32:18 -070091 name = None,
92 srcs = None,
Brian O'Connor1f165982016-04-06 21:36:09 -070093 group_id = ONOS_GROUP_ID,
94 version = ONOS_VERSION,
Brian O'Connor42c38cf2016-04-05 17:05:57 -070095 deps = [],
96 visibility = ['PUBLIC'],
97 license = 'NONE',
98 description = '',
99 debug = False,
Brian O'Connore5817c92016-04-06 15:41:48 -0700100 import_packages = '*',
101 export_packages = '*',
102 include_resources = NONE,
103 web_context = NONE,
Brian O'Connore4da59d2016-04-08 00:32:18 -0700104 resources = NONE,
Brian O'Connor0f6677d2016-04-06 23:26:51 -0700105 resources_root = None,
Brian O'Connor42c38cf2016-04-05 17:05:57 -0700106 **kwargs
107 ):
108
Brian O'Connore4da59d2016-04-08 00:32:18 -0700109 # if name and _get_name() != name:
110 # print _get_name(), '!=', name
111 if name is None:
112 name = _get_name()
113
114 if srcs is None:
115 srcs = glob([SRC + '/*.java'])
116
117 if resources == NONE and resources_root is not None:
118 resources = glob([resources_root + '**'])
119 elif resources == NONE:
120 resources = glob([RESOURCES_ROOT + '**'])
121
122 if resources and not resources_root:
123 resources_root = RESOURCES_ROOT
124
Brian O'Connor42c38cf2016-04-05 17:05:57 -0700125 bare_jar_name = name + '-jar'
126 osgi_jar_name = name + '-osgi'
127 mvn_coords = group_id + ':' + name + ':' + version
128
Brian O'Connor0f6677d2016-04-06 23:26:51 -0700129
Brian O'Connor42c38cf2016-04-05 17:05:57 -0700130 java_library(
131 name = bare_jar_name,
132 srcs = srcs,
133 deps = deps,
Brian O'Connor0f6677d2016-04-06 23:26:51 -0700134 visibility = [], #intentially, not visible
135 resources = resources,
136 resources_root = resources_root,
Brian O'Connor42c38cf2016-04-05 17:05:57 -0700137 **kwargs
138 )
139
Brian O'Connor4847ea32016-04-29 16:33:06 -0700140 cp = ':'.join(['$(classpath %s)' % c for c in deps]) if deps else '""'
Brian O'Connor42c38cf2016-04-05 17:05:57 -0700141
142 args = ( '$(location :%s)' % bare_jar_name, #input jar
143 '$OUT', #output jar
144 cp, #classpath
145 name, #bundle name
146 group_id, #group id
147 version, #version
148 license, #license url
Brian O'Connore5817c92016-04-06 15:41:48 -0700149 "'%s'" % import_packages, #packages to import
150 "'%s'" % export_packages, #packages to export
151 include_resources, #custom includes to classpath
Brian O'Connor42c38cf2016-04-05 17:05:57 -0700152 web_context, #web context (REST API only)
153 description, #description
154 )
155
156 #TODO stage_jar is a horrendous hack
157 stage_jar = 'pushd $SRCDIR; mkdir bin; cd bin; jar xf $(location :%s); ls; popd; ' % bare_jar_name
158 wrap_jar = '$(exe //utils/osgiwrap:osgi-jar) ' + ' '.join(args)
159 bash = stage_jar + wrap_jar
160 if debug:
161 bash = stage_jar + DEBUG_ARG + ' ' + wrap_jar
162 print bash
Brian O'Connor42c38cf2016-04-05 17:05:57 -0700163
164 genrule(
165 name = osgi_jar_name,
166 bash = bash,
Brian O'Connor1f165982016-04-06 21:36:09 -0700167 out = '%s-%s.jar' % (name, version), #FIXME add version to jar file
Brian O'Connore5817c92016-04-06 15:41:48 -0700168 srcs = glob(['src/main/webapp/**']),
Brian O'Connor42c38cf2016-04-05 17:05:57 -0700169 visibility = [], #intentially, not visible
170 )
171
172 # TODO we really should shade the jar with maven flavor
173 prebuilt_jar(
174 name = name,
175 maven_coords = mvn_coords,
176 binary_jar = ':' + osgi_jar_name,
177 visibility = visibility,
178 )
179
Brian O'Connor42c38cf2016-04-05 17:05:57 -0700180 ### Checkstyle
Brian O'Connor40a3fbd2016-06-08 13:54:46 -0700181 checkstyle(
Brian O'Connorbe95f682016-05-18 15:40:19 -0700182 name = name + '-checkstyle-files',
Brian O'Connor4847ea32016-04-29 16:33:06 -0700183 srcs = srcs,
Brian O'Connor40a3fbd2016-06-08 13:54:46 -0700184 jar_target = ':'+ bare_jar_name,
185 )
Brian O'Connorb3cc6042016-04-25 11:55:51 -0700186
Ray Milkeyc340e282016-06-30 14:45:42 -0700187 java_doc(
188 name = name + '-javadoc',
189 title = 'Java Docs',
190 pkgs = [ 'org.onosproject' ],
191 paths = [ 'src/main/java' ],
192 srcs = srcs,
193 deps = deps,
194 visibility = visibility,
195 do_it_wrong = False,
196 )
197
Brian O'Connorb3cc6042016-04-25 11:55:51 -0700198 # TODO add project config for intellij
199 # project_config(
200 # src_target = ':' + bare_jar_name,
201 # src_roots = [ 'src/main/java' ],
202 # test_target = ':' + name + '-tests',
203 # test_roots = [ 'src/test/java' ],
204 # )
205
Brian O'Connor42c38cf2016-04-05 17:05:57 -0700206 ### .m2 Install
Brian O'Connorb86c9202016-04-05 20:15:04 -0700207 mvn_cmd = ' '.join(( 'mvn install:install-file',
208 '-Dfile=$(location :%s)' % name,
209 '-DgroupId=%s' % group_id,
210 '-DartifactId=%s' % name,
211 '-Dversion=%s' % version,
212 '-Dpackaging=jar' ))
Brian O'Connore5817c92016-04-06 15:41:48 -0700213 cmd = mvn_cmd + ' > $OUT'
214 if FORCE_INSTALL:
215 # Add a random number to the command to force this rule to run.
216 # TODO We should make this configurable from CLI, perhaps with a flag.
217 cmd = 'FOO=%s ' % random.random() + cmd
Brian O'Connor42c38cf2016-04-05 17:05:57 -0700218 genrule(
219 name = name + '-install',
Brian O'Connorb86c9202016-04-05 20:15:04 -0700220 bash = cmd,
Brian O'Connor42c38cf2016-04-05 17:05:57 -0700221 out = 'install.log',
222 visibility = visibility,
223 )
224
Brian O'Connore4da59d2016-04-08 00:32:18 -0700225def osgi_jar_with_tests(
226 name = None,
227 deps = [],
228 test_srcs = None,
229 test_deps = [ '//lib:TEST' ],
230 test_resources = None,
231 test_resources_root = None,
232 visibility = [ 'PUBLIC' ],
233 **kwargs
234 ):
235
236 if name is None:
237 name = _get_name()
238
239 osgi_jar(name = name,
240 deps = deps,
241 visibility = visibility,
242 **kwargs)
Brian O'Connor0f6677d2016-04-06 23:26:51 -0700243
244 if test_resources and not test_resources_root:
Brian O'Connore4da59d2016-04-08 00:32:18 -0700245 test_resources_root = TEST_RESOURCES_ROOT
246 if test_resources_root and not test_resources:
247 test_resources = glob([test_resources_root + '**'])
248 if not test_resources and not test_resources_root:
249 test_resources = glob([TEST_RESOURCES_ROOT + '**'])
250 if test_resources:
251 test_resources_root = TEST_RESOURCES_ROOT
Brian O'Connor0f6677d2016-04-06 23:26:51 -0700252
Brian O'Connore4da59d2016-04-08 00:32:18 -0700253 if test_srcs is None:
254 test_srcs = glob([TEST + '/*.java'])
255
Brian O'Connore4da59d2016-04-08 00:32:18 -0700256 java_test(
257 name = name + '-tests',
258 srcs = test_srcs,
259 deps = deps +
260 test_deps +
261 [':' + name + '-jar'],
262 source_under_test = [':' + name + '-jar'],
263 resources = test_resources,
264 resources_root = test_resources_root,
265 visibility = visibility
Brian O'Connorbe95f682016-05-18 15:40:19 -0700266 )
267
Brian O'Connor40a3fbd2016-06-08 13:54:46 -0700268 checkstyle(
269 name = name + '-tests',
270 srcs = test_srcs,
271 jar_target = ':' + name + '-tests',
272 )
273
Brian O'Connore642f7c2016-05-23 18:33:04 -0700274 #FIXME need to run checkstyle on test sources
Ray Milkeyc340e282016-06-30 14:45:42 -0700275
276
277