Skip to content

[Feature Request] Zig as cross-platform UnixCompiler #369

@K0lb3

Description

@K0lb3

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions