aboutsummaryrefslogtreecommitdiffstats
path: root/v4l/scripts/make_makefile.pl
blob: 564c04f6862a9961c0fae7f0e91f9804864b7d3f (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
#!/usr/bin/perl
use FileHandle;
use File::Find;

my %instdir = ();

# Take a Makefile line of the form:
# obj-XXXXX = some_directory/ some_module.o
#
# All directories are processed by including the referenced Makefile and
# removing the directory.  The modules are added to the list of modules
# to install.  Prints the edited line to OUT.
# Arguments: directory Makefile is in, the objects, original line(s) from
# Makefile (with newlines intact).
sub check_line($$$)
{
	my $dir = shift;
	my $objs = shift;
	my $orig = shift;
	my $count = 0;

	foreach(split /\s+/, $objs) {
		if (m|/$|) { # Ends in /, means it's a directory
			# Delete this directory from original Makefile line
			$orig =~ s/$_[ \t]*//;
			$_ .= 'Makefile';
			# print STDERR "open new makefile $dir/$_\n";
			open_makefile("$dir/$_");
			next;
		}

		# It's a file, add it to list of files to install
		s/\.o$/.ko/;
		my $idir = $dir;
		$idir =~ s|^../linux/drivers/media/||;
		$instdir{$idir}{$_} = 1;
		$count++;
	}
	# Removing any tailling whitespace, just to be neat
	$orig =~ s/[ \t]+$//mg;

	# Print out original line, less directories, if there is anything
	# still there
	print OUT $orig if($count);
}

# Uses the string being assigned from a line of the form:
# EXTRA_CFLAGS += -Idrivers/media/something -Dwhatever
#
# All the -Idrivers/media/something options get deleted.  All the source
# files are linked into the v4l directory and built from there, so and -I
# option for them is unnecessary.  The line is printed to OUT if there is
# anything left after that.
sub remove_includes($$)
{
	my $flags = shift;
	my $orig = shift;
	my $count = 0;

	foreach(split /\s+/, $flags) {
		if (m|^-Idrivers/media|) {
			# Remove any -I flags from original Makefile line
			$orig =~ s/$_[ \t]*//;
			next;
		}
		$count++;	# Something wasn't deleted
	}
	$orig =~ s/[ \t]+$//mg;

	# Print out original line if there is anything we didn't delete
	print OUT $orig if($count);
}

sub open_makefile($) {
	my $file = shift;
	my $in = new FileHandle;
	my $orig;

	# only open a given Makefile once
	return if exists $makefiles{$file};
	$makefiles{$file} = 1;

	$file =~ m|^(.*)/[^/]*$|;
	my $dir = $1;

	# print STDERR "opening $file (dir=$dir)\n";
	$file =~ m|.*/(linux/.*)$|;
	print OUT "# Including $1\n";
	open $in, $file;

	while (<$in>) {
		# print STDERR "Line: $_";
		# Skip comment lines
		if (/^\s*#/) {
			print OUT $_;
			next;
		}
		m/^\s*-?include/ and die "Can't handle includes! In $file";

		# Handle line continuations
		if (/\\\n$/) {
			$_ .= <$in>;
			redo;
		}

		# $orig is what we will print, $_ is what we will parse
		$orig = $_;
		# Eat line continuations in string we will parse
		s/\s*\\\n\s*/ /g;
		# Eat comments
		s/#.*$//;

		if (/^\s*obj-.*:?=\s*(\S.*?)\s*$/) {
			# print STDERR "obj matched '$1'\n";
			check_line($dir, $1, $orig);	# will print line for us
			next;
		}
		if (/^\s*EXTRA_CFLAGS\s+\+?=\s*(\S.*?)\s*$/) {
			# print STDERR "cflags matched '$1'\n";
			remove_includes($1, $orig);	# will print line for us
			next;
		}
		# Any flags should be added to already existing flags
		$orig =~ s/ccflags-y\s*:=/ccflags-y +=/;
		print OUT $orig;
	}
	close $in;
}

my %obsolete;
sub getobsolete()
{
	open OBSOLETE, '<obsolete.txt' or die "Can't open obsolete.txt: $!";
	while (<OBSOLETE>) {
		next if (/^\s*#/ || /^\s*$/);
		chomp;
		if (m|^(.*)/([^/]*)$|) {
			$obsolete{$1}{"$2.ko"} = 1;
		} else {
			print "Unable to parse obsolete.txt:$.\n$_\n";
		}
	}

	close OBSOLETE;
}

sub removeobsolete()
{
	while ( my ($dir, $files) = each(%obsolete) ) {
		print OUT "\t\@echo -e \"\\nRemoving obsolete files from \$(KDIR26)/$dir:\"\n";
		print OUT "\t\@files='", join(' ', keys %$files), "'; ";

		print OUT "for i in \$\$files;do if [ -e \$(DESTDIR)\$(KDIR26)/$dir/\$\$i ]; then ";
		print OUT "echo -n \"\$\$i \";";
		print OUT " rm \$(DESTDIR)\$(KDIR26)/$dir/\$\$i; fi; done; ";

		print OUT "for i in \$\$files;do if [ -e \$(DESTDIR)\$(KDIR26)/$dir/\$\$i.gz ]; then ";
		print OUT "echo -n \"\$\$i.gz \";";
		print OUT " rm \$(DESTDIR)\$(KDIR26)/$dir/\$\$i.gz; fi; done; echo;\n\n";
	}
}

#
# Special hack for Ubuntu with their non-standard dirs
#
sub removeubuntu($)
{
	my $udir = shift;
	my $dest = "/lib/modules/\$(KERNELRELEASE)/$udir";
	my $filelist;

	while ( my ($dir, $files) = each(%instdir) ) {
		$filelist .= ' '. join(' ', keys %$files);
	}
	while ( my ($dir, $files) = each(%obsolete) ) {
		$filelist .= ' ' . join(' ', keys %$files);
	}
	$filelist =~ s/\s+$//;

	print OUT "\t\@if [ -d $dest ]; then ";
	print OUT "printf \"\\nHmm... distro kernel with a non-standard place for module backports detected.\\n";
	print OUT "Please always prefer to use vanilla upstream kernel with V4L/DVB\\n";
	print OUT "I'll try to remove old/obsolete LUM files from $dest:\\n\"; ";
	print OUT "files='", $filelist, "'; ";

	print OUT "for i in \$\$files;do find \"$dest\" \-name \"\$\$i\" \-exec echo \'{}\' \';\' ;";
	print OUT " find \"$dest\" \-name \"\$\$i\" \-exec rm \'{}\' \';\' ;";
	print OUT " done;";
	print OUT " fi\n";
}

sub parse_dir()
{
	my $file = $File::Find::name;

	return if (!($file =~ /Makefile$/));
	open_makefile($file);
}

##############################################################################

getobsolete();

open OUT, '>Makefile.media' or die 'Unable to write Makefile.media';
open_makefile('../linux/drivers/media/Makefile');

find({wanted => \&parse_dir, no_chdir => 1}, '../linux/drivers/staging');

# Creating Install rule
print OUT "media-install::\n";

removeobsolete();
removeubuntu("kernel/ubuntu/media");
removeubuntu("kernel/ubuntu/lirc");
removeubuntu("/updates/dkms");

print OUT "\t\@echo \"Installing kernel modules under \$(DESTDIR)\$(KDIR26)/:\"\n";

while (my ($dir, $files) = each %instdir) {
	print OUT "\t\@n=0;for i in ", join(' ', keys %$files), ";do ";
	print OUT "if [ -e \"\$\$i\" ]; then ";
	print OUT "if [ \$\$n -eq 0 ]; then ";
	print OUT "echo -n \"\t$dir/: \"; ";
	print OUT "install -d \$(DESTDIR)\$(KDIR26)/$dir; fi; ";
	print OUT "n=\$\$\(\(\$\$n+1\)\); ";
	print OUT "if [  \$\$n -eq 4 ]; then echo; echo -n \"\t\t\"; n=1; fi; ";
	print OUT "echo -n \"\$\$i \"; ";
	print OUT "install -m 644 -c \$\$i \$(DESTDIR)\$(KDIR26)/$dir; fi; done; ";
	print OUT "if [  \$\$n -ne 0 ]; then echo; ";
	print OUT "strip --strip-debug \$(DESTDIR)\$(KDIR26)/$dir/*.ko; ";
	print OUT "fi;\n\n";
}
print OUT "\t@echo\n";
print OUT "\t/sbin/depmod -a \$(KERNELRELEASE) \$(if \$(DESTDIR),-b \$(DESTDIR))\n\n";

# Creating Remove rule
print OUT "media-rminstall::\n";

removeobsolete();
removeubuntu("/ubuntu/media");
removeubuntu("/updates/dkms");

while ( my ($dir, $files) = each(%instdir) ) {
	print OUT "\t\@echo -e \"\\nRemoving old \$(KDIR26)/$dir files:\"\n";
	print OUT "\t\@files='", join(' ', keys %$files), "'; ";

	print OUT "for i in \$\$files;do if [ -e \$(DESTDIR)\$(KDIR26)/$dir/\$\$i ]; then ";
	print OUT "echo -n \"\$\$i \";";
	print OUT " rm \$(DESTDIR)\$(KDIR26)/$dir/\$\$i; fi; done; ";

	print OUT "for i in \$\$files;do if [ -e \$(DESTDIR)\$(KDIR26)/$dir/\$\$i.gz ]; then ";
	print OUT "echo -n \"\$\$i.gz \";";
	print OUT " rm \$(DESTDIR)\$(KDIR26)/$dir/\$\$i.gz; fi; done; echo;\n\n";
}

my $mediadeps = join(" \\\n", map("\t../linux/drivers/media/$_/Makefile", keys %instdir));
$mediadeps =~ s,\.\./linux/drivers/media/\.\.,..,g;

# Print dependencies of Makefile.media
print OUT "Makefile.media: ../linux/drivers/media/Makefile \\\n";
print OUT "\tobsolete.txt \\\n";
print OUT $mediadeps;
print OUT "\n";
close OUT;

Privacy Policy