]> git.pld-linux.org Git - packages/rpm-build-tools.git/blob - adapter.awk
- fix previous fix
[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@pld.org.pl>
12 # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
13
14 BEGIN {
15         preamble = 1            # Is it part of preamble? Default - yes
16         boc = 4                 # 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 >/dev/null")
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 /^#/ && (description == 0) {
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 (!/$.*Log.*$/)
277                         print "$" "Log:$" > changelog_file
278                 boc = 0
279         }
280         if (boc == 2) {
281                 if (!/All persons listed below/) {
282                         printf "All persons listed below can be reached at " > changelog_file
283                         print "<cvs_login>@pld.org.pl\n" > changelog_file
284                 }
285                 boc = 0
286         }
287         if (boc == 3) {
288                 if (!/PLD Team/)
289                         print "* %{date} PLD Team <feedback@pld.org.pl>" > changelog_file
290                 boc = 2
291         }
292         # Define date macro.
293         if (boc == 4) {
294                 if (date == 0) {
295                         printf "%%define date\t%%(echo `LC_ALL=\"C\"" > changelog_file
296                         print " date +\"%a %b %d %Y\"`)" > changelog_file
297                         date = 1
298                 }
299                 boc = 3
300         }
301
302         sub(/[ \t]+$/, "")
303         if (!/^%[a-z]+$/ || /changelog/)
304                 print > changelog_file
305         else
306                 print
307         next
308 }
309
310 ###########
311 # SCRIPTS #
312 ###########
313 /^%pre/, (/^%[a-z]+$/ && !/^%pre/) {
314         preamble = 0
315 }
316 /^%post/, (/^%[a-z]+$/ && !/^%post/) {
317         preamble = 0
318 }
319 /^%preun/, (/^%[a-z]+$/ && !/^%preun/) {
320         preamble = 0
321 }
322 /^%postun/, (/^%[a-z]+$/ && !/^%postun/) {
323         preamble = 0
324 }
325
326 #############
327 # PREAMBLES #
328 #############
329 preamble == 1 {
330         # There should not be a space after the name of field
331         # and before the colon.
332         sub(/[ \t]*:/, ":")
333         
334         field = tolower($1)
335         fieldnlower = $1
336         if (field ~ /group(\([^)]+\)):/)
337                 next
338         if (field ~ /group:/) {
339                 format_preamble()
340                 sub(/^Utilities\//,"Applications/",$2)
341                 sub(/^Games/,"Applications/Games",$2)
342                 sub(/^X11\/Games/,"X11/Applications/Games",$2)
343                 sub(/^X11\/GNOME\/Development\/Libraries/,"X11/Development/Libraries",$2)
344                 sub(/^X11\/GNOME\/Applications/,"X11/Applications",$2)
345                 sub(/^X11\/GNOME/,"X11/Applications",$2)
346                 sub(/^X11\/Utilities/,"X11/Applications",$2)
347                 sub(/^X11\/Games\/Strategy/,"X11/Applications/Games/Strategy",$2)
348                 sub(/^Shells/,"Applications/Shells",$2)
349
350                 sub(/^[^ \t]*[ \t]*/,"")
351                 Grupa = $0
352
353                 print "Group:\t\t" Grupa
354                 if (Grupa ~ /^X11/ && x11 == 0) # Is it X11 application?
355                        x11 = 1
356
357                 byl_plik_z_grupami = 0
358                 byl_opis_grupy = 0
359                 while ((getline linia_grup < groups_file) > 0) {
360                         byl_plik_z_grupami = 1
361                         if (linia_grup == Grupa) {
362                                 byl_opis_grupy = 1
363                                 break
364                         }
365                 }
366
367                 if (!byl_plik_z_grupami)
368                         print "######\t\t" groups_file ": no such file"
369                 else if (!byl_opis_grupy)
370                         print "######\t\t" "Unknown group!"
371                 
372                 close(groups_file)
373                 next
374         }
375         
376         if (field ~ /packager:|distribution:|docdir:|prefix:/)
377                 next
378         
379         if (field ~ /buildroot:/)
380                 $0 = $1 "%{tmpdir}/%{name}-%{version}-root-%(id -u -n)"
381
382         # Use "License" instead of "Copyright" if it is (L)GPL or BSD
383         if (field ~ /copyright:/ && $2 ~ /GPL|BSD/)
384                 $1 = "License:"
385         
386         if (field ~ /name:/)
387                 name = $2
388
389         if (field ~ /version:/)
390                 version = $2
391
392         if (field ~ /serial:/)
393                 $1 = "Epoch:"
394
395         # Use %{name} and %{version} in the filenames in "Source:"
396         if (field ~ /^source/ || field ~ /patch/) {
397                 n = split($2, url, /\//)
398                 if (url[n] ~ /\.gz$/) {
399                         url[n+1] = ".gz" url[n+1]
400                         sub(/\.gz$/,"",url[n])
401                 }
402                 if (url[n] ~ /\.zip$/) {
403                         url[n+1] = ".zip" url[n+1]
404                         sub(/\.zip$/,"",url[n])
405                 }
406                 if (url[n] ~ /\.tar$/) {
407                         url[n+1] = ".tar" url[n+1]
408                         sub(/\.tar$/,"",url[n])
409                 }
410                 if (url[n] ~ /\.patch$/) {
411                         url[n+1] = ".patch" url[n+1]
412                         sub(/\.patch$/,"",url[n])
413                 }
414                 if (url[n] ~ /\.bz2$/) {
415                         url[n+1] = ".bz2" url[n+1]
416                         sub(/\.bz2$/,"",url[n])
417                 }
418                 if (url[n] ~ /\.logrotate$/) {
419                         url[n+1] = ".logrotate" url[n+1]
420                         sub(/\.logrotate$/,"",url[n])
421                 }
422                 if (url[n] ~ /\.pamd$/) {
423                         url[n+1] = ".pamd" url[n+1]
424                         sub(/\.pamd$/,"",url[n])
425                 }
426
427                 filename = url[n]
428                 url[n] = fixedsub(name, "%{name}", url[n])
429                 if (field ~ /source/) 
430                         url[n] = fixedsub(version, "%{version}", url[n])
431                 $2 = fixedsub(filename, url[n], $2)
432         }
433
434         if (field ~ /^source:/)
435                 $1 = "Source0:" 
436
437         if (field ~ /patch:/)
438                 $1 = "Patch0:"
439         
440         format_preamble()
441         
442         if ($1 ~ /%define/) {
443                 # Do not add %define of _prefix if it already is.
444                 if ($2 ~ /^_prefix/) {
445                         sub("^"prefix, $3, bindir)
446                         sub("^"prefix, $3, sbindir)
447                         sub("^"prefix, $3, libdir)
448                         sub("^"prefix, $3, datadir)
449                         sub("^"prefix, $3, includedir)
450                         prefix = $3
451                         x11 = 2
452                 }
453                 if ($2 ~ /_bindir/ && !/_sbindir/)
454                         bindir = $3
455                 if ($2 ~ /_sbindir/)
456                         sbindir = $3
457                 if ($2 ~ /_libdir/)
458                         libdir = $3
459                 if ($2 ~ /_sysconfdir/)
460                         sysconfdir = $3
461                 if ($2 ~ /_datadir/)
462                         datadir = $3
463                 if ($2 ~ /_includedir/)
464                         includedir = $3
465                 if ($2 ~ /_mandir/)
466                         mandir = $3
467                 if ($2 ~ /_infodir/)
468                         infodir = $3
469         }
470 }
471
472
473 # main()  ;-)
474 {
475         preamble = 1
476         
477         sub(/[ \t]+$/, "")
478         print
479 }
480
481
482 END {
483         if (do_not_touch_anything)
484                 exit 0
485         
486         close(changelog_file)
487         while ((getline < changelog_file) > 0)
488                 print
489         system("rm -f " changelog_file)
490
491         if (did_clean == 0) {
492                 print ""
493                 print "%clean"
494                 print "rm -rf $RPM_BUILD_ROOT"
495         }
496
497         if (date == 0) {
498                 print ""
499                 print "%define date\t%(echo `LC_ALL=\"C\" date +\"%a %b %d %Y\"`)"
500         }
501         
502         if (has_changelog == 0)
503                 print "%changelog"
504
505         if (boc > 2)
506                 print "* %{date} PLD Team <feedback@pld.org.pl>"
507         if (boc > 1) {
508                 printf "All persons listed below can be reached at "
509                 print "<cvs_login>@pld.org.pl\n"
510         }
511         if (boc > 0)
512                 print "$" "Log:$"
513 }
514
515 function fixedsub(s1,s2,t,      ind) {
516 # substitutes fixed strings (not regexps)
517         if (ind = index(t,s1))
518                 t = substr(t, 1, ind-1) s2 substr(t, ind+length(s1))
519         return t
520 }
521
522 # There should be one or two tabs after the colon.
523 function format_preamble()
524 {
525         sub(/:[ \t]*/, ":")
526         if (match($0, /[A-Za-z0-9(),#_ \t]+[ \t]*:[ \t]*/) == 1) {
527                 if (RLENGTH < 8)
528                         sub(/:/, ":\t\t")
529                 else
530                         sub(/:/, ":\t")
531         }
532 }
533
534 # Replace directly specified directories with macros
535 function use_macros()
536 {
537         gsub(bindir, "%{_bindir}")
538         gsub("%{prefix}/bin", "%{_bindir}")
539         if(prefix"/bin" == bindir)
540                 gsub("%{_prefix}/bin", "%{_bindir}")
541
542         for (c = 1; c <= NF; c++) {
543                 if ($c ~ sbindir "/fix-info-dir")
544                         continue;
545                 gsub(sbindir, "%{_sbindir}", $c)
546         }
547
548         gsub("%{prefix}/sbin", "%{_sbindir}")
549         if(prefix"/sbin" == sbindir)
550                 gsub("%{_prefix}/sbin", "%{_sbindir}")
551
552         gsub(libdir, "%{_libdir}")
553         gsub("%{prefix}/lib", "%{_libdir}")
554         if(prefix"/lib" == libdir)
555                 gsub("%{_prefix}/lib", "%{_libdir}")
556
557         for (c = 1; c <= NF; c++) {
558                 if ($c ~ sysconfdir "/{?cron.")
559                         continue;
560                 if ($c ~ sysconfdir "/{?crontab.d")
561                         continue;
562                 if ($c ~ sysconfdir "/{?logrotate.d")
563                         continue;
564                 if ($c ~ sysconfdir "/{?pam.d")
565                         continue;
566                 if ($c ~ sysconfdir "/{?profile.d")
567                         continue;
568                 if ($c ~ sysconfdir "/{?rc.d")
569                         continue;
570                 if ($c ~ sysconfdir "/{?security")
571                         continue;
572                 if ($c ~ sysconfdir "/{?skel")
573                         continue;
574                 if ($c ~ sysconfdir "/{?sysconfig")
575                         continue;
576                 gsub(sysconfdir, "%{_sysconfdir}", $c)
577         }
578
579         gsub(datadir, "%{_datadir}")
580         gsub("%{prefix}/share", "%{_datadir}")
581         if(prefix"/share" == datadir)
582                 gsub("%{_prefix}/share", "%{_datadir}")
583
584         gsub(includedir, "%{_includedir}")
585         gsub("%{prefix}/include", "%{_includedir}")
586         if(prefix"/include" == includedir)
587                 gsub("%{_prefix}/include", "%{_includedir}")
588
589         gsub(mandir, "%{_mandir}")
590         if ($0 !~ "%{_datadir}/manual")
591                 gsub("%{_datadir}/man", "%{_mandir}")
592         gsub("%{_prefix}/share/man", "%{_mandir}")
593         gsub("%{prefix}/share/man", "%{_mandir}")
594         gsub("%{prefix}/man", "%{_mandir}")
595         gsub("%{_prefix}/man", "%{_mandir}")
596
597         gsub(infodir, "%{_infodir}")
598         gsub("%{prefix}/info", "%{_infodir}")
599         gsub("%{_prefix}/info", "%{_infodir}")
600
601         if (prefix !~ "/X11R6") {
602                 gsub("%{_datadir}/aclocal", "%{_aclocaldir}")
603         }
604
605         if (prefix != "/") {
606                 for (c = 1; c <= NF; c++) {
607                         if ($c ~ prefix "/sbin/fix-info-dir")
608                                 continue;
609                         gsub(prefix, "%{_prefix}", $c)
610                 }
611                 gsub("%{prefix}", "%{_prefix}")
612         }
613
614         gsub("%{PACKAGE_VERSION}", "%{version}")
615         gsub("%{PACKAGE_NAME}", "%{name}")
616
617         # we can move files between tge dirs below
618         if ($0 !~ "%{_applnkdir}") {
619                 gsub("%{_datadir}/gnome/apps", "%{_applnkdir}")
620                 gsub("%{_datadir}/applnk", "%{_applnkdir}")
621         }
622
623         gsub("^make$", "%{__make}")
624         gsub("^make ", "%{__make} ")
625
626         gsub("/usr/src/linux", "%{_kernelsrcdir}")
627         gsub("%{_prefix}/src/linux", "%{_kernelsrcdir}")
628 }
629         
630 function use_files_macros()
631 {
632         gsub("^%{_sbindir}", "%attr(755,root,root) %{_sbindir}")
633         gsub("^%{_bindir}", "%attr(755,root,root) %{_bindir}")
634 }
635
636 function fill(ch, n, i) {
637         for (i = 0; i < n; i++)
638                 printf("%c", ch)
639 }
640
641 function format_flush(line, indent, newline, word, first_word) {
642         first_word = 1
643         if (format_indent == -1) 
644                 newline = ""
645         else
646                 newline = fill(" ", format_indent) "- "
647
648         while (match(line, /[^\t ]+/)) {
649                 word = substr(line, RSTART, RLENGTH)
650                 if (length(newline) + length(word) + 1 > tw) {
651                         print newline
652                         
653                         if (format_indent == -1)
654                                 newline = ""
655                         else
656                                 newline = fill(" ", format_indent + 2)
657                         first_word = 1
658                 }
659
660                 if (first_word) {
661                         newline = newline word
662                         first_word = 0
663                 } else
664                         newline = newline " " word
665                         
666                 line = substr(line, RSTART + RLENGTH)
667         }
668         if (newline ~ /[^\t ]/) {
669                 print newline
670         }
671 }
672
673 function cflags(var)
674 {
675         if ($0 == var "=\"$RPM_OPT_FLAGS\"") {
676                 removed[var] = 1
677                 return 0
678         }
679                 
680         if (!/!\?debug/)
681                 sub("\$RPM_OPT_FLAGS", "%{rpmcflags}")
682         return 1
683 }
684
This page took 0.11685 seconds and 4 git commands to generate.