blob: 9c1ae9f77b0d1690d2df0df1afcf5c093cea349b [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'
14ONOS_VERSION = '1.6.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'Connor42c38cf2016-04-05 17:05:57 -070021def osgi_jar(
Brian O'Connore4da59d2016-04-08 00:32:18 -070022 name = None,
23 srcs = None,
Brian O'Connor1f165982016-04-06 21:36:09 -070024 group_id = ONOS_GROUP_ID,
25 version = ONOS_VERSION,
Brian O'Connor42c38cf2016-04-05 17:05:57 -070026 deps = [],
27 visibility = ['PUBLIC'],
28 license = 'NONE',
29 description = '',
30 debug = False,
Brian O'Connore5817c92016-04-06 15:41:48 -070031 import_packages = '*',
32 export_packages = '*',
33 include_resources = NONE,
34 web_context = NONE,
Brian O'Connore4da59d2016-04-08 00:32:18 -070035 resources = NONE,
Brian O'Connor0f6677d2016-04-06 23:26:51 -070036 resources_root = None,
Brian O'Connor42c38cf2016-04-05 17:05:57 -070037 **kwargs
38 ):
39
Brian O'Connore4da59d2016-04-08 00:32:18 -070040 # if name and _get_name() != name:
41 # print _get_name(), '!=', name
42 if name is None:
43 name = _get_name()
44
45 if srcs is None:
46 srcs = glob([SRC + '/*.java'])
47
48 if resources == NONE and resources_root is not None:
49 resources = glob([resources_root + '**'])
50 elif resources == NONE:
51 resources = glob([RESOURCES_ROOT + '**'])
52
53 if resources and not resources_root:
54 resources_root = RESOURCES_ROOT
55
Brian O'Connor42c38cf2016-04-05 17:05:57 -070056 bare_jar_name = name + '-jar'
57 osgi_jar_name = name + '-osgi'
58 mvn_coords = group_id + ':' + name + ':' + version
59
Brian O'Connor0f6677d2016-04-06 23:26:51 -070060
Brian O'Connor42c38cf2016-04-05 17:05:57 -070061 java_library(
62 name = bare_jar_name,
63 srcs = srcs,
64 deps = deps,
Brian O'Connor0f6677d2016-04-06 23:26:51 -070065 visibility = [], #intentially, not visible
66 resources = resources,
67 resources_root = resources_root,
Brian O'Connor42c38cf2016-04-05 17:05:57 -070068 **kwargs
69 )
70
Brian O'Connor4847ea32016-04-29 16:33:06 -070071 cp = ':'.join(['$(classpath %s)' % c for c in deps]) if deps else '""'
Brian O'Connor42c38cf2016-04-05 17:05:57 -070072
73 args = ( '$(location :%s)' % bare_jar_name, #input jar
74 '$OUT', #output jar
75 cp, #classpath
76 name, #bundle name
77 group_id, #group id
78 version, #version
79 license, #license url
Brian O'Connore5817c92016-04-06 15:41:48 -070080 "'%s'" % import_packages, #packages to import
81 "'%s'" % export_packages, #packages to export
82 include_resources, #custom includes to classpath
Brian O'Connor42c38cf2016-04-05 17:05:57 -070083 web_context, #web context (REST API only)
84 description, #description
85 )
86
87 #TODO stage_jar is a horrendous hack
88 stage_jar = 'pushd $SRCDIR; mkdir bin; cd bin; jar xf $(location :%s); ls; popd; ' % bare_jar_name
89 wrap_jar = '$(exe //utils/osgiwrap:osgi-jar) ' + ' '.join(args)
90 bash = stage_jar + wrap_jar
91 if debug:
92 bash = stage_jar + DEBUG_ARG + ' ' + wrap_jar
93 print bash
Brian O'Connor42c38cf2016-04-05 17:05:57 -070094
95 genrule(
96 name = osgi_jar_name,
97 bash = bash,
Brian O'Connor1f165982016-04-06 21:36:09 -070098 out = '%s-%s.jar' % (name, version), #FIXME add version to jar file
Brian O'Connore5817c92016-04-06 15:41:48 -070099 srcs = glob(['src/main/webapp/**']),
Brian O'Connor42c38cf2016-04-05 17:05:57 -0700100 visibility = [], #intentially, not visible
101 )
102
103 # TODO we really should shade the jar with maven flavor
104 prebuilt_jar(
105 name = name,
106 maven_coords = mvn_coords,
107 binary_jar = ':' + osgi_jar_name,
108 visibility = visibility,
109 )
110
Brian O'Connor42c38cf2016-04-05 17:05:57 -0700111 ### Checkstyle
Ray Milkeydb016212016-04-27 15:17:43 -0700112 if srcs:
Brian O'Connor67f1c292016-05-04 20:16:11 -0700113 chk_cmd = '#!/bin/bash\n'
Brian O'Connor4847ea32016-04-29 16:33:06 -0700114 base = get_base_path()
Brian O'Connoraf9e01f2016-05-17 15:26:09 -0700115 chk_cmd += 'OUT=$3\n'
Brian O'Connor63555d82016-05-10 14:17:07 -0700116 chk_cmd += ' '.join(( 'java -jar $1',
Brian O'Connoraf9e01f2016-05-17 15:26:09 -0700117 '-c $2',
118 ' '.join(['%s/%s' % (base, s) for s in srcs]) ))
119 chk_cmd += ' | tee $OUT'
120 chk_cmd += ' | grep -E "^.*[0-9]+:[0-9]+: error:"'
Brian O'Connor4847ea32016-04-29 16:33:06 -0700121 chk_cmd += ' | sed "s#^.*%s/#%s:#g"\n' % (base, name)
Brian O'Connoraf9e01f2016-05-17 15:26:09 -0700122 chk_cmd += 'RESULT=(${PIPESTATUS[*]})\n' # RESULT[0] is checkstyle result, RESULT[2] is grep
123 chk_cmd += 'test ${RESULT[2]} -eq 0 || cat $OUT\n'
124 chk_cmd += 'rm $OUT\n'
125 chk_cmd += 'exit ${RESULT[0]}'
Brian O'Connor42c38cf2016-04-05 17:05:57 -0700126
Brian O'Connor4847ea32016-04-29 16:33:06 -0700127 genrule(
128 name = name + '-checkstyle-sh',
129 bash = "echo '%s' > $OUT && chmod +x $OUT" % chk_cmd,
130 srcs = srcs,
131 out = 'checkstyle.sh',
132 )
133
134 sh_test(
135 name = name + '-checkstyle',
136 test = ':' + name + '-checkstyle-sh',
Brian O'Connor63555d82016-05-10 14:17:07 -0700137 args = [
138 '$(location //lib:checkstyle)',
Brian O'Connoraf9e01f2016-05-17 15:26:09 -0700139 '$(location //tools/build/conf:checkstyle-xml)',
140 '`mktemp /tmp/%s-checkstyle-XXXXXX`' % name,
Brian O'Connor63555d82016-05-10 14:17:07 -0700141 ],
142 deps = [
143 ':'+ bare_jar_name,
Brian O'Connor3724a032016-05-04 20:42:59 -0700144 '//tools/build/conf:suppressions-xml',
145 ],
Brian O'Connor4847ea32016-04-29 16:33:06 -0700146 labels = [ 'checkstyle' ],
147 )
Ray Milkeydb016212016-04-27 15:17:43 -0700148 else:
Brian O'Connor4847ea32016-04-29 16:33:06 -0700149 print 'Not generating checkstyle rule for %s because there are no sources.' % name
Brian O'Connorb3cc6042016-04-25 11:55:51 -0700150
151 # TODO add project config for intellij
152 # project_config(
153 # src_target = ':' + bare_jar_name,
154 # src_roots = [ 'src/main/java' ],
155 # test_target = ':' + name + '-tests',
156 # test_roots = [ 'src/test/java' ],
157 # )
158
Brian O'Connor42c38cf2016-04-05 17:05:57 -0700159 ### .m2 Install
Brian O'Connorb86c9202016-04-05 20:15:04 -0700160 mvn_cmd = ' '.join(( 'mvn install:install-file',
161 '-Dfile=$(location :%s)' % name,
162 '-DgroupId=%s' % group_id,
163 '-DartifactId=%s' % name,
164 '-Dversion=%s' % version,
165 '-Dpackaging=jar' ))
Brian O'Connore5817c92016-04-06 15:41:48 -0700166 cmd = mvn_cmd + ' > $OUT'
167 if FORCE_INSTALL:
168 # Add a random number to the command to force this rule to run.
169 # TODO We should make this configurable from CLI, perhaps with a flag.
170 cmd = 'FOO=%s ' % random.random() + cmd
Brian O'Connor42c38cf2016-04-05 17:05:57 -0700171 genrule(
172 name = name + '-install',
Brian O'Connorb86c9202016-04-05 20:15:04 -0700173 bash = cmd,
Brian O'Connor42c38cf2016-04-05 17:05:57 -0700174 out = 'install.log',
175 visibility = visibility,
176 )
177
Brian O'Connore4da59d2016-04-08 00:32:18 -0700178def osgi_jar_with_tests(
179 name = None,
180 deps = [],
181 test_srcs = None,
182 test_deps = [ '//lib:TEST' ],
183 test_resources = None,
184 test_resources_root = None,
185 visibility = [ 'PUBLIC' ],
186 **kwargs
187 ):
188
189 if name is None:
190 name = _get_name()
191
192 osgi_jar(name = name,
193 deps = deps,
194 visibility = visibility,
195 **kwargs)
Brian O'Connor0f6677d2016-04-06 23:26:51 -0700196
197 if test_resources and not test_resources_root:
Brian O'Connore4da59d2016-04-08 00:32:18 -0700198 test_resources_root = TEST_RESOURCES_ROOT
199 if test_resources_root and not test_resources:
200 test_resources = glob([test_resources_root + '**'])
201 if not test_resources and not test_resources_root:
202 test_resources = glob([TEST_RESOURCES_ROOT + '**'])
203 if test_resources:
204 test_resources_root = TEST_RESOURCES_ROOT
Brian O'Connor0f6677d2016-04-06 23:26:51 -0700205
Brian O'Connore4da59d2016-04-08 00:32:18 -0700206 if test_srcs is None:
207 test_srcs = glob([TEST + '/*.java'])
208
209 if not test_srcs:
210 print "Generating test rule for %s, but there are no tests." % name
211
212 java_test(
213 name = name + '-tests',
214 srcs = test_srcs,
215 deps = deps +
216 test_deps +
217 [':' + name + '-jar'],
218 source_under_test = [':' + name + '-jar'],
219 resources = test_resources,
220 resources_root = test_resources_root,
221 visibility = visibility
222 )