-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnob.c
More file actions
271 lines (213 loc) · 7.58 KB
/
Copy pathnob.c
File metadata and controls
271 lines (213 loc) · 7.58 KB
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
#define NOB_IMPLEMENTATION
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <time.h>
#include "nob.h"
typedef struct {
char** items;
uint64_t count;
uint64_t capacity;
} StringList;
#define SOURCE_FILE_COUNT 19// Modify this when adding a new files to the list of files
bool to_files(const char*[SOURCE_FILE_COUNT], StringList*, const char*, const char*);
bool build_object_files(const char*[SOURCE_FILE_COUNT], StringList, StringList*);
bool build_library(StringList);
bool build_example(void);
bool generate_build_h();
int main(int argc, char** argv) {
NOB_GO_REBUILD_URSELF(argc, argv);
const char* source_file_names[SOURCE_FILE_COUNT] = {
"Application", "Button", "CheckBox", "ComboBox", "Container", "Desktop", "InputDialog",
"Label","MessageDialog", "Panel", "PopupDialog", "Slider", "Widget", "Window",
"tinyfiledialogs","MenuBar","Menu", "MenuItem", "ImageView"
};
nob_mkdir_if_not_exists("./build");
if(!generate_build_h()){
return 1;
}
StringList src_files = {0};
if (!to_files(source_file_names, &src_files, "src", "cpp")) {
nob_log(NOB_ERROR, "Failed to make source files");
return 1;
}
StringList object_files = {0};
if (!build_object_files(source_file_names, src_files, &object_files)) {
nob_log(NOB_ERROR, "Failed to build sources into object files");
return 1;
}
if (!build_library(object_files)) {
nob_log(NOB_ERROR, "Failed to build final library artifacts");
return 1;
}
nob_log(NOB_INFO, "Library Built successfully. Artifacts are insider ./build");
if (!build_example()) {
return 1;
}
return 0;
}
bool build_example(void) {
char* raylib_home = getenv("RAYLIB_HOME");
if (raylib_home == NULL) {
nob_log(NOB_ERROR, "RAYLIB_HOME is not set");
return false;
}
char raylib_includes[1024];
char includes[1024];
sprintf(raylib_includes, "-I%s/include/", raylib_home);
sprintf(includes, "-Iinclude/");
char libs[1024];
sprintf(libs, "-L%s/lib/", raylib_home);
Nob_Cmd cmd = {0};
nob_cmd_append(&cmd, "g++");
nob_cmd_append(&cmd, "-ggdb");
nob_cmd_append(&cmd, raylib_includes);
nob_cmd_append(&cmd, includes);
nob_cmd_append(&cmd, "main.cpp");
nob_cmd_append(&cmd, "-o", "main.exe");
nob_cmd_append(&cmd, libs, "-Lbuild/");
#if defined(_WIN32) || defined (_WIN64)
nob_cmd_append(&cmd,
"-llibUI", "-lraylib", "-lm", "-lwinmm",
"-lgdi32", "-lopengl32", "-lole32", "-lcomdlg32");
#else
nob_cmd_append(&cmd, "-llibUI", "-lraylib", "-lm", "-lGL", "-lpthread", "-ldl", "-lrt", "-lX11");
#endif
nob_log(NOB_INFO, "Building example");
if (!nob_cmd_run(&cmd)) {
nob_log(NOB_ERROR, "Failed to build example");
return false;
}
nob_log(NOB_INFO, "Example built successfully");
return true;
}
bool build_library(StringList object_files) {
const char* static_object_name = "build/liblibUI.a";
#if defined(WIN32)
const char* shared_object_name = "build/libUI.dll";
const char* shared_lib_name = ".DLL";
#else
const char* shared_object_name = "build/libUI.so";
const char* shared_lib_name = ".SO";
#endif
char* raylib_home = getenv("RAYLIB_HOME");
if (raylib_home == NULL) {
nob_log(NOB_ERROR, "RAYLIB_HOME is not set");
return false;
}
char libs[1024];
sprintf(libs, "-L%s/lib/", raylib_home);
Nob_Cmd cmd = {0};
nob_cmd_append(&cmd, "g++");
nob_cmd_append(&cmd, "-shared");
for (uint64_t i = 0; i < object_files.count; ++i) {
nob_cmd_append(&cmd, object_files.items[i]);
}
nob_cmd_append(&cmd, "-o", shared_object_name);
nob_cmd_append(&cmd, libs);
#if defined(_WIN32) || defined (_WIN64)
nob_cmd_append(&cmd, "-lraylib", "-lm", "-lwinmm", "-lgdi32", "-lopengl32",
"-lgdi32", "-lopengl32", "-lole32", "-lcomdlg32");
#else
/* nob_cmd_append(&cmd, "-lraylib", "-lm", "-lGL", "-lpthread", "-ldl", "-lrt", "-lX11"); */
#endif
nob_log(NOB_INFO, "Creating .a file [%s]", static_object_name);
if (!nob_cmd_run(&cmd)) {
return false;
}
cmd.count = 0;
nob_cmd_append(&cmd, "ar");
nob_cmd_append(&cmd, "rcs");
nob_cmd_append(&cmd, static_object_name);
for (uint64_t i = 0; i < object_files.count; ++i) {
nob_cmd_append(&cmd, object_files.items[i]);
}
nob_log(NOB_INFO, "Creating %s file [%s]", shared_lib_name, shared_object_name);
if (!nob_cmd_run(&cmd)) {
return false;
}
return true;
}
bool build_object_files(const char* names[SOURCE_FILE_COUNT], StringList source_files, StringList* object_files) {
if (!to_files(names, object_files, "build", "o")) {
nob_log(NOB_ERROR, "Failed to name object files");
}
char* raylib_home = getenv("RAYLIB_HOME");
if (raylib_home == NULL) {
nob_log(NOB_ERROR, "RAYLIB_HOME is not set");
return false;
}
char raylib_includes[1024];
char includes[1024];
sprintf(raylib_includes, "-I%s/include/", raylib_home);
sprintf(includes, "-Iinclude/");
char libs[1024];
sprintf(libs, "-L%s/lib/", raylib_home);
Nob_Proc procs = {0};
for (uint64_t i = 0; i < SOURCE_FILE_COUNT; ++i) {
//:NOTE: Check if the object file is already up to date with the source file
int needs_rebuild = nob_needs_rebuild1(object_files->items[i], source_files.items[i]);
if(needs_rebuild < 0) return false;
if(!needs_rebuild) continue;//Skip obj files that are already up to date with source files
Nob_Cmd cmd = {0};
nob_cmd_append(&cmd, "g++");
#if !defined(_WIN32) || (_WIN64)
nob_cmd_append(&cmd, "-fPIC");
#endif
nob_cmd_append(&cmd, "-o");
nob_cmd_append(&cmd, object_files->items[i]);
nob_cmd_append(&cmd, "-c");
nob_cmd_append(&cmd, source_files.items[i]);
nob_cmd_append(&cmd, raylib_includes);
nob_cmd_append(&cmd, includes);
nob_log(NOB_INFO, "Compiling [%s] to [%s]", source_files.items[i], object_files->items[i]);
if (!nob_cmd_run(&cmd)) {
return false;
}
}
return true;
}
bool to_files(const char* names[SOURCE_FILE_COUNT], StringList* source_files, const char* dir, const char* ext) {
source_files->count = 0;
for (uint64_t i = 0; i < SOURCE_FILE_COUNT; i++) {
const char* name = names[i];
char buffer[1024];
sprintf(buffer, "%s/%s.%s", dir, name, ext);
nob_da_append(source_files, strdup(buffer));
}
assert(SOURCE_FILE_COUNT == source_files->count);
return true;
}
bool generate_build_h(){
time_t t = time(NULL);
struct tm tm = *localtime(&t);
nob_mkdir_if_not_exists("./include");
const char *path = "./include/build.h";
nob_log(NOB_INFO,"Generating %s", path);
FILE *f = fopen(path,"w");
if(f == NULL){
nob_log(NOB_ERROR,"Failed to open %s for writing", path);
return false;
}
Nob_String_Builder sb = {0};
nob_sb_appendf(&sb, "#ifndef __BUILD_DATE__\n\n");
nob_sb_appendf(&sb, "#define __BUILD_DATE__ \"%d-%02d-%02d %02d:%02d:%02d\"\n",
tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec);
char* os = NULL;
char* user = "UNKNOWN";
#if defined(__linux__) || defined(__unix__)
os = "Linux/Unix";
user = getenv("USER");
#elif defined(_WIN32) || defined(_WIN64)
os = "Windows";
user = getenv("USERS");
#endif
nob_sb_appendf(&sb, "#define __BUILD_BY__ \"%s\"\n", user);
nob_sb_appendf(&sb, "#define __BUILD_OS__ \"%s\"\n", os);
nob_sb_appendf(&sb,"\n#endif // LIBUI_BUILD_H\n");
fwrite(sb.items,1,sb.count,f);
nob_da_free(sb);
fclose(f);
nob_log(NOB_INFO,"Successfully generated %s", path);
return true;
}