Brian O'Connor | b86c920 | 2016-04-05 20:15:04 -0700 | [diff] [blame] | 1 | import random |
Brian O'Connor | 42c38cf | 2016-04-05 17:05:57 -0700 | [diff] [blame] | 2 | |
| 3 | DEBUG_ARG='JAVA_TOOL_OPTIONS="-Xdebug -Xrunjdwp:server=y,transport=dt_socket,address=5005,suspend=y"' |
Brian O'Connor | e5817c9 | 2016-04-06 15:41:48 -0700 | [diff] [blame] | 4 | FORCE_INSTALL=True |
| 5 | NONE='NONE' |
Brian O'Connor | 42c38cf | 2016-04-05 17:05:57 -0700 | [diff] [blame] | 6 | |
Brian O'Connor | e4da59d | 2016-04-08 00:32:18 -0700 | [diff] [blame] | 7 | SRC = 'src/main/java/**/' |
| 8 | TEST = 'src/test/java/**/' |
Brian O'Connor | 0f6677d | 2016-04-06 23:26:51 -0700 | [diff] [blame] | 9 | RESOURCES_ROOT = 'src/main/resources/' |
Brian O'Connor | e4da59d | 2016-04-08 00:32:18 -0700 | [diff] [blame] | 10 | TEST_RESOURCES_ROOT = 'src/test/resources/' |
| 11 | |
Ray Milkey | 171b89a | 2016-07-28 15:22:26 -0700 | [diff] [blame] | 12 | include_defs('//onos.defs') |
Brian O'Connor | e4da59d | 2016-04-08 00:32:18 -0700 | [diff] [blame] | 13 | |
| 14 | def _get_name(): |
| 15 | base_path = get_base_path() |
| 16 | return ONOS_ARTIFACT_BASE + base_path.replace('/', '-') #TODO Unix-separator |
Brian O'Connor | 1f16598 | 2016-04-06 21:36:09 -0700 | [diff] [blame] | 17 | |
Brian O'Connor | 40a3fbd | 2016-06-08 13:54:46 -0700 | [diff] [blame] | 18 | def 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 Vachuska | 275d2e8 | 2016-07-14 17:41:34 -0700 | [diff] [blame] | 37 | test = '//tools/build/conf:start-buck-daemon', |
Ray Milkey | 8526700 | 2016-11-16 11:06:35 -0800 | [diff] [blame] | 38 | deps = [ jar_target, '//tools/build/conf:onos-java-header' ], |
Brian O'Connor | 40a3fbd | 2016-06-08 13:54:46 -0700 | [diff] [blame] | 39 | args = [ |
Thomas Vachuska | 275d2e8 | 2016-07-14 17:41:34 -0700 | [diff] [blame] | 40 | '$(location //tools/build/conf:buck-daemon-jar)', |
| 41 | 'checkstyle', |
Brian O'Connor | 40a3fbd | 2016-06-08 13:54:46 -0700 | [diff] [blame] | 42 | '$(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'Connor | 40a3fbd | 2016-06-08 13:54:46 -0700 | [diff] [blame] | 49 | |
Ray Milkey | c340e28 | 2016-06-30 14:45:42 -0700 | [diff] [blame] | 50 | def 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] |
Ray Milkey | 2a74983 | 2016-08-02 15:22:20 -0700 | [diff] [blame] | 64 | |
| 65 | if len(srcs) != 0: |
Ray Milkey | c340e28 | 2016-06-30 14:45:42 -0700 | [diff] [blame] | 66 | cmd = ' '.join([ |
Ray Milkey | 2a74983 | 2016-08-02 15:22:20 -0700 | [diff] [blame] | 67 | 'while ! test -f .buckconfig; do cd ..; done;', |
| 68 | 'javadoc', |
| 69 | '-tag onos.rsModel:a:"onos model"', |
| 70 | '-quiet', |
| 71 | '-protected', |
| 72 | '-encoding UTF-8', |
| 73 | '-charset UTF-8', |
| 74 | '-notimestamp', |
| 75 | '-windowtitle "' + title + '"', |
| 76 | '-link http://docs.oracle.com/javase/8/docs/api', |
| 77 | '-subpackages ', |
| 78 | ':'.join(pkgs), |
| 79 | '-sourcepath ', |
| 80 | ':'.join(sourcepath), |
| 81 | ' -classpath ', |
| 82 | ':'.join(['$(classpath %s)' % n for n in deps]), |
| 83 | '-d $TMP', |
| 84 | ]) + ';jar cf $OUT -C $TMP .' |
| 85 | |
| 86 | genrule( |
| 87 | name = name, |
| 88 | cmd = cmd, |
| 89 | srcs = srcs, |
| 90 | out = name + '.jar', |
| 91 | visibility = visibility, |
Ray Milkey | c340e28 | 2016-06-30 14:45:42 -0700 | [diff] [blame] | 92 | ) |
| 93 | |
Brian O'Connor | 9c2c823 | 2016-11-08 17:13:14 -0800 | [diff] [blame] | 94 | def sonar( |
| 95 | name, |
| 96 | test = False |
| 97 | ): |
| 98 | |
| 99 | cmd = '; '.join([ 'rm -f $OUT', |
| 100 | 'printf "%(src_base)s = " >> $OUT', |
| 101 | '%(srcs)s >> $OUT', |
| 102 | 'echo "%(binary_base)s = %(classes)s" >> $OUT', |
| 103 | 'printf "%(lib_base)s = " >> $OUT', |
| 104 | '%(libraries)s >> $OUT' |
| 105 | ]) % { |
| 106 | 'srcs' : "echo $(srcs :%s) | sed 's/ /,/g'" % name, |
| 107 | 'classes' : ("$(bin_dir :%s#non-osgi)" if not test else "$(bin_dir :%s)") % name, |
| 108 | 'libraries' : "echo $(classpath :%s) | sed 's/:/,/g'" % name, |
| 109 | 'src_base' : 'sonar.sources' if not test else 'sonar.tests', |
| 110 | 'binary_base' : 'sonar.java.binaries' if not test else 'sonar.java.test.binaries', |
| 111 | 'lib_base' : 'sonar.java.libraries' if not test else 'sonar.java.test.libraries' |
| 112 | } |
| 113 | # FIXME do we need to specify dep here or with the expander cover it? |
| 114 | genrule( |
| 115 | name = name + "-sonar", |
| 116 | cmd = cmd, |
| 117 | out = 'sonar-project.properties' |
| 118 | ) |
Ray Milkey | c340e28 | 2016-06-30 14:45:42 -0700 | [diff] [blame] | 119 | |
Brian O'Connor | 42c38cf | 2016-04-05 17:05:57 -0700 | [diff] [blame] | 120 | def osgi_jar( |
Brian O'Connor | e4da59d | 2016-04-08 00:32:18 -0700 | [diff] [blame] | 121 | name = None, |
| 122 | srcs = None, |
Brian O'Connor | 1f16598 | 2016-04-06 21:36:09 -0700 | [diff] [blame] | 123 | group_id = ONOS_GROUP_ID, |
| 124 | version = ONOS_VERSION, |
Brian O'Connor | 42c38cf | 2016-04-05 17:05:57 -0700 | [diff] [blame] | 125 | deps = [], |
| 126 | visibility = ['PUBLIC'], |
| 127 | license = 'NONE', |
| 128 | description = '', |
| 129 | debug = False, |
Brian O'Connor | e5817c9 | 2016-04-06 15:41:48 -0700 | [diff] [blame] | 130 | import_packages = '*', |
Yuta HIGUCHI | f05db40 | 2016-08-12 18:36:33 -0700 | [diff] [blame] | 131 | dynamicimport_packages = '', |
Yuta HIGUCHI | 29d640c | 2017-04-19 19:37:18 -0700 | [diff] [blame] | 132 | export_packages = '!.,!*.impl.*,!*.internal.*,*', |
Ray Milkey | 2a74983 | 2016-08-02 15:22:20 -0700 | [diff] [blame] | 133 | package_name_root = 'org.onosproject', |
Brian O'Connor | 79b7067 | 2016-10-20 13:44:52 -0700 | [diff] [blame] | 134 | include_resources = {}, |
Brian O'Connor | e5817c9 | 2016-04-06 15:41:48 -0700 | [diff] [blame] | 135 | web_context = NONE, |
Brian O'Connor | 79b7067 | 2016-10-20 13:44:52 -0700 | [diff] [blame] | 136 | api_title = None, |
Brian O'Connor | e8468b5 | 2016-07-25 13:42:36 -0700 | [diff] [blame] | 137 | api_version = NONE, |
| 138 | api_package = NONE, |
| 139 | api_description = NONE, |
Brian O'Connor | e4da59d | 2016-04-08 00:32:18 -0700 | [diff] [blame] | 140 | resources = NONE, |
Brian O'Connor | 0f6677d | 2016-04-06 23:26:51 -0700 | [diff] [blame] | 141 | resources_root = None, |
Brian O'Connor | ee67495 | 2016-09-13 16:31:45 -0700 | [diff] [blame] | 142 | tests = None, |
Thomas Vachuska | 73436b5 | 2017-03-22 19:50:47 -0700 | [diff] [blame] | 143 | do_javadocs = True, |
| 144 | do_checkstyle = True, |
Brian O'Connor | 42c38cf | 2016-04-05 17:05:57 -0700 | [diff] [blame] | 145 | **kwargs |
| 146 | ): |
| 147 | |
Brian O'Connor | e4da59d | 2016-04-08 00:32:18 -0700 | [diff] [blame] | 148 | # if name and _get_name() != name: |
| 149 | # print _get_name(), '!=', name |
| 150 | if name is None: |
| 151 | name = _get_name() |
| 152 | |
| 153 | if srcs is None: |
| 154 | srcs = glob([SRC + '/*.java']) |
| 155 | |
| 156 | if resources == NONE and resources_root is not None: |
| 157 | resources = glob([resources_root + '**']) |
| 158 | elif resources == NONE: |
| 159 | resources = glob([RESOURCES_ROOT + '**']) |
| 160 | |
| 161 | if resources and not resources_root: |
| 162 | resources_root = RESOURCES_ROOT |
| 163 | |
Brian O'Connor | 42c38cf | 2016-04-05 17:05:57 -0700 | [diff] [blame] | 164 | mvn_coords = group_id + ':' + name + ':' + version |
| 165 | |
Brian O'Connor | e8468b5 | 2016-07-25 13:42:36 -0700 | [diff] [blame] | 166 | onos_jar( |
Brian O'Connor | ee67495 | 2016-09-13 16:31:45 -0700 | [diff] [blame] | 167 | name = name, |
| 168 | srcs = srcs + glob(['src/main/webapp/**']), |
Brian O'Connor | 42c38cf | 2016-04-05 17:05:57 -0700 | [diff] [blame] | 169 | deps = deps, |
Brian O'Connor | ee67495 | 2016-09-13 16:31:45 -0700 | [diff] [blame] | 170 | visibility = visibility, |
Brian O'Connor | 0f6677d | 2016-04-06 23:26:51 -0700 | [diff] [blame] | 171 | resources = resources, |
| 172 | resources_root = resources_root, |
Brian O'Connor | ee67495 | 2016-09-13 16:31:45 -0700 | [diff] [blame] | 173 | bundle_name = name, |
| 174 | group_id = group_id, |
| 175 | bundle_version = version, |
| 176 | bundle_license = license, |
| 177 | bundle_description = description, |
| 178 | import_packages = import_packages, |
| 179 | export_packages = export_packages, |
| 180 | include_resources = include_resources, |
| 181 | dynamicimport_packages = dynamicimport_packages, |
Brian O'Connor | e8468b5 | 2016-07-25 13:42:36 -0700 | [diff] [blame] | 182 | web_context = web_context, |
| 183 | api_title = api_title, |
| 184 | api_version = api_version, |
| 185 | api_package = api_package, |
| 186 | api_description = api_description, |
Brian O'Connor | ee67495 | 2016-09-13 16:31:45 -0700 | [diff] [blame] | 187 | tests = tests, |
| 188 | maven_coords = mvn_coords, |
Brian O'Connor | 42c38cf | 2016-04-05 17:05:57 -0700 | [diff] [blame] | 189 | **kwargs |
| 190 | ) |
| 191 | |
Brian O'Connor | 42c38cf | 2016-04-05 17:05:57 -0700 | [diff] [blame] | 192 | ### Checkstyle |
Thomas Vachuska | 73436b5 | 2017-03-22 19:50:47 -0700 | [diff] [blame] | 193 | if do_checkstyle: |
| 194 | checkstyle( |
| 195 | name = name + '-checkstyle-files', |
| 196 | srcs = srcs, |
| 197 | jar_target = ':'+ name, |
| 198 | ) |
Brian O'Connor | b3cc604 | 2016-04-25 11:55:51 -0700 | [diff] [blame] | 199 | |
Thomas Vachuska | 73436b5 | 2017-03-22 19:50:47 -0700 | [diff] [blame] | 200 | if do_javadocs: |
| 201 | java_doc( |
| 202 | name = name + '-javadoc', |
| 203 | title = 'Java Docs', |
| 204 | pkgs = [ package_name_root ], |
| 205 | paths = [ 'src/main/java' ], |
| 206 | srcs = srcs, |
| 207 | deps = deps, |
| 208 | visibility = visibility, |
| 209 | do_it_wrong = False, |
| 210 | ) |
Ray Milkey | c340e28 | 2016-06-30 14:45:42 -0700 | [diff] [blame] | 211 | |
Brian O'Connor | b3cc604 | 2016-04-25 11:55:51 -0700 | [diff] [blame] | 212 | # TODO add project config for intellij |
| 213 | # project_config( |
Brian O'Connor | ee67495 | 2016-09-13 16:31:45 -0700 | [diff] [blame] | 214 | # src_target = ':' + name, |
Brian O'Connor | b3cc604 | 2016-04-25 11:55:51 -0700 | [diff] [blame] | 215 | # src_roots = [ 'src/main/java' ], |
| 216 | # test_target = ':' + name + '-tests', |
| 217 | # test_roots = [ 'src/test/java' ], |
| 218 | # ) |
| 219 | |
Brian O'Connor | 42c38cf | 2016-04-05 17:05:57 -0700 | [diff] [blame] | 220 | ### .m2 Install |
Brian O'Connor | b86c920 | 2016-04-05 20:15:04 -0700 | [diff] [blame] | 221 | mvn_cmd = ' '.join(( 'mvn install:install-file', |
| 222 | '-Dfile=$(location :%s)' % name, |
| 223 | '-DgroupId=%s' % group_id, |
| 224 | '-DartifactId=%s' % name, |
| 225 | '-Dversion=%s' % version, |
| 226 | '-Dpackaging=jar' )) |
Brian O'Connor | e5817c9 | 2016-04-06 15:41:48 -0700 | [diff] [blame] | 227 | cmd = mvn_cmd + ' > $OUT' |
| 228 | if FORCE_INSTALL: |
| 229 | # Add a random number to the command to force this rule to run. |
| 230 | # TODO We should make this configurable from CLI, perhaps with a flag. |
| 231 | cmd = 'FOO=%s ' % random.random() + cmd |
Brian O'Connor | 42c38cf | 2016-04-05 17:05:57 -0700 | [diff] [blame] | 232 | genrule( |
| 233 | name = name + '-install', |
Brian O'Connor | b86c920 | 2016-04-05 20:15:04 -0700 | [diff] [blame] | 234 | bash = cmd, |
Brian O'Connor | 42c38cf | 2016-04-05 17:05:57 -0700 | [diff] [blame] | 235 | out = 'install.log', |
| 236 | visibility = visibility, |
| 237 | ) |
Brian O'Connor | 9c2c823 | 2016-11-08 17:13:14 -0800 | [diff] [blame] | 238 | sonar( |
| 239 | name = name, |
| 240 | ) |
Brian O'Connor | 42c38cf | 2016-04-05 17:05:57 -0700 | [diff] [blame] | 241 | |
Brian O'Connor | e4da59d | 2016-04-08 00:32:18 -0700 | [diff] [blame] | 242 | def osgi_jar_with_tests( |
| 243 | name = None, |
| 244 | deps = [], |
Brian O'Connor | ee67495 | 2016-09-13 16:31:45 -0700 | [diff] [blame] | 245 | group_id = ONOS_GROUP_ID, |
| 246 | version = ONOS_VERSION, |
Brian O'Connor | e4da59d | 2016-04-08 00:32:18 -0700 | [diff] [blame] | 247 | test_srcs = None, |
| 248 | test_deps = [ '//lib:TEST' ], |
| 249 | test_resources = None, |
| 250 | test_resources_root = None, |
| 251 | visibility = [ 'PUBLIC' ], |
| 252 | **kwargs |
| 253 | ): |
| 254 | |
| 255 | if name is None: |
| 256 | name = _get_name() |
| 257 | |
Brian O'Connor | 0f6677d | 2016-04-06 23:26:51 -0700 | [diff] [blame] | 258 | if test_resources and not test_resources_root: |
Brian O'Connor | e4da59d | 2016-04-08 00:32:18 -0700 | [diff] [blame] | 259 | test_resources_root = TEST_RESOURCES_ROOT |
| 260 | if test_resources_root and not test_resources: |
| 261 | test_resources = glob([test_resources_root + '**']) |
| 262 | if not test_resources and not test_resources_root: |
| 263 | test_resources = glob([TEST_RESOURCES_ROOT + '**']) |
| 264 | if test_resources: |
| 265 | test_resources_root = TEST_RESOURCES_ROOT |
Brian O'Connor | 0f6677d | 2016-04-06 23:26:51 -0700 | [diff] [blame] | 266 | |
Brian O'Connor | e4da59d | 2016-04-08 00:32:18 -0700 | [diff] [blame] | 267 | if test_srcs is None: |
| 268 | test_srcs = glob([TEST + '/*.java']) |
| 269 | |
Brian O'Connor | ee67495 | 2016-09-13 16:31:45 -0700 | [diff] [blame] | 270 | mvn_coords = group_id + ':' + name + ':jar:tests:' + version |
| 271 | |
Brian O'Connor | 9c2c823 | 2016-11-08 17:13:14 -0800 | [diff] [blame] | 272 | if test_srcs: |
| 273 | java_test( |
| 274 | name = name + '-tests', |
| 275 | srcs = test_srcs, |
| 276 | deps = deps + |
| 277 | test_deps + |
| 278 | [':' + name + '#non-osgi'], |
| 279 | resources = test_resources, |
| 280 | resources_root = test_resources_root, |
| 281 | maven_coords = mvn_coords, |
| 282 | visibility = visibility, |
| 283 | ) |
Brian O'Connor | be95f68 | 2016-05-18 15:40:19 -0700 | [diff] [blame] | 284 | |
Brian O'Connor | 9c2c823 | 2016-11-08 17:13:14 -0800 | [diff] [blame] | 285 | checkstyle( |
| 286 | name = name + '-tests', |
| 287 | srcs = test_srcs, |
| 288 | jar_target = ':' + name + '-tests', |
| 289 | ) |
| 290 | |
| 291 | sonar( |
| 292 | name = name + '-tests', |
| 293 | test = True |
| 294 | ) |
| 295 | |
| 296 | osgi_jar(name = name, |
| 297 | deps = deps, |
| 298 | group_id = group_id, |
| 299 | version = version, |
| 300 | visibility = visibility, |
| 301 | tests = [':' + name + '-tests'], |
| 302 | **kwargs) |
| 303 | else: |
| 304 | osgi_jar(name = name, |
| 305 | deps = deps, |
| 306 | group_id = group_id, |
| 307 | version = version, |
| 308 | visibility = visibility, |
| 309 | **kwargs) |
Thomas Vachuska | be1a196 | 2016-10-25 16:59:29 -0700 | [diff] [blame] | 310 | |
| 311 | def tar_file( |
| 312 | name, |
| 313 | srcs, |
| 314 | root = None, |
| 315 | out = None, |
| 316 | visibility = [ 'PUBLIC' ], |
| 317 | ): |
| 318 | |
| 319 | cmd = ( 'mkdir -p $TMP/%(root_dir)s && ' |
Ray Milkey | 442c8f2 | 2017-05-02 11:23:55 -0700 | [diff] [blame] | 320 | 'cp -R -L $SRCDIR/* $TMP/%(root_dir)s && ' |
Thomas Vachuska | be1a196 | 2016-10-25 16:59:29 -0700 | [diff] [blame] | 321 | 'tar -C $TMP -zcf $OUT %(root_dir)s' ) % { |
| 322 | 'root_dir': root if root is not None else name |
| 323 | } |
| 324 | |
| 325 | genrule( |
| 326 | name = name, |
| 327 | srcs = srcs, |
| 328 | bash = cmd, |
| 329 | out = out if out is not None else ( root if root is not None else name ) + '.tar.gz', |
| 330 | visibility = visibility, |
| 331 | ) |
Ray Milkey | 5c5454b | 2017-01-25 13:26:30 -0800 | [diff] [blame] | 332 | |
| 333 | def only_lib_dep_pom( |
| 334 | name, |
| 335 | src, |
| 336 | out, |
| 337 | version = ONOS_VERSION, |
| 338 | onosGroupId = ONOS_GROUP_ID, |
| 339 | visibility = [ 'PUBLIC' ], |
| 340 | ): |
| 341 | |
Ray Milkey | e46d31a | 2017-02-01 12:16:18 -0800 | [diff] [blame] | 342 | cmd = 'grep -v \<module\> ' + src + ' | sed "s#<modules>#<modules><module>lib</module>#" >$OUT' |
Ray Milkey | 5c5454b | 2017-01-25 13:26:30 -0800 | [diff] [blame] | 343 | |
| 344 | genrule( |
| 345 | name = name, |
| 346 | srcs = [ src ], |
| 347 | bash = cmd, |
| 348 | out = out, |
| 349 | visibility = visibility, |
| 350 | maven_coords = onosGroupId + ':onos:pom:' + version, |
| 351 | ) |
| 352 | |
| 353 | def pass_thru_pom( |
| 354 | name, |
| 355 | src, |
| 356 | out, |
| 357 | artifactId, |
| 358 | version = ONOS_VERSION, |
| 359 | onosGroupId = ONOS_GROUP_ID, |
| 360 | visibility = [ 'PUBLIC' ], |
| 361 | ): |
| 362 | |
| 363 | cmd = 'cp ' + src + ' $OUT' |
| 364 | |
| 365 | genrule( |
| 366 | name = name, |
| 367 | srcs = [ src ], |
| 368 | bash = cmd, |
| 369 | out = out, |
| 370 | visibility = visibility, |
| 371 | maven_coords = onosGroupId + ':' + artifactId + ':pom:' + version, |
| 372 | ) |