scripts/getver.sh: simplify revision calculation
[openwrt/staging/yousong.git] / scripts / target-metadata.pl
1 #!/usr/bin/env perl
2 use FindBin;
3 use lib "$FindBin::Bin";
4 use strict;
5 use metadata;
6 use Getopt::Long;
7
8 sub target_config_features(@) {
9 my $ret;
10
11 while ($_ = shift @_) {
12 /arm_v(\w+)/ and $ret .= "\tselect arm_v$1\n";
13 /broken/ and $ret .= "\tdepends on BROKEN\n";
14 /audio/ and $ret .= "\tselect AUDIO_SUPPORT\n";
15 /display/ and $ret .= "\tselect DISPLAY_SUPPORT\n";
16 /dt/ and $ret .= "\tselect USES_DEVICETREE\n";
17 /gpio/ and $ret .= "\tselect GPIO_SUPPORT\n";
18 /pci/ and $ret .= "\tselect PCI_SUPPORT\n";
19 /pcie/ and $ret .= "\tselect PCIE_SUPPORT\n";
20 /usb/ and $ret .= "\tselect USB_SUPPORT\n";
21 /usbgadget/ and $ret .= "\tselect USB_GADGET_SUPPORT\n";
22 /pcmcia/ and $ret .= "\tselect PCMCIA_SUPPORT\n";
23 /rtc/ and $ret .= "\tselect RTC_SUPPORT\n";
24 /squashfs/ and $ret .= "\tselect USES_SQUASHFS\n";
25 /jffs2$/ and $ret .= "\tselect USES_JFFS2\n";
26 /jffs2_nand/ and $ret .= "\tselect USES_JFFS2_NAND\n";
27 /ext4/ and $ret .= "\tselect USES_EXT4\n";
28 /targz/ and $ret .= "\tselect USES_TARGZ\n";
29 /cpiogz/ and $ret .= "\tselect USES_CPIOGZ\n";
30 /minor/ and $ret .= "\tselect USES_MINOR\n";
31 /ubifs/ and $ret .= "\tselect USES_UBIFS\n";
32 /fpu/ and $ret .= "\tselect HAS_FPU\n";
33 /spe_fpu/ and $ret .= "\tselect HAS_SPE_FPU\n";
34 /ramdisk/ and $ret .= "\tselect USES_INITRAMFS\n";
35 /powerpc64/ and $ret .= "\tselect powerpc64\n";
36 /nommu/ and $ret .= "\tselect NOMMU\n";
37 /mips16/ and $ret .= "\tselect HAS_MIPS16\n";
38 /rfkill/ and $ret .= "\tselect RFKILL_SUPPORT\n";
39 /low_mem/ and $ret .= "\tselect LOW_MEMORY_FOOTPRINT\n";
40 /small_flash/ and $ret .= "\tselect SMALL_FLASH\n";
41 /nand/ and $ret .= "\tselect NAND_SUPPORT\n";
42 }
43 return $ret;
44 }
45
46 sub target_name($) {
47 my $target = shift;
48 my $parent = $target->{parent};
49 if ($parent) {
50 return $target->{parent}->{name}." - ".$target->{name};
51 } else {
52 return $target->{name};
53 }
54 }
55
56 sub kver($) {
57 my $v = shift;
58 $v =~ tr/\./_/;
59 if (substr($v,0,2) eq "2_") {
60 $v =~ /(\d+_\d+_\d+)(_\d+)?/ and $v = $1;
61 } else {
62 $v =~ /(\d+_\d+)(_\d+)?/ and $v = $1;
63 }
64 return $v;
65 }
66
67 sub print_target($) {
68 my $target = shift;
69 my $features = target_config_features(@{$target->{features}});
70 my $help = $target->{desc};
71 my $confstr;
72
73 chomp $features;
74 $features .= "\n";
75 if ($help =~ /\w+/) {
76 $help =~ s/^\s*/\t /mg;
77 $help = "\thelp\n$help";
78 } else {
79 undef $help;
80 }
81
82 my $v = kver($target->{version});
83 if (@{$target->{subtargets}} == 0) {
84 $confstr = <<EOF;
85 config TARGET_$target->{conf}
86 bool "$target->{name}"
87 select LINUX_$v
88 EOF
89 }
90 else {
91 $confstr = <<EOF;
92 config TARGET_$target->{conf}
93 bool "$target->{name}"
94 EOF
95 }
96 if ($target->{subtarget}) {
97 $confstr .= "\tdepends on TARGET_$target->{boardconf}\n";
98 }
99 if (@{$target->{subtargets}} > 0) {
100 $confstr .= "\tselect HAS_SUBTARGETS\n";
101 grep { /broken/ } @{$target->{features}} and $confstr .= "\tdepends on BROKEN\n";
102 } else {
103 $confstr .= $features;
104 if ($target->{arch} =~ /\w/) {
105 $confstr .= "\tselect $target->{arch}\n";
106 }
107 if ($target->{has_devices}) {
108 $confstr .= "\tselect HAS_DEVICES\n";
109 }
110 }
111
112 foreach my $dep (@{$target->{depends}}) {
113 my $mode = "depends on";
114 my $flags;
115 my $name;
116
117 $dep =~ /^([@\+\-]+)(.+)$/;
118 $flags = $1;
119 $name = $2;
120
121 next if $name =~ /:/;
122 $flags =~ /-/ and $mode = "deselect";
123 $flags =~ /\+/ and $mode = "select";
124 $flags =~ /@/ and $confstr .= "\t$mode $name\n";
125 }
126 $confstr .= "$help\n\n";
127 print $confstr;
128 }
129
130 sub merge_package_lists($$) {
131 my $list1 = shift;
132 my $list2 = shift;
133 my @l = ();
134 my %pkgs;
135
136 foreach my $pkg (@$list1, @$list2) {
137 $pkgs{$pkg} = 1;
138 }
139 foreach my $pkg (keys %pkgs) {
140 push @l, $pkg unless ($pkg =~ /^-/ or $pkgs{"-$pkg"});
141 }
142 return sort(@l);
143 }
144
145 sub gen_target_config() {
146 my $file = shift @ARGV;
147 my @target = parse_target_metadata($file);
148 my %defaults;
149
150 my @target_sort = sort {
151 target_name($a) cmp target_name($b);
152 } @target;
153
154
155 print <<EOF;
156 choice
157 prompt "Target System"
158 default TARGET_ar71xx
159 reset if !DEVEL
160
161 EOF
162
163 foreach my $target (@target_sort) {
164 next if $target->{subtarget};
165 print_target($target);
166 }
167
168 print <<EOF;
169 endchoice
170
171 choice
172 prompt "Subtarget" if HAS_SUBTARGETS
173 EOF
174 foreach my $target (@target) {
175 next unless $target->{def_subtarget};
176 print <<EOF;
177 default TARGET_$target->{conf}_$target->{def_subtarget} if TARGET_$target->{conf}
178 EOF
179 }
180 print <<EOF;
181
182 EOF
183 foreach my $target (@target) {
184 next unless $target->{subtarget};
185 print_target($target);
186 }
187
188 print <<EOF;
189 endchoice
190
191 choice
192 prompt "Target Profile"
193
194 EOF
195 foreach my $target (@target) {
196 my $profile = $target->{profiles}->[0];
197 $profile or next;
198 print <<EOF;
199 default TARGET_$target->{conf}_$profile->{id} if TARGET_$target->{conf}
200 EOF
201 }
202
203 print <<EOF;
204
205 config TARGET_MULTI_PROFILE
206 bool "Multiple devices"
207 depends on HAS_DEVICES
208
209 EOF
210
211 foreach my $target (@target) {
212 my $profiles = $target->{profiles};
213 foreach my $profile (@{$target->{profiles}}) {
214 print <<EOF;
215 config TARGET_$target->{conf}_$profile->{id}
216 bool "$profile->{name}"
217 depends on TARGET_$target->{conf}
218 EOF
219 my @pkglist = merge_package_lists($target->{packages}, $profile->{packages});
220 foreach my $pkg (@pkglist) {
221 print "\tselect DEFAULT_$pkg\n";
222 $defaults{$pkg} = 1;
223 }
224 my $help = $profile->{desc};
225 if ($help =~ /\w+/) {
226 $help =~ s/^\s*/\t /mg;
227 $help = "\thelp\n$help";
228 } else {
229 undef $help;
230 }
231 print "$help\n";
232 }
233 }
234
235 print <<EOF;
236 endchoice
237
238 menu "Target Devices"
239 depends on TARGET_MULTI_PROFILE
240
241 EOF
242 foreach my $target (@target) {
243 my $profiles = $target->{profiles};
244 foreach my $profile (@{$target->{profiles}}) {
245 next unless $profile->{id} =~ /^DEVICE_/;
246 print <<EOF;
247 config TARGET_DEVICE_$target->{conf}_$profile->{id}
248 bool "$profile->{name}"
249 depends on TARGET_$target->{conf}
250 EOF
251 my @pkglist = merge_package_lists($target->{packages}, $profile->{packages});
252 foreach my $pkg (@pkglist) {
253 print "\tselect DEFAULT_$pkg\n";
254 $defaults{$pkg} = 1;
255 }
256 }
257 }
258
259 print <<EOF;
260
261 endmenu
262
263 config HAS_SUBTARGETS
264 bool
265
266 config HAS_DEVICES
267 bool
268
269 config TARGET_BOARD
270 string
271
272 EOF
273 foreach my $target (@target) {
274 $target->{subtarget} or print "\t\tdefault \"".$target->{board}."\" if TARGET_".$target->{conf}."\n";
275 }
276 print <<EOF;
277 config TARGET_SUBTARGET
278 string
279 default "generic" if !HAS_SUBTARGETS
280
281 EOF
282
283 foreach my $target (@target) {
284 foreach my $subtarget (@{$target->{subtargets}}) {
285 print "\t\tdefault \"$subtarget\" if TARGET_".$target->{conf}."_$subtarget\n";
286 }
287 }
288 print <<EOF;
289 config TARGET_PROFILE
290 string
291 EOF
292 foreach my $target (@target) {
293 my $profiles = $target->{profiles};
294 foreach my $profile (@$profiles) {
295 print "\tdefault \"$profile->{id}\" if TARGET_$target->{conf}_$profile->{id}\n";
296 }
297 }
298
299 print <<EOF;
300
301 config TARGET_ARCH_PACKAGES
302 string
303
304 EOF
305 foreach my $target (@target) {
306 next if @{$target->{subtargets}} > 0;
307 print "\t\tdefault \"".($target->{arch_packages} || $target->{board})."\" if TARGET_".$target->{conf}."\n";
308 }
309 print <<EOF;
310
311 config DEFAULT_TARGET_OPTIMIZATION
312 string
313 EOF
314 foreach my $target (@target) {
315 next if @{$target->{subtargets}} > 0;
316 print "\tdefault \"".$target->{cflags}."\" if TARGET_".$target->{conf}."\n";
317 }
318 print "\tdefault \"-Os -pipe -funit-at-a-time\"\n";
319 print <<EOF;
320
321 config CPU_TYPE
322 string
323 EOF
324 foreach my $target (@target) {
325 next if @{$target->{subtargets}} > 0;
326 print "\tdefault \"".$target->{cputype}."\" if TARGET_".$target->{conf}."\n";
327 }
328 print "\tdefault \"\"\n";
329
330 my %kver;
331 foreach my $target (@target) {
332 my $v = kver($target->{version});
333 next if $kver{$v};
334 $kver{$v} = 1;
335 print <<EOF;
336
337 config LINUX_$v
338 bool
339
340 EOF
341 }
342 foreach my $def (sort keys %defaults) {
343 print "\tconfig DEFAULT_".$def."\n";
344 print "\t\tbool\n\n";
345 }
346 }
347
348 sub gen_profile_mk() {
349 my $file = shift @ARGV;
350 my $target = shift @ARGV;
351 my @targets = parse_target_metadata($file);
352 foreach my $cur (@targets) {
353 next unless $cur->{id} eq $target;
354 print "PROFILE_NAMES = ".join(" ", map { $_->{id} } @{$cur->{profiles}})."\n";
355 foreach my $profile (@{$cur->{profiles}}) {
356 print $profile->{id}.'_NAME:='.$profile->{name}."\n";
357 print $profile->{id}.'_PACKAGES:='.join(' ', @{$profile->{packages}})."\n";
358 }
359 }
360 }
361
362 sub parse_command() {
363 GetOptions("ignore=s", \@ignore);
364 my $cmd = shift @ARGV;
365 for ($cmd) {
366 /^config$/ and return gen_target_config();
367 /^profile_mk$/ and return gen_profile_mk();
368 }
369 die <<EOF
370 Available Commands:
371 $0 config [file] Target metadata in Kconfig format
372 $0 profile_mk [file] [target] Profile metadata in makefile format
373
374 EOF
375 }
376
377 parse_command();