summaryrefslogtreecommitdiffstats
path: root/atoms_cc/buildtoolbox.pl
blob: 80ef477291660717bd3051fe1b023efee59e2a3c (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
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
#!/usr/bin/perl -w

# buildtoolbox.pl
# Usage: buildtoolbox.pl toolbox-archive [config file [stage]]

use strict;
use Cwd;

my ($TOOLBOXFILE, # Toolbox archive to compile
    $TOOLBOXNAME); # Name of the toolbox

# is_zip:
#    Return true if toolbox file extension is zip
sub is_zip {
	return $TOOLBOXFILE =~ /\.zip$/;
}

# get_tree_from_tgz:
#   Get all files (names) of the compressed (in tar.gz) sources
sub get_tree_from_tgz {
	my %files;
	
	open my $fd, "tar -tzf ${TOOLBOXFILE}|";
	
	while(<$fd>) {
		chomp;
		$files{$_} = 1;
	}
	
	close $fd;
	return %files;
}

# get_tree_from_zip:
#   Get all files (names) of the compressed (in zip) sources
sub get_tree_from_zip {
	my (%files, $line);
	
	# tail & head are here to skip header & footer
	open my $fd, "unzip -l ${TOOLBOXFILE} | tail -n +4 | head -n -2 |";
	
	while(<$fd>) {
		# zip output format: size date time filename
		/\s*\d+\s+\d+-\d+-\d+\s+\d+:\d+\s+(.+)/ or die "Bad output of unzip";
		chomp $1;
		$files{$1} = 1;
	}
	
	close $fd;
	return %files;
}

# get_tree:
#   Get all files (names) of the compressed sources, in a hash
#   (hash values are meaningless, set to 1)
sub get_tree {
	if(is_zip()) {
		return get_tree_from_zip();
	}
	else {
		return get_tree_from_tgz();
	}
}

# get_description_from_tgz:
#    Extract DESCRIPTION file from the archive (in tar.gz format)
sub get_description_from_tgz {
	open my $fd, "tar -xOzf ${TOOLBOXFILE} ${TOOLBOXNAME}/DESCRIPTION |";
	return $fd;
}

# get_description_from_tgz:
#    Extract DESCRIPTION file from the archive (in zip format)
sub get_description_from_zip {
	open my $fd, "unzip -c ${TOOLBOXFILE} ${TOOLBOXNAME}/DESCRIPTION | tail -n +3 | head -n -1 |";
	return $fd;
}

# get_description:
#   Extract DESCRIPTION file from the archive
sub get_description {
	if(is_zip) {
		return get_description_from_zip();
	}
	else {
		return get_description_from_tgz();
	}
}

# read_description:
#   Check if DESCRIPTION file is correct, and parse it (return a hash
#   field => value).
#   First argument is a file descriptor for the DESCRIPTION file (see
#   get_description)
sub read_description {
	my ($fd) = shift;
	my @required = qw(Toolbox Version Title Author Maintainer
	                  Description License Category);
	my @optional = qw(Date Depends URL Entity);
	my (%infos, $key, $val);
	my (%lines, %correct);
	
	# Populate hash
	while(<$fd>) {
		die "\":\" not followed by a space at line $." if(/:(?! )/);
		if(/:/) { # New field
			($key, $val) = split(/: /, $_, 2);
			$infos{$key} = $val;
			$lines{$key} = $.;
			$correct{$key} = 0;
		}
		else { # Continuation of previous field
			$infos{$key} .= $_;
		}
	}
	
	# Check presence of required fields, mark them as correct
	foreach (@required) {
		if(!defined($infos{$_})) {
			die "Mandatory field \"$_\" not defined";
		}
		else {
			$correct{$_} = 1;
		}
	}
	
	# Mark optional fields as correct
	foreach (@optional) {
		if(defined($infos{$_})) {
			$correct{$_} = 1;
		}
	}
	
	# Check that there's no incorrect (= unknown) fields
	foreach (keys(%infos)) {
		if($correct{$_} == 0) {
			die "Unknown field \"$_\" (defined at line $lines{$_})";
		}
	}
	
	chomp %infos;
	return %infos;
}

# check_tree:
#   Given a source tree of a toolbox (see get_tree), check if it is correct
#   (required files are present, files are at their right place, and so on...)
sub check_tree {
	my %tree = @_;
	my %newtree;
	
	# Check that all files are under a root which has the same name as the toolbox
	# Delete this root to simplify other tests
	foreach (keys %tree) {
		if(s#^\Q$TOOLBOXNAME\E(/|$)##) {
			$newtree{$_} = 1 if $_;
		}
		else {
			die "Incorrect archive: \"$_\" is not a child of \"$TOOLBOXNAME\"";
		}
	}
	%tree = %newtree;
	
	# Check that basic files are here
	my @required = qw(DESCRIPTION DESCRIPTION-FUNCTIONS readme.txt license.txt
	                  builder.sce loader.sce);
	push(@required, "etc/$TOOLBOXNAME.start");
	push(@required, "etc/$TOOLBOXNAME.end");
	
	foreach (@required) {
		if(!defined($tree{$_})) {
			die "Incorrect archive: required file \"$_\" not present";
		}
	}
	
	# macros/ must contain only .sci and .sce files
	# If it exists and is non-empty, it must contains buildmacros.sce
	my $macros_empty = 1;
	my $macros_has_builder = 0;
	foreach (grep { $_ =~ m#^macros/# } keys %tree) {
		if(/\.sc[ie]$/) {
			$macros_empty = 0;
			$macros_has_builder = 1 if(m#/buildmacros\.sce$#);
		}
		elsif(!/\/$/) { # Don't be /too/ nazi: allow sub-directories :)
			die "Incorrect archive: macros/ must contain only .sci and .sce files".
			    " (\"$_\" found)";
		}
	}
	
	if(!$macros_empty && !$macros_has_builder) {
		die "Incorrect archive: macros/ not empty and no buildmacros.sce script found";
	}
	
	# All fortran files must be in src/fortran
	foreach (grep { $_ =~ /\.f$/} keys %tree) {
		if(!m#^src/fortran/#) {
			die "Incorrect archive: \"$_\" is a fortran source and hence has to be in ".
			    "src/fortran";
		}
	}

	# All c files must be in src/c or sci_gateway/{c,fortran}
	foreach (grep { $_ =~ /\.[ch]$/} keys %tree) {
		if(!m#^(src/c|sci_gateway/(c|fortran))/#) {
			die "Incorrect archive: \"$_\" is a C source and hence has to be in ".
			    "src/c, sci_gateway/c or sci_gateway/fortran";
		}
	}
	
	# if src/c contains at least a .c file, src/c/buildsrc_c.sce must exists
	my $has_c_source = grep { $_ =~ m#^src/c/.+\.[ch]$# } keys %tree;
	my $has_c_src_builder = defined($tree{"src/c/buildsrc_c.sce"});
	if($has_c_source && !$has_c_src_builder) {
		die "Incorrect archives: C source found in src/c/ but no buildsrc_c.sce ".
		    "script found";
	}
	
	# if src/fortran contains at least a .f file, src/fortran/buildsrc_fortran.sce must exists
	my $has_f_source = grep { $_ =~ m#^src/fortran/.+\.f$# } keys %tree;
	my $has_f_src_builder = defined($tree{"src/fortran/buildsrc_fortran.sce"});
	if($has_f_source && !$has_f_src_builder) {
		die "Incorrect archives: Fortran source found in src/fortran/ ".
		    "but no buildsrc_fortran.sce script found";
	}
	
	# if src/*/buildsrc_*.sce exists, src/buildsrc.sce must exists 
	my $has_src_builder = defined($tree{"src/buildsrc.sce"});
	if(($has_f_source || $has_c_source) && !$has_src_builder) {
		die "Incorrect archive: sources file found but no buildsrc.sce script found";
	}
	
	# if sci_gateway/fortran contains at least a .c file,
	# sci_gateway/fortran/buildgateway_fortran.sce must exists.
	my $has_f_gateway = grep { m#^sci_gateway/fortran/.+\.[ch]$# } keys %tree;
	my $has_f_gateway_builder = defined($tree{"sci_gateway/fortran/buildgateway_fortran.sce"});
	if($has_f_gateway && !$has_f_gateway_builder) {
		die "Incorrect archive: Fortran gateway found but can't find any builder for it";
	}
	
	# if sci_gateway/c contains at least a .c file, sci_gateway/c/buildgateway_c.sce must exists
	my $has_c_gateway = grep { m#^sci_gateway/c/.+\.[ch]$# } keys %tree;
	my $has_c_gateway_builder = defined($tree{"sci_gateway/c/buildgateway_c.sce"});
	if($has_c_gateway && !$has_c_gateway_builder) {
		die "Incorrect archive: C gateway found but can't find any builder for it";
	}
	
	# if sci_gateway/*/buildgateway_*.sce exists, sci_gateway/buildgateway.sce must exists
	my $has_gateway_builder = defined($tree{"sci_gateway/buildgateway.sce"});
	if(($has_c_gateway || $has_f_gateway) && !$has_gateway_builder) {
		die "Incorrect archive: gateway found no gateway builder (buildgateway.sce) found";
	}
	
	# if help/ contains .xml files, it must contains a buildhelp.sce file
	my $has_help = grep { m#^help/.+\.xml$# } keys %tree;
	my $has_help_builder = defined($tree{"help/buildhelp.sce"});
	if($has_help && !$has_help_builder) {
		die "Incorrect archive: help files found but no help builder (buildhelp.sce) found";
	}
}

# Init global vars, check arguments
$TOOLBOXFILE = shift;
if(!defined($TOOLBOXFILE)) {
	die "Toolbox source file required";
}

if(! -r $TOOLBOXFILE) {
	die "$TOOLBOXFILE doesn't exists or can't be read";
}

$TOOLBOXNAME = $1 if ($TOOLBOXFILE =~ /^([^.]+)/);


check_tree(get_tree());