2014-06-10 22:25:35 +02:00
|
|
|
#!/usr/bin/env bash
|
2014-10-15 20:00:44 +02:00
|
|
|
#
|
2017-08-30 16:18:23 +02:00
|
|
|
# USAGE
|
2018-09-22 16:59:34 +02:00
|
|
|
# [targets='...'] [mpw_feature=0|1 ...] [CFLAGS='...'] [LDFLAGS='...'] ./build [-v|-d|-h|--] [cc arguments ...]
|
2017-08-30 16:18:23 +02:00
|
|
|
#
|
|
|
|
# By default, you should only need to run ./build
|
|
|
|
#
|
2018-09-22 16:59:34 +02:00
|
|
|
# -v: verbose mode, outputs state information and compiler commands.
|
|
|
|
# -d: debug build, modifies default build flags to produce binaries best suited for debugging.
|
|
|
|
# -h: show this usage information.
|
|
|
|
#
|
2017-08-30 16:18:23 +02:00
|
|
|
# You can customize the targets that are built using targets='...'. Use targets='all' to build all targets.
|
|
|
|
# By default, we only build the 'mpw' target.
|
|
|
|
# See targets_all for all possible targets as well as the features they support and require.
|
|
|
|
#
|
|
|
|
# Several features can be enabled or disabled using feature flags.
|
|
|
|
# See the Features section for an overview of the features, their default setting, their meaning and their dependencies.
|
|
|
|
# You will need to have each of the feature's dependencies installed for the build to succeed with that feature enabled.
|
|
|
|
#
|
|
|
|
# Finally, the C compiler can be tuned using CFLAGS, LDFLAGS and compiler arguments passed to the script.
|
2014-10-15 20:00:44 +02:00
|
|
|
#
|
|
|
|
# BUGS
|
|
|
|
# masterpassword@lyndir.com
|
|
|
|
#
|
|
|
|
# AUTHOR
|
|
|
|
# Maarten Billemont
|
|
|
|
#
|
2014-10-15 14:44:41 +02:00
|
|
|
cd "${BASH_SOURCE%/*}"
|
2014-10-16 04:17:49 +02:00
|
|
|
shopt -s extglob
|
2014-10-15 14:44:41 +02:00
|
|
|
set -e
|
2014-06-07 07:27:18 +02:00
|
|
|
|
2014-10-19 02:56:28 +02:00
|
|
|
|
|
|
|
### CONFIGURATION
|
2018-09-22 16:59:34 +02:00
|
|
|
verbose=0
|
|
|
|
|
|
|
|
# Options
|
|
|
|
while getopts :vdh opt; do
|
|
|
|
case $opt in
|
|
|
|
v) verbose=1 ;;
|
|
|
|
d) debug=1 ;;
|
|
|
|
h|?) sed -n '/^[^#]/q;p' "${BASH_SOURCE##*/}"; exit ;;
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
shift "$(( OPTIND - 1 ))"
|
|
|
|
|
|
|
|
# Targets to build
|
2017-08-30 16:18:23 +02:00
|
|
|
targets_all=(
|
|
|
|
mpw # C CLI version of Master Password (needs: mpw_sodium, optional: mpw_color, mpw_json).
|
|
|
|
mpw-bench # C CLI Master Password benchmark utility (needs: mpw_sodium).
|
|
|
|
mpw-tests # C Master Password algorithm test suite (needs: mpw_sodium, mpw_xml).
|
|
|
|
)
|
|
|
|
targets_default='mpw' # Override with: targets='...' ./build
|
2018-09-22 16:59:34 +02:00
|
|
|
targets=${targets[*]:-$targets_default}
|
2017-08-30 16:18:23 +02:00
|
|
|
|
2018-09-22 16:59:34 +02:00
|
|
|
# Features
|
2017-08-27 17:17:45 +02:00
|
|
|
mpw_sodium=${mpw_sodium:-1} # Implement crypto functions with sodium (depends on libsodium).
|
|
|
|
mpw_json=${mpw_json:-1} # Support JSON-based user configuration format (depends on libjson-c).
|
|
|
|
mpw_color=${mpw_color:-1} # Colorized identicon (depends on libncurses).
|
|
|
|
mpw_xml=${mpw_xml:-1} # XML parsing (depends on libxml2).
|
2014-11-21 14:06:29 +01:00
|
|
|
|
2018-09-22 16:59:34 +02:00
|
|
|
# Default build flags
|
|
|
|
cflags=( -O3 $CFLAGS ); unset CFLAGS
|
|
|
|
ldflags=( $LDFLAGS ); unset LDFLAGS
|
|
|
|
if (( debug )); then
|
|
|
|
cflags+=( -O0 -g )
|
|
|
|
fi
|
2017-04-08 20:25:54 +02:00
|
|
|
|
2018-09-22 16:59:34 +02:00
|
|
|
# Version
|
2017-11-06 21:38:30 +01:00
|
|
|
if { mpw_version=$(git describe --match '*-cli*' --long --dirty) || mpw_version=$(<VERSION); } 2>/dev/null; then
|
2017-08-13 17:02:05 +02:00
|
|
|
cflags+=( -D"MP_VERSION=$mpw_version" )
|
2017-08-08 06:00:14 +02:00
|
|
|
fi
|
2018-09-22 16:59:34 +02:00
|
|
|
echo "Current mpw source version ${mpw_version:-<unknown>}..."
|
|
|
|
|
|
|
|
# Meta
|
|
|
|
if (( verbose )); then
|
|
|
|
echo "mpw_sodium=${mpw_sodium}, mpw_json=${mpw_json}, mpw_color=${mpw_color}, mpw_xml=${mpw_xml}"
|
|
|
|
echo "CFLAGS: ${cflags[*]}"
|
|
|
|
echo "LDFLAGS: ${ldflags[*]}"
|
|
|
|
echo "targets: ${targets[*]}"
|
|
|
|
fi
|
2017-08-08 06:00:14 +02:00
|
|
|
|
2014-10-15 20:00:44 +02:00
|
|
|
|
2017-08-27 17:17:45 +02:00
|
|
|
### TARGET: MPW
|
2014-10-15 22:26:09 +02:00
|
|
|
mpw() {
|
2017-08-13 17:02:05 +02:00
|
|
|
# dependencies
|
2017-09-06 06:31:49 +02:00
|
|
|
use_mpw_sodium required
|
|
|
|
use_mpw_color optional
|
|
|
|
use_mpw_json optional
|
2014-10-16 04:17:49 +02:00
|
|
|
|
2017-08-13 17:02:05 +02:00
|
|
|
# target
|
2017-08-27 17:17:45 +02:00
|
|
|
cflags=(
|
2017-08-13 17:02:05 +02:00
|
|
|
"${cflags[@]}"
|
2017-04-08 20:25:54 +02:00
|
|
|
|
2017-03-21 19:07:40 +01:00
|
|
|
# mpw paths
|
2018-06-06 02:01:46 +02:00
|
|
|
-I"../core/src" -I"src"
|
2014-10-15 22:26:09 +02:00
|
|
|
)
|
2017-09-04 20:29:25 +02:00
|
|
|
ldflags=(
|
2017-08-13 17:02:05 +02:00
|
|
|
"${ldflags[@]}"
|
2014-10-15 22:26:09 +02:00
|
|
|
)
|
2017-08-13 17:02:05 +02:00
|
|
|
|
|
|
|
# build
|
2018-06-06 02:01:46 +02:00
|
|
|
cc "${cflags[@]}" "$@" \
|
2019-10-03 05:13:12 +02:00
|
|
|
"../core/src/base64.c" "../core/src/aes.c" "../core/src/mpw-algorithm.c" \
|
|
|
|
"../core/src/mpw-algorithm_v0.c" "../core/src/mpw-algorithm_v1.c" "../core/src/mpw-algorithm_v2.c" "../core/src/mpw-algorithm_v3.c" \
|
|
|
|
"../core/src/mpw-types.c" "../core/src/mpw-util.c" "../core/src/mpw-marshal-util.c" "../core/src/mpw-marshal.c" "src/mpw-cli-util.c" \
|
2018-06-06 02:01:46 +02:00
|
|
|
"${ldflags[@]}" "src/mpw-cli.c" -o "mpw"
|
2017-08-30 16:18:23 +02:00
|
|
|
echo "done! You can now run ./mpw-cli-tests, ./install or use ./$_"
|
2014-10-15 22:26:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-08-27 17:17:45 +02:00
|
|
|
### TARGET: MPW-BENCH
|
2014-10-15 22:26:09 +02:00
|
|
|
mpw-bench() {
|
2017-08-13 17:02:05 +02:00
|
|
|
# dependencies
|
2017-09-06 06:31:49 +02:00
|
|
|
use_mpw_sodium required
|
2014-10-16 04:17:49 +02:00
|
|
|
|
2017-08-13 17:02:05 +02:00
|
|
|
# target
|
2017-08-27 17:17:45 +02:00
|
|
|
cflags=(
|
2017-08-13 17:02:05 +02:00
|
|
|
"${cflags[@]}"
|
2017-04-08 20:25:54 +02:00
|
|
|
|
2017-03-21 19:07:40 +01:00
|
|
|
# mpw paths
|
2018-06-06 02:01:46 +02:00
|
|
|
-I"../core/src" -I"src"
|
2014-10-15 22:26:09 +02:00
|
|
|
)
|
2017-09-04 20:29:25 +02:00
|
|
|
ldflags=(
|
2017-08-13 17:02:05 +02:00
|
|
|
"${ldflags[@]}"
|
2014-10-15 22:26:09 +02:00
|
|
|
)
|
|
|
|
|
2017-08-13 17:02:05 +02:00
|
|
|
# build
|
2018-06-06 02:01:46 +02:00
|
|
|
cc "${cflags[@]}" "$@" \
|
2019-10-03 05:13:12 +02:00
|
|
|
"../core/src/base64.c" "../core/src/aes.c" "../core/src/mpw-algorithm.c" \
|
|
|
|
"../core/src/mpw-algorithm_v0.c" "../core/src/mpw-algorithm_v1.c" "../core/src/mpw-algorithm_v2.c" "../core/src/mpw-algorithm_v3.c" \
|
|
|
|
"../core/src/mpw-types.c" "../core/src/mpw-util.c" \
|
2018-06-06 02:01:46 +02:00
|
|
|
"${ldflags[@]}" "src/mpw-bench.c" -o "mpw-bench"
|
2017-08-30 16:18:23 +02:00
|
|
|
echo "done! You can now use ./$_"
|
2014-10-15 22:26:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-08-27 17:17:45 +02:00
|
|
|
### TARGET: MPW-TESTS
|
2014-12-22 05:45:19 +01:00
|
|
|
mpw-tests() {
|
2017-08-13 17:02:05 +02:00
|
|
|
# dependencies
|
2017-09-06 06:31:49 +02:00
|
|
|
use_mpw_sodium required
|
2017-09-10 19:57:14 +02:00
|
|
|
use_mpw_xml required
|
2014-12-22 05:45:19 +01:00
|
|
|
|
2017-08-13 17:02:05 +02:00
|
|
|
# target
|
2017-08-27 17:17:45 +02:00
|
|
|
cflags=(
|
2017-08-13 17:02:05 +02:00
|
|
|
"${cflags[@]}"
|
2017-04-08 20:25:54 +02:00
|
|
|
|
2017-03-21 19:07:40 +01:00
|
|
|
# mpw paths
|
2018-06-06 02:01:46 +02:00
|
|
|
-I"../core/src" -I"src"
|
2014-12-22 05:45:19 +01:00
|
|
|
)
|
2017-09-04 20:29:25 +02:00
|
|
|
ldflags=(
|
2017-08-13 17:02:05 +02:00
|
|
|
"${ldflags[@]}"
|
2014-12-22 05:45:19 +01:00
|
|
|
)
|
|
|
|
|
2017-08-13 17:02:05 +02:00
|
|
|
# build
|
2018-06-06 02:01:46 +02:00
|
|
|
cc "${cflags[@]}" "$@" \
|
2019-10-03 05:13:12 +02:00
|
|
|
"../core/src/base64.c" "../core/src/aes.c" "../core/src/mpw-algorithm.c" \
|
|
|
|
"../core/src/mpw-algorithm_v0.c" "../core/src/mpw-algorithm_v1.c" "../core/src/mpw-algorithm_v2.c" "../core/src/mpw-algorithm_v3.c" \
|
|
|
|
"../core/src/mpw-types.c" "../core/src/mpw-util.c" "src/mpw-tests-util.c" \
|
2018-06-06 02:01:46 +02:00
|
|
|
"${ldflags[@]}" "src/mpw-tests.c" -o "mpw-tests"
|
2017-08-30 16:18:23 +02:00
|
|
|
echo "done! You can now use ./$_"
|
2014-12-22 05:45:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-08-27 17:17:45 +02:00
|
|
|
### TOOLS
|
|
|
|
haslib() {
|
2017-09-06 06:31:49 +02:00
|
|
|
cc -x c "${ldflags[@]}" -l"$1" -o /dev/null - <<< 'int main() { return 0; }' &>/dev/null
|
2017-08-27 17:17:45 +02:00
|
|
|
}
|
2018-09-22 16:59:34 +02:00
|
|
|
cc() (
|
|
|
|
(( verbose )) && set -x
|
|
|
|
|
|
|
|
if { hash llvm-gcc; } 2>/dev/null; then
|
2017-08-27 17:17:45 +02:00
|
|
|
llvm-gcc "$@"
|
2018-09-22 16:59:34 +02:00
|
|
|
elif { hash gcc; } 2>/dev/null; then
|
2017-09-25 08:53:34 +02:00
|
|
|
gcc -std=c11 "$@"
|
2018-09-22 16:59:34 +02:00
|
|
|
elif { hash clang; } 2>/dev/null; then
|
2017-08-27 17:17:45 +02:00
|
|
|
clang "$@"
|
|
|
|
else
|
|
|
|
echo >&2 "Need a compiler. Please install GCC or LLVM."
|
|
|
|
exit 1
|
|
|
|
fi
|
2018-09-22 16:59:34 +02:00
|
|
|
)
|
2017-08-27 17:17:45 +02:00
|
|
|
|
|
|
|
|
|
|
|
### DEPENDENCIES
|
2017-09-06 06:31:49 +02:00
|
|
|
use() {
|
2017-11-07 00:41:18 +01:00
|
|
|
local option=$1 requisite=$2 lib=$3; shift 3
|
2017-09-06 06:31:49 +02:00
|
|
|
local enabled=${!option}
|
|
|
|
|
2017-09-10 19:57:14 +02:00
|
|
|
if (( enabled )); then
|
|
|
|
if haslib "$lib"; then
|
2017-11-07 00:41:18 +01:00
|
|
|
for lib in "$lib" "$@"; do
|
2017-11-07 03:58:51 +01:00
|
|
|
haslib "$lib" && ldflags+=( -l"$lib" )
|
2017-11-07 00:41:18 +01:00
|
|
|
done
|
2018-09-22 16:59:34 +02:00
|
|
|
echo "INFO: Enabled $option (lib$lib)."
|
2017-09-10 19:57:14 +02:00
|
|
|
return 0
|
|
|
|
|
|
|
|
elif [[ $requisite == required ]]; then
|
|
|
|
echo >&2 "ERROR: $option was enabled but is missing $lib library. Please install this library before continuing."
|
|
|
|
exit 1
|
|
|
|
|
|
|
|
else
|
|
|
|
echo >&2 "WARNING: $option was enabled but is missing $lib library. Will continue with $option disabled!"
|
|
|
|
return 1
|
|
|
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
elif [[ $requisite == required ]]; then
|
|
|
|
echo >&2 "ERROR: $option was required but is not enabled. Please enable the option or remove this target before continuing."
|
2017-09-06 06:31:49 +02:00
|
|
|
exit 1
|
2017-09-10 19:57:14 +02:00
|
|
|
|
2017-08-27 17:17:45 +02:00
|
|
|
else
|
2018-09-22 16:59:34 +02:00
|
|
|
echo "INFO: $option is supported but not enabled."
|
2017-09-10 19:57:14 +02:00
|
|
|
return 1
|
2017-08-27 17:17:45 +02:00
|
|
|
fi
|
|
|
|
}
|
2017-09-06 06:31:49 +02:00
|
|
|
use_mpw_sodium() {
|
|
|
|
local requisite=$1
|
|
|
|
use mpw_sodium "$requisite" sodium && cflags+=( -D"MPW_SODIUM=1" ) ||:
|
|
|
|
}
|
2017-08-27 17:17:45 +02:00
|
|
|
use_mpw_color() {
|
2017-09-06 06:31:49 +02:00
|
|
|
local requisite=$1
|
2017-11-07 00:41:18 +01:00
|
|
|
use mpw_color "$requisite" curses tinfo && cflags+=( -D"MPW_COLOR=1" ) ||:
|
2017-08-27 17:17:45 +02:00
|
|
|
}
|
|
|
|
use_mpw_json() {
|
2017-09-06 06:31:49 +02:00
|
|
|
local requisite=$1
|
|
|
|
use mpw_json "$requisite" json-c && cflags+=( -D"MPW_JSON=1" ) ||:
|
2017-08-27 17:17:45 +02:00
|
|
|
}
|
|
|
|
use_mpw_xml() {
|
2017-09-06 06:31:49 +02:00
|
|
|
local requisite=$1
|
2018-09-26 06:43:23 +02:00
|
|
|
use mpw_xml "$requisite" xml2 && cflags+=( $(xml2-config --cflags) ) ldflags+=( $(xml2-config --libs) ) ||:
|
2017-08-27 17:17:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
### BUILD TARGETS
|
2017-08-30 16:18:23 +02:00
|
|
|
for target in "${targets_all[@]}"; do
|
2018-09-22 16:59:34 +02:00
|
|
|
if [[ $targets == 'all' || " $targets " = *" $target "* ]]; then
|
2017-08-30 16:18:23 +02:00
|
|
|
echo
|
|
|
|
echo "Building target: $target..."
|
|
|
|
( "$target" "$@" )
|
|
|
|
fi
|
2014-10-15 22:26:09 +02:00
|
|
|
done
|