Create a gist now

Instantly share code, notes, and snippets.

@brockers /cmaker
Last active Aug 29, 2015

What would you like to do?
Bash script to setup the necessary components of an Autotools/Automake C application. Think of it as 'rails new projectname' for C.
#!/bin/bash
# Global Variables
PROJECT_FOLDER="$(pwd)"
# Make sure something is actually specified
if [[ -z "$1" ]]; then echo "Please specify a command (if not sure do `basename $0` --help)"; fi
usage() {
echo "`basename $0` [OPTIONS] meta script setting up a new C program in automake."
echo " "
echo "Options:"
echo " help|-h|--help ...Show this help menu"
echo " list ...List stuff"
echo " setup ...Test enviroment, dependancies, and setup."
echo " new|init <project-name> ...Initialize a new project <project-name>"
echo " in a new folder of the same name."
echo ""
exit 1
}
checkprojectdirectory() {
if [ $1 == $(basename `pwd`) ]; then
echo "...Current directory name matches $1, initializing project in current directory."
PROJECT_FOLDER="$(pwd)"
else
PROJECT_FOLDER="$(pwd)/$1"
fi
}
editvalue() {
VALUE=$1
CHANGE=$2
FILECHANGE=$3
if ! $( sed -i "s/$VALUE/$CHANGE/g" "$FILECHANGE" ); then
echo "Unable to modify $VALUE with $CHANGE in $FILECHANGE"
exit 1
fi
}
makeConfigureAc() {
FILETOCHANGE="$PROJECT_FOLDER/configure.ac"
echo " ...creating $FILETOCHANGE."
cat << 'EOF' > "$FILETOCHANGE"
AC_INIT([XXXX], [0.1], [bugs@XXXX.org])
AC_CONFIG_AUX_DIR([config])
AM_INIT_AUTOMAKE([-Wall -Werror foreign])
AC_PROG_CC
AC_PROG_CC_STDC
AC_CONFIG_HEADERS([config.h])
AC_CONFIG_FILES([
Makefile
src/Makefile
test/Makefile
])
AC_OUTPUT
EOF
editvalue 'XXXX' $1 $FILETOCHANGE
}
makeMakefileAm() {
FILETOCHANGE="$PROJECT_FOLDER/Makefile.am"
echo " ...creating $FILETOCHANGE."
cat << 'EOF' > "$FILETOCHANGE"
SUBDIRS = src test
dist_doc_DATA = README.md
EOF
}
makeSrcMakefileAm() {
FILETOCHANGE="$PROJECT_FOLDER/src/Makefile.am"
echo " ...creating $FILETOCHANGE."
cat << 'EOF' > "$FILETOCHANGE"
# ADD DEPENDANCY LIBRARIES TO AM_LD_FLAGS BELOW
AM_CPPFLAGS = -I$(top_srcdir)/include
bin_PROGRAMS = XXXXX
XXXXX_SOURCES = XXXXX.c
AM_LDFLAGS=-lpthread -lm -lncurses
EOF
editvalue 'XXXXX' $1 $FILETOCHANGE
}
makeTestMakefileAm() {
FILETOCHANGE="$PROJECT_FOLDER/test/Makefile.am"
echo " ...creating $FILETOCHANGE."
cat << 'EOF' > "$FILETOCHANGE"
# ADD DEPENDANCY LIBRARIES TO AM_LD_FLAGS BELOW
AM_CPPFLAGS = -I$(top_srcdir)/include
check_PROGRAMS = testXXXXXprogram
testXXXXXprogram_SOURCES = testXXXXXprogram.c
TESTS = testXXXXXprogram
EOF
editvalue 'XXXXX' $1 $FILETOCHANGE
}
makeMainC() {
FILETOCHANGE="$PROJECT_FOLDER/src/${1}.c"
echo " ...creating $FILETOCHANGE."
cat << 'EOF' > "$FILETOCHANGE"
#include <config.h>
#include <stdio.h>
#include "XXXXXX/XXXXXX.h"
int
main (void) {
puts ("Hello World!");
// MYNAME is defined in your header located in the include/ directory
// as is math.h
printf("Project %s is number %i\n", MYNAME, 2/2);
return 0;
}
EOF
editvalue 'XXXXXX' $1 $FILETOCHANGE
}
makeTestC() {
FILETOCHANGE="$PROJECT_FOLDER/test/test${1}program.c"
echo " ...creating $FILETOCHANGE."
cat << 'EOF' > "$FILETOCHANGE"
#include <stdio.h>
int main () {
printf("Can I printf successfully... yes I can!\n");
return 0;
}
EOF
editvalue 'XXXXXX' $1 $FILETOCHANGE
}
makeIncludeMainH() {
FILETOCHANGE="$PROJECT_FOLDER/include/$1/${1}.h"
echo " ...creating $FILETOCHANGE."
cat << 'EOF' > "$FILETOCHANGE"
#ifndef XXXXXXX_H_INCLUDED
#define XXXXXXX_H_INCLUDED
#include<stdio.h>
#include<math.h>
#define MYNAME "XXXXXXX"
#endif
EOF
editvalue 'XXXXXXX' $1 $FILETOCHANGE
}
makeReadme() {
FILETOCHANGE="$PROJECT_FOLDER/README.md"
echo " ...creating $FILETOCHANGE."
cat << 'EOF' > "$FILETOCHANGE"
Welcome to XXXXXXXX
===================
This is your initial README file. Basically you should now
be able to build your XXXXXXXX application by running:
autoreconf --install
./configure
make
Run your unit tests (found in the test/ directory) with
make check
...and initalise your project in git with (with a sane ignore file)
git init .
Couple things about your setup:
1) The default email address in configure.ac should probably be changed.
2) Your c header files should go in the include/ directory.
(new .h file can then be referenced with #include "include/newfilename.h")
3) Your source code should go in the src/ directory.
(new source code file names will then need to be added to the SOURCES line
in src/Makefile.am.)
4) Any additional libraries you need can be linked in src/Makefile.am
(See the existing examples in the file.)
Good Luck!
EOF
editvalue 'XXXXXXXX' $1 $FILETOCHANGE
}
makegitignore() {
FILETOCHANGE="$PROJECT_FOLDER/.gitignore"
echo " ...creating $FILETOCHANGE."
cat << 'EOF' > "$FILETOCHANGE"
Makefile
Makefile.in
aclocal.m4
autom4te.cache/
config.*
config/*
configure
src/.deps/
src/Makefile
src/Makefile.in
src/XXXXXX
src/XXXXXX.o
stamp-h1
test/.deps/
test/Makefile
test/Makefile.in
test/test-suite.log
test/testXXXXXXprogram
test/testXXXXXXprogram.log
test/testXXXXXXprogram.o
test/testXXXXXXprogram.trs
EOF
editvalue 'XXXXXX' $1 $FILETOCHANGE
}
createproject() {
echo "Initializing project $1"
mkdir -p "$PROJECT_FOLDER"
cd "$PROJECT_FOLDER"
# Start populating our empty project folder... or nearly empty.
echo " ...creating folder structure. "
mkdir -p {config,doc,lib,src,test,include}
mkdir -p "include/$1"
makeMakefileAm
makeConfigureAc $1
makeSrcMakefileAm $1
makeTestMakefileAm $1
makeMainC $1
makeTestC $1
makeIncludeMainH $1
makeReadme $1
makegitignore $1
exit 1
}
while [ -n $1 ]; do
case $1 in
-h|--help|help)
usage
;;
# setup)
# createsetup
# ;;
new|init)
checkprojectdirectory $2
createproject $2
;;
list)
echo " Sorry, feature not implemented yet... try --help for other options"
break
;;
# list
# ;;
*)
break
;;
esac
done
exit 1
Owner

brockers commented May 13, 2014

Once this is copied and made executable new projects can be created by running:

cmaker new project-name

and built by doing a:

autoreconfig --install
./configure
make

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment