update code to comply with new superstring policy
[nit.git] / src / android_platform.nit
1 # This file is part of NIT ( http://www.nitlanguage.org )
2 #
3 # Copyright 2014 Alexis Laferrière <alexis.laf@xymus.net>
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16
17 # Compile program for the Android platform
18 module android_platform
19
20 import platform
21 import abstract_compiler
22 import common_ffi
23 import android_annotations
24
25 redef class ToolContext
26 redef fun platform_from_name(name)
27 do
28 if name == "android" then return new AndroidPlatform
29 return super
30 end
31 end
32
33 class AndroidPlatform
34 super Platform
35
36 redef fun supports_libunwind do return false
37
38 redef fun toolchain(toolcontext) do return new AndroidToolchain(toolcontext)
39 end
40
41 class AndroidToolchain
42 super MakefileToolchain
43
44 var android_project_root: nullable String = null
45
46 redef fun compile_dir
47 do
48 var normal_compile_dir = super
49 android_project_root = normal_compile_dir
50 return "{normal_compile_dir}/jni/nit_compile/"
51 end
52
53 redef fun write_files(compiler, compile_dir, cfiles)
54 do
55 var android_project_root = android_project_root.as(not null)
56 var project = toolcontext.modelbuilder.android_project_for(compiler.mainmodule)
57 var short_project_name = compiler.mainmodule.name
58 var release = toolcontext.opt_release.value
59
60 var app_name = project.name
61 if app_name == null then app_name = compiler.mainmodule.name
62
63 var app_package = project.java_package
64 if app_package == null then app_package = "org.nitlanguage.{short_project_name}"
65
66 var app_version = project.version
67 if app_version == null then app_version = "1.0"
68
69 var args = ["android", "-s",
70 "create", "project",
71 "--name", short_project_name,
72 "--target", "android-10",
73 "--path", android_project_root,
74 "--package", app_package,
75 "--activity", short_project_name]
76 toolcontext.exec_and_check(args, "Android project error")
77
78 # create compile_dir
79 var dir = "{android_project_root}/jni/"
80 if not dir.file_exists then dir.mkdir
81
82 dir = compile_dir
83 if not dir.file_exists then dir.mkdir
84
85 # compile normal C files
86 super(compiler, compile_dir, cfiles)
87
88 # Gather extra C files generated elsewhere than in super
89 for f in compiler.extern_bodies do
90 if f isa ExternCFile then cfiles.add(f.filename.basename(""))
91 end
92
93 ## Generate delagating makefile
94 dir = "{android_project_root}/jni/"
95 """
96 include $(call all-subdir-makefiles)
97 """.write_to_file("{dir}/Android.mk")
98
99 ### generate makefile into "{compile_dir}/Android.mk"
100 dir = compile_dir
101 """
102 LOCAL_PATH := $(call my-dir)
103 include $(CLEAR_VARS)
104
105 LOCAL_CFLAGS := -D ANDROID
106 LOCAL_MODULE := main
107 LOCAL_SRC_FILES := \\
108 {{{cfiles.join(" \\\n")}}}
109 LOCAL_LDLIBS := -llog -landroid -lEGL -lGLESv1_CM -lz
110 LOCAL_STATIC_LIBRARIES := android_native_app_glue png
111
112 include $(BUILD_SHARED_LIBRARY)
113
114 $(call import-module,android/native_app_glue)
115 """.write_to_file("{dir}/Android.mk")
116
117 ### generate AndroidManifest.xml
118 dir = android_project_root
119 """<?xml version="1.0" encoding="utf-8"?>
120 <!-- BEGIN_INCLUDE(manifest) -->
121 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
122 package="{{{app_package}}}"
123 android:versionCode="{{{project.version_code}}}"
124 android:versionName="{{{app_version}}}">
125
126 <!-- This is the platform API where NativeActivity was introduced. -->
127 <uses-sdk android:minSdkVersion="9" />
128
129 <application
130 android:label="@string/app_name"
131 android:hasCode="true"
132 android:debuggable="{{{not release}}}">
133
134 <!-- Our activity is the built-in NativeActivity framework class.
135 This will take care of integrating with our NDK code. -->
136 <activity android:name="android.app.NativeActivity"
137 android:label="@string/app_name"
138 android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
139 android:configChanges="orientation|keyboardHidden"
140 android:screenOrientation="portrait">
141 <!-- Tell NativeActivity the name of or .so -->
142 <meta-data android:name=\"{{{app_package}}}\"
143 android:value=\"{{{app_name}}}\" />
144 <intent-filter>
145 <action android:name="android.intent.action.MAIN" />
146 <category android:name="android.intent.category.LAUNCHER" />
147 </intent-filter>
148 </activity>
149
150 {{{project.manifest_application_lines.join("\n")}}}
151
152 </application>
153
154 {{{project.manifest_lines.join("\n")}}}
155
156 </manifest>
157 <!-- END_INCLUDE(manifest) -->
158 """.write_to_file("{dir}/AndroidManifest.xml")
159
160 ### generate res/values/strings.xml
161 dir = "{android_project_root}/res/"
162 if not dir.file_exists then dir.mkdir
163 dir = "{dir}/values/"
164 if not dir.file_exists then dir.mkdir
165 """<?xml version="1.0" encoding="utf-8"?>
166 <resources>
167 <string name="app_name">{{{app_name}}}</string>
168 </resources>
169 """.write_to_file("{dir}/strings.xml")
170
171 ### Link to png sources
172 # libpng is not available on Android NDK
173 # FIXME make obtionnal when we have alternatives to mnit
174 var nit_dir = toolcontext.nit_dir
175 var share_dir = "{nit_dir or else ""}/share/"
176 if nit_dir == null or not share_dir.file_exists then
177 print "Android project error: Nit share directory not found, please use the environment variable NIT_DIR"
178 exit 1
179 end
180 share_dir = share_dir.realpath
181 var target_png_dir = "{android_project_root}/jni/png"
182 if not target_png_dir.file_exists then
183 toolcontext.exec_and_check(["ln", "-s", "{share_dir}/png/", target_png_dir], "Android project error")
184 end
185
186 ### Link to assets (for mnit and others)
187 # This will be accessed from `android_project_root`
188 var assets_dir
189 if compiler.mainmodule.location.file != null then
190 # it is a real file, use "{file}/../assets"
191 assets_dir = "{compiler.mainmodule.location.file.filename.dirname}/../assets"
192 else
193 # probably used -m, use "."
194 assets_dir = "assets"
195 end
196 if assets_dir.file_exists then
197 assets_dir = assets_dir.realpath
198 var target_assets_dir = "{android_project_root}/assets"
199 if not target_assets_dir.file_exists then
200 toolcontext.exec_and_check(["ln", "-s", assets_dir, target_assets_dir], "Android project error")
201 end
202 end
203 end
204
205 redef fun write_makefile(compiler, compile_dir, cfiles)
206 do
207 # Do nothing, already done in `write_files`
208 end
209
210 redef fun compile_c_code(compiler, compile_dir)
211 do
212 var android_project_root = android_project_root.as(not null)
213 var release = toolcontext.opt_release.value
214
215 # Compile C code (and thus Nit)
216 toolcontext.exec_and_check(["ndk-build", "-s", "-j", "4", "-C", android_project_root], "Android project error")
217
218 # Generate the apk
219 var args = ["ant", "-q", "-f", android_project_root+"/build.xml"]
220 if release then
221 args.add "release"
222 else args.add "debug"
223 toolcontext.exec_and_check(args, "Android project error")
224
225 # Move the apk to the target
226 var outname = toolcontext.opt_output.value
227 if outname == null then outname = "{compiler.mainmodule.name}.apk"
228
229 var src_apk_suffix
230 if release then
231 src_apk_suffix = "release-unsigned"
232 else src_apk_suffix = "debug"
233
234 toolcontext.exec_and_check(["mv", "{android_project_root}/bin/{compiler.mainmodule.name}-{src_apk_suffix}.apk", outname], "Android project error")
235 end
236 end
237
238 redef class JavaClassTemplate
239 redef fun write_to_files(compdir)
240 do
241 var jni_path = "jni/nit_compile/"
242 if compdir.has_suffix(jni_path) then
243 var path = "{compdir.substring(0, compdir.length-jni_path.length)}/src/"
244 return super(path)
245 else return super
246 end
247 end