Debian Codegraph @

tools/run_tests/artifacts/artifact_targets.py

raw
1 #!/usr/bin/env python3
2 # Copyright 2016 gRPC authors.
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
7 #
8 # http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15 """Definition of targets to build artifacts."""
16
17 import os.path
18 import random
19 import string
20 import sys
21
22 sys.path.insert(0, os.path.abspath('..'))
23 import python_utils.jobset as jobset
24
25 _LATEST_MANYLINUX = "manylinux2014"
26
27
28 def create_docker_jobspec(name,
29 dockerfile_dir,
30 shell_command,
31 environ={},
32 flake_retries=0,
33 timeout_retries=0,
34 timeout_seconds=30 * 60,
35 extra_docker_args=None,
36 verbose_success=False):
37 """Creates jobspec for a task running under docker."""
38 environ = environ.copy()
39 environ['ARTIFACTS_OUT'] = 'artifacts/%s' % name
40
41 docker_args = []
42 for k, v in list(environ.items()):
43 docker_args += ['-e', '%s=%s' % (k, v)]
44 docker_env = {
45 'DOCKERFILE_DIR': dockerfile_dir,
46 'DOCKER_RUN_SCRIPT': 'tools/run_tests/dockerize/docker_run.sh',
47 'DOCKER_RUN_SCRIPT_COMMAND': shell_command,
48 'OUTPUT_DIR': 'artifacts'
49 }
50 if extra_docker_args is not None:
51 docker_env['EXTRA_DOCKER_ARGS'] = extra_docker_args
52 jobspec = jobset.JobSpec(
53 cmdline=['tools/run_tests/dockerize/build_and_run_docker.sh'] +
54 docker_args,
55 environ=docker_env,
56 shortname='build_artifact.%s' % (name),
57 timeout_seconds=timeout_seconds,
58 flake_retries=flake_retries,
59 timeout_retries=timeout_retries,
60 verbose_success=verbose_success)
61 return jobspec
62
63
64 def create_jobspec(name,
65 cmdline,
66 environ={},
67 shell=False,
68 flake_retries=0,
69 timeout_retries=0,
70 timeout_seconds=30 * 60,
71 use_workspace=False,
72 cpu_cost=1.0,
73 verbose_success=False):
74 """Creates jobspec."""
75 environ = environ.copy()
76 if use_workspace:
77 environ['WORKSPACE_NAME'] = 'workspace_%s' % name
78 environ['ARTIFACTS_OUT'] = os.path.join('..', 'artifacts', name)
79 cmdline = ['bash', 'tools/run_tests/artifacts/run_in_workspace.sh'
80 ] + cmdline
81 else:
82 environ['ARTIFACTS_OUT'] = os.path.join('artifacts', name)
83
84 jobspec = jobset.JobSpec(cmdline=cmdline,
85 environ=environ,
86 shortname='build_artifact.%s' % (name),
87 timeout_seconds=timeout_seconds,
88 flake_retries=flake_retries,
89 timeout_retries=timeout_retries,
90 shell=shell,
91 cpu_cost=cpu_cost,
92 verbose_success=verbose_success)
93 return jobspec
94
95
96 _MACOS_COMPAT_FLAG = '-mmacosx-version-min=10.10'
97
98 _ARCH_FLAG_MAP = {'x86': '-m32', 'x64': '-m64'}
99
100
101 class PythonArtifact:
102 """Builds Python artifacts."""
103
104 def __init__(self, platform, arch, py_version, presubmit=False):
105 self.name = 'python_%s_%s_%s' % (platform, arch, py_version)
106 self.platform = platform
107 self.arch = arch
108 self.labels = ['artifact', 'python', platform, arch, py_version]
109 if presubmit:
110 self.labels.append('presubmit')
111 self.py_version = py_version
112 if platform == _LATEST_MANYLINUX:
113 self.labels.append('latest-manylinux')
114 if 'manylinux' in platform:
115 self.labels.append('linux')
116 if 'linux_extra' in platform:
117 # linux_extra wheels used to be built by a separate kokoro job.
118 # Their build is now much faster, so they can be included
119 # in the regular artifact build.
120 self.labels.append('linux')
121 if 'musllinux' in platform:
122 self.labels.append('linux')
123
124 def pre_build_jobspecs(self):
125 return []
126
127 def build_jobspec(self, inner_jobs=None):
128 environ = {}
129 if inner_jobs is not None:
130 # set number of parallel jobs when building native extension
131 # building the native extension is the most time-consuming part of the build
132 environ['GRPC_PYTHON_BUILD_EXT_COMPILER_JOBS'] = str(inner_jobs)
133
134 # This is necessary due to https://github.com/pypa/wheel/issues/406.
135 # distutils incorrectly generates a universal2 artifact that only contains
136 # x86_64 libraries.
137 if self.platform == "macos" and self.arch == "x64":
138 environ["GRPC_UNIVERSAL2_REPAIR"] = "true"
139
140 if self.platform == 'linux_extra':
141 # Crosscompilation build for armv7 (e.g. Raspberry Pi)
142 environ['PYTHON'] = '/opt/python/{}/bin/python3'.format(
143 self.py_version)
144 environ['PIP'] = '/opt/python/{}/bin/pip3'.format(self.py_version)
145 environ['GRPC_SKIP_PIP_CYTHON_UPGRADE'] = 'TRUE'
146 environ['GRPC_SKIP_TWINE_CHECK'] = 'TRUE'
147 return create_docker_jobspec(
148 self.name,
149 'tools/dockerfile/grpc_artifact_python_linux_{}'.format(
150 self.arch),
151 'tools/run_tests/artifacts/build_artifact_python.sh',
152 environ=environ,
153 timeout_seconds=60 * 60)
154 elif 'manylinux' in self.platform:
155 if self.arch == 'x86':
156 environ['SETARCH_CMD'] = 'linux32'
157 # Inside the manylinux container, the python installations are located in
158 # special places...
159 environ['PYTHON'] = '/opt/python/{}/bin/python'.format(
160 self.py_version)
161 environ['PIP'] = '/opt/python/{}/bin/pip'.format(self.py_version)
162 environ['GRPC_SKIP_PIP_CYTHON_UPGRADE'] = 'TRUE'
163 if self.arch == 'aarch64':
164 environ['GRPC_SKIP_TWINE_CHECK'] = 'TRUE'
165 else:
166 # only run auditwheel if we're not crosscompiling
167 environ['GRPC_RUN_AUDITWHEEL_REPAIR'] = 'TRUE'
168 # only build the packages that depend on grpcio-tools
169 # if we're not crosscompiling.
170 # - they require protoc to run on current architecture
171 # - they only have sdist packages anyway, so it's useless to build them again
172 environ['GRPC_BUILD_GRPCIO_TOOLS_DEPENDENTS'] = 'TRUE'
173 return create_docker_jobspec(
174 self.name,
175 'tools/dockerfile/grpc_artifact_python_%s_%s' %
176 (self.platform, self.arch),
177 'tools/run_tests/artifacts/build_artifact_python.sh',
178 environ=environ,
179 timeout_seconds=60 * 60 * 2)
180 elif 'musllinux' in self.platform:
181 environ['PYTHON'] = '/opt/python/{}/bin/python'.format(
182 self.py_version)
183 environ['PIP'] = '/opt/python/{}/bin/pip'.format(self.py_version)
184 environ['GRPC_SKIP_PIP_CYTHON_UPGRADE'] = 'TRUE'
185 environ['GRPC_RUN_AUDITWHEEL_REPAIR'] = 'TRUE'
186 environ['GRPC_PYTHON_BUILD_WITH_STATIC_LIBSTDCXX'] = 'TRUE'
187 return create_docker_jobspec(
188 self.name,
189 'tools/dockerfile/grpc_artifact_python_%s_%s' %
190 (self.platform, self.arch),
191 'tools/run_tests/artifacts/build_artifact_python.sh',
192 environ=environ,
193 timeout_seconds=60 * 60 * 2)
194 elif self.platform == 'windows':
195 environ['EXT_COMPILER'] = 'msvc'
196 # For some reason, the batch script %random% always runs with the same
197 # seed. We create a random temp-dir here
198 dir = ''.join(
199 random.choice(string.ascii_uppercase) for _ in range(10))
200 return create_jobspec(self.name, [
201 'tools\\run_tests\\artifacts\\build_artifact_python.bat',
202 self.py_version, '32' if self.arch == 'x86' else '64'
203 ],
204 environ=environ,
205 timeout_seconds=45 * 60,
206 use_workspace=True)
207 else:
208 environ['PYTHON'] = self.py_version
209 environ['SKIP_PIP_INSTALL'] = 'TRUE'
210 return create_jobspec(
211 self.name,
212 ['tools/run_tests/artifacts/build_artifact_python.sh'],
213 environ=environ,
214 timeout_seconds=60 * 60 * 2,
215 use_workspace=True)
216
217 def __str__(self):
218 return self.name
219
220
221 class RubyArtifact:
222 """Builds ruby native gem."""
223
224 def __init__(self, platform, gem_platform, presubmit=False):
225 self.name = 'ruby_native_gem_%s_%s' % (platform, gem_platform)
226 self.platform = platform
227 self.gem_platform = gem_platform
228 self.labels = ['artifact', 'ruby', platform, gem_platform]
229 if presubmit:
230 self.labels.append('presubmit')
231
232 def pre_build_jobspecs(self):
233 return []
234
235 def build_jobspec(self, inner_jobs=None):
236 environ = {}
237 if inner_jobs is not None:
238 # set number of parallel jobs when building native extension
239 environ['GRPC_RUBY_BUILD_PROCS'] = str(inner_jobs)
240 # Ruby build uses docker internally and docker cannot be nested.
241 # We are using a custom workspace instead.
242 return create_jobspec(self.name, [
243 'tools/run_tests/artifacts/build_artifact_ruby.sh',
244 self.gem_platform
245 ],
246 use_workspace=True,
247 timeout_seconds=90 * 60,
248 environ=environ)
249
250
251 class PHPArtifact:
252 """Builds PHP PECL package"""
253
254 def __init__(self, platform, arch, presubmit=False):
255 self.name = 'php_pecl_package_{0}_{1}'.format(platform, arch)
256 self.platform = platform
257 self.arch = arch
258 self.labels = ['artifact', 'php', platform, arch]
259 if presubmit:
260 self.labels.append('presubmit')
261
262 def pre_build_jobspecs(self):
263 return []
264
265 def build_jobspec(self, inner_jobs=None):
266 del inner_jobs # arg unused as PHP artifact build is basically just packing an archive
267 if self.platform == 'linux':
268 return create_docker_jobspec(
269 self.name,
270 'tools/dockerfile/test/php73_zts_debian11_{}'.format(self.arch),
271 'tools/run_tests/artifacts/build_artifact_php.sh')
272 else:
273 return create_jobspec(
274 self.name, ['tools/run_tests/artifacts/build_artifact_php.sh'],
275 use_workspace=True)
276
277
278 class ProtocArtifact:
279 """Builds protoc and protoc-plugin artifacts"""
280
281 def __init__(self, platform, arch, presubmit=False):
282 self.name = 'protoc_%s_%s' % (platform, arch)
283 self.platform = platform
284 self.arch = arch
285 self.labels = ['artifact', 'protoc', platform, arch]
286 if presubmit:
287 self.labels.append('presubmit')
288
289 def pre_build_jobspecs(self):
290 return []
291
292 def build_jobspec(self, inner_jobs=None):
293 environ = {}
294 if inner_jobs is not None:
295 # set number of parallel jobs when building protoc
296 environ['GRPC_PROTOC_BUILD_COMPILER_JOBS'] = str(inner_jobs)
297
298 if self.platform != 'windows':
299 environ['CXXFLAGS'] = ''
300 environ['LDFLAGS'] = ''
301 if self.platform == 'linux':
302 dockerfile_dir = 'tools/dockerfile/grpc_artifact_centos6_{}'.format(
303 self.arch)
304 if self.arch == 'aarch64':
305 # for aarch64, use a dockcross manylinux image that will
306 # give us both ready to use crosscompiler and sufficient backward compatibility
307 dockerfile_dir = 'tools/dockerfile/grpc_artifact_protoc_aarch64'
308 environ['LDFLAGS'] += ' -static-libgcc -static-libstdc++ -s'
309 return create_docker_jobspec(
310 self.name,
311 dockerfile_dir,
312 'tools/run_tests/artifacts/build_artifact_protoc.sh',
313 environ=environ)
314 else:
315 environ[
316 'CXXFLAGS'] += ' -std=c++14 -stdlib=libc++ %s' % _MACOS_COMPAT_FLAG
317 return create_jobspec(
318 self.name,
319 ['tools/run_tests/artifacts/build_artifact_protoc.sh'],
320 environ=environ,
321 timeout_seconds=60 * 60,
322 use_workspace=True)
323 else:
324 vs_tools_architecture = self.arch # architecture selector passed to vcvarsall.bat
325 environ['ARCHITECTURE'] = vs_tools_architecture
326 return create_jobspec(
327 self.name,
328 ['tools\\run_tests\\artifacts\\build_artifact_protoc.bat'],
329 environ=environ,
330 use_workspace=True)
331
332 def __str__(self):
333 return self.name
334
335
336 def _reorder_targets_for_build_speed(targets):
337 """Reorder targets to achieve optimal build speed"""
338 # ruby artifact build builds multiple artifacts at once, so make sure
339 # we start building ruby artifacts first, so that they don't end up
340 # being a long tail once everything else finishes.
341 return list(
342 sorted(targets,
343 key=lambda target: 0 if target.name.startswith('ruby_') else 1))
344
345
346 def targets():
347 """Gets list of supported targets"""
348 return _reorder_targets_for_build_speed([
349 ProtocArtifact('linux', 'x64', presubmit=True),
350 ProtocArtifact('linux', 'x86', presubmit=True),
351 ProtocArtifact('linux', 'aarch64', presubmit=True),
352 ProtocArtifact('macos', 'x64', presubmit=True),
353 ProtocArtifact('windows', 'x64', presubmit=True),
354 ProtocArtifact('windows', 'x86', presubmit=True),
355 PythonArtifact('manylinux2014', 'x64', 'cp37-cp37m', presubmit=True),
356 PythonArtifact('manylinux2014', 'x64', 'cp38-cp38', presubmit=True),
357 PythonArtifact('manylinux2014', 'x64', 'cp39-cp39'),
358 PythonArtifact('manylinux2014', 'x64', 'cp310-cp310'),
359 PythonArtifact('manylinux2014', 'x64', 'cp311-cp311', presubmit=True),
360 PythonArtifact('manylinux2014', 'x86', 'cp37-cp37m', presubmit=True),
361 PythonArtifact('manylinux2014', 'x86', 'cp38-cp38', presubmit=True),
362 PythonArtifact('manylinux2014', 'x86', 'cp39-cp39'),
363 PythonArtifact('manylinux2014', 'x86', 'cp310-cp310'),
364 PythonArtifact('manylinux2014', 'x86', 'cp311-cp311', presubmit=True),
365 PythonArtifact('manylinux2014', 'aarch64', 'cp37-cp37m',
366 presubmit=True),
367 PythonArtifact('manylinux2014', 'aarch64', 'cp38-cp38', presubmit=True),
368 PythonArtifact('manylinux2014', 'aarch64', 'cp39-cp39'),
369 PythonArtifact('manylinux2014', 'aarch64', 'cp310-cp310'),
370 # TODO(https://github.com/grpc/grpc/issues/30927): Support aarch64 with 3.11. Blocked on dockcross support.
371 PythonArtifact('linux_extra', 'armv7', 'cp37-cp37m', presubmit=True),
372 PythonArtifact('linux_extra', 'armv7', 'cp38-cp38'),
373 PythonArtifact('linux_extra', 'armv7', 'cp39-cp39'),
374 PythonArtifact('linux_extra', 'armv7', 'cp310-cp310'),
375 PythonArtifact('linux_extra', 'armv7', 'cp311-cp311', presubmit=True),
376 PythonArtifact('musllinux_1_1', 'x64', 'cp310-cp310'),
377 PythonArtifact('musllinux_1_1', 'x64', 'cp311-cp311', presubmit=True),
378 PythonArtifact('musllinux_1_1', 'x64', 'cp37-cp37m', presubmit=True),
379 PythonArtifact('musllinux_1_1', 'x64', 'cp38-cp38'),
380 PythonArtifact('musllinux_1_1', 'x64', 'cp39-cp39'),
381 PythonArtifact('musllinux_1_1', 'x86', 'cp310-cp310'),
382 PythonArtifact('musllinux_1_1', 'x86', 'cp311-cp311', presubmit=True),
383 PythonArtifact('musllinux_1_1', 'x86', 'cp37-cp37m', presubmit=True),
384 PythonArtifact('musllinux_1_1', 'x86', 'cp38-cp38'),
385 PythonArtifact('musllinux_1_1', 'x86', 'cp39-cp39'),
386 PythonArtifact('macos', 'x64', 'python3.7', presubmit=True),
387 PythonArtifact('macos', 'x64', 'python3.8'),
388 PythonArtifact('macos', 'x64', 'python3.9'),
389 PythonArtifact('macos', 'x64', 'python3.10', presubmit=True),
390 PythonArtifact('macos', 'x64', 'python3.11', presubmit=True),
391 PythonArtifact('windows', 'x86', 'Python37_32bit', presubmit=True),
392 PythonArtifact('windows', 'x86', 'Python38_32bit'),
393 PythonArtifact('windows', 'x86', 'Python39_32bit'),
394 PythonArtifact('windows', 'x86', 'Python310_32bit'),
395 PythonArtifact('windows', 'x86', 'Python311_32bit', presubmit=True),
396 PythonArtifact('windows', 'x64', 'Python37', presubmit=True),
397 PythonArtifact('windows', 'x64', 'Python38'),
398 PythonArtifact('windows', 'x64', 'Python39'),
399 PythonArtifact('windows', 'x64', 'Python310'),
400 PythonArtifact('windows', 'x64', 'Python311', presubmit=True),
401 RubyArtifact('linux', 'x86-mingw32', presubmit=True),
402 RubyArtifact('linux', 'x64-mingw32', presubmit=True),
403 RubyArtifact('linux', 'x64-mingw-ucrt', presubmit=True),
404 RubyArtifact('linux', 'x86_64-linux', presubmit=True),
405 RubyArtifact('linux', 'x86-linux', presubmit=True),
406 RubyArtifact('linux', 'x86_64-darwin', presubmit=True),
407 RubyArtifact('linux', 'arm64-darwin', presubmit=True),
408 PHPArtifact('linux', 'x64', presubmit=True),
409 PHPArtifact('macos', 'x64', presubmit=True),
410 ])