pypa / pypa/distutils

[Feature Request] Zig as cross-platform UnixCompiler

Open
#369 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
59
Forks
94
PR merge metrics
No merged PRs in 30d

Description

Handling cross-platform native extensions can be hard, especially when using modern C++ with custom compiler flags.
While one can expect Linux and Mac users to at least have gcc or clang available, this isn't the case for Windows users.
The most commonly used compiler on Windows is msvc, which is quite bulky in terms of installation size due to the dependencies.
While the alternative compilers for Windows don't have the size issue, they are non-trivial in installing and configuring for the average user.

Zig solves the named issues, is easy to install, is small, and accepts the same general flags on all platforms.
Although Zig is its own programming language, its toolset allows for compiling C and C++ projects.

Therefore, it would be handy if distutils support and detect Zig as a compiler.

Implementing this change would be relatively straightforward, requiring only an adjustment to the executables in the unix.Compiler.

class ZigCompiler(Compiler):
    src_extensions = [
        *Compiler.src_extensions,
        ".zig",
    ]
    language_map = {
        **Compiler.language_map,
        ".zig": "c",
    }
    zig_cmd = ["python", "-m", "ziglang"] # if the ziglang package is installed, just ["zig"] would work too
    executables = {
        "preprocessor": None,
        "compiler": [*zig_cmd, "cc", "-O"],
        "compiler_so": [*zig_cmd, "cc", "-O"],
        "compiler_cxx": [*zig_cmd, "c++", "-O"],
        "compiler_so_cxx": [*zig_cmd, "c++", "-O"],
        "linker_so": [*zig_cmd, "cc", "-shared"],
        "linker_so_cxx": [*zig_cmd, "c++", "-shared"],
        "linker_exe": [*zig_cmd, "cc"],
        "linker_exe_cxx": [*zig_cmd, "c++"],
        "archiver": [*zig_cmd, "ar", "-cr"],
        "ranlib": [*zig_cmd, "ranlib"],
    }

The code above is from a small test project st_zig which modifies setuptools, specifically distutils, to utilize Zig with the compiler class defined above. The same outcome can also be achieved using sysconfigs. Detecting whether Zig is available is also straightforward; it only requires checking for the presence of the zig command.

In case you're interested, I would like to open a PR based on this.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the distutils unix.Compiler class and its executables configuration; the issue does not name a specific file or test. Trace how compilers are detected and selected, then add Zig support for the listed compilation, linking, archiving, and ranlib commands. Done means Zig can be detected and used to build native extensions across platforms, with coverage for the supported behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
python, zig
Domain
build-system
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.