Skip to content

Instantly share code, notes, and snippets.

@aprell
Created October 12, 2012 09:57
Show Gist options
  • Save aprell/3878472 to your computer and use it in GitHub Desktop.
Save aprell/3878472 to your computer and use it in GitHub Desktop.
Project templates
#!/bin/bash
usage="Usage: $(basename "$0") <name>"
if [ $# -ne 1 ]; then
echo $usage
exit 0
fi
proj=$1
cd
# We assume a top-level directory called projects
tld=projects
if [ ! -d $tld ]; then
echo "Creating $HOME/$tld"
mkdir $tld
else
if [ -d "$tld/$proj" ]; then
echo "Warning: project \"$proj\" already exists!"
exit 0
fi
fi
cd $tld
echo "Creating $HOME/$tld/$proj"
echo "Creating $HOME/$tld/$proj/src"
echo "Creating $HOME/$tld/$proj/include"
mkdir -p "$proj/src" "$proj/include"
mkdir -p "$proj/src/deps"
echo "Creating $HOME/$tld/$proj/Makefile"
echo "Creating $HOME/$tld/$proj/src/Makefile"
echo "Creating $HOME/$tld/$proj/src/main.c"
touch "$proj/Makefile"
touch "$proj/src/Makefile"
touch "$proj/src/main.c"
# top-level makefile
echo -n "\
all clean $proj:
cd src && \$(MAKE) \$@
.PHONY: all clean
" >> "$proj/Makefile"
# src directory makefile
echo -n "\
include ../../common.mk
PROGS = $proj
SRCS = main.c
${proj}_SRCS = main.c
${proj}_LIBS =
include ../../rules.mk
include ../../deps.mk
" >> "$proj/src/Makefile"
# main source file
echo -n "\
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
printf(\"Hello %s!\n\", argv[0]);
return 0;
}
" >> "$proj/src/main.c"
# Build system requires common.mk, deps.mk, and rules.mk
if [ ! -f common.mk ]; then
echo "Creating $HOME/$tld/common.mk"
touch common.mk
echo -n "\
# Common definitions
CC = gcc
CPPFLAGS += -D_GNU_SOURCE
CFLAGS += -O0 -g -Wall -Wextra \$(INCLUDE)
INCLUDE += -I\$(HOME)/include
INCLUDE += -I../../utest/include
INCLUDE += -I. -I../include
LDFLAGS += -L\$(HOME)/lib
" >> common.mk
fi
if [ ! -f deps.mk ]; then
echo "Creating $HOME/$tld/deps.mk"
touch deps.mk
echo -n "\
# Dependency generation
# Project must define SRCS
DEPS = \$(addprefix deps/,\$(SRCS:.c=.d))
deps/%.d: %.c
\$(CC) -MM \$(CFLAGS) \$(CPPFLAGS) -o \$@ \$<
sed -i \"s/\(\$*.o\)/\1 deps\/\$(notdir \$@)/g\" \$@
# Prevent make from generating dependencies when running 'make clean'
ifneq (\$(MAKECMDGOALS),clean)
-include \$(DEPS)
endif
" >> deps.mk
fi
if [ ! -f rules.mk ]; then
echo "Creating $HOME/$tld/rules.mk"
touch rules.mk
echo -n "\
# Rule generation
# Project must define PROGS, SRCS, and *_SRCS
OBJS = \$(SRCS:.c=.o)
define RULE_template
\$(1): \$\$(\$(subst -,_,\$(1))_SRCS:.c=.o) \$\$(\$(subst -,_,\$(1))_PREREQS)
\$\$(CC) -o \$\$@ \$\$(filter %.o,\$\$^) \$\$(LDFLAGS) \$\$(IMPORTS) \$\$(\$(subst -,_,\$(1))_LIBS:%=-l%)
endef
# Default clean target
define RULE_clean
clean:
rm -f \$(1) \$\$(OBJS) \$\$(DEPS) *.a
endef
all: \$(PROGS)
\$(foreach prog,\$(PROGS),\$(eval \$(call RULE_template,\$(prog))))
\$(eval \$(call RULE_clean,\$(PROGS)))
.PHONY: all clean
" >> rules.mk
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment