Skip to content

Fail to parse glibc math headers when building Python C extensions #188

@arrufat

Description

@arrufat

Hi! I am currently developing Python bindings for my image processing library, zignal: https://github.com/bfactory-ai/zignal

Everything was working fine until llvm was replaced by arocc.
Building Python C extensions with @cImport fails because arocc cannot parse glibc's bits/mathcalls.h. The issue occurs when Python.h transitively includes <math.h>, which uses complex macro patterns.

In zignal, just typing zig build python-bindings triggers the bug, but I've prepared a minimal reproduction below with detailed steps and outputs.

Environment:

  • Zig version: 0.16.0-dev.457+f90510b08
  • OS: Arch Linux (kernel 6.16.8)
  • glibc: 2.42
  • Python: 3.13

Minimal Reproduction:

main.zig:

const std = @import("std");

const c = @cImport({
    @cDefine("PY_SSIZE_T_CLEAN", {});
    @cInclude("Python.h");
});

pub fn main() void {
    const version = c.Py_GetVersion();
    std.debug.print("Python version: {*}\n", .{version});
}

build.zig:

const std = @import("std");

pub fn build(b: *std.Build) void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});

const lib = b.addLibrary(.{
    .name = "test",
    .linkage = .dynamic,
    .root_module = b.createModule(.{
        .root_source_file = b.path("main.zig"),
        .target = target,
        .optimize = optimize,
        }),
    });

    lib.root_module.link_libc = true;
    lib.linkSystemLibrary("python3");
    b.installArtifact(lib);
}

Build command:

zig build

Error output:

error: /usr/include/bits/mathcalls.h:53:1: error: expected ';', found 'an identifier'
__MATHCALL_VEC (acos,, (_Mdouble_ __x));
^
/usr/include/bits/mathcalls-macros.h:22:54: note: expanded from here
__SIMD_DECL (__MATH_PRECNAME (function, suffix)) \
^
[... continues with similar errors for sin, cos, tan, etc.]

Root Cause:

The __MATHCALL_VEC macro in glibc expands through multiple levels:
__MATHCALL_VEC(acos,, (_Mdouble_ __x))__SIMD_DECL(__MATH_PRECNAME(acos, ))__SIMD_DECL(acosf32)

arocc expands __MATH_PRECNAME(acos, ) to acosf32 but then fails to parse the resulting code.

What I've tried:

Preprocessor workarounds don't help (at leats I couldn't make it work):

  • @cDefine("__SIMD_DECL(x)", ""): arocc still expands the arguments before discarding
  • @cDefine("__MATHCALL_VEC", "__MATHCALL"): arocc has similar issues with __MATHCALL's macro expansion

Let me know if you need anything else.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions