1 # This file is part of NIT ( http://www.nitlanguage.org )t
3 # Copyright 2014 Alexis Laferrière <alexis.laf@xymus.net>
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
9 # http://www.apache.org/licenses/LICENSE-2.0
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.
17 # Compile program for the Android platform
18 module android_platform
21 import abstract_compiler
23 import android_annotations
25 redef class ToolContext
26 redef fun platform_from_name
(name
)
28 if name
== "android" then return new AndroidPlatform
32 fun exec_and_check
(args
: Array[String])
37 # Is the wanted program available?
38 var proc_which
= new IProcess.from_a
("which", [prog
])
40 var res
= proc_which
.status
42 print
"Android project error: executable \"{prog}\
" not found"
46 # Execute the wanted program
47 var proc
= new Process.from_a
(prog
, args
)
51 print
"Android project error: execution of \"{prog} {args.join(" ")}\
" failed"
60 redef fun supports_libunwind
do return false
62 redef fun toolchain
(toolcontext
) do return new AndroidToolchain(toolcontext
)
65 class AndroidToolchain
66 super MakefileToolchain
68 var android_project_root
: nullable String = null
72 var normal_compile_dir
= super
73 android_project_root
= normal_compile_dir
74 return "{normal_compile_dir}/jni/nit_compile/"
77 redef fun write_files
(compiler
, compile_dir
, cfiles
)
79 var android_project_root
= android_project_root
.as(not null)
80 var project
= toolcontext
.modelbuilder
.android_project_for
(compiler
.mainmodule
)
81 var short_project_name
= compiler
.mainmodule
.name
83 var app_name
= project
.name
84 if app_name
== null then app_name
= compiler
.mainmodule
.name
87 var app_package
= project
.java_package
88 if app_package
== null then app_package
= "org.nitlanguage.{short_project_name}"
90 var app_version
= project
.version
91 if app_version
== null then app_version
= "1.0"
93 var args
= ["android", "-s",
95 "--name", short_project_name
,
96 "--target", "android-10",
97 "--path", android_project_root
,
98 "--package", app_package
,
99 "--activity", short_project_name
]
100 toolcontext
.exec_and_check
(args
)
103 var dir
= "{android_project_root}/jni/"
104 if not dir
.file_exists
then dir
.mkdir
107 if not dir
.file_exists
then dir
.mkdir
109 # compile normal C files
110 super(compiler
, compile_dir
, cfiles
)
112 # Gather extra C files generated elsewhere than in super
113 for f
in compiler
.extern_bodies
do
114 if f
isa ExternCFile then cfiles
.add
(f
.filename
.basename
(""))
117 ## Generate delagating makefile
118 dir
= "{android_project_root}/jni/"
119 var file
= new OFStream.open
("{dir}/Android.mk")
121 include $(call all-subdir-makefiles)
125 ### generate makefile into "{compile_dir}/Android.mk"
127 file
= new OFStream.open
("{dir}/Android.mk")
129 LOCAL_PATH := $(call my-dir)
130 include $(CLEAR_VARS)
132 LOCAL_CFLAGS := -D ANDROID
134 LOCAL_SRC_FILES := \\
135 {{{cfiles.join(" \\\n")}}}
136 LOCAL_LDLIBS := -llog -landroid -lEGL -lGLESv1_CM -lz
137 LOCAL_STATIC_LIBRARIES := android_native_app_glue png
139 include $(BUILD_SHARED_LIBRARY)
141 $(call import-module,android/native_app_glue)
145 ### generate AndroidManifest.xml
146 dir
= android_project_root
147 file
= new OFStream.open
("{dir}/AndroidManifest.xml")
148 file
.write
"""<?xml version="1.0" encoding="utf-8"?>
149 <!-- BEGIN_INCLUDE(manifest) -->
150 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
151 package="{{{app_package}}}"
152 android:versionCode="{{{project.version_code}}}"
153 android:versionName="{{{app_version}}}"
154 android:debuggable="true">
156 <!-- This is the platform API where NativeActivity was introduced. -->
157 <uses-sdk android:minSdkVersion="9" />
160 android:label="@string/app_name"
161 android:hasCode="true"
162 android:debuggable="true">
164 <!-- Our activity is the built-in NativeActivity framework class.
165 This will take care of integrating with our NDK code. -->
166 <activity android:name="android.app.NativeActivity"
167 android:label="@string/app_name"
168 android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
169 android:configChanges="orientation|keyboardHidden"
170 android:screenOrientation="portrait">
171 <!-- Tell NativeActivity the name of or .so -->
172 <meta-data android:name=\"{{{app_package}}}\"
173 android:value=\"{{{app_name}}}\" />
175 <action android:name="android.intent.action.MAIN" />
176 <category android:name="android.intent.category.LAUNCHER" />
180 {{{project.manifest_application_lines.join("\n")}}}
184 {{{project.manifest_lines.join("\n")}}}
187 <!-- END_INCLUDE(manifest) -->
191 ### generate res/values/strings.xml
192 dir
= "{android_project_root}/res/"
193 if not dir
.file_exists
then dir
.mkdir
194 dir
= "{dir}/values/"
195 if not dir
.file_exists
then dir
.mkdir
196 file
= new OFStream.open
("{dir}/strings.xml")
197 file
.write
"""<?xml version="1.0" encoding="utf-8"?>
199 <string name="app_name">{{{app_name}}}</string>
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}/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"
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
])
218 ### Link to assets (for mnit and others)
219 # This will be accessed from `android_project_root`
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"
225 # probably used -m, use "."
226 assets_dir
= "assets"
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
])
237 redef fun write_makefile
(compiler
, compile_dir
, cfiles
)
239 # Do nothing, already done in `write_files`
242 redef fun compile_c_code
(compiler
, compile_dir
)
244 var android_project_root
= android_project_root
.as(not null)
245 # Compile C code (and thus Nit)
246 toolcontext
.exec_and_check
(["ndk-build", "-s", "-j", "4", "-C", android_project_root
])
249 toolcontext
.exec_and_check
(["ant", "-q", "debug", "-f", android_project_root
+"/build.xml"])
251 # Move the apk to the target
252 var outname
= toolcontext
.opt_output
.value
253 if outname
== null then outname
= "{compiler.mainmodule.name}.apk"
254 toolcontext
.exec_and_check
(["mv", "{android_project_root}/bin/{compiler.mainmodule.name}-debug.apk", outname
])
258 redef class JavaClassTemplate
259 redef fun write_to_files
(compdir
)
261 var jni_path
= "jni/nit_compile/"
262 if compdir
.has_suffix
(jni_path
) then
263 var path
= "{compdir.substring(0, compdir.length-jni_path.length)}/src/"