677ae52a8c2e30a37c2d528698c126b6523af88f
[ghc.git] / boot-pkgs
1 #!/usr/bin/perl -w
2
3 use strict;
4
5 use File::Path 'rmtree';
6 use File::Basename;
7
8 my @library_dirs = ();
9 my @tarballs = glob("libraries/tarballs/*");
10
11 my $tarball;
12 my $package;
13 my $stamp;
14
15 for $tarball (@tarballs) {
16     $package = $tarball;
17     $package =~ s#^libraries/tarballs/##;
18     $package =~ s/-[0-9.]*(-snapshot)?\.tar\.gz$//;
19
20     # Sanity check, so we don't rmtree the wrong thing below
21     if (($package eq "") || ($package =~ m#[/.\\]#)) {
22         die "Bad package name: $package";
23     }
24
25     if (-d "libraries/$package/_darcs") {
26         print "Ignoring libraries/$package as it looks like a darcs checkout\n"
27     }
28     else {
29         $stamp = "libraries/stamp/$package";
30         if ((! -d "libraries/$package") || (! -f "$stamp")
31          || ((-M "libraries/stamp/$package") > (-M $tarball))) {
32             print "Unpacking $package\n";
33             if (-d "libraries/$package") {
34                 &rmtree("libraries/$package")
35                     or die "Can't remove libraries/$package: $!";
36             }
37             mkdir "libraries/$package"
38                 or die "Can't create libraries/$package: $!";
39             system ("sh", "-c", "cd 'libraries/$package' && { cat ../../$tarball | gzip -d | tar xf - ; } && mv */* .") == 0
40                 or die "Failed to unpack $package";
41             open STAMP, "> $stamp"
42                 or die "Failed to open stamp file: $!";
43             close STAMP
44                 or die "Failed to close stamp file: $!";
45         }
46     }
47 }
48
49 for $package (glob "libraries/*/") {
50     $package =~ s/\/$//;
51     my $pkgs = "$package/ghc-packages";
52     if (-f $pkgs) {
53         open PKGS, "< $pkgs"
54             or die "Failed to open $pkgs: $!";
55         while (<PKGS>) {
56             chomp;
57             if (/.+/) {
58                 push @library_dirs, "$package/$_";
59             }
60         }
61     }
62     else {
63         push @library_dirs, $package;
64     }
65 }
66
67 for $package (@library_dirs) {
68     my $dir = &basename($package);
69     my @cabals = glob("$package/*.cabal");
70     if ($#cabals > 0) {
71         die "Too many .cabal file in $package\n";
72     }
73     if ($#cabals eq 0) {
74         my $cabal = $cabals[0];
75         my $pkg;
76         my $stage;
77         my $top;
78         if (-f $cabal) {
79             $pkg = $cabal;
80             $pkg =~ s#.*/##;
81             $pkg =~ s/\.cabal$//;
82             if (-f "$package/ghc-stage") {
83                 open STAGE, "< $package/ghc-stage"
84                     or die "Can't open $package/ghc-stage: $!";
85                 $stage = int(<STAGE>);
86                 close STAGE
87                     or die "Failed closing $package/ghc-stage: $!";
88             }
89             else {
90                 $stage = 1;
91             }
92             $top = $package;
93             $top =~ s#[^/]+#..#g;
94
95             print "Creating $package/ghc.mk\n";
96             open GHCMK, "> $package/ghc.mk"
97                 or die "Opening $package/ghc.mk failed: $!";
98             print GHCMK "${package}_PACKAGE = ${pkg}\n";
99             print GHCMK "${package}_dist-install_GROUP = libraries\n";
100             print GHCMK "\$(eval \$(call build-package,${package},dist-install,${stage}))\n";
101             close GHCMK
102                 or die "Closing $package/ghc.mk failed: $!";
103
104             print "Creating $package/GNUmakefile\n";
105             open GNUMAKEFILE, "> $package/GNUmakefile"
106                 or die "Opening $package/GNUmakefile failed: $!";
107             print GNUMAKEFILE "dir = ${package}\n";
108             print GNUMAKEFILE "TOP = ${top}\n";
109             print GNUMAKEFILE "include \$(TOP)/mk/sub-makefile.mk\n";
110             print GNUMAKEFILE "FAST_MAKE_OPTS += stage=0\n";
111             close GNUMAKEFILE
112                 or die "Closing $package/GNUmakefile failed: $!";
113         }
114     }
115 }
116