-
Notifications
You must be signed in to change notification settings - Fork 47
/
main.c
1260 lines (1061 loc) · 34.3 KB
/
main.c
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
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* some parts written at high altitudes */
/* some parts written at the top of the eiffel tower */
/* system includes */
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
/* lua includes */
#define LUA_CORE /* make sure that we don't try to import these functions */
#include <lua.h>
#include <lualib.h> /* luaL_openlibs */
#include <lauxlib.h> /* luaL_loadfile */
/* program includes */
#include "mem.h"
#include "node.h"
#include "path.h"
#include "support.h"
#include "context.h"
#include "cache.h"
#include "statcache.h"
#include "luafuncs.h"
#include "platform.h"
#include "session.h"
#include "version.h"
#include "verify.h"
/* internal base.bam file */
#include "internal_base.h"
/* needed for getcwd */
#if defined(BAM_FAMILY_UNIX) || defined(BAM_FAMILY_BEOS)
#include <unistd.h>
#endif
#ifdef BAM_FAMILY_WINDOWS
#include <direct.h>
#define getcwd _getcwd /* stupid msvc is calling getcwd non-ISO-C++ conformant */
#endif
#define DEFAULT_REPORT_STYLE "s"
/* ** */
#define L_FUNCTION_PREFIX "bam_"
enum
{
OF_PRINT = 0x01,
OF_DEBUG = 0x02
};
struct OPTION
{
int flags;
const char **s;
int *v;
const char *sw;
const char *desc;
};
/* options passed via the command line */
static int option_force = 0;
static int option_clean = 0;
static int option_no_cache = 0;
static int option_no_scripttimestamp = 0;
static int option_dry = 0;
static int option_dependent = 0;
static int option_abort_on_error = 0;
static int option_debug_nodes = 0;
static int option_debug_nodes_html = 0;
static int option_debug_joblist = 0;
static int option_debug_dumpinternal = 0;
static int option_debug_nointernal = 0;
static int option_debug_trace_vm = 0;
static const char *option_debug_verify = NULL;
static int option_print_help = 0;
static int option_print_debughelp = 0;
static const char *option_debug_eventlog = NULL;
static int option_debug_eventlogflush = 0;
static const char *option_script = "bam.lua"; /* -f filename */
static const char *option_threads_str = NULL;
static const char *option_report_str = DEFAULT_REPORT_STYLE;
static const char *option_targets[128] = {0};
static const char* option_lua_execute = NULL;
static int option_num_targets = 0;
static const char *option_scriptargs[128] = {0};
static int option_num_scriptargs = 0;
static int option_win_msvcmode = 0;
/* exprimental options */
int option_cdep2 = 0;
int option_prio2 = 0;
/* filename of the dependency cache, will be filled in at start up, ".bam/xxxxxxxxyyyyyyyyy" = 22 top */
static char depcache_filename[128] = {0};
/* filename of the scancache will be filled in at start up, ".bam/scancache_xxxxxxxxyyyyyyyyy" = 32 top */
static char scancache_filename[128] = {0};
/* filename of the command cache */
static char outputcache_filename[] = ".bam/outputcache";
/* session object */
struct SESSION session = {
"bam", /* exe */
"bam", /* name */
1, /* threads */
0 /* rest */
};
/* storage for parameters */
static char parameters_buffer[1024] = {0};
static struct OPTION options[] = {
/*@OPTION Targets ( name )
Specify a target to be built. A target can be any output
specified to the [AddJob] function.
If no targets are specified, the default target will be built
If there are no default target and there is only one target
specified with the [Target] function, it will be built.
Otherwise bam will report an error.
There is a special pseudo target named ^all^ that represents
all targets specified by the [Target] function.
@END*/
/*@OPTION Script Arguments ( name=value )
Sets a script argument. These arguments can be fetched form the build script
by accessing the ^ScriptArgs^ table.
@END*/
{OF_PRINT, 0, 0 , "\n Execution:", ""},
/*@OPTION Abort on error ( -a )
Setting this will cause bam to abort the build process when an error has occured.
Normally it would continue as far as it can.
@END*/
{OF_PRINT, 0, &option_abort_on_error , "-a", "abort build on first error"},
/*@OPTION Lua execute ( -e )
Executes a lua file without running the build system.
@END*/
{OF_PRINT, &option_lua_execute, 0 , "-e filename", "executes specified lua file and exits"},
{0, &option_lua_execute, 0 , "-e", NULL},
/*@OPTION Clean ( -c )
Cleans the specified targets or the default target.
@END*/
{OF_PRINT, 0, &option_clean , "-c", "clean targets"},
/*@OPTION Force ( -f )
Forces all the jobs to be dirty
@END*/
{OF_PRINT, 0, &option_force , "-f", "force build"},
/*@OPTION Dependent build ( -d )
Builds all targets that are dependent on the given targets.
If no targets are given this option doesn't do anything.
@END*/
{OF_PRINT, 0, &option_dependent , "-d", "build targets that is dependent given targets"},
/*@OPTION Dry Run ( --dry )
Does everything that it normally would do but does not execute any
commands.
@END*/
{OF_PRINT, 0, &option_dry , "--dry", "dry run, don't run any jobs"},
/*@OPTION Threading ( -j N )
Sets the number of threads used when building. A good value for N is
the same number as logical cores on the machine. Set to 0 to disable.
@END*/
{OF_PRINT, &option_threads_str,0 , "-j num", "sets the number of threads to use (default: auto, -v will show it)"},
{0, &option_threads_str, 0 , "-j", NULL},
/*@OPTION Script File ( -s FILENAME )
Bam file to use. In normal operation, Bam executes
^bam.lua^. This option allows you to specify another bam
file.
@END*/
{OF_PRINT, &option_script,0 , "-s filename", "script file to use (default: bam.lua)"},
{0, &option_script, 0 , "-s", NULL},
{OF_PRINT, 0, 0 , "\n Lua:", ""},
/*@OPTION Script Locals ( -l )
Prints local and up values in the backtrace when there is a script error
@END*/
{OF_PRINT, 0, &session.lua_locals , "-l", "print local variables in backtrace"},
/*@OPTION Script Backtrace ( -t )
Prints backtrace when there is a script error
@END*/
{OF_PRINT, 0, &session.lua_backtrace , "-t", "print backtrace when an error occurs"},
{OF_PRINT, 0, 0 , "\n Output:", ""},
/*@OPTION Report Format ( -r [b][s][c] )
Sets the format of the progress report when building.
<ul>
<li>b</li> - Use a progress bar showing the percentage.
<li>s</li> - Show each step when building. (default)
<li>c</li> - Use ANSI colors.
</ul>
@END*/
{OF_PRINT, &option_report_str,0 , "-r flags", "build progress report format (default: " DEFAULT_REPORT_STYLE ")\n"
" " " b = progress bar\n"
" " " c = use ansi colors\n"
" " " s = build steps"},
{0, &option_report_str,0 , "-r", NULL},
/*@OPTION Verbose ( -v )
Prints all commands that are runned when building.
@END*/
{OF_PRINT, 0, &session.verbose , "-v", "be verbose"},
{OF_PRINT, 0, 0 , "\n Other:", ""},
/*@OPTION No cache ( -n )
Do not use cache when building.
@END*/
{OF_PRINT, 0, &option_no_cache , "-n", "don't use cache"},
/*@OPTION Ignore script timestamp ( -g )
Ignores the timestamp on the script when doing dirty checking.
Enabling this causes the output not to be rebuilt when the build script changes.
@END*/
{OF_PRINT, 0, &option_no_scripttimestamp, "-g", "ignore script timestamp"},
/*@OPTION Help ( -h, --help )
Prints out a short reference of the command line options and quits
directly after.
@END*/
{OF_PRINT, 0, &option_print_help , "-h, --help", "prints this help"},
{0, 0, &option_print_help , "-h", "prints this help"},
{0, 0, &option_print_help , "--help", "prints this help"},
/*@OPTION Debug Help ( --help-debug )
Prints out a reference over the debugging options.
@END*/
{OF_PRINT, 0, &option_print_debughelp , "--help-debug", "prints debugging options"},
{OF_DEBUG, 0, 0 , "\n Debug:", ""},
/*@OPTION Debug: Verify ( --debug-verify )
Check changes to files after every job is runned to make sure the correct outputs are updated and no
unknown side effects happened. This is done by recursivly checking every file in the current working
directory. Can be very slow and threading will be turned off.
@END*/
{OF_DEBUG, &option_debug_verify, 0, "--debug-verify basepath", "(EXPRIMENTAL) verify job outputs and look for unknown side effects"},
{0, &option_debug_verify, 0, "--debug-verify", 0},
/*@OPTION Debug: Dump Nodes ( --debug-nodes )
Dumps all nodes in the dependency graph, their state and their
dependent nodes. This is useful if you are writing your own
actions to verify that dependencies are correctly added.
@END*/
{OF_DEBUG, 0, &option_debug_nodes , "--debug-nodes", "prints all the nodes with dependencies and details"},
/*@OPTION Debug: Dump Nodes Detailed in HMTL ( --debug-nodes-html )
Same as --debug-detail put outputs a HTML document with clickable links
for easier browsing.
@END*/
{OF_DEBUG, 0, &option_debug_nodes_html , "--debug-nodes-html", "as --debug-nodes but in html format"},
/*@OPTION Debug: Dump Joblist ( --debug-joblist )
@END*/
{OF_DEBUG, 0, &option_debug_joblist , "--debug-joblist", "prints all the job in the order that they will be attempted"},
/*@OPTION Debug: Trace VM ( --debug-trace-vm )
Prints a the function and source line for every instruction that the vm makes.
@END*/
{OF_DEBUG, 0, &option_debug_trace_vm , "--debug-trace-vm", "prints a line for every instruction the vm makes"},
/*@OPTION Debug: Event Log ( --debug-eventlog FILENAME )
Outputs an build event log that contains all the events with timing information.
@END*/
{OF_DEBUG, &option_debug_eventlog, 0 , "--debug-eventlog", "dumps all build events into a file"},
/*@OPTION Debug: Event Log Flush ( --debug-eventlog-flush )
Flushes the event log after each write.
@END*/
{OF_DEBUG, 0, &option_debug_eventlogflush , "--debug-eventlog-flush", "flushes the eventlog after each write"},
/*@OPTION Debug: Dump Internal Scripts ( --debug-dump-int )
@END*/
{OF_DEBUG, 0, &option_debug_dumpinternal , "--debug-dump-int", "prints the internals scripts to stdout"},
/*@OPTION Debug: No Internal ( --debug-no-int )
Disables all the internal scripts that bam loads on startup.
@1, END*/
{OF_DEBUG, 0, &option_debug_nointernal , "--debug-no-int", "don't load internal scripts"},
/*
*/
{OF_DEBUG, 0, &option_cdep2, "--cdep2", "EXPRIMENTAL: New improved C dependency checker"},
/*
*/
{OF_DEBUG, 0, &option_prio2, "--prio2", "EXPRIMENTAL: New improved prioritization code"},
/* Magic highly exprimental switch for Microsoft Visual Studio. Enabling this will cause bam to execute
itself again and wait for the new child process to finish. The child process then removes the permissions
from the current user so visual studio can't kill the process. Then it starts a thread that monitors if
the parent process dies and then aborts the build softly without killing it's jobs. This fixes issues if
you abort a build in visual studio, it might leave broken object files behind. This also prevents
two bams to be started at the same time.
*/
{0, 0, &option_win_msvcmode , "--win-msvc-mode", "exprimental option for visual studio"},
/* terminate list */
{0, 0, 0, (const char*)0, (const char*)0}
};
static const char *internal_base_reader(lua_State *L, void *data, size_t *size)
{
char **p = (char **)data;
if(!*p)
return 0;
*size = strlen(*p);
data = *p;
*p = 0;
return data;
}
static void lua_setglobalstring(lua_State *L, const char *field, const char *s)
{
lua_pushstring(L, s);
lua_setglobal(L, field);
}
static void lua_vm_trace_hook(lua_State *L, lua_Debug *ar)
{
lua_getinfo(L, "nSl", ar);
if(ar->name)
printf("%s %s %d\n", ar->name, ar->source, ar->currentline);
}
static void *lua_alloctor_malloc(void *ud, void *ptr, size_t osize, size_t nsize)
{
if (nsize == 0)
{
free(ptr);
return NULL;
}
return realloc(ptr, nsize);
}
/* *** */
int register_lua_globals(struct lua_State *lua, const char* script_directory, const char* filename)
{
int i, error = 0, idx = 1;
/* add standard libs */
luaL_openlibs(lua);
/* add specific functions */
lua_register(lua, L_FUNCTION_PREFIX"add_job", lf_add_job);
lua_register(lua, L_FUNCTION_PREFIX"add_output", lf_add_output);
lua_register(lua, L_FUNCTION_PREFIX"add_sideeffect", lf_add_sideeffect);
lua_register(lua, L_FUNCTION_PREFIX"add_clean", lf_add_clean);
lua_register(lua, L_FUNCTION_PREFIX"add_pseudo", lf_add_pseudo);
lua_register(lua, L_FUNCTION_PREFIX"add_dependency", lf_add_dependency);
lua_register(lua, L_FUNCTION_PREFIX"add_constraint_shared", lf_add_constraint_shared);
lua_register(lua, L_FUNCTION_PREFIX"add_constraint_exclusive", lf_add_constraint_exclusive);
lua_register(lua, L_FUNCTION_PREFIX"default_target", lf_default_target);
lua_register(lua, L_FUNCTION_PREFIX"set_filter", lf_set_filter);
lua_register(lua, L_FUNCTION_PREFIX"set_priority", lf_set_priority);
lua_register(lua, L_FUNCTION_PREFIX"modify_priority", lf_modify_priority);
lua_register(lua, L_FUNCTION_PREFIX"skip_output_verification", lf_skip_output_verification);
/* advanced dependency checkers */
lua_register(lua, L_FUNCTION_PREFIX"add_dependency_cpp_set_paths", lf_add_dependency_cpp_set_paths);
lua_register(lua, L_FUNCTION_PREFIX"add_dependency_cpp", lf_add_dependency_cpp);
lua_register(lua, L_FUNCTION_PREFIX"add_dependency_search", lf_add_dependency_search);
/* path manipulation */
lua_register(lua, L_FUNCTION_PREFIX"path_join", lf_path_join);
lua_register(lua, L_FUNCTION_PREFIX"path_normalize", lf_path_normalize);
lua_register(lua, L_FUNCTION_PREFIX"path_isnice", lf_path_isnice);
lua_register(lua, L_FUNCTION_PREFIX"path_ext", lf_path_ext);
lua_register(lua, L_FUNCTION_PREFIX"path_dir", lf_path_dir);
lua_register(lua, L_FUNCTION_PREFIX"path_base", lf_path_base);
lua_register(lua, L_FUNCTION_PREFIX"path_filename", lf_path_filename);
lua_register(lua, L_FUNCTION_PREFIX"path_hash", lf_path_hash);
/* various support functions */
lua_register(lua, L_FUNCTION_PREFIX"collect", lf_collect);
lua_register(lua, L_FUNCTION_PREFIX"collectrecursive", lf_collectrecursive);
lua_register(lua, L_FUNCTION_PREFIX"collectdirs", lf_collectdirs);
lua_register(lua, L_FUNCTION_PREFIX"collectdirsrecursive", lf_collectdirsrecursive);
lua_register(lua, L_FUNCTION_PREFIX"listdir", lf_listdir);
lua_register(lua, L_FUNCTION_PREFIX"update_globalstamp", lf_update_globalstamp);
lua_register(lua, L_FUNCTION_PREFIX"loadfile", lf_loadfile);
lua_register(lua, L_FUNCTION_PREFIX"mkdir", lf_mkdir);
lua_register(lua, L_FUNCTION_PREFIX"mkdirs", lf_mkdirs);
lua_register(lua, L_FUNCTION_PREFIX"fileexist", lf_fileexist);
lua_register(lua, L_FUNCTION_PREFIX"nodeexist", lf_nodeexist);
lua_register(lua, L_FUNCTION_PREFIX"hash", lf_hash);
lua_register(lua, L_FUNCTION_PREFIX"sleep", lf_sleep);
lua_register(lua, L_FUNCTION_PREFIX"isfile", lf_isfile);
lua_register(lua, L_FUNCTION_PREFIX"isdir", lf_isdir);
lua_register(lua, L_FUNCTION_PREFIX"isstring", lf_isstring);
lua_register(lua, L_FUNCTION_PREFIX"istable", lf_istable);
lua_register(lua, L_FUNCTION_PREFIX"isoutput", lf_isoutput);
lua_register(lua, L_FUNCTION_PREFIX"table_walk", lf_table_walk);
lua_register(lua, L_FUNCTION_PREFIX"table_deepcopy", lf_table_deepcopy);
lua_register(lua, L_FUNCTION_PREFIX"table_tostring", lf_table_tostring);
lua_register(lua, L_FUNCTION_PREFIX"table_flatten", lf_table_flatten);
/* error handling */
lua_register(lua, "errorfunc", lf_errorfunc);
/* create arguments table */
lua_pushglobaltable(lua);
lua_pushstring(lua, CONTEXT_LUA_SCRIPTARGS_TABLE);
lua_newtable(lua);
for(i = 0; i < option_num_scriptargs; i++)
{
const char *separator = option_scriptargs[i];
while(*separator != '=' && *separator != '\0')
separator++;
if(*separator == '\0')
{
lua_pushnumber(lua, idx++);
lua_pushstring(lua, option_scriptargs[i]);
}
else
{
lua_pushlstring(lua, option_scriptargs[i], separator-option_scriptargs[i]);
lua_pushstring(lua, separator+1);
}
lua_settable(lua, -3);
}
lua_settable(lua, -3);
lua_pop(lua, 1);
/* create targets table */
lua_pushglobaltable(lua);
lua_pushstring(lua, CONTEXT_LUA_TARGETS_TABLE);
lua_newtable(lua);
for(i = 0; i < option_num_targets; i++)
{
lua_pushstring(lua, option_targets[i]);
lua_rawseti(lua, -2, i);
}
lua_settable(lua, -3);
lua_pop(lua, 1);
/* set paths */
{
char cwd[MAX_PATH_LENGTH];
if(!getcwd(cwd, sizeof(cwd)))
{
printf("%s: error: couldn't get current working directory\n", session.name);
return -1;
}
lua_setglobalstring(lua, CONTEXT_LUA_PATH, script_directory);
lua_setglobalstring(lua, CONTEXT_LUA_WORKPATH, cwd);
}
/* set version, family, platform, arch, verbocity */
lua_setglobalstring(lua, "_bam_version", BAM_VERSION_STRING);
lua_setglobalstring(lua, "_bam_version_complete", BAM_VERSION_STRING_COMPLETE);
lua_setglobalstring(lua, "family", BAM_FAMILY_STRING);
lua_setglobalstring(lua, "platform", BAM_PLATFORM_STRING);
lua_setglobalstring(lua, "arch", BAM_ARCH_STRING);
lua_setglobalstring(lua, "_bam_exe", session.exe);
lua_setglobalstring(lua, "_bam_modulefilename", filename);
lua_pushnumber(lua, session.verbose);
lua_setglobal(lua, "verbose");
if(option_debug_trace_vm)
lua_sethook(lua, lua_vm_trace_hook, LUA_MASKCOUNT, 1);
/* load base script */
if(!option_debug_nointernal)
{
int ret;
const char *p;
int f;
for(f = 0; internal_files[f].filename; f++)
{
p = internal_files[f].content;
if(session.verbose)
printf("%s: reading internal file '%s'\n", session.name, internal_files[f].filename);
lua_getglobal(lua, "errorfunc");
/* push error function to stack */
ret = lua_load(lua, internal_base_reader, (void *)&p, internal_files[f].filename, NULL);
if(ret != 0)
{
lf_errorfunc(lua);
if(ret == LUA_ERRSYNTAX)
{
}
else if(ret == LUA_ERRMEM)
printf("%s: memory allocation error\n", session.name);
else
printf("%s: unknown error parsing base script\n", session.name);
error = 1;
}
else if(lua_pcall(lua, 0, LUA_MULTRET, -2) != 0)
error = 1;
}
}
return error;
}
static int run_deferred_functions(struct CONTEXT *context, struct DEFERRED *cur)
{
for(; cur; cur = cur->next)
{
if(cur->run(context, cur))
return -1;
}
return 0;
}
static int bam_setup(struct CONTEXT *context, const char *scriptfile, const char **targets, int num_targets)
{
/* */
if(session.verbose)
printf("%s: setup started\n", session.name);
/* set filename */
context->filename = scriptfile;
/* set global timestamp to the script file */
context->globaltimestamp = file_timestamp(scriptfile);
/* */
context->forced = option_force;
/* fetch script directory */
{
char cwd[MAX_PATH_LENGTH];
char path[MAX_PATH_LENGTH];
if(!getcwd(cwd, sizeof(cwd)))
{
printf("%s: error: couldn't get current working directory\n", session.name);
return -1;
}
if(path_directory(context->filename, path, sizeof(path)))
{
printf("%s: error: path too long '%s'\n", session.name, path);
return -1;
}
if(path_join(cwd, -1, path, -1, context->script_directory, sizeof(context->script_directory)))
{
printf("%s: error: path too long when joining '%s' and '%s'\n", session.name, cwd, path);
return -1;
}
}
/* register all functions */
event_begin(0, "lua setup", NULL);
if(register_lua_globals(context->lua, context->script_directory, context->filename) != 0)
{
printf("%s: error: registering of lua functions failed\n", session.name);
return -1;
}
event_end(0, "lua setup", NULL);
/* load script */
if(session.verbose)
printf("%s: reading script from '%s'\n", session.name, scriptfile);
event_begin(0, "script load", NULL);
/* push error function to stack and load the script */
lua_getglobal(context->lua, "errorfunc");
switch(luaL_loadfile(context->lua, scriptfile))
{
case 0: break;
case LUA_ERRSYNTAX:
lf_errorfunc(context->lua);
return -1;
case LUA_ERRMEM:
printf("%s: memory allocation error\n", session.name);
return -1;
case LUA_ERRFILE:
printf("%s: error opening '%s'\n", session.name, scriptfile);
return -1;
default:
printf("%s: unknown error\n", session.name);
return -1;
}
event_end(0, "script load", NULL);
/* create the cache and tmp directory */
file_createdir(".bam");
/* start the background stat thread */
node_graph_start_statthread(context->graph);
/* call the code chunk */
event_begin(0, "script run", NULL);
if(lua_pcall(context->lua, 0, LUA_MULTRET, -2) != 0)
{
node_graph_end_statthread(context->graph);
if(!session.lua_backtrace)
printf("%s: script error (-t for more detail)\n", session.name);
return -1;
}
event_end(0, "script run", NULL);
/* stop the background stat thread */
event_begin(0, "stat", NULL);
node_graph_end_statthread(context->graph);
event_end(0, "stat", NULL);
/* run deferred functions */
event_begin(0, "deferred cpp dependencies", NULL);
if(run_deferred_functions(context, context->firstdeferred_cpp) != 0)
return -1;
event_end(0, "deferred cpp dependencies", NULL);
/* run deferred functions */
event_begin(0, "deferred cpp dependencies 2", NULL);
{
int i;
for ( i = 0; i < CSCAN_HASHSIZE; i++ ) {
struct DEFERRED_CSCAN * cur = context->firstcscans[i];
for ( ; cur; cur = cur->next ) {
if(run_deferred_functions(context, cur->first) != 0)
return -1;
}
}
}
event_end(0, "deferred cpp dependencies 2", NULL);
event_begin(0, "deferred search dependencies", NULL);
if(run_deferred_functions(context, context->firstdeferred_search) != 0)
return -1;
event_end(0, "deferred search dependencies", NULL);
/* */
if(session.verbose)
printf("%s: making build target\n", session.name);
/* make build target */
{
struct NODE *node;
int all_target = 0;
int i;
if(node_create(&context->target, context->graph, "_bam_buildtarget", NULL, TIMESTAMP_PSEUDO))
return -1;
if(num_targets)
{
/* search for all target */
for(i = 0; i < num_targets; i++)
{
if(strcmp(targets[i], "all") == 0)
{
all_target = 1;
break;
}
}
}
/* default too all if we have no targets or default target */
if(num_targets == 0 && !context->defaulttarget)
all_target = 1;
if(all_target)
{
/* build the all target */
for(node = context->graph->first; node; node = node->next)
{
if(node->firstparent == NULL && node != context->target)
{
if(!node_add_dependency (context->target, node))
return -1;
}
}
}
else
{
if(num_targets)
{
for(i = 0; i < num_targets; i++)
{
struct NODE *node = node_find(context->graph, targets[i]);
if(!node)
{
printf("%s: target '%s' not found\n", session.name, targets[i]);
return -1;
}
if(option_dependent)
{
/* TODO: this should perhaps do a reverse walk up in the tree to
find all dependent node with commandline */
struct NODELINK *parent;
for(parent = node->firstparent; parent; parent = parent->next)
{
if(!node_add_dependency (context->target, parent->node))
return -1;
}
}
else
{
if(!node_add_dependency (context->target, node))
return -1;
}
}
}
else
{
if(!node_add_dependency (context->target, context->defaulttarget))
return -1;
}
}
}
/* zero out the global timestamp if we don't want to use it */
if(option_no_scripttimestamp)
context->globaltimestamp = 0;
/* */
if(session.verbose)
printf("%s: setup done\n", session.name);
/* return success */
return 0;
}
/* null verify callback, used to seed the initial state */
static int verify_callback_null(const char *fullpath, hash_t hashid, time_t oldstamp, time_t newstamp, void *user) { return 0; }
/* *** */
static int bam(const char *scriptfile, const char **targets, int num_targets)
{
struct CONTEXT context;
int build_error = 0;
int setup_error = 0;
int report_done = 0;
time_t outputcache_timestamp = 0;
/* build time */
time_t starttime = time(0x0);
/* zero out and create memory heap, graph */
memset(&context, 0, sizeof(struct CONTEXT));
context.graphheap = mem_create();
context.deferredheap = mem_create();
context.graph = node_graph_create(context.graphheap);
context.statcache = statcache_create();
context.exit_on_error = option_abort_on_error;
context.buildtime = timestamp();
/* create lua context */
/* HACK: Store the context pointer as the userdata pointer to the allocator to make
sure that we have fast access to it. This makes the context_get_pointer call very fast */
context.lua = lua_newstate(lua_alloctor_malloc, &context);
/* install panic function */
lua_atpanic(context.lua, lf_panicfunc);
/* load cache (thread?) */
if(option_no_cache == 0)
{
/* create a hash of all the external variables that can cause the
script to generate different results */
hash_t cache_hash = 0;
char hashstr[64];
int i;
for(i = 0; i < option_num_scriptargs; i++)
cache_hash = string_hash_djb2_add(cache_hash, option_scriptargs[i]);
string_hash_tostr(cache_hash, hashstr);
sprintf(depcache_filename, ".bam/%s", hashstr);
sprintf(scancache_filename, ".bam/scancache_%s", hashstr);
event_begin(0, "depcache load", depcache_filename);
context.depcache = depcache_load(depcache_filename);
event_end(0, "depcache load", NULL);
event_begin(0, "scancache load", scancache_filename);
context.scancache = scancache_load(scancache_filename);
event_end(0, "scancache load", NULL);
event_begin(0, "outputcache load", outputcache_filename);
context.outputcache = outputcache_load(outputcache_filename, &outputcache_timestamp);
event_end(0, "outputcache load", NULL);
}
/* do the setup */
setup_error = bam_setup(&context, scriptfile, targets, num_targets);
/* done with the loopup heap */
mem_destroy(context.deferredheap);
/* close the lua state */
lua_close(context.lua);
/* time after script has run to completion etc */
context.postsetuptime = timestamp();
/* do actions if we don't have any errors */
if(!setup_error)
{
event_begin(0, "prepare", NULL);
build_error = context_build_prepare(&context);
event_end(0, "prepare", NULL);
if(!build_error)
{
event_begin(0, "prioritize", NULL);
build_error = context_build_prioritize(&context);
event_end(0, "prioritize", NULL);
}
/* start verification */
if(option_debug_verify)
{
/* special handle of . */
if(strcmp(option_debug_verify, ".") == 0)
context.verifystate = verify_create("");
else
context.verifystate = verify_create(option_debug_verify);
verify_update(context.verifystate, verify_callback_null, NULL);
}
if(!build_error)
{
if(option_debug_nodes) /* debug dump all nodes detailed */
node_debug_dump(context.graph, 0);
else if(option_debug_nodes_html) /* debug dump all nodes detailed as html*/
node_debug_dump(context.graph, 1);
else if(option_debug_joblist) /* debug dumps the joblist */
context_dump_joblist(&context);
else if(option_dry)
{
}
else
{
/* run build or clean */
if(option_clean)
{
event_begin(0, "clean", NULL);
build_error = context_build_clean(&context);
event_end(0, "end", NULL);
}
else
{
event_begin(0, "build", NULL);
build_error = context_build_make(&context);
event_end(0, "build", NULL);
report_done = 1;
}
/* save cache (thread?) */
if(option_no_cache == 0)
{
event_begin(0, "depcache save", depcache_filename);
depcache_save(depcache_filename, context.graph);
event_end(0, "depcache save", NULL);
event_begin(0, "scancache save", scancache_filename);
scancache_save(scancache_filename, context.graph);
event_end(0, "scancache save", NULL);
event_begin(0, "outputcache save", outputcache_filename);
outputcache_save(outputcache_filename, context.outputcache, context.graph, outputcache_timestamp);
event_end(0, "outputcache save", NULL);
}
}
}
}
/* clean up */
mem_destroy(context.graphheap);
free(context.joblist);
depcache_free(context.depcache);
statcache_free(context.statcache);
if(context.verifystate)
{
verify_destroy(context.verifystate);
}
/* print final report and return */
if(setup_error)
{
/* no error message on setup error, it reports fine itself */
return setup_error;
}
else if(build_error)
printf("%s: error: a build step failed\n", session.name);
else if(report_done)
{
if(context.num_jobs == 0)
printf("%s: targets are up to date already\n", session.name);
else
{
time_t s = time(0x0) - starttime;
if(s <= 1)
printf("%s: done\n", session.name);
else
printf("%s: done (%d:%.2d)\n", session.name, (int)(s/60), (int)(s%60));
}
}
return build_error;
}
/* signal handler */
static void abortsignal(int i)
{
static unsigned print = 1;
(void)i;
if(print)
printf("%s: signal caught, waiting for jobs to finish\n", session.name);
print = 0;
session.abort = 1;
}
void install_abort_signal()
{
install_signals(abortsignal);
}
/* */
static void print_help(int mask)
{
int j;
printf("usage: %s [<options>] [<variables>=<values>] [<targets>]\n", session.name);
for(j = 0; options[j].sw; j++)
{
if(options[j].flags&mask)
printf(" %-25s %s\n", options[j].sw, options[j].desc);
}
printf("\n");
printf("bam version " BAM_VERSION_STRING_COMPLETE " using " LUA_RELEASE "\n");
printf("\n");
}
static int parse_parameters(int num, char **params)
{
int i, j, restargs = 0;
/* parse parameters */
for(i = 0; i < num; ++i)
{
for(j = 0; options[j].sw; j++)
{
if(strcmp(params[i], options[j].sw) == 0)
{
if(options[j].s)
{
if(i+1 >= num)
{
printf("%s: you must supply a argument with %s\n", session.name, options[j].sw);
return -1;
}
i++;
*options[j].s = params[i];
}
else
*options[j].v = 1;