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, |
| 52 | visibility = [ 'PUBLIC' ] |
| 53 | ) |
| 54 | |
| 55 | remote_file( |
| 56 | name = name + '-jar', |
| 57 | out = '%s-%s.jar' % (artifactId, version), |
| 58 | url = 'mvn:%s:%s:%s:%s' % (groupId, artifactId, 'jar', version), |
| 59 | sha1 = sha1 |
| 60 | ) |
| 61 | return |
| 62 | |
Ray Milkey | f80bbb2 | 2016-03-11 10:16:22 -0800 | [diff] [blame] | 63 | from os import path |
| 64 | |
| 65 | parts = id.split(':') |
| 66 | if len(parts) not in [3, 4]: |
| 67 | raise NameError('%s:\nexpected id="groupId:artifactId:version[:classifier]"' |
| 68 | % id) |
| 69 | if len(parts) == 4: |
| 70 | group, artifact, version, classifier = parts |
| 71 | else: |
| 72 | group, artifact, version = parts |
| 73 | classifier = None |
| 74 | |
| 75 | # SNAPSHOT artifacts are handled differently on Google storage bucket: |
| 76 | # 'SNAPSHOT' is discarded from the directory name. However on other |
| 77 | # Maven repositories, most notable local repository located in |
| 78 | # ~/.m2/repository (and is supported through MAVEN_LOCAL repository) |
| 79 | # it must be preserved, otherwise the artifact wouldn't be found. |
| 80 | # Atm the SNAPSHOT part is only discarded for Google storage bucket. |
| 81 | if 'SNAPSHOT' in version and repository.startswith(GERRIT): |
| 82 | file_version = version.replace('-SNAPSHOT', '') |
| 83 | version = version.split('-SNAPSHOT')[0] + '-SNAPSHOT' |
| 84 | else: |
| 85 | file_version = version |
| 86 | |
| 87 | if classifier is not None: |
| 88 | file_version += '-' + classifier |
| 89 | |
| 90 | jar = path.join(name, artifact.lower() + '-' + file_version) |
| 91 | |
| 92 | url = '/'.join([ |
| 93 | repository, |
| 94 | group.replace('.', '/'), artifact, version, |
| 95 | artifact + '-' + file_version]) |
| 96 | |
| 97 | if full_url != '': |
| 98 | url = full_url |
| 99 | |
| 100 | binjar = jar + '.jar' |
| 101 | binurl = url + '.jar' |
| 102 | |
| 103 | srcjar = jar + '-src.jar' |
| 104 | srcurl = url + '-sources.jar' |
| 105 | |
| 106 | cmd = ['$(exe //buck-tools:download_file)', '-o', '$OUT', '-u', binurl] |
| 107 | if sha1: |
| 108 | cmd.extend(['-v', sha1]) |
| 109 | elif bin_sha1: |
| 110 | cmd.extend(['-v', bin_sha1]) |
| 111 | for x in exclude: |
| 112 | cmd.extend(['-x', x]) |
| 113 | if exclude_java_sources: |
| 114 | cmd.append('--exclude_java_sources') |
| 115 | if unsign: |
| 116 | cmd.append('--unsign') |
| 117 | |
| 118 | genrule( |
| 119 | name = '%s__download_bin' % name, |
| 120 | cmd = ' '.join(cmd), |
| 121 | out = binjar, |
| 122 | ) |
| 123 | license = ':LICENSE-' + license |
| 124 | if not local_license: |
| 125 | license = '//lib' + license |
| 126 | license = [license] |
| 127 | |
| 128 | if src_sha1 or attach_source: |
| 129 | cmd = ['$(exe //buck-tools:download_file)', '-o', '$OUT', '-u', srcurl] |
| 130 | if src_sha1: |
| 131 | cmd.extend(['-v', src_sha1]) |
| 132 | genrule( |
| 133 | name = '%s__download_src' % name, |
| 134 | cmd = ' '.join(cmd), |
| 135 | out = srcjar, |
| 136 | ) |
| 137 | prebuilt_jar( |
| 138 | name = '%s_src' % name, |
| 139 | binary_jar = ':%s__download_src' % name, |
Brian O'Connor | 42c38cf | 2016-04-05 17:05:57 -0700 | [diff] [blame] | 140 | maven_coords = id, |
Ray Milkey | f80bbb2 | 2016-03-11 10:16:22 -0800 | [diff] [blame] | 141 | deps = license, |
| 142 | visibility = visibility, |
| 143 | ) |
| 144 | else: |
| 145 | srcjar = None |
| 146 | genrule( |
| 147 | name = '%s_src' % name, |
| 148 | cmd = ':>$OUT', |
| 149 | out = '__%s__no_src' % name, |
| 150 | ) |
| 151 | |
| 152 | if exported_deps: |
| 153 | prebuilt_jar( |
| 154 | name = '%s__jar' % name, |
| 155 | deps = deps + license, |
| 156 | binary_jar = ':%s__download_bin' % name, |
| 157 | source_jar = ':%s__download_src' % name if srcjar else None, |
Brian O'Connor | 42c38cf | 2016-04-05 17:05:57 -0700 | [diff] [blame] | 158 | maven_coords = id, |
Ray Milkey | f80bbb2 | 2016-03-11 10:16:22 -0800 | [diff] [blame] | 159 | ) |
| 160 | java_library( |
| 161 | name = name, |
| 162 | exported_deps = exported_deps + [':' + name + '__jar'], |
| 163 | visibility = visibility, |
| 164 | ) |
| 165 | else: |
| 166 | prebuilt_jar( |
| 167 | name = name, |
| 168 | deps = deps + license, |
| 169 | binary_jar = ':%s__download_bin' % name, |
| 170 | source_jar = ':%s__download_src' % name if srcjar else None, |
| 171 | visibility = visibility, |
Brian O'Connor | 42c38cf | 2016-04-05 17:05:57 -0700 | [diff] [blame] | 172 | maven_coords = id, |
Ray Milkey | f80bbb2 | 2016-03-11 10:16:22 -0800 | [diff] [blame] | 173 | ) |
| 174 | |
| 175 | |
| 176 | def merge_maven_jars( |
| 177 | name, |
| 178 | srcs, |
| 179 | visibility = []): |
| 180 | |
| 181 | def cmd(jars): |
| 182 | return ('$(location //buck-tools:merge_jars) $OUT ' |
| 183 | + ' '.join(['$(location %s)' % j for j in jars])) |
| 184 | |
| 185 | genrule( |
| 186 | name = '%s__merged_bin' % name, |
| 187 | cmd = cmd(['%s__download_bin' % s for s in srcs]), |
| 188 | out = '%s__merged.jar' % name, |
| 189 | ) |
| 190 | genrule( |
| 191 | name = '%s__merged_src' % name, |
| 192 | cmd = cmd(['%s__download_src' % s for s in srcs]), |
| 193 | # buck-tools/eclipse/project.py requires -src.jar suffix. |
| 194 | out = '%s__merged-src.jar' % name, |
| 195 | ) |
| 196 | prebuilt_jar( |
| 197 | name = name, |
| 198 | binary_jar = ':%s__merged_bin' % name, |
| 199 | source_jar = ':%s__merged_src' % name, |
| 200 | visibility = visibility, |
| 201 | ) |