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 | |
Brian O'Connor | 0f6677d | 2016-04-06 23:26:51 -0700 | [diff] [blame] | 12 | |
Brian O'Connor | 1f16598 | 2016-04-06 21:36:09 -0700 | [diff] [blame] | 13 | ONOS_GROUP_ID = 'org.onosproject' |
Brian O'Connor | e642f7c | 2016-05-23 18:33:04 -0700 | [diff] [blame] | 14 | ONOS_VERSION = '1.7.0-SNAPSHOT' |
Brian O'Connor | e4da59d | 2016-04-08 00:32:18 -0700 | [diff] [blame] | 15 | ONOS_ARTIFACT_BASE = 'onos-' |
| 16 | |
| 17 | def _get_name(): |
| 18 | base_path = get_base_path() |
| 19 | return ONOS_ARTIFACT_BASE + base_path.replace('/', '-') #TODO Unix-separator |
Brian O'Connor | 1f16598 | 2016-04-06 21:36:09 -0700 | [diff] [blame] | 20 | |
Brian O'Connor | 40a3fbd | 2016-06-08 13:54:46 -0700 | [diff] [blame] | 21 | def 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 Vachuska | 275d2e8 | 2016-07-14 17:41:34 -0700 | [diff] [blame] | 40 | test = '//tools/build/conf:start-buck-daemon', |
Brian O'Connor | 40a3fbd | 2016-06-08 13:54:46 -0700 | [diff] [blame] | 41 | deps = [ jar_target ], |
| 42 | args = [ |
Thomas Vachuska | 275d2e8 | 2016-07-14 17:41:34 -0700 | [diff] [blame] | 43 | '$(location //tools/build/conf:buck-daemon-jar)', |
| 44 | 'checkstyle', |
Brian O'Connor | 40a3fbd | 2016-06-08 13:54:46 -0700 | [diff] [blame] | 45 | '$(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 | |
Ray Milkey | c340e28 | 2016-06-30 14:45:42 -0700 | [diff] [blame^] | 55 | def java_doc( |
| 56 | name, |
| 57 | title, |
| 58 | pkgs, |
| 59 | paths, |
| 60 | srcs = [], |
| 61 | deps = [], |
| 62 | visibility = [], |
| 63 | do_it_wrong = False, |
| 64 | ): |
| 65 | if do_it_wrong: |
| 66 | sourcepath = paths |
| 67 | else: |
| 68 | sourcepath = ['$SRCDIR/' + n for n in paths] |
| 69 | genrule( |
| 70 | name = name, |
| 71 | cmd = ' '.join([ |
| 72 | 'while ! test -f .buckconfig; do cd ..; done;', |
| 73 | 'javadoc', |
| 74 | '-quiet', |
| 75 | '-protected', |
| 76 | '-encoding UTF-8', |
| 77 | '-charset UTF-8', |
| 78 | '-notimestamp', |
| 79 | '-windowtitle "' + title + '"', |
| 80 | '-link http://docs.oracle.com/javase/7/docs/api', |
| 81 | '-subpackages ', |
| 82 | ':'.join(pkgs), |
| 83 | '-sourcepath ', |
| 84 | ':'.join(sourcepath), |
| 85 | ' -classpath ', |
| 86 | ':'.join(['$(classpath %s)' % n for n in deps]), |
| 87 | '-d $TMP', |
| 88 | ]) + ';jar cf $OUT -C $TMP .', |
| 89 | srcs = srcs, |
| 90 | out = name + '.jar', |
| 91 | visibility = visibility, |
| 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 = '*', |
| 107 | include_resources = NONE, |
| 108 | web_context = NONE, |
Brian O'Connor | e4da59d | 2016-04-08 00:32:18 -0700 | [diff] [blame] | 109 | resources = NONE, |
Brian O'Connor | 0f6677d | 2016-04-06 23:26:51 -0700 | [diff] [blame] | 110 | resources_root = None, |
Brian O'Connor | 42c38cf | 2016-04-05 17:05:57 -0700 | [diff] [blame] | 111 | **kwargs |
| 112 | ): |
| 113 | |
Brian O'Connor | e4da59d | 2016-04-08 00:32:18 -0700 | [diff] [blame] | 114 | # if name and _get_name() != name: |
| 115 | # print _get_name(), '!=', name |
| 116 | if name is None: |
| 117 | name = _get_name() |
| 118 | |
| 119 | if srcs is None: |
| 120 | srcs = glob([SRC + '/*.java']) |
| 121 | |
| 122 | if resources == NONE and resources_root is not None: |
| 123 | resources = glob([resources_root + '**']) |
| 124 | elif resources == NONE: |
| 125 | resources = glob([RESOURCES_ROOT + '**']) |
| 126 | |
| 127 | if resources and not resources_root: |
| 128 | resources_root = RESOURCES_ROOT |
| 129 | |
Brian O'Connor | 42c38cf | 2016-04-05 17:05:57 -0700 | [diff] [blame] | 130 | bare_jar_name = name + '-jar' |
| 131 | osgi_jar_name = name + '-osgi' |
| 132 | mvn_coords = group_id + ':' + name + ':' + version |
| 133 | |
Brian O'Connor | 0f6677d | 2016-04-06 23:26:51 -0700 | [diff] [blame] | 134 | |
Brian O'Connor | 42c38cf | 2016-04-05 17:05:57 -0700 | [diff] [blame] | 135 | java_library( |
| 136 | name = bare_jar_name, |
| 137 | srcs = srcs, |
| 138 | deps = deps, |
Brian O'Connor | 0f6677d | 2016-04-06 23:26:51 -0700 | [diff] [blame] | 139 | visibility = [], #intentially, not visible |
| 140 | resources = resources, |
| 141 | resources_root = resources_root, |
Brian O'Connor | 42c38cf | 2016-04-05 17:05:57 -0700 | [diff] [blame] | 142 | **kwargs |
| 143 | ) |
| 144 | |
Brian O'Connor | 4847ea3 | 2016-04-29 16:33:06 -0700 | [diff] [blame] | 145 | 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] | 146 | |
| 147 | args = ( '$(location :%s)' % bare_jar_name, #input jar |
| 148 | '$OUT', #output jar |
| 149 | cp, #classpath |
| 150 | name, #bundle name |
| 151 | group_id, #group id |
| 152 | version, #version |
| 153 | license, #license url |
Brian O'Connor | e5817c9 | 2016-04-06 15:41:48 -0700 | [diff] [blame] | 154 | "'%s'" % import_packages, #packages to import |
| 155 | "'%s'" % export_packages, #packages to export |
| 156 | include_resources, #custom includes to classpath |
Brian O'Connor | 42c38cf | 2016-04-05 17:05:57 -0700 | [diff] [blame] | 157 | web_context, #web context (REST API only) |
| 158 | description, #description |
| 159 | ) |
| 160 | |
| 161 | #TODO stage_jar is a horrendous hack |
| 162 | stage_jar = 'pushd $SRCDIR; mkdir bin; cd bin; jar xf $(location :%s); ls; popd; ' % bare_jar_name |
| 163 | wrap_jar = '$(exe //utils/osgiwrap:osgi-jar) ' + ' '.join(args) |
| 164 | bash = stage_jar + wrap_jar |
| 165 | if debug: |
| 166 | bash = stage_jar + DEBUG_ARG + ' ' + wrap_jar |
| 167 | print bash |
Brian O'Connor | 42c38cf | 2016-04-05 17:05:57 -0700 | [diff] [blame] | 168 | |
| 169 | genrule( |
| 170 | name = osgi_jar_name, |
| 171 | bash = bash, |
Brian O'Connor | 1f16598 | 2016-04-06 21:36:09 -0700 | [diff] [blame] | 172 | 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] | 173 | srcs = glob(['src/main/webapp/**']), |
Brian O'Connor | 42c38cf | 2016-04-05 17:05:57 -0700 | [diff] [blame] | 174 | visibility = [], #intentially, not visible |
| 175 | ) |
| 176 | |
| 177 | # TODO we really should shade the jar with maven flavor |
| 178 | prebuilt_jar( |
| 179 | name = name, |
| 180 | maven_coords = mvn_coords, |
| 181 | binary_jar = ':' + osgi_jar_name, |
| 182 | visibility = visibility, |
| 183 | ) |
| 184 | |
Brian O'Connor | 42c38cf | 2016-04-05 17:05:57 -0700 | [diff] [blame] | 185 | ### Checkstyle |
Brian O'Connor | 40a3fbd | 2016-06-08 13:54:46 -0700 | [diff] [blame] | 186 | checkstyle( |
Brian O'Connor | be95f68 | 2016-05-18 15:40:19 -0700 | [diff] [blame] | 187 | name = name + '-checkstyle-files', |
Brian O'Connor | 4847ea3 | 2016-04-29 16:33:06 -0700 | [diff] [blame] | 188 | srcs = srcs, |
Brian O'Connor | 40a3fbd | 2016-06-08 13:54:46 -0700 | [diff] [blame] | 189 | jar_target = ':'+ bare_jar_name, |
| 190 | ) |
Brian O'Connor | b3cc604 | 2016-04-25 11:55:51 -0700 | [diff] [blame] | 191 | |
Ray Milkey | c340e28 | 2016-06-30 14:45:42 -0700 | [diff] [blame^] | 192 | java_doc( |
| 193 | name = name + '-javadoc', |
| 194 | title = 'Java Docs', |
| 195 | pkgs = [ 'org.onosproject' ], |
| 196 | paths = [ 'src/main/java' ], |
| 197 | srcs = srcs, |
| 198 | deps = deps, |
| 199 | visibility = visibility, |
| 200 | do_it_wrong = False, |
| 201 | ) |
| 202 | |
Brian O'Connor | b3cc604 | 2016-04-25 11:55:51 -0700 | [diff] [blame] | 203 | # TODO add project config for intellij |
| 204 | # project_config( |
| 205 | # src_target = ':' + bare_jar_name, |
| 206 | # src_roots = [ 'src/main/java' ], |
| 207 | # test_target = ':' + name + '-tests', |
| 208 | # test_roots = [ 'src/test/java' ], |
| 209 | # ) |
| 210 | |
Brian O'Connor | 42c38cf | 2016-04-05 17:05:57 -0700 | [diff] [blame] | 211 | ### .m2 Install |
Brian O'Connor | b86c920 | 2016-04-05 20:15:04 -0700 | [diff] [blame] | 212 | mvn_cmd = ' '.join(( 'mvn install:install-file', |
| 213 | '-Dfile=$(location :%s)' % name, |
| 214 | '-DgroupId=%s' % group_id, |
| 215 | '-DartifactId=%s' % name, |
| 216 | '-Dversion=%s' % version, |
| 217 | '-Dpackaging=jar' )) |
Brian O'Connor | e5817c9 | 2016-04-06 15:41:48 -0700 | [diff] [blame] | 218 | cmd = mvn_cmd + ' > $OUT' |
| 219 | if FORCE_INSTALL: |
| 220 | # Add a random number to the command to force this rule to run. |
| 221 | # TODO We should make this configurable from CLI, perhaps with a flag. |
| 222 | cmd = 'FOO=%s ' % random.random() + cmd |
Brian O'Connor | 42c38cf | 2016-04-05 17:05:57 -0700 | [diff] [blame] | 223 | genrule( |
| 224 | name = name + '-install', |
Brian O'Connor | b86c920 | 2016-04-05 20:15:04 -0700 | [diff] [blame] | 225 | bash = cmd, |
Brian O'Connor | 42c38cf | 2016-04-05 17:05:57 -0700 | [diff] [blame] | 226 | out = 'install.log', |
| 227 | visibility = visibility, |
| 228 | ) |
| 229 | |
Brian O'Connor | e4da59d | 2016-04-08 00:32:18 -0700 | [diff] [blame] | 230 | def osgi_jar_with_tests( |
| 231 | name = None, |
| 232 | deps = [], |
| 233 | test_srcs = None, |
| 234 | test_deps = [ '//lib:TEST' ], |
| 235 | test_resources = None, |
| 236 | test_resources_root = None, |
| 237 | visibility = [ 'PUBLIC' ], |
| 238 | **kwargs |
| 239 | ): |
| 240 | |
| 241 | if name is None: |
| 242 | name = _get_name() |
| 243 | |
| 244 | osgi_jar(name = name, |
| 245 | deps = deps, |
| 246 | visibility = visibility, |
| 247 | **kwargs) |
Brian O'Connor | 0f6677d | 2016-04-06 23:26:51 -0700 | [diff] [blame] | 248 | |
| 249 | if test_resources and not test_resources_root: |
Brian O'Connor | e4da59d | 2016-04-08 00:32:18 -0700 | [diff] [blame] | 250 | test_resources_root = TEST_RESOURCES_ROOT |
| 251 | if test_resources_root and not test_resources: |
| 252 | test_resources = glob([test_resources_root + '**']) |
| 253 | if not test_resources and not test_resources_root: |
| 254 | test_resources = glob([TEST_RESOURCES_ROOT + '**']) |
| 255 | if test_resources: |
| 256 | test_resources_root = TEST_RESOURCES_ROOT |
Brian O'Connor | 0f6677d | 2016-04-06 23:26:51 -0700 | [diff] [blame] | 257 | |
Brian O'Connor | e4da59d | 2016-04-08 00:32:18 -0700 | [diff] [blame] | 258 | if test_srcs is None: |
| 259 | test_srcs = glob([TEST + '/*.java']) |
| 260 | |
| 261 | if not test_srcs: |
| 262 | print "Generating test rule for %s, but there are no tests." % name |
| 263 | |
| 264 | java_test( |
| 265 | name = name + '-tests', |
| 266 | srcs = test_srcs, |
| 267 | deps = deps + |
| 268 | test_deps + |
| 269 | [':' + name + '-jar'], |
| 270 | source_under_test = [':' + name + '-jar'], |
| 271 | resources = test_resources, |
| 272 | resources_root = test_resources_root, |
| 273 | visibility = visibility |
Brian O'Connor | be95f68 | 2016-05-18 15:40:19 -0700 | [diff] [blame] | 274 | ) |
| 275 | |
Brian O'Connor | 40a3fbd | 2016-06-08 13:54:46 -0700 | [diff] [blame] | 276 | checkstyle( |
| 277 | name = name + '-tests', |
| 278 | srcs = test_srcs, |
| 279 | jar_target = ':' + name + '-tests', |
| 280 | ) |
| 281 | |
Brian O'Connor | e642f7c | 2016-05-23 18:33:04 -0700 | [diff] [blame] | 282 | #FIXME need to run checkstyle on test sources |
Ray Milkey | c340e28 | 2016-06-30 14:45:42 -0700 | [diff] [blame^] | 283 | |
| 284 | |
| 285 | |