1 | class MRuby::Toolchain::Android
|
---|
2 | DEFAULT_ARCH = 'armeabi'
|
---|
3 | DEFAULT_PLATFORM = 'android-14'
|
---|
4 | DEFAULT_TOOLCHAIN = :gcc
|
---|
5 | DEFAULT_NDK_HOMES = %w{
|
---|
6 | /usr/local/opt/android-ndk
|
---|
7 | }
|
---|
8 | TOOLCHAINS = [:gcc, :clang]
|
---|
9 | ARCHITECTURES = %w{
|
---|
10 | armeabi armeabi-v7a arm64-v8a
|
---|
11 | mips mips64
|
---|
12 | x86 x86_64
|
---|
13 | }
|
---|
14 |
|
---|
15 | class AndroidNDKHomeNotFound < StandardError
|
---|
16 | def message
|
---|
17 | <<-EOM
|
---|
18 | Couldn't find Android NDK Home.
|
---|
19 | Set ANDROID_NDK_HOME environment variable or set :ndk_home parameter
|
---|
20 | EOM
|
---|
21 | end
|
---|
22 | end
|
---|
23 |
|
---|
24 | attr_reader :params
|
---|
25 |
|
---|
26 | def initialize(params)
|
---|
27 | @params = params
|
---|
28 | end
|
---|
29 |
|
---|
30 | def home_path
|
---|
31 | @home_path ||= Pathname(
|
---|
32 | params[:ndk_home] ||
|
---|
33 | ENV['ANDROID_NDK_HOME'] ||
|
---|
34 | DEFAULT_NDK_HOMES.find{ |path| File.directory?(path) } ||
|
---|
35 | raise(AndroidNDKHomeNotFound)
|
---|
36 | )
|
---|
37 | end
|
---|
38 |
|
---|
39 | def arch
|
---|
40 | params.fetch(:arch){ DEFAULT_ARCH }
|
---|
41 | end
|
---|
42 |
|
---|
43 | def platform
|
---|
44 | params.fetch(:platform){ DEFAULT_PLATFORM }
|
---|
45 | end
|
---|
46 |
|
---|
47 | def toolchain
|
---|
48 | params.fetch(:toolchain){ DEFAULT_TOOLCHAIN }
|
---|
49 | end
|
---|
50 |
|
---|
51 | def toolchain_version
|
---|
52 | params.fetch(:toolchain_version) do
|
---|
53 | test = case toolchain
|
---|
54 | when :gcc
|
---|
55 | case arch
|
---|
56 | when /armeabi/
|
---|
57 | 'arm-linux-androideabi-*'
|
---|
58 | when /arm64/
|
---|
59 | 'aarch64-linux-android-*'
|
---|
60 | when /mips64/
|
---|
61 | 'mips64el-linux-android-*'
|
---|
62 | when /mips/
|
---|
63 | 'mipsel-linux-android-*'
|
---|
64 | when /x86_64/
|
---|
65 | 'x86_64-*'
|
---|
66 | when /x86/
|
---|
67 | 'x86-*'
|
---|
68 | end
|
---|
69 | when :clang
|
---|
70 | 'llvm-*'
|
---|
71 | end
|
---|
72 |
|
---|
73 | Dir[home_path.join('toolchains',test)].map{|t| t.match(/-(\d+\.\d+)$/); $1.to_f }.max
|
---|
74 | end
|
---|
75 | end
|
---|
76 |
|
---|
77 | def toolchain_path
|
---|
78 | prefix = case toolchain
|
---|
79 | when :clang then 'llvm-'
|
---|
80 | when :gcc
|
---|
81 | case arch
|
---|
82 | when /armeabi/ then 'arm-linux-androideabi-'
|
---|
83 | when /arm64/ then 'aarch64-linux-android-'
|
---|
84 | when /x86_64/ then 'x86_64-'
|
---|
85 | when /x86/ then 'x86-'
|
---|
86 | when /mips64/ then 'mips64el-linux-android-'
|
---|
87 | when /mips/ then 'mipsel-linux-android-'
|
---|
88 | end
|
---|
89 | end
|
---|
90 | home_path.join('toolchains', prefix + toolchain_version.to_s, 'prebuilt', host_platform)
|
---|
91 | end
|
---|
92 |
|
---|
93 | def sysroot
|
---|
94 | path = case arch
|
---|
95 | when /armeabi/ then 'arch-arm'
|
---|
96 | when /arm64/ then 'arch-arm64'
|
---|
97 | when /x86_64/ then 'arch-x86_64'
|
---|
98 | when /x86/ then 'arch-x86'
|
---|
99 | when /mips64/ then 'arch-mips64'
|
---|
100 | when /mips/ then 'arch-mips'
|
---|
101 | end
|
---|
102 |
|
---|
103 | home_path.join('platforms', platform, path).to_s
|
---|
104 | end
|
---|
105 |
|
---|
106 | def bin(command)
|
---|
107 | command = command.to_s
|
---|
108 |
|
---|
109 | if toolchain == :gcc
|
---|
110 | command = case arch
|
---|
111 | when /armeabi/ then 'arm-linux-androideabi-'
|
---|
112 | when /arm64/ then 'aarch64-linux-android-'
|
---|
113 | when /x86_64/ then 'x86_64-linux-android-'
|
---|
114 | when /x86/ then 'i686-linux-android-'
|
---|
115 | when /mips64/ then 'mips64el-linux-android-'
|
---|
116 | when /mips/ then 'mipsel-linux-android-'
|
---|
117 | end + command
|
---|
118 | end
|
---|
119 |
|
---|
120 | toolchain_path.join('bin',command).to_s
|
---|
121 | end
|
---|
122 |
|
---|
123 | def cc
|
---|
124 | case toolchain
|
---|
125 | when :gcc then bin(:gcc)
|
---|
126 | when :clang then bin(:clang)
|
---|
127 | end
|
---|
128 | end
|
---|
129 |
|
---|
130 | def cflags
|
---|
131 | flags = []
|
---|
132 |
|
---|
133 | case toolchain
|
---|
134 | when :gcc
|
---|
135 | flags += %W(-ffunction-sections -funwind-tables -no-canonical-prefixes)
|
---|
136 | flags += %W(-D__android__ -mandroid --sysroot="#{sysroot}")
|
---|
137 | case arch
|
---|
138 | when /arm64/
|
---|
139 | flags += %W(-fpic -fstack-protector-strong)
|
---|
140 | when 'armeabi-v7a-hard'
|
---|
141 | flags += %W(-fpic -fstack-protector-strong -march=armv7-a -mhard-float -D_NDK_MATH_NO_SOFTFP=1 -mfpu=vfpv3-d16)
|
---|
142 | when 'armeabi-v7a'
|
---|
143 | flags += %W(-fpic -fstack-protector-strong -march=armv7-a -mfloat-abi=softfp -mfpu=vfpv3-d16)
|
---|
144 | when /arm/
|
---|
145 | flags += %W(-fpic -fstack-protector-strong -march=armv5te -mtune=xscale -msoft-float)
|
---|
146 | when /mips/
|
---|
147 | flags += %W(-fpic -fno-strict-aliasing -finline-functions -fmessage-length=0 -fno-inline-functions-called-once -fgcse-after-reload -frerun-cse-after-loop -frename-registers)
|
---|
148 | when /x86/
|
---|
149 | flags += %W(-fstack-protector-strong)
|
---|
150 | end
|
---|
151 | when :clang
|
---|
152 | end
|
---|
153 |
|
---|
154 | flags
|
---|
155 | end
|
---|
156 |
|
---|
157 | def ld
|
---|
158 | cc
|
---|
159 | end
|
---|
160 |
|
---|
161 | def ldflags
|
---|
162 | flags = []
|
---|
163 | case toolchain
|
---|
164 | when :gcc
|
---|
165 | flags += %W(-no-canonical-prefixes)
|
---|
166 | flags += %W(-D__android__ -mandroid --sysroot="#{sysroot}")
|
---|
167 | case arch
|
---|
168 | when 'armeabi-v7a-hard'
|
---|
169 | flags += %W(-march=armv7-a -Wl,--fix-cortex-a8 -Wl,--no-warn-mismatch -lm_hard)
|
---|
170 | when 'armeabi-v7a'
|
---|
171 | flags += %W(-march=armv7-a -Wl,--fix-cortex-a8)
|
---|
172 | end
|
---|
173 | end
|
---|
174 |
|
---|
175 | flags
|
---|
176 | end
|
---|
177 |
|
---|
178 | def ar
|
---|
179 | case toolchain
|
---|
180 | when :gcc then bin(:ar)
|
---|
181 | when :clang then bin('llvm-ar')
|
---|
182 | end
|
---|
183 | end
|
---|
184 |
|
---|
185 | def host_platform
|
---|
186 | case RUBY_PLATFORM
|
---|
187 | when /cygwin|mswin|mingw|bccwin|wince|emx/i
|
---|
188 | 'windows'
|
---|
189 | when /x86_64-darwin/i
|
---|
190 | 'darwin-x86_64'
|
---|
191 | when /darwin/i
|
---|
192 | 'darwin-x86'
|
---|
193 | when /x86_64-linux/i
|
---|
194 | 'linux-x86_64'
|
---|
195 | when /linux/i
|
---|
196 | 'linux-x86'
|
---|
197 | else
|
---|
198 | raise NotImplementedError, "Unknown host platform (#{RUBY_PLATFORM})"
|
---|
199 | end
|
---|
200 | end
|
---|
201 | end
|
---|
202 |
|
---|
203 | MRuby::Toolchain.new(:android) do |conf, params|
|
---|
204 | ndk = MRuby::Toolchain::Android.new(params)
|
---|
205 |
|
---|
206 | toolchain ndk.toolchain
|
---|
207 |
|
---|
208 | [conf.cc, conf.cxx, conf.objc, conf.asm].each do |cc|
|
---|
209 | cc.command = ndk.cc
|
---|
210 | cc.flags = ndk.cflags
|
---|
211 | end
|
---|
212 | conf.linker.command = ndk.ld
|
---|
213 | conf.linker.flags = ndk.ldflags
|
---|
214 | conf.archiver.command = ndk.ar
|
---|
215 | end
|
---|
216 |
|
---|