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