root/main/trunk/package-kits/linux/generate @ 29508

Revision 29508, 4.7 KB (checked in by jts21, 6 years ago)

Makefile generater now also generates package manager scripts. Generated scripts are tested and working on Arch

  • Property svn:executable set to *
Line 
1#!/usr/bin/env perl
2
3# Generates Makefiles for different systems and packages
4# Uses a hashmap to do config variables  (see readvar for how variables are
5#  read in and an example of how they are resolved / used)
6
7use strict;
8use warnings;
9use utf8;
10use File::Basename;
11use File::Path 'make_path';
12use File::Copy 'cp';
13use Storable 'dclone';
14use Digest::MD5;
15
16
17my $USAGE = "Usage: " . basename($0) . " <platforms> <packages>
18    platforms: A comma separated list of the platforms defined in platforms/
19               Or 'all' (for all platforms)
20    packages:  A comma separated list of the packages defined in packages/
21               Or 'all' (for all packages)
22";
23
24$#ARGV == 1 or die $USAGE;
25
26sub globbify {
27    my ($dir, $files) = @_;
28    $files =~ /^all$/i and return glob "$dir/*";
29    my @list = glob "$dir/{$files}";
30    for (@list) {
31        -f or die "Could not find '", $_, "'";
32    }
33    return @list;
34}
35
36sub empty {
37    /^\s*$/ and return 1;
38}
39
40sub comment {
41    /^\s*#/ and return 1;
42}
43
44sub trim {
45    for (@_) {
46        s/^\s+|\s+$//g;
47    }
48    return @_;
49}
50
51sub copydir {
52    my ($src, $dst, $subst) = @_;
53    mkdir $dst;
54    opendir my $DIRFH, $src or die "Could not open '$src': $!";
55    while (readdir $DIRFH) {
56        next if ($_ eq '.' or $_ eq '..');
57        print "    - $dst/$_\n";
58        if (-d "$src/$_") {
59            # copy folder
60            copydir ("$src/$_", "$dst/$_", $subst);
61        } elsif (-B "$src/$_") {
62            # copy binary file
63            cp "$src/$_", "$dst/$_" or die "Failed to copy '$_': $!";
64        } else {
65            # copy normal file
66            my $newFile = "$dst/$_";
67            open OUT, '>', $newFile
68                or die "Failed to open '$_' for writing: $!";
69            open IN, '<', "$src/$_"
70                or die "Failed to open '$_' for reading: $!";
71            while (<IN>) {
72                s/@([\w]+)@/$subst->{$1}/g;
73                print OUT;
74            }
75            my $perms = (stat IN)[2] & 07777;
76            close IN;
77            close OUT;
78            chmod ($perms | 0600, $newFile);
79        }
80    }
81    closedir $DIRFH;
82}
83
84my %escape = (
85    "n" => "\n",
86    "\\" => "\\",
87    " " => " "
88);
89
90sub readvar {
91    my ($vars, $line) = @_;
92    my ($var, $val) = trim (split ":", $line, 2);
93    defined $var and defined $val or die "Invalid variable assignment: '$line'";
94    local $/ = '';
95    $val =~ s/\\([ \\n]|$)/$escape{$1}/g;
96    $val =~ s/@([\w]+)@/$vars->{$1}/g;
97    $vars->{$var} = $val;
98}
99
100sub addsource {
101    my ($vars, $source) = @_;
102    my $md5 = Digest::MD5->new;
103    open my $MD5FH, '<', $source;
104    binmode $MD5FH;
105    $md5->addfile ($MD5FH);
106    my $field_separator = $vars->{'FIELD_SEPARATOR'};
107    $vars->{'SOURCES'} .= $field_separator . basename($source);
108    $vars->{'SOURCE_SUMS'} .= $field_separator . $md5->hexdigest;
109    close $MD5FH;
110}
111
112my @platforms = globbify "platforms", shift;
113my @packages  = globbify "packages",  shift;
114
115for (@platforms) {
116    my $platform = basename $_;
117    print "$platform\n";
118
119    open PLATFORM, '<', $_;
120    my %platform_vars;
121    while (<PLATFORM>) {
122        chomp;
123        (comment or empty) and next;
124        readvar \%platform_vars, $_;
125    }
126    close PLATFORM;
127
128    for (@packages) {
129        my $package = basename $_;
130        print " - $package\n";
131        my $package_path = "build/$platform/$package";
132        make_path $package_path;
133
134        open PACKAGE, '<', $_;
135        my %package_vars = %{dclone \%platform_vars};
136        while (<PACKAGE>) {
137            chomp;
138            empty and last;
139            comment and next;
140            readvar \%package_vars, $_;
141        }
142        for (keys %package_vars) {
143            defined $package_vars{$_} or $package_vars{$_} = ' ';
144            # print "$_ = $package_vars{$_}\n";
145        }
146
147        while (<PACKAGE>) {
148            chomp;
149            empty and last;
150            comment and next;
151            cp "files/$_", "$package_path/$_" or die "Failed to copy '$_': $!";
152            addsource \%package_vars, "$package_path/$_";
153        }
154
155        # write the makefile
156        my $outfile = "$package_path/Makefile";
157        print "    - $outfile\n";
158        open MAKEFILE, '>', $outfile
159            or die "Failed to open '$outfile' for writing: $!";
160        while (<PACKAGE>) {
161            chomp;
162            empty and last;
163            comment and next;
164            open IN, '<', "segments/$_"
165                or die "Failed to open '$_' for reading: $!";
166            while (<IN>) {
167                s/@([\w]+)@/$package_vars{$1}/g;
168                print MAKEFILE;
169            }
170            close IN;
171        }
172        close MAKEFILE;
173        close PACKAGE;
174        addsource \%package_vars, $outfile;
175
176        # write the package manager files
177        copydir ("managers/$platform_vars{'MANAGER'}", $package_path, \%package_vars);
178    }
179}
Note: See TracBrowser for help on using the browser.