shithub: moonfish

ref: b2480be72099183e82ca9e25c1faea6e4221d28e
dir: /minify.sh/

View raw version
#!/usr/bin/env bash

# moonfish is licensed under the AGPL (v3 or later)
# copyright 2023, 2024 zamfofex

set -e

# for every C source file
cat moonfish.h chess.c search.c main.c |

# remove the '#' from system '#include'
sed 's/^#\(include <\)/\1/g' |

# preprocess the file, add '#' back to 'include', remove debug statements
# note: this materialises the whole file
gcc -E -Dinclude='#include' -D'perror(...)=' -D'fprintf(...)=' -Dmoonfish_mini - |

# remove lines starting with '# '
sed '/^# /d' |

# remove leading white space from '#include' lines
sed 's/^[\t ]\+#/#/g' |

# place all '#include' lines on top
# note: this materialises the whole file
( txt="$(tee)" && { grep '^#' <<< "$txt" ; } && { grep -v '^#' <<< "$txt" ; } ) |

# put line breaks around string literals
sed 's/\("\(\\.\|[^"]\)*"\)/\n\1\n/g' |

# put line breaks around character literals
sed 's/\('"'"'\(\\.\|.\)*'"'"'\)/\n\1\n/g' |

# in every line that isn't a string literal or '#include' line,
# put line breaks around identifiers
sed '/^[^"'"'"'#]/s/[a-z0-9_]\+/\n\0\n/gi' |

# rename identifiers to be shorter
./rename.sh |

# replace all white space with tabs (except inside string literals)
# note: this makes the next 'sed' materialise the whole file
sed '/^[^"'"'"']/s/[\t ]\+/\t/g' |
tr '\n' '\t' |

# replace tabs between alphanumeric characters with a single space
# note: there are no tabs inside string literals
sed 's/\([a-z0-9_]\)\t\+\([a-z0-9_]\)/\1 \2/gi' |

# remove all tab characters
tr -d '\t' |

# put line breaks back around '#include' lines again
# (also remove whitespace betwen '#include' and '<')
# note: there is no white space within include file names
sed 's/#include<[^>]*>/\n\0\n/g' |

# remove all empty lines
sed '/^$/d' |

# remove duplicate lines (for '#include')
awk '!x[$0]++' |

# store the result into a file
tee moonfish.c |

# and also compress it
xz -9 -e > moonfish.c.xz

# also make it into a runnable program
cat - moonfish.c.xz > moonfish.sh << END
#!/bin/sh
t=\`mktemp\`
tail -n +5 "\$0"|xz -d|cc -march=native -O3 -o \$t -xc - -pthread
(sleep 3;rm \$t)&exec \$t
END
chmod +x moonfish.sh