Repository navigation
Expand file tree
/
Copy pathcpp_build.bash
More file actions
executable file
·135 lines (110 loc) · 2.66 KB
/
Copy pathcpp_build.bash
File metadata and controls
executable file
·135 lines (110 loc) · 2.66 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
#!/bin/bash
#Useful debugging tools
#-o prevents masking of pipeline errors
#-u variables control
#-e exit immediately on non-zero exit status
set -euo pipefail
PROJECT_NAME="my_project"
INIT_GIT=false
command_exists() {
command -v "$1" >/dev/null 2>&1
}
#Basic documentation
display_usage() {
cat <<EOF
Usage: $0 [options]
Options:
-n, --name <name> Project name (default: my_project)
-g, --git Initialize git
-h, --help Displays this help message
EOF
exit 0
}
error_exit() {
echo "Error: $1" >&2
exit 1
}
# Check for required commands(cmake, make, gcc, git etc.)
for cmd in cmake make git; do
if ! command_exists "$cmd"; then
error_exit "'$cmd' is required but not installed."
fi
done
if [ $# -eq 0 ]; then
display_usage
fi
while [ $# -gt 0 ]; do
case "$1" in
-n|--name)
if [ -z "${2:-}" ]; then
error_exit "Project name not specified."
fi
PROJECT_NAME="$2"
shift 2
;;
-g|--git)
INIT_GIT=true
shift
;;
-h|--help)
display_usage
;;
*)
echo "Unknown option $1"
display_usage
;;
esac
done
# Validate project name i.e no spaces
if [[ -z "$PROJECT_NAME" || "$PROJECT_NAME" =~ [[:space:]] ]]; then
error_exit "Invalid project name: '$PROJECT_NAME'"
fi
echo -e "Creating project: \e[32$PROJECT_NAME"
mkdir -p "$PROJECT_NAME" || error_exit "Failed to create directory."
#Most basic CMake configuration, can be further completet if needed
cat >"$PROJECT_NAME/CMakeLists.txt" <<EOF
cmake_minimum_required(VERSION 3.28)
project(${PROJECT_NAME})
set(CMAKE_CXX_STANDARD 17)
add_executable(${PROJECT_NAME} main.cpp)
EOF
#My basic template, can be changed to whatever you like
cat >"$PROJECT_NAME/main.cpp" <<EOF
#include <iostream>
#include <vector>
#include <algorithm>
#define ll long long
using namespace std;
int main() {
return 0;
}
EOF
#Gitignore and clang-format initialization
cat > "$PROJECT_NAME/.gitignore" <<EOF
build/
*.o
*.obj
*.exe
*.log
.env
EOF
cat > "$PROJECT_NAME/.clang-format" <<EOF
BasedOnStyle: Google
IndentWidth: 4
ColumnLimit: 100
EOF
mkdir -p "$PROJECT_NAME/builds"
cd "$PROJECT_NAME/builds"
cmake .. || error_exit "CMake configuration failed."
make || error_exit "Build failed."
cd ../..
#Git initialization if the option -g was specified
if [ "$INIT_GIT" = true ]; then
cd "$PROJECT_NAME"
git init -q
git add .
git commit -m "Initial commit"
echo -e "\e[33mGit repository initialized"
cd ..
fi
echo -e "\e[32mProject \e[37m'$PROJECT_NAME' \e[32mcreated successfully."