blob: 51db74fca8918745c2ed29ebb75a08cccd954d2b [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
Brian O'Connor42c38cf2016-04-05 17:05:57 -070055def osgi_jar(
Brian O'Connore4da59d2016-04-08 00:32:18 -070056 name = None,
57 srcs = None,
Brian O'Connor1f165982016-04-06 21:36:09 -070058 group_id = ONOS_GROUP_ID,
59 version = ONOS_VERSION,
Brian O'Connor42c38cf2016-04-05 17:05:57 -070060 deps = [],
61 visibility = ['PUBLIC'],
62 license = 'NONE',
63 description = '',
64 debug = False,
Brian O'Connore5817c92016-04-06 15:41:48 -070065 import_packages = '*',
66 export_packages = '*',
67 include_resources = NONE,
68 web_context = NONE,
Brian O'Connore4da59d2016-04-08 00:32:18 -070069 resources = NONE,
Brian O'Connor0f6677d2016-04-06 23:26:51 -070070 resources_root = None,
Brian O'Connor42c38cf2016-04-05 17:05:57 -070071 **kwargs
72 ):
73
Brian O'Connore4da59d2016-04-08 00:32:18 -070074 # if name and _get_name() != name:
75 # print _get_name(), '!=', name
76 if name is None:
77 name = _get_name()
78
79 if srcs is None:
80 srcs = glob([SRC + '/*.java'])
81
82 if resources == NONE and resources_root is not None:
83 resources = glob([resources_root + '**'])
84 elif resources == NONE:
85 resources = glob([RESOURCES_ROOT + '**'])
86
87 if resources and not resources_root:
88 resources_root = RESOURCES_ROOT
89
Brian O'Connor42c38cf2016-04-05 17:05:57 -070090 bare_jar_name = name + '-jar'
91 osgi_jar_name = name + '-osgi'
92 mvn_coords = group_id + ':' + name + ':' + version
93
Brian O'Connor0f6677d2016-04-06 23:26:51 -070094
Brian O'Connor42c38cf2016-04-05 17:05:57 -070095 java_library(
96 name = bare_jar_name,
97 srcs = srcs,
98 deps = deps,
Brian O'Connor0f6677d2016-04-06 23:26:51 -070099 visibility = [], #intentially, not visible
100 resources = resources,
101 resources_root = resources_root,
Brian O'Connor42c38cf2016-04-05 17:05:57 -0700102 **kwargs
103 )
104
Brian O'Connor4847ea32016-04-29 16:33:06 -0700105 cp = ':'.join(['$(classpath %s)' % c for c in deps]) if deps else '""'
Brian O'Connor42c38cf2016-04-05 17:05:57 -0700106
107 args = ( '$(location :%s)' % bare_jar_name, #input jar
108 '$OUT', #output jar
109 cp, #classpath
110 name, #bundle name
111 group_id, #group id
112 version, #version
113 license, #license url
Brian O'Connore5817c92016-04-06 15:41:48 -0700114 "'%s'" % import_packages, #packages to import
115 "'%s'" % export_packages, #packages to export
116 include_resources, #custom includes to classpath
Brian O'Connor42c38cf2016-04-05 17:05:57 -0700117 web_context, #web context (REST API only)
118 description, #description
119 )
120
121 #TODO stage_jar is a horrendous hack
122 stage_jar = 'pushd $SRCDIR; mkdir bin; cd bin; jar xf $(location :%s); ls; popd; ' % bare_jar_name
123 wrap_jar = '$(exe //utils/osgiwrap:osgi-jar) ' + ' '.join(args)
124 bash = stage_jar + wrap_jar
125 if debug:
126 bash = stage_jar + DEBUG_ARG + ' ' + wrap_jar
127 print bash
Brian O'Connor42c38cf2016-04-05 17:05:57 -0700128
129 genrule(
130 name = osgi_jar_name,
131 bash = bash,
Brian O'Connor1f165982016-04-06 21:36:09 -0700132 out = '%s-%s.jar' % (name, version), #FIXME add version to jar file
Brian O'Connore5817c92016-04-06 15:41:48 -0700133 srcs = glob(['src/main/webapp/**']),
Brian O'Connor42c38cf2016-04-05 17:05:57 -0700134 visibility = [], #intentially, not visible
135 )
136
137 # TODO we really should shade the jar with maven flavor
138 prebuilt_jar(
139 name = name,
140 maven_coords = mvn_coords,
141 binary_jar = ':' + osgi_jar_name,
142 visibility = visibility,
143 )
144
Brian O'Connor42c38cf2016-04-05 17:05:57 -0700145 ### Checkstyle
Brian O'Connor40a3fbd2016-06-08 13:54:46 -0700146 checkstyle(
Brian O'Connorbe95f682016-05-18 15:40:19 -0700147 name = name + '-checkstyle-files',
Brian O'Connor4847ea32016-04-29 16:33:06 -0700148 srcs = srcs,
Brian O'Connor40a3fbd2016-06-08 13:54:46 -0700149 jar_target = ':'+ bare_jar_name,
150 )
Brian O'Connorb3cc6042016-04-25 11:55:51 -0700151
152 # TODO add project config for intellij
153 # project_config(
154 # src_target = ':' + bare_jar_name,
155 # src_roots = [ 'src/main/java' ],
156 # test_target = ':' + name + '-tests',
157 # test_roots = [ 'src/test/java' ],
158 # )
159
Brian O'Connor42c38cf2016-04-05 17:05:57 -0700160 ### .m2 Install
Brian O'Connorb86c9202016-04-05 20:15:04 -0700161 mvn_cmd = ' '.join(( 'mvn install:install-file',
162 '-Dfile=$(location :%s)' % name,
163 '-DgroupId=%s' % group_id,
164 '-DartifactId=%s' % name,
165 '-Dversion=%s' % version,
166 '-Dpackaging=jar' ))
Brian O'Connore5817c92016-04-06 15:41:48 -0700167 cmd = mvn_cmd + ' > $OUT'
168 if FORCE_INSTALL:
169 # Add a random number to the command to force this rule to run.
170 # TODO We should make this configurable from CLI, perhaps with a flag.
171 cmd = 'FOO=%s ' % random.random() + cmd
Brian O'Connor42c38cf2016-04-05 17:05:57 -0700172 genrule(
173 name = name + '-install',
Brian O'Connorb86c9202016-04-05 20:15:04 -0700174 bash = cmd,
Brian O'Connor42c38cf2016-04-05 17:05:57 -0700175 out = 'install.log',
176 visibility = visibility,
177 )
178
Brian O'Connore4da59d2016-04-08 00:32:18 -0700179def osgi_jar_with_tests(
180 name = None,
181 deps = [],
182 test_srcs = None,
183 test_deps = [ '//lib:TEST' ],
184 test_resources = None,
185 test_resources_root = None,
186 visibility = [ 'PUBLIC' ],
187 **kwargs
188 ):
189
190 if name is None:
191 name = _get_name()
192
193 osgi_jar(name = name,
194 deps = deps,
195 visibility = visibility,
196 **kwargs)
Brian O'Connor0f6677d2016-04-06 23:26:51 -0700197
198 if test_resources and not test_resources_root:
Brian O'Connore4da59d2016-04-08 00:32:18 -0700199 test_resources_root = TEST_RESOURCES_ROOT
200 if test_resources_root and not test_resources:
201 test_resources = glob([test_resources_root + '**'])
202 if not test_resources and not test_resources_root:
203 test_resources = glob([TEST_RESOURCES_ROOT + '**'])
204 if test_resources:
205 test_resources_root = TEST_RESOURCES_ROOT
Brian O'Connor0f6677d2016-04-06 23:26:51 -0700206
Brian O'Connore4da59d2016-04-08 00:32:18 -0700207 if test_srcs is None:
208 test_srcs = glob([TEST + '/*.java'])
209
210 if not test_srcs:
211 print "Generating test rule for %s, but there are no tests." % name
212
213 java_test(
214 name = name + '-tests',
215 srcs = test_srcs,
216 deps = deps +
217 test_deps +
218 [':' + name + '-jar'],
219 source_under_test = [':' + name + '-jar'],
220 resources = test_resources,
221 resources_root = test_resources_root,
222 visibility = visibility
Brian O'Connorbe95f682016-05-18 15:40:19 -0700223 )
224
Brian O'Connor40a3fbd2016-06-08 13:54:46 -0700225 checkstyle(
226 name = name + '-tests',
227 srcs = test_srcs,
228 jar_target = ':' + name + '-tests',
229 )
230
Brian O'Connore642f7c2016-05-23 18:33:04 -0700231 #FIXME need to run checkstyle on test sources