]> git.pld-linux.org Git - packages/rpm-build-tools.git/blob - adapter.awk
- don't replace 'cp -pR' with 'install' line
[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-2003 PLD-Team <feedback@pld-linux.org>
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 # TODO
15 # - parse ../PLD-doc/BuildRequires.txt and setup proper BR epoches?
16 # - add "-nc" option to skip CVS interaction
17
18 BEGIN {
19         preamble = 1            # Is it part of preamble? Default - yes
20         boc = 4                 # Beggining of %changelog
21         bod = 0                 # Beggining of %description
22         tw = 70                 # Descriptions width
23
24         # If variable removed, then 1 (for removing it from export)
25         removed["LDFLAGS"] = 0
26         removed["CFLAGS"] = 0
27         removed["CXXFLAGS"] = 0
28
29         # If 1, we are inside of comment block (started with /^#%/)
30         comment_block = 0
31
32         # File with rpm groups
33         "rpm --eval %_sourcedir" | getline groups_file
34         groups_file = groups_file "/rpm.groups"
35         system("cd `rpm --eval %_sourcedir`; cvs up rpm.groups >/dev/null")
36
37         # Temporary file for changelog section
38         changelog_file = ENVIRON["HOME"] "/tmp/adapter.changelog"
39
40         # Load rpm macros
41         "rpm --eval %_prefix"   | getline prefix
42         "rpm --eval %_bindir"   | getline bindir
43         "rpm --eval %_sbindir"  | getline sbindir
44         "rpm --eval %_libdir"   | getline libdir
45         "rpm --eval %_sysconfdir" | getline sysconfdir
46         "rpm --eval %_datadir"  | getline datadir
47         "rpm --eval %_includedir" | getline includedir
48         "rpm --eval %_mandir"   | getline mandir
49         "rpm --eval %_infodir"  | getline infodir
50 }
51
52 # There should be a comment with CVS keywords on the first line of file.
53 FNR == 1 {
54         if (!/# \$Revision:/)   # If this line is already OK?
55                 print "# $" "Revision:$, " "$" "Date:$" # No
56         else {
57                 print $0                                # Yes
58                 next            # It is enough for first line
59         }
60 }
61
62 # If the latest line matched /%files/
63 defattr == 1 {
64         if ($0 !~ /defattr/)    # If no %defattr
65                 print "%defattr(644,root,root,755)"     # Add it
66         else
67                 $0 = "%defattr(644,root,root,755)"      # Correct mistakes (if any)
68         defattr = 0
69 }
70
71 # Comments
72 /^#/ && (description == 0) {
73         if (/This file does not like to be adapterized!/) {
74                 print                   # print this message
75                 while (getline)         # print the rest of spec as it is
76                         print
77                 do_not_touch_anything = 1 # do not touch anything in END()
78                 exit 0
79         }
80
81         # Generally, comments are printed without touching
82         sub(/[ \t]+$/, "")
83         print $0
84         next
85 }
86
87 # Remove defining _applnkdir (this macro has been included in rpm-3.0.4)
88 /^%define/ {
89         if ($2 == "_applnkdir")
90                 next
91         if ($2 == "date")
92                 date = 1
93 }
94
95 ################
96 # %description #
97 ################
98 /^%description/, (/^%[a-z]+/ && !/^%description/ && !/^%((end)?if|else)/) {
99         preamble = 0
100
101         if (/^%description/) {
102                 bod++
103                 format_line = ""
104                 format_indent = -1
105         }
106
107         # Format description
108         if (description == 1 && !/^%[a-z]+/ && !/^%description/) {
109                 if (/^[ \t]*$/) {
110                         format_flush(format_line, format_indent)
111                         print ""
112                         format_line = ""
113                         format_indent = -1
114                 } else if (/^[ \t]*[-\*][ \t]*/) {
115                         format_flush(format_line, format_indent)
116                         match($0, /^[ \t]*/)
117                         format_indent = RLENGTH
118                         match($0, /^[ \t]*[-\*][ \t]/)
119                         format_line = substr($0, RLENGTH)
120                 } else
121                         format_line = format_line " " $0
122                 next
123         }
124
125         if (/^%[a-z]+/ && (!/^%description/ || bod == 2)) {
126                 if (NF > 3 && $2 == "-l") {
127                         ll = $1
128                         for (i = 4; i <= NF; i++)
129                                 ll = ll " " $i
130                         $0 = ll " -l " $3
131                 }
132                 format_flush(format_line, format_indent)
133                 if (bod == 2) {
134                         bod = 1
135                         description = 1
136                 } else {
137                         bod = 0
138                         description = 0
139                 }
140         } else
141                 description = 1
142 }
143
144 #########
145 # %prep #
146 #########
147 /^%prep/, (/^%[a-z]+$/ && !/^%prep/ && !/^%((end)?if|else)/) {
148         preamble = 0
149
150         # Add '-q' to %setup
151         if (/^%setup/ && !/-q/)
152                 sub(/^%setup/, "%setup -q")
153 }
154
155 ##########
156 # %build #
157 ##########
158 /^%build/, (/^%[a-z]+$/ && !/^%build/ && !/^%((end)?if|else)/) {
159         preamble = 0
160
161         use_macros()
162
163         if (/^automake$/)
164                 sub(/$/, " -a -c")
165
166         if (/LDFLAGS/) {
167                 if (/LDFLAGS="-s"/) {
168                         removed["LDFLAGS"] = 1
169                         next
170                 } else {
171                         split($0, tmp, "LDFLAGS=")
172                         count = split(tmp[2], flags, "\"")
173                         if (flags[1] != "" && flags[1] !~ "!?debug") {
174                                 sub(/-s[" ]?/, "%{rpmldflags} ", flags[1])
175                                 $0 = tmp[1] line[1] "LDFLAGS=" flags[1] "\""
176                                 for (i = 2; i < count; i++)
177                                         $0 = $0 flags[i] "\""
178                         }
179                 }
180         }
181
182         if (/CFLAGS=/)
183                 if (cflags("CFLAGS") == 0)
184                         next
185
186         if (/CXXFLAGS=/)
187                 if (cflags("CXXFLAGS") == 0)
188                         next
189
190         if (/^export /) {
191                 if (removed["LDFLAGS"])
192                         sub(" LDFLAGS", "")
193                 if (removed["CFLAGS"])
194                         sub(" CFLAGS", "")
195                 if (removed["CXXFLAGS"])
196                         sub(" CXXFLAGS", "")
197                 # Is there still something?
198                 if (/^export[ ]*$/)
199                         next
200         }
201
202 }
203
204 ##########
205 # %clean #
206 ##########
207 /^%clean/, (/^%[a-z]+$/ && !/^%clean/ && !/^%((end)?if|else)/) {
208         did_clean = 1
209 }
210
211 ############
212 # %install #
213 ############
214 /^%install/, (/^%[a-z]+$/ && !/^%install/ && !/^%((end)?if|else)/) {
215
216         preamble = 0
217
218         if (/^[ \t]*rm([ \t]+-[rf]+)*[ \t]+\${?RPM_BUILD_ROOT}?/ && did_rmroot==0) {
219                 did_rmroot=1
220                 print "rm -rf $RPM_BUILD_ROOT"
221                 next
222         }
223
224         if (!/^(#?[ \t]*)$/ && !/^%install/ && did_rmroot==0) {
225                 print "rm -rf $RPM_BUILD_ROOT"
226                 did_rmroot=1
227         }
228
229         use_macros()
230
231         # 'install -d' instead 'mkdir -p'
232         if (/mkdir -p/)
233                 sub(/mkdir -p/, "install -d")
234
235         # 'install' instead 'cp -p'
236         if (/cp -p\b/)
237                 sub(/cp -p/, "install")
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/ && !/^%((end)?if|else)/) {
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         skip = 0
275         # There should be some CVS keywords on the first line of %changelog.
276         if (boc == 3) {
277                 if (!/PLD Team/)
278                         print "* %{date} PLD Team <feedback@pld-linux.org>" > changelog_file
279                 else
280                         skip = 1
281                 boc = 2
282         }
283         if (boc == 2 && !skip) {
284                 if (!/All persons listed below/) {
285                         printf "All persons listed below can be reached at " > changelog_file
286                         print "<cvs_login>@pld-linux.org\n" > changelog_file
287                 } else
288                         skip = 1
289                 boc = 1
290         }
291         if (boc == 1 && !skip) {
292                 if (!/^$/) {
293                         if (!/\$.*Log:.*\$/)
294                                 print "$" "Log:$" > changelog_file
295                         boc = 0
296                 }
297         }
298         # Define date macro.
299         if (boc == 4) {
300                 if (date == 0) {
301                         printf "%%define date\t%%(echo `LC_ALL=\"C\"" > changelog_file
302                         print " date +\"%a %b %d %Y\"`)" > changelog_file
303                         date = 1
304                 }
305                 boc = 3
306         }
307
308         sub(/[ \t]+$/, "")
309         if (!/^%[a-z]+$/ || /changelog/)
310                 print > changelog_file
311         else
312                 print
313         next
314 }
315
316 ###########
317 # SCRIPTS #
318 ###########
319 /^%pre/, (/^%[a-z]+$/ && !/^%pre/) {
320         preamble = 0
321 }
322 /^%post/, (/^%[a-z]+$/ && !/^%post/) {
323         preamble = 0
324 }
325 /^%preun/, (/^%[a-z]+$/ && !/^%preun/) {
326         preamble = 0
327 }
328 /^%postun/, (/^%[a-z]+$/ && !/^%postun/) {
329         preamble = 0
330 }
331
332 #############
333 # PREAMBLES #
334 #############
335 preamble == 1 {
336         # There should not be a space after the name of field
337         # and before the colon.
338         sub(/[ \t]*:/, ":")
339
340         field = tolower($1)
341         fieldnlower = $1
342         if (field ~ /group(\([^)]+\)):/)
343                 next
344         if (field ~ /group:/) {
345                 format_preamble()
346                 sub(/^Utilities\//,"Applications/",$2)
347                 sub(/^Games/,"Applications/Games",$2)
348                 sub(/^X11\/Games/,"X11/Applications/Games",$2)
349                 sub(/^X11\/GNOME\/Development\/Libraries/,"X11/Development/Libraries",$2)
350                 sub(/^X11\/GNOME\/Applications/,"X11/Applications",$2)
351                 sub(/^X11\/GNOME/,"X11/Applications",$2)
352                 sub(/^X11\/Utilities/,"X11/Applications",$2)
353                 sub(/^X11\/Games\/Strategy/,"X11/Applications/Games/Strategy",$2)
354                 sub(/^Shells/,"Applications/Shells",$2)
355
356                 sub(/^[^ \t]*[ \t]*/,"")
357                 Grupa = $0
358
359                 print "Group:\t\t" Grupa
360                 if (Grupa ~ /^X11/ && x11 == 0) # Is it X11 application?
361                         x11 = 1
362
363                 byl_plik_z_grupami = 0
364                 byl_opis_grupy = 0
365                 while ((getline linia_grup < groups_file) > 0) {
366                         byl_plik_z_grupami = 1
367                         if (linia_grup == Grupa) {
368                                 byl_opis_grupy = 1
369                                 break
370                         }
371                 }
372
373                 if (!byl_plik_z_grupami)
374                         print "######\t\t" groups_file ": no such file"
375                 else if (!byl_opis_grupy)
376                         print "######\t\t" "Unknown group!"
377
378                 close(groups_file)
379                 next
380         }
381
382         if (field ~ /packager:|distribution:|docdir:|prefix:/)
383                 next
384
385         if (field ~ /buildroot:/)
386                 $0 = $1 "%{tmpdir}/%{name}-%{version}-root-%(id -u -n)"
387
388         # Use "License" instead of "Copyright" if it is (L)GPL or BSD
389         if (field ~ /copyright:/ && $2 ~ /GPL|BSD/)
390                 $1 = "License:"
391
392         if (field ~ /name:/)
393                 name = $2
394
395         if (field ~ /version:/)
396                 version = $2
397
398         if (field ~ /serial:/)
399                 $1 = "Epoch:"
400
401         # Use %{name} and %{version} in the filenames in "Source:"
402         if (field ~ /^source/ || field ~ /patch/) {
403                 n = split($2, url, /\//)
404                 if (url[n] ~ /\.gz$/) {
405                         url[n+1] = ".gz" url[n+1]
406                         sub(/\.gz$/,"",url[n])
407                 }
408                 if (url[n] ~ /\.zip$/) {
409                         url[n+1] = ".zip" url[n+1]
410                         sub(/\.zip$/,"",url[n])
411                 }
412                 if (url[n] ~ /\.tar$/) {
413                         url[n+1] = ".tar" url[n+1]
414                         sub(/\.tar$/,"",url[n])
415                 }
416                 if (url[n] ~ /\.patch$/) {
417                         url[n+1] = ".patch" url[n+1]
418                         sub(/\.patch$/,"",url[n])
419                 }
420                 if (url[n] ~ /\.bz2$/) {
421                         url[n+1] = ".bz2" url[n+1]
422                         sub(/\.bz2$/,"",url[n])
423                 }
424                 if (url[n] ~ /\.logrotate$/) {
425                         url[n+1] = ".logrotate" url[n+1]
426                         sub(/\.logrotate$/,"",url[n])
427                 }
428                 if (url[n] ~ /\.pamd$/) {
429                         url[n+1] = ".pamd" url[n+1]
430                         sub(/\.pamd$/,"",url[n])
431                 }
432
433                 filename = url[n]
434                 url[n] = fixedsub(name, "%{name}", url[n])
435                 if (field ~ /source/)
436                         url[n] = fixedsub(version, "%{version}", url[n])
437                 $2 = fixedsub(filename, url[n], $2)
438
439                 # sourceforge urls
440                 sub("^http://dl.sourceforge.net/sourceforge/", "http://dl.sourceforge.net/", $2)
441                 sub("^http://dl.sourceforge.net/%{name}/", "http://dl.sourceforge.net/"  name "/", $2)
442         }
443
444
445         if (field ~ /^source:/)
446                 $1 = "Source0:"
447
448         if (field ~ /patch:/)
449                 $1 = "Patch0:"
450
451         format_preamble()
452
453         if ($1 ~ /%define/) {
454                 # Do not add %define of _prefix if it already is.
455                 if ($2 ~ /^_prefix/) {
456                         sub("^"prefix, $3, bindir)
457                         sub("^"prefix, $3, sbindir)
458                         sub("^"prefix, $3, libdir)
459                         sub("^"prefix, $3, datadir)
460                         sub("^"prefix, $3, includedir)
461                         prefix = $3
462                 }
463                 if ($2 ~ /_bindir/ && !/_sbindir/)
464                         bindir = $3
465                 if ($2 ~ /_sbindir/)
466                         sbindir = $3
467                 if ($2 ~ /_libdir/)
468                         libdir = $3
469                 if ($2 ~ /_sysconfdir/)
470                         sysconfdir = $3
471                 if ($2 ~ /_datadir/)
472                         datadir = $3
473                 if ($2 ~ /_includedir/)
474                         includedir = $3
475                 if ($2 ~ /_mandir/)
476                         mandir = $3
477                 if ($2 ~ /_infodir/)
478                         infodir = $3
479         }
480 }
481
482
483 # main() ;-)
484 {
485         preamble = 1
486
487         sub(/[ \t]+$/, "")
488         print
489 }
490
491
492 END {
493         if (do_not_touch_anything)
494                 exit 0
495
496         close(changelog_file)
497         while ((getline < changelog_file) > 0)
498                 print
499         system("rm -f " changelog_file)
500
501         if (did_clean == 0) {
502                 print ""
503                 print "%clean"
504                 print "rm -rf $RPM_BUILD_ROOT"
505         }
506
507         if (date == 0) {
508                 print ""
509                 print "%define date\t%(echo `LC_ALL=\"C\" date +\"%a %b %d %Y\"`)"
510         }
511
512         if (has_changelog == 0)
513                 print "%changelog"
514
515         if (boc > 2)
516                 print "* %{date} PLD Team <feedback@pld-linux.org>"
517         if (boc > 1) {
518                 printf "All persons listed below can be reached at "
519                 print "<cvs_login>@pld-linux.org\n"
520         }
521         if (boc > 0)
522                 print "$" "Log:$"
523 }
524
525 function fixedsub(s1,s2,t, ind) {
526 # substitutes fixed strings (not regexps)
527         if (ind = index(t,s1))
528                 t = substr(t, 1, ind-1) s2 substr(t, ind+length(s1))
529         return t
530 }
531
532 # There should be one or two tabs after the colon.
533 function format_preamble()
534 {
535         sub(/:[ \t]*/, ":")
536         if (match($0, /[A-Za-z0-9(),#_ \t]+[ \t]*:[ \t]*/) == 1) {
537                 if (RLENGTH < 8)
538                         sub(/:/, ":\t\t")
539                 else
540                         sub(/:/, ":\t")
541         }
542 }
543
544 # Replace directly specified directories with macros
545 function use_macros()
546 {
547         gsub(bindir, "%{_bindir}")
548         gsub("%{prefix}/bin", "%{_bindir}")
549         if(prefix"/bin" == bindir)
550                 gsub("%{_prefix}/bin", "%{_bindir}")
551
552         for (c = 1; c <= NF; c++) {
553                 if ($c ~ sbindir "/fix-info-dir")
554                         continue;
555                 gsub(sbindir, "%{_sbindir}", $c)
556         }
557
558         gsub("%{prefix}/sbin", "%{_sbindir}")
559         if(prefix"/sbin" == sbindir)
560                 gsub("%{_prefix}/sbin", "%{_sbindir}")
561
562         for (c = 1; c <= NF; c++) {
563                 if ($c ~ sysconfdir "/{?cron.")
564                         continue;
565                 if ($c ~ sysconfdir "/{?crontab.d")
566                         continue;
567                 if ($c ~ sysconfdir "/{?logrotate.d")
568                         continue;
569                 if ($c ~ sysconfdir "/{?pam.d")
570                         continue;
571                 if ($c ~ sysconfdir "/{?profile.d")
572                         continue;
573                 if ($c ~ sysconfdir "/{?rc.d")
574                         continue;
575                 if ($c ~ sysconfdir "/{?security")
576                         continue;
577                 if ($c ~ sysconfdir "/{?skel")
578                         continue;
579                 if ($c ~ sysconfdir "/{?sysconfig")
580                         continue;
581                 gsub(sysconfdir, "%{_sysconfdir}", $c)
582         }
583
584         gsub(datadir, "%{_datadir}")
585         gsub("%{prefix}/share", "%{_datadir}")
586         if(prefix"/share" == datadir)
587                 gsub("%{_prefix}/share", "%{_datadir}")
588
589         gsub(includedir, "%{_includedir}")
590         gsub("%{prefix}/include", "%{_includedir}")
591         if(prefix"/include" == includedir)
592                 gsub("%{_prefix}/include", "%{_includedir}")
593
594         gsub(mandir, "%{_mandir}")
595         if ($0 !~ "%{_datadir}/manual")
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         if (prefix !~ "/X11R6") {
607                 gsub("%{_datadir}/aclocal", "%{_aclocaldir}")
608         }
609
610         if (prefix != "/") {
611                 for (c = 1; c <= NF; c++) {
612                         if ($c ~ prefix "/sbin/fix-info-dir")
613                                 continue;
614                         gsub(prefix, "%{_prefix}", $c)
615                 }
616                 gsub("%{prefix}", "%{_prefix}")
617         }
618
619         gsub("%{PACKAGE_VERSION}", "%{version}")
620         gsub("%{PACKAGE_NAME}", "%{name}")
621
622         # we can move files between the dirs below
623         if ($0 !~ "%{_applnkdir}") {
624                 gsub("%{_datadir}/gnome/apps", "%{_applnkdir}")
625         }
626
627         gsub("^make$", "%{__make}")
628         gsub("^make ", "%{__make} ")
629
630         gsub("/usr/src/linux", "%{_kernelsrcdir}")
631         gsub("%{_prefix}/src/linux", "%{_kernelsrcdir}")
632 }
633
634
635 # insertion sort of A[1..n]
636 # copied from mawk manual
637 function isort(A,n,             i,j,hold) {
638         for (i = 2; i <= n; i++) {
639                 hold = A[j = i]
640                 while (A[j-1] > hold) {
641                         j-- ; A[j+1] = A[j]
642                 }
643                 A[j] = hold
644         }
645         # sentinel A[0] = "" will be created if needed
646 }
647
648
649 function use_files_macros(      i, n, t, a)
650 {
651         gsub("^%{_sbindir}", "%attr(755,root,root) %{_sbindir}")
652         gsub("^%{_bindir}", "%attr(755,root,root) %{_bindir}")
653
654         # sort %verify attrs
655         if (match($0, /%verify\(not (.*)\)/)) {
656                 t = substr($0, RSTART, RLENGTH)
657                 gsub(/^%verify\(not |\)$/, "", t)
658                 n = split(t, a, / /)
659                 isort(a, n)
660
661                 s = "%verify(not"
662                 for (i = 1 ; i <= n; i++) {
663                         s = s " " a[i]
664                 }
665                 s = s ")"
666
667                 gsub(/%verify\(not .*\)/, s)
668         }
669 }
670
671 function fill(ch, n, i) {
672         for (i = 0; i < n; i++)
673                 printf("%c", ch)
674 }
675
676 function format_flush(line, indent, newline, word, first_word) {
677         first_word = 1
678         if (format_indent == -1)
679                 newline = ""
680         else
681                 newline = fill(" ", format_indent) "- "
682
683         while (match(line, /[^\t ]+/)) {
684                 word = substr(line, RSTART, RLENGTH)
685                 if (length(newline) + length(word) + 1 > tw) {
686                         print newline
687
688                         if (format_indent == -1)
689                                 newline = ""
690                         else
691                                 newline = fill(" ", format_indent + 2)
692                         first_word = 1
693                 }
694
695                 if (first_word) {
696                         newline = newline word
697                         first_word = 0
698                 } else
699                         newline = newline " " word
700
701                 line = substr(line, RSTART + RLENGTH)
702         }
703         if (newline ~ /[^\t ]/) {
704                 print newline
705         }
706 }
707
708 function cflags(var)
709 {
710         if ($0 == var "=\"$RPM_OPT_FLAGS\"") {
711                 removed[var] = 1
712                 return 0
713         }
714
715         if (!/!\?debug/)
716                 sub("\$RPM_OPT_FLAGS", "%{rpmcflags}")
717         return 1
718 }
719
This page took 0.176754 seconds and 4 git commands to generate.