Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions NOTICE
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,11 @@ licensed under the Apache License, Version 2.0.
The distribution/ package is ported from rstudio/python-distribution-parser
(https://github.com/rstudio/python-distribution-parser), Copyright (c) 2024
Posit Software, PBC, originally MIT-licensed.

The tags/ package adapts platform-compatibility-tag algorithms (manylinux and
musllinux glibc/musl floor tables, macOS format lists, tag ordering) from
pypa/packaging (https://github.com/pypa/packaging), specifically
packaging/tags.py, packaging/_manylinux.py, and packaging/_musllinux.py,
licensed under the Apache License, Version 2.0; and from Astral's uv
(https://github.com/astral-sh/uv), specifically the uv-platform-tags crate,
dual-licensed under the Apache License, Version 2.0, or the MIT license.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Part of [RFD 0001 — Native Go PyPI Dependency Resolution](https://github.com/r
| `reqtxt/` | pip `requirements.txt` format |
| [`distribution/`](https://github.com/rstudio/python-distribution-parser) | METADATA / wheel parsing |
| `wheelname/` | wheel filename → structured fields |
| `tags/` | PEP 425/600/656 compatibility tags |
| `tags/` | PEP 425/600/656 compatibility tags (server-side, target-parameterized; no host detection) |

## License

Expand Down
9 changes: 8 additions & 1 deletion tags/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,12 @@
// Package tags computes and matches platform-compatibility tags
// (PEP 425 / 600 / 656).
//
// TODO(#18632): implement compatibility-tag support.
// A Target describes a Python interpreter/platform combination and Compiles
// into a Matcher holding the ordered set of compatible tags for that target,
// plus a priority index for ranking candidate wheels. Generation is entirely
// server-side and parameterized by the given Target — there is no host
// detection. Notable divergences from CPython's packaging library: a bare
// linux_<arch> tag is accepted but ranked last (below all manylinux and
// musllinux tags), and macOS targets are supported for version 11 and later
// only.
package tags
296 changes: 296 additions & 0 deletions tags/generate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,296 @@
// SPDX-License-Identifier: Apache-2.0 OR MIT
package tags

import (
"fmt"
"strconv"
)

// generateTags builds the full ordered, most-preferred-first list of
// compatible tags for a (validated) Target. The ordering follows the
// pypa/packaging convention (packaging.tags.cpython_tags /
// packaging.tags.compatible_tags), fixed to a specific declared target
// rather than the running interpreter/host:
//
// - "cp" targets: cp<XY>-cp<XY>-<plat> (exact ABI), then cp<XY>-abi3-<plat>,
// then cp<XY>-none-<plat>, then the abi3 walk down through older minor
// versions to cp32-abi3-<plat>; followed by the "compatible tier"
// py<XY>-none-<plat> down to py<X>0-none-<plat>; then cp<XY>-none-any;
// then the universal py*-none-any tail.
// - "py" targets: the same compatible tier and universal tail, without any
// interpreter-specific entries (there is no compiled ABI to bind to).
func generateTags(t Target) ([]Tag, error) {
if t.Implementation == "cp" {
return cpTags(t)
}
return pyTags(t)
}

func cpTags(t Target) ([]Tag, error) {
plats, err := t.platformTags()
if err != nil {
return nil, err
}
interp := interpTag("cp", t.PyMajor, t.PyMinor)

var out []Tag
appendPlatform := func(abi string) {
for _, p := range plats {
out = append(out, Tag{interp, abi, p})
}
}

// Exact ABI.
appendPlatform(interp)

// abi3 was introduced in Python 3.2; free-threaded ABIs are out of scope
// for now (see deferred-features follow-up).
useAbi3 := t.PyMajor == 3 && t.PyMinor >= 2
if useAbi3 {
appendPlatform("abi3")
}

appendPlatform("none")

if useAbi3 {
for minor := t.PyMinor - 1; minor >= 2; minor-- {
oldInterp := interpTag("cp", t.PyMajor, minor)
for _, p := range plats {
out = append(out, Tag{oldInterp, "abi3", p})
}
}
}

pyRange := pyInterpreterRange(t.PyMajor, t.PyMinor)
for _, v := range pyRange {
for _, p := range plats {
out = append(out, Tag{v, "none", p})
}
}

out = append(out, Tag{interp, "none", "any"})

for _, v := range pyRange {
out = append(out, Tag{v, "none", "any"})
}

return out, nil
}

func pyTags(t Target) ([]Tag, error) {
plats, err := t.platformTags()
if err != nil {
return nil, err
}
pyRange := pyInterpreterRange(t.PyMajor, t.PyMinor)

var out []Tag
for _, v := range pyRange {
for _, p := range plats {
out = append(out, Tag{v, "none", p})
}
}
for _, v := range pyRange {
out = append(out, Tag{v, "none", "any"})
}
return out, nil
}

// pyInterpreterRange mirrors packaging._py_interpreter_range: the exact
// "py<major><minor>" version, then the major-only "py<major>" version, then
// every older minor version down to "py<major>0", descending.
func pyInterpreterRange(major, minor int) []string {
out := make([]string, 0, minor+2)
out = append(out, interpTag("py", major, minor))
out = append(out, "py"+strconv.Itoa(major))
for m := minor - 1; m >= 0; m-- {
out = append(out, interpTag("py", major, m))
}
return out
}

func interpTag(prefix string, major, minor int) string {
return prefix + strconv.Itoa(major) + strconv.Itoa(minor)
}

// platformTags returns the target's platform component strings (there is
// usually exactly one, but the shape stays a slice for symmetry with the
// underlying pypa/packaging generators, some of which enumerate more than
// one platform tag per target, e.g. macOS's per-major-version format
// fallbacks).
func (t Target) platformTags() ([]string, error) {
switch t.OS {
case "windows":
return windowsPlatformTags(t.Arch), nil
case "linux":
return linuxPlatformTags(t), nil
case "macos":
return macosPlatformTags(t.Arch, t.MacMajor), nil
default:
// Unreachable: Target.validate rejects any other OS before
// generateTags is ever called.
panic("tags: unsupported OS " + t.OS)
}
}

func windowsPlatformTags(arch string) []string {
switch arch {
case "amd64":
return []string{"win_amd64"}
case "x86":
return []string{"win32"}
case "arm64":
return []string{"win_arm64"}
default:
// Unreachable: Target.validate rejects any other windows arch.
panic("tags: unsupported windows arch " + arch)
}
}

// legacyManylinuxAlias is a pre-PEP 600 manylinux tag ("manylinux1",
// "manylinux2010", "manylinux2014") that aliases one specific glibc version
// for a given architecture.
type legacyManylinuxAlias struct {
name string
major int
minor int
}

// manylinuxFloor is the oldest glibc version (major, minor) a given
// architecture's manylinux tags may claim, plus any legacy aliases that
// architecture supports. Values are uv's (github.com/astral-sh/uv) floor
// table, which matches pypa/packaging for x86_64/i686/aarch64/armv7l/
// ppc64/ppc64le/s390x but additionally floors riscv64 and loongarch64 --
// architectures packaging's own manylinux support predates.
//
// Rule (Global Constraints): a target declaring glibc (2, m) accepts a
// manylinux_2_y tag iff m >= y, down to this floor.
var manylinuxFloor = map[string]struct {
major, minor int
legacy []legacyManylinuxAlias
}{
"x86_64": {2, 5, []legacyManylinuxAlias{
{"manylinux1", 2, 5}, {"manylinux2010", 2, 12}, {"manylinux2014", 2, 17},
}},
"i686": {2, 5, []legacyManylinuxAlias{
{"manylinux1", 2, 5}, {"manylinux2010", 2, 12}, {"manylinux2014", 2, 17},
}},
"aarch64": {2, 17, []legacyManylinuxAlias{{"manylinux2014", 2, 17}}},
"armv7l": {2, 17, []legacyManylinuxAlias{{"manylinux2014", 2, 17}}},
"ppc64": {2, 17, []legacyManylinuxAlias{{"manylinux2014", 2, 17}}},
"ppc64le": {2, 17, []legacyManylinuxAlias{{"manylinux2014", 2, 17}}},
"s390x": {2, 17, []legacyManylinuxAlias{{"manylinux2014", 2, 17}}},
"riscv64": {2, 31, nil},
// loongarch64's floor is uv's; pypa/packaging does not yet recognize
// this architecture at all.
"loongarch64": {2, 36, nil},
}

// linuxPlatformTags builds the ordered platform-tag list for a linux
// Target: manylinux tags (glibc targets) or musllinux tags (musl targets)
// from newest to oldest down to the architecture's floor, each accompanied
// by any applicable legacy manylinux alias immediately after the version it
// aliases, followed last by the bare "linux_<arch>" tag -- a deliberate
// divergence from pypa/packaging (which does not emit bare linux_* tags at
// all) to support PPM's local-source/Git-builder wheels, ranked below every
// manylinux/musllinux tag since those carry a documented compatibility
// guarantee that a bare linux tag does not.
func linuxPlatformTags(t Target) []string {
var plats []string
switch t.Libc {
case "glibc":
plats = manylinuxTags(t.Arch, t.LibcMajor, t.LibcMinor)
case "musl":
plats = musllinuxTags(t.Arch, t.LibcMajor, t.LibcMinor)
default:
// Unreachable: Target.validate requires Libc to be "glibc" or
// "musl".
panic("tags: unsupported linux libc " + t.Libc)
}
return append(plats, "linux_"+t.Arch)
}

// manylinuxTags returns "manylinux_<major>_<minor>_<arch>" for every glibc
// version from the target's declared version down to the architecture's
// floor (inclusive), newest first, interleaving legacy aliases immediately
// after the version they alias. Only floors within the target's declared
// glibc major version are considered (per the Global Constraints rule,
// stated only for glibc major 2, which every currently defined floor uses);
// a target whose major doesn't match its floor's, or whose declared version
// is below the floor, yields no manylinux tags.
func manylinuxTags(arch string, major, minor int) []string {
floor, ok := manylinuxFloor[arch]
if !ok {
// Unreachable: Target.validate restricts linux Arch to linuxArchs,
// and every entry there has a manylinuxFloor.
panic("tags: no manylinux floor for arch " + arch)
}
if major != floor.major || minor < floor.minor {
return nil
}
out := make([]string, 0, (minor-floor.minor+1)+len(floor.legacy))
for m := minor; m >= floor.minor; m-- {
out = append(out, fmt.Sprintf("manylinux_%d_%d_%s", major, m, arch))
for _, alias := range floor.legacy {
if alias.major == major && alias.minor == m {
out = append(out, alias.name+"_"+arch)
}
}
}
return out
}

// musllinuxMajor is the only musl ABI major version musllinux tags are
// currently defined for (PEP 656); unlike manylinux/glibc, musl has not yet
// had an ABI-breaking major-version bump requiring a musllinux_2_* series.
const musllinuxMajor = 1

// musllinuxTags returns "musllinux_<major>_<minor>_<arch>" for every musl
// version from the target's declared version down to musllinux_<major>_0,
// newest first. Unlike manylinux there is no architecture-dependent floor or
// legacy alias. A target whose major doesn't match musllinuxMajor yields no
// musllinux tags, mirroring manylinuxTags' major-version guard.
func musllinuxTags(arch string, major, minor int) []string {
if major != musllinuxMajor {
return nil
}
out := make([]string, 0, minor+1)
for m := minor; m >= 0; m-- {
out = append(out, fmt.Sprintf("musllinux_%d_%d_%s", major, m, arch))
}
return out
}

// macosBinaryFormats is the ordered list of binary-format suffixes a macOS
// architecture's tags may claim (per pypa/packaging's _mac_binary_formats,
// restricted to the archs this package supports). "intel"/"fat64"/"fat32"
// are legacy 32/64-bit-Intel umbrella formats that only ever applied to
// x86_64 (and, pre-Intel-64, i386/ppc, which are out of scope here);
// "universal"/"universal2" are fat binaries spanning multiple architectures.
var macosBinaryFormats = map[string][]string{
"x86_64": {"x86_64", "intel", "fat64", "fat32", "universal2", "universal"},
"arm64": {"arm64", "universal2"},
}

// macosPlatformTags returns the ordered platform-tag list for a macOS
// Target: "macosx_<M>_0_<fmt>" for every major version M from the target's
// declared major version down to 11 (inclusive), newest first, each paired
// with every binary format the architecture supports. Only macOS 11+ is
// supported (Global Constraints: "macOS: 11+ only" -- pre-11's yearly
// major-version-10 minor-bump numbering, and pypa/packaging's compatibility
// tail of pre-11 "macosx_10_<n>_universal2" fallbacks, are both deferred).
func macosPlatformTags(arch string, major int) []string {
formats, ok := macosBinaryFormats[arch]
if !ok {
// Unreachable: Target.validate restricts macOS Arch to macosArchs,
// and every entry there has a macosBinaryFormats list.
panic("tags: no binary formats for macOS arch " + arch)
}
out := make([]string, 0, (major-11+1)*len(formats))
for m := major; m >= 11; m-- {
for _, f := range formats {
out = append(out, fmt.Sprintf("macosx_%d_0_%s", m, f))
}
}
return out
}
65 changes: 65 additions & 0 deletions tags/golden_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// SPDX-License-Identifier: Apache-2.0 OR MIT
package tags

import (
"encoding/json"
"os"
"path/filepath"
"testing"

"github.com/stretchr/testify/require"
)

// goldenTargets maps a testdata/*.json fixture base name to the Target that
// produced it. Fixtures are generated by tags/testdata/gen_goldens.py from
// the reference Python "packaging" library; see that script for exact
// generation parameters.
var goldenTargets = map[string]Target{
"windows-amd64.json": {Implementation: "cp", PyMajor: 3, PyMinor: 11, OS: "windows", Arch: "amd64"},
"generic-py3.json": {Implementation: "py", PyMajor: 3, PyMinor: 11, OS: "windows", Arch: "amd64"},
"cp311_glibc228_x86_64.json": {
Implementation: "cp", PyMajor: 3, PyMinor: 11, OS: "linux", Arch: "x86_64",
Libc: "glibc", LibcMajor: 2, LibcMinor: 28,
},
"cp39_glibc217_aarch64.json": {
Implementation: "cp", PyMajor: 3, PyMinor: 9, OS: "linux", Arch: "aarch64",
Libc: "glibc", LibcMajor: 2, LibcMinor: 17,
},
"cp312_musl12_x86_64.json": {
Implementation: "cp", PyMajor: 3, PyMinor: 12, OS: "linux", Arch: "x86_64",
Libc: "musl", LibcMajor: 1, LibcMinor: 2,
},
"cp310_macos12_arm64.json": {
Implementation: "cp", PyMajor: 3, PyMinor: 10, OS: "macos", Arch: "arm64",
MacMajor: 12, MacMinor: 0,
},
"cp312_macos14_x86_64.json": {
Implementation: "cp", PyMajor: 3, PyMinor: 12, OS: "macos", Arch: "x86_64",
MacMajor: 14, MacMinor: 0,
},
}

// TestGolden asserts that Target.Compile().Tags(), stringified in order,
// exactly matches the ordered tag list recorded by gen_goldens.py.
func TestGolden(t *testing.T) {
files, _ := filepath.Glob("testdata/*.json")
require.NotEmpty(t, files)
for _, f := range files {
name := filepath.Base(f)
t.Run(name, func(t *testing.T) {
var want []string
b, err := os.ReadFile(f)
require.NoError(t, err)
require.NoError(t, json.Unmarshal(b, &want))
tg, ok := goldenTargets[name]
require.True(t, ok, "no Target mapping for %s", name)
m, err := tg.Compile()
require.NoError(t, err)
var got []string
for _, tag := range m.Tags() {
got = append(got, tag.String())
}
require.Equal(t, want, got)
})
}
}
Loading