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