-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcreate_cpp_project.sh
More file actions
executable file
·316 lines (280 loc) · 9.67 KB
/
create_cpp_project.sh
File metadata and controls
executable file
·316 lines (280 loc) · 9.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
#! /bin/sh
# check wether the scripts path environment variable has been defined
SCRIPTS_PATH=`echo "${SCRIPTS_PATH}"`
if [ -z "${SCRIPTS_PATH}" ]
then
echo "The scripts path environment varibale has not been defined. Please see the wiki documentation for instructions on how to create it."
exit
fi
TEMPLATES_PATH=$SCRIPTS_PATH/cpp_project_template
DEP=
NAME=
while getopts "h:n:d" OPTION
do
case $OPTION in
h)
echo "******************************************"
echo "Script to generate a CPP project structure"
echo "******************************************"
echo "Options:"
echo " -n: Project name"
echo " -d: Dependency names to be added to the project"
echo ""
echo "Example of usage:"
echo "new_project.sh -n example_project"
echo ""
exit
;;
n)
NAME=$OPTARG
;;
d)
DEP=$OPTARG
;;
?)
echo "type -h for help"
exit
;;
esac
done
#Original name only for folder name
ORIGNAME=$NAME
#Lower case name
if [ $NAME ]
then
NAME=$(echo $NAME | tr '[:upper:]' '[:lower:]')
else
echo "No library name provided, aborting ..."
exit
fi
echo Generating $ORIGNAME project
#create the project directory
if [ -e "$ORIGNAME" ]
then
echo " ! $ORIGNAME directory already exists, skipping ..."
else
echo " > Creating $ORIGNAME directory"
mkdir $ORIGNAME
fi
#create the bin directory
BIN_DIR="$ORIGNAME/bin"
if [ -e "$BIN_DIR" ]
then
echo " ! $BIN_DIR directory already exists, skipping ..."
else
echo " > Creating $BIN_DIR directory"
mkdir $BIN_DIR
fi
# create the lib directory
LIB_DIR="$ORIGNAME/lib"
if [ -e "$LIB_DIR" ]
then
echo " ! $LIB_DIR directory already exists, skipping ..."
else
echo " > Creating $LIB_DIR directory"
mkdir $LIB_DIR
fi
# create the src directory
SRC_DIR="$ORIGNAME/src"
if [ -e "$SRC_DIR" ]
then
echo " ! $SRC_DIR directory already exists, skipping ..."
else
echo " > Creating $SRC_DIR directory"
mkdir $SRC_DIR
fi
#create the src/examples directory
EXAMPLES_DIR="$ORIGNAME/src/examples"
if [ -e "$EXAMPLES_DIR" ]
then
echo " ! $EXAMPLES_DIR directory already exists, skipping ..."
else
echo " > Creating $EXAMPLES_DIR directory"
mkdir $EXAMPLES_DIR
fi
#create the src/tests directory
TESTS_DIR="$ORIGNAME/src/tests"
if [ -e "$TESTS_DIR" ]
then
echo " ! $TESTS_DIR directory already exists, skipping ..."
else
echo " > Creating $TESTS_DIR directory"
mkdir $TESTS_DIR
cp -r $SCRIPTS_PATH/cpp_project_template/gtest $TESTS_DIR/.
cp -r $SCRIPTS_PATH/cpp_project_template/utils_gtest.h $TESTS_DIR/.
cp -r $SCRIPTS_PATH/cpp_project_template/gtest_example.cpp $TESTS_DIR/.
fi
# create the build directory
BUILD_DIR="$ORIGNAME/build"
if [ -e "$BUILD_DIR" ]
then
echo " ! $BUILD_DIR directory already exists, skipping ..."
else
echo " > Creating $BUILD_DIR directory"
mkdir $BUILD_DIR
fi
#create the doc directory
DOC_DIR="$ORIGNAME/doc"
if [ -e "$DOC_DIR" ]
then
echo " ! $DOC_DIR directory already exists, skipping ..."
else
echo " > Creating $DOC_DIR directory"
mkdir $DOC_DIR
fi
#create the doc/images directory
IMAGES_DIR="$ORIGNAME/doc/images"
if [ -e "$IMAGES_DIR" ]
then
echo " ! $IMAGES_DIR directory already exists, skipping ..."
else
echo " > Creating $IMAGES_DIR directory"
mkdir $IMAGES_DIR
fi
# create the doc/files directory
FILES_DIR="$ORIGNAME/doc/files"
if [ -e "$FILES_DIR" ]
then
echo " ! $FILES_DIR directory already exists, skipping ..."
else
echo " > Creating $FILES_DIR directory"
mkdir $FILES_DIR
fi
#parse the dependencies and create the dependencies file
arr=$(echo $DEP | tr "," "\n")
#Set the project name on the ReadMe.txt file
sed 's/project_name/'$ORIGNAME'/g' <$TEMPLATES_PATH/ReadMe_template.md >tmp.md
sed 's/library_name/'$NAME'/g' <tmp.md >$ORIGNAME/ReadMe.md
rm tmp.md
#Set the project name on the CMakeLists.txt script file
sed 's/project_name/'$NAME'/g' <$TEMPLATES_PATH/CMakeLists_template.txt >$ORIGNAME/CMakeLists.txt
#Set the project name on the doxygen_project_name.dox sript file
sed 's/project_name/'"$ORIGNAME"'/g' <$TEMPLATES_PATH/doxygen_project_name_template.conf >$ORIGNAME/doc/doxygen_project_name.conf
cp $TEMPLATES_PATH/doxygen_template.conf $ORIGNAME/doc/doxygen.conf
#create the CMakeLists.txt script file
echo "# library source files" >> CMakeLists.tmp
echo "SET(sources ${NAME}.cpp)" >> CMakeLists.tmp
echo "# application header files" >> CMakeLists.tmp
echo "SET(headers ${NAME}.h)" >> CMakeLists.tmp
echo "# locate the necessary dependencies" >> CMakeLists.tmp
for x in $arr
do
echo "FIND_PACKAGE($x REQUIRED)" >> CMakeLists.tmp
done
echo "# add the necessary include directories" >> CMakeLists.tmp
echo "INCLUDE_DIRECTORIES(.)" >> CMakeLists.tmp
for x in $arr
do
echo "INCLUDE_DIRECTORIES("'${'"${x}_INCLUDE_DIR"'}'")" >> CMakeLists.tmp
done
echo "# create the shared library" >> CMakeLists.tmp
echo "ADD_LIBRARY(${NAME} SHARED "'${'"sources"'}'")" >> CMakeLists.tmp
echo "# link necessary libraries" >> CMakeLists.tmp
for x in $arr
do
echo "TARGET_LINK_LIBRARIES(${NAME} "'${'"${x}_LIBRARY"'}'")" >> CMakeLists.tmp
done
echo "INSTALL(TARGETS $NAME" >> CMakeLists.tmp
echo " RUNTIME DESTINATION bin" >> CMakeLists.tmp
echo " LIBRARY DESTINATION lib/${NAME}" >> CMakeLists.tmp
echo " ARCHIVE DESTINATION lib/${NAME})" >> CMakeLists.tmp
echo "INSTALL(FILES "'${'"headers"'}' "DESTINATION include/${NAME})" >> CMakeLists.tmp
echo "INSTALL(FILES ../Find$NAME.cmake DESTINATION "'${'"CMAKE_ROOT"'}'"/Modules/)" >> CMakeLists.tmp
echo "" >> CMakeLists.tmp
echo "### EXAMPLES ###" >> CMakeLists.tmp
echo "ADD_SUBDIRECTORY(examples)" >> CMakeLists.tmp
echo "" >> CMakeLists.tmp
echo "### TESTS ###" >> CMakeLists.tmp
echo "option(BUILD_TESTS \"Build tests\" ON)" >> CMakeLists.tmp
echo "if(BUILD_TESTS)" >> CMakeLists.tmp
echo " MESSAGE(\"Building tests.\")" >> CMakeLists.tmp
echo " ADD_SUBDIRECTORY(tests)" >> CMakeLists.tmp
echo "endif(BUILD_TESTS)" >> CMakeLists.tmp
mv CMakeLists.tmp $ORIGNAME/src/CMakeLists.txt
echo "# create an example application" >> CMakeLists.tmp
echo "ADD_EXECUTABLE(${NAME}_test ${NAME}_test.cpp)" >> CMakeLists.tmp
echo "# link necessary libraries" >> CMakeLists.tmp
echo "TARGET_LINK_LIBRARIES(${NAME}_test $NAME)" >> CMakeLists.tmp
for x in $arr
do
echo "TARGET_LINK_LIBRARIES(${NAME}_test "'${'"${x}_LIBRARY"'}'")" >> CMakeLists.tmp
done
mv CMakeLists.tmp $ORIGNAME/src/examples/CMakeLists.txt
echo "# Retrieve googletest from github & compile" >> CMakeLists.tmp
echo "add_subdirectory(gtest)" >> CMakeLists.tmp
echo "" >> CMakeLists.tmp
echo "# Include gtest directory." >> CMakeLists.tmp
echo "include_directories( "'${'"GTEST_INCLUDE_DIRS"'}'")" >> CMakeLists.tmp
echo "" >> CMakeLists.tmp
echo "############# USE THIS TEST AS AN EXAMPLE #################" >> CMakeLists.tmp
echo "" >> CMakeLists.tmp
echo "# Create a specific test executable for gtest_example" >> CMakeLists.tmp
echo "add_own_gtest(gtest_example gtest_example.cpp)" >> CMakeLists.tmp
echo "target_link_libraries(gtest_example ${NAME})" >> CMakeLists.tmp
echo "" >> CMakeLists.tmp
echo "################# ADD YOUR TESTS BELOW ####################" >> CMakeLists.tmp
echo "# #" >> CMakeLists.tmp
echo "# ==== IN ALPHABETICAL ORDER! ==== #" >> CMakeLists.tmp
echo "" >> CMakeLists.tmp
mv CMakeLists.tmp $TESTS_DIR/CMakeLists.txt
#Set the project name on the Findlib.cmake file
sed 's/header_file/'"${NAME}.h"'/g' <$TEMPLATES_PATH/Findlib_template.cmake >tmp.cmake
sed 's/project_name/'$ORIGNAME'/g' <tmp.cmake >tmp2.cmake
sed 's/library_name/'$NAME'/g' <tmp2.cmake >$ORIGNAME/Find$NAME.cmake
rm tmp.cmake
rm tmp2.cmake
LIBRARY_NAME=$(echo $NAME | tr '[:lower:]' '[:upper:]')
Library_name=$(echo $NAME | sed 's/\([a-zA-Z]\)\([a-zA-Z0-9]*\)/\u\1\2/g')
sed 's/Library_name/'$Library_name'/g' <$TEMPLATES_PATH/library_header_template.h >tmp.h
sed 's/LIBRARY_NAME/'$LIBRARY_NAME'/g' <tmp.h >$ORIGNAME/src/$NAME.h
rm tmp.h
sed 's/library_name/'$NAME'/g' <$TEMPLATES_PATH/library_src_template.cpp >tmp.cpp
sed 's/Library_name/'$Library_name'/g' <tmp.cpp >tmp2.cpp
sed 's/Library_name/'$Library_name'/g' <tmp2.cpp >$ORIGNAME/src/$NAME.cpp
rm tmp.cpp
rm tmp2.cpp
sed 's/library_name/'$NAME'/g' <$TEMPLATES_PATH/example_src_template.cpp >$ORIGNAME/src/examples/${NAME}_test.cpp
sed 's/library_name/'$NAME'/g' <$TEMPLATES_PATH/main_template.dox >tmp.dox
sed 's/project_name/'"$ORIGNAME"'/g' <tmp.dox >$ORIGNAME/doc/main.dox
rm tmp.dox
#create gitignores
echo " > Creating gitignore files"
if [ -f $BIN_DIR/.gitignore ]
then
echo " ! gitignore file already exist in bin directory, skipping ..."
else
echo " > creating .gitignore file in bin directory"
cp $SCRIPTS_PATH/cpp_project_template/gitignore_template $BIN_DIR/.gitignore
fi
if [ -f $BUILD_DIR/.gitignore ]
then
echo " ! gitignore file already exist in build directory, skipping ..."
else
echo " > creating .gitignore file in build directory"
cp $SCRIPTS_PATH/cpp_project_template/gitignore_template $BUILD_DIR/.gitignore
fi
if [ -f $LIB_DIR/.gitignore ]
then
echo " ! gitignore file already exist in lib directory, skipping ..."
else
echo " > creating .gitignore file in lib directory"
cp $SCRIPTS_PATH/cpp_project_template/gitignore_template $LIB_DIR/.gitignore
fi
# ===== UNIT TESTING =====
CREATE_GITLABCI="N"
read -p "Do you want to add gitlab-CI (Y/n)?" yn
if [ -z $yn ]
then
yn="Y";
fi
case $yn in
[Yy]* ) CREATE_GITLABCI="Y";;
[Nn]* ) echo "Avoiding creating gitlab-CI features";;
* ) echo "Please answer yes or no.";;
esac
if [ $CREATE_GITLABCI="Y" ]
then
echo "Creataing gitlab-CI structures";
cp $SCRIPTS_PATH/cpp_project_template/gitlab_ci_template.yml $ORIGNAME/.gitlab-ci.yml
fi
echo "Project created."