cookutils view modules/compressor @ rev 1100

modules/compressor: fix ownership & permissions for cached files
author Aleksej Bobylev <al.bobylev@gmail.com>
date Fri Nov 30 01:19:41 2018 +0200 (2018-11-30)
parents 1463d32f6f90
children 8e94062e3045
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 owner=$(stat -c%u:%g "$2")
141 perms=$(stat -c%a "$2")
142 if [ -f "$cachefile" ]; then
143 ln -f "$cachefile" "$2"
144 chown $owner "$2"
145 chmod $perms "$2"
146 echo '+' >> "$cache_stat"
147 else
148 echo '-' >> "$cache_stat"
149 false
150 fi
151 }
154 # Store compressed file to the cache
155 # $1: path to cache entry to be stored; $2: path to compressed file to be stored
157 store_cache() {
158 mkdir -p "${1%/*}"
159 mv "$2" "$1"
160 ln "$1" "$2"
161 }
164 # Function to compress all man pages
165 # Compressing can be disabled with COOKOPTS="!manz"
167 compress_manpages() {
168 time0=$(get_time)
169 [ "${COOKOPTS/!manz/}" != "$COOKOPTS" ] && return
170 manpath="$install/usr/share/man"
171 [ -d "$manpath" ] || return
172 size0=$(sizes man); [ -z "$size0" ] && return
174 tpi advancecomp-static
176 action 'Compressing man pages...'
178 # We'll use only Gzip compression, so decompress other formats first
179 find $manpath -type f -name '*.bz2' -exec bunzip2 \{\} \;
180 find $manpath -type f -name '*.xz' -exec unxz \{\} \;
182 # Fast compress with gzip
183 find $manpath -type f ! -name '*.gz' -exec gzip \{\} \;
185 # Fix symlinks
186 for i in $(find $manpath -type l); do
187 dest=$(readlink $i | sed 's|\.[gbx]z2*$||')
188 link=$(echo $i | sed 's|\.[gbx]z2*$||')
189 rm $i; ln -s $dest.gz $link.gz
190 done
192 # Recompress with advdef (it can't compress, only recompress)
193 the_log="$(mktemp)"
194 if which advdef >/dev/null; then
195 IFS=$'\n'
196 for i in $(find $manpath -type f); do
197 if ! cached_path=$(query_cache mangz "$i"); then
198 cp -a "$i" "$i.orig$$" # save the original if something goes wrong
199 out="$(advdef -z4q "$i")"
200 if [ -n "$out" ]; then
201 echo "$i:"$'\n'"$out"$'\n' >> "$the_log"
202 mv -f "$i.orig$$" "$i" # restore the original
203 else
204 store_cache "$cached_path" "$i"
205 rm -f "$i.orig$$" # clean
206 fi
207 fi
208 done
209 else
210 echo 'Warning: advdef not found.' > "$the_log"
211 fi
213 comp_summary "$time0" "$size0" "$(sizes man)" "$the_log"
214 }
217 # Function to recompress all gzip archives
218 # Recompressing can be disabled with COOKOPTS="!gz"
220 recompress_gz() {
221 time0=$(get_time)
222 [ "${COOKOPTS/!gz/}" != "$COOKOPTS" ] && return
223 size0=$(sizes gz); [ -z "$size0" ] && return
225 tpi advancecomp-static
227 action 'Recompressing gzip files...'
229 # Recompress with advdef
230 the_log="$(mktemp)"
231 if which advdef >/dev/null; then
232 IFS=$'\n'
233 for i in $(find $install -type f -name '*.gz' ! -path '*/share/man/*'); do
234 if ! cached_path=$(query_cache gz "$i"); then
235 cp -a "$i" "$i.orig$$" # save the original if something goes wrong
236 out="$(advdef -z4q "$i")"
237 if [ -n "$out" ]; then
238 echo "$i:"$'\n'"$out"$'\n' >> "$the_log"
239 mv -f "$i.orig$$" "$i" # restore the original
240 else
241 store_cache "$cached_path" "$i"
242 rm -f "$i.orig$$" # clean
243 fi
244 fi
245 done
246 else
247 echo 'Warning: advdef not found.' > "$the_log"
248 fi
250 comp_summary "$time0" "$size0" "$(sizes gz)" "$the_log"
251 }
254 # Function to recompress all zip archives
255 # Recompressing can be disabled with COOKOPTS="!zip"
257 recompress_zip() {
258 time0=$(get_time)
259 [ "${COOKOPTS/!zip/}" != "$COOKOPTS" ] && return
260 size0=$(sizes zip); [ -z "$size0" ] && return
262 tpi advancecomp-static
264 action 'Recompressing zip files...'
266 # Recompress with advzip
267 the_log="$(mktemp)"
268 if which advzip >/dev/null; then
269 IFS=$'\n'
270 for i in $(find $install -type f -name '*.zip'); do
271 if ! cached_path=$(query_cache zip "$i"); then
272 cp -a "$i" "$i.orig$$" # save the original if something goes wrong
273 out="$(advzip -z3qk "$i")" # '-4' is more than two orders slower; use '-3'
274 if [ -n "$out" ]; then
275 echo "$i:"$'\n'"$out"$'\n' >> "$the_log"
276 mv -f "$i.orig$$" "$i" # restore the original
277 else
278 store_cache "$cached_path" "$i"
279 rm -f "$i.orig$$" # clean
280 fi
281 fi
282 done
283 else
284 echo 'Warning: advzip not found.' > "$the_log"
285 fi
287 comp_summary "$time0" "$size0" "$(sizes zip)" "$the_log"
288 }
291 # Function used after compile_rules() to compress all png images
292 # Compressing can be disabled with COOKOPTS="!pngz"
294 compress_png() {
295 time0=$(get_time)
296 [ "${COOKOPTS/!pngz/}" != "$COOKOPTS" ] && return
297 size0=$(sizes png); [ -z "$size0" ] && return
299 use_pq=true
300 use_op=true
301 [ "${COOKOPTS/!pngquant/}" != "$COOKOPTS" ] && use_pq=false
302 [ "${COOKOPTS/!optipng/}" != "$COOKOPTS" ] && use_op=false
303 $use_pq && tpi pngquant-static
304 $use_op && tpi optipng-static
306 action 'Compressing png images...'
308 the_log="$(mktemp)"
309 $use_pq && if ! which pngquant >/dev/null; then
310 echo 'Warning: pngquant not found.' > "$the_log"
311 use_pq=false
312 fi
313 $use_op && if ! which optipng >/dev/null; then
314 echo 'Warning: optipng not found.' >> "$the_log"
315 use_op=false
316 fi
318 oplevel=$(echo $COOKOPTS | grep 'op[0-8]' | sed 's|.*op\([0-8]\).*|\1|')
319 [ -z "$oplevel" ] && oplevel='2'
321 cache_section="png$oplevel"
322 $use_pq && cache_section="${cache_section}p"
323 $use_op && cache_section="${cache_section}o"
325 [ "$oplevel" == '8' ] && oplevel='7 -zm1-9'
327 pq_opt='--skip-if-larger' # Sublime Text is mad about `if` in $(), so put it separately
328 IFS=$'\n'
329 for i in $(find $install -type f -name '*.png'); do
330 unset IFS iserror
331 if ! cached_path=$(query_cache $cache_section "$i"); then
332 cp -a "$i" "$i.orig$$" # save the original if something goes wrong
333 if $use_pq; then
334 out="$(pngquant -f $pq_opt --ext .png --speed 1 "$i" 2>&1)"
335 if [ -n "$out" ]; then
336 echo "$i (pngquant):"$'\n'"$out"$'\n' >> "$the_log"
337 iserror='yes'
338 [ -e "$i.tmp" ] && rm "$i.tmp" # zero-size file remains on pngquant fail
339 fi
340 fi
341 if $use_op && [ -z "$iserror" ]; then
342 out="$(optipng -quiet -strip all -o$oplevel "$i" 2>&1)"
343 if [ -n "$out" ]; then
344 echo "$i (optipng):"$'\n'"$out"$'\n' >> "$the_log"
345 iserror='yes'
346 fi
347 fi
348 if [ -n "$iserror" ]; then
349 mv -f "$i.orig$$" "$i" # restore the original
350 else
351 store_cache "$cached_path" "$i"
352 rm -f "$i.orig$$" # clean
353 fi
354 fi
355 done
357 comp_summary "$time0" "$size0" "$(sizes png)" "$the_log"
358 }
361 # Function used after compile_rules() to compress all svg images
362 # Compressing can be disabled with COOKOPTS="!svgz"
364 compress_svg() {
365 time0=$(get_time)
366 [ "${COOKOPTS/!svgz/}" != "$COOKOPTS" ] && return
367 size0=$(sizes svg); [ -z "$size0" ] && return
369 tpi svgcleaner
371 action 'Compressing svg images...'
373 the_log="$(mktemp)"
374 if which svgcleaner >/dev/null; then
375 [ "${COOKOPTS/!svgextra/}" == "$COOKOPTS" ] &&
376 opts="--apply-transform-to-paths yes --coordinates-precision 1 --paths-coordinates-precision 1"
378 for i in $(IFS=$'\n' find $install -type f -name '*.svg'); do
379 out="$(unset IFS; svgcleaner "$i" "$i" --copy-on-error --quiet \
380 --multipass --remove-unresolved-classes no $opts 2>&1)"
381 [ -z "$out" ] || echo "$i:"$'\n'"$out"$'\n' >> "$the_log"
382 done
383 else
384 echo 'Warning: svgcleaner not found.' > "$the_log"
385 fi
387 comp_summary "$time0" "$size0" "$(sizes svg)" "$the_log"
388 }
391 # Function used after compile_rules() to compress all gif images
392 # Compressing can be disabled with COOKOPTS="!gifz"
394 compress_gif() {
395 time0=$(get_time)
396 [ "${COOKOPTS/!gifz/}" != "$COOKOPTS" ] && return
397 size0=$(sizes gif); [ -z "$size0" ] && return
399 tpi gifsicle
401 action 'Compressing gif images...'
403 the_log="$(mktemp)"
404 if which gifsicle >/dev/null; then
405 IFS=$'\n'
406 for i in $(find $install -type f -name '*.gif'); do
407 if ! cached_path=$(query_cache gif "$i"); then
408 unset IFS
409 # use intermediate file, if all well ($?=0), then substitute the original
410 if gifsicle -O3 "$i" -o "$i.$$" >> "$the_log" 2>&1; then
411 if [ -s "$i.$$" ]; then
412 mv "$i.$$" "$i"
413 store_cache "$cached_path" "$i"
414 fi
415 else
416 rm "$i.$$"
417 fi
418 fi
419 done
420 else
421 echo 'Warning: gifsicle not found.' > "$the_log"
422 fi
424 comp_summary "$time0" "$size0" "$(sizes gif)" "$the_log"
425 }
428 # Function used after compile_rules() to shrink all *.ui and *.glade files:
429 # remove insignificant spaces and comments
430 # Compressing can be disabled with COOKOPTS="!uiz"
432 compress_ui() {
433 [ "${COOKOPTS/!uiz/}" != "$COOKOPTS" ] && return
434 [ -z "$(find $install -type f \( -name '*.ui' -o -name '*.glade' \) )" ] && return
436 tpi xmlstarlet
438 action 'Compressing ui files...'
440 the_log="$(mktemp)"
441 if which xmlstarlet >/dev/null; then
442 size0=$(sizes xml)
443 time0=$(get_time)
444 temp_ui="$(mktemp)"
445 IFS=$'\n'
446 for ui in $(find $install -type f \( -name '*.ui' -o -name '*.glade' \) ); do
447 out="$(xmlstarlet c14n --without-comments "$ui" | xmlstarlet sel -B -t -c '*' > "$temp_ui")"
448 if [ -n "$out" ]; then
449 echo "$ui:"$'\n'"$out"$'\n' >> "$the_log"
450 else
451 cat "$temp_ui" > "$ui"
452 fi
453 done
454 else
455 echo 'Warning: xmlstarlet not found.' > "$the_log"
456 fi
458 comp_summary "$time0" "$size0" "$(sizes xml)" "$the_log"
459 rm "$temp_ui"
460 }
463 # Get list of supported locales...
465 get_supported_locales() {
466 lpc='/slitaz-i18n/stuff/locale-pack.conf'
467 if [ -e "$WOK$lpc" ]; then
468 # ... from package in the local wok
469 . "$WOK$lpc"
470 else
471 # ... from Hg
472 temp_conf=$(mktemp)
473 wget -q -O $temp_conf -T 10 "http://hg.slitaz.org/wok/raw-file/tip$lpc"
474 if [ -s $temp_conf ]; then
475 . $temp_conf
476 else
477 # Give up and use hardcoded list
478 LOCALE_PACK="ar ca cs da de el en es fi fr hr hu id is it ja nb nl nn pl pt \
479 pt_BR ro ru sl sv tr uk zh_CN zh_TW"
480 fi
481 rm $temp_conf
482 fi
483 echo $LOCALE_PACK
484 }
487 # Fix common errors and warnings in the .desktop files
488 # Fixing can be disabled with COOKOPTS="!fixdesktops"
490 fix_desktop_files() {
491 [ "${COOKOPTS/!fixdesktops/}" != "$COOKOPTS" ] && return
492 deskpath="$install/usr/share/applications"
493 [ -d "$deskpath" ] || return
494 [ -z "$(find $deskpath -type f -name '*.desktop')" ] && return
496 size0=$(sizes des)
497 time0=$(get_time)
499 if [ -n "$QA" -a -z "$(which desktop-file-validate)" ]; then
500 tpi desktop-file-validate-static
501 fi
503 # The variable $LOCALE is set in cook.conf and may be overridden in the receipt.
504 # Default value is "" (empty). That means for us that we'll use the full
505 # list of supported locales here.
506 [ -z "$LOCALE" ] && LOCALE=$(get_supported_locales)
508 IFS=$'\n'
509 for desktop in $(find $deskpath -type f -name '*.desktop'); do
510 cp "$desktop" "$desktop.orig"
512 # Sort out .desktop file (is prerequisite to correct working of `fix-desktop-file`)
513 sdft "$desktop" -i
515 # Fix common errors in .desktop file
516 fix-desktop-file "$desktop"
518 # Strip unsupported locales from .desktop file
519 [ "${COOKOPTS/!i18nz/}" == "$COOKOPTS" ] &&
520 sdft "$desktop" -i -k "$LOCALE"
522 # Extra-strip
523 [ "${COOKOPTS/!extradesktops/}" == "$COOKOPTS" ] &&
524 sdft "$desktop" -i -g -x -tf -r 'Keywords*' -o
526 if [ -n "$QA" ]; then
527 # Check the rest of errors, warnings and tips
528 _ 'QA: Checking %s...' "$(basename $desktop)"
529 busybox diff "$desktop.orig" "$desktop" | sed 's!^!|!'
530 if which desktop-file-validate >/dev/null; then
531 desktop-file-validate "$desktop" | busybox fold -s
532 else
533 echo 'Warning: desktop-file-validate not found.'
534 fi
535 echo
536 fi
538 rm "$desktop.orig"
539 done
541 comp_summary "$time0" "$size0" "$(sizes des)" '/dev/null'
542 }
545 # Normalize all *.mo files: unconditionally convert to UTF-8; remove strings that are not really necessary
546 # to the translation (msgid = msgstr)
547 # Normalization can be disabled with COOKOPTS="!monorm"
549 normalize_mo() {
550 [ "${COOKOPTS/!monorm/}" != "$COOKOPTS" ] && return
551 [ -z "$(find $install -type f -name '*.mo')" ] && return
553 # Gettext functions: msgunfmt, msguniq, msgconv, msgfmt
554 tpi gettext
555 # Gconv modules (convert to UTF-8)
556 tpi glibc-locale
558 action 'Normalizing mo files...'
560 the_log="$(mktemp)"
561 to_continue=true
562 for i in msgunfmt msguniq msgconv msgfmt; do
563 if ! which $i >/dev/null; then
564 echo "Warning: $i not found. Normalizing aborted" > "$the_log"
565 to_continue=false
566 fi
567 done
569 size0=$(sizes mo1)
570 time0=$(get_time)
572 # Process all existing *.mo files
573 IFS=$'\n'
574 $to_continue &&
575 for mo in $(find "$install" -type f -name '*.mo'); do
576 tmpfile="$(mktemp)"
578 # put ANY errors of {msgunfmt,msguniq,msgconv} to $out. FIXME?
579 out="$({ msgunfmt "$mo" | msguniq | msgconv -o "$tmpfile" -t 'UTF-8'; } 2>&1)"
580 if [ -n "$out" ]; then
581 # using literal $'\n' here instead of using `echo -e "...\n..."` because
582 # $out may contain escapes ('\r', '\v') that we should print as-is
583 echo "$mo:"$'\n'"$out"$'\n' >> "$the_log"
584 continue # proceed to next file
585 fi
587 # add newline
588 echo >> "$tmpfile"
590 # get Plural-Forms
591 awk '
592 BEGIN { skip = ""; }
593 {
594 if (! skip) {
595 s = $0;
596 gsub(/^[^\"]*\"/, "", s);
597 gsub(/\"$/, "", s);
598 printf("%s", s);
599 }
600 if (! $0) skip = "yes";
601 }
602 ' "$tmpfile" | sed 's|\\n|\n|g' | grep "^Plural-Forms:" > "$tmpfile.pf"
604 if ! grep -q 'msgid_plural' "$tmpfile"; then
605 echo > "$tmpfile.pf"
606 fi
608 # main
609 awk -v pf="$(cat "$tmpfile.pf")" '
610 function clean() {
611 mode = msgctxt = msgid = msgid_plural = msgstr = msgstr0 = msgstr1 = msgstr2 = msgstr3 = msgstr4 = msgstr5 = "";
612 }
614 function getstring() {
615 # Skip unquoted words at the beginning (msgid, msgstr...) and get string from inside quotes
616 s = $0;
617 gsub(/^[^\"]*\"/, "", s);
618 gsub(/\"$/, "", s);
619 return s;
620 }
622 BEGIN {
623 printf("msgid \"\"\nmsgstr \"\"\n\"Content-Type: text/plain; charset=UTF-8\\n\"\n");
624 if (pf)
625 printf("\"%s\\n\"\n", pf);
626 printf("\n");
627 skip = 1;
628 clean();
629 }
631 {
632 # Skip the entire header
633 if (!skip) {
634 if ($1 == "msgctxt" || $1 == "msgid" || $1 == "msgstr" || $1 == "msgid_plural")
635 mode = $1;
636 if ($1 == "msgstr[0]") mode = "msgstr0";
637 if ($1 == "msgstr[1]") mode = "msgstr1";
638 if ($1 == "msgstr[2]") mode = "msgstr2";
639 if ($1 == "msgstr[3]") mode = "msgstr3";
640 if ($1 == "msgstr[4]") mode = "msgstr4";
641 if ($1 == "msgstr[5]") mode = "msgstr5";
643 if (mode == "msgctxt") msgctxt = msgctxt getstring();
644 if (mode == "msgid") msgid = msgid getstring();
645 if (mode == "msgstr") msgstr = msgstr getstring();
646 if (mode == "msgid_plural") msgid_plural = msgid_plural getstring();
647 if (mode == "msgstr0") msgstr0 = msgstr0 getstring();
648 if (mode == "msgstr1") msgstr1 = msgstr1 getstring();
649 if (mode == "msgstr2") msgstr2 = msgstr2 getstring();
650 if (mode == "msgstr3") msgstr3 = msgstr3 getstring();
651 if (mode == "msgstr4") msgstr4 = msgstr4 getstring();
652 if (mode == "msgstr5") msgstr5 = msgstr5 getstring();
654 if (! $0) {
655 if (msgid != msgstr) {
656 if (msgctxt) printf("msgctxt \"%s\"\n", msgctxt);
657 printf("msgid \"%s\"\n", msgid);
658 if (msgid_plural) printf("msgid_plural \"%s\"\n", msgid_plural);
659 if (msgstr) printf("msgstr \"%s\"\n", msgstr);
660 if (msgstr0) printf("msgstr[0] \"%s\"\n", msgstr0);
661 if (msgstr1) printf("msgstr[1] \"%s\"\n", msgstr1);
662 if (msgstr2) printf("msgstr[2] \"%s\"\n", msgstr2);
663 if (msgstr3) printf("msgstr[3] \"%s\"\n", msgstr3);
664 if (msgstr4) printf("msgstr[4] \"%s\"\n", msgstr4);
665 if (msgstr5) printf("msgstr[5] \"%s\"\n", msgstr5);
666 printf("\n");
667 }
668 clean();
669 }
670 }
671 if ($0 == "") skip = "";
672 }
673 ' "$tmpfile" > "$tmpfile.awk"
675 out="$(msgfmt "$tmpfile.awk" -o "$tmpfile.mo" 2>&1)"
676 if [ -n "$out" ]; then
677 echo "$mo (msgfmt):"$'\n'"$out"$'\n' >> "$the_log"
678 continue # proceed to next file
679 fi
681 if [ -s "$tmpfile.mo" ]; then
682 rm "$mo"; mv "$tmpfile.mo" "$mo"
683 else
684 _ 'Error processing %s' "$mo" >> "$the_log"
685 echo >> "$the_log"
686 [ -e "$tmpfile.mo" ] && rm "$tmpfile.mo"
687 fi
689 # Clean
690 rm "$tmpfile" "$tmpfile.pf" "$tmpfile.awk"
691 done
693 comp_summary "$time0" "$size0" "$(sizes mo1)" "$the_log"
694 }
697 # Strip locale definitions: normalize whitespace and remove comments
698 # Stripping can be disabled with COOKOPTS="!locdef"
700 strip_locale_def() {
701 [ "${COOKOPTS/!locdef/}" != "$COOKOPTS" ] && return
702 [ ! -d "$install/usr/share/i18n/locales" ] && return
703 [ -z "$(find $install/usr/share/i18n/locales -type f)" ] && return
705 action 'Stripping locale definitions...'
706 size0=$(sizes loc)
707 time0=$(get_time)
709 for i in $(find $install/usr/share/i18n/locales -type f); do
710 sed -i 's| | |g; s| *| |g; s|^ ||; /^%/d' $i
711 done
713 comp_summary "$time0" "$size0" "$(sizes loc)"
714 }
717 # Find and strip: --strip-all (-s) or --strip-debug on static libs as well
718 # as removing unneeded files like in Python packages. Cross compiled binaries
719 # must be stripped with cross-tools aka $ARCH-slitaz-*-strip
720 # Stripping can be disabled with COOKOPTS="!strip"
722 strip_package() {
723 [ "${COOKOPTS/!strip/}" != "$COOKOPTS" ] && return
725 local i ifs="$IFS"
726 IFS=$'\n'
728 case "$ARCH" in
729 arm*|x86_64) export STRIP="$HOST_SYSTEM-strip" ;;
730 *) export STRIP='strip' ;;
731 esac
732 action 'Executing strip on all files...'
733 size0=0
734 size1=0
735 time0=$(get_time)
736 oldsize=$(sizes strip)
739 # GNU strip (GNU Binutils)
740 # -p --preserve-dates Copy modified/access timestamps to the output
741 # -s --strip-all Remove all symbols and relocation information
742 # --strip-unneeded Remove all symbols not needed by relocations
743 # -D --enable-deterministic-archives Produce deterministic output when stripping archives
744 # -g -S -d --strip-debug Remove all debugging symbols & sections
746 # Strip executable files
747 while read i; do
748 $STRIP -ps "$i" 2>/dev/null
749 done <<EOT
750 $(find_elf EXEC)
751 EOT
753 # Strip shared libraries
754 while read i; do
755 case $i in
756 *.dbg) ;; # skip library.so.*.dbg debugging symbols
757 *) $STRIP -p --strip-unneeded "$i" 2>/dev/null;;
758 esac
759 done <<EOT
760 $(find_elf DYN)
761 EOT
763 # Strip static libraries
764 # See also: https://wiki.debian.org/ReproducibleBuilds/TimestampsInStaticLibraries
765 find $fs -name '*.a' -exec $STRIP -pdD '{}' 2>/dev/null \;
768 # Remove Python *.pyc and *.pyo
769 find $fs -type f \( -name '*.pyc' -o -name '*.pyo' \) -delete 2>/dev/null
771 # Remove both with the empty subfolders:
772 # 1. Perl perllocal.pod and .packlist (unconditionally)
773 local perlfiles="$(find $fs -type f \( -name 'perllocal.pod' -o -name '.packlist' \))"
774 # 2. Perl *.pod (if not disabled)
775 [ "${COOKOPTS/!rmpod/}" == "$COOKOPTS" ] &&
776 perlfiles="$perlfiles"$'\n'"$(find $fs -type f -name '*.pod')"
777 echo "$perlfiles" | sort -u | xargs rm -f 2>/dev/null
778 echo "$perlfiles" | sort -u | awk 'BEGIN{FS=OFS="/"}{$NF="";print}' \
779 | xargs rmdir -p --ignore-fail-on-non-empty 2>/dev/null
781 # Strip documentation inside Perl files (*.pm and *.pl) (if not disabled)
782 [ "${COOKOPTS/!perlz/}" == "$COOKOPTS" ] &&
783 find $fs -type f \( -name '*.pm' -o -name '*.pl' \) -exec sed -i '/^=/,/^=cut/d' '{}' \;
785 newsize=$(sizes strip)
787 comp_summary "$time0" "$oldsize" "$newsize"
788 IFS="$ifs"
789 }
792 # Strip unsupported locales (.mo files)
794 strip_mo_i18n() {
795 [ "${COOKOPTS/!i18nz/}" != "$COOKOPTS" ] && return
797 [ ! -d "$fs/usr/share/locale" ] && return
798 [ -z "$(find $fs/usr/share/locale -type f -name '*.mo')" ] && return
800 action 'Thin out translation files...'
801 size0=$(sizes mo2)
802 time0=$(get_time)
804 # The variable $LOCALE is set in cook.conf and may be overridden in the receipt.
805 # Default value is "" (empty). That means for us that we'll use the full
806 # list of supported locales here.
807 [ -z "$LOCALE" ] && LOCALE=$(get_supported_locales)
809 # Existing locales
810 elocales=" $(ls -1 "$fs/usr/share/locale" | tr '\n' ' ') "
812 # Thin out the list of existing locales. At the end there will be only locales that need
813 # deleting (and the garbage like '_AU', _US', '_BR' leaving from 'en_AU', 'en_US', 'pt_BR'...)
814 for keep_locale in $LOCALE; do
815 elocales=${elocales//$keep_locale}
816 done
818 # Remove the unsupported locales
819 for rem_locale in $elocales; do
820 [ ! -d "$fs/usr/share/locale/$rem_locale" ] ||
821 rm -r "$fs/usr/share/locale/$rem_locale"
822 done
824 comp_summary "$time0" "$size0" "$(sizes mo2)"
825 }
830 case $1 in
831 install)
832 # Compressors working in the $install
833 case "$ARCH" in
834 arm*) ;;
835 *)
836 recompress_gz
837 recompress_zip
838 compress_manpages
839 compress_png
840 compress_svg
841 compress_gif
842 compress_ui
843 ;;
844 esac
846 fix_desktop_files
847 normalize_mo
848 strip_locale_def
849 ;;
850 fs)
851 # Compressors working in the $fs
852 strip_package
853 strip_mo_i18n
854 ;;
855 esac
857 # Clean
858 rm "$cache_stat"
859 find $comp_cache_root -type f -links -2 -delete