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