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
: String
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 project
= toolcontext
.modelbuilder
.android_project_for
(compiler
.mainmodule
)
80 var short_project_name
= compiler
.mainmodule
.name
82 var app_name
= project
.name
83 if app_name
== null then app_name
= compiler
.mainmodule
.name
86 var app_package
= project
.java_package
87 if app_package
== null then app_package
= "org.nitlanguage.{short_project_name}"
89 var app_version
= project
.version
90 if app_version
== null then app_version
= "1.0"
92 var args
= ["android", "-s",
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
)
102 var dir
= "{android_project_root}/jni/"
103 if not dir
.file_exists
then dir
.mkdir
106 if not dir
.file_exists
then dir
.mkdir
108 # compile normal C files
109 super(compiler
, compile_dir
, cfiles
)
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
(""))
116 ## Generate delagating makefile
117 dir
= "{android_project_root}/jni/"
118 var file
= new OFStream.open
("{dir}/Android.mk")
120 include $(call all-subdir-makefiles)
124 ### generate makefile into "{compile_dir}/Android.mk"
126 file
= new OFStream.open
("{dir}/Android.mk")
128 LOCAL_PATH := $(call my-dir)
129 include $(CLEAR_VARS)
131 LOCAL_CFLAGS := -D ANDROID
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
138 include $(BUILD_SHARED_LIBRARY)
140 $(call import-module,android/native_app_glue)
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">
155 <!-- This is the platform API where NativeActivity was introduced. -->
156 <uses-sdk android:minSdkVersion="9" />
159 android:label="@string/app_name"
160 android:hasCode="true"
161 android:debuggable="true">
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}}}\" />
174 <action android:name="android.intent.action.MAIN" />
175 <category android:name="android.intent.category.LAUNCHER" />
179 {{{project.manifest_application_lines.join("\n")}}}
183 {{{project.manifest_lines.join("\n")}}}
186 <!-- END_INCLUDE(manifest) -->
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"?>
198 <string name="app_name">{{{app_name}}}</string>
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"
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
])
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
])
231 redef fun write_makefile
(compiler
, compile_dir
, cfiles
)
233 # Do nothing, already done in `write_files`
236 redef fun compile_c_code
(compiler
, compile_dir
)
238 # Compile C code (and thus Nit)
239 toolcontext
.exec_and_check
(["ndk-build", "-s", "-j", "4", "-C", android_project_root
])
242 toolcontext
.exec_and_check
(["ant", "-q", "debug", "-f", android_project_root
+"/build.xml"])
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
])
251 redef class JavaClassTemplate
252 redef fun write_to_files
(compdir
)
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/"