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 |
|
---|
7 | use strict;
|
---|
8 | use warnings;
|
---|
9 | use utf8;
|
---|
10 | use File::Basename;
|
---|
11 | use File::Path 'make_path';
|
---|
12 | use File::Copy;
|
---|
13 | use Storable 'dclone';
|
---|
14 |
|
---|
15 | my $USAGE = "Usage: " . basename($0) . " <platforms> <packages>
|
---|
16 | platforms: A comma separated list of the platforms defined in pkg.in/platforms
|
---|
17 | Or 'all' (for all platforms)
|
---|
18 | packages: A comma separated list of the packages defined in pkg.in/packages
|
---|
19 | Or 'all' (for all platforms)
|
---|
20 | ";
|
---|
21 |
|
---|
22 | $#ARGV == 1 or die $USAGE;
|
---|
23 |
|
---|
24 | sub globbify {
|
---|
25 | my ($dir, $files) = @_;
|
---|
26 | $files =~ /^all$/i and return glob "$dir/*";
|
---|
27 | my @list = glob "$dir/{$files}";
|
---|
28 | for (@list) {
|
---|
29 | -f or die "Could not find '", $_, "'";
|
---|
30 | }
|
---|
31 | return @list;
|
---|
32 | }
|
---|
33 |
|
---|
34 | sub readvar {
|
---|
35 | my ($vars, $line) = @_;
|
---|
36 | my ($var, $val) = split "=", $line;
|
---|
37 | defined $var and defined $val or die "Invalid variable assignment: '$line'";
|
---|
38 | $val =~ s/%([\w]+)%/$vars->{$1}/g;
|
---|
39 | $vars->{$var} = $val;
|
---|
40 | }
|
---|
41 |
|
---|
42 | my @platforms = globbify "pkg.in/platforms", shift;
|
---|
43 | my @packages = globbify "pkg.in/packages", shift;
|
---|
44 |
|
---|
45 | for (@platforms) {
|
---|
46 | my ($platform) = /^pkg\.in\/platforms\/(.*)$/;
|
---|
47 | my %platform_vars = ();
|
---|
48 | open FH, '<', $_ or die "Failed to open '$_' for reading: $!";
|
---|
49 | # read platform variable definitons
|
---|
50 | while (<FH>) {
|
---|
51 | chomp;
|
---|
52 | readvar \%platform_vars, $_;
|
---|
53 | }
|
---|
54 | close FH;
|
---|
55 |
|
---|
56 | for (@packages) {
|
---|
57 | my ($package) = /^pkg\.in\/packages\/(.*)$/;
|
---|
58 | my $package_path = "build/$platform/$package";
|
---|
59 | make_path $package_path;
|
---|
60 |
|
---|
61 | open FH, '<', $_ or die "Failed to open '$_' for reading: $!";
|
---|
62 |
|
---|
63 | # read package file dependencies
|
---|
64 | while (<FH>) {
|
---|
65 | chomp;
|
---|
66 | /^[\s]*$/ and last;
|
---|
67 | -e "pkg.in/files/$_" or die "Could not find '", $_, "'";
|
---|
68 | copy "pkg.in/files/$_", "$package_path/$_" or die "Failed to copy '$_': $!";
|
---|
69 | }
|
---|
70 |
|
---|
71 | my %package_vars = %{dclone \%platform_vars};
|
---|
72 | # read package variable definitions
|
---|
73 | while (<FH>) {
|
---|
74 | chomp;
|
---|
75 | /^[\s]*$/ and last;
|
---|
76 | readvar \%package_vars, $_;
|
---|
77 | }
|
---|
78 |
|
---|
79 | my $outfile = "$package_path/Makefile";
|
---|
80 | open OUT, '>', $outfile or die "Failed to open '$outfile' for writing: $!";
|
---|
81 | # read package makefile segments
|
---|
82 | while (<FH>) {
|
---|
83 | chomp;
|
---|
84 | open SEG, '<', "pkg.in/segments/$_" or die "Failed to open '$_' for reading: $!";
|
---|
85 | # write the makefile
|
---|
86 | while (<SEG>) {
|
---|
87 | s/%([\w]+)%/$package_vars{$1}/g;
|
---|
88 | print OUT;
|
---|
89 | }
|
---|
90 | close SEG;
|
---|
91 | }
|
---|
92 | close FH;
|
---|
93 | close OUT;
|
---|
94 | }
|
---|
95 | print "Generated $platform build scripts in build/$platform/\n";
|
---|
96 | }
|
---|