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 = '', |
Brian O'Connor | e5817c9 | 2016-04-06 15:41:48 -0700 | [diff] [blame] | 132 | export_packages = '*', |
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, |
Brian O'Connor | 42c38cf | 2016-04-05 17:05:57 -0700 | [diff] [blame] | 143 | **kwargs |
| 144 | ): |
| 145 | |
Brian O'Connor | e4da59d | 2016-04-08 00:32:18 -0700 | [diff] [blame] | 146 | # if name and _get_name() != name: |
| 147 | # print _get_name(), '!=', name |
| 148 | if name is None: |
| 149 | name = _get_name() |
| 150 | |
| 151 | if srcs is None: |
| 152 | srcs = glob([SRC + '/*.java']) |
| 153 | |
| 154 | if resources == NONE and resources_root is not None: |
| 155 | resources = glob([resources_root + '**']) |
| 156 | elif resources == NONE: |
| 157 | resources = glob([RESOURCES_ROOT + '**']) |
| 158 | |
| 159 | if resources and not resources_root: |
| 160 | resources_root = RESOURCES_ROOT |
| 161 | |
Brian O'Connor | 42c38cf | 2016-04-05 17:05:57 -0700 | [diff] [blame] | 162 | mvn_coords = group_id + ':' + name + ':' + version |
| 163 | |
Brian O'Connor | e8468b5 | 2016-07-25 13:42:36 -0700 | [diff] [blame] | 164 | onos_jar( |
Brian O'Connor | ee67495 | 2016-09-13 16:31:45 -0700 | [diff] [blame] | 165 | name = name, |
| 166 | srcs = srcs + glob(['src/main/webapp/**']), |
Brian O'Connor | 42c38cf | 2016-04-05 17:05:57 -0700 | [diff] [blame] | 167 | deps = deps, |
Brian O'Connor | ee67495 | 2016-09-13 16:31:45 -0700 | [diff] [blame] | 168 | visibility = visibility, |
Brian O'Connor | 0f6677d | 2016-04-06 23:26:51 -0700 | [diff] [blame] | 169 | resources = resources, |
| 170 | resources_root = resources_root, |
Brian O'Connor | ee67495 | 2016-09-13 16:31:45 -0700 | [diff] [blame] | 171 | bundle_name = name, |
| 172 | group_id = group_id, |
| 173 | bundle_version = version, |
| 174 | bundle_license = license, |
| 175 | bundle_description = description, |
| 176 | import_packages = import_packages, |
| 177 | export_packages = export_packages, |
| 178 | include_resources = include_resources, |
| 179 | dynamicimport_packages = dynamicimport_packages, |
Brian O'Connor | e8468b5 | 2016-07-25 13:42:36 -0700 | [diff] [blame] | 180 | web_context = web_context, |
| 181 | api_title = api_title, |
| 182 | api_version = api_version, |
| 183 | api_package = api_package, |
| 184 | api_description = api_description, |
Brian O'Connor | ee67495 | 2016-09-13 16:31:45 -0700 | [diff] [blame] | 185 | tests = tests, |
| 186 | maven_coords = mvn_coords, |
Brian O'Connor | 42c38cf | 2016-04-05 17:05:57 -0700 | [diff] [blame] | 187 | **kwargs |
| 188 | ) |
| 189 | |
Brian O'Connor | 42c38cf | 2016-04-05 17:05:57 -0700 | [diff] [blame] | 190 | ### Checkstyle |
Brian O'Connor | 40a3fbd | 2016-06-08 13:54:46 -0700 | [diff] [blame] | 191 | checkstyle( |
Brian O'Connor | be95f68 | 2016-05-18 15:40:19 -0700 | [diff] [blame] | 192 | name = name + '-checkstyle-files', |
Brian O'Connor | 4847ea3 | 2016-04-29 16:33:06 -0700 | [diff] [blame] | 193 | srcs = srcs, |
Brian O'Connor | ee67495 | 2016-09-13 16:31:45 -0700 | [diff] [blame] | 194 | jar_target = ':'+ name, |
Brian O'Connor | 40a3fbd | 2016-06-08 13:54:46 -0700 | [diff] [blame] | 195 | ) |
Brian O'Connor | b3cc604 | 2016-04-25 11:55:51 -0700 | [diff] [blame] | 196 | |
Ray Milkey | c340e28 | 2016-06-30 14:45:42 -0700 | [diff] [blame] | 197 | java_doc( |
| 198 | name = name + '-javadoc', |
| 199 | title = 'Java Docs', |
Ray Milkey | 2a74983 | 2016-08-02 15:22:20 -0700 | [diff] [blame] | 200 | pkgs = [ package_name_root ], |
Ray Milkey | c340e28 | 2016-06-30 14:45:42 -0700 | [diff] [blame] | 201 | paths = [ 'src/main/java' ], |
| 202 | srcs = srcs, |
| 203 | deps = deps, |
| 204 | visibility = visibility, |
| 205 | do_it_wrong = False, |
| 206 | ) |
| 207 | |
Brian O'Connor | b3cc604 | 2016-04-25 11:55:51 -0700 | [diff] [blame] | 208 | # TODO add project config for intellij |
| 209 | # project_config( |
Brian O'Connor | ee67495 | 2016-09-13 16:31:45 -0700 | [diff] [blame] | 210 | # src_target = ':' + name, |
Brian O'Connor | b3cc604 | 2016-04-25 11:55:51 -0700 | [diff] [blame] | 211 | # src_roots = [ 'src/main/java' ], |
| 212 | # test_target = ':' + name + '-tests', |
| 213 | # test_roots = [ 'src/test/java' ], |
| 214 | # ) |
| 215 | |
Brian O'Connor | 42c38cf | 2016-04-05 17:05:57 -0700 | [diff] [blame] | 216 | ### .m2 Install |
Brian O'Connor | b86c920 | 2016-04-05 20:15:04 -0700 | [diff] [blame] | 217 | mvn_cmd = ' '.join(( 'mvn install:install-file', |
| 218 | '-Dfile=$(location :%s)' % name, |
| 219 | '-DgroupId=%s' % group_id, |
| 220 | '-DartifactId=%s' % name, |
| 221 | '-Dversion=%s' % version, |
| 222 | '-Dpackaging=jar' )) |
Brian O'Connor | e5817c9 | 2016-04-06 15:41:48 -0700 | [diff] [blame] | 223 | cmd = mvn_cmd + ' > $OUT' |
| 224 | if FORCE_INSTALL: |
| 225 | # Add a random number to the command to force this rule to run. |
| 226 | # TODO We should make this configurable from CLI, perhaps with a flag. |
| 227 | cmd = 'FOO=%s ' % random.random() + cmd |
Brian O'Connor | 42c38cf | 2016-04-05 17:05:57 -0700 | [diff] [blame] | 228 | genrule( |
| 229 | name = name + '-install', |
Brian O'Connor | b86c920 | 2016-04-05 20:15:04 -0700 | [diff] [blame] | 230 | bash = cmd, |
Brian O'Connor | 42c38cf | 2016-04-05 17:05:57 -0700 | [diff] [blame] | 231 | out = 'install.log', |
| 232 | visibility = visibility, |
| 233 | ) |
Brian O'Connor | 9c2c823 | 2016-11-08 17:13:14 -0800 | [diff] [blame] | 234 | sonar( |
| 235 | name = name, |
| 236 | ) |
Brian O'Connor | 42c38cf | 2016-04-05 17:05:57 -0700 | [diff] [blame] | 237 | |
Brian O'Connor | e4da59d | 2016-04-08 00:32:18 -0700 | [diff] [blame] | 238 | def osgi_jar_with_tests( |
| 239 | name = None, |
| 240 | deps = [], |
Brian O'Connor | ee67495 | 2016-09-13 16:31:45 -0700 | [diff] [blame] | 241 | group_id = ONOS_GROUP_ID, |
| 242 | version = ONOS_VERSION, |
Brian O'Connor | e4da59d | 2016-04-08 00:32:18 -0700 | [diff] [blame] | 243 | test_srcs = None, |
| 244 | test_deps = [ '//lib:TEST' ], |
| 245 | test_resources = None, |
| 246 | test_resources_root = None, |
| 247 | visibility = [ 'PUBLIC' ], |
| 248 | **kwargs |
| 249 | ): |
| 250 | |
| 251 | if name is None: |
| 252 | name = _get_name() |
| 253 | |
Brian O'Connor | 0f6677d | 2016-04-06 23:26:51 -0700 | [diff] [blame] | 254 | if test_resources and not test_resources_root: |
Brian O'Connor | e4da59d | 2016-04-08 00:32:18 -0700 | [diff] [blame] | 255 | test_resources_root = TEST_RESOURCES_ROOT |
| 256 | if test_resources_root and not test_resources: |
| 257 | test_resources = glob([test_resources_root + '**']) |
| 258 | if not test_resources and not test_resources_root: |
| 259 | test_resources = glob([TEST_RESOURCES_ROOT + '**']) |
| 260 | if test_resources: |
| 261 | test_resources_root = TEST_RESOURCES_ROOT |
Brian O'Connor | 0f6677d | 2016-04-06 23:26:51 -0700 | [diff] [blame] | 262 | |
Brian O'Connor | e4da59d | 2016-04-08 00:32:18 -0700 | [diff] [blame] | 263 | if test_srcs is None: |
| 264 | test_srcs = glob([TEST + '/*.java']) |
| 265 | |
Brian O'Connor | ee67495 | 2016-09-13 16:31:45 -0700 | [diff] [blame] | 266 | mvn_coords = group_id + ':' + name + ':jar:tests:' + version |
| 267 | |
Brian O'Connor | 9c2c823 | 2016-11-08 17:13:14 -0800 | [diff] [blame] | 268 | if test_srcs: |
| 269 | java_test( |
| 270 | name = name + '-tests', |
| 271 | srcs = test_srcs, |
| 272 | deps = deps + |
| 273 | test_deps + |
| 274 | [':' + name + '#non-osgi'], |
| 275 | resources = test_resources, |
| 276 | resources_root = test_resources_root, |
| 277 | maven_coords = mvn_coords, |
| 278 | visibility = visibility, |
| 279 | ) |
Brian O'Connor | be95f68 | 2016-05-18 15:40:19 -0700 | [diff] [blame] | 280 | |
Brian O'Connor | 9c2c823 | 2016-11-08 17:13:14 -0800 | [diff] [blame] | 281 | checkstyle( |
| 282 | name = name + '-tests', |
| 283 | srcs = test_srcs, |
| 284 | jar_target = ':' + name + '-tests', |
| 285 | ) |
| 286 | |
| 287 | sonar( |
| 288 | name = name + '-tests', |
| 289 | test = True |
| 290 | ) |
| 291 | |
| 292 | osgi_jar(name = name, |
| 293 | deps = deps, |
| 294 | group_id = group_id, |
| 295 | version = version, |
| 296 | visibility = visibility, |
| 297 | tests = [':' + name + '-tests'], |
| 298 | **kwargs) |
| 299 | else: |
| 300 | osgi_jar(name = name, |
| 301 | deps = deps, |
| 302 | group_id = group_id, |
| 303 | version = version, |
| 304 | visibility = visibility, |
| 305 | **kwargs) |
Thomas Vachuska | be1a196 | 2016-10-25 16:59:29 -0700 | [diff] [blame] | 306 | |
| 307 | def tar_file( |
| 308 | name, |
| 309 | srcs, |
| 310 | root = None, |
| 311 | out = None, |
| 312 | visibility = [ 'PUBLIC' ], |
| 313 | ): |
| 314 | |
| 315 | cmd = ( 'mkdir -p $TMP/%(root_dir)s && ' |
| 316 | 'cp -r $SRCDIR/ $TMP/%(root_dir)s && ' |
| 317 | 'tar -C $TMP -zcf $OUT %(root_dir)s' ) % { |
| 318 | 'root_dir': root if root is not None else name |
| 319 | } |
| 320 | |
| 321 | genrule( |
| 322 | name = name, |
| 323 | srcs = srcs, |
| 324 | bash = cmd, |
| 325 | out = out if out is not None else ( root if root is not None else name ) + '.tar.gz', |
| 326 | visibility = visibility, |
| 327 | ) |