Last active
March 26, 2019 06:41
-
-
Save hayamiz/408157dcd85abbc7c31ff6b7bbaba788 to your computer and use it in GitHub Desktop.
kutter
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| set -e | |
| ### | |
| ### kutter -- utility command for Cutter C/C++ testing framework | |
| ### | |
| __kutter_usage() { | |
| echo "Usage: kutter CMD [arg ...]" | |
| echo "" | |
| echo " Commands:" | |
| echo " - init: initialize a project skeleton with Cutter" | |
| echo "" | |
| } | |
| __kutter_mkdir() { | |
| echo " making directory: $1" | |
| if ! mkdir $1; then | |
| echo " => FAILED!" | |
| return 1 | |
| fi | |
| } | |
| __kutter_mkfile() { | |
| local file=$1 | |
| echo " making file: $1" | |
| if ! cat > $file; then | |
| echo " => FAILED!" | |
| return 1 | |
| fi | |
| } | |
| __kutter_cmd__init() { | |
| local projname=$1 | |
| if [[ -d "./$projname" ]]; then | |
| echo "[ERROR] ./$projname already exists" | |
| return 1 | |
| fi | |
| __kutter_mkdir "./$projname" | |
| __kutter_mkdir "./$projname/src" | |
| __kutter_mkdir "./$projname/config" | |
| __kutter_mkdir "./$projname/test" | |
| __kutter_mkfile "./$projname/autogen.sh" <<EOF | |
| #!/bin/sh | |
| run() | |
| { | |
| \$@ | |
| if test \$? -ne 0; then | |
| echo "Failed \$@" | |
| exit 1 | |
| fi | |
| } | |
| run aclocal \${ACLOCAL_ARGS} | |
| run libtoolize --copy --force | |
| run autoheader | |
| run automake --add-missing --foreign --copy | |
| run autoconf | |
| EOF | |
| chmod +x "./$projname/autogen.sh" | |
| __kutter_mkfile "./$projname/configure.ac" <<EOF | |
| AC_PREREQ(2.59) | |
| AC_INIT(${projname}, 0.0.1, [email protected]) | |
| AC_CONFIG_AUX_DIR([config]) | |
| AC_CONFIG_HEADER([src/config.h]) | |
| AM_INIT_AUTOMAKE(\$PACKAGE_NAME, \$PACKAGE_VERSION) | |
| AC_PROG_LIBTOOL | |
| m4_ifdef([AC_CHECK_CUTTER], [AC_CHECK_CUTTER], [ac_cv_use_cutter="no"]) | |
| AM_CONDITIONAL([WITH_CUTTER], [test "\$ac_cv_use_cutter" != "no"]) | |
| m4_ifdef([AC_CHECK_COVERAGE], [AC_CHECK_COVERAGE]) | |
| AC_CONFIG_FILES([Makefile | |
| src/Makefile | |
| test/Makefile]) | |
| AC_OUTPUT | |
| EOF | |
| __kutter_mkfile "./$projname/Makefile.am" <<EOF | |
| ACLOCAL_AMFLAGS = \$\$ACLOCAL_ARGS | |
| SUBDIRS = test | |
| EOF | |
| __kutter_mkfile "./$projname/test/Makefile.am" <<EOF | |
| if WITH_CUTTER | |
| TESTS = run-test.sh | |
| TESTS_ENVIRONMENT = NO_MAKE=yes CUTTER="\$(CUTTER)" | |
| noinst_LTLIBRARIES = test_${projname}.la | |
| endif | |
| INCLUDES = -I\$(top_srcdir)/src | |
| LIBS = \$(CUTTER_LIBS) \$(top_builddir)/src/lib${projname}.la | |
| AM_CFLAGS = \$(CUTTER_CFLAGS) | |
| LDFLAGS = -module -rpath \$(libdir) -avoid-version -no-undefined | |
| test_${projname}_la_SOURCES = test-${projname}.c | |
| echo-cutter: | |
| @echo \$(CUTTER) | |
| EOF | |
| __kutter_mkfile "./$projname/src/Makefile.am" <<EOF | |
| lib_LTLIBRARIES = lib${projname}.la | |
| LDFLAGS = -no-undefined | |
| lib${projname}_la_SOURCES = ${projname}.c | |
| EOF | |
| __kutter_mkfile "./$projname/test/run-test.sh" <<EOF | |
| #!/bin/sh | |
| export BASE_DIR="\`dirname \$0\`" | |
| top_dir="\$BASE_DIR/.." | |
| if test -z "\$NO_MAKE"; then | |
| make -C \$top_dir > /dev/null || exit 1 | |
| fi | |
| if test -z "\$CUTTER"; then | |
| CUTTER="\`make -s -C \$BASE_DIR echo-cutter\`" | |
| fi | |
| case `uname` in | |
| CYGWIN*) | |
| PATH="\$top_dir/src/.libs:\$PATH" | |
| ;; | |
| Darwin) | |
| DYLD_LIBRARY_PATH="\$top_dir/src/.libs:\$DYLD_LIBRARY_PATH" | |
| export DYLD_LIBRARY_PATH | |
| ;; | |
| *BSD) | |
| LD_LIBRARY_PATH="\$top_dir/src/.libs:\$LD_LIBRARY_PATH" | |
| export LD_LIBRARY_PATH | |
| ;; | |
| *) | |
| : | |
| ;; | |
| esac | |
| \$CUTTER -s \$BASE_DIR "\$@" \$BASE_DIR | |
| EOF | |
| chmod +x "./$projname/test/run-test.sh" | |
| __kutter_mkfile "./$projname/src/${projname}.c" <<EOF | |
| #include <stdlib.h> | |
| #include "${projname}.h" | |
| #define TRUE 1 | |
| #define FALSE 0 | |
| ${projname} * | |
| ${projname}_new (void) | |
| { | |
| return NULL; | |
| } | |
| int | |
| ${projname}_is_ok (${projname} *${projname}) | |
| { | |
| return TRUE; | |
| } | |
| EOF | |
| __kutter_mkfile "./$projname/src/${projname}.h" <<EOF | |
| #ifndef __${projname}_H__ | |
| #define __${projanme}_H__ | |
| typedef struct _${projname} ${projname}; | |
| ${projname} *${projname}_new (void); | |
| int ${projname}_is_ok (${projname} *${projname}); | |
| #endif | |
| EOF | |
| __kutter_mkfile "./$projname/test/test-${projname}.c" <<EOF | |
| #include <cutter.h> | |
| #include <${projname}.h> | |
| void test_new_${projname} (void); | |
| void | |
| test_new_${projname} (void) | |
| { | |
| ${projname} *${projname}; | |
| ${projname} = ${projname}_new(); | |
| cut_assert(${projname}_is_ok(${projname})); | |
| } | |
| EOF | |
| __kutter_mkfile "./$projname/README" <<EOF | |
| ${projname} | |
| ============= | |
| TODO: update this file | |
| EOF | |
| # __kutter_mkfile "./$projname/" | |
| } | |
| __kutter_help__cmd() { | |
| return 1 | |
| } | |
| if [[ $# -lt 1 ]]; then | |
| __kutter_usage | |
| exit 1 | |
| fi | |
| cmd=$1 | |
| shift | |
| if type -t "__kutter_cmd__${cmd}" > /dev/null; then | |
| eval "__kutter_cmd__${cmd}" $* | |
| exit $? | |
| else | |
| echo "[ERROR] no such subcommand: $cmd" | |
| exit 1 | |
| fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment