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