7 #use Data::Dump qw(dump);
10 die "syntax: $0 [--force --verbose] mainfile.c [-lc -lm -lncurses]\n--force will force a complete rebuild despite object file presence.";
36 $l-- while($l && substr($x, $l, 1) ne ".");
37 return substr($x, 0, $l + 1) if($l);
53 my $colstr = "\033[%dm";
60 printf $colstr, $colors->{$color};
64 printf $colstr, $colors->{"end"};
69 my $np = dirname
($nf);
70 my $nb = basename
($nf);
71 if(!defined($hdep{$nf})) {
73 printc
("red", "failed to find dependency $nf referenced from $self!\n");
74 die unless $nf =~ /\.h$/;
82 my ($self, $path, $tf) = @_;
83 my $absolute = substr($tf, 0, 1) eq "/";
84 my $nf = $absolute ?
$tf : abs_path
($path . "/" . $tf);
85 die "problem processing $self, $path, $tf" if(!defined($nf));
89 scandep_doit
($self, $d);
92 scandep_doit
($self, $nf);
97 my ($path, $file) = @_;
99 my $self = $path . "/" . $file;
103 open($fp, "<", $self) or die "could not open file $self: $!";
106 if ($line =~ /^\/\
/RcB: (\w{3,6}) \"(.+?)\"/) {
109 if($command eq "DEP") {
111 scandep
($self, $path, $tf);
113 } elsif($line =~ /^\s*#\s*include\s+\"([\w\.\/_\
-]+?
)\"/) {
115 scandep
($self, $path, $tf);
122 push @adep, $self if $file =~ /[\w_-]+\.[c]{1}$/; #only add .c files to deps...
125 my $forcerebuild = 0;
127 my $mainfile = undef;
129 my $arg1 = shift @ARGV or syntax
;
130 if($arg1 eq "--force") {
133 } elsif($arg1 eq "--verbose") {
139 $mainfile = shift unless defined($mainfile);
140 syntax
unless defined($mainfile);
143 if (defined($ENV{CC
})) {
147 printc
"blue", "[RcB] \$CC not set, defaulting to cc\n";
149 my $cflags = defined($ENV{CFLAGS
}) ?
$ENV{CFLAGS
} : "";
151 if (defined($ENV{NM
})) {
155 printc
"blue", "[RcB] \$NM not set, defaulting to nm\n";
160 printc
"magenta", "[CC] ", $cmdline, "\n";
161 my $reslt = `$cmdline 2>&1`;
166 my $link = expandarr
(@ARGV);
168 my $cnd = name_wo_ext
($mainfile);
169 my $cndo = $cnd . "o";
170 my $bin = $cnd . "out";
172 my $cfgn = name_wo_ext
($mainfile) . "rcb";
173 my $haveconfig = (-e
$cfgn);
175 printc
"blue", "[RcB] config file found. trying single compile.\n";
177 my $cs = expandarr
(@adep);
178 my $res = compile
("$cc $cflags $cs $link -o $bin");
179 if($res =~ /undefined reference to/) {
180 printc
"red", "[RcB] undefined reference[s] found, switching to scan mode\n";
184 printc
"red", "[RcB] error. exiting.\n";
186 printc
"green", "[RcB] success. $bin created.\n";
192 printc
"blue", "[RcB] scanning deps...";
194 scanfile dirname
(abs_path
($mainfile)), basename
($mainfile);
196 printc
"green", "done\n";
199 printc
"blue", "[RcB] compiling main file...\n";
200 my $op = compile
("$cc $cflags -c $mainfile -o $cndo");
201 exit 1 if($op =~ /error:/g);
215 printc
"blue", "[RcB] resolving linker deps...\n";
218 if($i + 1 >= @adep) { #last element of the array is the already build mainfile
223 @glob_missym = @missym, last unless $relink;
225 my @missym_old = @missym;
227 my $ex = expandhash
(\
%obj);
228 printc
"blue", "[RcB] trying to link ...\n";
229 my $cmd = "$cc $cflags $ex $link -o $bin";
230 printc
"cyan", "[LD] ", $cmd, "\n";
233 if(/undefined reference to [\'\`\"]{1}([\w\._]+)[\'\`\"]{1}/) {
238 /architecture of input file [\'\`\"]{1}([\w\.\/_
]+)[\'\
`\"]{1} is incompatible with/ ||
239 /fatal error: ([\w\.\/_]+): unsupported ELF machine number/
242 $i = delete $obj{$cnd};
243 printc "red", "[RcB] incompatible object file $cnd, rebuilding...\n";
248 @missym = @missym_old;
251 /collect2: ld returned 1 exit status/ ||
252 /In function [\'\`\"]{1}[\w_
]+[\'\
`\"]{1}:/ ||
253 /more undefined references to/
256 printc "red", "[RcB] FATAL: unexpected linker output!\n";
261 if(!scalar(@missym)) {
269 goto skip unless defined $cnd;
273 $cndo = name_wo_ext($cnd) . "o";
274 if(($forcerebuild || $rebuildflag || ! -e $cndo) && !defined($rebuilt{$cndo})) {
275 my $op = compile("$cc $cflags -c $cnd -o $cndo");
276 exit 1 if($op =~ /error:/);
279 @opa = `$nm -g
$cndo 2>&1`;
283 if(/[\da-fA-F]{8,16}\s+[TW]{1}\s+([\w_]+)/) {
285 $symhash{$symname} = 1;
287 } elsif (/File format not recognized/) {
288 printc "red", "[RcB] nm doesn't recognize the format of $cndo, rebuilding...\n";
294 $sym{$cndo} = \%symhash;
297 if(defined($symhash{$_})) {
314 printc "red", "[RcB] failed to resolve the following symbols, check your DEP tags\n";
319 printc "green", "[RcB] success. $bin created.\n";
320 printc "blue", "saving required dependencies to $cfgn\n";
322 open($fh, ">", $cfgn);
324 print { $fh } name_wo_ext($_), "c\n";