tmake 28 KB
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 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262
#!/usr/bin/perl
############################################################################
# 
#
# Creates a Makefile from a template and a project file.
#
# Copyright (C) 1996-1998 by Troll Tech AS.  All rights reserved.
#
# Permission to use, copy, modify, and distribute this software and its
# documentation for any purpose and without fee is hereby granted, provided
# that this copyright notice appears in all copies.
# No representations are made about the suitability of this software for any
# purpose. It is provided "as is" without express or implied warranty.
#
#
# Some important, global variables in tmake:
#   cpp_ext	C++ extension added to moc output (.cpp)
#   obj_ext	Object file extension (.o on Unix, .obj otherwise)
#   moc_aware	Will scan for files containing Qt signals/slots
#   moc_pre	Moc prefix for generated moc file: x.h -> moc_x.cpp
#   moc_ext	Moc extension for generated moc file: x.cpp -> x.moc
#   moc_cmd	The moc command in your makefile, $(MOC)
#   linebreak	Line break character (\)
#   dir_sep	Directory separator (/ on Unix, \ on Windows)
#   is_unix	Autodetected. If not Unix, assume Windows (Win32).
#
# If you need to customize any of these settings, do it before
# calling StdInit() in the template file.
#
############################################################################

$TMAKE_VERSION = "1.3";

if ($] < 5.0) {
    &tmake_error("This program requires perl version 5 or newer");
}

$cpp_ext	= "cpp";
$moc_aware	= 0;
$moc_pre	= "moc_";
$moc_ext	= "moc";
$moc_cmd	= '$(MOC)';
$linebreak	= "\\";
$really_unix	= &check_unix();
$is_unix	= $really_unix;
$dir_sep	= $is_unix ? "/" : "\\";
$obj_ext	= $is_unix ? "o" : "obj";
$depend_path	= "";
$nodepend	= 0;
$output_count	= 0;
$notrim_whitespace = 0;
$read_tmakeconf	= 0;
		
$template_name	= "";
$project_name	= "";
$outfile	= "";
%project	= ();
$eval_quit	= 0;

$project{"TMAKEPATH"} = $ENV{"TMAKEPATH"} . ";" . $ENV{"HOME"} . "/.tmake/";

while ( @ARGV ) {				# parse command line args
    $_ = shift @ARGV;
    if ( s/^-// ) {
	if ( /^e(.*)/ ) {
	    if ( ! $read_tmakeconf ) {
		$read_tmakeconf = 1;
		&ScanProject( &find_template("tmake.conf") );
	    }
	    $text = "";
	    eval(  ($1 eq "") ? shift @ARGV : $1 );
	    die $@ if $@;
	    print $text . "\n" if ($text ne "");
	    $eval_quit = 1;
	} elsif ( /^t(.*)/ ) {
	    $template_name = ($1 eq "") ? shift @ARGV : $1;
	} elsif ( /^o(.*)/ ) {
	    $outfile = ($1 eq "") ? shift @ARGV : $1;
	    ($outfile eq "-") && ($outfile = "");
	    if ( $outfile ne "" ) {
		open(STDOUT,">" . fix_path($outfile)) ||
		  &tmake_error("Can't create \"$outfile\"");
	    }
	} elsif ( /^p(.*)/ ) {
	    #
	    # The -p option is obsolete and will be removed in the next	    
	    # tmake release.
	    #
	    &tmake_warning( "-p option obsolete, instead use \"tmake file1.pro file2.pro ...\"");
	    my($pf) = ($1 eq "") ? shift @ARGV : $1;
	    if ( ! $read_tmakeconf ) {
		$read_tmakeconf = 1;
		&ScanProject( &find_template("tmake.conf") );
	    }
	    if ( ! ($pf =~ /\.pro$/i) && -f fix_path($pf . ".pro") ) {
		$pf .= ".pro";
	    }	
	    if ( $project_name eq "" ) {
		$project_name = $pf;
		$project{"PROJECT"} = $project_name;
		$project{"PROJECT"} =~ s/\.pro$//i;
		$project{"TARGET"} = $project{"PROJECT"};
	    }
	    if ( !&ScanProject($pf) ) {
		&tmake_error("Can't open project file \"$pf\"");
	    }
	} elsif ( /^unix$/ ) {
	    $is_unix = 1;
	    $dir_sep = "/";
	    $obj_ext = "o";
	} elsif ( /^win32$/ ) {
	    $is_unix = 0;
	    $dir_sep = "\\";
	    $obj_ext = "obj";
	} elsif ( /^nodepend$/ ) {
	    $nodepend = 1;			# don't generate dependencies
	} elsif ( /^v$/ ) {
	    $verbose = 1;
	} else {
	    &tmake_usage();
	}
    } elsif ( /^\s*((?:[^:\s]*?:)?)(\w+)\s*(\+=|\*=|\-=|\/=|=)/ ) {
	if ( ! $read_tmakeconf && ! (/^\s*TMAKEPATH/) ) {
	    $read_tmakeconf = 1;
	    &ScanProject( &find_template("tmake.conf") );
	}
	Project( $_ );				# manual project setting
    } else {
	my($pf) = $_;
	if ( ! $read_tmakeconf ) {
	    $read_tmakeconf = 1;
	    &ScanProject( &find_template("tmake.conf") );
	}
	if ( ! ($pf =~ /\.pro$/i) && -f fix_path($pf . ".pro") ) {
	    $pf .= ".pro";
	}	
	if ( $project_name eq "" ) {
	    $project_name = $pf;
	    $project{"PROJECT"} = $project_name;
	    $project{"PROJECT"} =~ s/\.pro$//i;
	    $project{"TARGET"} = $project{"PROJECT"};
	}
	if ( !&ScanProject($pf) ) {
	    &tmake_error("Can't open project file \"$pf\"");
	}
    }
}

&tmake_verb("Version $TMAKE_VERSION (runtime environment: " .
	    ($really_unix ? "Unix" : "Win32") . ")\n" );

if ( $eval_quit ) {
    &tmake_verb("Done!");
    exit 0;
}
($project_name eq "") && &tmake_usage();

if ( $template_name eq "" ) {
    $template_name = $project{"TEMPLATE"} ?
	    $project{"TEMPLATE"} : "default.t";
}
$template_name = &find_template($template_name);
&IncludeTemplate($template_name);
&tmake_verb("Done!");
exit 0;						# finished!



##############################################################################
# Subroutines from here
##############################################################################

#
# tmake_usage()
#
# Prints a message about program usage and exits
#

sub tmake_usage {
    print STDERR "Usage:\n    tmake [options] project-files\n";
    print STDERR "Options:\n";
    print STDERR "    -e expr    Evaluate expression, ignore template file\n";
    print STDERR "    -nodepend  Don't generate dependency information\n";
    print STDERR "    -o file    Write output to file\n";
    print STDERR "    -t file    Specify a template file\n";
    print STDERR "    -unix      Create output for Unix (auto detects)\n";
    print STDERR "    -v         Verbose/debug mode\n";
    print STDERR "    -win32     Create output for Win32 (auto detects)\n";
    exit 1;
}


#
# tmake_error(msg)
#
# Prints the message and exits
#

sub tmake_error {
    my($msg) = @_;
    print STDERR "tmake error: " . $msg . "\n";
    exit 1;
}


#
# tmake_warning(msg)
#
# Prints the warning message
#

sub tmake_warning {
    my($msg) = @_;
    print STDERR "tmake warning: " . $msg . "\n";
}


#
# tmake_verb()
#
# Prints a verbose message
#

sub tmake_verb {
    my($msg) = @_;
    $verbose && print STDERR "tmake: " . $msg . "\n";
}


#
# check_unix()
#
# Returns 1 if this is a Unix, 0 otherwise.
#

sub check_unix {
    my($r);
    $r = 0;
    if ( -f "/bin/uname" ) {
	$r = 1;
	(-f "\\bin\\uname") && ($r = 0);
    }
    if ( -f "/usr/bin/uname" ) {
	$r = 1;
	(-f "\\usr\\bin\\uname") && ($r = 0);
    }
    return $r;
}


#
# find_template(filename)
#
# Looks for the template file.
#   1.	search the current directory
#   2.	search the directories in TMAKEPATH
#   3.	search in $HOME/.tmake
#

sub find_template {
    my($filename) = @_;
    my($tb,$d,$p,@dirs);
    if ( !defined($template_base) || ($template_base eq "") ) {
	$tb = "";
    } else {
	$tb = $template_base . ";";
    }
    $d = $tb . $project{"TMAKEPATH"};
    @dirs = ("");
    push @dirs, &split_path( $d );
    $filename .= ".t" unless ($filename =~ /\.\w+$/);
    for $d ( @dirs ) {
	$p = $d . $filename;
	if ( -f fix_path($p) ) {
	    if ( $filename eq "tmake.conf" ) {
		$tmake_platform = $d;
		$tmake_platform =~ s-.*[/\\]([^/\\]*)[/\\]-$1-;
		&tmake_verb("Detected platform $tmake_platform");
	    }
	    return $p;
	}
	return ($d . $filename) if ( -f fix_path($d . $filename) );
    }
    &tmake_error("Template file " . $filename . " not found");
}


##############################################################################
# User functions
##############################################################################

#
# StdInit()
#
# Standard initialization
#

sub StdInit {
    my($p);
    return if $stdinit_done;
    $stdinit_done = 1;
    if ( defined($project{"OBJECTS_DIR"}) ) {
	$project{"OBJECTS_DIR"} = FixPath($project{"OBJECTS_DIR"});
	&mkdirp($project{"OBJECTS_DIR"},0777);
    }
    if ( defined($project{"MOC_DIR"}) ) {
	$project{"MOC_DIR"} = FixPath($project{"MOC_DIR"});
	&mkdirp($project{"MOC_DIR"},0777);
    }
    if ( defined($project{"DESTDIR"}) ) {
	$project{"DESTDIR"} = FixPath($project{"DESTDIR"});
	&mkdirp($project{"DESTDIR"},0777);
    }
    $project{"OBJECTS"} = &Objects($project{"SOURCES"});
    if ( $moc_aware ) {
	$project{"_HDRMOC"} = &list_moc($project{"HEADERS"},$moc_pre,$cpp_ext);
	$project{"_SRCMOC"} = &list_moc($project{"SOURCES"},"",$moc_ext);
	$project{"OBJMOC"}  = &Objects($project{"_HDRMOC"});
	$p = $project{"_HDRMOC"} . " " . $project{"_SRCMOC"};
	$p =~ s/(^\s+|\s+$)//g;
	$project{"SRCMOC"} = $p;
    }
    &AddIncludePath("");
}


sub FixPath {
    my($p) = @_;
    if ( !defined($p) || ($p eq "") || ($p eq ".") ) {
	$p = "";
    } else {
	$p .= $dir_sep;
	$p =~ s-[\\/]+-${dir_sep}-g;
    }
    return $p;
}


#
# Config(name)
#
# Returns true if the project variable CONFIG contains the
# configuration name.
#

sub Config {
    my($name) = @_;
    return $project{"CONFIG"} =~ /\b\Q$name\E\b/;
}


#
# DisableOutput()
#
# Disables tmake output.  Must be restored by calling a corresponding
# EnableOutput().
#

sub DisableOutput {
    $output_count++;
}


#
# EnableOutput()
#
# Enables tmake output again after DisableOutput() has been called.
#

sub EnableOutput {
    $output_count--;
}


#
# Now() - sets $text
#
# Sets $text to the current date and time.
#

sub Now {
    my($sec,$min,$hour,$mday,$mon,$year);
    ($sec,$min,$hour,$mday,$mon,$year) = localtime(time());
    $text = sprintf("%02d:%02d, %4d/%02d/%02d",
		    $hour, $min, 1900+$year, 1+$mon, $mday);
}


#
# expand_project_var(var)
#
# Internal function for Project().
# Expands a project value string.
#

sub expand_project_var {
    my($v) = @_;
    my($c);
    return "" if !defined($v);
    $c = 0;
    while ( $c < 100 ) {			# expand $$
	if ( $v =~ s/(\$\$\w+)/\035/ ) {
	    $_ = $1;
	    s/\$\$//g;
	    if ( !defined($project{$_}) ) {
		$v =~ s/\035//g;
	    } else {
		$v =~ s/\035/$project{$_}/g;
	    }
	    $c++;
	} else {
	    $c = 100;
	}
    }
    return $v;
}


#
# Project(strings)
#
# This is a powerful function for setting or reading project variables.
# Returns the resulting project variables (joined with space between).
#
# Get a project variable:
#   $s = Project("TEMPLATE");	    -> $s = "TEMPLATE"
#
# Set a project variable:
#   Project("TEMPLATE = lib");	    -> TEMPLATE = lib
#   Project("CONFIG =";)	    -> CONFIG empty
#
# Append to a project variable:
#   Project("CONFIG = qt");	    -> CONFIG = qt
#   Project("CONFIG += debug");	    -> CONFIG = qt debug
#
# Append to a project variable if it does not contain the value already:
#   Project("CONFIG = qt release"); -> CONFIG = qt release
#   Project("CONFIG *= qt");	    -> CONFIG = qt release
#   Project("CONFIG *= opengl");    -> CONFIG = qt release opengl
#
# Subtract from a project variable:
#   Project("THINGS = abc xyz");    -> THINGS = abc xyz
#   Project("THINGS -= abc");	    -> THINGS = xyz
#
# Search/replace on a project variable:
#   Project("CONFIG = tq opengl");  -> CONFIG = tq opengl
#   Project("CONFIG /= s/tq/qt/");  -> CONFIG = qt opengl
#
# The operations can be performed on several project variables at a time.
#
#   Project("TEMPLATE = app", "CONFIG *= opengl", "THINGS += klm");
#

sub Project {
    my @settings = @_;
    my($r,$if_var,$t,$s,$v,$p,$c);
    $r = "";
    foreach ( @settings ) {
	$v = $_;
	if ( $v =~ s/^\s*((?:[^:\s]*?:)?)(\w+)\s*(\+=|\*=|\-=|\/=|=)// ) {
	    $if_var = $1;
	    if ( $if_var ne "" ) {		
		chop $if_var;
		if ( $if_var eq "unix" ) {
		    return "" if !$is_unix;
		} elsif ( $if_var eq "win32" ) {
		    return "" if $is_unix;
		} elsif ( ($if_var ne $tmake_platform) && !Config($if_var) ) {
		    return "";
		}
	    }
	    $t = $2;
	    $s = $3;
	    if ( ! $notrim_whitespace ) {	
		$v =~ s/^\s+//;			# trim white space
		$v =~ s/\s+$//;
	    }
	    $v = expand_project_var($v);
	    $p = $project{$t};
	    if ( $s ne "=" && $v eq "" ) {
		# nothing to append, subtract or sed
	    } elsif ( $s eq "=" ) {		# set variable
		$p = $v;
	    } elsif ( $s eq "+=" ) {		# append
		if ( $p eq "" ) {
		    $p = $v;
		} else {
		    $p .= " " . $v;
		}
	    } elsif ( $s eq "*=" ) {		# append if not contained
		if ( !($p =~ /(?:^|\s)\Q$v\E(?:\s|$)/) ) {
		    if ( $p eq "" ) {
			$p = $v;
		    } else {
			$p .= " " . $v;
		    }
		}
	    } elsif ( $s eq "-=" ) {		# subtract
		$p =~ s/$v//g;
	    } elsif ( $s eq "/=" ) {		# sed
		$cmd = '$p =~ ' . $v;
		eval $cmd;
	    }
	    $project{$t} = expand_project_var($p);
	} else {
	    $p = expand_project_var($project{$v});
	}
	if ( $p ne "" ) {
	    $r = ($r eq "") ? $p : ($r . " " . $p);
	}
    }
    return $r;
}


#
# Substitute(string)
#
# This function substitutes project variables in a text.
#
# Example:
#   Substitute('The project name is "$$PROJECT"')
#

sub Substitute {
    my($subst) = @_;
    $text = expand_project_var($subst);
    return $text;
}


#
# ScanProject(file)
#
# Scans a project file. Inserts project variables into the global
# associative project array.
#

sub ScanProject {
    my($file) = @_;
    my($var,$val,@v,$more,$line,$endmark);

    $var = "";
    $line = 0;
    open(TMP,fix_path($file)) || return 0;

    &tmake_verb("Reading the project file $file");
    while ( <TMP> ) {
	$line++;
	s/\#.*//;				# strip comment
	s/^\s+//;				# strip white space
	s/\s+$//;
	if ( /^\s*((?:(?:[^:\s]*?:)?)\w+\s*(\+|\-|\*|\/)?=)/ ) {
	    $var = $1;				# var also contains the ".="
	    s/^.*?=\s*//;
	    if ( /^\<\<(.*)$/ ) {
		$endmark = $1;		
		$val = "";
		while ( <TMP> ) {
		    $line++;
		    if ( /^\Q$endmark\E$/ ) {
			$endmark = "";
			last;
		    }
		    $val .= $_;
		}
		if ( $endmark ne "" ) {
		    tmake_error("$file:$line: End marker $endmark not found");
		}
		chop $val if ( $val ne "" );
		$notrim_whitespace++;
		Project( $var . $val );
		$notrim_whitespace--;
		$var = "";
		$_ = "";
	    }
	}
	if ( $var ne "" ) {
	    $more = ( $_ =~ s/\s*\\\s*$// );	# more if \ at end of line
	    push( @v, split( /\s+/, $_ ) );
	    if ( ! $more ) {
		$val = join(" ",@v);
		Project( $var . $val );
		$var = "";
		@v = ();
	    }
	} elsif ( $_ ne "" ) {
	    tmake_error("$file:$line: Syntax error");
	}
    }
    close(TMP);
    &tmake_verb("Done reading the project file $file");
    return 1;
}


#
# IncludeTemplate(template_name)
#
# Includes and processes a template file.
#
# Below, we read the template file and executes any perl code found.
# Perl code comes after "#$". The variable $text contains the text
# to replace the perl code that was executed.
# Template comments begin with "#!".
#

sub IncludeTemplate {
    my($t_name) = @_;
    my($cmd,$cmd_block,$cmd_end,$is_cmd_block,$saveline,$spaceonly);
    local($text);
    local(*T);

    $t_name = &find_template($t_name);
    if ( $tmake_template_dict{$t_name} ) {
	&tmake_error("Cyclic template inclusion for $t_name");
    } else {
	$tmake_template_dict{$t_name} = 1;
    }
    $template_base = $t_name;
    $template_base =~ s-(.*[/\\]).*-$1-;
    &tmake_verb("Reading the template $t_name");
    open(T,fix_path($t_name)) ||
	&tmake_error("Can't open template file \"$t_name\"");

    while ( <T> ) {
	if ( /\#\!/ ) {				# tmake comment
	    s/\s*\#\!.*//;
	    next if /^$/;
	}
	if ( /\#\$(\{)?\s*(.*)\n/ ) {		# code
	    $cmd = $2;
	    $is_cmd_block = defined($1) && ($1 eq "{");
	    s/\#\$.*\n//;
	    if ( $is_cmd_block ) {		# code block #${ ...
		$saveline = $_;
		$cmd_block = $cmd;
		$cmd_end = 0;
		while ( <T> ) {
		    $cmd = $_;
		    $cmd =~ s/\s*\#\!.*//;	# tmake comment
		    if ( $cmd =~ /^\s*\#\$\}/ ) {
			$_ = "";
			$cmd_end = 1;
			last;
		    }
		    $cmd =~ s/^\s*\#(\$)?\s*//;
		    $cmd_block .= $cmd;
		}
		$cmd_end || &tmake_error('#$} expected but not found');
		$cmd = $cmd_block;
		$_ = $saveline;
	    }
	    $spaceonly = /^\s*$/;
	    $saveline = $_;
	    &tmake_verb("Evaluate: $cmd");
	    $text = "";
	    eval $cmd;
	    die $@ if $@;
	    next if $spaceonly && ($text =~ /^\s*$/);
	    print $saveline . $text . "\n" if  $output_count <= 0;
	} else {				# something else
	    print if $output_count <= 0;
	}
    }
    close( T );
}


#
# Expand(var) - appends to $text
#
# Expands a list of $project{} variables with a space character between them.
#

sub Expand {
    my @vars = @_;
    my($t);
    $t = Project(@vars);
    if ( $text eq "" ) {
	$text = $t;
    } elsif ( $t ne "" ) {
	$text .= " " . $t;
    }
    return $text;
}


#
# ExpandGlue(var,prepend,glue,append) - appends to $text
#
# Expands a $project{} variable, splits on whitespace
# and joins with $glue. $prepend is put at the start
# of the string and $append is put at the end of the
# string. The resulting string becomes "" if the project
# var is empty or not defined.
#
# Example:
#
#   The project file defines:
#	SOURCES = a b c
#
#   ExpandGlue("SOURCES","<","-",">")
#
#   The result:
#	$text = "<a-b-c>"
#

sub ExpandGlue {
    my($var,$prepend,$glue,$append) = @_;
    my($t,$v);
    $v = Project($var);
    if ( $v eq "" ) {
	$t = "";
    } else {
	$t = $prepend . join($glue,split(/\s+/,$v)) . $append;
    }
    if ( $text eq "" ) {
	$text = $t;
    } elsif ( $t ne "" ) {
	$text .= " " . $t;
    }
    return $text;
}


#
# ExpandList(var) - sets $text.
#
# Suitable for expanding HEADERS = ... etc. in a Makefile
#

sub ExpandList {
    my($var) = @_;
    return ExpandGlue($var,""," ${linebreak}\n\t\t","");
}


#
# ExpandPath(var,prepend,glue,append) - appends to $text
#
# Expands a $project{} variable, splits on either ';' or
# whitespace and joins with $glue. $prepend is put at the
# start of the string and $append is put at the end of the
# string. The resulting string becomes "" if the project
# variable is empty or not defined.
#
# If the variable contains at least one semicolon or tmake
# is running on Windows, the resulting items are put in
# double-quotes.
#
# Example:
#
#   The project file defines:
#	INCLUDEPATH = "C:\qt\include;c:\program files\msdev\include
#
#   ExpandGlue("INCLUDEPATH","-I","-I","")
#
#   The result:
#	$text = -I"c:\qt\include" -I"c:\program files\msdev\include"
#

sub ExpandPath {
    my($var,$prepend,$glue,$append) = @_;
    my($t,$v);
    my($s);
    $v = Project($var);
    if ( $v eq "" ) {
	$t = "";
    } else {
	if ( $v =~ /;/ || !$is_unix ) {
	    $prepend .= '"';
	    $glue = '"' . $glue . '"';
	    $append = '"' . $append;
	}

	if ( $v =~ /;/ ) {    
	    $t = $prepend . join($glue,split(/;+/,$v)) . $append;
	} else {
	    $t = $prepend . join($glue,split(/\s+/,$v)) . $append;
	}
    }
    if ( $text eq "" ) {
	$text = $t;
    } elsif ( $t ne "" ) {
	$text .= " " . $t;
    }
    return $text;
}


#
# TmakeSelf()
#
# Generates makefile rule to regenerate the makefile using tmake.
#

sub TmakeSelf {
    my $a = "tmake $project_name";
    if ( $nodepend ) {
	$a .= " -nodepend";
    }
    if ( $outfile ) {
	$text = "tmake: $outfile\n\n$outfile: $project_name\n\t";
	$a .= " -o $outfile";
    } else {
	$text = "tmake:\n\t";
    }
    $text .= $a
}


#
# Objects(files)
#
# Replaces any extension with .o ($obj_ext).
#

sub Objects {
    local($_) = @_;
    my(@a);
    @a = split(/\s+/,$_);
    foreach ( @a ) {
	s-\.\w+$-.${obj_ext}-;
	if ( defined($project{"OBJECTS_DIR"}) ) {
	    s-^.*[\\/]--;
	    $_ = $project{"OBJECTS_DIR"} . $_;
	}
    }
    return join(" ",@a);
}


#
# list_moc(files,prefix,extension)
#
# Scans all files and selects all files that contain Q_OBJECT.
# Insert a prefix before the filename and replaces the filename extention.
#

sub list_moc {
    my($files,$pre,$ext) = @_;
    my(@v,@m,@lines,$contents,$n,$f,$t);
    @v = split(/\s+/,$files);
    undef $/;
    foreach $f ( @v ) {
	if ( open(TMP,fix_path($f)) ) {
	    $contents = <TMP>;
	    close(TMP);
	    $n = 0;
	    @lines = split(/\n/,$contents);
	    grep( /tmake\s+ignore\s+Q_OBJECT/ && $n--, @lines );
	    $contents =~ s-/\*.*?\*/--gs; # strip C/C++ comments
	    $contents =~ s-//.*\n--g;
	    @lines = split(/\n/,$contents);
	    grep( /(^|\W)Q_OBJECT(\W|$)/ && $n++, @lines );
	    if ( $n > 0 ) {
		$t = $f;
		$t =~ s-^(.*[/\\])?([^/\\]*?)\.(\w+)$-$1${pre}$2.${ext}-;
		if ( defined($project{"MOC_DIR"}) ) {
		    $t =~ s-^.*[\\/]--;
		    $t = $project{"MOC_DIR"} . $t;
		}
		$moc_output{$f} = $t;
		$moc_input{$t}	= $f;
		push(@m,$t);
	    }
	    $contents = "";
	}
    }
    $/ = "\n";
    return join(" ",@m);
}


#
# BuildObj(objects,sources)
#
# Builds the object files.
#

sub BuildObj {
    my($obj,$src) = @_;
    my(@objv,$srcv,$i,$s,$o,$d,$c,$comp,$cimp);
    @objv = split(/\s+/,$obj);
    @srcv = split(/\s+/,$src);
    for $i ( 0..$#objv ) {
	$s = $srcv[$i];
	$o = $objv[$i];
	next if $s eq "";
	$text .= $o . ": " . $s;
	if ( defined($moc_output{$s}) && ($moc_output{$s} ne "") ) {
	    $text .= " ${linebreak}\n\t\t" . $moc_output{$s};
	}
	$d = &make_depend($s);
	$text .= " ${linebreak}\n\t\t" . $d if $d ne "";
	if ( ($s =~ /\.c$/) ) {
	    $comp = "TMAKE_RUN_CC";
	    $cimp = "TMAKE_RUN_CC_IMP";
	} else {
	    $comp = "TMAKE_RUN_CXX";
	    $cimp = "TMAKE_RUN_CXX_IMP";
	}
	if ( defined($project{"OBJECTS_DIR"}) ||
	     !defined($project{$cimp}) ) {
	    $c = $project{$comp};
	    $c =~ s/\$src/$s/;
	    $c =~ s/\$obj/$o/;
	    $text .= "\n\t$c";
	}
	$text .= "\n\n";
    }
    chop $text;
}


#
# BuildMocObj(objects,sources)
#
# Builds the moc object files.
#

sub BuildMocObj {
    my($obj,$src) = @_;
    my(@objv,$srcv,$i,$s,$o,$hdr,$d);
    @objv = split(/\s+/,$obj);
    @srcv = split(/\s+/,$src);
    for $i ( 0..$#objv ) {
	$s = $srcv[$i];
	$o = $objv[$i];
	$hdr = $moc_input{$srcv[$i]};
	$text .= $o . ": " . $s . " ${linebreak}\n\t\t" . $hdr;
	$d = &make_depend($hdr);
	$text .= " ${linebreak}\n\t\t" . $d if $d ne "";
	if ( defined($project{"OBJECTS_DIR"}) || defined($project{"MOC_DIR"})||
	     !defined($project{"TMAKE_RUN_CXX_IMP"}) ) {
	    $c = $project{"TMAKE_RUN_CXX"};
	    $c =~ s/\$src/$s/;
	    $c =~ s/\$obj/$o/;
	    $text .= "\n\t$c";
	}
	$text .= "\n\n";
    }
    chop $text;
}


#
# BuildMocSrc(files)
#
# Builds the moc source files from headers and sources.
#

sub BuildMocSrc {
    my($f) = @_;
    my(@v,$m,$o);
    @v = split(/\s+/,$f);
    foreach $m ( @v ) {
	$o = $moc_output{$m};
	if ( defined($o) && ($o ne "") ) {
	    $text .= "$o: $m\n\t$moc_cmd $m -o $o\n\n";
	}
    }
    chop $text;
}


#
# AddIncludePath(path)
#
# Adds path to the current include path, $project{"INCLUDEPATH"}.
#

sub AddIncludePath {
    my($path) = @_;
    my($p);
    if ( $project{"INCPATH"} &&
	 ($project{"INCPATH"} =~ /(?:^|\s)\Q$path\E(?:\s|$)/) ) {
	return;
    }
    $project{"INCLUDEPATH"} = "" if !defined($project{"INCLUDEPATH"});
    if ( !defined($project{"INCPATH_SEP"}) ) {
	if ( $project{"INCLUDEPATH"} =~ /;/ ) {
	    $project{"INCPATH_SEP"} = ";";
	} else {
	    $project{"INCPATH_SEP"} = " ";
	}
    }
    $p = $project{"INCLUDEPATH"};
    $p = ($p && $path) ? ($p . ";" . $path) : ($p . $path);
    $project{"INCLUDEPATH"} = $p;
    $p = join($project{"INCPATH_SEP"},&split_path($p));
    $p =~ s=[\\/]($project{"INCPATH_SEP"}|$)=$project{"INCPATH_SEP"}=g;
    $project{"INCPATH"} = $p;
}


#
# FindHighestLibVersion(dir,name)
#
# Returns the newest library version. Scans all the files in the specifies
# directory and returns the highest version number.
#
# Used on Windows only.
#
# Example:
#    FindHighestLibVersion("c:\qt\lib","qt") returns "200" if
#    the c:\qt\lib directory contains qt141.lib and qt200.lib.
#

sub FindHighestLibVersion {
    my($dir,$name) = @_;
    my(@files,$f,$v,$highest);
    $highest = "";
    @files = find_files($dir,"${name}.*\.lib");
    for $f ( @files ) {
	if ( $f =~ /(\d+)\.lib/i ) {
	    $v = $1;
	    if ( $highest eq "" || $v > $highest ) {
		$highest = $v;
	    }
	}
    }
    return $highest;
}


#
# Finds files.
#
# Examples:
#   find_files("/usr","\.cpp$",1)   - finds .cpp files in /usr and below
#   find_files("/tmp","^#",0)	    - finds #* files in /tmp
#

sub find_files {
    my($dir,$match,$descend) = @_;
    my($file,$p,@files);
    local(*D);
    $dir =~ s=\\=/=g;
    ($dir eq "") && ($dir = ".");
    if ( opendir(D,fix_path($dir)) ) {
	if ( $dir eq "." ) {
	    $dir = "";
	} else {
	    ($dir =~ /\/$/) || ($dir .= "/");
	}
	foreach $file ( readdir(D) ) {
	    next if ( $file  =~ /^\.\.?$/ );
	    $p = $dir . $file;
	    if ( $is_unix ) {
		($file =~ /$match/) && (push @files, $p);
	    } else {
		($file =~ /$match/i) && (push @files, $p);
	    }
	    if ( $descend && -d $p && ! -l $p ) {
		push @files, &find_files($p,$match,$descend);
	    }
	}
	closedir(D);
    }
    return @files;
}


#
# make_depend(file)
#
# Returns a list of included files.
# Uses the global $depend_path variable.
#

sub make_depend {
    my($file) = @_;
    my($i,$count);
    if ( $nodepend ) {
	return "";
    }
    if ( ! $depend_path_fixed ) {
	$depend_path_fixed = 1;
	if ( defined($project{"DEPENDPATH"}) ) {
	    $depend_path = $project{"DEPENDPATH"};
	} else {
	    $depend_path = "";
	}
	$count = 0;
	while ( $count < 100 ) {
	    if ( $depend_path =~ s/(\$[\{\(]?\w+[\}\)]?)/035/ ) {
		$_ = $1;
		s/[\$\{\}\(\)]//g;
		$depend_path =~ s/035/$ENV{$_}/g;
	    } else {
		$count = 100;
	    }
	}
	@dep_path = &split_path($depend_path);
    }
    @cur_dep_path = @dep_path;
    if ( $file =~ /(.*[\/\\])/ ) {
	$dep_curdir = $1;
	push @cur_dep_path, $dep_curdir;
    } else {
	$dep_curdir = "";
    }
    $dep_file = $file;
    &canonical_dep($file);
    %dep_dict = ();
    $i = &build_dep($file);
    chop $i;
    $i =~ s=/=$dir_sep=g unless $is_unix;
    $i =~ s=([a-zA-Z]):/=//$1/=g if (defined($gnuwin32) && $gnuwin32);
    return join(" ${linebreak}\n\t\t",split(/ /,$i) );
}

#
# build_dep() - Internal for make_depend()
#

sub build_dep {
    my($file) = @_;
    my(@i,$a,$n);
    $a = "";
    return $a if !(defined $depend_dict{$file});
    @i = split(/ /,$depend_dict{$file});
    for $n ( @i ) {
	if ( !defined($dep_dict{$n}) && defined($full_path{$n}) ) {
	    $dep_dict{$n} = 1;
	    $a .= $full_path{$n} . " " . &build_dep($n);
	}
    }
    return $a;
}

#
# canonical_dep(file) - Internal for make_depend()
#
# Reads the file and all included files recursively.
# %depend_dict associates a file name to a list of included files.
#

sub canonical_dep {
    my($file) = @_;
    my(@inc,$i);
    @inc = &scan_dep($file);
    if ( @inc ) {
	$depend_dict{$file} = join(" ",@inc);
	for $i ( @inc ) {
	    &canonical_dep($i) if !defined($depend_dict{$i});
	}
    }
}

#
# scan_dep(file) - Internal for make_depend()
#
# Returns an array of included files.
#

sub scan_dep {
    my($file) = @_;
    my($dir,$path,$found,@allincs,@includes,%incs);
    $path = $file;
    @includes = ();
    return @includes if $file =~ /\.$moc_ext$/; # avoid .moc files
    if ( ! (-f fix_path($path)) ) {
	$found = 0;
	for $dir ( @cur_dep_path ) {
	    $path = $dir . $file;
	    last if ( $found = (-f fix_path($path)) );
	}
	return @includes if ! $found;
    }
    undef $/;
    if ( open(TMP,fix_path($path)) ) {
	$full_path{$file} = $path;
	$_ = <TMP>;
	s-/\*.*?\*/--gs;			# strip C/C++ comments
	s-//.*\n-\n-g;
	@allincs = split(/\n/,$_);
	@allincs = grep(/^\s*#\s*include/,@allincs);
	foreach ( @allincs ) {			# all #include lines
	    next if !(/^\s*#\s*include\s+[<"]([^>"]*)[>"]/) || defined($incs{$1});
	    push(@includes,$1);
	    $incs{$1} = "1";
	}
	close(TMP);
    }
    $/ = "\n";
    return @includes;
}


#
# split_path(path)
#
# Splits a path containing : (Unix) or ; (MSDOS, NT etc.) separators.
# Returns an array.
#

sub split_path {
    my($p) = @_;
    my($s,@d);
    @d = ();
    return @d if !defined($p) || $p eq "";
    $p =~ s=:=;=g if $is_unix;
    $p =~ s=[/\\]+=/=g;
    if ( !($p =~ /;/) ) {
	$p =~ s/\s+/;/g;
    }
    $p =~ s/\s*;\s*/;/g;
    while( $p =~ /(?:(?:[^\"\;][^\;]*;*)|(?:\"[^\"]*\";*))/g ) {
	$s = $&;
	$s =~ s=\"==g;
	$s =~ s=[\s\;]+$==g;
	$s =~ s=([^/:])$=$1/=g;
	$s =~ s=/=$dir_sep=g unless $is_unix;
	push @d, $s;
    }
    return @d;
}


#
# fix_path(path)
#
# Converts all '\' to '/' if this really seems to be a Unix box.
#

sub fix_path {
    my($p) = @_;
    if ( $really_unix ) {
	$p =~ s-\\-/-g;
    } else {
	$p =~ s-/-\\-g;
    }
    return $p;
}


#
# mkdirp(filename,mode) - Internal for StdInit()
#
# Creates the directory specified by $filename, with permissions
# specified by mode (as modified by umask). Recursively calls
# mkdir, similar to 'mkdir -p'.
#

sub mkdirp {
    my($filename,$mode) = @_;
    if ( $filename =~ /\$\(\w+\)/ ) {  # ignore "$(something)"
	return 0;
    }
    $filename =~ s-[\\:/]+-/-g;
    if ( -d $filename ) {
	return 1;
    }
    $filename =~ m-^((.*)/)?(.*)-;
    if ( defined($2) && ! mkdirp($2,$mode) ) {
	return 0;
    }
    return mkdir($filename,$mode);
}