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