Merge: Refactor FFI, the framework is now based on MModules
[nit.git] / src / android_platform.nit
1 # This file is part of NIT ( http://www.nitlanguage.org )t
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
32 fun exec_and_check(args: Array[String])
33 do
34 var prog = args.first
35 args.remove_at 0
36
37 # Is the wanted program available?
38 var proc_which = new IProcess.from_a("which", [prog])
39 proc_which.wait
40 var res = proc_which.status
41 if res != 0 then
42 print "Android project error: executable \"{prog}\" not found"
43 exit 1
44 end
45
46 # Execute the wanted program
47 var proc = new Process.from_a(prog, args)
48 proc.wait
49 res = proc.status
50 if res != 0 then
51 print "Android project error: execution of \"{prog} {args.join(" ")}\" failed"
52 exit 1
53 end
54 end
55 end
56
57 class AndroidPlatform
58 super Platform
59
60 redef fun supports_libunwind do return false
61
62 redef fun toolchain(toolcontext) do return new AndroidToolchain(toolcontext)
63 end
64
65 class AndroidToolchain
66 super MakefileToolchain
67
68 var android_project_root: nullable String = null
69
70 redef fun compile_dir
71 do
72 var normal_compile_dir = super
73 android_project_root = normal_compile_dir
74 return "{normal_compile_dir}/jni/nit_compile/"
75 end
76
77 redef fun write_files(compiler, compile_dir, cfiles)
78 do
79 var android_project_root = android_project_root.as(not null)
80 var project = toolcontext.modelbuilder.android_project_for(compiler.mainmodule)
81 var short_project_name = compiler.mainmodule.name
82
83 var app_name = project.name
84 if app_name == null then app_name = compiler.mainmodule.name
85 print app_name
86
87 var app_package = project.java_package
88 if app_package == null then app_package = "org.nitlanguage.{short_project_name}"
89
90 var app_version = project.version
91 if app_version == null then app_version = "1.0"
92
93 var args = ["android", "-s",
94 "create", "project",
95 "--name", short_project_name,
96 "--target", "android-10",
97 "--path", android_project_root,
98 "--package", app_package,
99 "--activity", short_project_name]
100 toolcontext.exec_and_check(args)
101
102 # create compile_dir
103 var dir = "{android_project_root}/jni/"
104 if not dir.file_exists then dir.mkdir
105
106 dir = compile_dir
107 if not dir.file_exists then dir.mkdir
108
109 # compile normal C files
110 super(compiler, compile_dir, cfiles)
111
112 # Gather extra C files generated elsewhere than in super
113 for f in compiler.extern_bodies do
114 if f isa ExternCFile then cfiles.add(f.filename.basename(""))
115 end
116
117 ## Generate delagating makefile
118 dir = "{android_project_root}/jni/"
119 var file = new OFStream.open("{dir}/Android.mk")
120 file.write """
121 include $(call all-subdir-makefiles)
122 """
123 file.close
124
125 ### generate makefile into "{compile_dir}/Android.mk"
126 dir = compile_dir
127 file = new OFStream.open("{dir}/Android.mk")
128 file.write """
129 LOCAL_PATH := $(call my-dir)
130 include $(CLEAR_VARS)
131
132 LOCAL_CFLAGS := -D ANDROID
133 LOCAL_MODULE := main
134 LOCAL_SRC_FILES := \\
135 {{{cfiles.join(" \\\n")}}}
136 LOCAL_LDLIBS := -llog -landroid -lEGL -lGLESv1_CM -lz
137 LOCAL_STATIC_LIBRARIES := android_native_app_glue png
138
139 include $(BUILD_SHARED_LIBRARY)
140
141 $(call import-module,android/native_app_glue)
142 """
143 file.close
144
145 ### generate AndroidManifest.xml
146 dir = android_project_root
147 file = new OFStream.open("{dir}/AndroidManifest.xml")
148 file.write """<?xml version="1.0" encoding="utf-8"?>
149 <!-- BEGIN_INCLUDE(manifest) -->
150 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
151 package="{{{app_package}}}"
152 android:versionCode="{{{project.version_code}}}"
153 android:versionName="{{{app_version}}}"
154 android:debuggable="true">
155
156 <!-- This is the platform API where NativeActivity was introduced. -->
157 <uses-sdk android:minSdkVersion="9" />
158
159 <application
160 android:label="@string/app_name"
161 android:hasCode="true"
162 android:debuggable="true">
163
164 <!-- Our activity is the built-in NativeActivity framework class.
165 This will take care of integrating with our NDK code. -->
166 <activity android:name="android.app.NativeActivity"
167 android:label="@string/app_name"
168 android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
169 android:configChanges="orientation|keyboardHidden"
170 android:screenOrientation="portrait">
171 <!-- Tell NativeActivity the name of or .so -->
172 <meta-data android:name=\"{{{app_package}}}\"
173 android:value=\"{{{app_name}}}\" />
174 <intent-filter>
175 <action android:name="android.intent.action.MAIN" />
176 <category android:name="android.intent.category.LAUNCHER" />
177 </intent-filter>
178 </activity>
179
180 {{{project.manifest_application_lines.join("\n")}}}
181
182 </application>
183
184 {{{project.manifest_lines.join("\n")}}}
185
186 </manifest>
187 <!-- END_INCLUDE(manifest) -->
188 """
189 file.close
190
191 ### generate res/values/strings.xml
192 dir = "{android_project_root}/res/"
193 if not dir.file_exists then dir.mkdir
194 dir = "{dir}/values/"
195 if not dir.file_exists then dir.mkdir
196 file = new OFStream.open("{dir}/strings.xml")
197 file.write """<?xml version="1.0" encoding="utf-8"?>
198 <resources>
199 <string name="app_name">{{{app_name}}}</string>
200 </resources>"""
201 file.close
202
203 ### Link to png sources
204 # libpng is not available on Android NDK
205 # FIXME make obtionnal when we have alternatives to mnit
206 var nit_dir = toolcontext.nit_dir
207 var share_dir = "{nit_dir}/share/"
208 if nit_dir == null or not share_dir.file_exists then
209 print "Android project error: Nit share directory not found, please use the environment variable NIT_DIR"
210 exit 1
211 end
212 share_dir = share_dir.realpath
213 var target_png_dir = "{android_project_root}/jni/png"
214 if not target_png_dir.file_exists then
215 toolcontext.exec_and_check(["ln", "-s", "{share_dir}/png/", target_png_dir])
216 end
217
218 ### Link to assets (for mnit and others)
219 # This will be accessed from `android_project_root`
220 var assets_dir
221 if compiler.mainmodule.location.file != null then
222 # it is a real file, use "{file}/../assets"
223 assets_dir = "{compiler.mainmodule.location.file.filename.dirname}/../assets"
224 else
225 # probably used -m, use "."
226 assets_dir = "assets"
227 end
228 if assets_dir.file_exists then
229 assets_dir = assets_dir.realpath
230 var target_assets_dir = "{android_project_root}/assets"
231 if not target_assets_dir.file_exists then
232 toolcontext.exec_and_check(["ln", "-s", assets_dir, target_assets_dir])
233 end
234 end
235 end
236
237 redef fun write_makefile(compiler, compile_dir, cfiles)
238 do
239 # Do nothing, already done in `write_files`
240 end
241
242 redef fun compile_c_code(compiler, compile_dir)
243 do
244 var android_project_root = android_project_root.as(not null)
245 # Compile C code (and thus Nit)
246 toolcontext.exec_and_check(["ndk-build", "-s", "-j", "4", "-C", android_project_root])
247
248 # Generate the apk
249 toolcontext.exec_and_check(["ant", "-q", "debug", "-f", android_project_root+"/build.xml"])
250
251 # Move the apk to the target
252 var outname = toolcontext.opt_output.value
253 if outname == null then outname = "{compiler.mainmodule.name}.apk"
254 toolcontext.exec_and_check(["mv", "{android_project_root}/bin/{compiler.mainmodule.name}-debug.apk", outname])
255 end
256 end
257
258 redef class JavaClassTemplate
259 redef fun write_to_files(compdir)
260 do
261 var jni_path = "jni/nit_compile/"
262 if compdir.has_suffix(jni_path) then
263 var path = "{compdir.substring(0, compdir.length-jni_path.length)}/src/"
264 return super(path)
265 else return super
266 end
267 end