File: prep_env.sh

package info (click to toggle)
gemrb 0.8.4-1
  • links: PTS, VCS
  • area: contrib
  • in suites: stretch
  • size: 32,272 kB
  • ctags: 18,427
  • sloc: cpp: 123,672; python: 31,319; ansic: 2,060; objc: 756; sh: 566; makefile: 466; perl: 178; java: 105
file content (273 lines) | stat: -rw-r--r-- 10,687 bytes parent folder | download
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
#!/bin/bash

# TODO: figure out, what exactly openal needs or try sdl-mixer2

GEMRB_GIT_PATH=$1
APILEVEL=${2:-17}
ENVROOT=${3:-$1/android}
GEMRB_VERSION=""

function get_sources {
  local url=$1
  local expected=$2

  if [[ -z $expected ]]; then
    # use last component of url
    expected=${url##*/}
    expected=${expected%.git}
  fi

  if [[ -d $expected ]]; then
    cd $expected &&
    git pull
    rc=$?
    cd -
    return $rc
  else
    git clone $url $expected
  fi
}

function build_vorbis {
  echo -en "Checking out libogg-vorbis.\n"
  get_sources https://github.com/nwertzberger/libogg-vorbis-android.git &&
  echo -en "Building libogg-vorbis...\n" &&
  pushd "$ENVROOT/libogg-vorbis-android/main" &&
  ndk-build &&
  popd &&
  echo -en "Done with libogg-vorbis.\n"
}

function build_openal {
  # this still only works with a copied android.h from pelya/commandergenius
  # ifdef SDLVERSION somethingsomething in OpenALAudio solves this
  echo -en "Checking out openal.\n"
  get_sources http://repo.or.cz/openal-soft/android.git openal &&
  echo -en "Building openal...\n" &&
  pushd "$ENVROOT/openal/android" &&
  ndk-build &&
  popd &&
  echo -en "Done with openal.\n"
}

function build_libpng {
  echo -en "Checking out libpng...\n"
  get_sources https://github.com/julienr/libpng-android.git &&
  echo -en "Building libpng...\n" &&
  pushd "$ENVROOT/libpng-android" &&
  ndk-build &&
  popd &&
  echo -en "Done with libpng.\n"
}

function get_freetype {
  # can't precompile freetype, at least not as it comes from upstream
  get_sources https://github.com/cdave1/freetype2-android.git
}

function build_deps {
  build_openal &&
  build_vorbis &&
  build_libpng &&
  get_freetype
}

function setup_dir_struct {
  echo -en "Checking out SDL2...\n"
  # get SDL2
  pushd "$ENVROOT" &&
  if [[ -d SDL ]]; then
    cd SDL
    hg update; rc=$?
    cd -
    (exit $rc) # hack to reset the hg return value
  else
    hg clone http://hg.libsdl.org/SDL
  fi &&
  # and do what it says in its README.android
  echo -en "Creating the directory structure for the project..." &&
  mkdir -p build &&
  cp -r "$ENVROOT/SDL/android-project" build/ &&
  cp -r "$ENVROOT/build/android-project" "$ENVROOT/build/gemrb" &&
  echo -en "Done.\n" &&
  echo -en "Symlinking the GemRB-git path..." &&
  mkdir -p "$ENVROOT/build/gemrb/jni" &&
  rm -fr "$ENVROOT/build/gemrb/jni/SDL" "$ENVROOT/build/gemrb/jni/src/main" &&
  ln -sf "$GEMRB_GIT_PATH" "$ENVROOT/build/gemrb/jni/src/main" &&
  ln -sf "$ENVROOT/SDL" "$ENVROOT/build/gemrb/jni/SDL"
}

function move_libraries {
  echo -en "Creating directories and copying Makefiles for prebuilt libraries...\n"

  # freetype2 is special, it's not supposed to be precompiled according to upstream,
  # additionally, upstream makefile is actually broken and we need makefiles in every
  # directory up to where the current one is stored, because ndk-build doesn't see
  # the right one otherwise
  # the alternative would probably be to store the makefile at the root of the
  # freetype directory, but im not sure in how far that messes with library placement
  cp -rf "$ENVROOT/freetype2-android" "$ENVROOT/build/gemrb/jni/" &&
  cp "$ENVROOT/FREETYPEBUILD_Android.mk" "$ENVROOT/build/gemrb/jni/freetype2-android/Android/jni/Android.mk" &&
  cp "$ENVROOT/RECURSE_Android.mk" "$ENVROOT/build/gemrb/jni/freetype2-android/Android/Android.mk" &&
  cp "$ENVROOT/RECURSE_Android.mk" "$ENVROOT/build/gemrb/jni/freetype2-android/Android.mk" &&
  # im not happy with this, but it's ok for now i guess

  mkdir -p build/gemrb/jni/{libogg,libvorbis,libpng,openal} &&
  cp "$ENVROOT/OGG_Android.mk" "$ENVROOT/build/gemrb/jni/libogg/Android.mk" &&
  cp "$ENVROOT/VORBIS_Android.mk" "$ENVROOT/build/gemrb/jni/libvorbis/Android.mk" &&
  cp "$ENVROOT/OPENAL_Android.mk" "$ENVROOT/build/gemrb/jni/openal/Android.mk" &&
  cp "$ENVROOT/PNG_Android.mk" "$ENVROOT/build/gemrb/jni/libpng/Android.mk" &&

  echo -en "Copying prebuilt libraries and linking header directories...\n" &&

  # libogg
  cp "$ENVROOT/libogg-vorbis-android/main/libs/armeabi/libogg.so" "$ENVROOT/build/gemrb/jni/libogg/" &&
  ln -sf "$ENVROOT/libogg-vorbis-android/main/jni/include/" "$ENVROOT/build/gemrb/jni/libogg/include" &&

  # vorbis
  cp "$ENVROOT/libogg-vorbis-android/main/libs/armeabi/libvorbis.so" "$ENVROOT/build/gemrb/jni/libvorbis/" &&
  ln -sf "$ENVROOT/libogg-vorbis-android/main/jni/include/" "$ENVROOT/build/gemrb/jni/libvorbis/include" &&
  # those two are a little bit messy, because they both need their include directory
  # this is because they can't both be defined as prebuilt libraries in the same makefile and directory,
  # because that messes with makefile variables for some reason

  # png
  cp "$ENVROOT/libpng-android/obj/local/armeabi/libpng.a" "$ENVROOT/build/gemrb/jni/libpng/" &&
  ln -sf "$ENVROOT/libpng-android/jni/" "$ENVROOT/build/gemrb/jni/libpng/include" &&

  # openal
  cp "$ENVROOT/openal/android/libs/armeabi/libopenal.so" "$ENVROOT/build/gemrb/jni/openal/" &&
  ln -sf "$ENVROOT/openal/include" "$ENVROOT/build/gemrb/jni/openal/include" &&

  # python
  libpython="libpython-py4a.tar.bz2"
  if [[ ! -f $libpython ]]; then
    wget "http://sourceforge.net/projects/gemrb/files/Other%20Binaries/android/$libpython" -P "$ENVROOT"
  fi &&
  tar -xf "$ENVROOT/$libpython" -C "$ENVROOT/build/gemrb/jni/" &&

  echo -en "Done.\n"
}

function prepare_config {
  template=$1
  out=$2

  cp -f "$template" "$out" &&
  # change/enable/override some defaults
  sed -i 's,^#UseSoftKeyboard,UseSoftKeyboard,' "$out" &&
  sed -i 's,^Bpp=.*,Bpp=16,' "$out" &&
  sed -i 's,^#\?AudioDriver.*,AudioDriver = openal,' "$out" &&
  sed -i 's,^Bpp=.*,Bpp=16,' "$out" &&
  sed -i 's,^#MouseFeedback=.*,MouseFeedback=3,' "$out" &&
  # unclear why these default clearings are needed
  # currently the activity doesn't do anything with them
  sed -i 's,@DATA_DIR@,,' "$out" &&
  sed -i 's,GameOverridePath=.*,GameOverridePath=,' "$out" &&
  sed -i 's,GameDataPath=.*,GameDataPath=,' "$out" &&
  # convenience and better defaults
  sed -i 's,SavePath=.*,SavePath=/sdcard/gemrb/bg2/,' "$out" &&
  sed -i 's,CachePath=.*,CachePath=/sdcard/gemrb/bg2/cache,' "$out" &&
  # replace the whole block
  sed -i '/^GamePath/,/^CD5/ c\
#GamePath=/storage/emulated/0/Android/data/net.sourceforge.gemrb/files/bg2/\
#CD1=/storage/emulated/0/Android/data/net.sourceforge.gemrb/files/bg2/data\
GamePath=/sdcard/gemrb/bg2\
CD1=/sdcard/gemrb/bg2/data\
# CD2=<CD2_PLACEHOLDER>\
# CD3=<CD3_PLACEHOLDER>\
# CD4=<CD4_PLACEHOLDER>\
# CD5=<CD5_PLACEHOLDER>\
' "$out"
}

function move_and_edit_projectfiles {
  echo -en "Copying and editing files..."
  mkdir -p "$ENVROOT/build/gemrb/src/net/sourceforge/gemrb/" &&

  # copy the gemrb activity
  cp "$ENVROOT/GemRB.java" "$ENVROOT/build/gemrb/src/net/sourceforge/gemrb/" &&

  # prepare and copy the config file
  mkdir -p "$ENVROOT/build/gemrb/assets" &&
  prepare_config "$GEMRB_GIT_PATH/gemrb/GemRB.cfg.sample.in" "$ENVROOT/packaged.GemRB.cfg" &&
  mv "$ENVROOT/packaged.GemRB.cfg" "$ENVROOT/build/gemrb/assets" &&

  mkdir -p "$ENVROOT/build/gemrb/res"/drawable-{l,m,h,xh,xxh}dpi &&
  # copy the icons
  cp "$GEMRB_GIT_PATH/artwork/gemrb-logo-glow-36px.png" "$ENVROOT/build/gemrb/res/drawable-ldpi/ic_launcher.png" &&
  cp "$GEMRB_GIT_PATH/artwork/gemrb-logo-glow-48px.png" "$ENVROOT/build/gemrb/res/drawable-mdpi/ic_launcher.png" &&
  cp "$GEMRB_GIT_PATH/artwork/gemrb-logo-glow-72px.png" "$ENVROOT/build/gemrb/res/drawable-hdpi/ic_launcher.png" &&
  cp "$GEMRB_GIT_PATH/artwork/gemrb-logo-glow-96px.png" "$ENVROOT/build/gemrb/res/drawable-xhdpi/ic_launcher.png" &&
  cp "$GEMRB_GIT_PATH/artwork/gemrb-logo-glow-144px.png" "$ENVROOT/build/gemrb/res/drawable-xxhdpi/ic_launcher.png" &&

  # copy the makefile
  cp "$ENVROOT/GEMRB_Android.mk" "$ENVROOT/build/gemrb/jni/src/Android.mk" &&

  # and the Application.mk
  cp "$ENVROOT/GEMRB_Application.mk" "$ENVROOT/build/gemrb/jni/Application.mk" &&
  echo -en "Done.\n" &&

  # add the neccessary libraries to the base activity
  echo -en "Performing neccessary edits...\n" &&
  app="$ENVROOT/build/gemrb/src/org/libsdl/app/SDLActivity.java"
  if ! grep -q python <<< "$app"; then
    sed -i 's/"SDL2_image",/&\n "ogg", "vorbis", "openal", "python2.6",/' "$app"
  fi &&

  sed -i -e 's,sdlFormat = 0x8,sdlFormat = 0x1,g' "$app" &&

  sed -i -e 's,SDL_app,GemRB,' "$ENVROOT/build/gemrb/jni/SDL/src/main/android/SDL_android_main.c" &&
  sed -i -e 's,//exit,exit,' "$ENVROOT/build/gemrb/jni/SDL/src/main/android/SDL_android_main.c" &&

  # change activity class and application name, as well as enable debuggable
  sed -i -e s,org.libsdl.app,net.sourceforge.gemrb, "$ENVROOT/build/gemrb/AndroidManifest.xml" &&
  sed -i -e s,SDLActivity,GemRB, "$ENVROOT/build/gemrb/AndroidManifest.xml" &&
  sed -i -e 's/android:name="GemRB"\s*$/& android:screenOrientation="landscape"/' "$ENVROOT/build/gemrb/AndroidManifest.xml" &&
  sed -i -e s,android:versionName=.*,android:versionName=$GEMRB_VERSION, "$ENVROOT/build/gemrb/AndroidManifest.xml" &&
  sed -i -e '21 a\
                 android:debuggable="true"' "$ENVROOT/build/gemrb/AndroidManifest.xml" &&
  sed -i -e s,SDL\ App,GemRB, build/gemrb/res/values/strings.xml &&

  echo -en "Copying GemRB override, unhardcoded and GUIScripts folders..." &&
  mkdir -p "$ENVROOT/build/gemrb/assets" &&
  cp -r "$ENVROOT/build/gemrb/jni/src/main/gemrb/override" "$ENVROOT/build/gemrb/assets/" &&
  cp -r "$ENVROOT/build/gemrb/jni/src/main/gemrb/unhardcoded" "$ENVROOT/build/gemrb/assets/" &&
  cp -r "$ENVROOT/build/gemrb/jni/src/main/gemrb/GUIScripts" "$ENVROOT/build/gemrb/assets/" &&

  echo -en "Done.\n"
}

function finished {
  popd # back from $ENVROOT
  local build_path=${ENVROOT##$PWD/}
  echo -en "That should be it, provided all the commands ran succesfully.\n\n" # TODO: Error checking beyond $1
  echo -en "To build:\n"
  echo -en "  cd $build_path/build/gemrb\n"
  echo -en "  ndk-build && ant debug\n\n"
  echo -en "alternatively, for ndk-gdb debuggable builds: \n"
  echo -en "  cd $build_path/build/gemrb\n"
  echo -en "  ndk-build NDK_DEBUG=1 && ant debug\n\n"
  echo -en "The finished apk will be $ENVROOT/build/gemrb/bin/SDLActivity-debug.apk\n\n"
}

# Flow control starts here

if [ -z "$1" ]
then
  echo -en "Error: No argument supplied.\n
Usage:
  $0 /absolute/path/to/gemrb/git (optional path to build dir)\n"
  exit 1
fi

GEMRB_VERSION=$(grep "#define VERSION_GEMRB" "$GEMRB_GIT_PATH/gemrb/includes/globals.h" | awk -F' ' '{print $3}') &&
setup_dir_struct &&
move_and_edit_projectfiles &&
build_deps &&
move_libraries &&
android update project -t android-$APILEVEL -p "$ENVROOT/build/gemrb" &&
finished || {
  echo 'Building failed!'
  exit 2
}