summaryrefslogtreecommitdiffstats
path: root/scripts/dump-target-info.pl
blob: eec06ed6c494a51aa1549f0c73a0fba3233c4155 (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
#!/usr/bin/env perl

use strict;
use warnings;
use Cwd;

my (%targets, %architectures, %kernels, %devices);

$ENV{'TOPDIR'} = Cwd::getcwd();


sub parse_targetinfo {
	my ($target_dir, $subtarget) = @_;

	if (open M, "make -C '$target_dir' --no-print-directory DUMP=1 TARGET_BUILD=1 SUBTARGET='$subtarget' |") {
		my ($target_name, $target_arch, $target_kernel, $target_testing_kernel, @target_features);
		while (defined(my $line = readline M)) {
			chomp $line;

			if ($line =~ /^Target: (.+)$/) {
				$target_name = $1;
			}
			elsif ($line =~ /^Target-Arch-Packages: (.+)$/) {
				$target_arch = $1;
			}
			elsif ($line =~ /^Linux-Version: (\d\.\d+)\.\d+$/) {
				$target_kernel = $1;
			}
			elsif ($line =~ /^Linux-Testing-Version: (\d\.\d+)\.\d+$/) {
				$target_testing_kernel = $1;
			}
			elsif ($line =~ /^Target-Features: (.+)$/) {
				@target_features = split /\s+/, $1;
			}
			elsif ($line =~ /^@\@$/) {
				if ($target_name && $target_arch && $target_kernel &&
				    !grep { $_ eq 'broken' or $_ eq 'source-only' } @target_features) {
					$targets{$target_name} = $target_arch;
					$architectures{$target_arch} ||= [];
					push @{$architectures{$target_arch}}, $target_name;
					$kernels{$target_name} ||= [];
					push @{$kernels{$target_name}}, $target_kernel;
					if ($target_testing_kernel) {
						push @{$kernels{$target_name}}, $target_testing_kernel;
					}
				}

				undef $target_name;
				undef $target_arch;
				undef $target_kernel;
				undef $target_testing_kernel;
				@target_features = ();
			}
		}
		close M;
	}
}

sub parse_devices {
	my ($target_dir, $subtarget) = @_;

	if (open M, "make -C '$target_dir' --no-print-directory DUMP=1 TARGET_BUILD=1 SUBTARGET='$subtarget' V=s |") {
		my ($device_profile, $device_name, @device_alt_names, $device_is_alt);
		while (defined(my $line = readline M)) {
			chomp $line;

			if ($line =~ /^Target-Profile-Name: (.+)$/) {
				$device_name = $1;
			}
			elsif ($line =~ /^Target-Profile: DEVICE_(.+)$/) {
				$device_profile = $1;
			}
			# Logic behind this.
			# DUMP duplicate info for each alternative device name and
			# the alternative device name are printed first before the
			# primary device name
			# Alternative device titles always have the full list of
			# all the alternative device name.
			# The device name pattern for an alternative device name is
			# Target-Profile-Name: ALT_NAME (PRIMARY_NAME)
			# We compare the detected device name and check if it does
			# match the alternative device name pattern with one of
			# the alternative device name in Alternative device titles:
			# If an alternative device name is detected,
			# alternative device is skipped.
			elsif ($line =~ /^Alternative device titles:$/) {
				while (defined($line = readline M)) {
					if ($line =~ /^- (.+)$/) {
						if ($device_name =~ /^\Q$1\E \((.+)\)$/) {
							$device_is_alt = 1;
							last;
						}
						push @device_alt_names, $1;
					}
					else {
						last;
					}
				}
			}
			if ($line =~ /^@\@$/) {
				if ($device_name && $device_profile && ! $device_is_alt) {
					push @{$devices{$device_profile}}, $device_name;

					if (scalar @device_alt_names) {
						foreach my $device_alt_name (sort values @device_alt_names) {
							push @{$devices{$device_profile}}, $device_alt_name;
						}
					}
				}

				undef $device_name;
				undef $device_profile;
				undef $device_is_alt;
				@device_alt_names = ();
			}
		}
		close M;
	}
}

sub get_targetinfo {
	foreach my $target_makefile (glob "target/linux/*/Makefile") {
		my ($target_dir) = $target_makefile =~ m!^(.+)/Makefile$!;
		my @subtargets;

		if (open M, "make -C '$target_dir' --no-print-directory DUMP=1 TARGET_BUILD=1 val.FEATURES V=s 2>/dev/null |") {
			if (defined(my $line = readline M)) {
				chomp $line;
				if (grep { $_ eq 'broken' or $_ eq 'source-only' } split /\s+/, $line) {
					next;
				}
			}
		}

		if (open M, "make -C '$target_dir' --no-print-directory DUMP=1 TARGET_BUILD=1 val.SUBTARGETS V=s 2>/dev/null |") {
			if (defined(my $line = readline M)) {
				chomp $line;
				@subtargets = split /\s+/, $line;
			}
			close M;
		}

		push @subtargets, 'generic' if @subtargets == 0;

		foreach my $subtarget (@subtargets) {
			parse_targetinfo($target_dir, $subtarget);
		}
	}
}

sub get_devices {
	my ($target_subtarget) = @_;
	my ($target, $subtarget) = split /\//, $target_subtarget;

	my ($target_dir) = "target/linux/" . $target;

	parse_devices($target_dir, $subtarget)
}

if (@ARGV == 1 && $ARGV[0] eq 'targets') {
	get_targetinfo();
	foreach my $target_name (sort keys %targets) {
		printf "%s %s\n", $target_name, $targets{$target_name};
	}
}
elsif (@ARGV == 1 && $ARGV[0] eq 'architectures') {
	get_targetinfo();
	foreach my $target_arch (sort keys %architectures) {
		printf "%s %s\n", $target_arch, join ' ', @{$architectures{$target_arch}};
	}
}
elsif (@ARGV == 1 && $ARGV[0] eq 'kernels') {
	get_targetinfo();
	foreach my $target_name (sort keys %targets) {
		printf "%s %s\n", $target_name, join ' ', @{$kernels{$target_name}};
	}
}
elsif (@ARGV == 2 && $ARGV[0] eq 'devices') {
	get_devices($ARGV[1]);
	foreach my $device (sort keys %devices) {
		printf "%s \"%s\"\n", $device, join '" "', @{$devices{$device}};
	}
}
else {
	print "Usage: $0 targets\n";
	print "Usage: $0 architectures\n";
	print "Usage: $0 kernels\n";
	print "Usage: $0 devices <target/subtarget>\n";
}