/nobber/trunk

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/nobber/trunk
1 by Stream
Initial Commit.
1
#include <alloca.h>
2
#include <stdlib.h>
3
#include <stddef.h>
4
#include <stdio.h>
5
#include <stdlib.h>
6
#include <string.h>
7
#include <stdbool.h>
8
#include <stdint.h>
9
#include <inttypes.h>
10
#define NOB_IMPLEMENTATION
11
#define NOB_STRIP_PREFIX
12
#include "nob.h"
13
2 by Stream
* Added README
14
typedef struct da_strings_t DaStrings;
15
16
void * malloc0 (size_t size);
17
char * strip_extention (char * file_name);
18
char * c_to_o_ext (char * str);
19
DaStrings * compile_files (char * source_files[], size_t len, Nob_Cmd * cmd, Nob_Procs * procs);
20
void link_files (DaStrings * object_files,char * exec_name, Nob_Cmd * cmd);
21
void clean_files (char * source_files[], size_t len, Nob_Cmd * cmd);
1 by Stream
Initial Commit.
22
23
#define EXEC_NAME "nobber"
24
2 by Stream
* Added README
25
const char * default_build_flags[] = {
26
    "-std=gnu23",
27
    "-Wall",
28
    "-Wextra",
29
    "-mtune=native",
30
    "-g",
31
    NULL,
32
};
33
#define default_build_flags_count (NOB_ARRAY_LEN(default_build_flags) - 1)
34
35
const char * default_linking_flags[] = {
36
    "-lc",
37
    NULL,
38
};
39
#define default_linking_flags_count (NOB_ARRAY_LEN(default_linking_flags) - 1)
40
41
const char * src_files[] = {
42
    "src/main.c",
43
    "src/foo.c",
44
    NULL,
45
};
46
#define src_file_count (NOB_ARRAY_LEN(src_files) - 1)
47
1 by Stream
Initial Commit.
48
//#define free0(ptr) \
49
//    free (ptr); \
50
//    ptr = NULL;
51
#define free0(ptr) \
52
    memset ((void *)ptr, 0, sizeof ((void *) ptr)); \
53
    free (ptr);
54
2 by Stream
* Added README
55
struct da_strings_t {
56
    char ** items;
57
    size_t count;
58
    size_t capacity;
59
};
60
61
/** 
62
 * free the items in a list propperly!
63
 * TODO: Need a version that takes a FreeFunc.
64
 **/
1 by Stream
Initial Commit.
65
#define da_free_items(Type, da) { \
66
    for (Type *it = (da)->items; it < (da)->items + (da)->capacity; it++) { \
67
        if (*it == NULL) break;\
68
        free (*it);\
69
    }\
70
    free ((da)->items);\
71
}
72
2 by Stream
* Added README
73
int main (int argc, char ** argv) {
74
75
    Nob_Procs procs = {0};
76
    Nob_Cmd cmd = {0};
77
78
    NOB_GO_REBUILD_URSELF (argc, argv);
79
    [[maybe_unused]]  const char * program_name = nob_shift_args(&argc, &argv);
80
    const char * command_name = "build";
81
    if (argc > 0) command_name = nob_shift_args(&argc, &argv);
82
83
    if ((strcmp (command_name, "build")) == 0) {
84
        DaStrings * objects = compile_files ((char **)src_files, src_file_count, &cmd, &procs);
85
        nob_procs_wait_and_reset (&procs);
86
        link_files (objects, EXEC_NAME, &cmd);
87
        da_free_items (char *, objects);
88
        free (objects);
89
    } else if (strcmp (command_name, "clean") == 0) {
90
        clean_files ((char**)src_files, src_file_count, &cmd);
91
    } else {
92
        nob_log (NOB_INFO, "Unknown command %s\n", command_name);
93
    }
94
    if (cmd.capacity) nob_cmd_free (cmd);
95
96
    return 0;
97
}
98
99
/* ----------------- */
100
1 by Stream
Initial Commit.
101
void * malloc0 (size_t size) {
102
    void * out_val = malloc (size);
103
    memset (out_val, 0, size);
104
    return out_val;
105
}
106
107
108
// https://stackoverflow.com/questions/43163677/how-do-i-strip-a-file-extension-from-a-string-in-c
109
char * strip_extention (char * file_name) {
110
    char * out_str = malloc(strlen(file_name) + 1);
111
    strcpy (out_str, file_name);
112
    char *end = out_str + strlen(out_str);
113
114
    while (end > out_str && *end != '.' && *end != '\\' && *end != '/') {
115
        --end;
116
    }
117
118
    if ((end > out_str && *end == '.') &&
119
            (*(end - 1) != '\\' && *(end - 1) != '/')) {
120
        *end = '\0';
121
    }
122
123
    return out_str;
124
}
125
126
char * c_to_o_ext (char * str) {
127
    char * out_str = malloc (strlen(str) + 1);
128
    char * tmp_str = strip_extention (str);
129
    sprintf (out_str, "%s.o", tmp_str); 
130
    free (tmp_str);
131
    return out_str;
132
}
133
134
DaStrings * compile_files (char * source_files[], size_t len, Nob_Cmd * cmd, Nob_Procs * procs) {
135
    DaStrings * object_files = malloc0 (sizeof (DaStrings));
136
    for (size_t i = 0; i < len; i++) {
137
        cmd->count = 0;
138
        if (source_files[i] == NULL) break;
139
        nob_cmd_append (cmd, "cc");
140
        nob_cmd_append (cmd, source_files[i]);
141
        nob_da_append_many (cmd, default_build_flags, default_build_flags_count);
142
        nob_cmd_append (cmd, "-c");
143
        char * tmp_str = c_to_o_ext (source_files[i]);
144
        nob_cmd_append (cmd, "-o", tmp_str);
145
146
        char * s = malloc0 (strlen (tmp_str) + 1);
147
        strcpy (s, tmp_str);
148
        nob_da_append (object_files, s);
149
150
        nob_cmd_run (cmd, .async = procs);
151
        free0 (tmp_str);
152
    }
153
    return object_files;
154
}
155
156
void link_files (DaStrings * object_files,char * exec_name, Nob_Cmd * cmd) {
157
    nob_cmd_append (cmd, "cc");
158
    nob_cmd_append (cmd, "-o", exec_name);
159
    nob_da_append_many (cmd, (char**)object_files->items, object_files->count);
160
    nob_da_append_many (cmd, (char**) default_linking_flags, default_linking_flags_count);
161
    nob_cmd_run (cmd);
162
}
163
164
void clean_files (char * source_files[], size_t len, Nob_Cmd * cmd) {
165
    cmd->count = 0;
166
    DaStrings tmp_strings = {0};
167
    for (size_t i = 0; i < len; i++) {
168
        if (source_files[i] == NULL) break;
169
        char * tmp_file = strip_extention ((char *)source_files[i]);
170
        char * tmp_obj_file = c_to_o_ext (tmp_file);
171
        nob_da_append (&tmp_strings, tmp_obj_file);
172
        tmp_file = c_to_o_ext (tmp_file);
173
        free0 (tmp_file);
174
    }
175
    nob_cmd_append (cmd, "rm");
176
    nob_da_append_many (cmd, tmp_strings.items, tmp_strings.count);
177
    for (size_t i = 0; i < tmp_strings.count; i++) {
178
        char * s = tmp_strings.items[i];
179
        if (s == NULL) break;
180
    }
181
    nob_cmd_append (cmd, EXEC_NAME);
182
    nob_cmd_run (cmd);
183
    da_free_items (char *, &tmp_strings);
184
}
185