WRAPPER_C is a compiler wrapper. It is another Clang’s JSON compilation database generator for make-based build systems.

Unlike Bear which uses LD_PRELOAD, wrapper_c exploits the implicit variables CC and CXX to intercept the compilation commands from make and forwards the commands to the compiler.

In this post, I will show how to use wrapper_c to generate compile_commands.json for PostgreSQL.

I use docker to set the environment up. The followings are steps.

  1. Create a file named Dockerfile and edit it using the following content.

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    
    FROM ubuntu:22.04
    
    RUN apt-get update && apt-get --yes upgrade
    
    RUN apt-get install --yes build-essential python-is-python3 git python3-pip libreadline-dev bison flex
    
    RUN pip install meson ninja
    
    # Install wrapper_c.
    RUN git clone https://github.com/adonis0147/wrapper_c && \
        cd /wrapper_c && \
        git submodule update --init --progress && \
        meson build --buildtype=release -Dprefix=/opt/wrapper_c && \
        ninja install -C build
    
    CMD ["/bin/bash"]
    
  2. Build the docker image.

    1
    
    docker build --platform=linux/x86_64 -t wrapper_c .
    
  3. Run the image.

    1
    2
    
    mkdir -p data
    docker run -it --mount type=bind,source="$(pwd)/data",target=/data wrapper_c bash
    
  4. Run the following commands to finish the preparation.

    1
    2
    3
    4
    5
    6
    
    # Download the source code of PostgreSQL.
    cd /data
    git clone https://github.com/postgres/postgres
    cd postgres
    mkdir build
    cd build
    
  5. The usage of wrapper_c is simple. Just run the following commands and compile_commands.json will be generated.

    1
    2
    3
    4
    5
    
    CC=/opt/wrapper_c/bin/wrapper_gcc \
    CXX=/opt/wrapper_c/bin/wrapper_g++ \
    ../configure --prefix="$(pwd)/output" --enable-debug --enable-cassert
    
    /opt/wrapper_c/bin/wrapper_make -j "$(nproc)"