src: introduce new constructors
[nit.git] / src / android_platform.nit
1 #
2 # Copyright 2014 Alexis Laferrière <alexis.laf@xymus.net>
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
7 #
8 # http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15
16 # Compile program for the Android platform
17 module android_platform
18
19 import platform
20 import abstract_compiler
21 import common_ffi
22 intrude import common_ffi::extra_java_files
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 default_outname(mainmodule) do return "{mainmodule.name}.apk"
54
55 redef fun write_files(compiler, compile_dir, cfiles)
56 do
57 var android_project_root = android_project_root.as(not null)
58 var project = toolcontext.modelbuilder.android_project_for(compiler.mainmodule)
59 var short_project_name = compiler.mainmodule.name
60 var release = toolcontext.opt_release.value
61
62 var app_name = project.name
63 if app_name == null then app_name = compiler.mainmodule.name
64
65 var app_package = project.java_package
66 if app_package == null then app_package = "org.nitlanguage.{short_project_name}"
67
68 var app_version = project.version
69 if app_version == null then app_version = "1.0"
70
71 var app_min_api = project.min_api
72 if app_min_api == null then app_min_api = 10
73
74 var app_target_api = project.target_api
75 if app_target_api == null then app_target_api = app_min_api
76
77 var app_max_api = ""
78 if project.max_api != null then app_max_api = "android:maxSdkVersion=\"{project.max_api.as(not null)}\""
79
80 # Clear the previous android project, so there is no "existing project warning"
81 # or conflict between Java files of different projects
82 if android_project_root.file_exists then android_project_root.rmdir
83
84 var args = ["android", "-s",
85 "create", "project",
86 "--name", short_project_name,
87 "--target", "android-{app_target_api}",
88 "--path", android_project_root,
89 "--package", app_package,
90 "--activity", short_project_name]
91 toolcontext.exec_and_check(args, "Android project error")
92
93 # create compile_dir
94 var dir = "{android_project_root}/jni/"
95 if not dir.file_exists then dir.mkdir
96
97 dir = compile_dir
98 if not dir.file_exists then dir.mkdir
99
100 # compile normal C files
101 super(compiler, compile_dir, cfiles)
102
103 # Gather extra C files generated elsewhere than in super
104 for f in compiler.extern_bodies do
105 if f isa ExternCFile then cfiles.add(f.filename.basename(""))
106 end
107
108 # Is there an icon?
109 var resolutions = ["ldpi", "mdpi", "hdpi", "xhdpi", "xxhdpi", "xxxhdpi"]
110 var icon_available = false
111 for res in resolutions do
112 var path = "res/drawable-{res}/icon.png"
113 if path.file_exists then
114 icon_available = true
115 break
116 end
117 end
118
119 var icon_declaration
120 if icon_available then
121 icon_declaration = "android:icon=\"@drawable/icon\""
122 else icon_declaration = ""
123
124 # Also copy over the java files
125 dir = "{android_project_root}/src/"
126 var extra_java_files = compiler.mainmodule.extra_java_files
127 if extra_java_files != null then for file in extra_java_files do
128 var path = file.filename
129 path.file_copy_to("{dir}/{path.basename("")}")
130 end
131
132 ## Generate delagating makefile
133 dir = "{android_project_root}/jni/"
134 """
135 include $(call all-subdir-makefiles)
136 """.write_to_file("{dir}/Android.mk")
137
138 ### generate makefile into "{compile_dir}/Android.mk"
139 dir = compile_dir
140 """
141 LOCAL_PATH := $(call my-dir)
142 include $(CLEAR_VARS)
143
144 LOCAL_CFLAGS := -D ANDROID
145 LOCAL_MODULE := main
146 LOCAL_SRC_FILES := \\
147 {{{cfiles.join(" \\\n")}}}
148 LOCAL_LDLIBS := -llog -landroid -lEGL -lGLESv1_CM -lz
149 LOCAL_STATIC_LIBRARIES := android_native_app_glue png
150
151 include $(BUILD_SHARED_LIBRARY)
152
153 $(call import-module,android/native_app_glue)
154 """.write_to_file("{dir}/Android.mk")
155
156 ### generate AndroidManifest.xml
157 dir = android_project_root
158 """<?xml version="1.0" encoding="utf-8"?>
159 <!-- BEGIN_INCLUDE(manifest) -->
160 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
161 package="{{{app_package}}}"
162 android:versionCode="{{{project.version_code}}}"
163 android:versionName="{{{app_version}}}">
164
165 <!-- This is the platform API where NativeActivity was introduced. -->
166 <uses-sdk
167 android:minSdkVersion="{{{app_min_api}}}"
168 android:targetSdkVersion="{{{app_target_api}}}"
169 {{{app_max_api}}} />
170
171 <application
172 android:label="@string/app_name"
173 android:hasCode="true"
174 android:debuggable="{{{not release}}}">
175
176 <!-- Our activity is the built-in NativeActivity framework class.
177 This will take care of integrating with our NDK code. -->
178 <activity android:name="android.app.NativeActivity"
179 android:label="@string/app_name"
180 android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
181 android:configChanges="orientation|keyboardHidden"
182 android:screenOrientation="portrait"
183 {{{icon_declaration}}}>
184 <!-- Tell NativeActivity the name of or .so -->
185 <meta-data android:name=\"{{{app_package}}}\"
186 android:value=\"{{{app_name}}}\" />
187 <intent-filter>
188 <action android:name="android.intent.action.MAIN" />
189 <category android:name="android.intent.category.LAUNCHER" />
190 </intent-filter>
191 </activity>
192
193 {{{project.manifest_application_lines.join("\n")}}}
194
195 </application>
196
197 {{{project.manifest_lines.join("\n")}}}
198
199 </manifest>
200 <!-- END_INCLUDE(manifest) -->
201 """.write_to_file("{dir}/AndroidManifest.xml")
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 or else ""}/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], "Android project error")
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], "Android project error")
233 end
234 end
235
236 ### copy resources (for android)
237 # This will be accessed from `android_project_root`
238 var res_dir
239 if compiler.mainmodule.location.file != null then
240 # it is a real file, use "{file}/../res"
241 res_dir = "{compiler.mainmodule.location.file.filename.dirname}/../res"
242 else
243 # probably used -m, use "."
244 res_dir = "res"
245 end
246 if res_dir.file_exists then
247 # copy the res folder to .nit_compile
248 res_dir = res_dir.realpath
249 var target_res_dir = "{android_project_root}"
250 toolcontext.exec_and_check(["cp", "-R", res_dir, target_res_dir], "Android project error")
251 end
252
253 if not res_dir.file_exists or not "{res_dir}/values/strings.xml".file_exists then
254 # Create our own custom `res/values/string.xml` with the App name
255 """<?xml version="1.0" encoding="utf-8"?>
256 <resources>
257 <string name="app_name">{{{app_name}}}</string>
258 </resources>""".write_to_file "{dir}/res/values/strings.xml"
259 end
260 end
261
262 redef fun write_makefile(compiler, compile_dir, cfiles)
263 do
264 # Do nothing, already done in `write_files`
265 end
266
267 redef fun compile_c_code(compiler, compile_dir)
268 do
269 var android_project_root = android_project_root.as(not null)
270 var release = toolcontext.opt_release.value
271
272 # Compile C code (and thus Nit)
273 toolcontext.exec_and_check(["ndk-build", "-s", "-j", "-C", android_project_root], "Android project error")
274
275 # Generate the apk
276 var args = ["ant", "-q", "-f", android_project_root+"/build.xml"]
277 if release then
278 args.add "release"
279 else args.add "debug"
280 toolcontext.exec_and_check(args, "Android project error")
281
282 # Move the apk to the target
283 var outname = outfile(compiler.mainmodule)
284
285 var src_apk_suffix
286 if release then
287 src_apk_suffix = "release-unsigned"
288 else src_apk_suffix = "debug"
289
290 toolcontext.exec_and_check(["mv", "{android_project_root}/bin/{compiler.mainmodule.name}-{src_apk_suffix}.apk", outname], "Android project error")
291 end
292 end
293
294 redef class JavaClassTemplate
295 redef fun write_to_files(compdir)
296 do
297 var jni_path = "jni/nit_compile/"
298 if compdir.has_suffix(jni_path) then
299 var path = "{compdir.substring(0, compdir.length-jni_path.length)}/src/"
300 return super(path)
301 else return super
302 end
303 end