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