Skip to content

wisonye/cbuild

Repository files navigation

cbuild

1. What is it and what it can offer to you

Inspired by tsoding's nobuild and has been improved.

Easy to understand and use, with no extra configuration syntax (or language) you need to learn, only C, the one you're super familiar with:)

Here is what cbuild can offer to you:

  • Only C compiler needed for building your C project.

    You don't need to install the extra tools like make, cmake or anything you named it. Only C compiler needed.

    If you love C, then just write your own building process by using your favorite programming language, it's kind of fun and cool:)

    If you want further simplicity, just download zig cc (single executable) and use it as your c compiler with zero installation and configuration for your c compiling environment.

  • Improved color logging.

  • Support user provided env var to override the default settings.

  • Single cbuild.h to cover all you need, nothing else.


2. Limitation

Only work for Linux or BSD family at this moment.


3. Usage by examples

3.1 Basic

Source code in examples/basic


  • Create cbuild.c and include cbuild.h and write your own build process

    #define C_BUILD_IMPLEMENTATION
    #include "../../cbuild.h"
    
    int main(int argc, char **argv) {
        CB_setup_build_folder();
        CB_setup_compiler();
    
        // CB_compile_all("test.c", NULL);
        CB_compile_and_build_executable("test.c", NULL);
    }

  • Compile cbuild.c and then use it to compile your project:

    cc -o cbuild cbuild.c && ./cbuild && build/main_test

    basic_build


3.2 Use configurable env var for the compiled cbuild binary

Here are the default env values if you don't provide them:

Env var Default value
C_COMPILER cc
RELEASE_BUILD false and -g applied

When true, the following settings applied:
-O3 -DNDEBUG
EXECUTABLE main
BUILD_FOLDER build, folder will be created if not exists
NO_CACHE false

When true, it removes the entire BUILD_FOLDER (includes the previous built object files) and run a clean build

Also, here is the default C_FALGS, feel free to change to what you needed:

#define DEFAULT_C_FLAGS \
    "-pedantic-errors", "-Wall", "-Werror", "-Wextra", "-std=c11", "-g"
#define DEFAULT_C_FLAGS_RELEASE                                                      \
    "-pedantic-errors", "-Wall", "-Werror", "-Wextra", "-std=c11", "-O3", "-DNDEBUG" \

#define DEFAULT_C_FLAGS_SANITIZER                                        \
    "-pedantic-errors", "-Wall", "-Werror", "-Wextra", "-std=c11", "-g", \
        "-fsanitize=address", "-O1", "-fno-omit-frame-pointer"
#define DEFAULT_C_FLAGS_SANITIZER_RELEASE                                 \
    "-pedantic-errors", "-Wall", "-Werror", "-Wextra", "-std=c11", "-O3", \
        "-DNDEBUG", "-fsanitize=address", "-O1", "-fno-omit-frame-pointer"

Source code in examples/basic


  • Create cbuild.c and include cbuild.h and write your own build process

    #define C_BUILD_IMPLEMENTATION
    #include "../../cbuild.h"
    
    int main(int argc, char **argv) {
        CB_setup_build_folder();
        CB_setup_compiler();
    
        // CB_compile_all("test.c", NULL);
        CB_compile_and_build_executable("test.c", NULL);
    }

  • Compile cbuild.c and then use it to compile your project and run it:

    #!/bin/sh
    
    C_COMPILER=$(which clang)
    # C_COMPILER=$(which cc)
    EXECUTABLE="my_program"
    BUILD_FOLDER="temp_build"
    NO_CACHE=true
    RELEASE_BUILD=true
    
    #
    # Compile `cbuild.c` and then use it to compile the project
    #
    ${C_COMPILER} -o cbuild -g cbuild.c \
        && \
        ENABLE_SANITIZER=true \
        C_COMPILER=${C_COMPILER} \
        BUILD_FOLDER=${BUILD_FOLDER} \
        NO_CACHE=${NO_CACHE} \
        RELEASE_BUILD=${RELEASE_BUILD} \
        EXECUTABLE=${EXECUTABLE} \
        ./cbuild \
        && \
        ${BUILD_FOLDER}/${EXECUTABLE}

    basic_build_2


3.3 Use EXTRA_COMPILE_FLAGS and EXTRA_LINK_FLAGS macros for complicated projects

You should define EXTRA_COMPILE_FLAGS and EXTRA_LINK_FLAGS macros to cover all the third-parties C_FLAGS, just makes sure you define those macros before #define C_BUILD_IMPLEMENTATION. Otherwise, it won't work.

Source code in examples/raylib-demo


  • Create cbuild.c and include cbuild.h and write your own build process

    #include <stdio.h>
    
    #define EXTRA_COMPILE_FLAGS "-I/usr/local/Cellar/raylib/4.2.0/include"
    #define EXTRA_LINK_FLAGS "-L/usr/local/Cellar/raylib/4.2.0/lib", "-lraylib"
    
    #define C_BUILD_IMPLEMENTATION
    
    #include "../../cbuild.h"
    
    int main(int argc, char **argv) {
        CB_setup_build_folder();
        CB_setup_compiler();
    
        CB_compile_and_build_executable("src/game.c", "src/main.c", NULL);
    }

  • Compile cbuild.c and then use it to compile your project and run it:

    #!/bin/sh
    
    C_COMPILER=$(which clang)
    # C_COMPILER=$(which cc)
    EXECUTABLE="raylib_demo"
    BUILD_FOLDER="temp_build"
    NO_CACHE=true
    RELEASE_BUILD=true
    
    #
    # Compile `cbuild.c` and then use it to compile the project
    #
    ${C_COMPILER} -o cbuild -g cbuild.c \
        && \
        ENABLE_SANITIZER=true \
        C_COMPILER=${C_COMPILER} \
        BUILD_FOLDER=${BUILD_FOLDER} \
        NO_CACHE=${NO_CACHE} \
        RELEASE_BUILD=${RELEASE_BUILD} \
        EXECUTABLE=${EXECUTABLE} \
        ./cbuild \
        && \
        ${BUILD_FOLDER}/${EXECUTABLE}

    raylib-demo


About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages