From 4ac9f9e86431080dc9b519dbfb4635331cf0c526 Mon Sep 17 00:00:00 2001 From: Kevin Day Date: Tue, 28 Feb 2012 19:09:29 -0600 Subject: [PATCH] Add Script: generate This is the generic bootstrap build scripts for the FSS project. --- build/scripts/generate.sh | 324 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 324 insertions(+) create mode 100644 build/scripts/generate.sh diff --git a/build/scripts/generate.sh b/build/scripts/generate.sh new file mode 100644 index 0000000..54e59f4 --- /dev/null +++ b/build/scripts/generate.sh @@ -0,0 +1,324 @@ +#!/bin/bash +# license: lgpl-2.1 +# programmer: Kevin Day +# +# The purpose of this script is to provide a simple bootstrap tool to compile and install any part of the FLL project. +# The dependencies of this script are: bash, grep, and sed. + +generate_main(){ + local public_name="Simple FLL Project Make Script" + local system_name=generate + local called_name=$(basename $0) + local version=0.2.0 + + local grab_next= + local do_color=normal + local do_help= + local i=0 + local p= + local t=0 + + local c_reset="\\033[0m" + local c_title="\\033[1;33m" + local c_error="\\033[1;31m" + local c_warning="\\033[0;33m" + local c_highlight="\\033[1;32m" + local c_notice="\\033[0;01m" + local c_important="\\033[0;32m" + local c_subtle="\\033[1;30m" + local c_prefix="\\" + + local variables= + local settings_file=data/build/settings + local operation= + local path_build=build/ + + if [[ $# -gt 0 ]] ; then + t=$# + + while [[ $i -lt $t ]] ; do + let i=$i+1 + p=${!i} + + if [[ $grab_next == "" ]] ; then + if [[ $p == "-h" || $p == "--help" ]] ; then + do_help=yes + elif [[ $p == "+n" || $p == "++no_color" ]] ; then + do_color=none + elif [[ $p == "+l" || $p == "++light" ]] ; then + do_color=light + elif [[ $p == "+v" || $p == "++version" ]] ; then + echo $version + return + elif [[ $p == "-b" || $p == "--build" ]] ; then + grab_next=path_build + elif [[ $p == "-s" || $p == "--settings" ]] ; then + grab_next=settings_file + elif [[ $operation == "" ]] ; then + operation=$p + else + operation=fail + fi + else + if [[ $grab_next == "path_build" ]] ; then + path_build=$(echo $p | sed -e 's|^//*|/|' -e 's|/*$|/|') + elif [[ $grab_next == "settings_file" ]] ; then + settings_file=$(echo $p | sed -e 's|^//*|/|') + fi + + grab_next= + fi + done + + p= + fi + + generate_handle_colors + + if [[ $do_help == "yes" ]] ; then + generate_help + generate_cleanup + exit 0 + fi + + generate_load_settings + + if [[ $operation == "build" && ! -f ${path_build}.built ]] ; then + if [[ ! -f ${path_build}.prepared ]] ; then + generate_prepare_build + fi + + generate_operation_build + elif [[ $operation == "clean" ]] ; then + generate_operation_clean + else + echo -e "${c_error}ERROR: only one operation may be specified at a time.$c_reset" + fi +} + +generate_handle_colors(){ + if [[ $do_color == "light" ]] ; then + c_error="\\033[1;31m" + c_warning="\\033[0;31m" + c_title="\\033[1;34m" + c_highlight="\\033[0;34m" + c_notice="\\033[0;01m" + c_important="\\033[0;35m" + elif [[ $do_color == "none" ]] ; then + c_reset= + c_title= + c_error= + c_warning= + c_highlight= + c_notice= + c_important= + c_subtle= + c_prefix= + fi +} + +generate_help(){ + echo + echo -e "${c_title}$public_name$c_reset" + echo -e " ${c_notice}Version $version$c_reset" + echo + echo -e "$c_highlight$system_name$c_reset $c_notice<${c_reset}operation$c_notice>$c_reset" + echo -e " ${c_important}build${c_reset} Build or compile the code" + echo -e " ${c_important}clean${c_reset} Delete all build files" + echo + echo -e "${c_highlight}Options:$c_reset" + echo -e " -${c_important}h$c_reset, --${c_important}help$c_reset Print this help screen" + echo -e " +${c_important}l$c_reset, ++${c_important}light$c_reset Use color modes that show up better on light backgrounds" + echo -e " +${c_important}n$c_reset, ++${c_important}no_color$c_reset Do not use color" + echo -e " +${c_important}v$c_reset, ++${c_important}version$c_reset Print the version number of this program" + echo + echo -e "${c_highlight}Generate Options:$c_reset" + echo -e " -${c_important}b$c_reset, --${c_important}build${c_reset} Specify a custom build directory" + echo -e " -${c_important}s$c_reset, --${c_important}settings${c_reset} Specify a custom build settings file" + echo +} + +generate_id(){ + local name=$1 + + case $name in + "project_name") echo -n 0;; + "project_level") echo -n 1;; + "version_major") echo -n 2;; + "version_minor") echo -n 3;; + "version_micro") echo -n 4;; + "build_compiler") echo -n 5;; + "build_libraries") echo -n 6;; + "build_sources_library") echo -n 7;; + "build_sources_program") echo -n 8;; + "build_sources_headers") echo -n 10;; + "build_shared") echo -n 11;; + "flags_all") echo -n 12;; + "flags_shared") echo -n 13;; + "flags_static") echo -n 14;; + "flags_library") echo -n 15;; + "flags_program") echo -n 16;; + esac +} + +generate_load_settings(){ + local failure= + local i= + + if [[ $settings_file == "" ]] ; then + echo -e "${c_error}ERROR: no settings file has been defined.$c_reset" + failure=1 + elif [[ ! -f $settings_file ]] ; then + echo -e "${c_error}ERROR: no settings file $c_notice$settings_file$c_error could not be found or is not a valid file.$c_reset" + failure=1 + fi + + if [[ $failure != "" ]] ; then + generate_cleanup + exit $failure + fi + + for i in project_name project_level version_major version_minor version_micro build_compiler build_libraries build_sources_library build_sources_program build_sources_headers build_sources_headers build_shared flags_all flags_shared flags_static flags_library flags_program ; do + variables[$(generate_id $i)]=$(grep -s -o "^[[:space:]]*$i\>.*$" $settings_file | sed -e "s|^[[:space:]]*$i\>||" -e 's|^[[:space:]]*||') + done +} + +generate_prepare_build(){ + local failure= + local level=${variables[$(generate_id project_level)]} + + mkdir -vp ${path_build}{includes,programs,libraries} || failure=1 + + if [[ $failure == "" && $level != "" ]] ; then + mkdir -vp ${path_build}includes/level_$level || failure=1 + fi + + if [[ $failure == "" ]] ; then + for i in $headers ; do + ln -vsf ../../$i ${path_build}includes/level_$level/ + + if [[ $? != 0 ]] ; then + failure=1 + break + fi + done + fi + + if [[ $failure != "" ]] ; then + generate_cleanup + exit $failure + fi + + touch ${path_build}.prepared +} + +generate_operation_build(){ + local failure= + local name=${variables[$(generate_id project_name)]} + local level=${variables[$(generate_id project_level)]} + local major=${variables[$(generate_id version_major)]} + local minor=${variables[$(generate_id version_minor)]} + local micro=${variables[$(generate_id version_micro)]} + local compiler=${variables[$(generate_id build_compiler)]} + local arguments="-I${path_build}includes ${variables[$(generate_id flags_all)]} ${variables[$(generate_id build_libraries)]}" + local shared=${variables[$(generate_id build_shared)]} + local sources_library=${variables[$(generate_id build_sources_library)]} + local sources_program=${variables[$(generate_id build_sources_program)]} + local sources_headers=${variables[$(generate_id build_sources_headers)]} + local sources= + local i= + + if [[ $shared == "yes" ]] ; then + if [[ $sources_library != "" ]] ; then + for i in $sources_library ; do + sources="${sources}sources/c/$i " + done + + echo $compiler $arguments ${variables[$(generate_id flags_shared)]} ${variables[$(generate_id flags_library)]} $sources -shared -Wl,-soname,lib$name.so.$major -o ${path_build}libraries/lib$name.so.$major.$minor.$micro + $compiler $arguments ${variables[$(generate_id flags_shared)]} ${variables[$(generate_id flags_library)]} $sources -shared -Wl,-soname,lib$name.so.$major -o ${path_build}libraries/lib$name.so.$major.$minor.$micro || failure=1 + + if [[ $failure == "" ]] ; then + ln -vsf lib$name.so.$major.$minor.$micro ${path_build}libraries/lib$name.so.$major || failure=1 + ln -vsf lib$name.so.$major ${path_build}libraries/lib$name.so || failure=1 + fi + fi + + if [[ $failure == "" && $sources_program != "" ]] ; then + sources= + for i in $sources_program ; do + sources="${sources}sources/c/$i " + done + + echo $compiler $arguments ${variables[$(generate_id flags_shared)]} ${variables[$(generate_id flags_program)]} $sources -o ${path_build}programs/$name + $compiler $arguments ${variables[$(generate_id flags_shared)]} ${variables[$(generate_id flags_program)]} $sources -o ${path_build}programs/$name || failure=1 + fi + else + if [[ $sources_library != "" ]] ; then + for i in $sources_library ; do + sources="${sources}sources/c/$i " + done + + echo $compiler $arguments ${variables[$(generate_id flags_static)]} ${variables[$(generate_id flags_library)]} $sources -static -o ${path_build}libraries/lib$name.a + $compiler $arguments ${variables[$(generate_id flags_static)]} ${variables[$(generate_id flags_library)]} $sources -static -o ${path_build}libraries/lib$name.a || failure=1 + fi + + if [[ $failure == "" && $sources_program != "" ]] ; then + sources= + for i in $sources_program ; do + sources="${sources}sources/c/$i " + done + + echo $compiler $arguments ${variables[$(generate_id flags_static)]} ${variables[$(generate_id flags_program)]} $sources -static -o ${path_build}programs/$name + $compiler $arguments ${variables[$(generate_id flags_static)]} ${variables[$(generate_id flags_program)]} $sources -static -o ${path_build}programs/$name || failure=1 + fi + fi + + if [[ $failure == "" && $sources_headers != "" ]] ; then + for i in $sources_headers ; do + cp -vf sources/c/$i ${path_build}includes/level_$level/ || failure=1 + done + fi + + if [[ $failure != "" ]] ; then + echo -e "${c_error}ERROR: failed to build.$c_reset" + generate_cleanup + exit $failure + fi + + touch ${path_build}.built +} + +generate_operation_clean(){ + local i= + + for i in ${path_build}{includes,programs,libraries} ; do + if [[ -e $i ]] ; then + rm -vRf $i + fi + done + + if [[ -f ${path_build}.prepared ]] ; then + rm -vf ${path_build}.prepared + fi + + if [[ -f ${path_build}.built ]] ; then + rm -vf ${path_build}.built + fi +} + +generate_cleanup(){ + unset generate_main + unset generate_handle_colors + unset generate_help + unset generate_id + unset generate_load_settings + unset generate_prepare_build + unset generate_operation_build + unset generate_operation_install + unset generate_operation_uninstall + unset generate_operation_clean + unset generate_cleanup +} + +generate_main $* +generate_cleanup -- 1.8.3.1