Fix android-build.py (#16727)

* Rejuvenate android-build.py

- replace deprecated OptionParser with ArgumentParser
- PEP8
- better shebang
- main() function

* Fix android-build.py

- resurrect '-p' parameter to be able to specify android platform in
  cocos console as '--ap' parameter
- correct usage message
- fix some misprints

* android_build.py: stricter error handling and better handling of duplicate targets

- handle duplicates like that './android-build.py cpp cpp'
- don't default to debug if some incorrect build mode passed, raise
This commit is contained in:
Alexander Poluektov 2016-10-27 09:32:47 +02:00 committed by minggo
parent ed2dac1ba4
commit cfcbbaa6e5
1 changed files with 62 additions and 82 deletions

View File

@ -1,120 +1,100 @@
#!/usr/bin/python #!/usr/bin/env python
# android-build.py # android-build.py
# Build android # Build android
import sys import sys
import os, os.path import os, os.path
from optparse import OptionParser
CPP_SAMPLES = ['cpp-empty-test', 'cpp-tests', 'game-controller-test'] CPP_SAMPLES = ['cpp-empty-test', 'cpp-tests', 'game-controller-test']
LUA_SAMPLES = ['lua-empty-test', 'lua-tests', 'lua-game-controller-test'] LUA_SAMPLES = ['lua-empty-test', 'lua-tests', 'lua-game-controller-test']
JS_SAMPLES = ['js-tests'] JS_SAMPLES = ['js-tests']
ALL_SAMPLES = CPP_SAMPLES + LUA_SAMPLES + JS_SAMPLES ALL_SAMPLES = CPP_SAMPLES + LUA_SAMPLES + JS_SAMPLES
def caculate_built_samples(args):
''' Compute the sampels to be built def calculate_build_targets(args):
'cpp' for short of all cpp tests ''' Calculate build targets from list of targets passed on command line.
'lua' for short of all lua tests 'all' for all tests
'cpp' for all c++ tests
'lua' for all lua tests
'js' for all javascript tests
''' '''
if 'all' in args:
return ALL_SAMPLES
targets = [] targets = []
if 'cpp' in args: for arg in args:
targets += CPP_SAMPLES if arg == 'all':
args.remove('cpp') targets += ALL_SAMPLES
if 'lua' in args: elif arg == 'cpp':
targets += LUA_SAMPLES targets += CPP_SAMPLES
args.remove('lua') elif arg == 'lua':
if 'js' in args: targets += LUA_SAMPLES
targets += JS_SAMPLES elif arg == 'js':
args.remove('js') targets += JS_SAMPLES
else:
targets.append(arg)
targets += args # remove duplicates
# remove duplicate elements, for example
# python android-build.py cpp hellocpp
targets = set(targets) targets = set(targets)
return list(targets) return targets
def do_build(app_android_root, build_mode, app_abi):
def do_build(app_android_root, build_mode, app_abi, platform):
command = 'cocos compile -p android -s %s --ndk-mode %s --app-abi %s' % (app_android_root, build_mode, app_abi) command = 'cocos compile -p android -s %s --ndk-mode %s --app-abi %s' % (app_android_root, build_mode, app_abi)
if platform:
command += ' --ap %s' % platform
print command print command
if os.system(command) != 0: if os.system(command) != 0:
raise Exception("Build dynamic library for project [ " + app_android_root + " ] fails!") raise Exception('Build dynamic library for project [ %s ] failed!' % app_android_root)
def build_samples(target, build_mode, app_abi):
if build_mode is None: def build_targets(targets, build_mode, app_abi, api_level):
build_mode = 'debug'
elif build_mode != 'release':
build_mode = 'debug'
if app_abi is None: if api_level:
app_abi = 'armeabi-v7a' platform = 'android-%s' % api_level
else:
platform = None
build_targets = caculate_built_samples(target) build_targets = calculate_build_targets(targets)
app_android_root = '' app_android_root = ''
target_proj_path_map = { cocos_root = os.path.join(os.path.dirname(os.path.realpath(__file__)), '..')
"cpp-empty-test": "tests/cpp-empty-test",
"game-controller-test": "tests/game-controller-test",
"cpp-tests": "tests/cpp-tests",
"lua-empty-test": "tests/lua-empty-test",
"lua-tests": "tests/lua-tests",
"lua-game-controller-test": "tests/lua-game-controller-test",
"js-tests": "tests/js-tests"
}
cocos_root = os.path.join(os.path.dirname(os.path.realpath(__file__)), "..")
for target in build_targets: for target in build_targets:
if target in target_proj_path_map: app_android_root = os.path.join(cocos_root, 'tests', target)
app_android_root = os.path.join(cocos_root, target_proj_path_map[target]) do_build(app_android_root, build_mode, app_abi, platform)
else:
print 'unknown target: %s' % target
continue
do_build(app_android_root, build_mode, app_abi)
# -------------- main -------------- def main():
if __name__ == '__main__': from argparse import ArgumentParser, RawTextHelpFormatter
#parse the params description = '''
usage = """ This script is mainly used for building tests built-in with cocos2d-x.
This script is mainy used for building tests built-in with cocos2d-x.
Usage: %prog [options] [cpp-empty-test|cpp-tests|lua-empty-test|lua-tests|js-tests|cpp|lua|all] If you are new to cocos2d-x, we recommend you to start with cpp-empty-test or lua-empty-test.'''
If you are new to cocos2d-x, I recommend you start with cpp-empty-test, lua-empty-test. parser = ArgumentParser(description=description, formatter_class=RawTextHelpFormatter)
parser.add_argument('-n', '--ndk', dest='app_abi', default='armeabi-v7a',
help='specifies Android ABI')
parser.add_argument('-p', '--platform', dest='api_level',
help='specifies Android API Level')
parser.add_argument('-b', '--build', dest='build_mode', metavar='BUILD_MODE', default='debug', choices=['debug', 'release'],
help='the build mode for java project, debug (default) or release. ' +
'To get more information, please refer to ' +
'http://developer.android.com/tools/building/building-cmdline.html')
parser.add_argument('targets', nargs='+', metavar='targets',
help='targets to build. A target is one of [cpp-empty-test|cpp-tests|lua-empty-test|lua-tests|js-tests|cpp|lua|js|all]',
choices=['cpp-empty-test', 'cpp-tests', 'lua-empty-test', 'lua-tests', 'js-tests', 'cpp', 'lua', 'js', 'all'])
args = parser.parse_args()
You can combine these targets like this: try:
build_targets(args.targets, args.build_mode, args.app_abi, args.api_level)
python android-build.py cpp-empty-test lua-empty-test except Exception as e:
print e
"""
parser = OptionParser(usage=usage)
parser.add_option("-n", "--ndk", dest="app_abi",
help='It is not used anymore, because cocos console does not support it.')
parser.add_option("-p", "--platform", dest="android_platform",
help='This parameter is not used any more, just keep compatible.')
parser.add_option("-b", "--build", dest="build_mode",
help='The build mode for java project,debug[default] or release. \
Get more information, \
please refer to http://developer.android.com/tools/building/building-cmdline.html')
(opts, args) = parser.parse_args()
if len(args) == 0:
parser.print_help()
sys.exit(1) sys.exit(1)
else:
try:
build_samples(args, opts.build_mode, opts.app_abi) if __name__ == '__main__':
except Exception as e: main()
print e
sys.exit(1)