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";
75 sub process_include_dirs
{
76 my @p = split / /, $cflags;
78 push @includedirs, ""; # empty entry for the first iteration in scandep_doit
79 for($i = 0; $i < scalar(@p); $i++) {
82 push @includedirs, $p[$i];
83 } elsif($p[$i] =~ /^\-I(.+)/) {
84 push @includedirs, $1;
91 printf $colstr, $colors->{$color};
95 printf $colstr, $colors->{"end"};
100 my $is_header = ($na =~ /\.h$/);
101 for my $i (@includedirs) {
102 my $delim = ($i eq "") ?
"" : "/";
103 my $nf = $i . $delim . $na;
104 my $np = dirname
($nf);
105 my $nb = basename
($nf);
106 if(!defined($hdep{$nf})) {
114 last unless $is_header;
116 printc
("red", "failed to find dependency $na referenced from $self!\n");
117 die unless $is_header;
121 my ($basepath, $relpath) = @_;
122 #print "$basepath ::: $relpath\n";
123 die "both path's must start with /" if(substr($basepath, 0, 1) ne "/" || substr($relpath, 0, 1) ne "/");
124 $basepath .= "/" if($basepath !~ /\
/$/ && -d
$basepath);
125 $relpath .= "/" if($relpath !~ /\
/$/ && -d
$relpath);
129 $l++ while(substr($basepath, $l, 1) eq substr($relpath, $l, 1));
131 $l-- while(substr($basepath, $l, 1) ne "/");
133 $l++ if substr($relpath, $l, 1) eq "/";
134 my $res = substr($relpath, $l);
136 while($l2 < length($basepath)) {
137 $sl++ if substr($basepath, $l2, 1) eq "/";
141 for ($i = 0; $i < $sl; $i++) {
149 my ($self, $path, $tf) = @_;
150 my $is_header = ($tf =~ /\.h$/);
151 my $absolute = substr($tf, 0, 1) eq "/";
153 my $fullpath = abs_path
($path) . "/" . $tf;
154 # the stuff in the || () part is to pass headers which are in the CFLAGS include path
155 # unmodified to scandep_doit
156 my $nf = $absolute || ($is_header && $tf !~ /^\./ && ! -e
$fullpath) ?
$tf : $fullpath;
157 printc
("red", "[RcB] warning: $tf not found, continuing...\n"), return if !defined($nf);
160 if ($nf =~ /^\// && ! $is_header) {
161 $nf = make_relative
($this_path, $nf);
163 die "problem processing $self, $path, $tf" if(!defined($nf));
165 my @deps = glob($nf);
167 scandep_doit
($self, $d);
170 scandep_doit
($self, $nf);
175 my $forcerebuild = 0;
179 my $mainfile = undef;
180 my $ignore_errors = 0;
181 my $debug_cflags = 0;
184 my ($path, $file) = @_;
186 my $self = $path . "/" . $file;
191 open($fp, "<", $self) or die "could not open file $self: $!";
194 if ($line =~ /^\/\
/RcB: (\w{3,7}) \"(.+?)\"/) {
197 if($command eq "DEP") {
198 next if $skipinclude;
200 print "found RcB DEP $self -> $tf\n" if $verbose;
201 scandep
($self, $path, $tf);
202 } elsif ($command eq "LINK") {
203 next if $skipinclude;
204 print "found RcB LINK $self -> $arg\n" if $verbose;
206 } elsif ($command eq "SKIPON") {
207 $skipinclude = 1 if $cflags =~ /-D\Q$arg\E/;
208 } elsif ($command eq "SKIPOFF") {
209 $skipinclude = 0 if $cflags =~ /-D\Q$arg\E/;
211 } elsif($line =~ /^\s*#\s*include\s+\"([\w\.\/_\
-]+?
)\"/) {
213 next if file_ext
($tf) eq ".c";
215 print "skipping $self -> $tf\n" if $verbose;
218 print "found header ref $self -> $tf\n" if $verbose;
219 scandep
($self, $path, $tf);
226 push @adep, $self if $file =~ /[\w_-]+\.[c]{1}$/; #only add .c files to deps...
230 my $arg1 = shift @ARGV or syntax
;
231 if($arg1 eq "--force") {
234 } elsif($arg1 eq "--verbose") {
237 } elsif($arg1 eq "--new") {
240 } elsif($arg1 eq "--step") {
243 } elsif($arg1 eq "--ignore-errors") {
246 } elsif($arg1 eq "--debug") {
253 $mainfile = shift unless defined($mainfile);
254 syntax
unless defined($mainfile);
257 if (defined($ENV{CC
})) {
261 printc
"blue", "[RcB] \$CC not set, defaulting to cc\n";
264 process_include_dirs
();
266 $cflags .= $debug_cflags ?
" -O0 -g3" : "";
269 if (defined($ENV{NM
})) {
273 printc
"blue", "[RcB] \$NM not set, defaulting to nm\n";
278 printc
"magenta", "[CC] ", $cmdline, "\n";
279 my $reslt = `$cmdline 2>&1`;
281 printc
"red", "ERROR ", $!, "\n";
288 $link = expandarr
(@ARGV) . " ";
290 my $cnd = name_wo_ext
($mainfile);
291 my $cndo = $cnd . "o";
292 my $bin = $cnd . "out";
294 my $cfgn = name_wo_ext
($mainfile) . "rcb";
295 my $haveconfig = (-e
$cfgn);
296 if($haveconfig && !$ignore_rcb) {
297 printc
"blue", "[RcB] config file found. trying single compile.\n";
298 @adep = `cat $cfgn | grep "^DEP " | cut -b 5-`;
299 my @rcb_links = `cat $cfgn | grep "^LINK" | cut -b 6-`;
300 my $cs = expandarr
(@adep);
301 my $ls = expandarr
(@rcb_links);
302 $link = $ls if (defined($ls) && $ls ne "");
303 my $res = compile
("$cc $cflags $cs $link -o $bin");
304 if($res =~ /undefined reference to/) {
305 printc
"red", "[RcB] undefined reference[s] found, switching to scan mode\n";
308 printc
"red", "[RcB] error. exiting.\n";
310 printc
"green", "[RcB] success. $bin created.\n";
316 printc
"blue", "[RcB] scanning deps...";
318 scanfile dirname
(abs_path
($mainfile)), basename
($mainfile);
320 printc
"green", "done\n";
323 printc
"blue", "[RcB] compiling main file...\n";
324 my $op = compile
("$cc $cflags -c $mainfile -o $cndo");
325 exit 1 if($op =~ /error:/g);
339 printc
"blue", "[RcB] resolving linker deps...\n";
342 if($i + 1 >= @adep) { #last element of the array is the already build mainfile
347 %glob_missym = %missym, last unless $relink;
349 my %missym_old = %missym;
351 my $ex = expandhash
(\
%obj);
352 printc
"blue", "[RcB] trying to link ...\n";
353 my $cmd = "$cc $cflags $ex $link -o $bin";
354 printc
"cyan", "[LD] ", $cmd, "\n";
357 if(/undefined reference to [\'\`\"]{1}([\w\._]+)[\'\`\"]{1}/) {
362 /([\/\w\
._\
-]+): file
not recognized
: File format
not recognized
/ ||
363 /architecture of input file [\'\`\"]{1}([\/\w\
._\
-]+)[\'\
`\"]{1} is incompatible with/ ||
364 /fatal error: ([\/\w\._\-]+): unsupported ELF machine number/ ||
365 /ld: ([\/\w\._\-]+): Relocations in generic ELF/
368 $i = delete $obj{$cnd};
369 printc "red", "[RcB] incompatible object file $cnd, rebuilding...\n";
374 %missym = %missym_old;
377 /collect2: ld returned 1 exit status/ ||
378 /collect2: error: ld returned 1 exit status/ ||
379 /In function [\'\`\"]{1}[\w_
]+[\'\
`\"]{1}:/ ||
380 /more undefined references to/
383 printc "red", "[RcB] Warning: unexpected linker output!\n";
386 if(!scalar(keys %missym)) {
394 goto skip unless defined $cnd;
398 $cndo = name_wo_ext($cnd) . "o";
399 if(($forcerebuild || $rebuildflag || ! -e $cndo) && !defined($rebuilt{$cndo})) {
400 my $op = compile("$cc $cflags -c $cnd -o $cndo");
401 if($op =~ /error:/) {
402 exit 1 unless($ignore_errors);
407 @opa = `$nm -g
$cndo 2>&1`;
411 if(/[\da-fA-F]{8,16}\s+[TWRBCD]{1}\s+([\w_]+)/) {
413 $symhash{$symname} = 1;
415 } elsif (/File format not recognized/) {
416 printc "red", "[RcB] nm doesn't recognize the format of $cndo, rebuilding...\n";
422 $sym{$cndo} = \%symhash;
425 if(defined($symhash{$_})) {
429 if($objfail || $step) {
432 printc "red", "[RcB] adding $cndo to the bunch...\n" if $step;
443 printc "red", "[RcB] failed to resolve the following symbols, check your DEP tags\n";
444 for(keys %glob_missym) {
448 printc "green", "[RcB] success. $bin created.\n";
449 printc "blue", "saving required dependencies to $cfgn\n";
451 open($fh, ">", $cfgn);
453 print { $fh } "DEP ", name_wo_ext($_), "c\n";
455 print { $fh } "LINK ", $link, "\n" if($link);