Base/build.bash

113 lines
2.8 KiB
Bash
Executable File

#!/usr/bin/env bash
# Get the absolute repository root path and go there
cd $(dirname "$(pwd)/$0")
REPOSITORY_FOLDER=$(pwd)
# Configuration Values start here
PROJECT_NAME="librr"
GCC_ARGUMENTS="-std=c11 -Wall"
MAIN_OBJECTS_FOLDER="$REPOSITORY_FOLDER/.build/objects"
# End of configuration values
# Constants not meant for configuration
CONFIG_FILE_INCLUDE_PATHS="build-config/include_paths.txt"
MODULE_LIST_PATH="build-config/modules.txt"
function get_include_path_configuration {
MODULE_NAME=$1
INCLUDE_CONFIG_PATH="$REPOSITORY_FOLDER/$MODULE_NAME/$CONFIG_FILE_INCLUDE_PATHS"
INCLUDE_STATEMENTS="-I $REPOSITORY_FOLDER/$MODULE_NAME/inc-c/ -I $REPOSITORY_FOLDER/$MODULE_NAME/exports/"
if [[ ! -f $INCLUDE_CONFIG_PATH ]]
then
return
fi
for LINE in $(cat $INCLUDE_CONFIG_PATH)
do
INCLUDE_STATEMENTS="$INCLUDE_STATEMENTS -I $REPOSITORY_FOLDER/$LINE"
done
}
# Arguments:
# 1) Module name / folder name in project root (May not include slashes)
function compile_single_module() {
MODULE_NAME=$1
ENTRY_WORKING_DIR=$(pwd)
printf "\n\n\n"
echo "|================> Module Separator <================|"
echo "> Name: $MODULE_NAME "
echo "> Path: $REPOSITORY_FOLDER/$MODULE_NAME"
echo " "
get_include_path_configuration $MODULE_NAME
MODULE_OBJECTS_FOLDER="$MAIN_OBJECTS_FOLDER/$MODULE_NAME"
mkdir -p $MODULE_OBJECTS_FOLDER
cd $REPOSITORY_FOLDER/$1
cd src-c/
for SOURCE_FILE in $(find . -mindepth 1)
do
# Create all subfolders of this module's source folder
if [[ -d $SOURCE_FILE ]] then
mkdir -p $MODULE_OBJECTS_FOLDER/$SOURCE_FILE
continue
fi
# If this is not a regular file, quietly ignore it.
if [[ ! -f $SOURCE_FILE ]] then
continue
fi
# Cut out the ./ at the front of the path
SOURCE_FILE=$(echo $SOURCE_FILE | cut -c 3-)
echo "==> File: $SOURCE_FILE"
gcc -c $GCC_ARGUMENTS -o $MODULE_OBJECTS_FOLDER/$SOURCE_FILE.o $SOURCE_FILE $INCLUDE_STATEMENTS
done
OBJECT_FILES=$(find $MODULE_OBJECTS_FOLDER -mindepth 1 -type f)
ar -rvs $REPOSITORY_FOLDER/.build/$PROJECT_NAME-$MODULE_NAME.a $OBJECT_FILES
cd $ENTRY_WORKING_DIR
}
function compile_all_modules() {
for MODULE_PATH in $(cat $MODULE_LIST_PATH)
do
compile_single_module $MODULE_PATH
done
}
case $1 in
"release")
GCC_ARGUMENTS="$GCC_ARGUMENTS -O3"
compile_all_modules
;;
"small")
GCC_ARGUMENTS="$GCC_ARGUMENTS -Os"
compile_all_modules
;;
"debug")
GCC_ARGUMENTS="$GCC_ARGUMENTS -p -g3 -Wextra"
compile_all_modules
;;
*)
echo "Please choose one of the following profiles:"
echo "[ release | small | debug ]"
;;
esac