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