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