]> git.pld-linux.org Git - packages/rpm-build-tools.git/blob - adapter.awk
-O0 not -O for debug
[packages/rpm-build-tools.git] / adapter.awk
1 #!/bin/awk -f
2 #
3 # This is adapter v0.23. Adapter adapts .spec files for PLD.
4 #
5 # Copyright (C) 1999-2001 PLD-Team <pld-list@pld.org.pl>
6 # Authors:
7 #       Micha³ Kuratczyk <kura@pld.org.pl>
8 #       Sebastian Zagrodzki <s.zagrodzki@mimuw.edu.pl>
9 #       Tomasz K³oczko <kloczek@rudy.mif.pg.gda.pl>
10 #       Artur Frysiak <wiget@pld.org.pl>
11 #       Michal Kochanowicz <mkochano@ee.pw.edu.pl>
12 # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
13
14 BEGIN {
15         preamble = 1            # Is it part of preamble? Default - yes
16         boc = 2                 # Beggining of %changelog
17         bod = 0                 # Beggining of %description
18         tw = 70                 # Descriptions width
19
20         # File with rpm groups
21         "rpm --eval %_sourcedir" | getline groups_file
22         groups_file = groups_file "/rpm.groups"
23         system("cd `rpm --eval %_sourcedir`; cvs up rpm.groups 1>&2")
24
25         # Temporary file for changelog section
26         changelog_file = ENVIRON["HOME"] "/tmp/adapter.changelog"
27
28         # Load rpm macros
29         "rpm --eval %_prefix"   | getline prefix
30         "rpm --eval %_bindir"   | getline bindir
31         "rpm --eval %_sbindir"  | getline sbindir
32         "rpm --eval %_libdir"   | getline libdir
33         "rpm --eval %_sysconfdir" | getline sysconfdir
34         "rpm --eval %_datadir"  | getline datadir
35         "rpm --eval %_includedir" | getline includedir
36         "rpm --eval %_mandir"   | getline mandir
37         "rpm --eval %_infodir"  | getline infodir
38 }
39
40 # There should be a comment with CVS keywords on the first line of file.
41 FNR == 1 {
42         if (!/# \$Revision:/)   # If this line is already OK?
43                 print "# $" "Revision:$, " "$" "Date:$" # No
44         else {
45                 print $0                                # Yes
46                 next            # It is enough for first line
47         }
48 }
49
50 # If the latest line matched /%files/
51 defattr == 1 {
52         if ($0 !~ /defattr/)    # If no %defattr
53                 print "%defattr(644,root,root,755)"     # Add it
54         else
55                 $0 = "%defattr(644,root,root,755)"      # Correct mistakes (if any)
56         defattr = 0
57 }
58
59 # Remove defining _applnkdir (this macro has been included in rpm-3.0.4)
60 /^%define/ {
61         if ($2 == "_applnkdir")
62                 next
63         if ($2 == "date")
64                 date = 1
65 }
66
67 # descriptions:
68 /^%description/, (/^%[a-z]+/ && !/^%description/) {
69         preamble = 0
70
71         if (/^%description/) {
72                 bod++
73                 format_line = ""
74                 format_indent = -1
75         }
76
77         # Define _prefix and _mandir if it is X11 application
78         if (/^%description$/ && x11 == 1) {
79                 print "%define\t\t_prefix\t\t/usr/X11R6"
80                 print "%define\t\t_mandir\t\t%{_prefix}/man\n"
81                 prefix = "/usr/X11R6"
82                 x11 = 2
83         }
84         
85         # Format description
86         if (description == 1 && !/^%[a-z]+/ && !/^%description/) {
87                 if (/^[ \t]*$/) {
88                         format_flush(format_line, format_indent)
89                         print ""
90                         format_line = ""
91                         format_indent = -1
92                 } else if (/^[ \t]*[-\*][ \t]*/) {
93                         format_flush(format_line, format_indent)
94                         match($0, /^[ \t]*/)    
95                         format_indent = RLENGTH
96                         match($0, /^[ \t]*[-\*][ \t]/)
97                         format_line = substr($0, RLENGTH)
98                 } else 
99                         format_line = format_line " " $0
100                 next
101         }
102  
103         if (/^%[a-z]+/ && (!/^%description/ || bod == 2)) {
104                 format_flush(format_line, format_indent)
105                 if (bod == 2) {
106                         bod = 1
107                         description = 1
108                 } else {
109                         bod = 0
110                         description = 0
111                 }
112         } else
113                 description = 1
114 }
115
116 # %prep section:
117 /^%prep/, (/^%[a-z]+$/ && !/^%prep/) {
118         preamble = 0
119         
120         # Add '-q' to %setup
121         if (/^%setup/ && !/-q/)
122                 sub(/^%setup/, "%setup -q")
123 }
124
125 # %build section:
126 /^%build/, (/^%[a-z]+$/ && !/^%build/) {
127         preamble = 0
128
129         use_macros()
130         
131         if (/^automake$/)
132                 sub(/$/, " -a -c")
133
134         if (/LDFLAGS/) {
135                 if (/LDFLAGS="-s"/)
136                         sub(/LDFLAGS="-s"[ ]*/, "")
137                 else {
138                         split($0, tmp, "LDFLAGS=\"")
139                         count = split(tmp[2], flags, "\"")
140                         sub(/-s[" ]?/, "%{!?debug:-s} ", flags[1])
141                         $0 = tmp[1] line[1] "LDFLAGS=\"" flags[1] "\""
142                         for (i = 2; i < count; i++)
143                                 $0 = $0 flags[i] "\""
144                 }
145         }
146
147         if (/CFLAGS=/)
148                 if (cflags("CFLAGS") == 0)
149                         next
150
151         if (/CXXFLAGS=/)
152                 if (cflags("CXXFLAGS") == 0)
153                         next
154 }
155
156 # %clean section:
157 /^%clean/, (/^%[a-z]+$/ && !/^%clean/) {
158         did_clean = 1
159 }
160
161 # %install section:
162 /^%install/, (/^%[a-z]+$/ && !/^%install/) {
163         
164         preamble = 0
165         
166         if (/^[ \t]*rm([ \t]+-[rf]+)*[ \t]+\${?RPM_BUILD_ROOT}?/ && did_rmroot==0) {
167                 did_rmroot=1
168                 print "rm -rf $RPM_BUILD_ROOT"
169                 next
170         }
171
172         if (!/^(#?[ \t]*)$/ && !/^%install/ && did_rmroot==0) {
173                 print "rm -rf $RPM_BUILD_ROOT"
174                 did_rmroot=1
175         }
176         
177         use_macros()
178         
179         # 'install -d' instead 'mkdir -p'
180         if (/mkdir -p/)
181                 sub(/mkdir -p/, "install -d")
182                 
183         # No '-u root' or '-g root' for 'install'
184         if (/^install/ && /-[ug][ \t]*root/)
185                 gsub(/-[ug][ \t]*root /, "")
186         
187         if (/^install/ && /-m[ \t]*644/)
188                 gsub(/-m[ \t]*644 /, "")
189         
190         # No lines contain 'chown' or 'chgrp' if owner/group is 'root'
191         if (($1 ~ /chown/ && $2 ~ /root\.root/) || ($1 ~ /chgrp/ && $2 ~ /root/))
192                 next
193         
194         # No lines contain 'chmod' if it sets the modes to '644'
195         if ($1 ~ /chmod/ && $2 ~ /644/)
196                 next
197         
198         # 'gzip -9nf' for compressing
199         if ($1 ~ /gzip|bzip2/)
200                 next
201 }
202
203
204 # %files section:
205 /^%files/, (/^%[a-z \-]+$/ && !/^%files/) {
206         preamble = 0
207         
208         if ($0 ~ /^%files/)
209                 defattr = 1
210         
211         use_macros()
212 }
213
214 # %changelog section:
215 /^%changelog/, (/^%[a-z]+$/ && !/^%changelog/) {
216         preamble = 0
217         has_changelog = 1
218         # There should be some CVS keywords on the first line of %changelog.
219         if (boc == 1) {
220                 if (!/PLD Team/) {
221                         print "* %{date} PLD Team <pld-list@pld.org.pl>" > changelog_file
222                         printf "All persons listed below can be reached at " > changelog_file
223                         print "<cvs_login>@pld.org.pl\n" > changelog_file
224                         print "$" "Log:$" > changelog_file
225                 }
226                 boc = 0
227         }
228         
229         # Define date macro.
230         if (boc == 2) {
231                 if (date == 0) {
232                         printf "%%define date\t%%(echo `LC_ALL=\"C\"" > changelog_file
233                         print " date +\"%a %b %d %Y\"`)" > changelog_file
234                         date = 1
235                 }
236                 boc = 1
237         }
238
239         if (!/^%[a-z]+$/ || /changelog/)
240                 print > changelog_file
241         else
242                 print
243         next
244 }
245
246 # preambles:
247 preamble == 1 {
248         # There should not be a space after the name of field
249         # and before the colon.
250         sub(/[ \t]*:/, ":")
251         
252         field = tolower($1)
253         if (Byla_grupa == 1 && field ~ /^#/)
254                 next
255         if (Byla_grupa == 1 && field !~ /group(\(..\))?:/) {
256                 Byla_grupa = 0
257                 print "Group:\t\t" Grupa["en"]
258                 if (Grupa["en"] ~ /^X11/ && x11 == 0)   # Is it X11 application?
259                        x11 = 1
260
261                 byl_plik_z_grupami = 0
262                 byl_opis_grupy = 0
263                 while ((getline linia_grup < groups_file) > 0) {
264                         byl_plik_z_grupami = 1
265                         if (linia_grup == Grupa["en"]) {
266                                 byl_opis_grupy = 1
267                                 break
268                         }
269                 }
270
271                 if (!byl_plik_z_grupami)
272                         print "######\t\t" groups_file ": no such file"
273                 else if (!byl_opis_grupy)
274                         print "######\t\t" "Unknown group!"
275                 else
276                         while (getline linia_grup < groups_file) {
277                                 if (linia_grup == "")
278                                         break
279                                 split(linia_grup, g, /[\[\]:]/)
280                                 sub(/^[ \t]*/,"",g[4])
281                                 Grupa[g[2]]=g[4]
282                         }
283                 
284                 close(groups_file)
285
286                 delete Grupa["en"]
287                 for (jezyk in Grupa) {
288                         print "Group(" jezyk "):\t" Grupa[jezyk] | "sort"
289                         delete Grupa[jezyk]
290                 }
291                 close ("sort")
292         }
293         
294         if (field ~ /packager:|distribution:|docdir:|prefix:/)
295                 next
296         
297         if (field ~ /buildroot:/)
298                 $0 = $1 "%{tmpdir}/%{name}-%{version}-root-%(id -u -n)"
299
300         if (field ~ /group(\(..\))?:/) {
301                 format_preamble()
302                 sub(/^Utilities\//,"Applications/",$2)
303                 sub(/^Games/,"Applications/Games",$2)
304                 sub(/^X11\/Games/,"Applications\/Games",$2)
305                 sub(/^X11\/GNOME\/Development\/Libraries/,"X11/Development/Libraries",$2)
306                 sub(/^X11\/GNOME\/Applications/,"X11/Applications",$2)
307                 sub(/^X11\/GNOME/,"X11/Applications",$2)
308                 sub(/^X11\/Utilities/,"X11\/Applications",$2)
309                 sub(/^X11\/Games\/Strategy/,"X11/Applications/Games/Strategy",$2)
310                 sub(/^Shells/,"Applications/Shells",$2)
311
312                 if (!match(field,/\(..\):/))
313                         glang="en"
314                 else
315                         glang=substr(field,RSTART+1,2)
316                 sub(/^[^ \t]*[ \t]*/,"")
317                 Grupa[glang] = $0
318                 Byla_grupa = 1
319                 
320                 next    # Line is already formatted and printed
321         }
322                 
323         # Use "License" instead of "Copyright" if it is (L)GPL or BSD
324         if (field ~ /copyright:/ && $2 ~ /GPL|BSD/)
325                 $1 = "License:"
326         
327         if (field ~ /name:/)
328                 name = $2
329
330         if (field ~ /version:/)
331                 version = $2
332
333         if (field ~ /serial:/)
334                 $1 = "Epoch:"
335
336         # Use %{name} and %{version} in the filenames in "Source:"
337         if (field ~ /source/ || field ~ /patch/) {
338                 n = split($2, url, /\//)
339                 if (url[n] ~ /\.gz$/) {
340                         url[n+1] = ".gz" url[n+1]
341                         sub(/\.gz$/,"",url[n])
342                 }
343                 if (url[n] ~ /\.zip$/) {
344                         url[n+1] = ".zip" url[n+1]
345                         sub(/\.zip$/,"",url[n])
346                 }
347                 if (url[n] ~ /\.tar$/) {
348                         url[n+1] = ".tar" url[n+1]
349                         sub(/\.tar$/,"",url[n])
350                 }
351                 if (url[n] ~ /\.patch$/) {
352                         url[n+1] = ".patch" url[n+1]
353                         sub(/\.patch$/,"",url[n])
354                 }
355                 if (url[n] ~ /\.bz2$/) {
356                         url[n+1] = ".bz2" url[n+1]
357                         sub(/\.bz2$/,"",url[n])
358                 }
359                 if (url[n] ~ /\.logrotate$/) {
360                         url[n+1] = ".logrotate" url[n+1]
361                         sub(/\.logrotate$/,"",url[n])
362                 }
363                 if (url[n] ~ /\.pamd$/) {
364                         url[n+1] = ".pamd" url[n+1]
365                         sub(/\.pamd$/,"",url[n])
366                 }
367
368                 filename = url[n]
369                 url[n] = fixedsub(name, "%{name}", url[n])
370                 if (field ~ /source/) 
371                         url[n] = fixedsub(version, "%{version}", url[n])
372                 $2 = fixedsub(filename, url[n], $2)
373         }
374
375         if (field ~ /source:/)
376                 $1 = "Source0:" 
377
378         if (field ~ /patch:/)
379                 $1 = "Patch0:"
380         
381         format_preamble()
382         
383         if ($1 ~ /%define/) {
384                 # Do not add %define of _prefix if it already is.
385                 if ($2 ~ /_prefix/) {
386                         sub("^"prefix, $3, bindir)
387                         sub("^"prefix, $3, sbindir)
388                         sub("^"prefix, $3, libdir)
389                         sub("^"prefix, $3, datadir)
390                         sub("^"prefix, $3, includedir)
391                         prefix = $3
392                         x11 = 2
393                 }
394                 if ($2 ~ /_bindir/ && !/_sbindir/)
395                         bindir = $3
396                 if ($2 ~ /_sbindir/)
397                         sbindir = $3
398                 if ($2 ~ /_libdir/)
399                         libdir = $3
400                 if ($2 ~ /_sysconfdir/)
401                         sysconfdir = $3
402                 if ($2 ~ /_datadir/)
403                         datadir = $3
404                 if ($2 ~ /_includedir/)
405                         includedir = $3
406                 if ($2 ~ /_mandir/)
407                         mandir = $3
408                 if ($2 ~ /_infodir/)
409                         infodir = $3
410         }
411 }
412
413
414 # main()  ;-)
415 {
416         preamble = 1
417         
418         print
419 }
420
421
422 END {
423         close(changelog_file)
424         while ((getline < changelog_file) > 0)
425                 print
426         system("rm -f " changelog_file)
427
428         if (did_clean == 0) {
429                 print ""
430                 print "%clean"
431                 print "rm -rf $RPM_BUILD_ROOT"
432         }
433
434         if (date == 0) {
435                 print ""
436                 print "%define date\t%(echo `LC_ALL=\"C\" date +\"%a %b %d %Y\"`)"
437         }
438         
439         if (has_changelog == 0)
440                 print "%changelog"
441
442         if (boc > 0) {
443                 print "* %{date} PLD Team <pld-list@pld.org.pl>"
444                 printf "All persons listed below can be reached at "
445                 print "<cvs_login>@pld.org.pl\n"
446                 print "$" "Log:$"
447         }
448 }
449
450 function fixedsub(s1,s2,t,      ind) {
451 # substitutes fixed strings (not regexps)
452         if (ind = index(t,s1))
453                 t = substr(t, 1, ind-1) s2 substr(t, ind+length(s1))
454         return t
455 }
456
457 # There should be one or two tabs after the colon.
458 function format_preamble()
459 {
460         sub(/:[ \t]*/, ":")
461         if (match($0, /[A-Za-z0-9()# \t]+[ \t]*:[ \t]*/) == 1) {
462                 if (RLENGTH < 8)
463                         sub(/:/, ":\t\t")
464                 else
465                         sub(/:/, ":\t")
466         }
467 }
468
469 # Replace directly specified directories with macros
470 function use_macros()
471 {
472         gsub(bindir, "%{_bindir}")
473         gsub("%{_prefix}/bin", "%{_bindir}")
474         gsub("%{prefix}/bin", "%{_bindir}")
475
476         for (c = 1; c <= NF; c++) {
477                 if ($c ~ sbindir "/fix-info-dir")
478                         continue;
479                 gsub(sbindir, "%{_sbindir}", $c)
480         }
481
482         gsub("%{prefix}/sbin", "%{_sbindir}")
483         gsub("%{_prefix}/sbib", "%{_sbindir}")
484
485         gsub(libdir, "%{_libdir}")
486         gsub("%{prefix}/lib", "%{_libdir}")
487         gsub("%{_prefix}/lib", "%{_libdir}")
488
489         for (c = 1; c <= NF; c++) {
490                 if ($c ~ sysconfdir "/cron.d")
491                         continue;
492                 if ($c ~ sysconfdir "/crontab.d")
493                         continue;
494                 if ($c ~ sysconfdir "/logrotate.d")
495                         continue;
496                 if ($c ~ sysconfdir "/pam.d")
497                         continue;
498                 if ($c ~ sysconfdir "/profile.d")
499                         continue;
500                 if ($c ~ sysconfdir "/rc.d")
501                         continue;
502                 if ($c ~ sysconfdir "/security")
503                         continue;
504                 if ($c ~ sysconfdir "/skel")
505                         continue;
506                 if ($c ~ sysconfdir "/sysconfig")
507                         continue;
508                 gsub(sysconfdir, "%{_sysconfdir}", $c)
509         }
510
511         gsub(datadir, "%{_datadir}")
512         gsub("%{prefix}/share", "%{_datadir}")
513         gsub("%{_prefix}/share", "%{_datadir}")
514
515         gsub(includedir, "%{_includedir}")
516         gsub("%{prefix}/include", "%{_includedir}")
517         gsub("%{_prefix}/include", "%{_includedir}")
518
519         gsub(mandir, "%{_mandir}")
520         gsub("%{_datadir}/man", "%{_mandir}")
521         gsub("%{_prefix}/share/man", "%{_mandir}")
522         gsub("%{prefix}/share/man", "%{_mandir}")
523         gsub("%{prefix}/man", "%{_mandir}")
524         gsub("%{_prefix}/man", "%{_mandir}")
525
526         gsub(infodir, "%{_infodir}")
527         gsub("%{prefix}/info", "%{_infodir}")
528         gsub("%{_prefix}/info", "%{_infodir}")
529
530         gsub("%{_datadir}/aclocal", "%{_aclocaldir}")
531
532         if (prefix != "/") {
533                 for (c = 1; c <= NF; c++) {
534                         if ($c ~ prefix "/sbin/fix-info-dir")
535                                 continue;
536                         gsub(prefix, "%{_prefix}", $c)
537                 }
538                 gsub("%{prefix}", "%{_prefix}")
539         }
540
541         gsub("%{PACKAGE_VERSION}", "%{version}")
542         gsub("%{PACKAGE_NAME}", "%{name}")
543
544         gsub("^%{_sbindir}", "%attr(755,root,root) %{_sbindir}")
545         gsub("^%{_bindir}", "%attr(755,root,root) %{_bindir}")
546
547         gsub("%{_datadir}/gnome/apps", "%{_applnkdir}")
548         gsub("%{_datadir}/applnk", "%{_applnkdir}")
549
550         gsub("^make$", "%{__make}")
551         gsub("^make ", "%{__make} ")
552 }
553
554 function fill(ch, n, i) {
555         for (i = 0; i < n; i++)
556                 printf("%c", ch)
557 }
558
559 function format_flush(line, indent, newline, word, first_word) {
560         first_word = 1
561         if (format_indent == -1) 
562                 newline = ""
563         else
564                 newline = fill(" ", format_indent) "- "
565
566         while (match(line, /[^\t ]+/)) {
567                 word = substr(line, RSTART, RLENGTH)
568                 if (length(newline) + length(word) + 1 > tw) {
569                         print newline
570                         
571                         if (format_indent == -1)
572                                 newline = ""
573                         else
574                                 newline = fill(" ", format_indent + 2)
575                         first_word = 1
576                 }
577
578                 if (first_word) {
579                         newline = newline word
580                         first_word = 0
581                 } else
582                         newline = newline " " word
583                         
584                 line = substr(line, RSTART + RLENGTH)
585         }
586         if (newline ~ /[^\t ]/) {
587                 print newline
588         }
589 }
590
591 function cflags(var)
592 {
593         if ($0 == var "=\"$RPM_OPT_FLAGS\"")
594                 return 0
595                 
596         sub("\$RPM_OPT_FLAGS", "%{!?debug:$RPM_OPT_FLAGS}%{?debug:-O0 -g}")
597         return 1
598 }
599
This page took 0.078129 seconds and 4 git commands to generate.