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