summaryrefslogtreecommitdiffstats
path: root/tmake/bin/progen
blob: 5be6411ae797c92029a5228da52def48cc5ff8e2 (plain)
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
#!/usr/bin/perl
############################################################################
# 
#
# Generates a tmake 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.
#
############################################################################

# Default project settings
$project{"TEMPLATE"} = "app";
$project{"CONFIG"}   = "qt warn_on release";

@project_extra = ();

while ( @ARGV ) {			# parse command line args
    $_ = shift @ARGV;
    if ( s/^-// ) {
	if ( /^o(.*)/ ) {
	    $outfile = ($1 eq "") ? shift @ARGV : $1;
	    ($outfile eq "-") && ($outfile = "");
	} elsif ( /^n(.*)/ ) {
	    $project{"TARGET"} = ($1 eq "") ? shift @ARGV : $1;
	} elsif ( /^t(.*)/ ) {
	    $project{"TEMPLATE"} = ($1 eq "") ? shift @ARGV : $1;
	    $project{"TEMPLATE"} =~ s/\.t$//i;
	} elsif ( /lower/ ) {
	    $tolower = 1;
	} else {
	    &progen_usage;
	}
    } elsif ( /^\s*(?:[\w\-]+:)?\w+\s*[\+\-\*\/]?=/ ) {	# project override
	push( @project_extra, $_ );
    } else {
	push (@files, $_ );
    }
}

$outfile eq "" || open(STDOUT,">" . $outfile) ||
    &progen_error("Can't create \"$outfile\"");

if ( ! @files ) {
    @files = &find_files(".",".*",1);
}

if ( $tolower ) {
    foreach $f ( @files ) {
	$f =~ tr/A-Z/a-z/;
    }
}

@hdr = sort grep(/\.(h|hh|hpp|hxx)$/i,@files);
@src = sort grep(/\.(c|cpp|cc|cxx)$/i && ! /moc_/i,@files);

# Remove source files that are included by other source files
foreach $f ( @src ) {
    $srcdict{$f} = 1;
}
foreach $f ( @src ) {
    if ( open(F,"< $f") ) {
	while ( <F> ) {
	    if ( /^\s*#\s*include\s+\"([^\"]*)\"/ ) {
		$srcdict{$1} = 0;
	    }
	}
    }
}
foreach $f( @src ) {
    $srcdict{$f} && (push(@src2,$f));
}
@src = @src2;

$project{"HEADERS"} = join(" ",sort @hdr);
$project{"SOURCES"} = join(" ",sort @src);

foreach $p ( @project_extra ) {
    if ( $p =~ /^\s*((?:[\w\-]+:)?\w+)\s*([\+\-\*\/])?=\s*(.*)/ ) {
	if ( $project{$1} ne "" ) {
	    Project($p);
	}
    }
}

$project{"HEADERS"} =~ s/\s+/ \\\n\t\t  /g;
$project{"SOURCES"} =~ s/\s+/ \\\n\t\t  /g;

print "TEMPLATE\t= " . $project{"TEMPLATE"} . "\n";
print "CONFIG\t\t= " . $project{"CONFIG"} . "\n";
print "HEADERS\t\t= " . $project{"HEADERS"} . "\n";
print "SOURCES\t\t= " . $project{"SOURCES"} . "\n";
if ( $project{"TARGET"} ne "" ) {
    print "TARGET\t\t= " . $project{"TARGET"} . "\n";
}

foreach ( @project_extra ) {
    if ( /^\s*((?:[\w\-]+:)?\w+)\s*([\+\-\*\/])?=\s*(.*)/ ) {
	if ( $project{$1} eq "" ) {
	    $t = $1;
	    if ( length($t) < 8 ) {
		$t .= "\t\t";
	    } elsif ( length($t) < 16 ) {
		$t .= "\t";
	    } else {
		$t .= " ";
	    }
	    print "$t$2= $3\n";
	}
    }
}

exit 0;


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

sub progen_usage {
    print STDERR "Usage:\n    progen [options] [files]\n";
    print STDERR "Options:\n";
    print STDERR "    -lower   Lower-case letters filenames (useful for non-Unix)\n";
    print STDERR "    -n name  Specify a project name (= TARGET)\n";
    print STDERR "    -o file  Write output to \"file\"\n";
    print STDERR "    -t file  Specify a template file other than qtapp\n";
    exit 1;
}


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

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


#
# 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,$dir) ) {
	if ( $dir eq "." ) {
	    $dir = "";
	} else {
	    ($dir =~ /\/$/) || ($dir .= "/");
	}
	foreach $file ( readdir(D) ) {
	    next if ( $file  =~ /^\.\.?$/ );
	    $p = $dir . $file;
	    ($file =~ /$match/i) && (push @files, $p);
	    if ( $descend && -d $p && ! -l $p ) {
		push @files, &find_files($p,$match,$descend);
	    }
	}
	closedir(D);
    }
    return @files;
}


#
# strip_project_val(tag)
#
# Strips white space from project value strings.
#

sub strip_project_val {
    my($v) = @_;
    $v =~ s/^\s+//;				# trim white space
    $v =~ s/\s+$//;
    return $v;
}


#
# Project(strings)
#
# This is a powerful function for setting or reading project variables.
# Returns the resulting project variables (joined with space between).
#
# This is a slightly modified version of the Project function in tmake.

sub Project {
    my @settings = @_;
    my($r,$t,$s,$v,$p,$c);
    $r = "";
    foreach ( @settings ) {
	$v = $_;
	if ( $v =~ s/^\s*((?:[\w\-]+:)?\w+)\s*(\+=|\*=|\-=|\/=|=)\s*// ) {
	    $t = $1;
	    $s = $2;
	    $v = strip_project_val($v);
	    $p = $project{$t};
	    if ( $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} = strip_project_val($p);
	} else {
	    $p = strip_project_val($project{$v});
	}
	if ( $p ne "" ) {
	    $r = ($r eq "") ? $p : ($r . " " . $p);
	}
    }
    return $r;
}