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