summaryrefslogtreecommitdiff
path: root/macos/configure
blob: 91a037690bed6c275293b8e67fa14ff1adf9e576 (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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
# This is a simple configure script for MacOS and MPW.
# Note that this script can't be run directly from MPW perl
# because it has the wrong end-of-line characters. See README.

# Copyright 2000, 2001 Free Software Foundation, Inc.
#
# This file is part of the GNU MP Library.
#
# The GNU MP Library is free software; you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; either version 2.1 of the License, or (at your
# option) any later version.
#
# The GNU MP Library is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
# or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
# License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with the GNU MP Library; see the file COPYING.LIB.  If not, write to
# the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
# MA 02111-1307, USA.

# ***************************** NOTE **************************************
# This script try to figure out things about this release but has a hard
# time doing it. It reads some lines in "configure", "Makefile.am" and
# "mp*/Makefile.am" and try to guess things. With every new release
# this script has to be tested.
# ***************************** NOTE **************************************

use strict;

###########################################################################
#
#  Platform dependent
#
###########################################################################

$/ = "\012"			# Input files use Unix end-of-line chars
  if $^O eq 'MacOS';

###########################################################################
#
#  Constants
#
###########################################################################

# Directories to create

my @dirs =
  (
   'MpfObj',
   'MpnObj',
   'MpqObj',
   'MpzObj',
   'CmnObj',
   'AsmObj',
   'Asm',
   'MpfBin',
   'MpqBin',
   'MpzBin',
  );

my $cfg;			# Will point to %cfg_apple or %cfg_cw

my %cfg_apple =
  (
   'cc'          =>  'MrC',
   'coptions'    => ['-ansi on -i : -i :: -i ::mpn:powerpc32 -i ::mpz',
		     '-opt speed -inline on'],
   'link'        => 'PPCLink',
   'linkoptions' => '-sym on -xm library',
   'tooloptions' => "-t 'MPST' -c 'MPS'",
   'asm'         => 'PPCAsm',
   'aoptions'    => '-sym on',
   'syslibs'     => [
		     '"{SharedLibraries}"StdCLib',
		     '"{SharedLibraries}"InterfaceLib',
		     '"{SharedLibraries}"MathLib',
		     '"{PPCLibraries}"StdCRuntime.o',
		     '"{PPCLibraries}"PPCCRuntime.o',
		    ],
  );

my %cfg_cw =
  (
   'cc'          =>  'MWCPPC',
   'coptions'    => [
		     '-opt all -w nounusedvar,noextended',
		     '-i :: -i ::mpn:powerpc32 -i ::mpz',
		     '-sym full',
		    ],
   'link'        => 'MWLinkPPC',
   'linkoptions' => '-sym fullpath -library',
   'tooloptions' => '-mpwtool',
   'asm'         => 'PPCAsm',
   'aoptions'    => '-sym on',
   'syslibs'     => [
		     '"{SharedLibraries}"InterfaceLib',
		     '"{MWPPCLibraries}MSL MPWCRuntime.Lib"',
		     '"{MWPPCLibraries}MSL C.PPC MPW(NL).Lib"',
		     '"{SharedLibraries}"MathLib',
		    ],
  );

# We only set the variables that have a value
my %vars =
  (
   '__GMP_BITS_PER_MP_LIMB' => 32,
   '__GMP_HAVE_HOST_CPU_FAMILY_power' => 0,
   '__GMP_HAVE_HOST_CPU_FAMILY_powerpc' => 1,
   'DEFN_LONG_LONG_LIMB' => '',
   '__GMP_LIBGMP_STATIC' => 0,
   '__GMP_LIBGMP_SHARED' => 0,
  );

my $make_in      = 'Makefile.in';
my $make         = 'Makefile';
my $configure    = mf("../configure");
my $configure_in = mf("../configure.in");
my $config_in    = mf("../config.in");
my $gmp_h        = mf('../gmp.h');
my $gmp_h_in     = mf('../gmp-h.in');
my $mpn_asm_dir  = "../mpn/powerpc32";
my $mpn_gen_dir  = "../mpn/generic";
my $config_h     = 'config.h';

my $asm_start    = "\ttoc";	# No dot, like ".dot"?

###########################################################################
#
#  Read command line
#
###########################################################################

$cfg = \%cfg_apple;		# Default

if (@ARGV and $ARGV[0] =~ /^cw|co/) {
  $cfg = \%cfg_cw;
  $make .= '.cw';
}

###########################################################################
#
#  Parse top configure file for mpn files
#
###########################################################################

my ($files,$optional,$package,$version) = parse_top_configure($configure);
my %mpn_objects = map {$_ => 1} (@$files,@$optional);
my %mpn_optional = map {$_ => 1} @$optional;

###########################################################################
#
#  Create config.h from ../config.in
#
###########################################################################

my @asm_files;

open(CONFIG_IN, $config_in)
  or die "Can't open \"$config_in\"\n";
open(CONFIG_H, ">$config_h")
  or die "Can't create \"$config_h\"\n";

while (<CONFIG_IN>) {
  chomp;

  if (/^\s*#\s*undef\s+HAVE_NATIVE_mpn_(\w+)/ and
      -r mf("$mpn_asm_dir/$1.asm")) {

    if (defined delete $mpn_objects{$1}) {
      push(@asm_files, $1);
      print CONFIG_H "\#define HAVE_NATIVE_mpn_$1 1\n";
    } else {
      print STDERR "Warning: Found asm file \"$1\" but no corresponding C file - ignoring\n";
    }

  } elsif (/^\s*#\s*undef\s+inline\b/) {
    print CONFIG_H "\#define inline\n";
  } elsif (/^\s*#\s*undef\s+HAVE_STDARG\b/) {
    print CONFIG_H "\#define HAVE_STDARG 1\n";
  } elsif (/^\s*#\s*undef\s+HAVE_STRCHR\b/) {
    print CONFIG_H "\#define HAVE_STRCHR 1\n";
  } elsif (/^\s*#\s*undef\s+HAVE_HOST_CPU_FAMILY_powerpc\b/) {
    print CONFIG_H "\#define HAVE_HOST_CPU_FAMILY_powerpc 1\n";
  } elsif (/^\s*#\s*undef\s+WANT_TMP_NOTREENTRANT\b/) {
    print CONFIG_H "\#define WANT_TMP_NOTREENTRANT 1\n";
  } elsif (/^\s*#\s*undef\s+PACKAGE\b/) {
    print CONFIG_H "\#define PACKAGE \"$package\"\n";
  } elsif (/^\s*#\s*undef\s+VERSION\b/) {
    print CONFIG_H "\#define VERSION \"$version\"\n";
  } elsif (/^\s*#\s*undef\s+STDC_HEADERS\b/) {
    print CONFIG_H "\#define STDC_HEADERS 1\n";
  } else {			# Blank line, leave it
    print CONFIG_H "$_\n";
  }
}

close CONFIG_H;
close CONFIG_IN;

###########################################################################
#
#  Create gmp.h from ../gmp-h.in
#
###########################################################################

open(GMP_H_IN, $gmp_h_in)
  or die "Can't open \"$gmp_h_in\"\n";
open(GMP_H, ">$gmp_h")
  or die "Can't create \"$gmp_h\"\n";

while (<GMP_H_IN>) {
  chomp;			# Remove whatever ending it was

  # Do the variable substitution

  s/\@([^\@]+)\@/exists $vars{$1} ? $vars{$1} : ''/ge;

  print GMP_H "$_\n";
}

close GMP_H;
close GMP_H_IN;

###########################################################################
#
#  Create directories
#
###########################################################################

foreach (@dirs) {
  -d $_ or mkdir $_, 0775
    or die "Can't create directory \"$_\"\n";
}

###########################################################################
#
#  Parse the *.asm files found and convert them to MPW format
#
###########################################################################

my $file;

foreach $file (@asm_files) {
  my $ifile = mf("$mpn_asm_dir/$file.asm");
  my $ofile = mf("Asm/$file.s");

  open(ASM, $ifile)
    or die "Can't read file \"$ifile\"\n";
  open(NEW, ">$ofile")
    or die "Can't create file \"$ofile\"\n";

  while (<ASM>) {
    chomp;			# Remove whatever ending it was

    s/\bdnl\b/;/ or s/\bC\b/;/;	# Use ; comments

    s/include\s*\(.*?\)//;	# Don't use include macro

    s/ASM_START\s*\(.*?\)/$asm_start/;

    s/PROLOGUE\s*\(\s*(.*?)\s*\)/asm_prologue($1)/e;

    s/EPILOGUE\s*\(\s*(.*?)\s*\)/asm_epilogue($1)/e;

    s/\n/\x0D/g;
    print NEW "$_\x0D";		# Use MacOS end-of-line character
  }
  
  close ASM;
  close NEW;
}


###########################################################################
#
#  Parse the Makefile.in and produce the Makefile
#
###########################################################################

# Check if we have optional left in C directory

foreach (keys %mpn_objects) {
  delete $mpn_objects{$_}
    if $mpn_optional{$_} and !-r mf("$mpn_gen_dir/$_.c");
}

my $mpn_objects = join(' ', map {"{MpnObjDir}$_.o"}  sort keys %mpn_objects);
$mpn_objects =~ s/(.{1,66})\s/$1 \xB6\x0D\t/g;

my @asm_objects = @asm_files;
my @asm_sources = @asm_files;

# Adjust configuration

foreach (keys %{$cfg}) {
  $$cfg{$_} = join(" \xB6\x0D\t\t", @{$$cfg{$_}})
    if ref $$cfg{$_};
}

my %config =
  (
   'version' => $version,
   'package' => $package,
   'c'       => "\xB6",
   'dep'     => "\xC4",
   'wildcard'=> "\xC5",
   'asm_objects' =>
   	join(" \xB6\x0D\t",map {$_ = "{AsmObjDir}$_.o"} sort @asm_objects),
   'asm_sources' =>
   	join(" \xB6\x0D\t",map {$_ = "{AsmSrcDir}$_.s"} sort @asm_sources),
   'mpn_objects' => $mpn_objects,
   'mpz_objects' => what_objects("mpz","../mpz","{MpzObjDir}"),
   'mpf_objects' => what_objects("mpf","../mpf","{MpfObjDir}"),
   'mpq_objects' => what_objects("mpq","../mpq","{MpqObjDir}"),
   'gmp_objects' => what_objects("gmp","..",    "{CmnObjDir}"),
   %{$cfg},
  );


open(IN, $make_in)
  or die "Can't read file \"$make_in\"\n";
open(OUT, ">$make")
  or die "Can't create file \"$make\"\n";

while (<IN>) {
  chomp;			# Remove whatever ending it was

  # Do the variable substitution

  s/\@([^\@]+)\@/exists $config{$1} ? $config{$1} : ''/ge;

  print OUT "$_\x0D";		# Use MacOS end-of-line character
}
  
close IN;
close OUT;

###########################################################################
#
#  Parse the configure.in file to find the mpn files to compile and link
#  Find package name and version
#
###########################################################################

sub parse_top_configure {
  my $cfg = shift;

  open(CONFIGURE, $cfg)
    or die "Can't open \"$cfg\"\n";
  my $old_eol = $/;
  undef $/;
  my $text = <CONFIGURE>;
  $/ = $old_eol;
  close CONFIGURE;

  my ($package) = $text =~ /(?:\n|\r)PACKAGE\s*=\s*(\S+)/;
  my ($version) = $text =~ /(?:\n|\r)VERSION\s*=\s*(\S+)/;

  my $files = join(' ',$text =~ /(?:\n|\r)gmp_mpn_functions\s*=\s*\"([^\"]+)/);
  $files =~ s/\\/ /g;
  $files =~ s/\$\{?\w*\}?//g;
  my @files = sort split(' ',$files);

  $files = join(' ',$text =~ /(?:\n|\r)gmp_mpn_functions_optional\s*=\s*\"([^\"]+)/);
  $files =~ s/\\/ /g;
  $files =~ s/\$\{?\w*\}?//g;
  my @optional = sort split(' ',$files);

  @files > 30 or die "Can't find mpn files in \"$cfg\"\n";
  defined $package or die "Can't find package name in \"$cfg\"\n";
  defined $version or die "Can't find version name in \"$cfg\"\n";
  return (\@files,\@optional,$package,$version);
}

###########################################################################
#
#  Find the C files for mpz, mpf .....
#
###########################################################################

sub what_objects {
  my $part   = shift;
  my $srcdir = shift;
  my $dstdir = shift;

  my $makefile_am = mf("$srcdir/Makefile.am");

  # We look in the Makefile.am file
  open(MAKEFILE_AM, $makefile_am)
    or die "Can't open file \"$makefile_am\"\n";

  # I had as short version of this using more advanced
  # regular expressions on the whole file content but
  # MacPerl freezes my Mac every time..... :-(

  my $line;
  my $text = '';

  while (defined($line = <MAKEFILE_AM>) ) {
    chomp $line;

    if ($line =~ s/^lib${part}_la_SOURCES\s*=//) {
      do {
	chomp $line;
	if ($line =~ s/\\\s*$//) {
	  $text .= " $line";
	} else {
	  $text .= " $line";
	  next;
	}
      } while (defined($line = <MAKEFILE_AM>));
    }

    if ($line =~ s/^nodist_lib${part}_la_SOURCES\s*=//) {
      do {
	chomp $line;
	if ($line =~ s/\\\s*$//) {
	  $text .= " $line";
	} else {
	  $text .= " $line";
	  last;
	}
      } while (defined($line = <MAKEFILE_AM>));
    }
  }
  close MAKEFILE_AM;

  my @ofiles = split(' ',$text);
  @ofiles > 10 or die "Can't find $part files in \"$makefile_am\"\n";
  my $ofiles = join(' ', map {/^(.+)\.c$/ and $_ = "$dstdir$1.o"} @ofiles);
  $ofiles =~ s/(.{1,66})\s/$1 \xB6\x0D\t/g;

  return $ofiles;
}

###########################################################################
#
#  Assembler
#
###########################################################################

sub asm_epilogue {
    my $func = shift;
    return "\tcsect .__g$func\[pr]";
}

sub asm_prologue {
    my $func = shift;

    my $asm = <<HERE;
	EXPORT __g$func\[DS]
	EXPORT .__g$func\[PR]

	TC __g$func\[TC], __g$func\[DS]
			
	CSECT __g$func\[DS]
	DC.L .__g$func\[PR]
	DC.L TOC[tc0]
		
	CSECT .__g$func\[PR]
	FUNCTION .__g$func\[PR]	
HERE
    return $asm;
}


###########################################################################
#
#  Platform dependent filename conversion
#
###########################################################################

sub mf {
  my $path = shift;

  return $path unless $^O eq 'MacOS';

  $path =~ /:/ and die "File name already converted to mac format: $path\n";

  if ($path =~ s&^/&&) {
    # Absolute path
    unless ($path =~ s&/&:&g) {
      # This is a drive name
      $path .= ':';
    }
  } else {
    # Relative path
    if ($path =~ s&/&:&g) {
      # Contains slash
      $path = ":$path";
      $path =~ s&\.\.:&:&g;
    } else {
      # Plain file name, no directory part
    }
  }
  return $path;
}