You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

119 lines
3.0 KiB
Bash

#!/bin/bash
# TODO: Make it more friendly
# Check if the environment file existed to configure everything
if [ ! -f .env ]; then
echo "Voltaire doesn't seem to have been configured beforehand"
echo ""
printf "This is fine, but to be built properly, it needs to know where "
echo "the Cytoplasm library and include files are."
printf 'Cytoplasm library path(and runtime stub)? '
read CYTOPLASM_LIB
printf 'Cytoplasm include path? '
read CYTOPLASM_INC
printf "CYTOPLASM_LIB=%s\n" "$CYTOPLASM_LIB" > .env
printf "CYTOPLASM_INC=%s\n" "$CYTOPLASM_INC" >> .env
else
# Load the env file. This stores information like the Cytoplasm path
source .env
fi
SOURCE_DIR=src
OBJECT_DIR=build
# TODO: Make this editable
DEFINES="-DVOLTAIRE_VERSION=\"$(git rev-parse --short HEAD)\""
INCLUDE="-I$CYTOPLASM_INC -I$SOURCE_DIR/include $DEFINES"
CFLAGS="$INCLUDE -g -ggdb"
CC=cc
echo "Voltaire build utility"
echo "======================"
# mod_time: Stolen straight from Telodendria's tools/bin/td
mod_time() {
if [ -n "$1" ] && [ -f "$1" ]; then
case "$(uname)" in
Linux | CYGWIN_NT* | Haiku)
stat -c %Y "$1"
;;
*BSD | DragonFly | Minix)
stat -f %m "$1"
;;
*)
# Platform unknown, force rebuilding the whole
# project every time.
echo "0"
;;
esac
else
echo "0"
fi
}
# Usage: modified [source file]
# Checks if the time for the source file is higher than the time for the
# object file
modified() {
SOURCE_MOD=$(mod_time "$SOURCE_DIR/$1.c")
OBJECT_MOD=$(mod_time "$OBJECT_DIR/$1.o")
if [ $SOURCE_MOD -ge $OBJECT_MOD ]; then
echo "1"
else
echo "0"
fi
}
# Get all source files
compile_build() {
SOURCE_FILES=$(find $SOURCE_DIR -type f -name "*.c")
TMP_FILES=$(find $OBJECT_DIR -type f -name "*.o")
OBJECT_FILES=""
CLEAN_BUILD=1
if [ -z "$TMP_FILES" ]; then
CLEAN_BUILD=0
else
for FILE in $TMP_FILES; do
# Does the file has it's C counterpart?
BASENAME=${FILE#$OBJECT_DIR/}
BASENAME=$SOURCE_DIR/${BASENAME%.o}.c
if [ -f $BASENAME ]; then
OBJECT_FILES="$OBJECT_FILES $FILE"
fi
done
fi
echo $CYTOPLASM_INC
for SOURCE_FILE in $SOURCE_FILES; do
BASENAME=${SOURCE_FILE#$SOURCE_DIR/}
MODIFIED=$(modified ${BASENAME%.c})
if [ $MODIFIED -eq 1 ]; then
echo "CC $BASENAME"
mkdir -p $OBJECT_DIR/$(dirname $BASENAME)
$CC -c $CFLAGS $SOURCE_FILE -o $OBJECT_DIR/${BASENAME%.c}.o
[ $CLEAN_BUILD -eq 0 ] && OBJECT_FILES="$OBJECT_FILES $OBJECT_DIR/${BASENAME%.c}.o"
fi
done
cc $OBJECT_FILES $CYTOPLASM_LIB/cytoplasm.o -L$CYTOPLASM_LIB -lcytoplasm -o voltaire
}
compile_clean() {
rm -rf voltaire $OBJECT_DIR/*
}
if [ -z $1 ]; then
compile_build
else
compile_$1
fi