Search code examples
pythoncclangcpython

Can CPython be compiled with Clang?


I'm trying to build CPython using Clang, with very specific requirements:

I tried setting env variable CC to my clang executable location (i.e. /opt/llvm/5/bin/clang, but the ./configure command fails with the following:
configure: error: C compiler cannot create executables

What flags should I set to make this build work?


Solution

  • As the commenters on OP's post pointed out, you can compile cpython with clang. Here are reproducible instructions, in the form of a Dockerfile.

    FROM ubuntu:16.04
    WORKDIR /opt/cpython-2.7.14
    RUN apt-get update -qq \
        && apt-get install --yes build-essential curl \
        # Install clang
        && curl -fsSL https://releases.llvm.org/5.0.0/clang+llvm-5.0.0-linux-x86_64-ubuntu16.04.tar.xz \
        | tar xJ -C /usr/local --strip-components 1 \
        && curl -fsSL https://www.python.org/ftp/python/2.7.14/Python-2.7.14.tgz \
        | tar xz --strip-components 1 \
        && CC=/usr/local/bin/clang ./configure --prefix /usr/local/cpython-2.7.14 \
        && make \
        && make install
    ENTRYPOINT ["/usr/local/cpython-2.7.14/bin/python"]
    
    docker build --tag cpython:2.7 .
    docker run --rm cpython:2.7 --version
    # Python 2.7.14
    

    It is difficult to say what OP's original issue was, because it seemed like a problem with the clang installation. Looking at the configure logs would provide more information.

    Disclaimer: Python 2 has reached its end of life, and ubuntu 16.04 reaches its end of life in April 2021.