src: remove remaining references of subclasses on AMethPropdef
[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: String
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 project = toolcontext.modelbuilder.android_project_for(compiler.mainmodule)
80 var short_project_name = compiler.mainmodule.name
81
82 var app_name = project.name
83 if app_name == null then app_name = compiler.mainmodule.name
84 print app_name
85
86 var app_package = project.java_package
87 if app_package == null then app_package = "org.nitlanguage.{short_project_name}"
88
89 var app_version = project.version
90 if app_version == null then app_version = "1.0"
91
92 var args = ["android", "-s",
93 "create", "project",
94 "--name", short_project_name,
95 "--target", "android-10",
96 "--path", android_project_root,
97 "--package", app_package,
98 "--activity", short_project_name]
99 toolcontext.exec_and_check(args)
100
101 # create compile_dir
102 var dir = "{android_project_root}/jni/"
103 if not dir.file_exists then dir.mkdir
104
105 dir = compile_dir
106 if not dir.file_exists then dir.mkdir
107
108 # compile normal C files
109 super(compiler, compile_dir, cfiles)
110
111 # Gather extra C files generated elsewhere than in super
112 for f in compiler.extern_bodies do
113 if f isa ExternCFile then cfiles.add(f.filename.basename(""))
114 end
115
116 ## Generate delagating makefile
117 dir = "{android_project_root}/jni/"
118 var file = new OFStream.open("{dir}/Android.mk")
119 file.write """
120 include $(call all-subdir-makefiles)
121 """
122 file.close
123
124 ### generate makefile into "{compile_dir}/Android.mk"
125 dir = compile_dir
126 file = new OFStream.open("{dir}/Android.mk")
127 file.write """
128 LOCAL_PATH := $(call my-dir)
129 include $(CLEAR_VARS)
130
131 LOCAL_CFLAGS := -D ANDROID
132 LOCAL_MODULE := main
133 LOCAL_SRC_FILES := \\
134 {{{cfiles.join(" \\\n")}}}
135 LOCAL_LDLIBS := -llog -landroid -lEGL -lGLESv1_CM -lz
136 LOCAL_STATIC_LIBRARIES := android_native_app_glue png
137
138 include $(BUILD_SHARED_LIBRARY)
139
140 $(call import-module,android/native_app_glue)
141 """
142 file.close
143
144 ### generate AndroidManifest.xml
145 dir = android_project_root
146 file = new OFStream.open("{dir}/AndroidManifest.xml")
147 file.write """<?xml version="1.0" encoding="utf-8"?>
148 <!-- BEGIN_INCLUDE(manifest) -->
149 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
150 package="{{{app_package}}}"
151 android:versionCode="1"
152 android:versionName="{{{app_version}}}"
153 android:debuggable="true">
154
155 <!-- This is the platform API where NativeActivity was introduced. -->
156 <uses-sdk android:minSdkVersion="9" />
157
158 <application
159 android:label="@string/app_name"
160 android:hasCode="true"
161 android:debuggable="true">
162
163 <!-- Our activity is the built-in NativeActivity framework class.
164 This will take care of integrating with our NDK code. -->
165 <activity android:name="android.app.NativeActivity"
166 android:label="@string/app_name"
167 android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
168 android:configChanges="orientation|keyboardHidden"
169 android:screenOrientation="portrait">
170 <!-- Tell NativeActivity the name of or .so -->
171 <meta-data android:name=\"{{{app_package}}}\"
172 android:value=\"{{{app_name}}}\" />
173 <intent-filter>
174 <action android:name="android.intent.action.MAIN" />
175 <category android:name="android.intent.category.LAUNCHER" />
176 </intent-filter>
177 </activity>
178
179 {{{project.manifest_application_lines.join("\n")}}}
180
181 </application>
182
183 {{{project.manifest_lines.join("\n")}}}
184
185 </manifest>
186 <!-- END_INCLUDE(manifest) -->
187 """
188 file.close
189
190 ### generate res/values/strings.xml
191 dir = "{android_project_root}/res/"
192 if not dir.file_exists then dir.mkdir
193 dir = "{dir}/values/"
194 if not dir.file_exists then dir.mkdir
195 file = new OFStream.open("{dir}/strings.xml")
196 file.write """<?xml version="1.0" encoding="utf-8"?>
197 <resources>
198 <string name="app_name">{{{app_name}}}</string>
199 </resources>"""
200 file.close
201
202 ### Link to png sources
203 # libpng is not available on Android NDK
204 # FIXME make obtionnal when we have alternatives to mnit
205 var nit_dir = toolcontext.nit_dir
206 var share_dir = "{nit_dir}/share/"
207 if nit_dir == null or not share_dir.file_exists then
208 print "Android project error: Nit share directory not found, please use the environment variable NIT_DIR"
209 exit 1
210 end
211 share_dir = share_dir.realpath
212 var target_png_dir = "{android_project_root}/jni/png"
213 if not target_png_dir.file_exists then
214 toolcontext.exec_and_check(["ln", "-s", "{share_dir}/png/", target_png_dir])
215 end
216
217 ### Link to assets (for mnit and others)
218 # This will be accessed from `android_project_root`
219 var mainmodule_dir = compiler.mainmodule.location.file.filename.dirname
220 var assets_dir = "{mainmodule_dir}/../assets"
221 if not assets_dir.file_exists then assets_dir = "{mainmodule_dir}/assets"
222 if assets_dir.file_exists then
223 assets_dir = assets_dir.realpath
224 var target_assets_dir = "{android_project_root}/assets"
225 if not target_assets_dir.file_exists then
226 toolcontext.exec_and_check(["ln", "-s", assets_dir, target_assets_dir])
227 end
228 end
229 end
230
231 redef fun write_makefile(compiler, compile_dir, cfiles)
232 do
233 # Do nothing, already done in `write_files`
234 end
235
236 redef fun compile_c_code(compiler, compile_dir)
237 do
238 # Compile C code (and thus Nit)
239 toolcontext.exec_and_check(["ndk-build", "-s", "-j", "4", "-C", android_project_root])
240
241 # Generate the apk
242 toolcontext.exec_and_check(["ant", "-q", "debug", "-f", android_project_root+"/build.xml"])
243
244 # Move the apk to the target
245 var outname = toolcontext.opt_output.value
246 if outname == null then outname = "{compiler.mainmodule.name}.apk"
247 toolcontext.exec_and_check(["mv", "{android_project_root}/bin/{compiler.mainmodule.name}-debug.apk", outname])
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