Ray Milkey | f80bbb2 | 2016-03-11 10:16:22 -0800 | [diff] [blame] | 1 | # Copyright (C) 2013 The Android Open Source Project |
| 2 | # |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | # you may not use this file except in compliance with the License. |
| 5 | # You may obtain a copy of the License at |
| 6 | # |
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 9 | # Unless required by applicable law or agreed to in writing, software |
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | # See the License for the specific language governing permissions and |
| 13 | # limitations under the License. |
| 14 | |
| 15 | GERRIT = 'GERRIT:' |
| 16 | GERRIT_API = 'GERRIT_API:' |
| 17 | MAVEN_CENTRAL = 'MAVEN_CENTRAL:' |
| 18 | MAVEN_LOCAL = 'MAVEN_LOCAL:' |
| 19 | |
| 20 | def define_license(name): |
| 21 | n = 'LICENSE-' + name |
| 22 | genrule( |
| 23 | name = n, |
| 24 | cmd = 'ln -s $SRCS $OUT', |
| 25 | srcs = [n], |
| 26 | out = n, |
| 27 | visibility = ['PUBLIC'], |
| 28 | ) |
| 29 | |
| 30 | def maven_jar( |
| 31 | name, |
| 32 | id, |
| 33 | license, |
| 34 | exclude = [], |
| 35 | exclude_java_sources = False, |
| 36 | unsign = False, |
| 37 | deps = [], |
| 38 | exported_deps = [], |
| 39 | sha1 = '', bin_sha1 = '', src_sha1 = '', |
| 40 | repository = MAVEN_CENTRAL, |
| 41 | attach_source = True, |
| 42 | visibility = ['PUBLIC'], |
| 43 | local_license = False, |
| 44 | full_url = ''): |
Brian O'Connor | 5f207d2 | 2016-04-08 00:31:19 -0700 | [diff] [blame] | 45 | |
| 46 | if not full_url: |
| 47 | groupId, artifactId, version = id.split(':') |
| 48 | |
| 49 | prebuilt_jar( |
| 50 | name = name, |
| 51 | binary_jar = ':%s-jar' % name, |
Ray Milkey | 820d633 | 2016-04-11 13:20:42 -0700 | [diff] [blame] | 52 | maven_coords = id, |
Brian O'Connor | 5f207d2 | 2016-04-08 00:31:19 -0700 | [diff] [blame] | 53 | visibility = [ 'PUBLIC' ] |
| 54 | ) |
| 55 | |
| 56 | remote_file( |
| 57 | name = name + '-jar', |
| 58 | out = '%s-%s.jar' % (artifactId, version), |
| 59 | url = 'mvn:%s:%s:%s:%s' % (groupId, artifactId, 'jar', version), |
| 60 | sha1 = sha1 |
| 61 | ) |
| 62 | return |
| 63 | |
Ray Milkey | f80bbb2 | 2016-03-11 10:16:22 -0800 | [diff] [blame] | 64 | from os import path |
| 65 | |
| 66 | parts = id.split(':') |
| 67 | if len(parts) not in [3, 4]: |
| 68 | raise NameError('%s:\nexpected id="groupId:artifactId:version[:classifier]"' |
| 69 | % id) |
| 70 | if len(parts) == 4: |
| 71 | group, artifact, version, classifier = parts |
| 72 | else: |
| 73 | group, artifact, version = parts |
| 74 | classifier = None |
| 75 | |
| 76 | # SNAPSHOT artifacts are handled differently on Google storage bucket: |
| 77 | # 'SNAPSHOT' is discarded from the directory name. However on other |
| 78 | # Maven repositories, most notable local repository located in |
| 79 | # ~/.m2/repository (and is supported through MAVEN_LOCAL repository) |
| 80 | # it must be preserved, otherwise the artifact wouldn't be found. |
| 81 | # Atm the SNAPSHOT part is only discarded for Google storage bucket. |
| 82 | if 'SNAPSHOT' in version and repository.startswith(GERRIT): |
| 83 | file_version = version.replace('-SNAPSHOT', '') |
| 84 | version = version.split('-SNAPSHOT')[0] + '-SNAPSHOT' |
| 85 | else: |
| 86 | file_version = version |
| 87 | |
| 88 | if classifier is not None: |
| 89 | file_version += '-' + classifier |
| 90 | |
| 91 | jar = path.join(name, artifact.lower() + '-' + file_version) |
| 92 | |
| 93 | url = '/'.join([ |
| 94 | repository, |
| 95 | group.replace('.', '/'), artifact, version, |
| 96 | artifact + '-' + file_version]) |
| 97 | |
| 98 | if full_url != '': |
| 99 | url = full_url |
| 100 | |
| 101 | binjar = jar + '.jar' |
| 102 | binurl = url + '.jar' |
| 103 | |
| 104 | srcjar = jar + '-src.jar' |
| 105 | srcurl = url + '-sources.jar' |
| 106 | |
| 107 | cmd = ['$(exe //buck-tools:download_file)', '-o', '$OUT', '-u', binurl] |
| 108 | if sha1: |
| 109 | cmd.extend(['-v', sha1]) |
| 110 | elif bin_sha1: |
| 111 | cmd.extend(['-v', bin_sha1]) |
| 112 | for x in exclude: |
| 113 | cmd.extend(['-x', x]) |
| 114 | if exclude_java_sources: |
| 115 | cmd.append('--exclude_java_sources') |
| 116 | if unsign: |
| 117 | cmd.append('--unsign') |
| 118 | |
| 119 | genrule( |
| 120 | name = '%s__download_bin' % name, |
| 121 | cmd = ' '.join(cmd), |
| 122 | out = binjar, |
| 123 | ) |
| 124 | license = ':LICENSE-' + license |
| 125 | if not local_license: |
| 126 | license = '//lib' + license |
| 127 | license = [license] |
| 128 | |
| 129 | if src_sha1 or attach_source: |
| 130 | cmd = ['$(exe //buck-tools:download_file)', '-o', '$OUT', '-u', srcurl] |
| 131 | if src_sha1: |
| 132 | cmd.extend(['-v', src_sha1]) |
| 133 | genrule( |
| 134 | name = '%s__download_src' % name, |
| 135 | cmd = ' '.join(cmd), |
| 136 | out = srcjar, |
| 137 | ) |
| 138 | prebuilt_jar( |
| 139 | name = '%s_src' % name, |
| 140 | binary_jar = ':%s__download_src' % name, |
Brian O'Connor | 42c38cf | 2016-04-05 17:05:57 -0700 | [diff] [blame] | 141 | maven_coords = id, |
Ray Milkey | f80bbb2 | 2016-03-11 10:16:22 -0800 | [diff] [blame] | 142 | deps = license, |
| 143 | visibility = visibility, |
| 144 | ) |
| 145 | else: |
| 146 | srcjar = None |
| 147 | genrule( |
| 148 | name = '%s_src' % name, |
| 149 | cmd = ':>$OUT', |
| 150 | out = '__%s__no_src' % name, |
| 151 | ) |
| 152 | |
| 153 | if exported_deps: |
| 154 | prebuilt_jar( |
| 155 | name = '%s__jar' % name, |
| 156 | deps = deps + license, |
| 157 | binary_jar = ':%s__download_bin' % name, |
| 158 | source_jar = ':%s__download_src' % name if srcjar else None, |
Brian O'Connor | 42c38cf | 2016-04-05 17:05:57 -0700 | [diff] [blame] | 159 | maven_coords = id, |
Ray Milkey | f80bbb2 | 2016-03-11 10:16:22 -0800 | [diff] [blame] | 160 | ) |
| 161 | java_library( |
| 162 | name = name, |
| 163 | exported_deps = exported_deps + [':' + name + '__jar'], |
| 164 | visibility = visibility, |
| 165 | ) |
| 166 | else: |
| 167 | prebuilt_jar( |
| 168 | name = name, |
| 169 | deps = deps + license, |
| 170 | binary_jar = ':%s__download_bin' % name, |
| 171 | source_jar = ':%s__download_src' % name if srcjar else None, |
| 172 | visibility = visibility, |
Brian O'Connor | 42c38cf | 2016-04-05 17:05:57 -0700 | [diff] [blame] | 173 | maven_coords = id, |
Ray Milkey | f80bbb2 | 2016-03-11 10:16:22 -0800 | [diff] [blame] | 174 | ) |
| 175 | |
| 176 | |
| 177 | def merge_maven_jars( |
| 178 | name, |
| 179 | srcs, |
| 180 | visibility = []): |
| 181 | |
| 182 | def cmd(jars): |
| 183 | return ('$(location //buck-tools:merge_jars) $OUT ' |
| 184 | + ' '.join(['$(location %s)' % j for j in jars])) |
| 185 | |
| 186 | genrule( |
| 187 | name = '%s__merged_bin' % name, |
| 188 | cmd = cmd(['%s__download_bin' % s for s in srcs]), |
| 189 | out = '%s__merged.jar' % name, |
| 190 | ) |
| 191 | genrule( |
| 192 | name = '%s__merged_src' % name, |
| 193 | cmd = cmd(['%s__download_src' % s for s in srcs]), |
| 194 | # buck-tools/eclipse/project.py requires -src.jar suffix. |
| 195 | out = '%s__merged-src.jar' % name, |
| 196 | ) |
| 197 | prebuilt_jar( |
| 198 | name = name, |
| 199 | binary_jar = ':%s__merged_bin' % name, |
| 200 | source_jar = ':%s__merged_src' % name, |
| 201 | visibility = visibility, |
| 202 | ) |