2018-07-17 23:17:58 +00:00
|
|
|
from __future__ import print_function
|
2021-09-17 22:47:53 +00:00
|
|
|
|
2018-07-17 23:17:58 +00:00
|
|
|
import os
|
|
|
|
import os.path
|
|
|
|
import platform
|
2021-09-17 22:47:53 +00:00
|
|
|
import subprocess
|
|
|
|
import sys
|
2018-07-17 23:17:58 +00:00
|
|
|
|
2021-09-17 22:47:53 +00:00
|
|
|
PLATFORM_DIRS = {
|
|
|
|
'Windows': 'windows-x86_64',
|
|
|
|
'Linux': 'linux-x86_64',
|
|
|
|
'Darwin': 'darwin-x86-64',
|
|
|
|
}
|
2018-07-17 23:17:58 +00:00
|
|
|
|
2019-11-05 13:09:08 +00:00
|
|
|
# The values here must correspond with those in ../docker/prebuild.sh
|
2018-07-17 23:17:58 +00:00
|
|
|
BUILD_TARGETS = [
|
|
|
|
{
|
|
|
|
'arch': 'arm',
|
|
|
|
'goarch': 'arm',
|
|
|
|
'jni_dir': 'armeabi',
|
2022-11-27 20:53:18 +00:00
|
|
|
'cc': 'armv7a-linux-androideabi{}-clang',
|
2018-07-17 23:17:58 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
'arch': 'arm64',
|
|
|
|
'goarch': 'arm64',
|
|
|
|
'jni_dir': 'arm64-v8a',
|
2022-11-27 20:53:18 +00:00
|
|
|
'cc': 'aarch64-linux-android{}-clang',
|
2018-07-17 23:17:58 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
'arch': 'x86',
|
|
|
|
'goarch': '386',
|
|
|
|
'jni_dir': 'x86',
|
2022-11-27 20:53:18 +00:00
|
|
|
'cc': 'i686-linux-android{}-clang',
|
2019-11-05 10:02:24 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
'arch': 'x86_64',
|
|
|
|
'goarch': 'amd64',
|
|
|
|
'jni_dir': 'x86_64',
|
2022-11-27 20:53:18 +00:00
|
|
|
'cc': 'x86_64-linux-android{}-clang',
|
2018-07-17 23:17:58 +00:00
|
|
|
}
|
|
|
|
]
|
|
|
|
|
2021-09-17 22:47:53 +00:00
|
|
|
|
2018-07-17 23:17:58 +00:00
|
|
|
def fail(message, *args, **kwargs):
|
|
|
|
print((message % args).format(**kwargs))
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
|
|
|
|
def get_min_sdk(project_dir):
|
|
|
|
with open(os.path.join(project_dir, 'app', 'build.gradle')) as file_handle:
|
|
|
|
for line in file_handle:
|
|
|
|
tokens = list(filter(None, line.split()))
|
|
|
|
if len(tokens) == 2 and tokens[0] == 'minSdkVersion':
|
|
|
|
return int(tokens[1])
|
|
|
|
|
|
|
|
fail('Failed to find minSdkVersion')
|
|
|
|
|
|
|
|
|
|
|
|
def get_ndk_home():
|
|
|
|
if not os.environ.get('ANDROID_NDK_HOME', ''):
|
2021-12-08 09:55:50 +00:00
|
|
|
if not os.environ.get('NDK_VERSION', '') or not os.environ.get('ANDROID_HOME', ''):
|
|
|
|
fail('ANDROID_NDK_HOME or NDK_VERSION and ANDROID_HOME environment variable must be defined')
|
|
|
|
return os.path.join(os.environ['ANDROID_HOME'], 'ndk', os.environ['NDK_VERSION'])
|
2018-07-17 23:17:58 +00:00
|
|
|
return os.environ['ANDROID_NDK_HOME']
|
|
|
|
|
2021-09-17 22:47:53 +00:00
|
|
|
if platform.system() not in PLATFORM_DIRS:
|
2018-07-17 23:17:58 +00:00
|
|
|
fail('Unsupported python platform %s. Supported platforms: %s', platform.system(),
|
2021-09-17 22:47:53 +00:00
|
|
|
', '.join(PLATFORM_DIRS.keys()))
|
2018-07-17 23:17:58 +00:00
|
|
|
|
|
|
|
module_dir = os.path.dirname(os.path.realpath(__file__))
|
|
|
|
project_dir = os.path.realpath(os.path.join(module_dir, '..'))
|
2021-09-17 22:47:53 +00:00
|
|
|
# Use separate build dir so standalone ndk isn't deleted by `gradle clean`
|
2018-07-17 23:17:58 +00:00
|
|
|
build_dir = os.path.join(module_dir, 'gobuild')
|
|
|
|
go_build_dir = os.path.join(build_dir, 'go-packages')
|
|
|
|
syncthing_dir = os.path.join(module_dir, 'src', 'github.com', 'syncthing', 'syncthing')
|
|
|
|
min_sdk = get_min_sdk(project_dir)
|
|
|
|
|
|
|
|
# Make sure all tags are available for git describe
|
|
|
|
# https://github.com/syncthing/syncthing-android/issues/872
|
|
|
|
subprocess.check_call([
|
|
|
|
'git',
|
|
|
|
'-C',
|
|
|
|
syncthing_dir,
|
|
|
|
'fetch',
|
|
|
|
'--tags'
|
|
|
|
])
|
|
|
|
|
|
|
|
for target in BUILD_TARGETS:
|
2021-02-03 09:45:54 +00:00
|
|
|
|
|
|
|
print('Building syncthing for', target['arch'])
|
2018-07-17 23:17:58 +00:00
|
|
|
|
|
|
|
environ = os.environ.copy()
|
|
|
|
environ.update({
|
2019-02-26 19:49:57 +00:00
|
|
|
'GO111MODULE': 'on',
|
2018-07-17 23:17:58 +00:00
|
|
|
'CGO_ENABLED': '1',
|
|
|
|
})
|
|
|
|
|
2022-11-27 21:17:31 +00:00
|
|
|
cc = os.path.join(
|
2022-11-27 21:32:07 +00:00
|
|
|
get_ndk_home(), "toolchains", "llvm", "prebuilt",
|
|
|
|
PLATFORM_DIRS[platform.system()], "bin",
|
2022-11-27 21:17:31 +00:00
|
|
|
target['cc'].format(min_sdk))
|
2021-02-03 09:45:54 +00:00
|
|
|
subprocess.check_call(
|
|
|
|
['go', 'run', 'build.go', '-goos', 'android',
|
|
|
|
'-goarch', target['goarch'], '-cc', cc,
|
|
|
|
'-pkgdir', os.path.join(go_build_dir, target['goarch']),
|
|
|
|
'-no-upgrade', 'build'],
|
|
|
|
env=environ, cwd=syncthing_dir)
|
2018-07-17 23:17:58 +00:00
|
|
|
|
|
|
|
# Copy compiled binary to jniLibs folder
|
|
|
|
target_dir = os.path.join(project_dir, 'app', 'src', 'main', 'jniLibs', target['jni_dir'])
|
|
|
|
if not os.path.isdir(target_dir):
|
|
|
|
os.makedirs(target_dir)
|
|
|
|
target_artifact = os.path.join(target_dir, 'libsyncthing.so')
|
|
|
|
if os.path.exists(target_artifact):
|
|
|
|
os.unlink(target_artifact)
|
|
|
|
os.rename(os.path.join(syncthing_dir, 'syncthing'), target_artifact)
|
|
|
|
|
|
|
|
print('Finished build for', target['arch'])
|
|
|
|
|
|
|
|
print('All builds finished')
|