]> git.pld-linux.org Git - packages/rpm-build-tools.git/blob - adapter.awk
don't fail if no sources found (hack to allow build nosrc packages)
[packages/rpm-build-tools.git] / adapter.awk
1 #!/bin/awk -f
2 #
3 # This is adapter v0.25. 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 # Do not touch commented sections!
68 /^#%/ {
69         comment_block = 1
70 }
71
72 comment_block == 1 {
73         if (/^#/) {
74                 # Print as is
75                 print $0
76                 next
77         } else
78                 comment_block = 0
79 }
80
81 # Remove defining _applnkdir (this macro has been included in rpm-3.0.4)
82 /^%define/ {
83         if ($2 == "_applnkdir")
84                 next
85         if ($2 == "date")
86                 date = 1
87 }
88
89 ################
90 # %description #
91 ################
92 /^%description/, (/^%[a-z]+/ && !/^%description/) {
93         preamble = 0
94
95         if (/^%description/) {
96                 bod++
97                 format_line = ""
98                 format_indent = -1
99         }
100
101         # Define _prefix and _mandir if it is X11 application
102         if (/^%description$/ && x11 == 1) {
103                 print "%define\t\t_prefix\t\t/usr/X11R6"
104                 print "%define\t\t_mandir\t\t%{_prefix}/man\n"
105                 prefix = "/usr/X11R6"
106                 x11 = 2
107         }
108         
109         # Format description
110         if (description == 1 && !/^%[a-z]+/ && !/^%description/) {
111                 if (/^[ \t]*$/) {
112                         format_flush(format_line, format_indent)
113                         print ""
114                         format_line = ""
115                         format_indent = -1
116                 } else if (/^[ \t]*[-\*][ \t]*/) {
117                         format_flush(format_line, format_indent)
118                         match($0, /^[ \t]*/)    
119                         format_indent = RLENGTH
120                         match($0, /^[ \t]*[-\*][ \t]/)
121                         format_line = substr($0, RLENGTH)
122                 } else 
123                         format_line = format_line " " $0
124                 next
125         }
126  
127         if (/^%[a-z]+/ && (!/^%description/ || bod == 2)) {
128                 format_flush(format_line, format_indent)
129                 if (bod == 2) {
130                         bod = 1
131                         description = 1
132                 } else {
133                         bod = 0
134                         description = 0
135                 }
136         } else
137                 description = 1
138 }
139
140 #########
141 # %prep #
142 #########
143 /^%prep/, (/^%[a-z]+$/ && !/^%prep/) {
144         preamble = 0
145         
146         # Add '-q' to %setup
147         if (/^%setup/ && !/-q/)
148                 sub(/^%setup/, "%setup -q")
149 }
150
151 ##########
152 # %build #
153 ##########
154 /^%build/, (/^%[a-z]+$/ && !/^%build/) {
155         preamble = 0
156
157         use_macros()
158         
159         if (/^automake$/)
160                 sub(/$/, " -a -c")
161
162         if (/LDFLAGS/) {
163                 if (/LDFLAGS="-s"/) {
164                         removed["LDFLAGS"] = 1
165                         next
166                 } else {
167                         split($0, tmp, "LDFLAGS=")
168                         count = split(tmp[2], flags, "\"")
169                         if (flags[1] != "" && flags[1] !~ "!?debug") {
170                                 sub(/-s[" ]?/, "%{rpmldflags} ", flags[1])
171                                 $0 = tmp[1] line[1] "LDFLAGS=" flags[1] "\""
172                                 for (i = 2; i < count; i++)
173                                         $0 = $0 flags[i] "\""
174                         }
175                 }
176         }
177
178         if (/CFLAGS=/)
179                 if (cflags("CFLAGS") == 0)
180                         next
181
182         if (/CXXFLAGS=/)
183                 if (cflags("CXXFLAGS") == 0)
184                         next
185         
186         if (/^export /) {
187                 if (removed["LDFLAGS"])
188                         sub(" LDFLAGS", "")
189                 if (removed["CFLAGS"])
190                         sub(" CFLAGS", "")
191                 if (removed["CXXFLAGS"])
192                         sub(" CXXFLAGS", "")
193                 # Is there still something?
194                 if (/^export[ ]*$/)
195                         next
196         }
197                         
198 }
199
200 ##########
201 # %clean #
202 ##########
203 /^%clean/, (/^%[a-z]+$/ && !/^%clean/) {
204         did_clean = 1
205 }
206
207 ############
208 # %install #
209 ############
210 /^%install/, (/^%[a-z]+$/ && !/^%install/) {
211         
212         preamble = 0
213         
214         if (/^[ \t]*rm([ \t]+-[rf]+)*[ \t]+\${?RPM_BUILD_ROOT}?/ && did_rmroot==0) {
215                 did_rmroot=1
216                 print "rm -rf $RPM_BUILD_ROOT"
217                 next
218         }
219
220         if (!/^(#?[ \t]*)$/ && !/^%install/ && did_rmroot==0) {
221                 print "rm -rf $RPM_BUILD_ROOT"
222                 did_rmroot=1
223         }
224         
225         use_macros()
226         
227         # 'install -d' instead 'mkdir -p'
228         if (/mkdir -p/)
229                 sub(/mkdir -p/, "install -d")
230                 
231         # No '-u root' or '-g root' for 'install'
232         if (/^install/ && /-[ug][ \t]*root/)
233                 gsub(/-[ug][ \t]*root /, "")
234         
235         if (/^install/ && /-m[ \t]*644/)
236                 gsub(/-m[ \t]*644 /, "")
237         
238         # No lines contain 'chown' or 'chgrp' if owner/group is 'root'
239         if (($1 ~ /chown/ && $2 ~ /root\.root/) || ($1 ~ /chgrp/ && $2 ~ /root/))
240                 next
241         
242         # No lines contain 'chmod' if it sets the modes to '644'
243         if ($1 ~ /chmod/ && $2 ~ /644/)
244                 next
245 }
246
247 ##########
248 # %files #
249 ##########
250 /^%files/, (/^%[a-z \-]+$/ && !/^%files/) {
251         preamble = 0
252         
253         if ($0 ~ /^%files/)
254                 defattr = 1
255         
256         use_macros()
257 }
258
259 ##############
260 # %changelog #
261 ##############
262 /^%changelog/, (/^%[a-z]+$/ && !/^%changelog/) {
263         preamble = 0
264         has_changelog = 1
265         # There should be some CVS keywords on the first line of %changelog.
266         if (boc == 1) {
267                 if (!/PLD Team/) {
268                         print "* %{date} PLD Team <pld-list@pld.org.pl>" > changelog_file
269                         printf "All persons listed below can be reached at " > changelog_file
270                         print "<cvs_login>@pld.org.pl\n" > changelog_file
271                         print "$" "Log:$" > changelog_file
272                 }
273                 boc = 0
274         }
275         
276         # Define date macro.
277         if (boc == 2) {
278                 if (date == 0) {
279                         printf "%%define date\t%%(echo `LC_ALL=\"C\"" > changelog_file
280                         print " date +\"%a %b %d %Y\"`)" > changelog_file
281                         date = 1
282                 }
283                 boc = 1
284         }
285
286         if (!/^%[a-z]+$/ || /changelog/)
287                 print > changelog_file
288         else
289                 print
290         next
291 }
292
293 ###########
294 # SCRIPTS #
295 ###########
296 /^%pre/, (/^%[a-z]+$/ && !/^%pre/) {
297         preamble = 0
298 }
299 /^%post/, (/^%[a-z]+$/ && !/^%post/) {
300         preamble = 0
301 }
302 /^%preun/, (/^%[a-z]+$/ && !/^%preun/) {
303         preamble = 0
304 }
305 /^%postun/, (/^%[a-z]+$/ && !/^%postun/) {
306         preamble = 0
307 }
308
309 #############
310 # PREAMBLES #
311 #############
312 preamble == 1 {
313         # There should not be a space after the name of field
314         # and before the colon.
315         sub(/[ \t]*:/, ":")
316         
317         field = tolower($1)
318         if (Byla_grupa == 1 && field ~ /^#/)
319                 next
320         if (Byla_grupa == 1 && field !~ /group(\(..\))?:/) {
321                 Byla_grupa = 0
322                 print "Group:\t\t" Grupa["en"]
323                 if (Grupa["en"] ~ /^X11/ && x11 == 0)   # Is it X11 application?
324                        x11 = 1
325
326                 byl_plik_z_grupami = 0
327                 byl_opis_grupy = 0
328                 while ((getline linia_grup < groups_file) > 0) {
329                         byl_plik_z_grupami = 1
330                         if (linia_grup == Grupa["en"]) {
331                                 byl_opis_grupy = 1
332                                 break
333                         }
334                 }
335
336                 if (!byl_plik_z_grupami)
337                         print "######\t\t" groups_file ": no such file"
338                 else if (!byl_opis_grupy)
339                         print "######\t\t" "Unknown group!"
340                 else
341                         while (getline linia_grup < groups_file) {
342                                 if (linia_grup == "")
343                                         break
344                                 split(linia_grup, g, /[\[\]:]/)
345                                 sub(/^[ \t]*/,"",g[4])
346                                 Grupa[g[2]]=g[4]
347                         }
348                 
349                 close(groups_file)
350
351                 delete Grupa["en"]
352                 for (jezyk in Grupa) {
353                         print "Group(" jezyk "):\t" Grupa[jezyk] | "sort"
354                         delete Grupa[jezyk]
355                 }
356                 close ("sort")
357         }
358         
359         if (field ~ /packager:|distribution:|docdir:|prefix:/)
360                 next
361         
362         if (field ~ /buildroot:/)
363                 $0 = $1 "%{tmpdir}/%{name}-%{version}-root-%(id -u -n)"
364
365         if (field ~ /group(\(..\))?:/) {
366                 format_preamble()
367                 sub(/^Utilities\//,"Applications/",$2)
368                 sub(/^Games/,"Applications/Games",$2)
369                 sub(/^X11\/Games/,"Applications\/Games",$2)
370                 sub(/^X11\/GNOME\/Development\/Libraries/,"X11/Development/Libraries",$2)
371                 sub(/^X11\/GNOME\/Applications/,"X11/Applications",$2)
372                 sub(/^X11\/GNOME/,"X11/Applications",$2)
373                 sub(/^X11\/Utilities/,"X11\/Applications",$2)
374                 sub(/^X11\/Games\/Strategy/,"X11/Applications/Games/Strategy",$2)
375                 sub(/^Shells/,"Applications/Shells",$2)
376
377                 if (!match(field,/\(..\):/))
378                         glang="en"
379                 else
380                         glang=substr(field,RSTART+1,2)
381                 sub(/^[^ \t]*[ \t]*/,"")
382                 Grupa[glang] = $0
383                 Byla_grupa = 1
384                 
385                 next    # Line is already formatted and printed
386         }
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
440         if (field ~ /source:/)
441                 $1 = "Source0:" 
442
443         if (field ~ /patch:/)
444                 $1 = "Patch0:"
445         
446         format_preamble()
447         
448         if ($1 ~ /%define/) {
449                 # Do not add %define of _prefix if it already is.
450                 if ($2 ~ /_prefix/) {
451                         sub("^"prefix, $3, bindir)
452                         sub("^"prefix, $3, sbindir)
453                         sub("^"prefix, $3, libdir)
454                         sub("^"prefix, $3, datadir)
455                         sub("^"prefix, $3, includedir)
456                         prefix = $3
457                         x11 = 2
458                 }
459                 if ($2 ~ /_bindir/ && !/_sbindir/)
460                         bindir = $3
461                 if ($2 ~ /_sbindir/)
462                         sbindir = $3
463                 if ($2 ~ /_libdir/)
464                         libdir = $3
465                 if ($2 ~ /_sysconfdir/)
466                         sysconfdir = $3
467                 if ($2 ~ /_datadir/)
468                         datadir = $3
469                 if ($2 ~ /_includedir/)
470                         includedir = $3
471                 if ($2 ~ /_mandir/)
472                         mandir = $3
473                 if ($2 ~ /_infodir/)
474                         infodir = $3
475         }
476 }
477
478
479 # main()  ;-)
480 {
481         preamble = 1
482         
483         print
484 }
485
486
487 END {
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 > 0) {
508                 print "* %{date} PLD Team <pld-list@pld.org.pl>"
509                 printf "All persons listed below can be reached at "
510                 print "<cvs_login>@pld.org.pl\n"
511                 print "$" "Log:$"
512         }
513 }
514
515 function fixedsub(s1,s2,t,      ind) {
516 # substitutes fixed strings (not regexps)
517         if (ind = index(t,s1))
518                 t = substr(t, 1, ind-1) s2 substr(t, ind+length(s1))
519         return t
520 }
521
522 # There should be one or two tabs after the colon.
523 function format_preamble()
524 {
525         sub(/:[ \t]*/, ":")
526         if (match($0, /[A-Za-z0-9()# \t]+[ \t]*:[ \t]*/) == 1) {
527                 if (RLENGTH < 8)
528                         sub(/:/, ":\t\t")
529                 else
530                         sub(/:/, ":\t")
531         }
532 }
533
534 # Replace directly specified directories with macros
535 function use_macros()
536 {
537         gsub(bindir, "%{_bindir}")
538         gsub("%{_prefix}/bin", "%{_bindir}")
539         gsub("%{prefix}/bin", "%{_bindir}")
540
541         for (c = 1; c <= NF; c++) {
542                 if ($c ~ sbindir "/fix-info-dir")
543                         continue;
544                 gsub(sbindir, "%{_sbindir}", $c)
545         }
546
547         gsub("%{prefix}/sbin", "%{_sbindir}")
548         gsub("%{_prefix}/sbib", "%{_sbindir}")
549
550         gsub(libdir, "%{_libdir}")
551         gsub("%{prefix}/lib", "%{_libdir}")
552         gsub("%{_prefix}/lib", "%{_libdir}")
553
554         for (c = 1; c <= NF; c++) {
555                 if ($c ~ sysconfdir "/cron.d")
556                         continue;
557                 if ($c ~ sysconfdir "/crontab.d")
558                         continue;
559                 if ($c ~ sysconfdir "/logrotate.d")
560                         continue;
561                 if ($c ~ sysconfdir "/pam.d")
562                         continue;
563                 if ($c ~ sysconfdir "/profile.d")
564                         continue;
565                 if ($c ~ sysconfdir "/rc.d")
566                         continue;
567                 if ($c ~ sysconfdir "/security")
568                         continue;
569                 if ($c ~ sysconfdir "/skel")
570                         continue;
571                 if ($c ~ sysconfdir "/sysconfig")
572                         continue;
573                 gsub(sysconfdir, "%{_sysconfdir}", $c)
574         }
575
576         gsub(datadir, "%{_datadir}")
577         gsub("%{prefix}/share", "%{_datadir}")
578         gsub("%{_prefix}/share", "%{_datadir}")
579
580         gsub(includedir, "%{_includedir}")
581         gsub("%{prefix}/include", "%{_includedir}")
582         gsub("%{_prefix}/include", "%{_includedir}")
583
584         gsub(mandir, "%{_mandir}")
585         gsub("%{_datadir}/man", "%{_mandir}")
586         gsub("%{_prefix}/share/man", "%{_mandir}")
587         gsub("%{prefix}/share/man", "%{_mandir}")
588         gsub("%{prefix}/man", "%{_mandir}")
589         gsub("%{_prefix}/man", "%{_mandir}")
590
591         gsub(infodir, "%{_infodir}")
592         gsub("%{prefix}/info", "%{_infodir}")
593         gsub("%{_prefix}/info", "%{_infodir}")
594
595         gsub("%{_datadir}/aclocal", "%{_aclocaldir}")
596
597         if (prefix != "/") {
598                 for (c = 1; c <= NF; c++) {
599                         if ($c ~ prefix "/sbin/fix-info-dir")
600                                 continue;
601                         gsub(prefix, "%{_prefix}", $c)
602                 }
603                 gsub("%{prefix}", "%{_prefix}")
604         }
605
606         gsub("%{PACKAGE_VERSION}", "%{version}")
607         gsub("%{PACKAGE_NAME}", "%{name}")
608
609         gsub("^%{_sbindir}", "%attr(755,root,root) %{_sbindir}")
610         gsub("^%{_bindir}", "%attr(755,root,root) %{_bindir}")
611
612         gsub("%{_datadir}/gnome/apps", "%{_applnkdir}")
613         gsub("%{_datadir}/applnk", "%{_applnkdir}")
614
615         gsub("^make$", "%{__make}")
616         gsub("^make ", "%{__make} ")
617 }
618
619 function fill(ch, n, i) {
620         for (i = 0; i < n; i++)
621                 printf("%c", ch)
622 }
623
624 function format_flush(line, indent, newline, word, first_word) {
625         first_word = 1
626         if (format_indent == -1) 
627                 newline = ""
628         else
629                 newline = fill(" ", format_indent) "- "
630
631         while (match(line, /[^\t ]+/)) {
632                 word = substr(line, RSTART, RLENGTH)
633                 if (length(newline) + length(word) + 1 > tw) {
634                         print newline
635                         
636                         if (format_indent == -1)
637                                 newline = ""
638                         else
639                                 newline = fill(" ", format_indent + 2)
640                         first_word = 1
641                 }
642
643                 if (first_word) {
644                         newline = newline word
645                         first_word = 0
646                 } else
647                         newline = newline " " word
648                         
649                 line = substr(line, RSTART + RLENGTH)
650         }
651         if (newline ~ /[^\t ]/) {
652                 print newline
653         }
654 }
655
656 function cflags(var)
657 {
658         if ($0 == var "=\"$RPM_OPT_FLAGS\"") {
659                 removed[var] = 1
660                 return 0
661         }
662                 
663         if (!/!\?debug/)
664                 sub("\$RPM_OPT_FLAGS", "%{rpmcflags}")
665         return 1
666 }
667
This page took 0.098191 seconds and 3 git commands to generate.