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', |
Brian O'Connor | 40a3fbd | 2016-06-08 13:54:46 -0700 | [diff] [blame] | 38 | deps = [ jar_target ], |
| 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 | |
| 94 | |
Brian O'Connor | 42c38cf | 2016-04-05 17:05:57 -0700 | [diff] [blame] | 95 | def osgi_jar( |
Brian O'Connor | e4da59d | 2016-04-08 00:32:18 -0700 | [diff] [blame] | 96 | name = None, |
| 97 | srcs = None, |
Brian O'Connor | 1f16598 | 2016-04-06 21:36:09 -0700 | [diff] [blame] | 98 | group_id = ONOS_GROUP_ID, |
| 99 | version = ONOS_VERSION, |
Brian O'Connor | 42c38cf | 2016-04-05 17:05:57 -0700 | [diff] [blame] | 100 | deps = [], |
| 101 | visibility = ['PUBLIC'], |
| 102 | license = 'NONE', |
| 103 | description = '', |
| 104 | debug = False, |
Brian O'Connor | e5817c9 | 2016-04-06 15:41:48 -0700 | [diff] [blame] | 105 | import_packages = '*', |
| 106 | export_packages = '*', |
Ray Milkey | 2a74983 | 2016-08-02 15:22:20 -0700 | [diff] [blame] | 107 | package_name_root = 'org.onosproject', |
Brian O'Connor | e5817c9 | 2016-04-06 15:41:48 -0700 | [diff] [blame] | 108 | include_resources = NONE, |
| 109 | web_context = NONE, |
Brian O'Connor | e8468b5 | 2016-07-25 13:42:36 -0700 | [diff] [blame^] | 110 | api_title = NONE, |
| 111 | api_version = NONE, |
| 112 | api_package = NONE, |
| 113 | api_description = NONE, |
Brian O'Connor | e4da59d | 2016-04-08 00:32:18 -0700 | [diff] [blame] | 114 | resources = NONE, |
Brian O'Connor | 0f6677d | 2016-04-06 23:26:51 -0700 | [diff] [blame] | 115 | resources_root = None, |
Brian O'Connor | 42c38cf | 2016-04-05 17:05:57 -0700 | [diff] [blame] | 116 | **kwargs |
| 117 | ): |
| 118 | |
Brian O'Connor | e4da59d | 2016-04-08 00:32:18 -0700 | [diff] [blame] | 119 | # if name and _get_name() != name: |
| 120 | # print _get_name(), '!=', name |
| 121 | if name is None: |
| 122 | name = _get_name() |
| 123 | |
| 124 | if srcs is None: |
| 125 | srcs = glob([SRC + '/*.java']) |
| 126 | |
| 127 | if resources == NONE and resources_root is not None: |
| 128 | resources = glob([resources_root + '**']) |
| 129 | elif resources == NONE: |
| 130 | resources = glob([RESOURCES_ROOT + '**']) |
| 131 | |
| 132 | if resources and not resources_root: |
| 133 | resources_root = RESOURCES_ROOT |
| 134 | |
Brian O'Connor | e8468b5 | 2016-07-25 13:42:36 -0700 | [diff] [blame^] | 135 | if api_title != NONE: |
| 136 | r = 'WEB-INF/classes/apidoc/swagger.json=bin/swagger.json' |
| 137 | include_resources = include_resources + ',' + r if include_resources != NONE else r |
| 138 | |
Brian O'Connor | 42c38cf | 2016-04-05 17:05:57 -0700 | [diff] [blame] | 139 | bare_jar_name = name + '-jar' |
| 140 | osgi_jar_name = name + '-osgi' |
| 141 | mvn_coords = group_id + ':' + name + ':' + version |
| 142 | |
Brian O'Connor | 0f6677d | 2016-04-06 23:26:51 -0700 | [diff] [blame] | 143 | |
Brian O'Connor | e8468b5 | 2016-07-25 13:42:36 -0700 | [diff] [blame^] | 144 | onos_jar( |
Brian O'Connor | 42c38cf | 2016-04-05 17:05:57 -0700 | [diff] [blame] | 145 | name = bare_jar_name, |
| 146 | srcs = srcs, |
| 147 | deps = deps, |
Brian O'Connor | 0f6677d | 2016-04-06 23:26:51 -0700 | [diff] [blame] | 148 | visibility = [], #intentially, not visible |
| 149 | resources = resources, |
| 150 | resources_root = resources_root, |
Brian O'Connor | e8468b5 | 2016-07-25 13:42:36 -0700 | [diff] [blame^] | 151 | web_context = web_context, |
| 152 | api_title = api_title, |
| 153 | api_version = api_version, |
| 154 | api_package = api_package, |
| 155 | api_description = api_description, |
Brian O'Connor | 42c38cf | 2016-04-05 17:05:57 -0700 | [diff] [blame] | 156 | **kwargs |
| 157 | ) |
| 158 | |
Brian O'Connor | 4847ea3 | 2016-04-29 16:33:06 -0700 | [diff] [blame] | 159 | cp = ':'.join(['$(classpath %s)' % c for c in deps]) if deps else '""' |
Brian O'Connor | 42c38cf | 2016-04-05 17:05:57 -0700 | [diff] [blame] | 160 | |
| 161 | args = ( '$(location :%s)' % bare_jar_name, #input jar |
| 162 | '$OUT', #output jar |
| 163 | cp, #classpath |
| 164 | name, #bundle name |
| 165 | group_id, #group id |
| 166 | version, #version |
| 167 | license, #license url |
Brian O'Connor | e5817c9 | 2016-04-06 15:41:48 -0700 | [diff] [blame] | 168 | "'%s'" % import_packages, #packages to import |
| 169 | "'%s'" % export_packages, #packages to export |
| 170 | include_resources, #custom includes to classpath |
Brian O'Connor | 42c38cf | 2016-04-05 17:05:57 -0700 | [diff] [blame] | 171 | web_context, #web context (REST API only) |
| 172 | description, #description |
| 173 | ) |
| 174 | |
| 175 | #TODO stage_jar is a horrendous hack |
| 176 | stage_jar = 'pushd $SRCDIR; mkdir bin; cd bin; jar xf $(location :%s); ls; popd; ' % bare_jar_name |
| 177 | wrap_jar = '$(exe //utils/osgiwrap:osgi-jar) ' + ' '.join(args) |
| 178 | bash = stage_jar + wrap_jar |
| 179 | if debug: |
| 180 | bash = stage_jar + DEBUG_ARG + ' ' + wrap_jar |
| 181 | print bash |
Brian O'Connor | 42c38cf | 2016-04-05 17:05:57 -0700 | [diff] [blame] | 182 | |
Brian O'Connor | e8468b5 | 2016-07-25 13:42:36 -0700 | [diff] [blame^] | 183 | # FIXME: make sure that /swagger.json gets filtered |
Brian O'Connor | 42c38cf | 2016-04-05 17:05:57 -0700 | [diff] [blame] | 184 | genrule( |
| 185 | name = osgi_jar_name, |
| 186 | bash = bash, |
Brian O'Connor | 1f16598 | 2016-04-06 21:36:09 -0700 | [diff] [blame] | 187 | out = '%s-%s.jar' % (name, version), #FIXME add version to jar file |
Brian O'Connor | e5817c9 | 2016-04-06 15:41:48 -0700 | [diff] [blame] | 188 | srcs = glob(['src/main/webapp/**']), |
Brian O'Connor | 42c38cf | 2016-04-05 17:05:57 -0700 | [diff] [blame] | 189 | visibility = [], #intentially, not visible |
| 190 | ) |
| 191 | |
| 192 | # TODO we really should shade the jar with maven flavor |
| 193 | prebuilt_jar( |
| 194 | name = name, |
| 195 | maven_coords = mvn_coords, |
| 196 | binary_jar = ':' + osgi_jar_name, |
| 197 | visibility = visibility, |
| 198 | ) |
| 199 | |
Brian O'Connor | 42c38cf | 2016-04-05 17:05:57 -0700 | [diff] [blame] | 200 | ### Checkstyle |
Brian O'Connor | 40a3fbd | 2016-06-08 13:54:46 -0700 | [diff] [blame] | 201 | checkstyle( |
Brian O'Connor | be95f68 | 2016-05-18 15:40:19 -0700 | [diff] [blame] | 202 | name = name + '-checkstyle-files', |
Brian O'Connor | 4847ea3 | 2016-04-29 16:33:06 -0700 | [diff] [blame] | 203 | srcs = srcs, |
Brian O'Connor | 40a3fbd | 2016-06-08 13:54:46 -0700 | [diff] [blame] | 204 | jar_target = ':'+ bare_jar_name, |
| 205 | ) |
Brian O'Connor | b3cc604 | 2016-04-25 11:55:51 -0700 | [diff] [blame] | 206 | |
Ray Milkey | c340e28 | 2016-06-30 14:45:42 -0700 | [diff] [blame] | 207 | java_doc( |
| 208 | name = name + '-javadoc', |
| 209 | title = 'Java Docs', |
Ray Milkey | 2a74983 | 2016-08-02 15:22:20 -0700 | [diff] [blame] | 210 | pkgs = [ package_name_root ], |
Ray Milkey | c340e28 | 2016-06-30 14:45:42 -0700 | [diff] [blame] | 211 | paths = [ 'src/main/java' ], |
| 212 | srcs = srcs, |
| 213 | deps = deps, |
| 214 | visibility = visibility, |
| 215 | do_it_wrong = False, |
| 216 | ) |
| 217 | |
Brian O'Connor | b3cc604 | 2016-04-25 11:55:51 -0700 | [diff] [blame] | 218 | # TODO add project config for intellij |
| 219 | # project_config( |
| 220 | # src_target = ':' + bare_jar_name, |
| 221 | # src_roots = [ 'src/main/java' ], |
| 222 | # test_target = ':' + name + '-tests', |
| 223 | # test_roots = [ 'src/test/java' ], |
| 224 | # ) |
| 225 | |
Brian O'Connor | 42c38cf | 2016-04-05 17:05:57 -0700 | [diff] [blame] | 226 | ### .m2 Install |
Brian O'Connor | b86c920 | 2016-04-05 20:15:04 -0700 | [diff] [blame] | 227 | mvn_cmd = ' '.join(( 'mvn install:install-file', |
| 228 | '-Dfile=$(location :%s)' % name, |
| 229 | '-DgroupId=%s' % group_id, |
| 230 | '-DartifactId=%s' % name, |
| 231 | '-Dversion=%s' % version, |
| 232 | '-Dpackaging=jar' )) |
Brian O'Connor | e5817c9 | 2016-04-06 15:41:48 -0700 | [diff] [blame] | 233 | cmd = mvn_cmd + ' > $OUT' |
| 234 | if FORCE_INSTALL: |
| 235 | # Add a random number to the command to force this rule to run. |
| 236 | # TODO We should make this configurable from CLI, perhaps with a flag. |
| 237 | cmd = 'FOO=%s ' % random.random() + cmd |
Brian O'Connor | 42c38cf | 2016-04-05 17:05:57 -0700 | [diff] [blame] | 238 | genrule( |
| 239 | name = name + '-install', |
Brian O'Connor | b86c920 | 2016-04-05 20:15:04 -0700 | [diff] [blame] | 240 | bash = cmd, |
Brian O'Connor | 42c38cf | 2016-04-05 17:05:57 -0700 | [diff] [blame] | 241 | out = 'install.log', |
| 242 | visibility = visibility, |
| 243 | ) |
| 244 | |
Brian O'Connor | e4da59d | 2016-04-08 00:32:18 -0700 | [diff] [blame] | 245 | def osgi_jar_with_tests( |
| 246 | name = None, |
| 247 | deps = [], |
| 248 | test_srcs = None, |
| 249 | test_deps = [ '//lib:TEST' ], |
| 250 | test_resources = None, |
| 251 | test_resources_root = None, |
| 252 | visibility = [ 'PUBLIC' ], |
| 253 | **kwargs |
| 254 | ): |
| 255 | |
| 256 | if name is None: |
| 257 | name = _get_name() |
| 258 | |
| 259 | osgi_jar(name = name, |
| 260 | deps = deps, |
| 261 | visibility = visibility, |
| 262 | **kwargs) |
Brian O'Connor | 0f6677d | 2016-04-06 23:26:51 -0700 | [diff] [blame] | 263 | |
| 264 | if test_resources and not test_resources_root: |
Brian O'Connor | e4da59d | 2016-04-08 00:32:18 -0700 | [diff] [blame] | 265 | test_resources_root = TEST_RESOURCES_ROOT |
| 266 | if test_resources_root and not test_resources: |
| 267 | test_resources = glob([test_resources_root + '**']) |
| 268 | if not test_resources and not test_resources_root: |
| 269 | test_resources = glob([TEST_RESOURCES_ROOT + '**']) |
| 270 | if test_resources: |
| 271 | test_resources_root = TEST_RESOURCES_ROOT |
Brian O'Connor | 0f6677d | 2016-04-06 23:26:51 -0700 | [diff] [blame] | 272 | |
Brian O'Connor | e4da59d | 2016-04-08 00:32:18 -0700 | [diff] [blame] | 273 | if test_srcs is None: |
| 274 | test_srcs = glob([TEST + '/*.java']) |
| 275 | |
Brian O'Connor | e4da59d | 2016-04-08 00:32:18 -0700 | [diff] [blame] | 276 | java_test( |
| 277 | name = name + '-tests', |
| 278 | srcs = test_srcs, |
| 279 | deps = deps + |
| 280 | test_deps + |
| 281 | [':' + name + '-jar'], |
| 282 | source_under_test = [':' + name + '-jar'], |
| 283 | resources = test_resources, |
| 284 | resources_root = test_resources_root, |
| 285 | visibility = visibility |
Brian O'Connor | be95f68 | 2016-05-18 15:40:19 -0700 | [diff] [blame] | 286 | ) |
| 287 | |
Brian O'Connor | 40a3fbd | 2016-06-08 13:54:46 -0700 | [diff] [blame] | 288 | checkstyle( |
| 289 | name = name + '-tests', |
| 290 | srcs = test_srcs, |
| 291 | jar_target = ':' + name + '-tests', |
| 292 | ) |
| 293 | |
Brian O'Connor | e642f7c | 2016-05-23 18:33:04 -0700 | [diff] [blame] | 294 | #FIXME need to run checkstyle on test sources |