-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathollvm-build.sh
executable file
·152 lines (127 loc) · 3.89 KB
/
ollvm-build.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
#!/usr/bin/env bash
# vim: ts=2 sw=2
#
# Helper script for building Obfuscator-LLVM in a Docker container
#
# Copyright (c) 2017 Nick Diego Yamane <[email protected]>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
set -e
function show_usage() {
cat << EOF
Usage: ollvm-build.sh <path/to/ollvm/src>
Build a volume-mapped local Obfuscator-LLVM inside docker container.
If no arguments are given, 'bash' is executed in the build container
instead of the build script.
Available options:
-h|--help show this help message
-b|--build-only run only the build step (do not install)
Positional arguments:
path/to/ollvm/src the O-LLVM source directory path
All options after '--' are passed to CMake invocation.
EOF
}
# In docker-mode should be set in Dockerfile
# In host-mode, set using command-line arg
OLLVM_DIR=${OLLVM_DIR:-}
DOCKER_MODE=0
BUILD_ONLY=0
BUILD_DIR_NAME='build'
INSTALL_DIR_NAME='_installed_'
declare -a CMAKE_ARGS
declare -a FWD_ARGS
# Process command line arguments
while [ $# -gt 0 ]; do
case "$1" in
--)
shift
;;
-d|--docker)
DOCKER_MODE=1
shift
;;
-b|--build-only)
BUILD_ONLY=1
shift
;;
-h|--help)
show_usage
exit 0
;;
*)
if [ -d "$1" ]; then
OLLVM_DIR=$(readlink -f $1)
shift
else
CMAKE_ARGS+=( "$@" )
shift $#
fi
esac
done
# Process CMAKE_ARGS and generate build vars:
# BUILD_TYPE, BUILD_DIR, INSTALL_DIR
BUILD_TYPE=${BUILD_TYPE:-Release}
CMAKE_ARGS_HAVE_BUILD_TYPE=0
for i in ${CMAKE_ARGS[@]}; do
if grep "^-DCMAKE_BUILD_TYPE=" <<< "$i"; then
CMAKE_ARGS_HAVE_BUILD_TYPE=1
BUILD_TYPE="${i#-DCMAKE_BUILD_TYPE=}"
break
fi
done
(( CMAKE_ARGS_HAVE_BUILD_TYPE )) || CMAKE_ARGS+=( "-DCMAKE_BUILD_TYPE=${BUILD_TYPE}" )
BUILD_DIR="${OLLVM_DIR}/${BUILD_DIR_NAME}_${BUILD_TYPE,,}"
INSTALL_DIR="${BUILD_DIR}/${INSTALL_DIR_NAME}"
if (( DOCKER_MODE )); then
# Checking source folder sanity-check
if [ ! -f "$OLLVM_DIR/llvm.spec.in" ]; then
echo "Invalid O-LLVM source dir. Aborting..."
exit 1
fi
mkdir -p "$BUILD_DIR"
cd "$BUILD_DIR"
if [ ! -f 'build.ninja' ]; then
# FIXME: -DCMAKE_INSTALL_PREFIX not supported in DOCKER_MODE=0
# Currently it's always overwritten with BUILD_DIR/INSTALL_DIR_NAME
CMAKE_ARGS+=( "-DCMAKE_INSTALL_PREFIX='$INSTALL_DIR'" )
echo "Running cmake"
cmake -GNinja "${CMAKE_ARGS[@]}" "$OLLVM_DIR"
fi
ninja -j3
if (( BUILD_ONLY )); then
echo "Build done!"
exit 0
fi
ninja install
# TODO pakage install dir
else # script called from host
(( BUILD_ONLY )) && FWD_ARGS+=( --build-only )
if [ -z $OLLVM_DIR ]; then
echo "O-LLVM source dir not set. Entering in bash mode.."
show_usage
DOCKER_CMD='bash'
else
DOCKER_CMD="/scripts/ollvm-build.sh --docker ${FWD_ARGS[*]} ${CMAKE_ARGS:+-- ${CMAKE_ARGS[*]}}"
fi
DOCKER_IMAGE_NAME='nickdiego/ollvm-build'
DOCKER_CONTAINER_NAME='ollvm-build'
DOCKER_OPTS=( '--name' $DOCKER_CONTAINER_NAME --rm -v "$OLLVM_DIR:/ollvm/src" )
echo "##"
echo "## Starting O-LLVM build.."
echo "## Source dir : $OLLVM_DIR"
echo "## Docker image: $DOCKER_IMAGE_NAME"
echo "##"
docker run "${DOCKER_OPTS[@]}" -it $DOCKER_IMAGE_NAME $DOCKER_CMD
echo "Build finished successfully. Output directory: $BUILD_DIR"
fi