Last active
March 25, 2017 12:14
-
-
Save Bajron/4faf322db9e6096599217b957d756744 to your computer and use it in GitHub Desktop.
Minimake
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
if [ "${BASH_SOURCE[0]}" == "${0}" ]; then | |
echo "* This file is supposed to be sourced!" | |
echo "" | |
echo "Usage:" | |
echo " source $0" | |
exit 1 | |
fi | |
# core functionality that you want to copy+customize+paste | |
alias "g++=g++ -std=c++14 -Wall" | |
# while this is logical... | |
#function ot() { [ "${1%%.cpp}" -ot "${1%%.cpp}.cpp" ]; } | |
# this can handle includes in the same directory :) | |
function ot(){ local f="${1:-${minimake_DEFAULT}}"; [ "$(ls -t -1 | head -1)" != "${f%.cpp}" ]; } | |
function b() { local f="${1:-${minimake_DEFAULT}}"; g++ -o "${f%.cpp}" "${f%.cpp}.cpp" -Wall -Wshadow $(minimake_gf "$f"); } | |
function m() { local f="${1:-${minimake_DEFAULT}}"; ot "$f" && b "$f"; } | |
function x() { local f="${1:-${minimake_DEFAULT}}"; shift; m "$f" ; ot "$f" || timeout 120 ./"${f%.cpp}" "$@"; } | |
function minimake_gf() { echo ''; } #< default for copy+paste | |
# fireworks | |
function br() { local f="${1:-${minimake_DEFAULT}}"; g++ -O2 -o "${f%.cpp}" "${f%.cpp}.cpp" $(minimake_gf "$f"); } | |
function bd() { local f="${1:-${minimake_DEFAULT}}"; g++ -g -o "${f%.cpp}" "${f%.cpp}.cpp" $(minimake_gf "$f"); } | |
function mr() { local f="${1:-${minimake_DEFAULT}}"; ot "$f" && br "$f"; } | |
function md() { local f="${1:-${minimake_DEFAULT}}"; ot "$f" && bd "$f"; } | |
function d() { local f="${1:-${minimake_DEFAULT}}"; shift; bd "$f"; ot "$f" || gdb --args ./"${f%.cpp}" "$@"; } | |
function t() { local f="${1:-${minimake_DEFAULT}}"; shift; m "$f"; ot "$f" || timeout 120 ./"${f%.cpp}" "$@" < "${f%.cpp}.in"; } | |
function r() { local f="${1:-${minimake_DEFAULT}}"; shift; mr "$f"; ot "$f" || timeout 120 ./"${f%.cpp}" "$@" 2>/dev/null; } | |
function c() { local f="${1:-${minimake_DEFAULT}}"; [ -x "${f%.cpp}" ] && rm -v "${f%.cpp}"; } | |
function e() { local f="${1:-${minimake_DEFAULT}}"; [ "$f" ] && "${EDITOR:-vim}" "${f%.cpp}.cpp"; } | |
function n() { local f="${1:-${minimake_DEFAULT}}"; [ "$f" != "" -a ! -f "${f%.cpp}.cpp" ] && cat >"${f%.cpp}.cpp" <<EOF | |
#include <iostream> | |
int main() { | |
return 0; | |
} | |
EOF | |
} | |
function minimake_gf() { | |
test -f "${1%.cpp}.cpp" && | |
head "${1%.cpp}.cpp" | sed -nr 's|^// flags:(.*)$|\1|p'; | |
} | |
function h() { | |
cat<<EOF | |
Minimake environment | |
This utility is simple. It works only for .cpp files. | |
It is a replacement for "make file" utility when pure | |
defaults are not enough (e.g. you use C++11). | |
Experimental feature -- you can use additional flags | |
in compilation by typing in the first line of your cpp: | |
// flags: <flags go here> | |
Usage: | |
<cmd> file[.cpp] | |
Minimake commands: | |
b : compile given .cpp file | |
m : compile given .cpp file if changed | |
x : make and execute cpp file | |
d : debug with gdb (gdb command tip: run < file.in) | |
t : test with file.in as stdin | |
r : release build; compile with optimization, ignore stderr | |
c : clean; remove compiled program | |
e : open given .cpp file in editor | |
n : create new .cpp file | |
h : this message | |
To cleanup after minimake run: | |
mm_clean | |
To refresh environment after changes: | |
mm_refresh | |
To provide or reset default target for single letter commands use: | |
mm_set file[.cpp] | |
mm_unset | |
To save shell state for later/other console use: | |
mm_save | |
mm_sync or source .minimake.state | |
Notice these functions can create .minimake.state file in current directory. | |
Your minimake definition is in ${minimake_SOURCE}. | |
This value is available under \$minimake_SOURCE. | |
EOF | |
} | |
function minimake_init() { | |
if [ "${PS1##(mm}" = "${PS1}" ]; then | |
minimake_pre_PS1="$PS1" | |
PS1="(mm\${minimake_DEFAULT:+: \${minimake_DEFAULT}}) ${PS1}" | |
fi | |
if [ "$minimake_SOURCE" ]; then | |
mm_refresh | |
else | |
echo "Minimake environment initialized. Run h command for help." | |
fi | |
} | |
function mm_refresh() { | |
local load="${minimake_SOURCE}" | |
local default="${minimake_DEFAULT}" | |
mm_clean | |
source "${load}" | |
[ "$default" ] && mm_set "$default" | |
} | |
function mm_set() { minimake_DEFAULT="${1}";} | |
function mm_unset() { unset minimake_DEFAULT;} | |
function mm_save() { | |
echo "source '$minimake_SOURCE'" > .minimake.state | |
[ "$rosalind_SOURCE" ] && | |
echo "source '$rosalind_SOURCE'" >> .minimake.state | |
[ "$minimake_DEFAULT" ] && | |
echo "mm_set '$minimake_DEFAULT'" >> .minimake.state | |
} | |
function mm_sync() { | |
[ -f .minimake.state ] && source .minimake.state | |
} | |
function mm_clean() { | |
PS1="${minimake_pre_PS1}" | |
unalias g++ | |
unset -f b m x ot br mr bd md d t r c h e | |
unset -f minimake_init | |
unset -f minimake_gf | |
unset -f mm_clean | |
unset -f mm_refresh | |
unset -f mm_set | |
unset -f mm_unset | |
unset -f mm_save | |
unset -f mm_sync | |
unset -v minimake_pre_PS1 minimake_SOURCE minimake_DEFAULT | |
} | |
minimake_init | |
minimake_SOURCE="$(readlink -f ${BASH_SOURCE[0]})" | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment