-
Notifications
You must be signed in to change notification settings - Fork 3
/
make.sh
executable file
·270 lines (230 loc) · 9.09 KB
/
make.sh
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
#!/usr/bin/env bash
set -e
function errecho {
>&2 echo $@
}
# Customization variables.
CE3D2_BUILD_DIRECTORY=build
CE3D2_DOCS_BUILD_DIRECTORY=$CE3D2_BUILD_DIRECTORY/docs
CE3D2_DEBUG_BUILD_DIRECTORY=$CE3D2_BUILD_DIRECTORY/debug
CE3D2_RELEASE_BUILD_DIRECTORY=$CE3D2_BUILD_DIRECTORY/release
CE3D2_PUBLISH_DIRECTORY=$CE3D2_BUILD_DIRECTORY/publish
CMAKE_FLAGS=""
CMAKE_DEBUG_FLAGS="-DCMAKE_BUILD_TYPE=Debug -DTESTS_ENABLED=ON"
CMAKE_RELEASE_FLAGS="-DCMAKE_BUILD_TYPE=Release"
CMAKE_DOCS_FLAGS="-DCMAKE_BUILD_TYPE=Documentation"
# Executes the given task while displaying the task description.
function task {
echo -n $1
${*/$1/""}
echo " Done."
}
# Creates the given build directory.
function create_build_directory {
if ! test -d "$1"; then
task "Creating build directory..." \
mkdir -p "$1"
fi
}
# Performs the common build steps for all build targets.
# Arguments:
# 1: The build directory.
# 2: Additional flags to pass to CMake together with CMAKE_FLAGS.
# 3: The target for the generated makefile (can be left empty).
function build {
create_build_directory $1
working_dir=$(pwd)
cd $1
cmake $working_dir $CMAKE_FLAGS $2
make "${@:3}"
cd $working_dir
}
function TARGET_help {
echo "Following targets are available:"
echo
echo "help Displays this help."
echo
echo "debug Build CE3D2 (debug) into '$CE3D2_DEBUG_BUILD_DIRECTORY'."
echo " To decrease build time using parallel builds, you can"
echo " append '-j <number-of-processes>'."
echo "release Build CE3D2 (release) into" \
"'$CE3D2_RELEASE_BUILD_DIRECTORY'."
echo " To decrease build time using parallel builds, you can"
echo " append '-j <number-of-processes>'."
echo "docs Builds the HTML documentation into" \
"'$CE3D2_DOCS_BUILD_DIRECTORY/html'."
echo "install debug|release"
echo " Installs CE3D2 onto your system."
echo "publish Publishes CE3D2 binaries. This command creates also"
echo " necessary branches and commits to advance the versions"
echo " correctly. If a custom version (MAJOR.MINOR) is provided"
echo " as argument, this will be the next dev-version incremented"
echo " to. If no argument is given, the minor-version will be"
echo " incremented by one as the next dev-version."
echo "clean Clean up build files. You can specify explicitly with"
echo " multiple arguments which targets shall be cleaned up."
}
function TARGET_debug {
build $CE3D2_DEBUG_BUILD_DIRECTORY "$CMAKE_DEBUG_FLAGS" "$@"
}
function TARGET_release {
build $CE3D2_RELEASE_BUILD_DIRECTORY "$CMAKE_RELEASE_FLAGS" "$@"
}
function TARGET_docs {
build $CE3D2_DOCS_BUILD_DIRECTORY "$CMAKE_DOCS_FLAGS" docs
}
function TARGET_install {
if [ "$1" == "debug" ]; then
build $CE3D2_DEBUG_BUILD_DIRECTORY "$CMAKE_DEBUG_FLAGS" install
elif [ "$1" == "release" ]; then
build $CE3D2_RELEASE_BUILD_DIRECTORY "$CMAKE_RELEASE_FLAGS" install
else
echo "Invalid parameter for install target, use 'debug' or 'release'."
fi
}
function TARGET_clean {
function clean_debug {
task "Deleting debug build..." \
rm -rf $CE3D2_DEBUG_BUILD_DIRECTORY
}
function clean_release {
task "Deleting release build..." \
rm -rf $CE3D2_RELEASE_BUILD_DIRECTORY
}
function clean_docs {
task "Deleting documentation..." \
rm -rf $CE3D2_DOCS_BUILD_DIRECTORY
}
function clean_publish {
task "Deleting published artifacts..." \
rm -rf $CE3D2_PUBLISH_DIRECTORY
}
if [ $# -eq 0 ]; then
clean_debug
clean_release
clean_docs
clean_publish
else
for target in "$@"; do
clean_$target
done
fi
}
function TARGET_publish {
# Read the current CE3D2 version using the main CMakeLists.txt
CMAKE_LISTS_CONTENTS="$(cat CMakeLists.txt)"
regex="set\(CE3D2_VERSION_MAJOR ([0-9]+)\)"
[[ $CMAKE_LISTS_CONTENTS =~ $regex ]] && \
CE3D2_VERSION_MAJOR=${BASH_REMATCH[1]}
regex="set\(CE3D2_VERSION_MINOR ([0-9]+)\)"
[[ $CMAKE_LISTS_CONTENTS =~ $regex ]] && \
CE3D2_VERSION_MINOR=${BASH_REMATCH[1]}
regex="set\(CE3D2_VERSION_MICRO ([0-9]+|dev)\)"
[[ $CMAKE_LISTS_CONTENTS =~ $regex ]] && \
CE3D2_VERSION_MICRO=${BASH_REMATCH[1]}
CE3D2_VERSION="$CE3D2_VERSION_MAJOR.$CE3D2_VERSION_MINOR.$CE3D2_VERSION_MICRO"
if [ "$CE3D2_VERSION_MICRO" != "dev" ]; then
errecho "CE3D2's version is currently a release version ($CE3D2_VERSION)!"
errecho "Please change CE3D2_VERSION_MICRO back to 'dev' inside"
errecho "CMakeLists.txt before publishing again."
return 1
fi
# Save away current branch name to switch back again to in the end.
branch_name=$(git symbolic-ref -q HEAD)
branch_name=${branch_name##refs/heads/}
branch_name=${branch_name:-HEAD}
function replace {
grep -q "$1" "$3"
sed --in-place --expression "s/$1/$2/g" $3
}
# Start release modifications on a new branch. Stash away existing changes.
number_of_stashes=`git stash list | wc -l`
git stash
if [ "$number_of_stashes" -eq "$(git stash list | wc -l)" ]; then
stash_pop_needed="no"
else
stash_pop_needed="yes"
fi
branch_name_release="release-$CE3D2_VERSION_MAJOR.$CE3D2_VERSION_MINOR"
git checkout -b $branch_name_release
# Modify versions and other information in source.
replace "set(CE3D2_VERSION_MICRO dev)" "set(CE3D2_VERSION_MICRO 0)" CMakeLists.txt
replace "PROJECT_NUMBER\( *\)= \"\\\${PROJECT_VERSION} (at \\\${GIT_HEAD} on \\\${GIT_BRANCH})\"" "PROJECT_NUMBER\1= \"\\\${PROJECT_VERSION}\"" docs/doxyfile.in
# Stage all modified files and commit. Ignore all untracked files.
git commit -a -m "Release $CE3D2_VERSION_MAJOR.$CE3D2_VERSION_MINOR"
# Build all necessary targets for publishing.
TARGET_release
TARGET_debug
TARGET_docs
# Build archives.
mkdir -p $CE3D2_PUBLISH_DIRECTORY
function generate-archive-name {
echo "CE3D2-$CE3D2_VERSION_MAJOR-$CE3D2_VERSION_MINOR-x86_64-$1"
}
function build-archive {
local tempdir=`mktemp --directory`
local archive_name=`generate-archive-name $1`
mkdir -p $tempdir/$archive_name
cp -r "${@:2}" $tempdir/$archive_name
tar -czvf $CE3D2_PUBLISH_DIRECTORY/$archive_name.tar.gz -C $tempdir $archive_name
rm -rf $tempdir
}
build-archive release LICENSE $CE3D2_RELEASE_BUILD_DIRECTORY/CE3D2/libCE3D2.so
build-archive debug LICENSE $CE3D2_DEBUG_BUILD_DIRECTORY/CE3D2/libCE3D2.so
build-archive docs LICENSE $CE3D2_DOCS_BUILD_DIRECTORY/html/*
# Increment to next dev-version. Do that on a new branch.
if [ -z "$1" ]; then
CE3D2_NEXT_VERSION_MAJOR="$CE3D2_VERSION_MAJOR"
CE3D2_NEXT_VERSION_MINOR=$((CE3D2_VERSION_MINOR+1))
else
regex="([0-9]+)\.([0-9]+)"
[[ $1 =~ $regex ]] && \
CE3D2_NEXT_VERSION_MAJOR=${BASH_REMATCH[1]} && \
CE3D2_NEXT_VERSION_MINOR=${BASH_REMATCH[2]}
fi
branch_name_post_release="release-$CE3D2_NEXT_VERSION_MAJOR.$CE3D2_NEXT_VERSION_MINOR.dev"
git checkout -b $branch_name_post_release
replace "set(CE3D2_VERSION_MAJOR [0-9]\+)" "set(CE3D2_VERSION_MAJOR $CE3D2_NEXT_VERSION_MAJOR)" CMakeLists.txt
replace "set(CE3D2_VERSION_MINOR [0-9]\+)" "set(CE3D2_VERSION_MINOR $CE3D2_NEXT_VERSION_MINOR)" CMakeLists.txt
replace "set(CE3D2_VERSION_MICRO 0)" "set(CE3D2_VERSION_MICRO dev)" CMakeLists.txt
replace "PROJECT_NUMBER\( *\)= \"\\\${PROJECT_VERSION}\"" "PROJECT_NUMBER\1= \"\\\${PROJECT_VERSION} (at \\\${GIT_HEAD} on \\\${GIT_BRANCH})\"" docs/doxyfile.in
git commit -a -m "Increment version to $CE3D2_NEXT_VERSION_MAJOR.$CE3D2_NEXT_VERSION_MINOR.dev"
git checkout $branch_name
if [ "$stash_pop_needed" = "yes" ]; then
git stash pop
fi
echo
echo "Created following branches:"
echo "- $branch_name_release"
echo "- $branch_name_post_release"
echo
echo "To do a proper release now, take following steps:"
echo "1. Create a Pull-Request for $branch_name_release on GitHub."
echo "2. Merge the Pull-Request."
echo "3. Create a release tag on GitHub for the master branch. Attach following files:"
echo " - $CE3D2_PUBLISH_DIRECTORY/$(generate-archive-name release).tar.gz"
echo " - $CE3D2_PUBLISH_DIRECTORY/$(generate-archive-name debug).tar.gz"
echo " - $CE3D2_PUBLISH_DIRECTORY/$(generate-archive-name docs).tar.gz"
echo "4. Create a Pull-Request for $branch_name_post_release."
echo "5. Merge the Pull-Request."
}
# Redirect to target functions.
case $1 in
"help")
TARGET_help;;
"debug")
TARGET_debug "${@:2}";;
"release")
TARGET_release "${@:2}";;
"docs")
TARGET_docs;;
"install")
TARGET_install $2;;
"publish")
TARGET_publish $2;;
"clean")
TARGET_clean "${@:2}";;
*)
echo "Invalid target specified."
TARGET_help;;
esac