1*60517a1eSAndroid Build Coastguard Worker# Copyright 2024 The Bazel Authors. All rights reserved. 2*60517a1eSAndroid Build Coastguard Worker# 3*60517a1eSAndroid Build Coastguard Worker# Licensed under the Apache License, Version 2.0 (the "License"); 4*60517a1eSAndroid Build Coastguard Worker# you may not use this file except in compliance with the License. 5*60517a1eSAndroid Build Coastguard Worker# You may obtain a copy of the License at 6*60517a1eSAndroid Build Coastguard Worker# 7*60517a1eSAndroid Build Coastguard Worker# http://www.apache.org/licenses/LICENSE-2.0 8*60517a1eSAndroid Build Coastguard Worker# 9*60517a1eSAndroid Build Coastguard Worker# Unless required by applicable law or agreed to in writing, software 10*60517a1eSAndroid Build Coastguard Worker# distributed under the License is distributed on an "AS IS" BASIS, 11*60517a1eSAndroid Build Coastguard Worker# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12*60517a1eSAndroid Build Coastguard Worker# See the License for the specific language governing permissions and 13*60517a1eSAndroid Build Coastguard Worker# limitations under the License. 14*60517a1eSAndroid Build Coastguard Worker 15*60517a1eSAndroid Build Coastguard Worker""" 16*60517a1eSAndroid Build Coastguard WorkerEXPERIMENTAL: This is experimental and may be removed without notice 17*60517a1eSAndroid Build Coastguard Worker 18*60517a1eSAndroid Build Coastguard WorkerCreate repositories for uv toolchain dependencies 19*60517a1eSAndroid Build Coastguard Worker""" 20*60517a1eSAndroid Build Coastguard Worker 21*60517a1eSAndroid Build Coastguard Workerload("//python/uv/private:toolchain_types.bzl", "UV_TOOLCHAIN_TYPE") 22*60517a1eSAndroid Build Coastguard Workerload("//python/uv/private:toolchains_repo.bzl", "uv_toolchains_repo") 23*60517a1eSAndroid Build Coastguard Workerload("//python/uv/private:versions.bzl", "UV_PLATFORMS", "UV_TOOL_VERSIONS") 24*60517a1eSAndroid Build Coastguard Worker 25*60517a1eSAndroid Build Coastguard WorkerUV_BUILD_TMPL = """\ 26*60517a1eSAndroid Build Coastguard Worker# Generated by repositories.bzl 27*60517a1eSAndroid Build Coastguard Workerload("@rules_python//python/uv:toolchain.bzl", "uv_toolchain") 28*60517a1eSAndroid Build Coastguard Worker 29*60517a1eSAndroid Build Coastguard Workeruv_toolchain( 30*60517a1eSAndroid Build Coastguard Worker name = "uv_toolchain", 31*60517a1eSAndroid Build Coastguard Worker uv = "{binary}", 32*60517a1eSAndroid Build Coastguard Worker version = "{version}", 33*60517a1eSAndroid Build Coastguard Worker) 34*60517a1eSAndroid Build Coastguard Worker""" 35*60517a1eSAndroid Build Coastguard Worker 36*60517a1eSAndroid Build Coastguard Workerdef _uv_repo_impl(repository_ctx): 37*60517a1eSAndroid Build Coastguard Worker platform = repository_ctx.attr.platform 38*60517a1eSAndroid Build Coastguard Worker uv_version = repository_ctx.attr.uv_version 39*60517a1eSAndroid Build Coastguard Worker 40*60517a1eSAndroid Build Coastguard Worker is_windows = "windows" in platform 41*60517a1eSAndroid Build Coastguard Worker 42*60517a1eSAndroid Build Coastguard Worker suffix = ".zip" if is_windows else ".tar.gz" 43*60517a1eSAndroid Build Coastguard Worker filename = "uv-{platform}{suffix}".format( 44*60517a1eSAndroid Build Coastguard Worker platform = platform, 45*60517a1eSAndroid Build Coastguard Worker suffix = suffix, 46*60517a1eSAndroid Build Coastguard Worker ) 47*60517a1eSAndroid Build Coastguard Worker url = "https://github.com/astral-sh/uv/releases/download/{version}/{filename}".format( 48*60517a1eSAndroid Build Coastguard Worker version = uv_version, 49*60517a1eSAndroid Build Coastguard Worker filename = filename, 50*60517a1eSAndroid Build Coastguard Worker ) 51*60517a1eSAndroid Build Coastguard Worker if filename.endswith(".tar.gz"): 52*60517a1eSAndroid Build Coastguard Worker strip_prefix = filename[:-len(".tar.gz")] 53*60517a1eSAndroid Build Coastguard Worker else: 54*60517a1eSAndroid Build Coastguard Worker strip_prefix = "" 55*60517a1eSAndroid Build Coastguard Worker 56*60517a1eSAndroid Build Coastguard Worker repository_ctx.download_and_extract( 57*60517a1eSAndroid Build Coastguard Worker url = url, 58*60517a1eSAndroid Build Coastguard Worker sha256 = UV_TOOL_VERSIONS[repository_ctx.attr.uv_version][repository_ctx.attr.platform].sha256, 59*60517a1eSAndroid Build Coastguard Worker stripPrefix = strip_prefix, 60*60517a1eSAndroid Build Coastguard Worker ) 61*60517a1eSAndroid Build Coastguard Worker 62*60517a1eSAndroid Build Coastguard Worker binary = "uv.exe" if is_windows else "uv" 63*60517a1eSAndroid Build Coastguard Worker repository_ctx.file( 64*60517a1eSAndroid Build Coastguard Worker "BUILD.bazel", 65*60517a1eSAndroid Build Coastguard Worker UV_BUILD_TMPL.format( 66*60517a1eSAndroid Build Coastguard Worker binary = binary, 67*60517a1eSAndroid Build Coastguard Worker version = uv_version, 68*60517a1eSAndroid Build Coastguard Worker ), 69*60517a1eSAndroid Build Coastguard Worker ) 70*60517a1eSAndroid Build Coastguard Worker 71*60517a1eSAndroid Build Coastguard Workeruv_repository = repository_rule( 72*60517a1eSAndroid Build Coastguard Worker _uv_repo_impl, 73*60517a1eSAndroid Build Coastguard Worker doc = "Fetch external tools needed for uv toolchain", 74*60517a1eSAndroid Build Coastguard Worker attrs = { 75*60517a1eSAndroid Build Coastguard Worker "platform": attr.string(mandatory = True, values = UV_PLATFORMS.keys()), 76*60517a1eSAndroid Build Coastguard Worker "uv_version": attr.string(mandatory = True, values = UV_TOOL_VERSIONS.keys()), 77*60517a1eSAndroid Build Coastguard Worker }, 78*60517a1eSAndroid Build Coastguard Worker) 79*60517a1eSAndroid Build Coastguard Worker 80*60517a1eSAndroid Build Coastguard Worker# buildifier: disable=unnamed-macro 81*60517a1eSAndroid Build Coastguard Workerdef uv_register_toolchains(uv_version = None, register_toolchains = True): 82*60517a1eSAndroid Build Coastguard Worker """Convenience macro which does typical toolchain setup 83*60517a1eSAndroid Build Coastguard Worker 84*60517a1eSAndroid Build Coastguard Worker Skip this macro if you need more control over the toolchain setup. 85*60517a1eSAndroid Build Coastguard Worker 86*60517a1eSAndroid Build Coastguard Worker Args: 87*60517a1eSAndroid Build Coastguard Worker uv_version: The uv toolchain version to download. 88*60517a1eSAndroid Build Coastguard Worker register_toolchains: If true, repositories will be generated to produce and register `uv_toolchain` targets. 89*60517a1eSAndroid Build Coastguard Worker """ 90*60517a1eSAndroid Build Coastguard Worker if not uv_version: 91*60517a1eSAndroid Build Coastguard Worker fail("uv_version is required") 92*60517a1eSAndroid Build Coastguard Worker 93*60517a1eSAndroid Build Coastguard Worker toolchain_names = [] 94*60517a1eSAndroid Build Coastguard Worker toolchain_labels_by_toolchain = {} 95*60517a1eSAndroid Build Coastguard Worker toolchain_compatible_with_by_toolchain = {} 96*60517a1eSAndroid Build Coastguard Worker 97*60517a1eSAndroid Build Coastguard Worker for platform in UV_PLATFORMS.keys(): 98*60517a1eSAndroid Build Coastguard Worker uv_repository_name = UV_PLATFORMS[platform].default_repo_name 99*60517a1eSAndroid Build Coastguard Worker 100*60517a1eSAndroid Build Coastguard Worker uv_repository( 101*60517a1eSAndroid Build Coastguard Worker name = uv_repository_name, 102*60517a1eSAndroid Build Coastguard Worker uv_version = uv_version, 103*60517a1eSAndroid Build Coastguard Worker platform = platform, 104*60517a1eSAndroid Build Coastguard Worker ) 105*60517a1eSAndroid Build Coastguard Worker 106*60517a1eSAndroid Build Coastguard Worker toolchain_name = uv_repository_name + "_toolchain" 107*60517a1eSAndroid Build Coastguard Worker toolchain_names.append(toolchain_name) 108*60517a1eSAndroid Build Coastguard Worker toolchain_labels_by_toolchain[toolchain_name] = "@{}//:uv_toolchain".format(uv_repository_name) 109*60517a1eSAndroid Build Coastguard Worker toolchain_compatible_with_by_toolchain[toolchain_name] = UV_PLATFORMS[platform].compatible_with 110*60517a1eSAndroid Build Coastguard Worker 111*60517a1eSAndroid Build Coastguard Worker uv_toolchains_repo( 112*60517a1eSAndroid Build Coastguard Worker name = "uv_toolchains", 113*60517a1eSAndroid Build Coastguard Worker toolchain_type = str(UV_TOOLCHAIN_TYPE), 114*60517a1eSAndroid Build Coastguard Worker toolchain_names = toolchain_names, 115*60517a1eSAndroid Build Coastguard Worker toolchain_labels = toolchain_labels_by_toolchain, 116*60517a1eSAndroid Build Coastguard Worker toolchain_compatible_with = toolchain_compatible_with_by_toolchain, 117*60517a1eSAndroid Build Coastguard Worker ) 118*60517a1eSAndroid Build Coastguard Worker 119*60517a1eSAndroid Build Coastguard Worker if register_toolchains: 120*60517a1eSAndroid Build Coastguard Worker native.register_toolchains("@uv_toolchains//:all") 121