first commit

This commit is contained in:
Noah Swerhun 2021-12-23 22:34:22 -06:00
commit 47a23d0198
2 changed files with 133 additions and 0 deletions

4
.gitignore vendored Normal file
View file

@ -0,0 +1,4 @@
.makefile
foo
obj/
src/

129
cbuild.sh Executable file
View file

@ -0,0 +1,129 @@
#!/bin/sh
# --- USER CONFIG ---
CC="gcc"
CFLAGS="-Wall -Wpedantic -std=c99"
LDFLAGS=""
LDLIBS=""
TARGET="foo"
SRCDIR="src"
OBJDIR="obj"
MAKEFILE=".makefile"
# -------------------
PROG_COMMAND="./cbuild.sh __progress__"
SRC="$(find ${SRCDIR} -name '*\.c')"
OBJ="$(find ${SRCDIR} -name '*\.c' |
sed "s/${SRCDIR}\//${OBJDIR}\//" |
sed "s/\.c$/.o/" |
tr '\n' ' ')"
srcnum="$(find ${SRCDIR} -name '*\.c' -exec printf %c {} + | wc -c)"
export_vars() {
export CC
export CFLAGS
export LDFLAGS
export LDLIBS
export TARGET
export OBJ
export PROG_COMMAND
}
info() {
printf "\e[34;1m[*]\e[21m %s\e[0m\n" "$@"
}
err() {
printf "\e[31;1m[!]\e[21m %s\e[0m\n" "$@"
}
gen_makefile() {
info "Generating makefile..."
[ -f "${MAKEFILE}" ] && chmod +w "${MAKEFILE}"
cat > "${MAKEFILE}" <<'EOF'
##########################
# GENERATED BY cbuild.sh #
# DO NOT MODIFY BY HAND #
##########################
.POSIX:
.SUFFIXES:
$(TARGET): $(OBJ)
@$(PROG_COMMAND) link $(TARGET)
@$(CC) $(LDFLAGS) -o $(TARGET) $(OBJ) $(LDLIBS)
EOF
i=1
echo "${SRC}" | while read srcname; do
objname="$(echo "${srcname}" |
sed "s/${SRCDIR}\//${OBJDIR}\//" |
sed "s/\.c$/.o/")"
gcc -MM -MT "${objname}" "${srcname}" >> "${MAKEFILE}"
printf "\t@%s object %s\n" \
'$(PROG_COMMAND)' "${objname}" >> "${MAKEFILE}"
printf "\t@%s %s -o %s -c %s\n" '$(CC)' '$(CFLAGS)' \
"${objname}" "${srcname}" >> "${MAKEFILE}"
i=$((${i}+1))
done
chmod -w "${MAKEFILE}"
}
build() {
gen_makefile
[ ! -d "${OBJDIR}" ] && mkdir "${OBJDIR}"
export_vars
make -f "${MAKEFILE}" -e "${TARGET}" -n | grep "^${CC}" | wc -l > .cbuild_prog.tmp
info "Beginning build..."
make -f "${MAKEFILE}" -e "${TARGET}" &&
info "Build SUCCESSFUL" ||
err "Build FAILURE"
rm .cbuild_prog.tmp
}
clean() {
rm -rf "${OBJDIR}"
chmod +w "${MAKEFILE}"
rm "${TARGET}" "${MAKEFILE}"
}
run() {
build
printf '\e[36;1mRunning\e[0m %s\n' "./${TARGET}"
"./${TARGET}"
}
dry_run() {
gen_makefile
export_vars
make -f "${MAKEFILE}" -e "${TARGET}" -n
}
__progress__() {
ntargets=$(cat .cbuild_prog.tmp)
export_vars
rtargets="$(make -f "${MAKEFILE}" -e "${TARGET}" -n | grep "^${CC}" | wc -l)"
targetno=$((${ntargets} - ${rtargets} + 1))
if [ "${1}" = "object" ]; then
printf '\e[35;1m[%3d/%d]\e[0m ' "${targetno}" "${ntargets}"
printf '\e[32mBuilding\e[0m '
elif [ "${1}" = "link" ]; then
printf '\e[36;1m[%3d/%d]\e[0m ' "${targetno}" "${ntargets}"
printf '\e[36;1mLinking\e[0m '
fi
echo "${2}"
}
case $1 in
build) build;;
clean) clean;;
buildcn) clean ; build;;
generate) gen_makefile && info "Done";;
run) run;;
dryrun) dry_run;;
__progress__) __progress__ "${2}" "${3}";;
esac