cookutils view modules/compressor @ rev 1045

modules/compressor: *.png.tmp file may be missing on fail
author Aleksej Bobylev <al.bobylev@gmail.com>
date Wed Apr 04 11:44:07 2018 +0300 (2018-04-04)
parents c57d43c19d8a
children 0af059297232
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 xml) find $install -type f \( -name '*.ui' -o -name '*.glade' \) -exec ls -l \{\} \; ;;
110 des) find $install -type f -name '*.desktop' -exec ls -l \{\} \; ;;
111 mo1) find $install -type f -name '*.mo' -exec ls -l \{\} \; ;;
112 loc) find $install/usr/share/i18n/locales -type f -exec ls -l \{\} \; ;;
113 mo2) find $fs/usr/share/locale -type f -name '*.mo' -exec ls -l \{\} \; ;;
114 gz) find $install -type f -name '*.gz' ! -path '*/share/man/*' -exec ls -l \{\} \; ;;
115 zip) find $install -type f -name '*.zip' -exec ls -l \{\} \; ;;
116 strip)
117 {
118 find_elf EXEC
119 find_elf DYN
120 find $fs -type f \( -name '*.a' -o -name '*.pyc' -o -name '*.pyo' \
121 -o -name '.packlist' -o -name '*.pm' -o -name '*.pl' -o -name '*.pod' \)
122 } \
123 | tr '\n' '\0' \
124 | xargs -0 ls -l
125 ;;
126 esac | awk '{s+=$5}END{print s}'
127 IFS="$ifs"
128 }
131 # Query cache for already existing compressed file; substitute original file with the cached
132 # compressed one if so.
133 # $1: cache section (gz, mangz, png, etc.); $2: path to original file
135 query_cache() {
136 md5=$(md5sum "$2")
137 cachefile="$comp_cache_root/$1/${md5%% *}"
138 echo "$cachefile"
139 if [ -f "$cachefile" ]; then
140 ln -f "$cachefile" "$2"
141 echo '+' >> "$cache_stat"
142 else
143 echo '-' >> "$cache_stat"
144 false
145 fi
146 }
149 # Store compressed file to the cache
150 # $1: path to cache entry to be stored; $2: path to compressed file to be stored
152 store_cache() {
153 mkdir -p "${1%/*}"
154 mv "$2" "$1"
155 ln "$1" "$2"
156 }
159 # Function to compress all man pages
160 # Compressing can be disabled with COOKOPTS="!manz"
162 compress_manpages() {
163 time0=$(get_time)
164 [ "${COOKOPTS/!manz/}" != "$COOKOPTS" ] && return
165 manpath="$install/usr/share/man"
166 [ -d "$manpath" ] || return
167 size0=$(sizes man); [ -z "$size0" ] && return
169 tpi advancecomp-static
171 action 'Compressing man pages...'
173 # We'll use only Gzip compression, so decompress other formats first
174 find $manpath -type f -name '*.bz2' -exec bunzip2 \{\} \;
175 find $manpath -type f -name '*.xz' -exec unxz \{\} \;
177 # Fast compress with gzip
178 find $manpath -type f ! -name '*.gz' -exec gzip \{\} \;
180 # Fix symlinks
181 for i in $(find $manpath -type l); do
182 dest=$(readlink $i | sed 's|\.[gbx]z2*$||')
183 link=$(echo $i | sed 's|\.[gbx]z2*$||')
184 rm $i; ln -s $dest.gz $link.gz
185 done
187 # Recompress with advdef (it can't compress, only recompress)
188 the_log="$(mktemp)"
189 if which advdef >/dev/null; then
190 IFS=$'\n'
191 for i in $(find $manpath -type f); do
192 if ! cached_path=$(query_cache mangz "$i"); then
193 cp -a "$i" "$i.orig$$" # save the original if something goes wrong
194 out="$(advdef -z4q "$i")"
195 if [ -n "$out" ]; then
196 echo "$i:"$'\n'"$out"$'\n' >> "$the_log"
197 mv -f "$i.orig$$" "$i" # restore the original
198 else
199 store_cache "$cached_path" "$i"
200 rm -f "$i.orig$$" # clean
201 fi
202 fi
203 done
204 else
205 echo 'Warning: advdef not found.' > "$the_log"
206 fi
208 comp_summary "$time0" "$size0" "$(sizes man)" "$the_log"
209 }
212 # Function to recompress all gzip archives
213 # Recompressing can be disabled with COOKOPTS="!gz"
215 recompress_gz() {
216 time0=$(get_time)
217 [ "${COOKOPTS/!gz/}" != "$COOKOPTS" ] && return
218 size0=$(sizes gz); [ -z "$size0" ] && return
220 tpi advancecomp-static
222 action 'Recompressing gzip files...'
224 # Recompress with advdef
225 the_log="$(mktemp)"
226 if which advdef >/dev/null; then
227 IFS=$'\n'
228 for i in $(find $install -type f -name '*.gz' ! -path '*/share/man/*'); do
229 if ! cached_path=$(query_cache gz "$i"); then
230 cp -a "$i" "$i.orig$$" # save the original if something goes wrong
231 out="$(advdef -z4q "$i")"
232 if [ -n "$out" ]; then
233 echo "$i:"$'\n'"$out"$'\n' >> "$the_log"
234 mv -f "$i.orig$$" "$i" # restore the original
235 else
236 store_cache "$cached_path" "$i"
237 rm -f "$i.orig$$" # clean
238 fi
239 fi
240 done
241 else
242 echo 'Warning: advdef not found.' > "$the_log"
243 fi
245 comp_summary "$time0" "$size0" "$(sizes gz)" "$the_log"
246 }
249 # Function to recompress all zip archives
250 # Recompressing can be disabled with COOKOPTS="!zip"
252 recompress_zip() {
253 time0=$(get_time)
254 [ "${COOKOPTS/!zip/}" != "$COOKOPTS" ] && return
255 size0=$(sizes zip); [ -z "$size0" ] && return
257 tpi advancecomp-static
259 action 'Recompressing zip files...'
261 # Recompress with advzip
262 the_log="$(mktemp)"
263 if which advzip >/dev/null; then
264 IFS=$'\n'
265 for i in $(find $install -type f -name '*.zip'); do
266 if ! cached_path=$(query_cache zip "$i"); then
267 cp -a "$i" "$i.orig$$" # save the original if something goes wrong
268 out="$(advzip -z3qk "$i")" # '-4' is more than two orders slower; use '-3'
269 if [ -n "$out" ]; then
270 echo "$i:"$'\n'"$out"$'\n' >> "$the_log"
271 mv -f "$i.orig$$" "$i" # restore the original
272 else
273 store_cache "$cached_path" "$i"
274 rm -f "$i.orig$$" # clean
275 fi
276 fi
277 done
278 else
279 echo 'Warning: advzip not found.' > "$the_log"
280 fi
282 comp_summary "$time0" "$size0" "$(sizes zip)" "$the_log"
283 }
286 # Function used after compile_rules() to compress all png images
287 # Compressing can be disabled with COOKOPTS="!pngz"
289 compress_png() {
290 time0=$(get_time)
291 [ "${COOKOPTS/!pngz/}" != "$COOKOPTS" ] && return
292 size0=$(sizes png); [ -z "$size0" ] && return
294 use_pq=true
295 use_op=true
296 [ "${COOKOPTS/!pngquant/}" != "$COOKOPTS" ] && use_pq=false
297 [ "${COOKOPTS/!optipng/}" != "$COOKOPTS" ] && use_op=false
298 $use_pq && tpi pngquant-static
299 $use_op && tpi optipng-static
301 action 'Compressing png images...'
303 the_log="$(mktemp)"
304 $use_pq && if ! which pngquant >/dev/null; then
305 echo 'Warning: pngquant not found.' > "$the_log"
306 use_pq=false
307 fi
308 $use_op && if ! which optipng >/dev/null; then
309 echo 'Warning: optipng not found.' >> "$the_log"
310 use_op=false
311 fi
313 oplevel=$(echo $COOKOPTS | grep 'op[0-8]' | sed 's|.*op\([0-8]\).*|\1|')
314 [ -z "$oplevel" ] && oplevel='2'
316 cache_section="png$oplevel"
317 $use_pq && cache_section="${cache_section}p"
318 $use_op && cache_section="${cache_section}o"
320 [ "$oplevel" == '8' ] && oplevel='7 -zm1-9'
322 pq_opt='--skip-if-larger' # Sublime Text is mad about `if` in $(), so put it separately
323 IFS=$'\n'
324 for i in $(find $install -type f -name '*.png'); do
325 unset IFS iserror
326 if ! cached_path=$(query_cache $cache_section "$i"); then
327 cp -a "$i" "$i.orig$$" # save the original if something goes wrong
328 if $use_pq; then
329 out="$(pngquant -f $pq_opt --ext .png --speed 1 "$i" 2>&1)"
330 if [ -n "$out" ]; then
331 echo "$i (pngquant):"$'\n'"$out"$'\n' >> "$the_log"
332 iserror='yes'
333 [ -e "$i.tmp" ] && rm "$i.tmp" # zero-size file remains on pngquant fail
334 fi
335 fi
336 if $use_op && [ -z "$iserror" ]; then
337 out="$(optipng -quiet -strip all -o$oplevel "$i" 2>&1)"
338 if [ -n "$out" ]; then
339 echo "$i (optipng):"$'\n'"$out"$'\n' >> "$the_log"
340 iserror='yes'
341 fi
342 fi
343 if [ -n "$iserror" ]; then
344 mv -f "$i.orig$$" "$i" # restore the original
345 else
346 store_cache "$cached_path" "$i"
347 rm -f "$i.orig$$" # clean
348 fi
349 fi
350 done
352 comp_summary "$time0" "$size0" "$(sizes png)" "$the_log"
353 }
356 # Function used after compile_rules() to compress all svg images
357 # Compressing can be disabled with COOKOPTS="!svgz"
359 compress_svg() {
360 time0=$(get_time)
361 [ "${COOKOPTS/!svgz/}" != "$COOKOPTS" ] && return
362 size0=$(sizes svg); [ -z "$size0" ] && return
364 tpi svgcleaner
366 action 'Compressing svg images...'
368 if which svgcleaner >/dev/null; then
369 [ "${COOKOPTS/!svgextra/}" == "$COOKOPTS" ] &&
370 opts="--apply-transform-to-paths yes --coordinates-precision 1 --paths-coordinates-precision 1"
372 the_log="$(mktemp)"
373 for i in $(IFS=$'\n' find $install -type f -name '*.svg'); do
374 out="$(unset IFS; svgcleaner "$i" "$i" --copy-on-error --quiet \
375 --multipass --remove-unresolved-classes no $opts 2>&1)"
376 [ -z "$out" ] || echo "$i:"$'\n'"$out"$'\n' >> "$the_log"
377 done
378 else
379 echo 'Warning: svgcleaner not found.' > "$the_log"
380 fi
382 comp_summary "$time0" "$size0" "$(sizes svg)" "$the_log"
383 }
386 # Function used after compile_rules() to shrink all *.ui and *.glade files:
387 # remove insignificant spaces and comments
388 # Compressing can be disabled with COOKOPTS="!uiz"
390 compress_ui() {
391 [ "${COOKOPTS/!uiz/}" != "$COOKOPTS" ] && return
392 [ -z "$(find $install -type f \( -name '*.ui' -o -name '*.glade' \) )" ] && return
394 tpi xmlstarlet
396 action 'Compressing ui files...'
398 if which xmlstarlet >/dev/null; then
399 size0=$(sizes xml)
400 time0=$(get_time)
401 temp_ui="$(mktemp)"
402 the_log="$(mktemp)"
403 IFS=$'\n'
404 for ui in $(find $install -type f \( -name '*.ui' -o -name '*.glade' \) ); do
405 out="$(xmlstarlet c14n --without-comments "$ui" | xmlstarlet sel -B -t -c '*' > "$temp_ui")"
406 if [ -n "$out" ]; then
407 echo "$ui:"$'\n'"$out"$'\n' >> "$the_log"
408 else
409 cat "$temp_ui" > "$ui"
410 fi
411 done
412 else
413 echo 'Warning: xmlstarlet not found.' > "$the_log"
414 fi
416 comp_summary "$time0" "$size0" "$(sizes xml)" "$the_log"
417 rm "$temp_ui"
418 }
421 # Get list of supported locales...
423 get_supported_locales() {
424 lpc='/slitaz-i18n/stuff/locale-pack.conf'
425 if [ -e "$WOK$lpc" ]; then
426 # ... from package in the local wok
427 . "$WOK$lpc"
428 else
429 # ... from Hg
430 temp_conf=$(mktemp)
431 wget -q -O $temp_conf -T 10 "http://hg.slitaz.org/wok/raw-file/tip$lpc"
432 if [ -s $temp_conf ]; then
433 . $temp_conf
434 else
435 # Give up and use hardcoded list
436 LOCALE_PACK="ar ca cs da de el en es fi fr hr hu id is it ja nb nl nn pl pt \
437 pt_BR ro ru sl sv tr uk zh_CN zh_TW"
438 fi
439 rm $temp_conf
440 fi
441 echo $LOCALE_PACK
442 }
445 # Fix common errors and warnings in the .desktop files
446 # Fixing can be disabled with COOKOPTS="!fixdesktops"
448 fix_desktop_files() {
449 [ "${COOKOPTS/!fixdesktops/}" != "$COOKOPTS" ] && return
450 deskpath="$install/usr/share/applications"
451 [ -d "$deskpath" ] || return
452 [ -z "$(find $deskpath -type f -name '*.desktop')" ] && return
454 size0=$(sizes des)
455 time0=$(get_time)
457 if [ -n "$QA" -a -z "$(which desktop-file-validate)" ]; then
458 tpi desktop-file-validate-static
459 fi
461 # The variable $LOCALE is set in cook.conf and may be overridden in the receipt.
462 # Default value is "" (empty). That means for us that we'll use the full
463 # list of supported locales here.
464 [ -z "$LOCALE" ] && LOCALE=$(get_supported_locales)
466 IFS=$'\n'
467 for desktop in $(find $deskpath -type f -name '*.desktop'); do
468 cp "$desktop" "$desktop.orig"
470 # Sort out .desktop file (is prerequisite to correct working of `fix-desktop-file`)
471 sdft "$desktop" -i
473 # Fix common errors in .desktop file
474 fix-desktop-file "$desktop"
476 # Strip unsupported locales from .desktop file
477 [ "${COOKOPTS/!i18nz/}" == "$COOKOPTS" ] &&
478 sdft "$desktop" -i -k "$LOCALE"
480 # Extra-strip
481 [ "${COOKOPTS/!extradesktops/}" == "$COOKOPTS" ] &&
482 sdft "$desktop" -i -g -x -tf -r 'Keywords*' -o
484 if [ -n "$QA" ]; then
485 # Check the rest of errors, warnings and tips
486 _ 'QA: Checking %s...' "$(basename $desktop)"
487 busybox diff "$desktop.orig" "$desktop" | sed 's!^!|!'
488 if which desktop-file-validate >/dev/null; then
489 desktop-file-validate "$desktop" | busybox fold -s
490 else
491 echo 'Warning: desktop-file-validate not found.'
492 fi
493 echo
494 fi
496 rm "$desktop.orig"
497 done
499 comp_summary "$time0" "$size0" "$(sizes des)" '/dev/null'
500 }
503 # Normalize all *.mo files: unconditionally convert to UTF-8; remove strings that are not really necessary
504 # to the translation (msgid = msgstr)
505 # Normalization can be disabled with COOKOPTS="!monorm"
507 normalize_mo() {
508 [ "${COOKOPTS/!monorm/}" != "$COOKOPTS" ] && return
509 [ -z "$(find $install -type f -name '*.mo')" ] && return
511 # Gettext functions: msgunfmt, msguniq, msgconv, msgfmt
512 tpi gettext
513 # Gconv modules (convert to UTF-8)
514 tpi glibc-locale
516 action 'Normalizing mo files...'
518 the_log="$(mktemp)"
519 to_continue=true
520 for i in msgunfmt msguniq msgconv msgfmt; do
521 if ! which $i >/dev/null; then
522 echo "Warning: $i not found. Normalizing aborted" > "$the_log"
523 to_continue=false
524 fi
525 done
527 size0=$(sizes mo1)
528 time0=$(get_time)
530 # Process all existing *.mo files
531 IFS=$'\n'
532 $to_continue &&
533 for mo in $(find "$install" -type f -name '*.mo'); do
534 tmpfile="$(mktemp)"
536 # put ANY errors of {msgunfmt,msguniq,msgconv} to $out. FIXME?
537 out="$({ msgunfmt "$mo" | msguniq | msgconv -o "$tmpfile" -t 'UTF-8'; } 2>&1)"
538 if [ -n "$out" ]; then
539 # using literal $'\n' here instead of using `echo -e "...\n..."` because
540 # $out may contain escapes ('\r', '\v') that we should print as-is
541 echo "$mo:"$'\n'"$out"$'\n' >> "$the_log"
542 continue # proceed to next file
543 fi
545 # add newline
546 echo >> "$tmpfile"
548 # get Plural-Forms
549 awk '
550 BEGIN { skip = ""; }
551 {
552 if (! skip) {
553 s = $0;
554 gsub(/^[^\"]*\"/, "", s);
555 gsub(/\"$/, "", s);
556 printf("%s", s);
557 }
558 if (! $0) skip = "yes";
559 }
560 ' "$tmpfile" | sed 's|\\n|\n|g' | grep "^Plural-Forms:" > "$tmpfile.pf"
562 if ! grep -q 'msgid_plural' "$tmpfile"; then
563 echo > "$tmpfile.pf"
564 fi
566 # main
567 awk -v pf="$(cat "$tmpfile.pf")" '
568 function clean() {
569 mode = msgctxt = msgid = msgid_plural = msgstr = msgstr0 = msgstr1 = msgstr2 = msgstr3 = msgstr4 = msgstr5 = "";
570 }
572 function getstring() {
573 # Skip unquoted words at the beginning (msgid, msgstr...) and get string from inside quotes
574 s = $0;
575 gsub(/^[^\"]*\"/, "", s);
576 gsub(/\"$/, "", s);
577 return s;
578 }
580 BEGIN {
581 printf("msgid \"\"\nmsgstr \"\"\n\"Content-Type: text/plain; charset=UTF-8\\n\"\n");
582 if (pf)
583 printf("\"%s\\n\"\n", pf);
584 printf("\n");
585 skip = 1;
586 clean();
587 }
589 {
590 # Skip the entire header
591 if (!skip) {
592 if ($1 == "msgctxt" || $1 == "msgid" || $1 == "msgstr" || $1 == "msgid_plural")
593 mode = $1;
594 if ($1 == "msgstr[0]") mode = "msgstr0";
595 if ($1 == "msgstr[1]") mode = "msgstr1";
596 if ($1 == "msgstr[2]") mode = "msgstr2";
597 if ($1 == "msgstr[3]") mode = "msgstr3";
598 if ($1 == "msgstr[4]") mode = "msgstr4";
599 if ($1 == "msgstr[5]") mode = "msgstr5";
601 if (mode == "msgctxt") msgctxt = msgctxt getstring();
602 if (mode == "msgid") msgid = msgid getstring();
603 if (mode == "msgstr") msgstr = msgstr getstring();
604 if (mode == "msgid_plural") msgid_plural = msgid_plural getstring();
605 if (mode == "msgstr0") msgstr0 = msgstr0 getstring();
606 if (mode == "msgstr1") msgstr1 = msgstr1 getstring();
607 if (mode == "msgstr2") msgstr2 = msgstr2 getstring();
608 if (mode == "msgstr3") msgstr3 = msgstr3 getstring();
609 if (mode == "msgstr4") msgstr4 = msgstr4 getstring();
610 if (mode == "msgstr5") msgstr5 = msgstr5 getstring();
612 if (! $0) {
613 if (msgid != msgstr) {
614 if (msgctxt) printf("msgctxt \"%s\"\n", msgctxt);
615 printf("msgid \"%s\"\n", msgid);
616 if (msgid_plural) printf("msgid_plural \"%s\"\n", msgid_plural);
617 if (msgstr) printf("msgstr \"%s\"\n", msgstr);
618 if (msgstr0) printf("msgstr[0] \"%s\"\n", msgstr0);
619 if (msgstr1) printf("msgstr[1] \"%s\"\n", msgstr1);
620 if (msgstr2) printf("msgstr[2] \"%s\"\n", msgstr2);
621 if (msgstr3) printf("msgstr[3] \"%s\"\n", msgstr3);
622 if (msgstr4) printf("msgstr[4] \"%s\"\n", msgstr4);
623 if (msgstr5) printf("msgstr[5] \"%s\"\n", msgstr5);
624 printf("\n");
625 }
626 clean();
627 }
628 }
629 if ($0 == "") skip = "";
630 }
631 ' "$tmpfile" > "$tmpfile.awk"
633 out="$(msgfmt "$tmpfile.awk" -o "$tmpfile.mo" 2>&1)"
634 if [ -n "$out" ]; then
635 echo "$mo (msgfmt):"$'\n'"$out"$'\n' >> "$the_log"
636 continue # proceed to next file
637 fi
639 if [ -s "$tmpfile.mo" ]; then
640 rm "$mo"; mv "$tmpfile.mo" "$mo"
641 else
642 _ 'Error processing %s' "$mo" >> "$the_log"
643 echo >> "$the_log"
644 [ -e "$tmpfile.mo" ] && rm "$tmpfile.mo"
645 fi
647 # Clean
648 rm "$tmpfile" "$tmpfile.pf" "$tmpfile.awk"
649 done
651 comp_summary "$time0" "$size0" "$(sizes mo1)" "$the_log"
652 }
655 # Strip locale definitions: normalize whitespace and remove comments
656 # Stripping can be disabled with COOKOPTS="!locdef"
658 strip_locale_def() {
659 [ "${COOKOPTS/!locdef/}" != "$COOKOPTS" ] && return
660 [ ! -d "$install/usr/share/i18n/locales" ] && return
661 [ -z "$(find $install/usr/share/i18n/locales -type f)" ] && return
663 action 'Stripping locale definitions...'
664 size0=$(sizes loc)
665 time0=$(get_time)
667 for i in $(find $install/usr/share/i18n/locales -type f); do
668 sed -i 's| | |g; s| *| |g; s|^ ||; /^%/d' $i
669 done
671 comp_summary "$time0" "$size0" "$(sizes loc)"
672 }
675 # Find and strip: --strip-all (-s) or --strip-debug on static libs as well
676 # as removing unneeded files like in Python packages. Cross compiled binaries
677 # must be stripped with cross-tools aka $ARCH-slitaz-*-strip
678 # Stripping can be disabled with COOKOPTS="!strip"
680 strip_package() {
681 [ "${COOKOPTS/!strip/}" != "$COOKOPTS" ] && return
683 local i ifs="$IFS"
684 IFS=$'\n'
686 case "$ARCH" in
687 arm*|x86_64) export STRIP="$HOST_SYSTEM-strip" ;;
688 *) export STRIP='strip' ;;
689 esac
690 action 'Executing strip on all files...'
691 size0=0
692 size1=0
693 time0=$(get_time)
694 oldsize=$(sizes strip)
697 # GNU strip (GNU Binutils)
698 # -p --preserve-dates Copy modified/access timestamps to the output
699 # -s --strip-all Remove all symbols and relocation information
700 # --strip-unneeded Remove all symbols not needed by relocations
701 # -D --enable-deterministic-archives Produce deterministic output when stripping archives
702 # -g -S -d --strip-debug Remove all debugging symbols & sections
704 # Strip executable files
705 while read i; do
706 $STRIP -ps "$i" 2>/dev/null
707 done <<EOT
708 $(find_elf EXEC)
709 EOT
711 # Strip shared libraries
712 while read i; do
713 case $i in
714 *.dbg) ;; # skip library.so.*.dbg debugging symbols
715 *) $STRIP -p --strip-unneeded "$i" 2>/dev/null;;
716 esac
717 done <<EOT
718 $(find_elf DYN)
719 EOT
721 # Strip static libraries
722 # See also: https://wiki.debian.org/ReproducibleBuilds/TimestampsInStaticLibraries
723 find $fs -name '*.a' -exec $STRIP -pdD '{}' 2>/dev/null \;
726 # Remove Python *.pyc and *.pyo
727 find $fs -type f \( -name '*.pyc' -o -name '*.pyo' \) -delete 2>/dev/null
729 # Remove both with the empty subfolders:
730 # 1. Perl perllocal.pod and .packlist (unconditionally)
731 local perlfiles="$(find $fs -type f \( -name 'perllocal.pod' -o -name '.packlist' \))"
732 # 2. Perl *.pod (if not disabled)
733 [ "${COOKOPTS/!rmpod/}" == "$COOKOPTS" ] &&
734 perlfiles="$perlfiles $(find $fs -type f -name '*.pod')"
735 echo "$perlfiles" | xargs rm -f 2>/dev/null
736 echo "$perlfiles" | awk 'BEGIN{FS=OFS="/"}{$NF="";print}' | xargs rmdir -p 2>/dev/null
738 # Strip documentation inside Perl files (*.pm and *.pl) (if not disabled)
739 [ "${COOKOPTS/!perlz/}" == "$COOKOPTS" ] &&
740 find $fs -type f \( -name '*.pm' -o -name '*.pl' \) -exec sed -i '/^=/,/^=cut/d' '{}' \;
742 newsize=$(sizes strip)
744 comp_summary "$time0" "$oldsize" "$newsize"
745 IFS="$ifs"
746 }
749 # Strip unsupported locales (.mo files)
751 strip_mo_i18n() {
752 [ "${COOKOPTS/!i18nz/}" != "$COOKOPTS" ] && return
754 [ ! -d "$fs/usr/share/locale" ] && return
755 [ -z "$(find $fs/usr/share/locale -type f -name '*.mo')" ] && return
757 action 'Thin out translation files...'
758 size0=$(sizes mo2)
759 time0=$(get_time)
761 # The variable $LOCALE is set in cook.conf and may be overridden in the receipt.
762 # Default value is "" (empty). That means for us that we'll use the full
763 # list of supported locales here.
764 [ -z "$LOCALE" ] && LOCALE=$(get_supported_locales)
766 # Existing locales
767 elocales=" $(ls -1 "$fs/usr/share/locale" | tr '\n' ' ') "
769 # Thin out the list of existing locales. At the end there will be only locales that need
770 # deleting (and the garbage like '_AU', _US', '_BR' leaving from 'en_AU', 'en_US', 'pt_BR'...)
771 for keep_locale in $LOCALE; do
772 elocales=${elocales//$keep_locale}
773 done
775 # Remove the unsupported locales
776 for rem_locale in $elocales; do
777 [ ! -d "$fs/usr/share/locale/$rem_locale" ] ||
778 rm -r "$fs/usr/share/locale/$rem_locale"
779 done
781 comp_summary "$time0" "$size0" "$(sizes mo2)"
782 }
787 case $1 in
788 install)
789 # Compressors working in the $install
790 case "$ARCH" in
791 arm*) ;;
792 *)
793 recompress_gz
794 recompress_zip
795 compress_manpages
796 compress_png
797 compress_svg
798 compress_ui
799 ;;
800 esac
802 fix_desktop_files
803 normalize_mo
804 strip_locale_def
805 ;;
806 fs)
807 # Compressors working in the $fs
808 strip_package
809 strip_mo_i18n
810 ;;
811 esac
813 # Clean
814 rm "$cache_stat"
815 find $comp_cache_root -type f -links -2 -delete