2 ######################################################################
4 # Runs any module configuration tests
6 # Called (currently) from syncqt, and expects a few arguments
8 # configtests $basedir $out_basedir $qtbasedir $quietmode
10 # Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
11 # Contact: Nokia Corporation (qt-info@nokia.com)
13 ######################################################################
18 # use packages -------------------------------------------------------
20 use File::Path 'mkpath';
21 use File::Spec::Functions;
26 # Which file to look for the %configtests variable in
27 my $configTestSource = "sync.profile";
31 warn " $0 <module base directory> <module output directory> <QtBase directory> <generator spec>\n";
35 # These might be needed in sync.profile
36 our $basedir = $ARGV[0];
37 our $out_basedir = $ARGV[1];
38 our $qtbasedir = $ARGV[2];
39 my $generator = $ARGV[3];
43 my $qmakeCachePath = catfile($out_basedir, ".qmake.cache");
45 my $QMAKE = catfile($qtbasedir, "bin", ($^O =~ /win32/i) ? 'qmake.exe' : 'qmake');
47 # try the qmake from the path (e.g. this is a shadow build)
51 # Need to use the right make
52 # SYMBIAN_UNIX/MINGW should fall back to the non SYMBIAN ones
53 my $MAKE = 'make'; # default, only works on unix
54 if ($generator =~ /UNIX|XCODE/i) { # XCODE = make?
56 } elsif ($generator =~ /MINGW/i) {
57 $MAKE = 'mingw32-make';
58 } elsif ($generator =~ /MSVC.NET|MSBUILD/i) {
61 # Unhandled (at least): BMAKE, GBUILD, SYMBIAN_ABLD, SYMBIAN_SBSV2
62 warn "Unrecognized generator spec ($generator) - assuming '$MAKE'\n";
65 ######################################################################
66 # Syntax: fileContents(filename)
67 # Params: filename, string, filename of file to return contents
69 # Purpose: Get the contents of a file.
70 # Returns: String with contents of the file, or empty string if file
72 # Warning: Dies if it does exist but script cannot get read access.
73 ######################################################################
76 my $filecontents = "";
78 open(I, "< $filename") || die "Could not open $filename for reading, read block?";
87 ######################################################################
88 # Syntax: loadConfigTests()
90 # Purpose: Loads the config tests from the source basedir into %configtests.
92 ######################################################################
94 my $configprofile = catfile($basedir, $configTestSource);
96 unless ($result = do $configprofile) {
97 die "configtests couldn't parse $configprofile: $@\n" if $@;
98 # We don't check for non null output, since that is valid
102 ######################################################################
103 # Syntax: hashesAreDifferent
105 # Purpose: Compares two hashes. (must have same key=value for everything)
106 # Returns: 0 if they are the same, 1 otherwise
107 ######################################################################
108 sub hashesAreDifferent {
112 if (keys %a != keys %b) {
116 my %cmp = map { $_ => 1 } keys %a;
117 for my $key (keys %b) {
118 last unless exists $cmp{$key};
119 last unless $a{$key} eq $b{$key};
129 ######################################################################
130 # Syntax: executeSomething
131 # Params: A list of things.
133 # Purpose: Executes the first arg, passing the list.
134 # stderr is redirected to stdout, and the output is captured.
135 # Returns: The output.
136 ######################################################################
137 sub executeSomething {
138 my ($program, @args) = @_;
140 my $pid = open(KID_TO_READ, "-|");
145 while (<KID_TO_READ>) {
146 $output = $output . $_;
148 close(KID_TO_READ) || $! == 0 || warn "\nFailed to execute $program: exited $?";
150 # redirect STDERR to STDOUT
151 open STDERR, ">&STDOUT";
154 exec ($program, @args) || die "\nCan't exec $program: $!\n";
161 ######################################################################
162 # Syntax: executeTest()
165 # The testName variable controls the actual config test run - the
166 # source is assumed to be in $basedir/config.tests/$testName, and
167 # when 'qmake; make clean; make' is run, is expected to produce a file
168 # $out_basedir/config.tests/$testName/$testName. If this test passes,
169 # then 'config_test_$testName = yes' will be written to $out_basedir/.qmake.cache
171 # Purpose: Runs a configuration time test.
172 # Returns: 0 if the test fails, 1 if it passes, 2 if the test is skipped
173 # (e.g. .pro file has requires(x) and x is not satisfied)
174 ######################################################################
178 my $oldWorkingDir = getcwd();
181 my @QMAKEARGS = ('CONFIG-=debug_and_release');
183 my $testOutDir = catdir($out_basedir, 'config.tests', $testName);
185 # Since we might be cross compiling, look for barename (Linux) and .exe (Win32/Symbian)
186 my $testOutFile1 = catfile($testOutDir, "$testName.exe");
187 my $testOutFile2 = catfile($testOutDir, $testName);
189 if (abs_path($basedir) eq abs_path($out_basedir)) {
190 chdir $testOutDir or die "\nUnable to change to config test directory ($testOutDir): $!\n";
191 } else { # shadow build
192 if (! -e $testOutDir) {
193 mkpath $testOutDir or die "\nUnable to create shadow build config test directory ($testOutDir): $!\n";
195 chdir $testOutDir or die "\nUnable to change to config test directory ($testOutDir): $!\n";
197 push (@QMAKEARGS, catdir($basedir, 'config.tests', $testName));
200 # First remove existing stuff (XXX this probably needs generator specific code, but hopefully
201 # the target removal below will suffice)
203 executeSomething($MAKE, 'distclean');
206 # and any targets that we might find that weren't distcleaned
207 unlink $testOutFile1, $testOutFile2;
210 executeSomething($QMAKE, @QMAKEARGS);
211 my $makeOutput = executeSomething(($MAKE));
213 # If make prints "blah blah blah\nSkipped." we consider this a skipped test
214 if ($makeOutput !~ qr(^Skipped\.$)ms) {
215 # Check the test exists (can't reliably execute, especially for cross compilation)
216 if (-e $testOutFile1 or -e $testOutFile2) {
223 chdir $oldWorkingDir or die "\nUnable to restore working directory: $!\n";
227 # Now run configuration tests
228 # %configtests is a map from config test name to a map of parameters
232 # "simple" => {fatal => 1, message => "Missing required 'simple' component\n"},
233 # "failed" => {message => "You need to install the FAILED sdk for this to work\n"}
236 # Parameters and their defaults:
237 # - fatal [false] - whether failing this test should abort everything
238 # - message [""] - A special message to display if this test fails
242 # Only do this step for modules that have config tests
243 # (qtbase doesn't). We try to preserve existing contents (and furthermore
244 # only write to .qmake.cache if the tests change)
245 if (abs_path($out_basedir) ne abs_path($qtbasedir)) {
246 # Read any existing content
247 my $existingContents = fileContents($qmakeCachePath);
250 my @fatalTestsEncountered;
252 # Parse the existing results so we can check if we change them
253 while ($existingContents =~ /^config_test_(.*) = (yes|no)$/gm) {
254 $oldTestResults{$1} = $2;
257 # Get the longest length test name so we can pretty print
258 use List::Util qw(max);
259 my $maxNameLength = max map { length $_ } keys %configtests;
264 # Now run the configuration tests
265 print "Configuration tests:\n" if (%configtests);
267 while ((my $testName, my $testParameters) = each %configtests) {
268 printf " % *s: ", $maxNameLength, $testName; # right aligned, yes/no lines up
270 my $fatalTest = $testParameters->{"fatal"};
271 my $message = $testParameters->{"message"};
273 my $testResult = executeTest($testName);
274 my @testResultStrings = ("no\n","yes\n","skipped\n");
276 $newTestResults{$testName} = (($testResult == 1) ? "yes" : "no"); # skipped = no
278 if ($testResult == 0) {
281 print "no (fatal)\n";
282 # Report the fatality at the end, too
283 push (@fatalTestsEncountered, $testName);
287 if (defined($message)) {
289 print "\n" unless chop $message eq "\n";
293 print $testResultStrings[$testResult];
297 # Check if the test results are different
298 if (hashesAreDifferent(\%oldTestResults, \%newTestResults)) {
299 # Generate the new contents
300 my $newContents = $existingContents;
302 # Strip out any existing config test results
303 $newContents =~ s/^config_test_.*$//gms;
304 $newContents =~ s/^# Compile time test results.*$//gms;
306 # Add any remaining content and make sure we start on a new line
307 if ($newContents and chop $newContents ne '\n') {
308 $newContents = $newContents . "\n";
312 if (%newTestResults) {
313 $newContents = $newContents . '# Compile time test results ('.(localtime).")\n";
316 while ((my $testName, my $testResult) = each %newTestResults) {
317 $newContents = $newContents . "config_test_$testName = $testResult\n";
322 open my $cacheFileHandle, ">$qmakeCachePath" or die "Unable to open $qmakeCachePath for writing: $!\n";
324 print $cacheFileHandle $newContents;
326 close $cacheFileHandle or die "Unable to close $qmakeCachePath: $!\n";
329 # Now see if we have to die
330 if (@fatalTestsEncountered) {
331 if ($#fatalTestsEncountered == 0) {
332 warn "Mandatory configuration test (".$fatalTestsEncountered[0].") failed.\n\n";
334 warn "Mandatory configuration tests (". join (", ", @fatalTestsEncountered) . ") failed.\n\n";