summaryrefslogtreecommitdiff
path: root/README.md
blob: 6dee142ad68d66c463bee89ae0e1357f6d5e9fc1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
# C BUILD

## Description

A lightweight single-header build system library for managing **one or more C projects** — without Makefiles.

It lets you:

* Define build logic directly in C.
* Build **sequentially** or **in parallel**.
* Run executables right after building.
* Automatically handle **self-rebuilding** build scripts.
* Use **wildcards** for file lists.
* Extend with your own macros.

---

## Installation

```bash
wget https://raw.githubusercontent.com/AnAnnoyinGoose/cbuild/refs/heads/main/src/lib/stb_cbuild.h -O cbuild.h
sudo mkdir -p /usr/local/include/cbuild
sudo cp cbuild.h /usr/local/include/cbuild
```

Now include it in your projects:

```c
#include <cbuild/cbuild.h>
```

---

## Showcase

This section shows **all functionality** of CBuild.

---

### 1. A simple project

```c
#include <cbuild/cbuild.h>

static _CB_PROJECT *hello = {0};

int main(void) {
    _CB_CREATE_PROJECT(hello,
        .name   = "Hello",
        .files  = CB_STRLIST("hello.c"),
        .output = "hello"
    );

    _CB_PROJECT_BUILD(.projects = CB_PROJECT_LIST(hello), .run = 1);
}
```

Builds `hello.c`, produces `./hello`, and runs it.

---

### 2. Rebuild mode

```c
#include <cbuild/cbuild.h>
#include <stdio.h>

static _CB_PROJECT *self = {0};

int main(void) {
    _CB_CREATE_PROJECT(self,
        .name       = "Rebuild",
        .files      = CB_STRLIST("main.c"),
        .output     = "rebuild",
        .is_rebuild = 1
    );

    _CB_PROJECT_BUILD(.projects = CB_PROJECT_LIST(self), .run = 1);

    printf("You will never see this if rebuild triggers.\n");
}
```

With `.is_rebuild = 1`:

* Builds the project.
* Runs it.
* Exits the whole process immediately.

---

### 3. Multiple projects

```c
#include <cbuild/cbuild.h>

static _CB_PROJECT *a = {0};
static _CB_PROJECT *b = {0};
static _CB_PROJECT *c = {0};

int main(void) {
    _CB_CREATE_PROJECT(a, .name = "A", .files = CB_STRLIST("a.c"), .output = "a");
    _CB_CREATE_PROJECT(b, .name = "B", .files = CB_STRLIST("b.c"), .output = "b");
    _CB_CREATE_PROJECT(c, .name = "C", .files = CB_STRLIST("c.c"), .output = "c");

    _CB_PROJECT_BUILD(.projects = CB_PROJECT_LIST(a, b, c), .run = 1);
}
```

Builds projects **sequentially** and runs them in order.

---

### 4. Parallel builds

```c
_CB_PROJECT_BUILD(
    .projects = CB_PROJECT_LIST(a, b, c),
    .run = 0,
    .parallel = 3
);
```

Builds 3 projects at the same time, **without running them**.

---

### 5. Wildcards

```c
_CB_CREATE_PROJECT(a,
    .name   = "Wildcard Example",
    .files  = CB_STRLIST("src/*.c"),   // expands to all C files in src/
    .output = "app"
);
```

Automatically expands `*.c` into a list of files.

---

### 6. Build flags

```c
_CB_CREATE_PROJECT(crypto,
    .name       = "CryptoTool",
    .files      = CB_STRLIST("main.c"),
    .output     = "cryptotool",
    .buildflags = CB_STRLIST("-lssl -lcrypto")
);
```

Adds extra compiler/linker flags.
*(Rebuild projects automatically add `-lssl -lcrypto`.)*

---

### 7. Dumping project info

```c
cb_dump_to_console(crypto);
```

Prints details of the project (name, files, output, flags).

---

### 8. Freeing resources

```c
cb_free_project(crypto);
```

Frees memory used by a project.

---

## API Reference

### Macros

```c
#define     CB_DEBUG              // Enable debug logging
#define     _CB_LOG_TO_FILE       // Write logs to cbuild.log

MACRO       CB_DEBUG_LOG(fmt, ...); 
MACRO       _CB_PROJECT_BUILD(projects, run, parallel, run_if_skipped);
MACRO       _CB_CREATE_PROJECT(name, output, CB_STRLIST(files), CB_STRLIST(buildflags), CB_STRLIST(flags), is_rebuild);
MACRO       CB_PROJECT_LIST(...);
MACRO       CB_STRLIST(...);
```

### Types

```c
TYPE        _CB_PROJECT   // Represents a single project
```

### Functions

```c
static void cb_dump_to_console(const _CB_PROJECT*);  // Print project info
static void cb_free_project(const _CB_PROJECT*);     // Free memory
```

---

## Full Demo Program

This example uses **all features at once**:

```c
#include <cbuild/cbuild.h>
#include <stdio.h>

static _CB_PROJECT *self   = {0};
static _CB_PROJECT *lib    = {0};
static _CB_PROJECT *tool   = {0};
static _CB_PROJECT *tests  = {0};

int main(void) {
    // Self-rebuild project (bootstrap)
    _CB_CREATE_PROJECT(self,
        .name       = "BuildScript",
        .files      = CB_STRLIST("main.c"),
        .output     = "rebuild",
        .is_rebuild = 1
    );

    // Library project with wildcards
    _CB_CREATE_PROJECT(lib,
        .name   = "MyLibrary",
        .files  = CB_STRLIST("src/*.c"),   // collect all .c files
        .output = "libmylib.a"
    );

    // Tool project with custom flags
    _CB_CREATE_PROJECT(tool,
        .name       = "Tool",
        .files      = CB_STRLIST("tool.c"),
        .output     = "tool",
        .buildflags = CB_STRLIST("-lssl -lcrypto")
    );

    // Test suite project
    _CB_CREATE_PROJECT(tests,
        .name   = "Tests",
        .files  = CB_STRLIST("tests/*.c"),
        .output = "tests"
    );

    // Print project info
    cb_dump_to_console(lib);
    cb_dump_to_console(tool);

    // Build all projects, run only tests, build others in parallel 

    // Cleanup
    cb_free_project(lib);
    cb_free_project(tool);
    cb_free_project(tests);

    return 0;
}
```

What happens here:

1. **Self-rebuilds** the build script.
2. Builds a **library** from all `src/*.c`.
3. Builds a **tool** with extra flags.
4. Builds and runs a **test suite**.
5. Uses **parallelism** (`.parallel = 2`).
6. Dumps project info for debugging.
7. Frees resources before exiting.

---


## Py DSL

This is a [Py DSL](https://en.wikipedia.org/wiki/Domain-specific_language) for CBuild.
It allows the user to write Python in C projects.
CBuild translates it to C and compiles it.

### Example
`main.c`
```C
#define _CB_IMPLEMENTATION
#define CB_DEBUG
#define _CB_PY // Enable Python support
#include <cbuild/cbuild.h>

static _CB_PROJECT *this = {0};
static _CB_PROJECT *CB_PY = {0};

int main(int argc, char *argv[]) {
  _CB_CREATE_PROJECT(
      this, .name = "cmpy-rebuild", 
      .files = CB_STRLIST("src/main.c"), .build_type = BUILD_EXEC, .is_rebuild = 1,
      .output = "bin/cmpy-rebuild",
      .buildflags =
          CB_STRLIST("-std=c99 -D_POSIX_C_SOURCE=200809L -D_GNU_SOURCE"));
  _CB_PROJECT_BUILD(.projects = CB_PROJECT_LIST(this));

  _CB_CREATE_PROJECT(
      CB_PY, .name = "cmpy", 
      .files = CB_STRLIST("src/cmpy.c"), .build_type = BUILD_EXEC, .CB_PYTHON = 1, // Sets the project to use the DSL
      .output = "bin/cmpy",
      .buildflags =
          CB_STRLIST("-std=c99"));
  _CB_PROJECT_BUILD(.projects = CB_PROJECT_LIST(CB_PY), .run = 1, .run_if_skipped = 1);

  printf("rebuild complete\n");
  return 0;
}
```

`src/cmpy.c`
```C
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
int main(void) {
// __CB_PY_BEGIN
// x = 1
// x = 2
// 
// name = "John"
// surname = "Doe"
//
//
// for i in range(10):
//    print(x)
//    x = x*2
// print("Name: ", name, surname)
// __CB_PY_END
  return EXIT_SUCCESS;
}
```


## Notes

* Rebuild projects auto-add `-lssl -lcrypto`.
* Supports **wildcards**, **parallel builds**, and **macro-based configuration**.
* Debug logging can be redirected to console or file.

---

## License

MIT License – free to use, modify, and distribute.