-
Notifications
You must be signed in to change notification settings - Fork 0
/
bob.cmd
executable file
·429 lines (378 loc) · 12.2 KB
/
bob.cmd
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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
:;#!/bin/bash
:;#
:;# This is a sketch of an experimental design for a build system where project definitions are written in C.
:;# The build system is packaged as a single shell script which contains the C library code as a heredoc string.
:;# The shell script creates a concatenation from the library code and the file specified as the first command line argument.
:;# Then that is compiled and the result is run, which takes any required build-related actions; for this toy implementation,
:;# the only build action is running the C compiler directly, but it could also generate makefiles or VS solutions like premake.
:;#
:;# Note that using some tricks you can write a single file that is both a valid Unix shell script and Windows batch file,
:;# which combined with cross-platform code for the C library would allow a portable single-file build system solution.
:;#
:;# Example usage:
:;#
:;# Suppose you have a C project with source files bar.c and baz.c and you want to build an executable called foo.
:;#
:;# Create foo.bob:
:;#
:;# void setup() {
:;# c_executable("foo");
:;# sources("bar.c", "baz.c");
:;# }
:;#
:;# Command line transcript:
:;#
:;# $ ./bob.cmd foo
:;# [PROJECT] foo
:;# Compiling C executable 'foo'
:;# [COMMAND] gcc -o foo bar.c baz.c
:;#
:;# $ ./foo
:;# STUFF
:<<"::CMDLITERAL"
@echo off
setlocal
set tmp_c=%TEMP%\bob-c.%random%
::CMDLITERAL
:;# Implement goto as a no-op in Shell because it simplifies some escaping.
:;function goto(){
:;
:;}
:;# What follows has to be on the same line because anything aftwards gets put in the C code
:;# Shell sees the CMD function call as comment up to the end quote
:;# CMD sees the Shell code as an end of line comment
:;# Both are able to share the :END label to end the C code block.
: '
call :heredoc bobc %tmp_c% && goto END &:'; bob_c=$(cat <<:END
#line 55 "bob.cmd"
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iso646.h>
char *strf(char *fmt, ...) {
va_list args;
va_start(args, fmt);
size_t size = vsnprintf(NULL, 0, fmt, args) + 1;
va_end(args);
char *str = malloc(size);
va_start(args, fmt);
vsnprintf(str, size, fmt, args);
va_end(args);
return str;
}
void vmsg(char *kind, char *fmt, va_list args) {
printf("%s%*s", kind, (int)(strlen(kind) <= 10 ? 10 - strlen(kind) : 1), "");
vprintf(fmt, args);
//This newline character causes compile errors on a shell with stricter posix compliance
//ie. calling the script with sh (bash doesnt complain)
printf("\n");
}
void msg(char *kind, char *fmt, ...) {
va_list args;
va_start(args, fmt);
vmsg(kind, fmt, args);
va_end(args);
}
void error(char *fmt, ...) {
va_list args;
va_start(args, fmt);
vmsg("[ERROR]", fmt, args);
va_end(args);
exit(1);
}
void info(char *fmt, ...) {
va_list args;
va_start(args, fmt);
vmsg("", fmt, args);
va_end(args);
}
int run(char *str) {
msg("[COMMAND]", "%s", str);
return system(str);
}
typedef struct {
char *data;
size_t num_items;
size_t max_items;
} array_t;
void array_init(array_t *array, size_t item_size) {
array->num_items = 0;
array->max_items = 16;
array->data = malloc(array->max_items * item_size);
}
void array_reset (array_t *array){
array->num_items = 0;
}
void array_add(array_t *array, void *item_ptr, size_t item_size) {
if (array->num_items == array->max_items) {
array->max_items *= 2;
array->data = realloc(array->data, array->max_items * item_size);
}
memcpy(array->data + array->num_items*item_size, item_ptr, item_size);
array->num_items++;
}
typedef struct {
union {
struct {
char **files;
size_t num_files;
};
array_t files_array;
};
} file_list_t;
void file_list_init(file_list_t *file_list) {
array_init(&file_list->files_array, sizeof(char *));
}
void file_list_add(file_list_t *file_list, char *filename) {
array_add(&file_list->files_array, &filename, sizeof(char *));
}
typedef struct {
union {
struct {
char **buildcommands;
size_t num_buildcommands;
};
array_t buildcommands_array;
};
} buildcommand_list_t;
void buildcommand_list_init(buildcommand_list_t *buildcommand_list) {
array_init(&buildcommand_list->buildcommands_array, sizeof(char *));
}
void buildcommand_list_add(buildcommand_list_t *buildcommand_list, char *buildcommandname) {
array_add(&buildcommand_list->buildcommands_array, &buildcommandname, sizeof(char *));
}
typedef struct {
union {
struct {
char **linkcommands;
size_t num_linkcommands;
};
array_t linkcommands_array;
};
} linkcommand_list_t;
void linkcommand_list_init(linkcommand_list_t *linkcommand_list) {
array_init(&linkcommand_list->linkcommands_array, sizeof(char *));
}
void linkcommand_list_add(linkcommand_list_t *linkcommand_list, char *linkcommandname) {
array_add(&linkcommand_list->linkcommands_array, &linkcommandname, sizeof(char *));
}
enum {
NONE,
RESET,
C_EXECUTABLE,
WASM_MODULE
};
typedef struct {
int type;
char *name;
file_list_t sources;
buildcommand_list_t buildcommands;
linkcommand_list_t linkcommands;
} project_t;
void project_init(project_t *project) {
project->type = NONE;
project->name = "<unnamed>";
file_list_init(&project->sources);
buildcommand_list_init(&project->buildcommands);
linkcommand_list_init(&project->linkcommands);
}
void project_reset(project_t *project){
project->type = RESET;
project->name = "<unnamed>";
array_reset(&project->sources.files_array);
array_reset(&project->buildcommands.buildcommands_array);
array_reset(&project->linkcommands.linkcommands_array);
}
void project_build(project_t *project) {
if (project->type == RESET) return; //skip projects that have just been reset
if (project->type == NONE) error("Trying to build project '%s' with no type selected.", project->name);
msg("[PROJECT]", "%s", project->name);
if (project->type == C_EXECUTABLE) {
info("Compiling C executable '%s'", project->name);
if (project->sources.num_files == 0) error("No source files specified.");
char *cmd = strf("clang -o %s", project->name);
for (size_t i = 0; i < project->buildcommands.num_buildcommands; i++)
cmd = strf("%s %s", cmd, project->buildcommands.buildcommands[i]);
for (size_t i = 0; i < project->linkcommands.num_linkcommands; i++)
cmd = strf("%s %s", cmd, project->linkcommands.linkcommands[i]);
for (size_t i = 0; i < project->sources.num_files; i++)
cmd = strf("%s %s", cmd, project->sources.files[i]);
// Uses iso646.h to avoid the not equal operator which interferes with CMD delayed expansion
if (run(cmd) not_eq 0) error("Compilation failed.");
}
}
project_t *this_project;
void c_executable(char *name) {
this_project->type = C_EXECUTABLE;
this_project->name = name;
}
void end_project() {
project_build(this_project);
project_reset(this_project);
}
void source(char *file) {
file_list_add(&this_project->sources, file);
}
void sources_func(char *file, ...) {
va_list args;
va_start(args, file);
while (file) {
source(file);
file = va_arg(args, char *);
}
va_end(args);
}
#define sources(...) sources_func(__VA_ARGS__, NULL)
void buildcommand(char *command) {
buildcommand_list_add(&this_project->buildcommands, command);
}
void buildcommands_func(char *command, ...) {
va_list args;
va_start(args, command);
while (command) {
buildcommand(command);
command = va_arg(args, char *);
}
va_end(args);
}
#define buildcommands(...) buildcommands_func(__VA_ARGS__, NULL)
void linkcommand(char *command) {
linkcommand_list_add(&this_project->linkcommands, command);
}
void linkcommands_func(char *command, ...) {
va_list args;
va_start(args, command);
while (command) {
linkcommand(command);
command = va_arg(args, char *);
}
va_end(args);
}
#define linkcommands(...) linkcommands_func(__VA_ARGS__, NULL)
void setup();
int main(int argc, char **argv) {
//printf("Parameters: ");
//for(int i=0;i<argc;i++){
//printf("%s ",argv[i]);
//}
//printf("\n");
project_t default_project;
project_init(&default_project);
this_project = &default_project;
setup();
project_build(this_project);
return 0;
}
:END
)
:;# CMD skips to it's own processing from here and Shell ignores it
goto CMDSCRIPT
#########################################
## Shell Script ##
#########################################
set -e
function cleanup {
rm -f $tmp_c $tmp_exe
}
function findCCompiler() {
if type gcc > /dev/null ; then
echo "gcc"
elif type clang > /dev/null ; then
echo "clang"
else
printf >&2 "[ERROR] Unable to locate suitable bootstrapping compiler.\n Are gcc or clang in PATH?\n";
exit 1;
fi
}
trap cleanup EXIT
#OSX mktemp (At least as of 10.10.5 Yosemite) fails to substitute if the file template also has an extension
#also has no --suffix option, Randomising the extension and telling gcc it's a c file was simplest work around.
#Debian mktemp expects at least 6 X's
tmp_c=$(mktemp bob-c.XXXXXX)
#Due to the above suffix issues with OSX mktemp we can't base the exe name on the c file
tmp_exe=$(mktemp /tmp/bob-exe.XXXXXX)
echo "$bob_c" >&$tmp_c
echo "#line 0 \"$1.bob\"" >>$tmp_c
cat $1.bob >>$tmp_c
compiler=$(findCCompiler)
cat $tmp_c>>"bob.log"
echo "Building with: "$compiler
eval "$compiler -std=c99 -o $tmp_exe -x c $tmp_c" ||{ echo "[ERROR] Bootstrap Compilation failed!"; exit 1; }
eval $tmp_exe ${@}
exit 0
:: #########################################
:: ## Command Prompt Script ##
:: #########################################
:CMDSCRIPT
echo #line 0 "%1.bob" >>%tmp_c%
type %1.bob >>%tmp_c%
set tmp_exe=%TEMP%\bob-exe.%random%.exe
WHERE /q gcc
IF %ERRORLEVEL% EQU 0 ECHO gcc was found
WHERE /q clang-cl
IF %ERRORLEVEL% EQU 0 (
ECHO clang-cl was found
set compiler=clang-cl
GOTO COMPILE
)
::Use vswhere to get latest installed visual studio directory
set vswhere_path=%ProgramFiles(x86)%\Microsoft Visual Studio\Installer\
set PATH=%PATH%;%vswhere_path%
set choco_path=%ProgramData%\chocolatey\bin\
set PATH=%PATH%;%choco_path%
pushd %CD%
::cd /d %vswhere_path%
for /f "usebackq delims=" %%i in (`vswhere.exe -products * -latest -requires Microsoft.VisualStudio.Component.VC.Tools.x86.x64 -property installationPath`) do (
set vcpath=%%i
)
popd
echo Found VC: %vcpath%
set compiler=cl
::Work around the fact that VC 2017 vcvarsall is an alias for the Dev command prompt and messes with working directory
pushd %CD%
call "%vcpath%\VC\Auxiliary\Build\vcvarsall.bat" x64 >nul 2>&1
if %errorlevel% NEQ 0 echo [ERROR] Problem configuring VC environment, unable to successfully run vcvarsall
popd
:COMPILE
%compiler% /Tc %tmp_c% /Fe%tmp_exe% /nologo
if %errorlevel% NEQ 0 (
echo [ERROR] Bootstrap Compilation failed!
exit /b 1
)
%tmp_exe% %*
del %tmp_c%
del %tmp_exe%
::Is this one necessary?
endlocal
endlocal
:: End of main CMD script
goto :EOF
:: ########################################
:: ## Batch File Heredoc processing code
:: ## Searches the batch file for the unique ID
:: ## Everything from that ID to end label is echoed into file
:: ########################################
:invokedoc <uniqueIDX>
setlocal enabledelayedexpansion
set go=
for /f "delims=" %%A in ('findstr /n "^" "%~f0"') do (
set "line=%%A" && set "line=!line:*:=!"
if defined go (if #!line:~1!==#!go::=! (goto :EOF) else echo(!line!)
if "!line:~0,13!"=="call :heredoc" (
for /f "tokens=3 delims=>^ " %%i in ("!line!") do (
if #%%i==#%1 (
for /f "tokens=2 delims=&" %%I in ("!line!") do (
for /f "tokens=2" %%x in ("%%I") do set "go=%%x"
)
)
)
)
)
exit /B
::A wrapping function so we do not expose Shell to IO redirection on the single line
:heredoc <iniqueIDX> <filename>
setlocal
call :invokedoc %1 > %~2
endlocal
exit /B