From 46be632798ce01a88e99099a3eb8789bad087b18 Mon Sep 17 00:00:00 2001 From: PHO Date: Wed, 13 Jun 2012 16:11:43 +0900 Subject: [PATCH] Split the script to several files. --- .gitignore | 2 + Build | 193 ----------------------------------------- Makefile | 25 ++++++ lib/detectNumCPUs.bash | 13 +++ lib/run.bash | 18 ++++ lib/setPath.bash | 28 ++++++ src/functions.bash | 44 ++++++++++ src/main.bash | 62 +++++++++++++ src/preamble.bash | 25 ++++++ src/resource.bash | 10 +++ 10 files changed, 227 insertions(+), 193 deletions(-) create mode 100644 .gitignore delete mode 100755 Build create mode 100644 Makefile create mode 100644 lib/detectNumCPUs.bash create mode 100644 lib/run.bash create mode 100644 lib/setPath.bash create mode 100644 src/functions.bash create mode 100644 src/main.bash create mode 100644 src/preamble.bash create mode 100644 src/resource.bash diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..766307c --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +Build +Build.tmp diff --git a/Build b/Build deleted file mode 100755 index 023e7dc..0000000 --- a/Build +++ /dev/null @@ -1,193 +0,0 @@ -#!/usr/bin/env bash -# ----------------------------------------------------------------------------- -# Build automation script designed to work with autotools. -# ----------------------------------------------------------------------------- -set -e - -# ----------------------------------------------------------------------------- -# Functions used by Build.rc -# -declare prefix -function setPrefix () { - if (( $# != 1 )); then - echo "Usage: $0 PREFIX" >&2 - return 1 - fi - prefix="$1" -} - -function setPath () { - if (( $# == 0 )); then - echo "Usage: $0 VAR [PATH, ...]" >&2 - return 1 - fi - - local -r var="$1" - local paths=("${@:2}") - - case ${#paths[@]} in - 0) - unset $var;; - 1) - export $var="$paths";; - *) - export $var="${paths[0]}" - local path - for path in "${paths[@]:1}"; do - local val=$(eval echo \$$var) - export $var="$val:$path" - done - esac - - # Special case for PATH: we prepend "$prefix/bin" to it. - if [[ $var = "PATH" ]]; then - PATH="$prefix/bin:$PATH" - fi -} - -declare -a configArgs -function setConfigArgs () { - configArgs=( \ - --prefix="$prefix" \ - PATH="$PATH" \ - "$@" \ - ) -} - -declare buildTarget -function setBuildTarget () { - if (( $# != 1 )); then - echo "Usage: $0 TARGET" >&2 - return 1 - fi - buildTarget="$1" -} - -declare docDirectory -function setDocDirectory () { - if (( $# != 1 )); then - echo "Usage: $0 DIRECTORY" >&2 - return 1 - fi - docDirectory="$1" -} - -declare docTarget -function setDocTarget () { - if (( $# != 1 )); then - echo "Usage: $0 TARGET" >&2 - return 1 - fi - docTarget="$1" -} - -# ----------------------------------------------------------------------------- -# Setting variables -# -setPrefix "/usr/local" -setPath PATH "/usr/bin" "/bin" -setConfigArgs -setBuildTarget "all" -setDocDirectory "." -setDocTarget "all" - -if [ -f "Build.rc" ]; then - . "Build.rc" -fi - -# ----------------------------------------------------------------------------- -# Main -# -function run () { - if (( $# == 0 )); then - echo "run: Usage: run CMD [ARG...]" >&2 - return 1 - fi - - if [[ -t 1 ]]; then - # Bold + Green - echo -ne "\e[1;32m" - echo "$@" - echo -ne "\e[0m" - else - echo "$@" - fi - - $@ -} - -function detectNumCPUs () { - if sysctl -n "hw.ncpu" 2>/dev/null; then - # This works for most BSDs. - : - elif grep -qF processor /proc/cpuinfo 2>/dev/null; then - # Linux sucks... - grep -cF processor /proc/cpuinfo - else - echo "WARNING: I don't know how to detect the number of processors on this platform." >&2 - echo 1 - fi -} - -function runMake () { - run gmake -w -j $(detectNumCPUs) $@ -} - -function configure () { - if [[ ! -f configure.ac && ! -f configure.in ]]; then - echo "ERROR: Neither configure.ac nor configure.in exists in the current directory." >&2 - return 1 - fi - - if [[ ! -f configure ]]; then - run autoreconf -v -i -f - fi - - if [[ ! -f "_build/Makefile" ]]; then - run mkdir -p _build - run pushd _build - run ../configure "${configArgs[@]}" - run popd - fi -} - -function build () { - configure - runMake -C "_build" $buildTarget -} - -function doc () { - configure - runMake -C "_build/$docDirectory" $docTarget -} - -function check () { - build - runMake -C "_build" check -} - -function clean () { - run rm -rf "_build" -} - -function dist () { - configure - runMake -C "_build" dist -} - -function install () { - build - runMake -C "_build" install -} - -case "$1" in - ""|"build") build ;; - "doc" ) doc ;; - "check" ) check ;; - "clean" ) clean ;; - "dist" ) dist ;; - "install" ) install;; - *) - echo "Usage: $0 [build | doc | check | clean | dist | install]" >&2 - exit 1 -esac diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..a85cff8 --- /dev/null +++ b/Makefile @@ -0,0 +1,25 @@ +SOURCES = \ + src/preamble.bash \ + lib/detectNumCPUs.bash \ + lib/run.bash \ + lib/setPath.bash \ + src/functions.bash \ + src/resource.bash \ + src/main.bash \ + $(NULL) + +all: Build + +clean: + rm -f Build Build.tmp + +Build: $(SOURCES) + rm -f $@.tmp + for i in $(SOURCES); do \ + cat $$i >> $@.tmp; \ + echo >> $@.tmp; \ + done + head -q -n -1 $@.tmp > $@ + rm -f $@.tmp + +.PHONY: all clean \ No newline at end of file diff --git a/lib/detectNumCPUs.bash b/lib/detectNumCPUs.bash new file mode 100644 index 0000000..7720247 --- /dev/null +++ b/lib/detectNumCPUs.bash @@ -0,0 +1,13 @@ +function detectNumCPUs () { + if sysctl -n "hw.ncpu" 2>/dev/null; then + # This works for most BSDs. + : + elif grep -qF processor /proc/cpuinfo 2>/dev/null; then + # Linux sucks... + grep -cF processor /proc/cpuinfo + else + echo -n "WARNING: $0 doesn't know how to detect the number of" >&2 + echo -n "processors on this platform. Assuming we have just one..." >&2 + echo 1 + fi +} diff --git a/lib/run.bash b/lib/run.bash new file mode 100644 index 0000000..b68dc33 --- /dev/null +++ b/lib/run.bash @@ -0,0 +1,18 @@ +function run () { + if (( $# == 0 )); then + echo "run: Usage: run CMD [ARG...]" >&2 + return 1 + fi + + # Is the /dev/stdout opened to a tty? + if [[ -t 1 ]]; then + # Bold + Green + echo -ne "\e[1;32m" + echo "$@" + echo -ne "\e[0m" + else + echo "$@" + fi + + $@ +} diff --git a/lib/setPath.bash b/lib/setPath.bash new file mode 100644 index 0000000..5158dd6 --- /dev/null +++ b/lib/setPath.bash @@ -0,0 +1,28 @@ +function setPath () { + if (( $# == 0 )); then + echo "Usage: $0 VAR [PATH, ...]" >&2 + return 1 + fi + + local -r var="$1" + local paths=("${@:2}") + + case ${#paths[@]} in + 0) + unset $var;; + 1) + export $var="$paths";; + *) + export $var="${paths[0]}" + local path + for path in "${paths[@]:1}"; do + local val=$(eval echo \$$var) + export $var="$val:$path" + done + esac + + # THINKME: Special case for PATH: we prepend "$prefix/bin" to it. + if [[ $var = "PATH" ]]; then + PATH="$prefix/bin:$PATH" + fi +} diff --git a/src/functions.bash b/src/functions.bash new file mode 100644 index 0000000..fe7f3df --- /dev/null +++ b/src/functions.bash @@ -0,0 +1,44 @@ +declare prefix +function setPrefix () { + if (( $# != 1 )); then + echo "Usage: $0 PREFIX" >&2 + return 1 + fi + prefix="$1" +} + +declare -a configArgs +function setConfigArgs () { + configArgs=( \ + --prefix="$prefix" \ + PATH="$PATH" \ + "$@" \ + ) +} + +declare buildTarget +function setBuildTarget () { + if (( $# != 1 )); then + echo "Usage: $0 TARGET" >&2 + return 1 + fi + buildTarget="$1" +} + +declare docDirectory +function setDocDirectory () { + if (( $# != 1 )); then + echo "Usage: $0 DIRECTORY" >&2 + return 1 + fi + docDirectory="$1" +} + +declare docTarget +function setDocTarget () { + if (( $# != 1 )); then + echo "Usage: $0 TARGET" >&2 + return 1 + fi + docTarget="$1" +} diff --git a/src/main.bash b/src/main.bash new file mode 100644 index 0000000..1bb450c --- /dev/null +++ b/src/main.bash @@ -0,0 +1,62 @@ +function runMake () { + run gmake -w -j $(detectNumCPUs) $@ +} + +function configure () { + if [[ ! -f configure.ac && ! -f configure.in ]]; then + echo "ERROR: Neither configure.ac nor configure.in exists in the current directory." >&2 + return 1 + fi + + if [[ ! -f configure ]]; then + run autoreconf -v -i -f + fi + + if [[ ! -f "_build/Makefile" ]]; then + run mkdir -p _build + run pushd _build + run ../configure "${configArgs[@]}" + run popd + fi +} + +function build () { + configure + runMake -C "_build" $buildTarget +} + +function doc () { + configure + runMake -C "_build/$docDirectory" $docTarget +} + +function check () { + build + runMake -C "_build" check +} + +function clean () { + run rm -rf "_build" +} + +function dist () { + configure + runMake -C "_build" dist +} + +function install () { + build + runMake -C "_build" install +} + +case "$1" in + ""|"build") build ;; + "doc" ) doc ;; + "check" ) check ;; + "clean" ) clean ;; + "dist" ) dist ;; + "install" ) install;; + *) + echo "Usage: $0 [build | doc | check | clean | dist | install]" >&2 + exit 1 +esac diff --git a/src/preamble.bash b/src/preamble.bash new file mode 100644 index 0000000..45ef638 --- /dev/null +++ b/src/preamble.bash @@ -0,0 +1,25 @@ +#!/usr/bin/env bash +#-----------------------------------------------------------------------------# +# THIS SCRIPT IS AUTOMATICALLY GENERATED FROM SEVERAL SOURCE FILES. # +# DO NOT EDIT THIS BY HAND, OR YOU WILL REGRET IT LATER. # +#-----------------------------------------------------------------------------# +# # +# "Build" -- an automation script designed to work with autotools. # +# # +# Author / Maintainer: # +# PHO # +# # +# Master repository: # +# git://git.cielonegro.org/autobuild.git # +# # +# License: # +# Public Domain # +# # +# Bug tracker: # +# Not available currently. # +# # +# Please report any bugs, feature requests, and pull requests (the # +# most preferred!) to the maintainer presented above. # +# # +#-----------------------------------------------------------------------------# +set -e diff --git a/src/resource.bash b/src/resource.bash new file mode 100644 index 0000000..a050006 --- /dev/null +++ b/src/resource.bash @@ -0,0 +1,10 @@ +setPrefix "/usr/local" +setPath PATH "/usr/bin" "/bin" +setConfigArgs +setBuildTarget "all" +setDocDirectory "." +setDocTarget "all" + +if [ -f "Build.rc" ]; then + . "Build.rc" +fi -- 2.40.0