From 77cde5bb3acc79309454f65a28f5933564cc03fd Mon Sep 17 00:00:00 2001 From: Jayanth-L Date: Mon, 5 Aug 2019 13:13:23 -0700 Subject: [PATCH 1/2] Add android support, Update README.md Compiles and runs fine on Android platform --- README.md | 23 ++++++++++++++++++++++- SConstruct | 38 +++++++++++++++++++++++++++++++++++--- 2 files changed, 57 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index f9d5d2e9..31fc1a7e 100644 --- a/README.md +++ b/README.md @@ -76,8 +76,16 @@ $ cd godot-cpp $ scons platform= generate_bindings=yes $ cd .. ``` +For android: +Download latest Android NDK from official website and set NDK path. +``` +$ export PATH="$PATH:/PATH-TO-ANDROID-NDK/toolchains/llvm/prebuilt/linux-x86_64/bin/" +$ scons platform=android generate_bindings=yes +``` +you can also specify architecture by enabling bits=64 (or 32) default is 64 -> Replace `` with either `windows`, `linux` or `osx`. + +> Replace `` with either `windows`, `linux`, `osx` or `android`. > Include `use_llvm=yes` for using clang++ @@ -189,6 +197,19 @@ $ link /nologo /dll /out:bin\libtest.dll /implib:bin\libsimple.lib src\init.obj *macOS* For OSX you need to find out what compiler flags need to be used. +*Android* +``` +$ cd SimpleLibrary +$ aarch64-linux-android29-clang -fPIC -o src/init.os -c src/init.cpp -g -O3 -std=c++14 -Igodot-cpp/include -Igodot-cpp/include/core -Igodot-cpp/include/gen -Igodot-cpp/godot_headers +$ aarch64-linux-android29-clang -o bin/libtest.so -shared src/init.os -Lgodot-cpp/bin -l +``` +> use `armv7a-linux-androideabi29-clang` for 32 bit armeabi-v7a library + +> This creates the file `libtest.so` in your `SimpleLibrary/bin` directory. + +> You will need to replace `` with the file that was created in [**Compiling the cpp bindings library**](#compiling-the-cpp-bindings-library) + + ### Creating `.gdnlib` and `.gdns` files follow [godot_header/README.md](https://github.com/GodotNativeTools/godot_headers/blob/master/README.md#how-do-i-use-native-scripts-from-the-editor) to create the `.gdns` diff --git a/SConstruct b/SConstruct index 8d4a2a06..2f3f8113 100644 --- a/SConstruct +++ b/SConstruct @@ -29,7 +29,7 @@ opts.Add(EnumVariable( 'platform', 'Target platform', host_platform, - allowed_values=('linux', 'osx', 'windows'), + allowed_values=('linux', 'osx', 'windows', 'android'), ignorecase=2 )) opts.Add(EnumVariable( @@ -73,8 +73,14 @@ opts.Add(BoolVariable( 'Generate GDNative API bindings', False )) +opts.Add(EnumVariable( + 'android_api_level', + 'Target Android API Level', + '29', + ('29', '28', '27', '26') +)) -env = Environment() +env = Environment(ENV = os.environ) opts.Update(env) Help(opts.GenerateHelpText(env)) @@ -82,7 +88,8 @@ is64 = sys.maxsize > 2**32 if ( env['TARGET_ARCH'] == 'amd64' or env['TARGET_ARCH'] == 'emt64' or - env['TARGET_ARCH'] == 'x86_64' + env['TARGET_ARCH'] == 'x86_64' or + env['TARGET_ARCH'] == 'arm64-v8a' ): is64 = True @@ -119,6 +126,31 @@ if env['platform'] == 'linux': env.Append(CCFLAGS=['-m32']) env.Append(LINKFLAGS=['-m32']) +# Add android target as it works for both armeabi-v7a and arm64-v8a architectures +elif env['platform'] == 'android': + # Use clang by default on android(NDK provides only clang) + env['use_llvm'] = 'yes' + if env['use_llvm']: + if(env['bits'] == '64'): + env['CXX'] = 'aarch64-linux-android' + env['android_api_level'] + '-clang++' + elif(env['bits'] == '32'): + env['CXX'] = 'armv7a-linux-androideabi' + env['android_api_level'] +'-clang++' + + env.Append(CCFLAGS=['-fPIC', '-g', '-std=c++14', '-Wwrite-strings']) + env.Append(LINKFLAGS=["-Wl,-R,'$$ORIGIN'"]) + + if env['target'] == 'debug': + env.Append(CCFLAGS=['-Og']) + elif env['target'] == 'release': + env.Append(CCFLAGS=['-O3']) + + if env['bits'] == '64': + env.Append(CCFLAGS=['-m64']) + env.Append(LINKFLAGS=['-m64']) + elif env['bits'] == '32': + env.Append(CCFLAGS=['-m32']) + env.Append(LINKFLAGS=['-m32']) + elif env['platform'] == 'osx': # Use Clang on macOS by default env['CXX'] = 'clang++' From 7482074779722df2b704e28ef352dd7bf80cc448 Mon Sep 17 00:00:00 2001 From: TGRCDev Date: Wed, 21 Aug 2019 01:03:49 -0700 Subject: [PATCH 2/2] Android compile fixed for Windows --- README.md | 8 +-- SConstruct | 141 +++++++++++++++++++++++++++++++++++++++++------------ 2 files changed, 113 insertions(+), 36 deletions(-) diff --git a/README.md b/README.md index 31fc1a7e..b8d9cddc 100644 --- a/README.md +++ b/README.md @@ -77,12 +77,12 @@ $ scons platform= generate_bindings=yes $ cd .. ``` For android: -Download latest Android NDK from official website and set NDK path. +Download the latest [Android NDK](https://developer.android.com/ndk/downloads) from the official website and set NDK path. ``` -$ export PATH="$PATH:/PATH-TO-ANDROID-NDK/toolchains/llvm/prebuilt/linux-x86_64/bin/" -$ scons platform=android generate_bindings=yes +$ scons platform=android generate_bindings=yes ANDROID_NDK_ROOT="/PATH-TO-ANDROID-NDK/" android_arch=< > ``` -you can also specify architecture by enabling bits=64 (or 32) default is 64 +`android_arch` can be `armv7, arm64v8, x86, x86_64`. +`ANDROID_NDK_ROOT` can also be set in the environment variables of your computer if you do not want to include it in your Scons call. > Replace `` with either `windows`, `linux`, `osx` or `android`. diff --git a/SConstruct b/SConstruct index 2f3f8113..65a26b86 100644 --- a/SConstruct +++ b/SConstruct @@ -3,6 +3,41 @@ import os import sys +# Workaround for MinGW. See: +# http://www.scons.org/wiki/LongCmdLinesOnWin32 +if (os.name=="nt"): + import subprocess + + def mySubProcess(cmdline,env): + #print "SPAWNED : " + cmdline + startupinfo = subprocess.STARTUPINFO() + startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW + proc = subprocess.Popen(cmdline, stdin=subprocess.PIPE, stdout=subprocess.PIPE, + stderr=subprocess.PIPE, startupinfo=startupinfo, shell = False, env = env) + data, err = proc.communicate() + rv = proc.wait() + if rv: + print("=====") + print(err.decode("utf-8")) + print("=====") + return rv + + def mySpawn(sh, escape, cmd, args, env): + + newargs = ' '.join(args[1:]) + cmdline = cmd + " " + newargs + + rv=0 + if len(cmdline) > 32000 and cmd.endswith("ar") : + cmdline = cmd + " " + args[1] + " " + args[2] + " " + for i in range(3,len(args)) : + rv = mySubProcess( cmdline + args[i], env ) + if rv : + break + else: + rv = mySubProcess( cmdline, env ) + + return rv def add_sources(sources, dir, extension): for f in os.listdir(dir): @@ -74,11 +109,21 @@ opts.Add(BoolVariable( False )) opts.Add(EnumVariable( - 'android_api_level', - 'Target Android API Level', - '29', - ('29', '28', '27', '26') + 'android_arch', + 'Target Android architecture', + 'armv7', + ['armv7','arm64v8','x86','x86_64'] )) +opts.Add( + 'android_api_level', + 'Target Android API level', + '18' if ARGUMENTS.get("android_arch", 'armv7') in ['armv7', 'x86'] else '21' +) +opts.Add( + 'ANDROID_NDK_ROOT', + 'Path to your Android NDK installation. By default, uses ANDROID_NDK_ROOT from your defined environment variables.', + os.environ.get("ANDROID_NDK_ROOT", None) +) env = Environment(ENV = os.environ) opts.Update(env) @@ -99,7 +144,7 @@ if env['bits'] == 'default': # This makes sure to keep the session environment variables on Windows. # This way, you can run SCons in a Visual Studio 2017 prompt and it will find # all the required tools -if host_platform == 'windows': +if host_platform == 'windows' and env['platform'] != 'android': if env['bits'] == '64': env = Environment(TARGET_ARCH='amd64') elif env['bits'] == '32': @@ -126,31 +171,6 @@ if env['platform'] == 'linux': env.Append(CCFLAGS=['-m32']) env.Append(LINKFLAGS=['-m32']) -# Add android target as it works for both armeabi-v7a and arm64-v8a architectures -elif env['platform'] == 'android': - # Use clang by default on android(NDK provides only clang) - env['use_llvm'] = 'yes' - if env['use_llvm']: - if(env['bits'] == '64'): - env['CXX'] = 'aarch64-linux-android' + env['android_api_level'] + '-clang++' - elif(env['bits'] == '32'): - env['CXX'] = 'armv7a-linux-androideabi' + env['android_api_level'] +'-clang++' - - env.Append(CCFLAGS=['-fPIC', '-g', '-std=c++14', '-Wwrite-strings']) - env.Append(LINKFLAGS=["-Wl,-R,'$$ORIGIN'"]) - - if env['target'] == 'debug': - env.Append(CCFLAGS=['-Og']) - elif env['target'] == 'release': - env.Append(CCFLAGS=['-O3']) - - if env['bits'] == '64': - env.Append(CCFLAGS=['-m64']) - env.Append(LINKFLAGS=['-m64']) - elif env['bits'] == '32': - env.Append(CCFLAGS=['-m32']) - env.Append(LINKFLAGS=['-m32']) - elif env['platform'] == 'osx': # Use Clang on macOS by default env['CXX'] = 'clang++' @@ -205,6 +225,62 @@ elif env['platform'] == 'windows': '-static-libgcc', '-static-libstdc++', ]) +elif env['platform'] == 'android': + if host_platform == 'windows': + env = env.Clone(tools=['mingw']) + env["SPAWN"] = mySpawn + + # Verify NDK root + if not 'ANDROID_NDK_ROOT' in env: + raise ValueError("To build for Android, ANDROID_NDK_ROOT must be defined. Please set ANDROID_NDK_ROOT to the root folder of your Android NDK installation.") + + # Validate API level + api_level = int(env['android_api_level']) + if env['android_arch'] in ['x86_64', 'arm64v8'] and api_level < 21: + print("WARN: 64-bit Android architectures require an API level of at least 21; setting android_api_level=21") + env['android_api_level'] = '21' + api_level = 21 + + # Setup toolchain + toolchain = env['ANDROID_NDK_ROOT'] + "/toolchains/llvm/prebuilt/" + if host_platform == "windows": + toolchain += "windows" + import platform as pltfm + if pltfm.machine().endswith("64"): + toolchain += "-x86_64" + elif host_platform == "linux": + toolchain += "linux-x86_64" + elif host_platform == "osx": + toolchain += "darwin-x86_64" + env.PrependENVPath('PATH', toolchain + "/bin") # This does nothing half of the time, but we'll put it here anyways + + # Get architecture info + arch_info_table = { + "armv7" : { + "march":"armv7-a", "target":"armv7a-linux-androideabi", "tool_path":"arm-linux-androideabi", "compiler_path":"armv7a-linux-androideabi", + "ccflags" : ['-mfpu=neon'] + }, + "arm64v8" : { + "march":"armv8-a", "target":"aarch64-linux-android", "tool_path":"aarch64-linux-android", "compiler_path":"aarch64-linux-android", + "ccflags" : [] + }, + "x86" : { + "march":"i686", "target":"i686-linux-android", "tool_path":"i686-linux-android", "compiler_path":"i686-linux-android", + "ccflags" : ['-mstackrealign'] + }, + "x86_64" : {"march":"x86-64", "target":"x86_64-linux-android", "tool_path":"x86_64-linux-android", "compiler_path":"x86_64-linux-android", + "ccflags" : [] + } + } + arch_info = arch_info_table[env['android_arch']] + + # Setup tools + env['CC'] = toolchain + "/bin/clang" + env['CXX'] = toolchain + "/bin/clang++" + env['AR'] = toolchain + "/bin/" + arch_info['tool_path'] + "-ar" + + env.Append(CCFLAGS=['--target=' + arch_info['target'] + env['android_api_level'], '-march=' + arch_info['march'], '-fPIC'])#, '-fPIE', '-fno-addrsig', '-Oz']) + env.Append(CCFLAGS=arch_info['ccflags']) env.Append(CPPPATH=[ '.', @@ -234,10 +310,11 @@ add_sources(sources, 'src/core', 'cpp') add_sources(sources, 'src/gen', 'cpp') library = env.StaticLibrary( - target='bin/' + 'libgodot-cpp.{}.{}.{}'.format( + target='bin/' + 'libgodot-cpp.{}.{}.{}{}'.format( env['platform'], env['target'], - env['bits'], + env['bits'] if env['platform'] != 'android' else env['android_arch'], + env['LIBSUFFIX'] ), source=sources ) Default(library)