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