Created
December 21, 2016 12:25
-
-
Save redxef/98386b9a80f8c2aa83b6aa680c3d87e4 to your computer and use it in GitHub Desktop.
Creates a project folder for developing in C with Sublime Text 3 project file and makefile
This file contains 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/sh | |
## | |
# @file create-sublime-project.sh | |
# @version 1.0 | |
# @author redxef | |
# @brief Creates a project folder for developing a c application using Sublime Text 3 and a makefile | |
# | |
# The script automatically generates a makefile with the listed dependencies and the option to manually add compiler flags. | |
# Secondly a Sublime project file is generated, with all include directories add for easy integration with | |
# ClangAutoComplete: https://packagecontrol.io/packages/ClangAutoComplete. | |
# The makefile us written for gnu-make. | |
# The following flags are available: | |
# -n the name of the project | |
# -m the makefile to use, if not specified, then the integrated version is used | |
# -d list of dependencies for which to configure (the list is terminated with a dash (-)) | |
DEFAULT_SUBLIME_PROJECT=' | |
{ | |
"folders": | |
[ | |
{ | |
"path": ".", | |
} | |
], | |
"include_dirs": | |
[ | |
__DIR_INSERT__ | |
], | |
}' | |
DEFAULT_MAKEFILE='# change source dirs and compiled names | |
D_SRC := src/ | |
D_OBJ := obj/ | |
name := _$__NAME__ | |
testname := _$__NAME__-test | |
# include- and library-directories, specify libs to be linked | |
# -I | |
DIR_INCLUDE := | |
# -L | |
DIR_LIBPATH := | |
# -l | |
LIBS := | |
# pkg-config | |
PAKAGES := _$__PAKAGES__ | |
# change compile flags | |
CC := gcc | |
CFLAGS := -g -Wall | |
# all the sources | |
C_SRCS := $(filter-out $(D_SRC)test.c, $(filter-out $(D_SRC)main.c, $(wildcard $(D_SRC)*.c))) | |
C_TEST := $(D_SRC)test.c $(C_SRCS) | |
C_MAIN := $(D_SRC)main.c $(C_SRCS) | |
# object out files | |
O_TEST := $(patsubst $(D_SRC)%, $(D_OBJ)%, $(patsubst %.c, %.o, $(C_TEST))) | |
O_MAIN := $(patsubst $(D_SRC)%, $(D_OBJ)%, $(patsubst %.c, %.o, $(C_MAIN))) | |
# flags | |
FLAG_INCLUDE = | |
FLAG_LIBDIRS = | |
FLAG_LINKLIB = | |
# ifdef $(PAKAGES) | |
FLAG_INCLUDE += $(shell pkg-config --cflags $(PAKAGES) 2>/dev/null) | |
FLAG_LIBDIRS += $(shell pkg-config --libs-only-L $(PAKAGES) 2>/dev/null) | |
FLAG_LINKLIB += $(shell pkg-config --libs-only-l $(PAKAGES) 2>/dev/null) | |
# endif | |
FLAG_INCLUDE += $(foreach includedir,$(DIR_INCLUDE),-I$(includedir)) | |
FLAG_LIBDIRS += $(foreach librarydir,$(DIR_LIBPATH),-L$(librarydir)) | |
FLAG_LINKLIB += $(foreach library,$(LIBS),-l$(library)) | |
main: $(name) | |
test: $(testname) | |
all: $(name) $(testname) | |
$(D_OBJ)%.o: $(D_SRC)%.c | |
$(CC) $(CFLAGS) $(FLAG_INCLUDE) -c $< -o $@ | |
$(name): $(O_MAIN) | |
$(CC) $(FLAG_LIBDIRS) $(FLAG_LINKLIB) -o $@ $(O_MAIN) | |
$(testname): $(O_TEST) | |
$(CC) $(FLAG_LIBDIRS) $(FLAG_LINKLIB) -o $@ $(O_TEST) | |
init: | |
mkdir -p $(D_SRC) | |
mkdir -p $(D_OBJ) | |
ln -s $(name) main | |
ln -s $(testname) test | |
clean: | |
rm -f $(name) | |
rm -f $(testname) | |
rm -f $(O_TEST) | |
rm -f $(O_MAIN) | |
rm -f main | |
rm -f test' | |
main() { | |
local flag_dmake=1 | |
local name='proj' | |
local makefile="$DEFAULT_MAKEFILE" | |
local sublime_project="$DEFAULT_SUBLIME_PROJECT" | |
local deps=( ) | |
local incs=( ) | |
local flag=__NONE__ | |
for arg in "$@"; do | |
if [ $flag = __NAME__ ]; then | |
name="$arg" | |
flag=__NONE__ | |
elif [ $flag = __MAKE__ ]; then | |
makefile="$(cat "$arg")" | |
flag=__NONE__ | |
elif [ $flag = __DEPS__ ]; then | |
if [ "$arg" = - ]; then | |
flag=__NONE__ | |
else | |
deps+=("$arg") | |
fi | |
else | |
if [ "$arg" = -n ]; then | |
flag=__NAME__ | |
elif [ "$arg" = -m ]; then | |
flag=__MAKE__ | |
elif [ "$arg" = -d ]; then | |
flag=__DEPS__ | |
fi | |
fi | |
done | |
# generate dependencies | |
incs=( $(pkg-config --cflags "${deps[@]}") ) | |
# generate project file | |
sublime_project="$DEFAULT_SUBLIME_PROJECT" | |
sublime_project="$(echo "$sublime_project" | sed -e 's|__DIR_INSERT__|'"$(printf ' "%s",\n' "${incs[@]}" | tr '\n' '\r')"'|' | tr '\r' '\n')" | |
mkdir "$name" | |
echo "$sublime_project" > "$name/$name.sublime-project" | |
echo "$(echo "$makefile" | sed 's/_$__NAME__/'"$name"'/g' | sed 's/_$__PAKAGES__/'"$deps"'/g' |sed -e 's/ */\\t/g' )" > "$name/makefile" | |
if [ $flag_dmake -eq 1 ]; then | |
{ | |
cd "$name" | |
make init | |
} | |
fi | |
} | |
main "$@" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment