7 #use Data::Dump qw(dump);
9 my $this_path = abs_path
();
10 my $cflags = defined($ENV{CFLAGS
}) ?
$ENV{CFLAGS
} : "";
13 die "syntax: $0 [--new --force --verbose --step --ignore-errors] mainfile.c [-lc -lm -lncurses]\n" .
14 "--new will ignore an existing .rcb file and rescan the deps\n" .
15 "--force will force a complete rebuild despite object file presence.\n" .
16 "--verbose will print the complete linker output and other info\n" .
17 "--debug adds -O0 -g3 to CFLAGS\n" .
18 "--step will add one dependency after another, to help finding hidden deps\n";
44 $l-- while($l && substr($x, $l, 1) ne ".");
45 return substr($x, 0, $l + 1) if($l);
52 $l-- while($l && substr($x, $l, 1) ne ".");
53 return substr($x, $l) if($l);
69 my $colstr = "\033[%dm";
76 printf $colstr, $colors->{$color};
80 printf $colstr, $colors->{"end"};
85 my $np = dirname
($nf);
86 my $nb = basename
($nf);
87 if(!defined($hdep{$nf})) {
89 printc
("red", "failed to find dependency $nf referenced from $self!\n");
90 die unless $nf =~ /\.h$/;
98 my ($basepath, $relpath) = @_;
99 #print "$basepath ::: $relpath\n";
100 die "both path's must start with /" if(substr($basepath, 0, 1) ne "/" || substr($relpath, 0, 1) ne "/");
101 $basepath .= "/" if($basepath !~ /\
/$/ && -d
$basepath);
102 $relpath .= "/" if($relpath !~ /\
/$/ && -d
$relpath);
106 $l++ while(substr($basepath, $l, 1) eq substr($relpath, $l, 1));
108 $l-- while(substr($basepath, $l, 1) ne "/");
110 $l++ if substr($relpath, $l, 1) eq "/";
111 my $res = substr($relpath, $l);
113 while($l2 < length($basepath)) {
114 $sl++ if substr($basepath, $l2, 1) eq "/";
118 for ($i = 0; $i < $sl; $i++) {
126 my ($self, $path, $tf) = @_;
127 my $absolute = substr($tf, 0, 1) eq "/";
129 my $nf = $absolute ?
$tf : abs_path
($path) . "/" . $tf;
130 printc
("red", "[RcB] warning: $tf not found, continuing...\n"), return if !defined($nf);
133 if ($nf =~ /^\// && $nf !~ /\
.h
$/) {
134 $nf = make_relative
($this_path, $nf);
136 die "problem processing $self, $path, $tf" if(!defined($nf));
138 my @deps = glob($nf);
140 scandep_doit
($self, $d);
143 scandep_doit
($self, $nf);
148 my $forcerebuild = 0;
152 my $mainfile = undef;
153 my $ignore_errors = 0;
154 my $debug_cflags = 0;
157 my ($path, $file) = @_;
159 my $self = $path . "/" . $file;
164 open($fp, "<", $self) or die "could not open file $self: $!";
167 if ($line =~ /^\/\
/RcB: (\w{3,7}) \"(.+?)\"/) {
170 if($command eq "DEP") {
171 next if $skipinclude;
173 print "found RcB DEP $self -> $tf\n" if $verbose;
174 scandep
($self, $path, $tf);
175 } elsif ($command eq "LINK") {
176 next if $skipinclude;
177 print "found RcB LINK $self -> $arg\n" if $verbose;
179 } elsif ($command eq "SKIPON") {
180 $skipinclude = 1 if $cflags =~ /-D\Q$arg\E/;
181 } elsif ($command eq "SKIPOFF") {
182 $skipinclude = 0 if $cflags =~ /-D\Q$arg\E/;
184 } elsif($line =~ /^\s*#\s*include\s+\"([\w\.\/_\
-]+?
)\"/) {
186 next if file_ext
($tf) eq ".c";
188 print "skipping $self -> $tf\n" if $verbose;
191 print "found header ref $self -> $tf\n" if $verbose;
192 scandep
($self, $path, $tf);
199 push @adep, $self if $file =~ /[\w_-]+\.[c]{1}$/; #only add .c files to deps...
203 my $arg1 = shift @ARGV or syntax
;
204 if($arg1 eq "--force") {
207 } elsif($arg1 eq "--verbose") {
210 } elsif($arg1 eq "--new") {
213 } elsif($arg1 eq "--step") {
216 } elsif($arg1 eq "--ignore-errors") {
219 } elsif($arg1 eq "--debug") {
226 $mainfile = shift unless defined($mainfile);
227 syntax
unless defined($mainfile);
230 if (defined($ENV{CC
})) {
234 printc
"blue", "[RcB] \$CC not set, defaulting to cc\n";
237 $cflags .= $debug_cflags ?
" -O0 -g3" : "";
240 if (defined($ENV{NM
})) {
244 printc
"blue", "[RcB] \$NM not set, defaulting to nm\n";
249 printc
"magenta", "[CC] ", $cmdline, "\n";
250 my $reslt = `$cmdline 2>&1`;
252 printc
"red", "ERROR ", $!, "\n";
259 $link = expandarr
(@ARGV) . " ";
261 my $cnd = name_wo_ext
($mainfile);
262 my $cndo = $cnd . "o";
263 my $bin = $cnd . "out";
265 my $cfgn = name_wo_ext
($mainfile) . "rcb";
266 my $haveconfig = (-e
$cfgn);
267 if($haveconfig && !$ignore_rcb) {
268 printc
"blue", "[RcB] config file found. trying single compile.\n";
269 @adep = `cat $cfgn | grep "^DEP " | cut -b 5-`;
270 my @rcb_links = `cat $cfgn | grep "^LINK" | cut -b 6-`;
271 my $cs = expandarr
(@adep);
272 my $ls = expandarr
(@rcb_links);
273 $link = $ls if (defined($ls) && $ls ne "");
274 my $res = compile
("$cc $cflags $cs $link -o $bin");
275 if($res =~ /undefined reference to/) {
276 printc
"red", "[RcB] undefined reference[s] found, switching to scan mode\n";
279 printc
"red", "[RcB] error. exiting.\n";
281 printc
"green", "[RcB] success. $bin created.\n";
287 printc
"blue", "[RcB] scanning deps...";
289 scanfile dirname
(abs_path
($mainfile)), basename
($mainfile);
291 printc
"green", "done\n";
294 printc
"blue", "[RcB] compiling main file...\n";
295 my $op = compile
("$cc $cflags -c $mainfile -o $cndo");
296 exit 1 if($op =~ /error:/g);
310 printc
"blue", "[RcB] resolving linker deps...\n";
313 if($i + 1 >= @adep) { #last element of the array is the already build mainfile
318 %glob_missym = %missym, last unless $relink;
320 my %missym_old = %missym;
322 my $ex = expandhash
(\
%obj);
323 printc
"blue", "[RcB] trying to link ...\n";
324 my $cmd = "$cc $cflags $ex $link -o $bin";
325 printc
"cyan", "[LD] ", $cmd, "\n";
328 if(/undefined reference to [\'\`\"]{1}([\w\._]+)[\'\`\"]{1}/) {
333 /([\/\w\
._\
-]+): file
not recognized
: File format
not recognized
/ ||
334 /architecture of input file [\'\`\"]{1}([\/\w\
._\
-]+)[\'\
`\"]{1} is incompatible with/ ||
335 /fatal error: ([\/\w\._\-]+): unsupported ELF machine number/ ||
336 /ld: ([\/\w\._\-]+): Relocations in generic ELF/
339 $i = delete $obj{$cnd};
340 printc "red", "[RcB] incompatible object file $cnd, rebuilding...\n";
345 %missym = %missym_old;
348 /collect2: ld returned 1 exit status/ ||
349 /collect2: error: ld returned 1 exit status/ ||
350 /In function [\'\`\"]{1}[\w_
]+[\'\
`\"]{1}:/ ||
351 /more undefined references to/
354 printc "red", "[RcB] Warning: unexpected linker output!\n";
357 if(!scalar(keys %missym)) {
365 goto skip unless defined $cnd;
369 $cndo = name_wo_ext($cnd) . "o";
370 if(($forcerebuild || $rebuildflag || ! -e $cndo) && !defined($rebuilt{$cndo})) {
371 my $op = compile("$cc $cflags -c $cnd -o $cndo");
372 if($op =~ /error:/) {
373 exit 1 unless($ignore_errors);
378 @opa = `$nm -g
$cndo 2>&1`;
382 if(/[\da-fA-F]{8,16}\s+[TWRBCD]{1}\s+([\w_]+)/) {
384 $symhash{$symname} = 1;
386 } elsif (/File format not recognized/) {
387 printc "red", "[RcB] nm doesn't recognize the format of $cndo, rebuilding...\n";
393 $sym{$cndo} = \%symhash;
396 if(defined($symhash{$_})) {
400 if($objfail || $step) {
403 printc "red", "[RcB] adding $cndo to the bunch...\n" if $step;
414 printc "red", "[RcB] failed to resolve the following symbols, check your DEP tags\n";
415 for(keys %glob_missym) {
419 printc "green", "[RcB] success. $bin created.\n";
420 printc "blue", "saving required dependencies to $cfgn\n";
422 open($fh, ">", $cfgn);
424 print { $fh } "DEP ", name_wo_ext($_), "c\n";
426 print { $fh } "LINK ", $link, "\n" if($link);