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