Formatting: kill off "stealth whitespace"
[platform/upstream/nasm.git] / version.pl
1 #!/usr/bin/perl
2 #
3 # version.pl
4 #
5 # Parse the NASM version file and produce appropriate macros
6 #
7 # The NASM version number is assumed to consist of:
8 #
9 # <major>.<minor>[.<subminor>][pl<patchlevel>]]<tail>
10 #
11 # ... where <tail> is not necessarily numeric, but if it is of the form
12 # -<digits> it is assumed to be a snapshot release.
13 #
14 # This defines the following macros:
15 #
16 # version.h:
17 # NASM_MAJOR_VER
18 # NASM_MINOR_VER
19 # NASM_SUBMINOR_VER     -- this is zero if no subminor
20 # NASM_PATCHLEVEL_VER   -- this is zero is no patchlevel
21 # NASM_SNAPSHOT         -- if snapshot
22 # NASM_VERSION_ID       -- version number encoded
23 # NASM_VER              -- whole version number as a string
24 #
25 # version.mac:
26 # __NASM_MAJOR__
27 # __NASM_MINOR__
28 # __NASM_SUBMINOR__
29 # __NASM_PATCHLEVEL__
30 # __NASM_SNAPSHOT__
31 # __NASM_VERSION_ID__
32 # __NASM_VER__
33 #
34
35 ($what) = @ARGV;
36
37 $line = <STDIN>;
38 chomp $line;
39
40 undef $man, $min, $smin, $plvl, $tail;
41 $is_rc = 0;
42
43 if ( $line =~ /^([0-9]+)\.([0-9]+)(.*)$/ ) {
44     $maj  = $1;
45     $min  = $2;
46     $tail = $3;
47     if ( $tail =~ /^\.([0-9]+)(.*)$/ ) {
48         $smin = $1;
49         $tail = $2;
50     }
51     if ( $tail =~ /^(pl|\.)([0-9]+)(.*)$/ ) {
52         $plvl = $2;
53         $tail = $3;
54     } elsif ( $tail =~ /^rc([0-9]+)(.*)$/ ) {
55         $is_rc = 1;
56         $plvl = $1;
57         $tail = $2;
58     }
59 } else {
60     die "$0: Invalid input format\n";
61 }
62
63 if ($tail =~ /^\-([0-9]+)/) {
64     $snapshot = $1;
65 } else {
66     undef $snapshot;
67 }
68
69 $nmaj = $maj+0;   $nmin = $min+0;
70 $nsmin = $smin+0; $nplvl = $plvl+0;
71
72 if ($is_rc) {
73     $nplvl += 90;
74     if ($nsmin > 0) {
75         $nsmin--;
76     } else {
77         $nsmin = 99;
78         if ($nmin > 0) {
79             $nmin--;
80         } else {
81             $nmin = 99;
82             $nmaj--;
83         }
84     }
85 }
86
87 $nasm_id = ($nmaj << 24)+($nmin << 16)+($nsmin << 8)+$nplvl;
88
89 $mangled_ver = sprintf("%d.%02d.%02d", $nmaj, $nmin, $nsmin);
90 $mangled_ver .= '.'.$nplvl if ($nplvl != 0);
91 ($mtail = $tail) =~ tr/-/./;
92 $mangled_ver .= $mtail;
93
94 if ( $what eq 'h' ) {
95     print  "#ifndef NASM_VERSION_H\n";
96     print  "#define NASM_VERSION_H\n";
97     printf "#define NASM_MAJOR_VER      %d\n", $nmaj;
98     printf "#define NASM_MINOR_VER      %d\n", $nmin;
99     printf "#define NASM_SUBMINOR_VER   %d\n", $nsmin;
100     printf "#define NASM_PATCHLEVEL_VER %d\n", $nplvl;
101     if (defined($snapshot)) {
102         printf "#define NASM_SNAPSHOT       %d\n", $snapshot;
103     }
104     printf "#define NASM_VERSION_ID     0x%08x\n", $nasm_id;
105     printf "#define NASM_VER            \"%s\"\n", $line;
106     print  "#endif /* NASM_VERSION_H */\n";
107 } elsif ( $what eq 'mac' ) {
108     printf "%%define __NASM_MAJOR__ %d\n", $nmaj;
109     printf "%%define __NASM_MINOR__ %d\n", $nmin;
110     printf "%%define __NASM_SUBMINOR__ %d\n", $nsmin;
111     printf "%%define __NASM_PATCHLEVEL__ %d\n", $nplvl;
112     if (defined($snapshot)) {
113         printf "%%define __NASM_SNAPSHOT__ %d\n", $snapshot;
114     }
115     printf "%%define __NASM_VERSION_ID__ 0%08Xh\n", $nasm_id;
116     printf "%%define __NASM_VER__ \"%s\"\n", $line;
117 } elsif ( $what eq 'sed' ) {
118     printf "s/\@\@NASM_MAJOR\@\@/%d/g\n", $nmaj;
119     printf "s/\@\@NASM_MINOR\@\@/%d/g\n", $nmin;
120     printf "s/\@\@NASM_SUBMINOR\@\@/%d/g\n", $nsmin;
121     printf "s/\@\@NASM_PATCHLEVEL\@\@/%d/g\n", $nplvl;
122     printf "s/\@\@NASM_SNAPSHOT\@\@/%d/g\n", $snapshot; # Possibly empty
123     printf "s/\@\@NASM_VERSION_ID\@\@/%d/g\n", $nasm_id;
124     printf "s/\@\@NASM_VERSION_XID\@\@/0x%08x/g\n", $nasm_id;
125     printf "s/\@\@NASM_VER\@\@/%s/g\n", $line;
126     printf "s/\@\@NASM_MANGLED_VER\@\@/%s/g\n", $mangled_ver;
127 } elsif ( $what eq 'id' ) {
128     print $nasm_id, "\n";        # Print ID in decimal
129 } elsif ( $what eq 'xid' ) {
130     printf "0x%08x\n", $nasm_id; # Print ID in hexadecimal
131 } else {
132     die "$0: Unknown output: $what\n";
133 }
134
135 exit 0;