James Keenan via RT wrote:
A participant in this weekend's hackathon in Toronto posed this question:

"Invoking the compiler on a simple source file, then checking that the
generated code exists seems such an obvious test that there must be a
fatal flaw in it. What am I missing?"

This patch grew out of Hackathon Toronto and was posted to RT, but not initially cc-ed to the list. Please review this patch. Thank you very much.

kid51


The attached patch to inter/progs.pm is my attempt to improve
Configure's behaviour.

It writes an embedded minimal C program to disk. (If there's already a
file called "minimal.c" it complains and dies.) It then attempts to
call the specified compiler. If the result isn't an a.out, it dies
promptly and gracefully. (It can be fooled, if something other than a
C compiler writes an a.out; that's how I tested it, but that's really
a pathological case.)

The comments should explain what's going on.



Index: progs.pm
===================================================================
--- progs.pm (revision 18351)
+++ progs.pm (working copy)
@@ -38,14 +38,31 @@
# If none work, then set to null command.
# XXX need config support for a null command.
my $null = 'echo';
- my $first_working = sub {
+ my $output = 'a.out';
+ my $min = 'minimal.c'; # File name probably not in use
+ if (-f $min) { # but die if it is
+ warn "\nPlease rename minimal.c - I create it\n" ;
+ exit 1;
+ }
+ open(MIN, ">".$min);
+ print MIN <DATA>; # Create test program (from U-Haul)
+ close MIN;
+ unlink($output) if -f $output; # in case there is rubbish lying around
+
+ my $first_working = sub {
foreach (@_) {
- `$_ -h 2>&1`;
- return $_ if not $?;
+
+ system ($_, $min) ; # Attempt simplest possible compile
+ if (-f $output) # See if it produced something
+ {
+ unlink($output, $min); # Sweep up the debris
+ return $_ ; # And return the successful program name
+ }
}
+
+ unlink($min); # Tidy up, even after failure
return $null;
- };
-
+ };
my $ask = $conf->options->get('ask');
if ($ask) {
print <<'END';
@@ -66,6 +83,10 @@
$cc = integrate( $conf->data->get('cc'), $conf->options->get('cc') );
$cc = prompt( "What C compiler do you want to use?", $cc )
if $ask;
+ if ($first_working->($cc) eq 'echo') {
+ warn "No compiler found (tried '$cc')\n";
+ exit 1;
+ }
$conf->data->set( cc => $cc );

$link = integrate( $conf->data->get('link'), $conf->options->get('link') );
@@ -139,3 +160,13 @@
# fill-column: 100
# End:
# vim: expandtab shiftwidth=4:
+__DATA__
+/* Start of minimal.c - any C compiler should create a.out from this */
+include <stdio.h>
+
+int main()
+{
+ printf ("Compiler found \n");
+}
+
+/* End of minimal.c */

Reply via email to