cookutils view modules/compressor @ rev 1067

modules/compressor: compress_gif(): compress files with spaces in the file names (see amsn package)
author Aleksej Bobylev <al.bobylev@gmail.com>
date Wed Jun 06 01:42:12 2018 +0300 (2018-06-06)
parents c69daec36de2
children c1db2b6009d0
line source
1 #!/bin/sh
2 #
3 # compressor - module of the SliTaz Cook
4 # Copyright (C) SliTaz GNU/Linux - GNU GPL v3
5 #
7 . /usr/lib/slitaz/libcook.sh
9 # Compressor cache stuff
11 comp_cache_root='/home/slitaz/cache/cook'
12 mkdir -p "$comp_cache_root"
13 cache_stat=$(mktemp)
15 # Cache notes.
16 # Do not do the same job twice. Getting the file from the cache is much faster
17 # than compressing the file one more time. In addition, this cache is trying not
18 # to take extra space using the hardlinks. Although the files from the cache
19 # without reference to itself should be removed periodically.
24 #
25 # Functions
26 #
29 # tazpkg install command
31 tpi() {
32 tazpkg -gi --quiet --local --cookmode $1
33 }
36 # Working with time (with hundredths precision)
38 get_time() {
39 cut -d" " -f2 /proc/uptime
40 }
42 calc_time() {
43 # L10n: 's' is for seconds, 'm' is for minutes
44 awk -va="$1" -vb="$(get_time)" -vs="$(_ 's')" -vm="$(_ 'm')" '
45 BEGIN{
46 time = b - a;
47 if (time < 30)
48 printf("%.2f%s\n", time, s);
49 else
50 printf("%.2f%s ~ %.0f%s\n", time, s, time / 60, m);
51 }'
52 }
55 # Compressor mini summary
57 comp_summary() {
58 # "$time0" "$size0" "$size1" "$log_file"
59 status
60 [ "$2" -eq 0 ] && return
61 saving=$(awk -va="$2" -vb="$3" 'BEGIN{ printf("%.0f\n", (a - b) / 1024) }')
62 cache_msg=''
63 if [ -s "$cache_stat" ]; then
64 cache_msg=$(_n ' Cache hit: %d/%d.' "$(fgrep '+' $cache_stat | wc -l)" "$(wc -l < $cache_stat)")
65 echo -n > $cache_stat
66 fi
67 _ ' Time: %s. Size: %s B -> %s B. Save: %s KB.%s' \
68 "$(calc_time $1)" "$2" "$3" "$saving" "$cache_msg"
70 if [ -s "$4" ]; then
71 _ 'Compressor warnings and errors:'
72 awk '{printf " %s\n", $0;}' "$4"
73 echo
74 fi
75 # Clean log
76 [ ! -f "$4" ] || rm "$4"
77 }
80 # Find ELF files
82 find_elf() {
83 local i ifs="$IFS"
84 IFS=$'\n'
85 find $fs -type f \
86 | while read i; do
87 # output of `readelf -h <file> is human-readable information,
88 # we are interested in the next line:
89 # Type: EXEC (Executable file)
90 # or
91 # Type: DYN (Shared object file)
92 if [ "$(readelf -h "$i" 2>/dev/null \
93 | sed -n '/Type:/ s|.*: *\([^ ]*\) .*|\1|p')" == "$1" ]; then
94 echo "$i" # $1 = { EXEC | DYN }
95 fi
96 done
97 IFS="$ifs"
98 }
101 # Calculating different sizes
103 sizes() {
104 local ifs="$IFS"; IFS=$'\n'
105 case $1 in
106 man) find $install/usr/share/man -type f -exec ls -l \{\} \; ;;
107 png) find $install -type f -name '*.png' -exec ls -l \{\} \; ;;
108 svg) find $install -type f -name '*.svg' -exec ls -l \{\} \; ;;
109 gif) find $install -type f -name '*.gif' -exec ls -l \{\} \; ;;
110 xml) find $install -type f \( -name '*.ui' -o -name '*.glade' \) -exec ls -l \{\} \; ;;
111 des) find $install -type f -name '*.desktop' -exec ls -l \{\} \; ;;
112 mo1) find $install -type f -name '*.mo' -exec ls -l \{\} \; ;;
113 loc) find $install/usr/share/i18n/locales -type f -exec ls -l \{\} \; ;;
114 mo2) find $fs/usr/share/locale -type f -name '*.mo' -exec ls -l \{\} \; ;;
115 gz) find $install -type f -name '*.gz' ! -path '*/share/man/*' -exec ls -l \{\} \; ;;
116 zip) find $install -type f -name '*.zip' -exec ls -l \{\} \; ;;
117 strip)
118 {
119 find_elf EXEC
120 find_elf DYN
121 find $fs -type f \( -name '*.a' -o -name '*.pyc' -o -name '*.pyo' \
122 -o -name '.packlist' -o -name '*.pm' -o -name '*.pl' -o -name '*.pod' \)
123 } \
124 | tr '\n' '\0' \
125 | xargs -0 ls -l
126 ;;
127 esac | awk '{s+=$5}END{print s}'
128 IFS="$ifs"
129 }
132 # Query cache for already existing compressed file; substitute original file with the cached
133 # compressed one if so.
134 # $1: cache section (gz, mangz, png, etc.); $2: path to original file
136 query_cache() {
137 md5=$(md5sum "$2")
138 cachefile="$comp_cache_root/$1/${md5%% *}"
139 echo "$cachefile"
140 if [ -f "$cachefile" ]; then
141 ln -f "$cachefile" "$2"
142 echo '+' >> "$cache_stat"
143 else
144 echo '-' >> "$cache_stat"
145 false
146 fi
147 }
150 # Store compressed file to the cache
151 # $1: path to cache entry to be stored; $2: path to compressed file to be stored
153 store_cache() {
154 mkdir -p "${1%/*}"
155 mv "$2" "$1"
156 ln "$1" "$2"
157 }
160 # Function to compress all man pages
161 # Compressing can be disabled with COOKOPTS="!manz"
163 compress_manpages() {
164 time0=$(get_time)
165 [ "${COOKOPTS/!manz/}" != "$COOKOPTS" ] && return
166 manpath="$install/usr/share/man"
167 [ -d "$manpath" ] || return
168 size0=$(sizes man); [ -z "$size0" ] && return
170 tpi advancecomp-static
172 action 'Compressing man pages...'
174 # We'll use only Gzip compression, so decompress other formats first
175 find $manpath -type f -name '*.bz2' -exec bunzip2 \{\} \;
176 find $manpath -type f -name '*.xz' -exec unxz \{\} \;
178 # Fast compress with gzip
179 find $manpath -type f ! -name '*.gz' -exec gzip \{\} \;
181 # Fix symlinks
182 for i in $(find $manpath -type l); do
183 dest=$(readlink $i | sed 's|\.[gbx]z2*$||')
184 link=$(echo $i | sed 's|\.[gbx]z2*$||')
185 rm $i; ln -s $dest.gz $link.gz
186 done
188 # Recompress with advdef (it can't compress, only recompress)
189 the_log="$(mktemp)"
190 if which advdef >/dev/null; then
191 IFS=$'\n'
192 for i in $(find $manpath -type f); do
193 if ! cached_path=$(query_cache mangz "$i"); then
194 cp -a "$i" "$i.orig$$" # save the original if something goes wrong
195 out="$(advdef -z4q "$i")"
196 if [ -n "$out" ]; then
197 echo "$i:"$'\n'"$out"$'\n' >> "$the_log"
198 mv -f "$i.orig$$" "$i" # restore the original
199 else
200 store_cache "$cached_path" "$i"
201 rm -f "$i.orig$$" # clean
202 fi
203 fi
204 done
205 else
206 echo 'Warning: advdef not found.' > "$the_log"
207 fi
209 comp_summary "$time0" "$size0" "$(sizes man)" "$the_log"
210 }
213 # Function to recompress all gzip archives
214 # Recompressing can be disabled with COOKOPTS="!gz"
216 recompress_gz() {
217 time0=$(get_time)
218 [ "${COOKOPTS/!gz/}" != "$COOKOPTS" ] && return
219 size0=$(sizes gz); [ -z "$size0" ] && return
221 tpi advancecomp-static
223 action 'Recompressing gzip files...'
225 # Recompress with advdef
226 the_log="$(mktemp)"
227 if which advdef >/dev/null; then
228 IFS=$'\n'
229 for i in $(find $install -type f -name '*.gz' ! -path '*/share/man/*'); do
230 if ! cached_path=$(query_cache gz "$i"); then
231 cp -a "$i" "$i.orig$$" # save the original if something goes wrong
232 out="$(advdef -z4q "$i")"
233 if [ -n "$out" ]; then
234 echo "$i:"$'\n'"$out"$'\n' >> "$the_log"
235 mv -f "$i.orig$$" "$i" # restore the original
236 else
237 store_cache "$cached_path" "$i"
238 rm -f "$i.orig$$" # clean
239 fi
240 fi
241 done
242 else
243 echo 'Warning: advdef not found.' > "$the_log"
244 fi
246 comp_summary "$time0" "$size0" "$(sizes gz)" "$the_log"
247 }
250 # Function to recompress all zip archives
251 # Recompressing can be disabled with COOKOPTS="!zip"
253 recompress_zip() {
254 time0=$(get_time)
255 [ "${COOKOPTS/!zip/}" != "$COOKOPTS" ] && return
256 size0=$(sizes zip); [ -z "$size0" ] && return
258 tpi advancecomp-static
260 action 'Recompressing zip files...'
262 # Recompress with advzip
263 the_log="$(mktemp)"
264 if which advzip >/dev/null; then
265 IFS=$'\n'
266 for i in $(find $install -type f -name '*.zip'); do
267 if ! cached_path=$(query_cache zip "$i"); then
268 cp -a "$i" "$i.orig$$" # save the original if something goes wrong
269 out="$(advzip -z3qk "$i")" # '-4' is more than two orders slower; use '-3'
270 if [ -n "$out" ]; then
271 echo "$i:"$'\n'"$out"$'\n' >> "$the_log"
272 mv -f "$i.orig$$" "$i" # restore the original
273 else
274 store_cache "$cached_path" "$i"
275 rm -f "$i.orig$$" # clean
276 fi
277 fi
278 done
279 else
280 echo 'Warning: advzip not found.' > "$the_log"
281 fi
283 comp_summary "$time0" "$size0" "$(sizes zip)" "$the_log"
284 }
287 # Function used after compile_rules() to compress all png images
288 # Compressing can be disabled with COOKOPTS="!pngz"
290 compress_png() {
291 time0=$(get_time)
292 [ "${COOKOPTS/!pngz/}" != "$COOKOPTS" ] && return
293 size0=$(sizes png); [ -z "$size0" ] && return
295 use_pq=true
296 use_op=true
297 [ "${COOKOPTS/!pngquant/}" != "$COOKOPTS" ] && use_pq=false
298 [ "${COOKOPTS/!optipng/}" != "$COOKOPTS" ] && use_op=false
299 $use_pq && tpi pngquant-static
300 $use_op && tpi optipng-static
302 action 'Compressing png images...'
304 the_log="$(mktemp)"
305 $use_pq && if ! which pngquant >/dev/null; then
306 echo 'Warning: pngquant not found.' > "$the_log"
307 use_pq=false
308 fi
309 $use_op && if ! which optipng >/dev/null; then
310 echo 'Warning: optipng not found.' >> "$the_log"
311 use_op=false
312 fi
314 oplevel=$(echo $COOKOPTS | grep 'op[0-8]' | sed 's|.*op\([0-8]\).*|\1|')
315 [ -z "$oplevel" ] && oplevel='2'
317 cache_section="png$oplevel"
318 $use_pq && cache_section="${cache_section}p"
319 $use_op && cache_section="${cache_section}o"
321 [ "$oplevel" == '8' ] && oplevel='7 -zm1-9'
323 pq_opt='--skip-if-larger' # Sublime Text is mad about `if` in $(), so put it separately
324 IFS=$'\n'
325 for i in $(find $install -type f -name '*.png'); do
326 unset IFS iserror
327 if ! cached_path=$(query_cache $cache_section "$i"); then
328 cp -a "$i" "$i.orig$$" # save the original if something goes wrong
329 if $use_pq; then
330 out="$(pngquant -f $pq_opt --ext .png --speed 1 "$i" 2>&1)"
331 if [ -n "$out" ]; then
332 echo "$i (pngquant):"$'\n'"$out"$'\n' >> "$the_log"
333 iserror='yes'
334 [ -e "$i.tmp" ] && rm "$i.tmp" # zero-size file remains on pngquant fail
335 fi
336 fi
337 if $use_op && [ -z "$iserror" ]; then
338 out="$(optipng -quiet -strip all -o$oplevel "$i" 2>&1)"
339 if [ -n "$out" ]; then
340 echo "$i (optipng):"$'\n'"$out"$'\n' >> "$the_log"
341 iserror='yes'
342 fi
343 fi
344 if [ -n "$iserror" ]; then
345 mv -f "$i.orig$$" "$i" # restore the original
346 else
347 store_cache "$cached_path" "$i"
348 rm -f "$i.orig$$" # clean
349 fi
350 fi
351 done
353 comp_summary "$time0" "$size0" "$(sizes png)" "$the_log"
354 }
357 # Function used after compile_rules() to compress all svg images
358 # Compressing can be disabled with COOKOPTS="!svgz"
360 compress_svg() {
361 time0=$(get_time)
362 [ "${COOKOPTS/!svgz/}" != "$COOKOPTS" ] && return
363 size0=$(sizes svg); [ -z "$size0" ] && return
365 tpi svgcleaner
367 action 'Compressing svg images...'
369 if which svgcleaner >/dev/null; then
370 [ "${COOKOPTS/!svgextra/}" == "$COOKOPTS" ] &&
371 opts="--apply-transform-to-paths yes --coordinates-precision 1 --paths-coordinates-precision 1"
373 the_log="$(mktemp)"
374 for i in $(IFS=$'\n' find $install -type f -name '*.svg'); do
375 out="$(unset IFS; svgcleaner "$i" "$i" --copy-on-error --quiet \
376 --multipass --remove-unresolved-classes no $opts 2>&1)"
377 [ -z "$out" ] || echo "$i:"$'\n'"$out"$'\n' >> "$the_log"
378 done
379 else
380 echo 'Warning: svgcleaner not found.' > "$the_log"
381 fi
383 comp_summary "$time0" "$size0" "$(sizes svg)" "$the_log"
384 }
387 # Function used after compile_rules() to compress all gif images
388 # Compressing can be disabled with COOKOPTS="!gifz"
390 compress_gif() {
391 time0=$(get_time)
392 [ "${COOKOPTS/!gifz/}" != "$COOKOPTS" ] && return
393 size0=$(sizes gif); [ -z "$size0" ] && return
395 tpi gifsicle
397 action 'Compressing gif images...'
399 if which gifsicle >/dev/null; then
400 the_log="$(mktemp)"
401 IFS=$'\n'
402 for i in $(find $install -type f -name '*.gif'); do
403 unset IFS
404 # use intermediate file, if all well ($?=0), then substitute the original
405 if gifsicle -O3 "$i" -o "$i.$$" >> "$the_log" 2>&1; then
406 mv "$i.$$" "$i"
407 else
408 rm "$i.$$"
409 fi
410 done
411 else
412 echo 'Warning: gifsicle not found.' > "$the_log"
413 fi
415 comp_summary "$time0" "$size0" "$(sizes gif)" "$the_log"
416 }
419 # Function used after compile_rules() to shrink all *.ui and *.glade files:
420 # remove insignificant spaces and comments
421 # Compressing can be disabled with COOKOPTS="!uiz"
423 compress_ui() {
424 [ "${COOKOPTS/!uiz/}" != "$COOKOPTS" ] && return
425 [ -z "$(find $install -type f \( -name '*.ui' -o -name '*.glade' \) )" ] && return
427 tpi xmlstarlet
429 action 'Compressing ui files...'
431 if which xmlstarlet >/dev/null; then
432 size0=$(sizes xml)
433 time0=$(get_time)
434 temp_ui="$(mktemp)"
435 the_log="$(mktemp)"
436 IFS=$'\n'
437 for ui in $(find $install -type f \( -name '*.ui' -o -name '*.glade' \) ); do
438 out="$(xmlstarlet c14n --without-comments "$ui" | xmlstarlet sel -B -t -c '*' > "$temp_ui")"
439 if [ -n "$out" ]; then
440 echo "$ui:"$'\n'"$out"$'\n' >> "$the_log"
441 else
442 cat "$temp_ui" > "$ui"
443 fi
444 done
445 else
446 echo 'Warning: xmlstarlet not found.' > "$the_log"
447 fi
449 comp_summary "$time0" "$size0" "$(sizes xml)" "$the_log"
450 rm "$temp_ui"
451 }
454 # Get list of supported locales...
456 get_supported_locales() {
457 lpc='/slitaz-i18n/stuff/locale-pack.conf'
458 if [ -e "$WOK$lpc" ]; then
459 # ... from package in the local wok
460 . "$WOK$lpc"
461 else
462 # ... from Hg
463 temp_conf=$(mktemp)
464 wget -q -O $temp_conf -T 10 "http://hg.slitaz.org/wok/raw-file/tip$lpc"
465 if [ -s $temp_conf ]; then
466 . $temp_conf
467 else
468 # Give up and use hardcoded list
469 LOCALE_PACK="ar ca cs da de el en es fi fr hr hu id is it ja nb nl nn pl pt \
470 pt_BR ro ru sl sv tr uk zh_CN zh_TW"
471 fi
472 rm $temp_conf
473 fi
474 echo $LOCALE_PACK
475 }
478 # Fix common errors and warnings in the .desktop files
479 # Fixing can be disabled with COOKOPTS="!fixdesktops"
481 fix_desktop_files() {
482 [ "${COOKOPTS/!fixdesktops/}" != "$COOKOPTS" ] && return
483 deskpath="$install/usr/share/applications"
484 [ -d "$deskpath" ] || return
485 [ -z "$(find $deskpath -type f -name '*.desktop')" ] && return
487 size0=$(sizes des)
488 time0=$(get_time)
490 if [ -n "$QA" -a -z "$(which desktop-file-validate)" ]; then
491 tpi desktop-file-validate-static
492 fi
494 # The variable $LOCALE is set in cook.conf and may be overridden in the receipt.
495 # Default value is "" (empty). That means for us that we'll use the full
496 # list of supported locales here.
497 [ -z "$LOCALE" ] && LOCALE=$(get_supported_locales)
499 IFS=$'\n'
500 for desktop in $(find $deskpath -type f -name '*.desktop'); do
501 cp "$desktop" "$desktop.orig"
503 # Sort out .desktop file (is prerequisite to correct working of `fix-desktop-file`)
504 sdft "$desktop" -i
506 # Fix common errors in .desktop file
507 fix-desktop-file "$desktop"
509 # Strip unsupported locales from .desktop file
510 [ "${COOKOPTS/!i18nz/}" == "$COOKOPTS" ] &&
511 sdft "$desktop" -i -k "$LOCALE"
513 # Extra-strip
514 [ "${COOKOPTS/!extradesktops/}" == "$COOKOPTS" ] &&
515 sdft "$desktop" -i -g -x -tf -r 'Keywords*' -o
517 if [ -n "$QA" ]; then
518 # Check the rest of errors, warnings and tips
519 _ 'QA: Checking %s...' "$(basename $desktop)"
520 busybox diff "$desktop.orig" "$desktop" | sed 's!^!|!'
521 if which desktop-file-validate >/dev/null; then
522 desktop-file-validate "$desktop" | busybox fold -s
523 else
524 echo 'Warning: desktop-file-validate not found.'
525 fi
526 echo
527 fi
529 rm "$desktop.orig"
530 done
532 comp_summary "$time0" "$size0" "$(sizes des)" '/dev/null'
533 }
536 # Normalize all *.mo files: unconditionally convert to UTF-8; remove strings that are not really necessary
537 # to the translation (msgid = msgstr)
538 # Normalization can be disabled with COOKOPTS="!monorm"
540 normalize_mo() {
541 [ "${COOKOPTS/!monorm/}" != "$COOKOPTS" ] && return
542 [ -z "$(find $install -type f -name '*.mo')" ] && return
544 # Gettext functions: msgunfmt, msguniq, msgconv, msgfmt
545 tpi gettext
546 # Gconv modules (convert to UTF-8)
547 tpi glibc-locale
549 action 'Normalizing mo files...'
551 the_log="$(mktemp)"
552 to_continue=true
553 for i in msgunfmt msguniq msgconv msgfmt; do
554 if ! which $i >/dev/null; then
555 echo "Warning: $i not found. Normalizing aborted" > "$the_log"
556 to_continue=false
557 fi
558 done
560 size0=$(sizes mo1)
561 time0=$(get_time)
563 # Process all existing *.mo files
564 IFS=$'\n'
565 $to_continue &&
566 for mo in $(find "$install" -type f -name '*.mo'); do
567 tmpfile="$(mktemp)"
569 # put ANY errors of {msgunfmt,msguniq,msgconv} to $out. FIXME?
570 out="$({ msgunfmt "$mo" | msguniq | msgconv -o "$tmpfile" -t 'UTF-8'; } 2>&1)"
571 if [ -n "$out" ]; then
572 # using literal $'\n' here instead of using `echo -e "...\n..."` because
573 # $out may contain escapes ('\r', '\v') that we should print as-is
574 echo "$mo:"$'\n'"$out"$'\n' >> "$the_log"
575 continue # proceed to next file
576 fi
578 # add newline
579 echo >> "$tmpfile"
581 # get Plural-Forms
582 awk '
583 BEGIN { skip = ""; }
584 {
585 if (! skip) {
586 s = $0;
587 gsub(/^[^\"]*\"/, "", s);
588 gsub(/\"$/, "", s);
589 printf("%s", s);
590 }
591 if (! $0) skip = "yes";
592 }
593 ' "$tmpfile" | sed 's|\\n|\n|g' | grep "^Plural-Forms:" > "$tmpfile.pf"
595 if ! grep -q 'msgid_plural' "$tmpfile"; then
596 echo > "$tmpfile.pf"
597 fi
599 # main
600 awk -v pf="$(cat "$tmpfile.pf")" '
601 function clean() {
602 mode = msgctxt = msgid = msgid_plural = msgstr = msgstr0 = msgstr1 = msgstr2 = msgstr3 = msgstr4 = msgstr5 = "";
603 }
605 function getstring() {
606 # Skip unquoted words at the beginning (msgid, msgstr...) and get string from inside quotes
607 s = $0;
608 gsub(/^[^\"]*\"/, "", s);
609 gsub(/\"$/, "", s);
610 return s;
611 }
613 BEGIN {
614 printf("msgid \"\"\nmsgstr \"\"\n\"Content-Type: text/plain; charset=UTF-8\\n\"\n");
615 if (pf)
616 printf("\"%s\\n\"\n", pf);
617 printf("\n");
618 skip = 1;
619 clean();
620 }
622 {
623 # Skip the entire header
624 if (!skip) {
625 if ($1 == "msgctxt" || $1 == "msgid" || $1 == "msgstr" || $1 == "msgid_plural")
626 mode = $1;
627 if ($1 == "msgstr[0]") mode = "msgstr0";
628 if ($1 == "msgstr[1]") mode = "msgstr1";
629 if ($1 == "msgstr[2]") mode = "msgstr2";
630 if ($1 == "msgstr[3]") mode = "msgstr3";
631 if ($1 == "msgstr[4]") mode = "msgstr4";
632 if ($1 == "msgstr[5]") mode = "msgstr5";
634 if (mode == "msgctxt") msgctxt = msgctxt getstring();
635 if (mode == "msgid") msgid = msgid getstring();
636 if (mode == "msgstr") msgstr = msgstr getstring();
637 if (mode == "msgid_plural") msgid_plural = msgid_plural getstring();
638 if (mode == "msgstr0") msgstr0 = msgstr0 getstring();
639 if (mode == "msgstr1") msgstr1 = msgstr1 getstring();
640 if (mode == "msgstr2") msgstr2 = msgstr2 getstring();
641 if (mode == "msgstr3") msgstr3 = msgstr3 getstring();
642 if (mode == "msgstr4") msgstr4 = msgstr4 getstring();
643 if (mode == "msgstr5") msgstr5 = msgstr5 getstring();
645 if (! $0) {
646 if (msgid != msgstr) {
647 if (msgctxt) printf("msgctxt \"%s\"\n", msgctxt);
648 printf("msgid \"%s\"\n", msgid);
649 if (msgid_plural) printf("msgid_plural \"%s\"\n", msgid_plural);
650 if (msgstr) printf("msgstr \"%s\"\n", msgstr);
651 if (msgstr0) printf("msgstr[0] \"%s\"\n", msgstr0);
652 if (msgstr1) printf("msgstr[1] \"%s\"\n", msgstr1);
653 if (msgstr2) printf("msgstr[2] \"%s\"\n", msgstr2);
654 if (msgstr3) printf("msgstr[3] \"%s\"\n", msgstr3);
655 if (msgstr4) printf("msgstr[4] \"%s\"\n", msgstr4);
656 if (msgstr5) printf("msgstr[5] \"%s\"\n", msgstr5);
657 printf("\n");
658 }
659 clean();
660 }
661 }
662 if ($0 == "") skip = "";
663 }
664 ' "$tmpfile" > "$tmpfile.awk"
666 out="$(msgfmt "$tmpfile.awk" -o "$tmpfile.mo" 2>&1)"
667 if [ -n "$out" ]; then
668 echo "$mo (msgfmt):"$'\n'"$out"$'\n' >> "$the_log"
669 continue # proceed to next file
670 fi
672 if [ -s "$tmpfile.mo" ]; then
673 rm "$mo"; mv "$tmpfile.mo" "$mo"
674 else
675 _ 'Error processing %s' "$mo" >> "$the_log"
676 echo >> "$the_log"
677 [ -e "$tmpfile.mo" ] && rm "$tmpfile.mo"
678 fi
680 # Clean
681 rm "$tmpfile" "$tmpfile.pf" "$tmpfile.awk"
682 done
684 comp_summary "$time0" "$size0" "$(sizes mo1)" "$the_log"
685 }
688 # Strip locale definitions: normalize whitespace and remove comments
689 # Stripping can be disabled with COOKOPTS="!locdef"
691 strip_locale_def() {
692 [ "${COOKOPTS/!locdef/}" != "$COOKOPTS" ] && return
693 [ ! -d "$install/usr/share/i18n/locales" ] && return
694 [ -z "$(find $install/usr/share/i18n/locales -type f)" ] && return
696 action 'Stripping locale definitions...'
697 size0=$(sizes loc)
698 time0=$(get_time)
700 for i in $(find $install/usr/share/i18n/locales -type f); do
701 sed -i 's| | |g; s| *| |g; s|^ ||; /^%/d' $i
702 done
704 comp_summary "$time0" "$size0" "$(sizes loc)"
705 }
708 # Find and strip: --strip-all (-s) or --strip-debug on static libs as well
709 # as removing unneeded files like in Python packages. Cross compiled binaries
710 # must be stripped with cross-tools aka $ARCH-slitaz-*-strip
711 # Stripping can be disabled with COOKOPTS="!strip"
713 strip_package() {
714 [ "${COOKOPTS/!strip/}" != "$COOKOPTS" ] && return
716 local i ifs="$IFS"
717 IFS=$'\n'
719 case "$ARCH" in
720 arm*|x86_64) export STRIP="$HOST_SYSTEM-strip" ;;
721 *) export STRIP='strip' ;;
722 esac
723 action 'Executing strip on all files...'
724 size0=0
725 size1=0
726 time0=$(get_time)
727 oldsize=$(sizes strip)
730 # GNU strip (GNU Binutils)
731 # -p --preserve-dates Copy modified/access timestamps to the output
732 # -s --strip-all Remove all symbols and relocation information
733 # --strip-unneeded Remove all symbols not needed by relocations
734 # -D --enable-deterministic-archives Produce deterministic output when stripping archives
735 # -g -S -d --strip-debug Remove all debugging symbols & sections
737 # Strip executable files
738 while read i; do
739 $STRIP -ps "$i" 2>/dev/null
740 done <<EOT
741 $(find_elf EXEC)
742 EOT
744 # Strip shared libraries
745 while read i; do
746 case $i in
747 *.dbg) ;; # skip library.so.*.dbg debugging symbols
748 *) $STRIP -p --strip-unneeded "$i" 2>/dev/null;;
749 esac
750 done <<EOT
751 $(find_elf DYN)
752 EOT
754 # Strip static libraries
755 # See also: https://wiki.debian.org/ReproducibleBuilds/TimestampsInStaticLibraries
756 find $fs -name '*.a' -exec $STRIP -pdD '{}' 2>/dev/null \;
759 # Remove Python *.pyc and *.pyo
760 find $fs -type f \( -name '*.pyc' -o -name '*.pyo' \) -delete 2>/dev/null
762 # Remove both with the empty subfolders:
763 # 1. Perl perllocal.pod and .packlist (unconditionally)
764 local perlfiles="$(find $fs -type f \( -name 'perllocal.pod' -o -name '.packlist' \))"
765 # 2. Perl *.pod (if not disabled)
766 [ "${COOKOPTS/!rmpod/}" == "$COOKOPTS" ] &&
767 perlfiles="$perlfiles $(find $fs -type f -name '*.pod')"
768 echo "$perlfiles" | xargs rm -f 2>/dev/null
769 echo "$perlfiles" | awk 'BEGIN{FS=OFS="/"}{$NF="";print}' | xargs rmdir -p 2>/dev/null
771 # Strip documentation inside Perl files (*.pm and *.pl) (if not disabled)
772 [ "${COOKOPTS/!perlz/}" == "$COOKOPTS" ] &&
773 find $fs -type f \( -name '*.pm' -o -name '*.pl' \) -exec sed -i '/^=/,/^=cut/d' '{}' \;
775 newsize=$(sizes strip)
777 comp_summary "$time0" "$oldsize" "$newsize"
778 IFS="$ifs"
779 }
782 # Strip unsupported locales (.mo files)
784 strip_mo_i18n() {
785 [ "${COOKOPTS/!i18nz/}" != "$COOKOPTS" ] && return
787 [ ! -d "$fs/usr/share/locale" ] && return
788 [ -z "$(find $fs/usr/share/locale -type f -name '*.mo')" ] && return
790 action 'Thin out translation files...'
791 size0=$(sizes mo2)
792 time0=$(get_time)
794 # The variable $LOCALE is set in cook.conf and may be overridden in the receipt.
795 # Default value is "" (empty). That means for us that we'll use the full
796 # list of supported locales here.
797 [ -z "$LOCALE" ] && LOCALE=$(get_supported_locales)
799 # Existing locales
800 elocales=" $(ls -1 "$fs/usr/share/locale" | tr '\n' ' ') "
802 # Thin out the list of existing locales. At the end there will be only locales that need
803 # deleting (and the garbage like '_AU', _US', '_BR' leaving from 'en_AU', 'en_US', 'pt_BR'...)
804 for keep_locale in $LOCALE; do
805 elocales=${elocales//$keep_locale}
806 done
808 # Remove the unsupported locales
809 for rem_locale in $elocales; do
810 [ ! -d "$fs/usr/share/locale/$rem_locale" ] ||
811 rm -r "$fs/usr/share/locale/$rem_locale"
812 done
814 comp_summary "$time0" "$size0" "$(sizes mo2)"
815 }
820 case $1 in
821 install)
822 # Compressors working in the $install
823 case "$ARCH" in
824 arm*) ;;
825 *)
826 recompress_gz
827 recompress_zip
828 compress_manpages
829 compress_png
830 compress_svg
831 compress_gif
832 compress_ui
833 ;;
834 esac
836 fix_desktop_files
837 normalize_mo
838 strip_locale_def
839 ;;
840 fs)
841 # Compressors working in the $fs
842 strip_package
843 strip_mo_i18n
844 ;;
845 esac
847 # Clean
848 rm "$cache_stat"
849 find $comp_cache_root -type f -links -2 -delete