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 $absolute = substr($tf, 0, 1) eq "/";
152 my $nf = $absolute ?
$tf : abs_path
($path) . "/" . $tf;
153 printc
("red", "[RcB] warning: $tf not found, continuing...\n"), return if !defined($nf);
156 if ($nf =~ /^\// && $nf !~ /\
.h
$/) {
157 $nf = make_relative
($this_path, $nf);
159 die "problem processing $self, $path, $tf" if(!defined($nf));
161 my @deps = glob($nf);
163 scandep_doit
($self, $d);
166 scandep_doit
($self, $nf);
171 my $forcerebuild = 0;
175 my $mainfile = undef;
176 my $ignore_errors = 0;
177 my $debug_cflags = 0;
180 my ($path, $file) = @_;
182 my $self = $path . "/" . $file;
187 open($fp, "<", $self) or die "could not open file $self: $!";
190 if ($line =~ /^\/\
/RcB: (\w{3,7}) \"(.+?)\"/) {
193 if($command eq "DEP") {
194 next if $skipinclude;
196 print "found RcB DEP $self -> $tf\n" if $verbose;
197 scandep
($self, $path, $tf);
198 } elsif ($command eq "LINK") {
199 next if $skipinclude;
200 print "found RcB LINK $self -> $arg\n" if $verbose;
202 } elsif ($command eq "SKIPON") {
203 $skipinclude = 1 if $cflags =~ /-D\Q$arg\E/;
204 } elsif ($command eq "SKIPOFF") {
205 $skipinclude = 0 if $cflags =~ /-D\Q$arg\E/;
207 } elsif($line =~ /^\s*#\s*include\s+\"([\w\.\/_\
-]+?
)\"/) {
209 next if file_ext
($tf) eq ".c";
211 print "skipping $self -> $tf\n" if $verbose;
214 print "found header ref $self -> $tf\n" if $verbose;
215 scandep
($self, $path, $tf);
222 push @adep, $self if $file =~ /[\w_-]+\.[c]{1}$/; #only add .c files to deps...
226 my $arg1 = shift @ARGV or syntax
;
227 if($arg1 eq "--force") {
230 } elsif($arg1 eq "--verbose") {
233 } elsif($arg1 eq "--new") {
236 } elsif($arg1 eq "--step") {
239 } elsif($arg1 eq "--ignore-errors") {
242 } elsif($arg1 eq "--debug") {
249 $mainfile = shift unless defined($mainfile);
250 syntax
unless defined($mainfile);
253 if (defined($ENV{CC
})) {
257 printc
"blue", "[RcB] \$CC not set, defaulting to cc\n";
260 process_include_dirs
();
262 $cflags .= $debug_cflags ?
" -O0 -g3" : "";
265 if (defined($ENV{NM
})) {
269 printc
"blue", "[RcB] \$NM not set, defaulting to nm\n";
274 printc
"magenta", "[CC] ", $cmdline, "\n";
275 my $reslt = `$cmdline 2>&1`;
277 printc
"red", "ERROR ", $!, "\n";
284 $link = expandarr
(@ARGV) . " ";
286 my $cnd = name_wo_ext
($mainfile);
287 my $cndo = $cnd . "o";
288 my $bin = $cnd . "out";
290 my $cfgn = name_wo_ext
($mainfile) . "rcb";
291 my $haveconfig = (-e
$cfgn);
292 if($haveconfig && !$ignore_rcb) {
293 printc
"blue", "[RcB] config file found. trying single compile.\n";
294 @adep = `cat $cfgn | grep "^DEP " | cut -b 5-`;
295 my @rcb_links = `cat $cfgn | grep "^LINK" | cut -b 6-`;
296 my $cs = expandarr
(@adep);
297 my $ls = expandarr
(@rcb_links);
298 $link = $ls if (defined($ls) && $ls ne "");
299 my $res = compile
("$cc $cflags $cs $link -o $bin");
300 if($res =~ /undefined reference to/) {
301 printc
"red", "[RcB] undefined reference[s] found, switching to scan mode\n";
304 printc
"red", "[RcB] error. exiting.\n";
306 printc
"green", "[RcB] success. $bin created.\n";
312 printc
"blue", "[RcB] scanning deps...";
314 scanfile dirname
(abs_path
($mainfile)), basename
($mainfile);
316 printc
"green", "done\n";
319 printc
"blue", "[RcB] compiling main file...\n";
320 my $op = compile
("$cc $cflags -c $mainfile -o $cndo");
321 exit 1 if($op =~ /error:/g);
335 printc
"blue", "[RcB] resolving linker deps...\n";
338 if($i + 1 >= @adep) { #last element of the array is the already build mainfile
343 %glob_missym = %missym, last unless $relink;
345 my %missym_old = %missym;
347 my $ex = expandhash
(\
%obj);
348 printc
"blue", "[RcB] trying to link ...\n";
349 my $cmd = "$cc $cflags $ex $link -o $bin";
350 printc
"cyan", "[LD] ", $cmd, "\n";
353 if(/undefined reference to [\'\`\"]{1}([\w\._]+)[\'\`\"]{1}/) {
358 /([\/\w\
._\
-]+): file
not recognized
: File format
not recognized
/ ||
359 /architecture of input file [\'\`\"]{1}([\/\w\
._\
-]+)[\'\
`\"]{1} is incompatible with/ ||
360 /fatal error: ([\/\w\._\-]+): unsupported ELF machine number/ ||
361 /ld: ([\/\w\._\-]+): Relocations in generic ELF/
364 $i = delete $obj{$cnd};
365 printc "red", "[RcB] incompatible object file $cnd, rebuilding...\n";
370 %missym = %missym_old;
373 /collect2: ld returned 1 exit status/ ||
374 /collect2: error: ld returned 1 exit status/ ||
375 /In function [\'\`\"]{1}[\w_
]+[\'\
`\"]{1}:/ ||
376 /more undefined references to/
379 printc "red", "[RcB] Warning: unexpected linker output!\n";
382 if(!scalar(keys %missym)) {
390 goto skip unless defined $cnd;
394 $cndo = name_wo_ext($cnd) . "o";
395 if(($forcerebuild || $rebuildflag || ! -e $cndo) && !defined($rebuilt{$cndo})) {
396 my $op = compile("$cc $cflags -c $cnd -o $cndo");
397 if($op =~ /error:/) {
398 exit 1 unless($ignore_errors);
403 @opa = `$nm -g
$cndo 2>&1`;
407 if(/[\da-fA-F]{8,16}\s+[TWRBCD]{1}\s+([\w_]+)/) {
409 $symhash{$symname} = 1;
411 } elsif (/File format not recognized/) {
412 printc "red", "[RcB] nm doesn't recognize the format of $cndo, rebuilding...\n";
418 $sym{$cndo} = \%symhash;
421 if(defined($symhash{$_})) {
425 if($objfail || $step) {
428 printc "red", "[RcB] adding $cndo to the bunch...\n" if $step;
439 printc "red", "[RcB] failed to resolve the following symbols, check your DEP tags\n";
440 for(keys %glob_missym) {
444 printc "green", "[RcB] success. $bin created.\n";
445 printc "blue", "saving required dependencies to $cfgn\n";
447 open($fh, ">", $cfgn);
449 print { $fh } "DEP ", name_wo_ext($_), "c\n";
451 print { $fh } "LINK ", $link, "\n" if($link);