cookutils view modules/compressor @ rev 1030
Tiny edits
author | Paul Issott <paul@slitaz.org> |
---|---|
date | Sun Feb 04 20:42:31 2018 +0000 (2018-02-04) |
parents | 7d37b282b4fb |
children | 32c318c574a7 |
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 | xargs ls -l
124 ;;
125 esac | awk '{s+=$5}END{print s}'
126 IFS="$ifs"
127 }
130 # Query cache for already existing compressed file; substitute original file with the cached
131 # compressed one if so.
132 # $1: cache section (gz, mangz, png, etc.); $2: path to original file
134 query_cache() {
135 md5=$(md5sum "$2")
136 cachefile="$comp_cache_root/$1/${md5%% *}"
137 echo "$cachefile"
138 if [ -f "$cachefile" ]; then
139 ln -f "$cachefile" "$2"
140 echo '+' >> "$cache_stat"
141 else
142 echo '-' >> "$cache_stat"
143 false
144 fi
145 }
148 # Store compressed file to the cache
149 # $1: path to cache entry to be stored; $2: path to compressed file to be stored
151 store_cache() {
152 mkdir -p "${1%/*}"
153 mv "$2" "$1"
154 ln "$1" "$2"
155 }
158 # Function to compress all man pages
159 # Compressing can be disabled with COOKOPTS="!manz"
161 compress_manpages() {
162 time0=$(get_time)
163 [ "${COOKOPTS/!manz/}" != "$COOKOPTS" ] && return
164 manpath="$install/usr/share/man"
165 [ -d "$manpath" ] || return
166 size0=$(sizes man); [ -z "$size0" ] && return
168 tpi advancecomp-static
170 action 'Compressing man pages...'
172 # We'll use only Gzip compression, so decompress other formats first
173 find $manpath -type f -name '*.bz2' -exec bunzip2 \{\} \;
174 find $manpath -type f -name '*.xz' -exec unxz \{\} \;
176 # Fast compress with gzip
177 find $manpath -type f ! -name '*.gz' -exec gzip \{\} \;
179 # Fix symlinks
180 for i in $(find $manpath -type l); do
181 dest=$(readlink $i | sed 's|\.[gbx]z2*$||')
182 link=$(echo $i | sed 's|\.[gbx]z2*$||')
183 rm $i; ln -s $dest.gz $link.gz
184 done
186 # Recompress with advdef (it can't compress, only recompress)
187 the_log="$(mktemp)"
188 if which advdef >/dev/null; then
189 IFS=$'\n'
190 for i in $(find $manpath -type f); do
191 if ! cached_path=$(query_cache mangz "$i"); then
192 cp -a "$i" "$i.orig$$" # save the original if something goes wrong
193 out="$(advdef -z4q "$i")"
194 if [ -n "$out" ]; then
195 echo "$i:"$'\n'"$out"$'\n' >> "$the_log"
196 mv -f "$i.orig$$" "$i" # restore the original
197 else
198 store_cache "$cached_path" "$i"
199 rm -f "$i.orig$$" # clean
200 fi
201 fi
202 done
203 else
204 echo 'Warning: advdef not found.' > "$the_log"
205 fi
207 comp_summary "$time0" "$size0" "$(sizes man)" "$the_log"
208 }
211 # Function to recompress all gzip archives
212 # Recompressing can be disabled with COOKOPTS="!gz"
214 recompress_gz() {
215 time0=$(get_time)
216 [ "${COOKOPTS/!gz/}" != "$COOKOPTS" ] && return
217 size0=$(sizes gz); [ -z "$size0" ] && return
219 tpi advancecomp-static
221 action 'Recompressing gzip files...'
223 # Recompress with advdef
224 the_log="$(mktemp)"
225 if which advdef >/dev/null; then
226 IFS=$'\n'
227 for i in $(find $install -type f -name '*.gz' ! -path '*/share/man/*'); do
228 if ! cached_path=$(query_cache gz "$i"); then
229 cp -a "$i" "$i.orig$$" # save the original if something goes wrong
230 out="$(advdef -z4q "$i")"
231 if [ -n "$out" ]; then
232 echo "$i:"$'\n'"$out"$'\n' >> "$the_log"
233 mv -f "$i.orig$$" "$i" # restore the original
234 else
235 store_cache "$cached_path" "$i"
236 rm -f "$i.orig$$" # clean
237 fi
238 fi
239 done
240 else
241 echo 'Warning: advdef not found.' > "$the_log"
242 fi
244 comp_summary "$time0" "$size0" "$(sizes gz)" "$the_log"
245 }
248 # Function to recompress all zip archives
249 # Recompressing can be disabled with COOKOPTS="!zip"
251 recompress_zip() {
252 time0=$(get_time)
253 [ "${COOKOPTS/!zip/}" != "$COOKOPTS" ] && return
254 size0=$(sizes zip); [ -z "$size0" ] && return
256 tpi advancecomp-static
258 action 'Recompressing zip files...'
260 # Recompress with advzip
261 the_log="$(mktemp)"
262 if which advzip >/dev/null; then
263 IFS=$'\n'
264 for i in $(find $install -type f -name '*.zip'); do
265 if ! cached_path=$(query_cache zip "$i"); then
266 cp -a "$i" "$i.orig$$" # save the original if something goes wrong
267 out="$(advzip -z3qk "$i")" # '-4' is more than two orders slower; use '-3'
268 if [ -n "$out" ]; then
269 echo "$i:"$'\n'"$out"$'\n' >> "$the_log"
270 mv -f "$i.orig$$" "$i" # restore the original
271 else
272 store_cache "$cached_path" "$i"
273 rm -f "$i.orig$$" # clean
274 fi
275 fi
276 done
277 else
278 echo 'Warning: advzip not found.' > "$the_log"
279 fi
281 comp_summary "$time0" "$size0" "$(sizes zip)" "$the_log"
282 }
285 # Function used after compile_rules() to compress all png images
286 # Compressing can be disabled with COOKOPTS="!pngz"
288 compress_png() {
289 time0=$(get_time)
290 [ "${COOKOPTS/!pngz/}" != "$COOKOPTS" ] && return
291 size0=$(sizes png); [ -z "$size0" ] && return
293 use_pq=true
294 use_op=true
295 [ "${COOKOPTS/!pngquant/}" != "$COOKOPTS" ] && use_pq=false
296 [ "${COOKOPTS/!optipng/}" != "$COOKOPTS" ] && use_op=false
297 $use_pq && tpi pngquant-static
298 $use_op && tpi optipng-static
300 action 'Compressing png images...'
302 the_log="$(mktemp)"
303 $use_pq && if ! which pngquant >/dev/null; then
304 echo 'Warning: pngquant not found.' > "$the_log"
305 use_pq=false
306 fi
307 $use_op && if ! which optipng >/dev/null; then
308 echo 'Warning: optipng not found.' >> "$the_log"
309 use_op=false
310 fi
312 oplevel=$(echo $COOKOPTS | grep 'op[0-8]' | sed 's|.*op\([0-8]\).*|\1|')
313 [ -z "$oplevel" ] && oplevel='2'
315 cache_section="png$oplevel"
316 $use_pq && cache_section="${cache_section}p"
317 $use_op && cache_section="${cache_section}o"
319 [ "$oplevel" == '8' ] && oplevel='7 -zm1-9'
321 pq_opt='--skip-if-larger' # Sublime Text is mad about `if` in $(), so put it separately
322 IFS=$'\n'
323 for i in $(find $install -type f -name '*.png'); do
324 unset IFS iserror
325 if ! cached_path=$(query_cache $cache_section "$i"); then
326 cp -a "$i" "$i.orig$$" # save the original if something goes wrong
327 if $use_pq; then
328 out="$(pngquant -f $pq_opt --ext .png --speed 1 "$i" 2>&1)"
329 if [ -n "$out" ]; then
330 echo "$i (pngquant):"$'\n'"$out"$'\n' >> "$the_log"
331 iserror='yes'
332 fi
333 fi
334 if $use_op && [ -z "$iserror" ]; then
335 out="$(optipng -quiet -strip all -o$oplevel "$i" 2>&1)"
336 if [ -n "$out" ]; then
337 echo "$i (optipng):"$'\n'"$out"$'\n' >> "$the_log"
338 iserror='yes'
339 fi
340 fi
341 if [ -n "$iserror" ]; then
342 mv -f "$i.orig$$" "$i" # restore the original
343 else
344 store_cache "$cached_path" "$i"
345 rm -f "$i.orig$$" # clean
346 fi
347 fi
348 done
350 comp_summary "$time0" "$size0" "$(sizes png)" "$the_log"
351 }
354 # Function used after compile_rules() to compress all svg images
355 # Compressing can be disabled with COOKOPTS="!svgz"
357 compress_svg() {
358 time0=$(get_time)
359 [ "${COOKOPTS/!svgz/}" != "$COOKOPTS" ] && return
360 size0=$(sizes svg); [ -z "$size0" ] && return
362 tpi svgcleaner
364 action 'Compressing svg images...'
366 if which svgcleaner >/dev/null; then
367 [ "${COOKOPTS/!svgextra/}" == "$COOKOPTS" ] &&
368 opts="--apply-transform-to-paths yes --coordinates-precision 1 --paths-coordinates-precision 1"
370 the_log="$(mktemp)"
371 for i in $(IFS=$'\n' find $install -type f -name '*.svg'); do
372 out="$(unset IFS; svgcleaner "$i" "$i" --copy-on-error --quiet \
373 --multipass --remove-unresolved-classes no $opts 2>&1)"
374 [ -z "$out" ] || echo "$i:"$'\n'"$out"$'\n' >> "$the_log"
375 done
376 else
377 echo 'Warning: svgcleaner not found.' > "$the_log"
378 fi
380 comp_summary "$time0" "$size0" "$(sizes svg)" "$the_log"
381 }
384 # Function used after compile_rules() to shrink all *.ui and *.glade files:
385 # remove insignificant spaces and comments
386 # Compressing can be disabled with COOKOPTS="!uiz"
388 compress_ui() {
389 [ "${COOKOPTS/!uiz/}" != "$COOKOPTS" ] && return
390 [ -z "$(find $install -type f \( -name '*.ui' -o -name '*.glade' \) )" ] && return
392 tpi xmlstarlet
394 action 'Compressing ui files...'
396 if which xmlstarlet >/dev/null; then
397 size0=$(sizes xml)
398 time0=$(get_time)
399 temp_ui="$(mktemp)"
400 the_log="$(mktemp)"
401 IFS=$'\n'
402 for ui in $(find $install -type f \( -name '*.ui' -o -name '*.glade' \) ); do
403 out="$(xmlstarlet c14n --without-comments "$ui" | xmlstarlet sel -B -t -c '*' > "$temp_ui")"
404 if [ -n "$out" ]; then
405 echo "$ui:"$'\n'"$out"$'\n' >> "$the_log"
406 else
407 cat "$temp_ui" > "$ui"
408 fi
409 done
410 else
411 echo 'Warning: xmlstarlet not found.' > "$the_log"
412 fi
414 comp_summary "$time0" "$size0" "$(sizes xml)" "$the_log"
415 rm "$temp_ui"
416 }
419 # Get list of supported locales...
421 get_supported_locales() {
422 lpc='/slitaz-i18n/stuff/locale-pack.conf'
423 if [ -e "$WOK$lpc" ]; then
424 # ... from package in the local wok
425 . "$WOK$lpc"
426 else
427 # ... from Hg
428 temp_conf=$(mktemp)
429 wget -q -O $temp_conf -T 10 "http://hg.slitaz.org/wok/raw-file/tip$lpc"
430 if [ -s $temp_conf ]; then
431 . $temp_conf
432 else
433 # Give up and use hardcoded list
434 LOCALE_PACK="ar ca cs da de el en es fi fr hr hu id is it ja nb nl nn pl pt \
435 pt_BR ro ru sl sv tr uk zh_CN zh_TW"
436 fi
437 rm $temp_conf
438 fi
439 echo $LOCALE_PACK
440 }
443 # Fix common errors and warnings in the .desktop files
444 # Fixing can be disabled with COOKOPTS="!fixdesktops"
446 fix_desktop_files() {
447 [ "${COOKOPTS/!fixdesktops/}" != "$COOKOPTS" ] && return
448 deskpath="$install/usr/share/applications"
449 [ -d "$deskpath" ] || return
450 [ -z "$(find $deskpath -type f -name '*.desktop')" ] && return
452 size0=$(sizes des)
453 time0=$(get_time)
455 if [ -n "$QA" -a -z "$(which desktop-file-validate)" ]; then
456 tpi desktop-file-validate-static
457 fi
459 # The variable $LOCALE is set in cook.conf and may be overridden in the receipt.
460 # Default value is "" (empty). That means for us that we'll use the full
461 # list of supported locales here.
462 [ -z "$LOCALE" ] && LOCALE=$(get_supported_locales)
464 IFS=$'\n'
465 for desktop in $(find $deskpath -type f -name '*.desktop'); do
466 cp "$desktop" "$desktop.orig"
468 # Sort out .desktop file (is prerequisite to correct working of `fix-desktop-file`)
469 sdft "$desktop" -i
471 # Fix common errors in .desktop file
472 fix-desktop-file "$desktop"
474 # Strip unsupported locales from .desktop file
475 [ "${COOKOPTS/!i18nz/}" == "$COOKOPTS" ] &&
476 sdft "$desktop" -i -k "$LOCALE"
478 # Extra-strip
479 [ "${COOKOPTS/!extradesktops/}" == "$COOKOPTS" ] &&
480 sdft "$desktop" -i -g -x -tf -r 'Keywords*' -o
482 if [ -n "$QA" ]; then
483 # Check the rest of errors, warnings and tips
484 _ 'QA: Checking %s...' "$(basename $desktop)"
485 busybox diff "$desktop.orig" "$desktop" | sed 's!^!|!'
486 if which xmlstarlet >/dev/null; then
487 desktop-file-validate "$desktop" | busybox fold -s
488 else
489 echo 'Warning: desktop-file-validate not found.'
490 fi
491 echo
492 fi
494 rm "$desktop.orig"
495 done
497 comp_summary "$time0" "$size0" "$(sizes des)" '/dev/null'
498 }
501 # Normalize all *.mo files: unconditionally convert to UTF-8; remove strings that are not really necessary
502 # to the translation (msgid = msgstr)
503 # Normalization can be disabled with COOKOPTS="!monorm"
505 normalize_mo() {
506 [ "${COOKOPTS/!monorm/}" != "$COOKOPTS" ] && return
507 [ -z "$(find $install -type f -name '*.mo')" ] && return
509 # Gettext functions: msgunfmt, msguniq, msgconv, msgfmt
510 tpi gettext
511 # Gconv modules (convert to UTF-8)
512 tpi glibc-locale
514 action 'Normalizing mo files...'
516 the_log="$(mktemp)"
517 to_continue=true
518 for i in msgunfmt msguniq msgconv msgfmt; do
519 if ! which $i >/dev/null; then
520 echo "Warning: $i not found. Normalizing aborted" > "$the_log"
521 to_continue=false
522 fi
523 done
525 size0=$(sizes mo1)
526 time0=$(get_time)
528 # Process all existing *.mo files
529 IFS=$'\n'
530 $to_continue &&
531 for mo in $(find "$install" -type f -name '*.mo'); do
532 tmpfile="$(mktemp)"
534 # put ANY errors of {msgunfmt,msguniq,msgconv} to $out. FIXME?
535 out="$({ msgunfmt "$mo" | msguniq | msgconv -o "$tmpfile" -t 'UTF-8'; } 2>&1)"
536 if [ -n "$out" ]; then
537 # using literal $'\n' here instead of using `echo -e "...\n..."` because
538 # $out may contain escapes ('\r', '\v') that we should print as-is
539 echo "$mo:"$'\n'"$out"$'\n' >> "$the_log"
540 continue # proceed to next file
541 fi
543 # add newline
544 echo >> "$tmpfile"
546 # get Plural-Forms
547 awk '
548 BEGIN { skip = ""; }
549 {
550 if (! skip) {
551 s = $0;
552 gsub(/^[^\"]*\"/, "", s);
553 gsub(/\"$/, "", s);
554 printf("%s", s);
555 }
556 if (! $0) skip = "yes";
557 }
558 ' "$tmpfile" | sed 's|\\n|\n|g' | grep "^Plural-Forms:" > "$tmpfile.pf"
560 if ! grep -q 'msgid_plural' "$tmpfile"; then
561 echo > "$tmpfile.pf"
562 fi
564 # main
565 awk -v pf="$(cat "$tmpfile.pf")" '
566 function clean() {
567 mode = msgctxt = msgid = msgid_plural = msgstr = msgstr0 = msgstr1 = msgstr2 = msgstr3 = msgstr4 = msgstr5 = "";
568 }
570 function getstring() {
571 # Skip unquoted words at the beginning (msgid, msgstr...) and get string from inside quotes
572 s = $0;
573 gsub(/^[^\"]*\"/, "", s);
574 gsub(/\"$/, "", s);
575 return s;
576 }
578 BEGIN {
579 printf("msgid \"\"\nmsgstr \"\"\n\"Content-Type: text/plain; charset=UTF-8\\n\"\n");
580 if (pf)
581 printf("\"%s\\n\"\n", pf);
582 printf("\n");
583 skip = 1;
584 clean();
585 }
587 {
588 # Skip the entire header
589 if (!skip) {
590 if ($1 == "msgctxt" || $1 == "msgid" || $1 == "msgstr" || $1 == "msgid_plural")
591 mode = $1;
592 if ($1 == "msgstr[0]") mode = "msgstr0";
593 if ($1 == "msgstr[1]") mode = "msgstr1";
594 if ($1 == "msgstr[2]") mode = "msgstr2";
595 if ($1 == "msgstr[3]") mode = "msgstr3";
596 if ($1 == "msgstr[4]") mode = "msgstr4";
597 if ($1 == "msgstr[5]") mode = "msgstr5";
599 if (mode == "msgctxt") msgctxt = msgctxt getstring();
600 if (mode == "msgid") msgid = msgid getstring();
601 if (mode == "msgstr") msgstr = msgstr getstring();
602 if (mode == "msgid_plural") msgid_plural = msgid_plural getstring();
603 if (mode == "msgstr0") msgstr0 = msgstr0 getstring();
604 if (mode == "msgstr1") msgstr1 = msgstr1 getstring();
605 if (mode == "msgstr2") msgstr2 = msgstr2 getstring();
606 if (mode == "msgstr3") msgstr3 = msgstr3 getstring();
607 if (mode == "msgstr4") msgstr4 = msgstr4 getstring();
608 if (mode == "msgstr5") msgstr5 = msgstr5 getstring();
610 if (! $0) {
611 if (msgid != msgstr) {
612 if (msgctxt) printf("msgctxt \"%s\"\n", msgctxt);
613 printf("msgid \"%s\"\n", msgid);
614 if (msgid_plural) printf("msgid_plural \"%s\"\n", msgid_plural);
615 if (msgstr) printf("msgstr \"%s\"\n", msgstr);
616 if (msgstr0) printf("msgstr[0] \"%s\"\n", msgstr0);
617 if (msgstr1) printf("msgstr[1] \"%s\"\n", msgstr1);
618 if (msgstr2) printf("msgstr[2] \"%s\"\n", msgstr2);
619 if (msgstr3) printf("msgstr[3] \"%s\"\n", msgstr3);
620 if (msgstr4) printf("msgstr[4] \"%s\"\n", msgstr4);
621 if (msgstr5) printf("msgstr[5] \"%s\"\n", msgstr5);
622 printf("\n");
623 }
624 clean();
625 }
626 }
627 if ($0 == "") skip = "";
628 }
629 ' "$tmpfile" > "$tmpfile.awk"
631 out="$(msgfmt "$tmpfile.awk" -o "$tmpfile.mo" 2>&1)"
632 if [ -n "$out" ]; then
633 echo "$mo (msgfmt):"$'\n'"$out"$'\n' >> "$the_log"
634 continue # proceed to next file
635 fi
637 if [ -s "$tmpfile.mo" ]; then
638 rm "$mo"; mv "$tmpfile.mo" "$mo"
639 else
640 _ 'Error processing %s' "$mo" >> "$the_log"
641 echo >> "$the_log"
642 [ -e "$tmpfile.mo" ] && rm "$tmpfile.mo"
643 fi
645 # Clean
646 rm "$tmpfile" "$tmpfile.pf" "$tmpfile.awk"
647 done
649 comp_summary "$time0" "$size0" "$(sizes mo1)" "$the_log"
650 }
653 # Strip locale definitions: normalize whitespace and remove comments
654 # Stripping can be disabled with COOKOPTS="!locdef"
656 strip_locale_def() {
657 [ "${COOKOPTS/!locdef/}" != "$COOKOPTS" ] && return
658 [ ! -d "$install/usr/share/i18n/locales" ] && return
659 [ -z "$(find $install/usr/share/i18n/locales -type f)" ] && return
661 action 'Stripping locale definitions...'
662 size0=$(sizes loc)
663 time0=$(get_time)
665 for i in $(find $install/usr/share/i18n/locales -type f); do
666 sed -i 's| | |g; s| *| |g; s|^ ||; /^%/d' $i
667 done
669 comp_summary "$time0" "$size0" "$(sizes loc)"
670 }
673 # Find and strip: --strip-all (-s) or --strip-debug on static libs as well
674 # as removing unneeded files like in Python packages. Cross compiled binaries
675 # must be stripped with cross-tools aka $ARCH-slitaz-*-strip
676 # Stripping can be disabled with COOKOPTS="!strip"
678 strip_package() {
679 [ "${COOKOPTS/!strip/}" != "$COOKOPTS" ] && return
681 local i ifs="$IFS"
682 IFS=$'\n'
684 case "$ARCH" in
685 arm*|x86_64) export STRIP="$HOST_SYSTEM-strip" ;;
686 *) export STRIP='strip' ;;
687 esac
688 action 'Executing strip on all files...'
689 size0=0
690 size1=0
691 time0=$(get_time)
692 oldsize=$(sizes strip)
695 # GNU strip (GNU Binutils)
696 # -p --preserve-dates Copy modified/access timestamps to the output
697 # -s --strip-all Remove all symbols and relocation information
698 # --strip-unneeded Remove all symbols not needed by relocations
699 # -D --enable-deterministic-archives Produce deterministic output when stripping archives
700 # -g -S -d --strip-debug Remove all debugging symbols & sections
702 # Strip executable files
703 while read i; do
704 $STRIP -ps "$i" 2>/dev/null
705 done <<EOT
706 $(find_elf EXEC)
707 EOT
709 # Strip shared libraries
710 while read i; do
711 case $i in
712 *.dbg) ;; # skip library.so.*.dbg debugging symbols
713 *) $STRIP -p --strip-unneeded "$i" 2>/dev/null;;
714 esac
715 done <<EOT
716 $(find_elf DYN)
717 EOT
719 # Strip static libraries
720 # See also: https://wiki.debian.org/ReproducibleBuilds/TimestampsInStaticLibraries
721 find $fs -name '*.a' -exec $STRIP -pdD '{}' 2>/dev/null \;
724 # Remove Python *.pyc and *.pyo
725 find $fs -type f \( -name '*.pyc' -o -name '*.pyo' \) -delete 2>/dev/null
727 # Remove both with the empty subfolders:
728 # 1. Perl perllocal.pod and .packlist (unconditionally)
729 local perlfiles="$(find $fs -type f \( -name 'perllocal.pod' -o -name '.packlist' \))"
730 # 2. Perl *.pod (if not disabled)
731 [ "${COOKOPTS/!rmpod/}" == "$COOKOPTS" ] &&
732 perlfiles="$perlfiles $(find $fs -type f -name '*.pod')"
733 echo "$perlfiles" | xargs rm -f 2>/dev/null
734 echo "$perlfiles" | awk 'BEGIN{FS=OFS="/"}{$NF="";print}' | xargs rmdir -p 2>/dev/null
736 # Strip documentation inside Perl files (*.pm and *.pl) (if not disabled)
737 [ "${COOKOPTS/!perlz/}" == "$COOKOPTS" ] &&
738 find $fs -type f \( -name '*.pm' -o -name '*.pl' \) -exec sed -i '/^=/,/^=cut/d' '{}' \;
740 newsize=$(sizes strip)
742 comp_summary "$time0" "$oldsize" "$newsize"
743 IFS="$ifs"
744 }
747 # Strip unsupported locales (.mo files)
749 strip_mo_i18n() {
750 [ "${COOKOPTS/!i18nz/}" != "$COOKOPTS" ] && return
752 [ ! -d "$fs/usr/share/locale" ] && return
753 [ -z "$(find $fs/usr/share/locale -type f -name '*.mo')" ] && return
755 action 'Thin out translation files...'
756 size0=$(sizes mo2)
757 time0=$(get_time)
759 # The variable $LOCALE is set in cook.conf and may be overridden in the receipt.
760 # Default value is "" (empty). That means for us that we'll use the full
761 # list of supported locales here.
762 [ -z "$LOCALE" ] && LOCALE=$(get_supported_locales)
764 # Existing locales
765 elocales=" $(ls -1 "$fs/usr/share/locale" | tr '\n' ' ') "
767 # Thin out the list of existing locales. At the end there will be only locales that need
768 # deleting (and the garbage like '_AU', _US', '_BR' leaving from 'en_AU', 'en_US', 'pt_BR'...)
769 for keep_locale in $LOCALE; do
770 elocales=${elocales//$keep_locale}
771 done
773 # Remove the unsupported locales
774 for rem_locale in $elocales; do
775 [ ! -d "$fs/usr/share/locale/$rem_locale" ] ||
776 rm -r "$fs/usr/share/locale/$rem_locale"
777 done
779 comp_summary "$time0" "$size0" "$(sizes mo2)"
780 }
785 case $1 in
786 install)
787 # Compressors working in the $install
788 case "$ARCH" in
789 arm*) ;;
790 *)
791 recompress_gz
792 recompress_zip
793 compress_manpages
794 compress_png
795 compress_svg
796 compress_ui
797 ;;
798 esac
800 fix_desktop_files
801 normalize_mo
802 strip_locale_def
803 ;;
804 fs)
805 # Compressors working in the $fs
806 strip_package
807 strip_mo_i18n
808 ;;
809 esac
811 # Clean
812 rm "$cache_stat"
813 find $comp_cache_root -type f -links -2 -delete