Skip to content

View project on GitHub

Usage example

minimal setup

c3build
compiler(VERSION MIN 0.7.9)
project(example VERSION 0.1.0 AUTHOR "John") -- VERSION and AUTHOR are optional

add("src/main.c3") -- assuming that this file exists

program(example)

this projects setup

c3build
compiler(VERSION MIN 0.7.1 MAX 0.7.9)
project(c3build
        VERSION 0.3.2
        AUTHOR "Kiana Bennett")

find(EXE "git")

option("--trust=full")

ifdef(exe_git)
    option("-D GIT_HASH")
endif()

POSIX_INSTALL_DIR   = "$(HOME)/$(PROJECT)"
WINDOWS_INSTALL_DIR = "$(HOME)\\$(PROJECT)"
WINDOWS_FILE_NAME   = "$(PROJECT).exe"

task("install")
    print("Installing...")
    ifdef(OS_LINUX)
        remove(DIR "$(POSIX_INSTALL_DIR)")
        create(DIR "$(POSIX_INSTALL_DIR)")
        copy(FILE "build/$(PROJECT)" "$(POSIX_INSTALL_DIR)/$(PROJECT)")
        cmd("chmod +x $(POSIX_INSTALL_DIR)/$(PROJECT)")
    endif()
    ifdef(OS_DARWIN)
        remove(DIR "$(POSIX_INSTALL_DIR)")
        create(DIR "$(POSIX_INSTALL_DIR)")
        copy(FILE "build/$(PROJECT)" "$(POSIX_INSTALL_DIR)/$(PROJECT)")
        cmd("chmod +x $(POSIX_INSTALL_DIR)/$(PROJECT)")
    endif()
    ifdef(OS_WINDOWS)
        remove(DIR "$(WINDOWS_INSTALL_DIR)")
        create(DIR "$(WINDOWS_INSTALL_DIR)")
        copy(FILE "build\\$(WINDOWS_FILE_NAME)" "$(WINDOWS_INSTALL_DIR)\\$(WINDOWS_FILE_NAME)")
        cmd("setx PATH \"%PATH%;$(WINDOWS_INSTALL_DIR)\"")
    endif()
endtask()

add("src/")

program(c3build)

Functions

compiler()

compiler() defines the compiler version

c3build
compiler(VERSION 0.7.5)
compiler(VERSION MIN 0.7.5)
compiler(VERSION MAX 0.7.9)
compiler(VERSION MIN 0.7.5 MAX 0.7.9)

project()

project() defines the basic project info and is required for the script to function.

c3build
project(example VERSION 0.1.0 AUTHOR "John")

require()

require() exits the build file if the requested file could not be found.

c3build
require(LIB "LLVM") -- creates a variable `lib_llvm`
require(EXE "git")  -- creates a variable `exe_git`

find()

unlike require(), find() does not exit the script if the file cannot be found.

c3build
find(LIB "LLVM") -- creates a variable `lib_llvm`
find(EXE "git")  -- creates a variable `exe_git`

ifdef()

ifdef() provides a body whose statements will be run if the variable is defined.

c3build
find(EXE "git")

ifdef(exe_git)
    ...
endif()

option()

option() lets you add compile options to the command.

c3build
option("--trust=full")
option("-D EXAMPLE")

task()

task() lets you define a task that can be run by the script.

c3build
task("example")
    ...
endtask()

It can then be run like this

shell
c3build example

You can also chain multiple tasks

shell
c3build clean install example

cmd()

cmd() lets you execute terminal commands.

c3build
cmd("chmod +x ./program")

program()

program() is what executes the compile command so it should always be at the end of the file.

c3build
project(example VERSION 0.1.0)
program(example) -- name must match with project name

you can also tell the compiler what to link with and it should be done this way

c3build
...
require(LIB "LLVM")

program(example LINK lib_llvm) -- name must match with project name

create()

create() allows you to create files and folders.

c3build
create(DIR "$(HOME)/example")
create(FILE "$(HOME)/example/test.txt")

copy()

copy() allows you to copy files and folders.

c3build
copy(DIR "./example" "$(HOME)/example")
copy(FILE "./test.txt" "$(HOME)/example/test.txt")

print()

print() allows you to print to the console.

c3build
print("example message")

remove()

remove() allows you to delete files and folders.

c3build
remove(DIR "$(HOME)/example")
remove(FILE "$(HOME)/example/test.txt")

add()

add() lets you add files to the compile command.

c3build
add("src/main.c3"
    "src/other.c3")
add("lib/") -- you can also add directories

Variables

when using require() or find() a variable gets created. The variable can be used to check if the executable or library was found and if so, it returns the path to the file. A variable can either be prefixed by lib_ or exe_ depending on the first parameter (LIB or EXE). Variables can be used in strings for the functions option, cmd, create, copy, print and remove with the following syntax: $(VAR_NAME)

Builtins

HOME - points to the home directory
PROJECT - points to the project name
VERSION - points to the project version
AUTHOR - points to the project author
OS_WINDOWS - can only be used for ifdef and will result in empty string if used otherwise
OS_DARWIN - can only be used for ifdef and will result in empty string if used otherwise
OS_LINUX - can only be used for ifdef and will result in empty string if used otherwise

Custom

You can also create variables like this

c3build
PERSON = "John"

print("Hello $(PERSON)")

Additionally you can reference variables in other variables

c3build
FOO = "foo"
BAR = "bar"
FOO_BAR = "$(FOO)$(BAR)"

Variable Injection

c3build allows you to inject the AUTHOR and VERSION into c3. You can create constants in a file called c3build.c3 anywhere in your project like so

cpp
module version;

const VERSION = "@@VERSION@@";
const AUTHOR  = "@@AUTHOR@@";

Note that the constant names and module themselves are not important, only the string is.

Installing

As of now c3build does not offer any prebuilt binaries and must be built from source.

shell
git clone https://github.com/lyranie/c3-build-file.git
cd c3-build-file
./bootstrap.sh

or if on Windows

shell
git clone https://github.com/lyranie/c3-build-file.git
cd c3-build-file
./bootstrap.ps1

and that's it!