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