| 1 | #!/usr/bin/perl | ||
| 2 | |||
| 3 | # Copyright 2007, 2008, 2009, 2010, 2011, 2014 Kevin Ryde | ||
| 4 | |||
| 5 | # This file is part of Devel-Mallinfo. | ||
| 6 | |||
| 7 | # Devel-Mallinfo is free software; you can redistribute it and/or modify it | ||
| 8 | # under the terms of the GNU General Public License as published by the Free | ||
| 9 | # Software Foundation; either version 3, or (at your option) any later | ||
| 10 | # version. | ||
| 11 | |||
| 12 | # Devel-Mallinfo is distributed in the hope that it will be useful, but | ||
| 13 | # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY | ||
| 14 | # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | ||
| 15 | # for more details. | ||
| 16 | |||
| 17 | # You should have received a copy of the GNU General Public License along | ||
| 18 | # with Devel-Mallinfo. If not, see <http://www.gnu.org/licenses/>. | ||
| 19 | |||
| 20 | |||
| 21 | |||
| 22 | # Usage: perl config.pl makeprog objext exeext | ||
| 23 | # | ||
| 24 | # Generate a "config.h" file for use by Mallinfo.xs. Normally this is run | ||
| 25 | # from Makefile.PL, with $(MAKE), $(OBJ_EXT) and $(EXE_EXT) as arguments. | ||
| 26 | # Those args are used to run "make" on feature test programs. There's | ||
| 27 | # probably better ways to do C configury like this, but for one header, one | ||
| 28 | # struct, and the struct contents it's enough. | ||
| 29 | # | ||
| 30 | # Being separate from Makefile.PL lets this script use the $(CC) and rules | ||
| 31 | # in the Makefile. Thus for instance | ||
| 32 | # | ||
| 33 | # perl Makefile.PL OPTIMIZE=-g | ||
| 34 | # | ||
| 35 | # gets that OPTIMIZE setting used in the configure tests. Overrides at the | ||
| 36 | # make stage however will depend on make having the $(MAKEFLAGS) environment | ||
| 37 | # variable mechanism like GNU make has. For instance | ||
| 38 | # | ||
| 39 | # perl Makefile.PL | ||
| 40 | # make OPTIMIZE=-g # only with MAKEFLAGS sub-make mechanism | ||
| 41 | # | ||
| 42 | |||
| 43 | use strict; | ||
| 44 | use POSIX; | ||
| 45 | |||
| 46 | # Ensure output goes immediately so messages are in the right place relative | ||
| 47 | # to external program runs. Perl output is already supposed to be flushed | ||
| 48 | # before system() and friends in 5.6 and up, but as per the perlfunc man | ||
| 49 | # page this might not be the case on all platforms. | ||
| 50 | select(STDERR); | ||
| 51 | $| = 1; | ||
| 52 | select(STDOUT); | ||
| 53 | $| = 1; | ||
| 54 | |||
| 55 | # defaults just for running "perl config.pl" to try it out | ||
| 56 | my $make = $ARGV[0] || 'make'; | ||
| 57 | my $objext = $ARGV[1] || '.o'; | ||
| 58 | my $exeext = $ARGV[2] || ''; | ||
| 59 | print "config.pl: settings\n"; | ||
| 60 | print " MAKE=$make\n"; | ||
| 61 | print " OBJ_EXT=$objext\n"; | ||
| 62 | print " EXE_EXT=$exeext\n"; | ||
| 63 | |||
| 64 | sub unlink_maybe { | ||
| 65 | my ($filename) = @_; | ||
| 66 | if (unlink ($filename) == 0) { | ||
| 67 | if ($! != ENOENT) { | ||
| 68 | die "Cannot delete file $filename: $!"; | ||
| 69 | } | ||
| 70 | } | ||
| 71 | } | ||
| 72 | |||
| 73 | sub write_file { | ||
| 74 | my ($filename, $content) = @_; | ||
| 75 | (open (OUT, ">$filename") | ||
| 76 | and print OUT $content | ||
| 77 | and close OUT) | ||
| 78 | or die "Cannot write to file $filename: $@"; | ||
| 79 | } | ||
| 80 | |||
| 81 | sub read_file { | ||
| 82 | my ($filename) = @_; | ||
| 83 | my $content; | ||
| 84 | (open IN, "<$filename" | ||
| 85 | and do { local $/ = undef; $content = <IN> } | ||
| 86 | and close IN) | ||
| 87 | or die "Cannot read $filename: $@"; | ||
| 88 | return $content; | ||
| 89 | } | ||
| 90 | |||
| 91 | # run "make" on $target, return 1 for success, 0 for fail | ||
| 92 | sub make { | ||
| 93 | my ($target) = @_; | ||
| 94 | print "$make $target\n"; | ||
| 95 | # MSDOS builds of perl don't always have WIFEXITED in the POSIX module :-(, | ||
| 96 | # so just look for 0 as the wait status for exit code 0 (success) | ||
| 97 | return (system ("$make $target") == 0 ? 1 : 0); | ||
| 98 | } | ||
| 99 | |||
| 100 | # attempt to compile and link the program code in $code to produce an | ||
| 101 | # executable, return 1 if successful, 0 if not | ||
| 102 | sub try_compile_and_link { | ||
| 103 | my ($code) = @_; | ||
| 104 | unlink_maybe ("a.out"); | ||
| 105 | unlink_maybe ("conftest$exeext"); | ||
| 106 | unlink_maybe ("conftest$objext"); | ||
| 107 | unlink_maybe ("conftest.i"); | ||
| 108 | write_file ('conftest.c', | ||
| 109 | "/* generated by config.pl */\n" . $code); | ||
| 110 | # using .c.o rule from MakeMaker, plus conftest-link rule in Makefile.PL | ||
| 111 | return make ("conftest-link"); | ||
| 112 | } | ||
| 113 | |||
| 114 | #------------------------------------------------------------------------------ | ||
| 115 | # $CC availability | ||
| 116 | |||
| 117 | print "config.pl: testing whether compiler works\n"; | ||
| 118 | my $compiler_works = try_compile_and_link (' | ||
| 119 | int main (void) { return 0; } | ||
| 120 | '); | ||
| 121 | if (! $compiler_works) { | ||
| 122 | die "Oops, our compile+link rule in the Makefile doesn't work"; | ||
| 123 | } | ||
| 124 | |||
| 125 | #------------------------------------------------------------------------------ | ||
| 126 | # malloc.h availability/usability: | ||
| 127 | # - freebsd has deliberate #error in malloc.h, which is very stupid | ||
| 128 | # - darwin doesn't have malloc.h at all | ||
| 129 | # | ||
| 130 | print "config.pl: testing whether <malloc.h> is available\n"; | ||
| 131 | my $have_malloc_h = try_compile_and_link (' | ||
| 132 | #include <malloc.h> | ||
| 133 | int main (void) { return 0; } | ||
| 134 | '); | ||
| 135 | print "config.pl: <malloc.h> ", ($have_malloc_h?"is":"is not"), " available\n"; | ||
| 136 | |||
| 137 | my $hash_include_malloc_h = ($have_malloc_h ? "#include <malloc.h>\n" : ""); | ||
| 138 | |||
| 139 | |||
| 140 | #------------------------------------------------------------------------------ | ||
| 141 | # mallinfo() availability | ||
| 142 | |||
| 143 | print "config.pl: testing whether mallinfo() function is available\n"; | ||
| 144 | my $have_mallinfo = try_compile_and_link (" | ||
| 145 | #include <stdlib.h> | ||
| 146 | $hash_include_malloc_h | ||
| 147 | int main (void) { | ||
| 148 | struct mallinfo m; | ||
| 149 | m = mallinfo (); | ||
| 150 | return (m.arena != 0); | ||
| 151 | } | ||
| 152 | "); | ||
| 153 | if ($have_mallinfo) { | ||
| 154 | print "config.pl: mallinfo() is available\n"; | ||
| 155 | } else { | ||
| 156 | print <<EOF; | ||
| 157 | +------------------------------------------+ | ||
| 158 | + WARNING: | | ||
| 159 | + WARNING: mallinfo() not available | | ||
| 160 | + WARNING: Devel::Mallinfo will do nothing | | ||
| 161 | + WARNING: | | ||
| 162 | +------------------------------------------+ | ||
| 163 | EOF | ||
| 164 | } | ||
| 165 | |||
| 166 | #------------------------------------------------------------------------------ | ||
| 167 | # struct mallinfo fields | ||
| 168 | |||
| 169 | my @mallinfo_fields; | ||
| 170 | if ($have_mallinfo) { | ||
| 171 | print "config.pl: extracting fields from struct mallinfo\n"; | ||
| 172 | # using conftest.c program above and the .c.i rule from MakeMaker | ||
| 173 | if (! make ('conftest.i')) { | ||
| 174 | if (! make ('conftest-fallback-i')) { | ||
| 175 | die "Oops, can't make conftest.i"; | ||
| 176 | } | ||
| 177 | } | ||
| 178 | my $preproc = read_file ('conftest.i'); | ||
| 179 | |||
| 180 | if (! ($preproc =~ /struct\s+mallinfo\s*\{(.*?)}/s)) { | ||
| 181 | print "\"struct mallinfo\" not found in conftest.i preprocessed output\n"; | ||
| 182 | |||
| 183 | } else { | ||
| 184 | my $body = $1; # the insides of the struct | ||
| 185 | while ($body =~ /([A-Za-z0-9_]+)\s*;/g) { # field name before each ";" | ||
| 186 | push @mallinfo_fields, $1; | ||
| 187 | } | ||
| 188 | if (@mallinfo_fields) { | ||
| 189 | print "struct mallinfo fields found:\n ",join(' ',@mallinfo_fields),"\n"; | ||
| 190 | } else { | ||
| 191 | print "no fields matched in \"struct mallinfo\" body\n"; | ||
| 192 | } | ||
| 193 | } | ||
| 194 | |||
| 195 | if (! @mallinfo_fields) { | ||
| 196 | print <<EOF; | ||
| 197 | +----------------------------------------------+ | ||
| 198 | + WARNING: | | ||
| 199 | + WARNING: cannot grep struct mallinfo fields, | | ||
| 200 | + WARNING: using default fallbacks | | ||
| 201 | + WARNING: | | ||
| 202 | +----------------------------------------------+ | ||
| 203 | EOF | ||
| 204 | @mallinfo_fields = qw(arena ordblks smblks hblks hblkhd | ||
| 205 | usmblks fsmblks uordblks fordblks keepcost); | ||
| 206 | } | ||
| 207 | } | ||
| 208 | |||
| 209 | |||
| 210 | #------------------------------------------------------------------------------ | ||
| 211 | # malloc_info() availability | ||
| 212 | |||
| 213 | print "config.pl: testing whether malloc_info() function is available\n"; | ||
| 214 | my $have_malloc_info = try_compile_and_link (" | ||
| 215 | #include <stdio.h> | ||
| 216 | #include <stdlib.h> | ||
| 217 | $hash_include_malloc_h | ||
| 218 | int main (void) { | ||
| 219 | malloc_info(0,stdout); | ||
| 220 | return 0; | ||
| 221 | } | ||
| 222 | "); | ||
| 223 | if ($have_malloc_info) { | ||
| 224 | print "config.pl: malloc_info() is available\n"; | ||
| 225 | } else { | ||
| 226 | print "config.pl: malloc_info() not available\n"; | ||
| 227 | } | ||
| 228 | |||
| 229 | |||
| 230 | #------------------------------------------------------------------------------ | ||
| 231 | # malloc_stats() availability | ||
| 232 | |||
| 233 | print "config.pl: testing whether malloc_stats() function is available\n"; | ||
| 234 | my $have_malloc_stats = try_compile_and_link (" | ||
| 235 | #include <stdlib.h> | ||
| 236 | $hash_include_malloc_h | ||
| 237 | int main (void) { | ||
| 238 | malloc_stats(); | ||
| 239 | return 0; | ||
| 240 | } | ||
| 241 | "); | ||
| 242 | if ($have_malloc_stats) { | ||
| 243 | print "config.pl: malloc_stats() is available\n"; | ||
| 244 | } else { | ||
| 245 | print "config.pl: malloc_stats() not available\n"; | ||
| 246 | } | ||
| 247 | |||
| 248 | |||
| 249 | #------------------------------------------------------------------------------ | ||
| 250 | # malloc_trim() availability | ||
| 251 | |||
| 252 | print "config.pl: testing whether malloc_trim() function is available\n"; | ||
| 253 | my $have_malloc_trim = try_compile_and_link (" | ||
| 254 | #include <stdlib.h> | ||
| 255 | $hash_include_malloc_h | ||
| 256 | int main (void) { | ||
| 257 | malloc_trim(1); | ||
| 258 | return 0; | ||
| 259 | } | ||
| 260 | "); | ||
| 261 | if ($have_malloc_trim) { | ||
| 262 | print "config.pl: malloc_trim() is available\n"; | ||
| 263 | } else { | ||
| 264 | print "config.pl: malloc_trim() not available\n"; | ||
| 265 | } | ||
| 266 | |||
| 267 | |||
| 268 | #------------------------------------------------------------------------------ | ||
| 269 | |||
| 270 | open OUT, '>config.h' or die; | ||
| 271 | print OUT "/* generated by config.pl -- DO NOT EDIT */ | ||
| 272 | |||
| 273 | #define HAVE_MALLOC_H $have_malloc_h | ||
| 274 | #define HAVE_MALLINFO $have_mallinfo | ||
| 275 | #define HAVE_MALLOC_INFO $have_malloc_info | ||
| 276 | #define HAVE_MALLOC_STATS $have_malloc_stats | ||
| 277 | #define HAVE_MALLOC_TRIM $have_malloc_trim | ||
| 278 | |||
| 279 | #define STRUCT_MALLINFO_FIELDS \\ | ||
| 280 | "; | ||
| 281 | my $field; | ||
| 282 | foreach $field (@mallinfo_fields) { | ||
| 283 | print OUT " FIELD ($field); \\\n"; | ||
| 284 | } | ||
| 285 | print OUT " /*end*/\n"; | ||
| 286 | close OUT or die; | ||
| 287 | |||
| 288 | exit 0; |