build.h
is a single-header build system written in C that provides essential tools for file operations, process management, and build automation.
- File Operations: Read/write files, check file existence, get modification times
- Directory Handling: List files recursively/non-recursively, create directories
- String Manipulation: Join, split, replace characters in strings
- Process Management: Execute commands, async command execution with await
- Build Automation: Check if binaries need rebuilding based on source timestamps
- Memory Management: Arena allocator for efficient memory handling
- Error Handling: Simple error handling mechanism
- Logging: Basic logging functionality
- Include
build.h
in your project - Define
BUILD_IMPLEMENTATION
in one C file before including - Use the provided functions
#define BUILD_IMPLEMENTATION
#include "build.h"
int main() {
bh_init_auto(); // Optional auto-build setup
// Your build logic here
bh_files_t sources = {0};
bh_recursive_files_get("src", &sources);
const char* cmd = bh_fmt("cc -o myapp %s", bh_files_to_string(&sources, ' '));
bh_execute(cmd);
bh_darray_free(&sources);
return 0;
}
bh_init_arena()
- Initialize memory arenabh_arena_alloc()
- Allocate memory from arenabh_fmt()
- String formatting (similar to printf)bh_log()
- Log messages with different levels
bh_file_read()
- Read entire filebh_file_write()
- Write to filebh_file_get_time()
- Get file modification timebh_path_exist()
- Check if path exists and its typebh_mkdir()
- Create directory (including parents)
bh_files_get()
- List files in directorybh_recursive_files_get()
- Recursively list filesbh_files_to_string()
- Join filenames with separator
bh_string_join()
- Concatenate two stringsbh_string_chop()
- Extract substringbh_string_replace_char()
- Replace characters in stringbh_string_to_array()
- Split string by separator
bh_execute()
- Execute shell commandbh_push_async()
- Run command asynchronouslybh_await()
- Wait for async commands to completebh_is_binary_old()
- Check if binary is older than sourcesbh_on_binary_old_execute()
- Conditional command execution
bh_define_darray()
- Define new dynamic array typebh_darray_push()
- Add item to arraybh_darray_pop()
- Remove last itembh_darray_get()
- Access item by indexbh_darray_len()
- Get array lengthbh_darray_free()
- Free array memory
bh_try {
FILE* fp = fopen("missing.txt", "r");
bh_uthrow(fp, bh_FileNotFoundError);
} bh_catch(bh_FileNotFoundError) {
bh_log(3, "File not found!");
}
build.h
can automatically rebuild itself when modified:
int main(int argc, char* argv[]) {
bh_init(argc, argv); // Checks if rebuild is needed
// Rest of your program
return 0;
}
- Basic Build Script
#define BUILD_IMPLEMENTATION
#include "build.h"
int main() {
bh_files_t sources = {0};
bh_recursive_files_get("src", &sources);
const char* cmd = bh_fmt("cc -o myapp %s -Wall -Wextra",
bh_files_to_string(&sources, ' '));
bh_execute(cmd);
bh_darray_free(&sources);
return 0;
}
bh_async_t async = {0};
bh_push_async(&async, "cc -c file1.c -o file1.o");
bh_push_async(&async, "cc -c file2.c -o file2.o");
bh_await(&async);
License
MIT License - see LICENSE file for details. Contributing
Contributions are welcome! Please open issues or pull requests for any bugs or feature requests.