苦于学习工作过程中,长期没有固定风格,不便于有效的关联自己的书写代码,特此,写了个shell脚本,用于以后以同意风格工程结构目录,记录如下:
#########################################################################
# File Name: createproject.sh
# Author: ma6174
# mail: ma6174@163.com
# Created Time: Mon 30 Jul 2018 09:26:00 PM CST
#########################################################################
#!/bin/bash
#创建顶层Makefile
function CreateTopMakefile()
{
text="include Makefile.in\n\n"
text+="MODULEDIR=modules\n\n"
text+=".PHONY:all clean \n\n"
text+="all: MODULE \$(CUR_OBJS) \$(TARGET)\n\n"
text+="MODULE:\$(MODULEDIR)\n"
text+="\tmake -C \$^\n\n"
text+="\$(CUR_OBJS):%.o:%.c\n"
text+="\t\$(CC) -c \$(CFLAGS) -o \$@ $< \$(INCLUDE)\n\n"
text+="\$(TARGET):\$(CUR_OBJS)\n"
text+="\t\$(CC) -o \$@ \$< \$(INCLUDE) \$(LDFLAGS)\n\n"
text+="clean:\n"
text+="\t@\$(foreach N, \$(MODULEDIR),make clean -C \$(N);)\n"
text+="\trm -rf \$(CUR_OBJS) \$(TARGET)\n\n"
text+="\trm -rf \$(LIBSDIR)\n\n"
echo -e $text >$1
}
function CreateTopMakefileIn()
{
text="APP=main\n\n"
text+="CC=gcc\n\n"
text+="CFLAGS= -Wall -ggdb3\n\n"
text+="LIBSDIR=lib\n\n"
text+="LDFLAGS=\n\n"
text+="INCLUDE=-I./include\n\n"
text+="CUR_SOURCE=\$(wildcard src/*.c)\n\n"
text+="CUR_OBJS=\$(patsubst %.c, %.o, \$(CUR_SOURCE))\n\n"
text+="BINDIR=bin/\n\n"
text+="TARGET=\$(BINDIR)\$(APP)\n"
echo -e $text >$1
}
function CreateModuleDirMakefile()
{
text="SUBDIRS :="$2"\n\n"
text+=".PHONY:all clean\n\n"
text+="all:\n"
text+="\t@\$(foreach N, \$(SUBDIRS),make -C \$(N);)\n\n"
text+="clean:\n"
text+="\t@\$(foreach N, \$(SUBDIRS),make clean -C \$(N);)\n"
echo -e $text >$1
}
function CreateModuleMakefile()
{
text="include Makefile.in\n\n"
text+=".PHONY:all clean\n\n"
text+="all:\$(CUR_OBJS) \$(TARGET)\n\n"
text+="\$(CUR_OBJS):%.o:%.c\n"
text+="\t\$(CC) -c \$(CFLAGS) -o \$@ \$< \$(INCLUDE)\n\n"
text+="\$(TARGET):\$(CUR_OBJS)\n"
text+="\t\$(CC) -fPIC -shared -o \$@ \$<\n"
text+="\tcp \$(TARGET) \$(INSTALLDIR)/lib\n"
text+="\tcp \$(INCLUDEDIR)/*.h \$(INSTALLDIR)/include\n\n"
text+="clean:\n"
text+="\trm -rf \$(CUR_OBJS) \$(TARGET)\n"
echo -e $text >$1
}
function CreateModuleMakefileIn()
{
text="MODULE=lib"$2"\n\n"
text+="CC=gcc\n"
text+="CFLAGS= -Wall -ggdb3 -fPIC\n"
text+="LIBSDIR=\n"
text+="INCLUDEDIR=include\n"
text+="INSTALLDIR=../..\n"
text+="INCLUDE=-I\$(INCLUDEDIR)\n"
text+="CUR_SOURCE=\$(wildcard src/*.c)\n"
text+="CUR_OBJS=\$(patsubst %.c, %.o, \$(CUR_SOURCE))\n"
text+="LIBSDIR=lib/\n"
text+="TARGET=\$(LIBSDIR)\$(MODULE).so"
echo -e $text >$1
}
function CreateMainMakefile()
{
text="include Makefile.in\n\n"
text+=".PHONY:all clean\n\n"
text+="all:\$(CUR_OBJS) \$(TARGET)\n\n"
text+="\$(CUR_OBJS):%.o:%.c\n"
text+="\t\$(CC) -c \$(CFLAGS) -o \$@ \$< \$(INCLUDE)\n\n"
text+="\$(TARGET):\$(CUR_OBJS)\n"
text+="\t\$(CC) -o \$@ \$<\n\n"
text+="clean:\n"
text+="\t\rm -rf \$(CUR_OBJS) \$(TARGET)\n"
echo -e $text >$1
}
function CreateMainMakefileIn()
{
text="APP="$2"\n"
text+="CC=gcc\n"
text+="CFLAGS= -Wall -ggdb3\n"
text+="LIBS=\n"
text+="INCLUDE=-I./include\n"
text+="CUR_SOURCE=\$(wildcard *.c)\n"
text+="CUR_OBJS=\$(patsubst %.c, %.o, \$(CUR_SOURCE))\n"
text+="BINDIR=lib/\n"
text+="TARGET=\$(BINDIR)\$(APP)\n"
echo -e $text >$1
}
dirlist="bin include doc lib src modules test"
modules="sample "
moduledir="include lib src test"
IFS=" "
dirarr=($dirlist)
modulearr=($modules)
moduledirarr=($moduledir)
function createmoduledir()
{
for var in ${moduledirarr[@]};
do
mkdir -p $1/$var
done
}
function create_project()
{
projname=$1
mkdir -p $projname
if [ -d $projname ];then
CreateTopMakefile $projname/Makefile
CreateTopMakefileIn $projname/Makefile.in
fi
for dir in ${dirarr[@]};
do
mkdir -p $projname/$dir
done
if [ -d $projname/modules ];then
CreateModuleDirMakefile $projname/modules/Makefile $modules
for modl in ${modulearr[@]};
do
mkdir -p $projname/modules/$modl
CreateModuleMakefile $projname/modules/$modl/Makefile
CreateModuleMakefileIn $projname/modules/$modl/Makefile.in $modl
createmoduledir $projname/modules/$modl
done
fi
#if 0
if [ -d $projname/src ];then
CreateMainMakefile $projname/src/Makefile
CreateMainMakefileIn $projname/src/Makefile.in $projname
fi
#endif
}
if [ $# -ne 1 ];then
echo "==[E]please input project name!"
fi
project=$1
if [ -d $project ];then
echo "文件夹已经存在"
exit
fi
create_project $project