[270] | 1 | class MRuby::Toolchain::Android
|
---|
[331] | 2 |
|
---|
| 3 | DEFAULT_ARCH = 'armeabi' # TODO : Revise if arch should have a default
|
---|
| 4 |
|
---|
| 5 | DEFAULT_TOOLCHAIN = :clang
|
---|
| 6 |
|
---|
[270] | 7 | DEFAULT_NDK_HOMES = %w{
|
---|
[331] | 8 | /usr/local/opt/android-sdk/ndk-bundle
|
---|
[270] | 9 | /usr/local/opt/android-ndk
|
---|
[331] | 10 | %LOCALAPPDATA%/Android/android-sdk/ndk-bundle
|
---|
| 11 | %LOCALAPPDATA%/Android/android-ndk
|
---|
| 12 | ~/Library/Android/sdk/ndk-bundle
|
---|
| 13 | ~/Library/Android/ndk
|
---|
[270] | 14 | }
|
---|
[331] | 15 |
|
---|
| 16 | TOOLCHAINS = [:clang, :gcc]
|
---|
| 17 |
|
---|
[270] | 18 | ARCHITECTURES = %w{
|
---|
| 19 | armeabi armeabi-v7a arm64-v8a
|
---|
[331] | 20 | x86 x86_64
|
---|
[270] | 21 | mips mips64
|
---|
| 22 | }
|
---|
| 23 |
|
---|
| 24 | class AndroidNDKHomeNotFound < StandardError
|
---|
| 25 | def message
|
---|
| 26 | <<-EOM
|
---|
| 27 | Couldn't find Android NDK Home.
|
---|
| 28 | Set ANDROID_NDK_HOME environment variable or set :ndk_home parameter
|
---|
| 29 | EOM
|
---|
| 30 | end
|
---|
| 31 | end
|
---|
| 32 |
|
---|
[331] | 33 | class PlatformDirNotFound < StandardError
|
---|
| 34 | def message
|
---|
| 35 | <<-EOM
|
---|
| 36 | Couldn't find Android NDK platform directories.
|
---|
| 37 | Set ANDROID_PLATFORM environment variable or set :platform parameter
|
---|
| 38 | EOM
|
---|
| 39 | end
|
---|
| 40 | end
|
---|
| 41 |
|
---|
[270] | 42 | attr_reader :params
|
---|
| 43 |
|
---|
| 44 | def initialize(params)
|
---|
| 45 | @params = params
|
---|
| 46 | end
|
---|
| 47 |
|
---|
[331] | 48 | def bin_gcc(command)
|
---|
| 49 | command = command.to_s
|
---|
| 50 |
|
---|
| 51 | command = case arch
|
---|
| 52 | when /armeabi/ then 'arm-linux-androideabi-'
|
---|
| 53 | when /arm64-v8a/ then 'aarch64-linux-android-'
|
---|
| 54 | when /x86_64/ then 'x86_64-linux-android-'
|
---|
| 55 | when /x86/ then 'i686-linux-android-'
|
---|
| 56 | when /mips64/ then 'mips64el-linux-android-'
|
---|
| 57 | when /mips/ then 'mipsel-linux-android-'
|
---|
| 58 | end + command
|
---|
| 59 |
|
---|
| 60 | gcc_toolchain_path.join('bin', command).to_s
|
---|
| 61 | end
|
---|
| 62 |
|
---|
| 63 | def bin(command)
|
---|
| 64 | command = command.to_s
|
---|
| 65 | toolchain_path.join('bin', command).to_s
|
---|
| 66 | end
|
---|
| 67 |
|
---|
[270] | 68 | def home_path
|
---|
| 69 | @home_path ||= Pathname(
|
---|
| 70 | params[:ndk_home] ||
|
---|
[331] | 71 | ENV['ANDROID_NDK_HOME'] ||
|
---|
| 72 | DEFAULT_NDK_HOMES.find { |path|
|
---|
| 73 | path.gsub! '%LOCALAPPDATA%', ENV['LOCALAPPDATA'] || '%LOCALAPPDATA%'
|
---|
| 74 | path.gsub! '\\', '/'
|
---|
| 75 | path.gsub! '~', Dir.home || '~'
|
---|
| 76 | File.directory?(path)
|
---|
| 77 | } || raise(AndroidNDKHomeNotFound)
|
---|
[270] | 78 | )
|
---|
| 79 | end
|
---|
| 80 |
|
---|
| 81 | def toolchain
|
---|
[331] | 82 | @toolchain ||= params.fetch(:toolchain){ DEFAULT_TOOLCHAIN }
|
---|
[270] | 83 | end
|
---|
| 84 |
|
---|
[331] | 85 | def toolchain_path
|
---|
| 86 | @toolchain_path ||= case toolchain
|
---|
[270] | 87 | when :gcc
|
---|
[331] | 88 | gcc_toolchain_path
|
---|
[270] | 89 | when :clang
|
---|
[331] | 90 | home_path.join('toolchains', 'llvm' , 'prebuilt', host_platform)
|
---|
[270] | 91 | end
|
---|
[331] | 92 | end
|
---|
[270] | 93 |
|
---|
[331] | 94 | def gcc_toolchain_path
|
---|
| 95 | if @gcc_toolchain_path === nil then
|
---|
| 96 | prefix = case arch
|
---|
| 97 | when /armeabi/ then 'arm-linux-androideabi-'
|
---|
| 98 | when /arm64-v8a/ then 'aarch64-linux-android-'
|
---|
| 99 | when /x86_64/ then 'x86_64-'
|
---|
| 100 | when /x86/ then 'x86-'
|
---|
| 101 | when /mips64/ then 'mips64el-linux-android-'
|
---|
| 102 | when /mips/ then 'mipsel-linux-android-'
|
---|
| 103 | end
|
---|
| 104 |
|
---|
| 105 | test = case arch
|
---|
| 106 | when /armeabi/ then 'arm-linux-androideabi-*'
|
---|
| 107 | when /arm64-v8a/ then 'aarch64-linux-android-*'
|
---|
| 108 | when /x86_64/ then 'x86_64-*'
|
---|
| 109 | when /x86/ then 'x86-*'
|
---|
| 110 | when /mips64/ then 'mips64el-linux-android-*'
|
---|
| 111 | when /mips/ then 'mipsel-linux-android-*'
|
---|
| 112 | end
|
---|
| 113 |
|
---|
| 114 | gcc_toolchain_version = Dir[home_path.join('toolchains', test)].map{|t| t.match(/-(\d+\.\d+)$/); $1.to_f }.max
|
---|
| 115 | @gcc_toolchain_path = home_path.join('toolchains', prefix + gcc_toolchain_version.to_s, 'prebuilt', host_platform)
|
---|
[270] | 116 | end
|
---|
[331] | 117 | @gcc_toolchain_path
|
---|
[270] | 118 | end
|
---|
| 119 |
|
---|
[331] | 120 | def host_platform
|
---|
| 121 | @host_platform ||= case RUBY_PLATFORM
|
---|
| 122 | when /cygwin|mswin|mingw|bccwin|wince|emx/i
|
---|
| 123 | path = home_path.join('toolchains', 'llvm' , 'prebuilt', 'windows*')
|
---|
| 124 | Dir.glob(path.to_s){ |item|
|
---|
| 125 | next if File.file?(item)
|
---|
| 126 | path = Pathname(item)
|
---|
| 127 | break
|
---|
| 128 | }
|
---|
| 129 | path.basename
|
---|
| 130 | when /x86_64-darwin/i
|
---|
| 131 | 'darwin-x86_64'
|
---|
| 132 | when /darwin/i
|
---|
| 133 | 'darwin-x86'
|
---|
| 134 | when /x86_64-linux/i
|
---|
| 135 | 'linux-x86_64'
|
---|
| 136 | when /linux/i
|
---|
| 137 | 'linux-x86'
|
---|
| 138 | else
|
---|
| 139 | raise NotImplementedError, "Unknown host platform (#{RUBY_PLATFORM})"
|
---|
| 140 | end
|
---|
[270] | 141 | end
|
---|
| 142 |
|
---|
[331] | 143 | def arch
|
---|
| 144 | @arch ||= (params[:arch] || ENV['ANDROID_ARCH'] || DEFAULT_ARCH).to_s
|
---|
| 145 | end
|
---|
| 146 |
|
---|
[270] | 147 | def sysroot
|
---|
[331] | 148 | @sysroot ||= home_path.join('platforms', platform,
|
---|
| 149 | case arch
|
---|
| 150 | when /armeabi/ then 'arch-arm'
|
---|
| 151 | when /arm64-v8a/ then 'arch-arm64'
|
---|
| 152 | when /x86_64/ then 'arch-x86_64'
|
---|
| 153 | when /x86/ then 'arch-x86'
|
---|
| 154 | when /mips64/ then 'arch-mips64'
|
---|
| 155 | when /mips/ then 'arch-mips'
|
---|
| 156 | end
|
---|
| 157 | ).to_s
|
---|
| 158 | end
|
---|
[270] | 159 |
|
---|
[331] | 160 | def platform
|
---|
| 161 | if @platform === nil then
|
---|
| 162 | @platform = params[:platform] || ENV['ANDROID_PLATFORM'] || nil
|
---|
| 163 | if @platform === nil
|
---|
| 164 | Dir.glob(home_path.join('platforms/android-*').to_s){ |item|
|
---|
| 165 | next if File.file?(item)
|
---|
| 166 | if @platform === nil
|
---|
| 167 | @platform = Integer(item.rpartition('-')[2])
|
---|
| 168 | else
|
---|
| 169 | platform = Integer(item.rpartition('-')[2])
|
---|
| 170 | @platform = platform > @platform ? platform : @platform
|
---|
| 171 | end
|
---|
| 172 | }
|
---|
| 173 | if @platform === nil
|
---|
| 174 | raise(PlatformDirNotFound)
|
---|
| 175 | else
|
---|
| 176 | @platform = "android-#{@platform}"
|
---|
| 177 | end
|
---|
| 178 | end
|
---|
| 179 | end
|
---|
| 180 | if Integer(@platform.rpartition('-')[2]) < 21
|
---|
| 181 | case arch
|
---|
| 182 | when /arm64-v8a/, /x86_64/, /mips64/
|
---|
| 183 | raise NotImplementedError, "Platform (#{@platform}) has no implementation for architecture (#{arch})"
|
---|
| 184 | end
|
---|
| 185 | end
|
---|
| 186 | @platform
|
---|
[270] | 187 | end
|
---|
| 188 |
|
---|
[331] | 189 | def armeabi_v7a_mfpu
|
---|
| 190 | @armeabi_v7a_mfpu ||= (params[:mfpu] || 'vfpv3-d16').to_s
|
---|
| 191 | end
|
---|
[270] | 192 |
|
---|
[331] | 193 | def armeabi_v7a_mfloat_abi
|
---|
| 194 | @armeabi_v7a_mfloat_abi ||= (params[:mfloat_abi] || 'softfp').to_s
|
---|
| 195 | end
|
---|
| 196 |
|
---|
| 197 | def no_warn_mismatch
|
---|
| 198 | if %W(soft softfp).include? armeabi_v7a_mfloat_abi
|
---|
| 199 | ''
|
---|
| 200 | else
|
---|
| 201 | ',--no-warn-mismatch'
|
---|
[270] | 202 | end
|
---|
| 203 | end
|
---|
| 204 |
|
---|
| 205 | def cc
|
---|
| 206 | case toolchain
|
---|
[331] | 207 | when :gcc then bin_gcc('gcc')
|
---|
| 208 | when :clang then bin('clang')
|
---|
[270] | 209 | end
|
---|
| 210 | end
|
---|
| 211 |
|
---|
[331] | 212 | def ar
|
---|
| 213 | case toolchain
|
---|
| 214 | when :gcc then bin_gcc('ar')
|
---|
| 215 | when :clang then bin_gcc('ar')
|
---|
| 216 | end
|
---|
| 217 | end
|
---|
| 218 |
|
---|
| 219 | def ctarget
|
---|
[270] | 220 | flags = []
|
---|
| 221 |
|
---|
| 222 | case toolchain
|
---|
| 223 | when :gcc
|
---|
| 224 | case arch
|
---|
[331] | 225 | when /armeabi-v7a/ then flags += %W(-march=armv7-a)
|
---|
| 226 | when /armeabi/ then flags += %W(-march=armv5te)
|
---|
| 227 | when /arm64-v8a/ then flags += %W(-march=armv8-a)
|
---|
| 228 | when /x86_64/ then flags += %W(-march=x86-64)
|
---|
| 229 | when /x86/ then flags += %W(-march=i686)
|
---|
| 230 | when /mips64/ then flags += %W(-march=mips64r6)
|
---|
| 231 | when /mips/ then flags += %W(-march=mips32)
|
---|
[270] | 232 | end
|
---|
| 233 | when :clang
|
---|
[331] | 234 | case arch
|
---|
| 235 | when /armeabi-v7a/ then flags += %W(-target armv7-none-linux-androideabi)
|
---|
| 236 | when /armeabi/ then flags += %W(-target armv5te-none-linux-androideabi)
|
---|
| 237 | when /arm64-v8a/ then flags += %W(-target aarch64-none-linux-android)
|
---|
| 238 | when /x86_64/ then flags += %W(-target x86_64-none-linux-android)
|
---|
| 239 | when /x86/ then flags += %W(-target i686-none-linux-android)
|
---|
| 240 | when /mips64/ then flags += %W(-target mips64el-none-linux-android)
|
---|
| 241 | when /mips/ then flags += %W(-target mipsel-none-linux-android)
|
---|
| 242 | end
|
---|
[270] | 243 | end
|
---|
| 244 |
|
---|
[331] | 245 | case arch
|
---|
| 246 | when /armeabi-v7a/ then flags += %W(-mfpu=#{armeabi_v7a_mfpu} -mfloat-abi=#{armeabi_v7a_mfloat_abi})
|
---|
| 247 | when /armeabi/ then flags += %W(-mtune=xscale -msoft-float)
|
---|
| 248 | when /arm64-v8a/ then flags += %W()
|
---|
| 249 | when /x86_64/ then flags += %W()
|
---|
| 250 | when /x86/ then flags += %W()
|
---|
| 251 | when /mips64/ then flags += %W(-fmessage-length=0)
|
---|
| 252 | when /mips/ then flags += %W(-fmessage-length=0)
|
---|
| 253 | end
|
---|
| 254 |
|
---|
[270] | 255 | flags
|
---|
| 256 | end
|
---|
| 257 |
|
---|
[331] | 258 | def cflags
|
---|
| 259 | flags = []
|
---|
| 260 |
|
---|
| 261 | flags += %W(-MMD -MP -D__android__ -DANDROID --sysroot="#{sysroot}")
|
---|
| 262 | flags += ctarget
|
---|
| 263 | case toolchain
|
---|
| 264 | when :gcc
|
---|
| 265 | when :clang
|
---|
| 266 | flags += %W(-gcc-toolchain "#{gcc_toolchain_path.to_s}" -Wno-invalid-command-line-argument -Wno-unused-command-line-argument)
|
---|
| 267 | end
|
---|
| 268 | flags += %W(-fpic -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes)
|
---|
| 269 |
|
---|
| 270 | flags
|
---|
[270] | 271 | end
|
---|
| 272 |
|
---|
| 273 | def ldflags
|
---|
| 274 | flags = []
|
---|
[331] | 275 |
|
---|
| 276 | flags += %W(--sysroot="#{sysroot}")
|
---|
| 277 |
|
---|
| 278 | flags
|
---|
| 279 | end
|
---|
| 280 |
|
---|
| 281 | def ldflags_before_libraries
|
---|
| 282 | flags = []
|
---|
| 283 |
|
---|
[270] | 284 | case toolchain
|
---|
| 285 | when :gcc
|
---|
| 286 | case arch
|
---|
[331] | 287 | when /armeabi-v7a/ then flags += %W(-Wl#{no_warn_mismatch})
|
---|
[270] | 288 | end
|
---|
[331] | 289 | when :clang
|
---|
| 290 | flags += %W(-gcc-toolchain "#{gcc_toolchain_path.to_s}")
|
---|
| 291 | case arch
|
---|
| 292 | when /armeabi-v7a/ then flags += %W(-target armv7-none-linux-androideabi -Wl,--fix-cortex-a8#{no_warn_mismatch})
|
---|
| 293 | when /armeabi/ then flags += %W(-target armv5te-none-linux-androideabi)
|
---|
| 294 | when /arm64-v8a/ then flags += %W(-target aarch64-none-linux-android)
|
---|
| 295 | when /x86_64/ then flags += %W(-target x86_64-none-linux-android)
|
---|
| 296 | when /x86/ then flags += %W(-target i686-none-linux-android)
|
---|
| 297 | when /mips64/ then flags += %W(-target mips64el-none-linux-android)
|
---|
| 298 | when /mips/ then flags += %W(-target mipsel-none-linux-android)
|
---|
| 299 | end
|
---|
[270] | 300 | end
|
---|
[331] | 301 | flags += %W(-no-canonical-prefixes)
|
---|
| 302 |
|
---|
[270] | 303 | flags
|
---|
| 304 | end
|
---|
| 305 | end
|
---|
| 306 |
|
---|
| 307 | MRuby::Toolchain.new(:android) do |conf, params|
|
---|
[331] | 308 | android = MRuby::Toolchain::Android.new(params)
|
---|
[270] | 309 |
|
---|
[331] | 310 | toolchain android.toolchain
|
---|
[270] | 311 |
|
---|
| 312 | [conf.cc, conf.cxx, conf.objc, conf.asm].each do |cc|
|
---|
[331] | 313 | cc.command = android.cc
|
---|
| 314 | cc.flags = android.cflags
|
---|
[270] | 315 | end
|
---|
[331] | 316 |
|
---|
| 317 | conf.archiver.command = android.ar
|
---|
| 318 | conf.linker.command = android.cc
|
---|
| 319 | conf.linker.flags = android.ldflags
|
---|
| 320 | conf.linker.flags_before_libraries = android.ldflags_before_libraries
|
---|
[270] | 321 | end
|
---|