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
|
#!/usr/bin/perl
# Copyright (C) 2006-2010 Thomas Zander <zander@kde.org>
$COMMAND = "unsercmake";
$VERSION = "$COMMAND 0.32";
# This script allows you to build a target library including dependency checking, but it
# limits the dependency checking to the current dir and its subdirs meaning a lot of
# unwanted steps are skipped.
# user settings...
# if we assume the user has something like: /kde/project as a sourcedir and /kde/current/project
# as a build dir then we can auto-switch between those dirs
# Please alter the variable to set the basedir and the builddir. Like this:
# BASE=BUILD
# Examples:
# $buildDirType = "/kde=/kde/current";
# $buildDirType = "kde/{project}=kde/{project}/build";
$buildDirType = "";
##########################
# Copyright (c) 2006-2010 Thomas Zander <zander@kde.org>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License,
# or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
if ($buildDirType eq "") {
print "Not setup!\n";
print " This tool can automatically switch from your source dir to the builddir.\n";
print " To do this correctly you have to alter the $COMMAND using your editor\n";
print " and alter the buildDirType variable according to the comments.\n";
exit;
}
#argument options
$argMap{"C"}="directory";
$argMap{"j"}="jobs";
$argMap{"n"}="just-print";
$argMap{"v"}="verbose";
$argMap{"k"}="keep-going";
$argMap{"p"}="projecthelp";
$argMap{"h"}="help";
$argMap{"e"}="exclude";
$argMap{"q"}="quiet";
@options=split(" ", "help directory jobs just-print verbose keep-going version projecthelp exclude quiet");
@argCommands=split(" ","directory jobs exclude");
&parseArguments(@ARGV);
if(defined $help) {
print "Usage: $COMMAND [OPTION] ...\n";
print "Available options:\n\n";
print " -h, --help This help.\n";
print " -p --projecthelp print project help information\n";
print " -j, --jobs=N Allow N parallel jobs.\n";
print " -k, --keep-going Keep going when some targets can't be made.\n";
print " -C, --directory=dir Change to directory dir before doing anything.\n";
print " -v, --verbose Show verbose output.\n";
print " --version Show version information and copyright notice.\n";
print " -n --just-print Only print out the commands to call.\n";
print " -q --quiet Don't print details\n";
print " -e --exclude=RegEx Exclude libraries and subdirs based on a regular expression\n";
exit;
}
if(defined $version) {
print "$VERSION\nWritten by Thomas Zander\n\nThis is free software see the source for copying conditions. There is NO\nwarranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n";
exit;
}
if(defined $directory) {
chdir $directory || die "Failed to change directory to '$directory'\n";
}
while(! -f "CMakeFiles" && ! -f "CMakeLists.txt") {
# for the case where there is a subdir but that subdir does not have a cmakefile
chdir ".." || die "Error: Can not find a cmake based basedir\nPlease start from the source dir\n";
if(`pwd` eq "/\n") {
die "Error: Can not find a cmake based basedir\nPlease start from the source dir\n";
}
}
$dir=`pwd`;
chomp($dir);
$builddir = "$dir/";
# find the build-dir where the Makefile lives
@bd = split("=", $buildDirType);
if($#bd > 0) {
# make sure slashes are all round
$bd[0]="/$bd[0]/";
$bd[1]="/$bd[1]/";
$bd[0]=~s/\/\//\//g;
$bd[1]=~s/\/\//\//g;
my $wildcard="";
if($bd[0]=~/(\{.*\})/) { # used {project} wildcard
$wildcard=$1;
if($bd[1]=~/$wildcard/) {
$bd[0]=~s/$wildcard/(.*?)/;
}
}
if($builddir=~/$bd[0]/ && $builddir=~/$bd[1]/ == false) {
my $project=$wildcard;
$builddir=~/$bd[0]/;
if($builddir=~m/$bd[0]/) {
$project=$1;
}
$builddir=~s/$bd[0]/$bd[1]/;
if($wildcard ne "" && $project ne "") {
$builddir=~s/$wildcard/$project/;
}
#print "$dir => $builddir\n";
}
}
$makeDependNeeded = 0;
&readCmakeFile(".");
if(defined $projecthelp) {
print "\nLibraries:\n\n";
foreach $key (sort {uc($a) cmp uc($b)} @libraries) {
print " $key";
if(defined $exclude && $key=~/$exclude/) {
print " [excluded]";
}
print "\n";
}
exit;
}
$doInstall = 0;
if($#userArgs >= 0) { # limit to user request
foreach $lib (@userArgs) {
if($lib eq "install") {
$doInstall = 1;
if($#userArgs == 0) {
@targets = @libraries;
}
next;
}
my $pureLib = $lib;
$pureLib=~s/^[\/ \\]*//;
$pureLib=~s/[\/ \\]*$//;
if(&contains($pureLib, @libraries)) {
push @targets, $pureLib;
}
else {
push @nonLibTargets, $lib;
}
}
}
else {
@targets=@libraries;
}
foreach $lib (@targets) { # add dependencies
@deps = &dependencies(0, $lib);
LIB: foreach $l (@deps) {
if($l eq "ERROR") {
exit 1;
}
if(defined $exclude && $l=~/$exclude/) {
next;
}
foreach $x (@maketargets) {
if($l eq $x) {
next LIB;
}
}
push @maketargets, $l;
}
}
# cd to builddir
chdir $builddir || die "Can't find builddir '$builddir'\n";
if($makeDependNeeded) {
system ("make depend") == 0 or exit 1;
}
@actualTargets=`make help`;
foreach $target (@nonLibTargets) {
foreach $h (@actualTargets) {
if($h=~/\b$target\b/) {
push @maketargets, $target;
}
}
}
if($doInstall == 0 && $#maketargets < 0) {
print "No targets\n";
exit;
}
if(defined $jobs) {
$jobs="-j $jobs";
}
if(defined $verbose) {
$verbose="VERBOSE=0";
}
foreach $lib (@maketargets) {
chdir "$builddir/$locations{$lib}";
$dir = $locations{$lib};
$dir=~s/^\.\///;
if(&checkTargetAvail($lib) == 1) {
if(! defined $quiet) {
print "Building lib: $lib (in $dir)\n";
}
if(defined $just_print) {
next;
}
foreach $h (@actualTargets) {
if($h=~/(\b$lib\_automoc)/) {
system ("make $jobs $verbose $1");
last;
}
}
my $rc = system ("make $jobs $verbose $lib/fast");
if(!(defined $keep_going) && $rc != 0) {
exit 1;
}
}
elsif(! defined $quiet) {
print "Skipping lib: $lib (in $dir)\n";
}
}
# install!
if($doInstall == 1) {
if(! defined $quiet) {
print "Installing changed files";
#if($dir ne ".") { print " in '$dir'"; }
print "\n";
}
if(! defined $just_print) {
@output=`(cd $builddir && make install/fast)`;
my $line;
foreach $line (@output) {
if ($line=~/^-- (Installing: .*)$/) {
print "$1\n";
}
}
}
}
###############
sub readCmakeFile {
my $dir=shift(@_);
my $file="CMakeLists.txt";
if($makeDependNeeded == 0) {
my $buildAge = (stat("$builddir/$dir/CMakeFiles/CMakeDirectoryInformation.cmake"))[9];
my $fileAge = (stat("$dir/$file"))[9];
if($fileAge > $buildAge) {
$makeDependNeeded = 1;
print "makeDepend needed due to $dir\n";
}
}
my @subdirs;
open FILE, "<", "$dir/$file";
my $inLink=0;
my $lib = "";
foreach $line (<FILE>) {
if($inLink == 0 && $line=~/^\s*add_subdirectory\s*\((.*)\)/i) {
my $d=$1;
$d=~s/^\s*//; #chomp
$d=~s/\s*$//;
push @subdirs, $d;
}
my $libs = "";
if($inLink == 1) {
$libs = $line;
}
if($line=~/^\s*target_link_libraries\s*\((.*)/) {
$libs = $1;
$inLink=1;
}
if($inLink == 1 && $libs ne "") {
foreach $i (split(" ", $libs)) {
if($i=~/\)$/) { # incase no space was used
$i=~s/\)$//;
$inLink = 0;
}
if($i eq ")") { #incase a space was used
$inLink = 0;
}
elsif($inLink==1 && $lib eq "") { # a library is defined in this file
$lib = $i;
push @libraries, $lib;
#print "found lib: $lib\n";
$locations{$lib} = "$dir";
}
else { # and that libraries dependencies
$$lib .= "$i ";
#print " supportlib $i\n";
}
}
$libs ="";
}
if($inLink == 0) {
$lib = "";
}
}
close FILE;
# only go into subdir when the DartTestfile lists the subdir.
open FILE, "<", "$builddir/$dir/DartTestfile.txt";
foreach $line (<FILE>) {
if($line=~/^SUBDIRS\s*\((.*)\)/) {
my $subdir;
foreach $subdir (@subdirs) {
if(defined $exclude && "/$subdir/"=~/$exclude/) {
next;
}
if($line=~/\b$subdir\b/) {
&readCmakeFile("$dir/$subdir");
}
}
last;
}
}
close FILE;
}
sub dependencies() {
my $level=shift(@_);
my $target=shift(@_);
if($level > 12) {
print "ERROR; dependency checking hit 12 levels deep, probably a circular dependency, baling out\n";
print " dependencies traceback (bottom depends on top):\n";
print " $target\n";
return "ERROR";
}
#my $inset ="";
#for($i=0; $i < $level; $i++) { $inset .=" "; }
#print "$inset |dependencies of '$target'\n";
my @depend;
my $lib;
#print "$inset depsLine='$$target'\n";
foreach $lib (split(" ", $$target)) {
my $found=0;
my $l;
# if not in our libraries, skip it.
foreach $l (@libraries) {
if($l eq $lib) {
$found=1;
last;
}
}
if($found == 0) {
next;
}
#print "$inset has dependency $lib\n";
if(&contains($lib, @depend) == 0) {
#print "$inset $lib not yet present in: \"";
#foreach $dslf (@depend) {
# print " $dslf";
#}
#print "\"\n";
# before we add it, lets find the dependencies of that library
foreach $l (&dependencies($level+1, $lib)) {
if($l eq "ERROR") {
print " $target\n";
return "ERROR";
}
if(&contains($l, @depend) == 0) {
push @depend, $l;
}
}
push @depend, $lib;
}
}
if(&contains($target, @depend) == 0) {
push @depend, $target;
}
#foreach $dslf (@depend) {
# print "$inset += '$dslf'\n";
#}
return @depend;
}
## Returns 1 if the first arg is already present in the rest of the array, 0 otherwise
sub contains() {
my $new=shift(@_);
@array = @_;
my $item;
foreach $item (@array) {
if($item eq $new) {
return 1;
}
}
return 0;
}
sub parseArguments() {
@arguments=@_;
for($i=0; $i<=$#arguments; $i++) {
if($arguments[$i]=~/(-{1,2}\S+?)(=\S+|)$/) {
$command=$1;
$param=$2;
if($command=~/^-\b(.)(.*)/) {
my $char=$1;
$param=$2;
$command=$argMap{$char};
}
$command=~s/^--//;
if(&contains($command, @options) == 0) {
print "unrecognized option '$arguments[$i]'\n";
print "Try `$COMMAND --help' for more information.\n";
exit 1;
}
if($param eq "" && &contains($command, @argCommands) == 1) {
$i++;
if($arguments[$i]=~/^-/ || $#arguments < $i) {
print "Missing argument: $arguments[$i-1]\n";
exit 1;
}
$param = $arguments[$i];
}
if($param eq "") {
$param =1;
}
$command=~s/\W/_/g;
# print "setting param $command\n";
$$command=$param;
}
else {
push @userArgs, $arguments[$i];
}
}
}
sub checkTargetAvail() {
my $target=shift(@_);
open FILE, "<", "Makefile";
foreach $line (<FILE>) {
if($line=~/^$target:/) {
close FILE;
return 1;
}
}
close FILE;
return 0;
}
|