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