blob: 845ac9b640e57016e01808cb4c1ed63532c0bbb1 [file] [log] [blame]
Ray Milkeyf80bbb22016-03-11 10:16:22 -08001# 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
15GERRIT = 'GERRIT:'
16GERRIT_API = 'GERRIT_API:'
17MAVEN_CENTRAL = 'MAVEN_CENTRAL:'
18MAVEN_LOCAL = 'MAVEN_LOCAL:'
19
20def 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
30def 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,
Brian O'Connor42c38cf2016-04-05 17:05:57 -0700122 maven_coords = id,
Ray Milkeyf80bbb22016-03-11 10:16:22 -0800123 deps = license,
124 visibility = visibility,
125 )
126 else:
127 srcjar = None
128 genrule(
129 name = '%s_src' % name,
130 cmd = ':>$OUT',
131 out = '__%s__no_src' % name,
132 )
133
134 if exported_deps:
135 prebuilt_jar(
136 name = '%s__jar' % name,
137 deps = deps + license,
138 binary_jar = ':%s__download_bin' % name,
139 source_jar = ':%s__download_src' % name if srcjar else None,
Brian O'Connor42c38cf2016-04-05 17:05:57 -0700140 maven_coords = id,
Ray Milkeyf80bbb22016-03-11 10:16:22 -0800141 )
142 java_library(
143 name = name,
144 exported_deps = exported_deps + [':' + name + '__jar'],
145 visibility = visibility,
146 )
147 else:
148 prebuilt_jar(
149 name = name,
150 deps = deps + license,
151 binary_jar = ':%s__download_bin' % name,
152 source_jar = ':%s__download_src' % name if srcjar else None,
153 visibility = visibility,
Brian O'Connor42c38cf2016-04-05 17:05:57 -0700154 maven_coords = id,
Ray Milkeyf80bbb22016-03-11 10:16:22 -0800155 )
156
157
158def merge_maven_jars(
159 name,
160 srcs,
161 visibility = []):
162
163 def cmd(jars):
164 return ('$(location //buck-tools:merge_jars) $OUT '
165 + ' '.join(['$(location %s)' % j for j in jars]))
166
167 genrule(
168 name = '%s__merged_bin' % name,
169 cmd = cmd(['%s__download_bin' % s for s in srcs]),
170 out = '%s__merged.jar' % name,
171 )
172 genrule(
173 name = '%s__merged_src' % name,
174 cmd = cmd(['%s__download_src' % s for s in srcs]),
175 # buck-tools/eclipse/project.py requires -src.jar suffix.
176 out = '%s__merged-src.jar' % name,
177 )
178 prebuilt_jar(
179 name = name,
180 binary_jar = ':%s__merged_bin' % name,
181 source_jar = ':%s__merged_src' % name,
182 visibility = visibility,
183 )