d5498d6dbfb7e4992e96f4f2327d3998f9fcdc17
[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 android_project_root = "{super}/android/"
49 self.android_project_root = android_project_root
50 return "{android_project_root}/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 # Clear the previous android project, so there is no "existing project warning"
70 # or conflict between Java files of different projects
71 if android_project_root.file_exists then android_project_root.rmdir
72
73 var args = ["android", "-s",
74 "create", "project",
75 "--name", short_project_name,
76 "--target", "android-10",
77 "--path", android_project_root,
78 "--package", app_package,
79 "--activity", short_project_name]
80 toolcontext.exec_and_check(args, "Android project error")
81
82 # create compile_dir
83 var dir = "{android_project_root}/jni/"
84 if not dir.file_exists then dir.mkdir
85
86 dir = compile_dir
87 if not dir.file_exists then dir.mkdir
88
89 # compile normal C files
90 super(compiler, compile_dir, cfiles)
91
92 # Gather extra C files generated elsewhere than in super
93 for f in compiler.extern_bodies do
94 if f isa ExternCFile then cfiles.add(f.filename.basename(""))
95 end
96
97 ## Generate delagating makefile
98 dir = "{android_project_root}/jni/"
99 """
100 include $(call all-subdir-makefiles)
101 """.write_to_file("{dir}/Android.mk")
102
103 ### generate makefile into "{compile_dir}/Android.mk"
104 dir = compile_dir
105 """
106 LOCAL_PATH := $(call my-dir)
107 include $(CLEAR_VARS)
108
109 LOCAL_CFLAGS := -D ANDROID
110 LOCAL_MODULE := main
111 LOCAL_SRC_FILES := \\
112 {{{cfiles.join(" \\\n")}}}
113 LOCAL_LDLIBS := -llog -landroid -lEGL -lGLESv1_CM -lz
114 LOCAL_STATIC_LIBRARIES := android_native_app_glue png
115
116 include $(BUILD_SHARED_LIBRARY)
117
118 $(call import-module,android/native_app_glue)
119 """.write_to_file("{dir}/Android.mk")
120
121 ### generate AndroidManifest.xml
122 dir = android_project_root
123 """<?xml version="1.0" encoding="utf-8"?>
124 <!-- BEGIN_INCLUDE(manifest) -->
125 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
126 package="{{{app_package}}}"
127 android:versionCode="{{{project.version_code}}}"
128 android:versionName="{{{app_version}}}">
129
130 <!-- This is the platform API where NativeActivity was introduced. -->
131 <uses-sdk android:minSdkVersion="9" />
132
133 <application
134 android:label="@string/app_name"
135 android:hasCode="true"
136 android:debuggable="{{{not release}}}">
137
138 <!-- Our activity is the built-in NativeActivity framework class.
139 This will take care of integrating with our NDK code. -->
140 <activity android:name="android.app.NativeActivity"
141 android:label="@string/app_name"
142 android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
143 android:configChanges="orientation|keyboardHidden"
144 android:screenOrientation="portrait">
145 <!-- Tell NativeActivity the name of or .so -->
146 <meta-data android:name=\"{{{app_package}}}\"
147 android:value=\"{{{app_name}}}\" />
148 <intent-filter>
149 <action android:name="android.intent.action.MAIN" />
150 <category android:name="android.intent.category.LAUNCHER" />
151 </intent-filter>
152 </activity>
153
154 {{{project.manifest_application_lines.join("\n")}}}
155
156 </application>
157
158 {{{project.manifest_lines.join("\n")}}}
159
160 </manifest>
161 <!-- END_INCLUDE(manifest) -->
162 """.write_to_file("{dir}/AndroidManifest.xml")
163
164 ### Link to png sources
165 # libpng is not available on Android NDK
166 # FIXME make obtionnal when we have alternatives to mnit
167 var nit_dir = toolcontext.nit_dir
168 var share_dir = "{nit_dir or else ""}/share/"
169 if nit_dir == null or not share_dir.file_exists then
170 print "Android project error: Nit share directory not found, please use the environment variable NIT_DIR"
171 exit 1
172 end
173 share_dir = share_dir.realpath
174 var target_png_dir = "{android_project_root}/jni/png"
175 if not target_png_dir.file_exists then
176 toolcontext.exec_and_check(["ln", "-s", "{share_dir}/png/", target_png_dir], "Android project error")
177 end
178
179 ### Link to assets (for mnit and others)
180 # This will be accessed from `android_project_root`
181 var assets_dir
182 if compiler.mainmodule.location.file != null then
183 # it is a real file, use "{file}/../assets"
184 assets_dir = "{compiler.mainmodule.location.file.filename.dirname}/../assets"
185 else
186 # probably used -m, use "."
187 assets_dir = "assets"
188 end
189 if assets_dir.file_exists then
190 assets_dir = assets_dir.realpath
191 var target_assets_dir = "{android_project_root}/assets"
192 if not target_assets_dir.file_exists then
193 toolcontext.exec_and_check(["ln", "-s", assets_dir, target_assets_dir], "Android project error")
194 end
195 end
196
197 ### copy resources (for android)
198 # This will be accessed from `android_project_root`
199 var res_dir
200 if compiler.mainmodule.location.file != null then
201 # it is a real file, use "{file}/../res"
202 res_dir = "{compiler.mainmodule.location.file.filename.dirname}/../res"
203 else
204 # probably used -m, use "."
205 res_dir = "res"
206 end
207 if res_dir.file_exists then
208 res_dir = res_dir.realpath
209 var target_res_dir = "{android_project_root}"
210 if target_res_dir.file_exists then
211 # copy the res folder to .nit_compile
212 toolcontext.exec_and_check(["cp", "-R", res_dir, target_res_dir], "Android project error")
213 end
214 end
215
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 var android_project_root = android_project_root.as(not null)
226 var release = toolcontext.opt_release.value
227
228 # Compile C code (and thus Nit)
229 toolcontext.exec_and_check(["ndk-build", "-s", "-j", "4", "-C", android_project_root], "Android project error")
230
231 # Generate the apk
232 var args = ["ant", "-q", "-f", android_project_root+"/build.xml"]
233 if release then
234 args.add "release"
235 else args.add "debug"
236 toolcontext.exec_and_check(args, "Android project error")
237
238 # Move the apk to the target
239 var outname = toolcontext.opt_output.value
240 if outname == null then outname = "{compiler.mainmodule.name}.apk"
241
242 var src_apk_suffix
243 if release then
244 src_apk_suffix = "release-unsigned"
245 else src_apk_suffix = "debug"
246
247 toolcontext.exec_and_check(["mv", "{android_project_root}/bin/{compiler.mainmodule.name}-{src_apk_suffix}.apk", outname], "Android project error")
248 end
249 end
250
251 redef class JavaClassTemplate
252 redef fun write_to_files(compdir)
253 do
254 var jni_path = "jni/nit_compile/"
255 if compdir.has_suffix(jni_path) then
256 var path = "{compdir.substring(0, compdir.length-jni_path.length)}/src/"
257 return super(path)
258 else return super
259 end
260 end