second attempt at updating submodules; I seem to have trouble with this...
[ghc-hetmet.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         if (! -d "libraries/stamp") {
30             mkdir "libraries/stamp";
31         }
32         $stamp = "libraries/stamp/$package";
33         if ((! -d "libraries/$package") || (! -f "$stamp")
34          || ((-M "libraries/stamp/$package") > (-M $tarball))) {
35             print "Unpacking $package\n";
36             if (-d "libraries/$package") {
37                 &rmtree("libraries/$package")
38                     or die "Can't remove libraries/$package: $!";
39             }
40             mkdir "libraries/$package"
41                 or die "Can't create libraries/$package: $!";
42             system ("sh", "-c", "cd 'libraries/$package' && { cat ../../$tarball | gzip -d | tar xf - ; } && mv */* .") == 0
43                 or die "Failed to unpack $package";
44             open STAMP, "> $stamp"
45                 or die "Failed to open stamp file: $!";
46             close STAMP
47                 or die "Failed to close stamp file: $!";
48         }
49     }
50 }
51
52 for $package (glob "libraries/*/") {
53     $package =~ s/\/$//;
54     my $pkgs = "$package/ghc-packages";
55     if (-f $pkgs) {
56         open PKGS, "< $pkgs"
57             or die "Failed to open $pkgs: $!";
58         while (<PKGS>) {
59             chomp;
60             if (/.+/) {
61                 push @library_dirs, "$package/$_";
62             }
63         }
64     }
65     else {
66         push @library_dirs, $package;
67     }
68 }
69
70 for $package (@library_dirs) {
71     my $dir = &basename($package);
72     my @cabals = glob("$package/*.cabal");
73     if ($#cabals > 0) {
74         die "Too many .cabal file in $package\n";
75     }
76     if ($#cabals eq 0) {
77         my $cabal = $cabals[0];
78         my $pkg;
79         my $top;
80         if (-f $cabal) {
81             $pkg = $cabal;
82             $pkg =~ s#.*/##;
83             $pkg =~ s/\.cabal$//;
84             $top = $package;
85             $top =~ s#[^/]+#..#g;
86             $dir = $package;
87             $dir =~ s#^libraries/##g;
88
89             print "Creating $package/ghc.mk\n";
90             open GHCMK, "> $package/ghc.mk"
91                 or die "Opening $package/ghc.mk failed: $!";
92             print GHCMK "${package}_PACKAGE = ${pkg}\n";
93             print GHCMK "${package}_dist-install_GROUP = libraries\n";
94             print GHCMK "\$(eval \$(call build-package,${package},dist-install,\$(if \$(filter ${dir},\$(STAGE2_PACKAGES)),2,1)))\n";
95             close GHCMK
96                 or die "Closing $package/ghc.mk failed: $!";
97
98             print "Creating $package/GNUmakefile\n";
99             open GNUMAKEFILE, "> $package/GNUmakefile"
100                 or die "Opening $package/GNUmakefile failed: $!";
101             print GNUMAKEFILE "dir = ${package}\n";
102             print GNUMAKEFILE "TOP = ${top}\n";
103             print GNUMAKEFILE "include \$(TOP)/mk/sub-makefile.mk\n";
104             print GNUMAKEFILE "FAST_MAKE_OPTS += stage=0\n";
105             close GNUMAKEFILE
106                 or die "Closing $package/GNUmakefile failed: $!";
107         }
108     }
109 }
110