Android-build.py changes for Window and cygwin

Version 3 seems to give a final blow to Eclipse.
So I must use android-build

Some changes are in order.
Hint: Do not install python in cygwin it does not work.
Hint: Make sure that you have version 2.7 of Python (Version 3 does not work)

Now my changes

1: Change the first line so you do not have to explicitly invoke python.
because I am lazy

you can now use
./android-build.py Boxit -p 10
instead of
python ./android-build.py Boxit -p 10
I am saving you 7 key stroke… :blush:

2: Adding my own projects

myapps

3: Add an option to specify the number of cpu used.

Why: if I compile with multiple cpu’s the batch keep stopping waiting for jobs to terminate
You have to manually restart the batch. Not bad if you compile one project but pretty bad for multiple projects

This used to be available in an earlier version.
To do this I have added the num_of_cpu variable. All very strait forward just look at the code.

4: I also print of all the project names. If you are told that a name is not found you can check easily.


#!python 
# android-build.py
# Build android

import sys
import os, os.path
from optparse import OptionParser

CPP_SAMPLES = ['cpp-empty-test', 'cpp-tests', 'game-controller-test']
LUA_SAMPLES = ['lua-empty-test', 'lua-tests', 'lua-game-controller-test']
JS_SAMPLES = ['js-tests']
LUA_MYAPPS = ['hellolua', 'dark', 'dark', 'edit', 'testlua', 'Boxit', 'Premix2', 'Cristals', 'Krazy', 'Catch', 'whichword']**

ALL_SAMPLES = CPP_SAMPLES + LUA_SAMPLES + JS_SAMPLES + LUA_MYAPPS

def caculate_built_samples(args):
    ''' Compute the sampels to be built
    'cpp' for short of all cpp tests
    'lua' for short of all lua tests
    '''

    if 'all' in args:
        return ALL_SAMPLES

    targets = []
    if 'cpp' in args:
        targets += CPP_SAMPLES
        args.remove('cpp')
    if 'lua' in args:
        targets += LUA_SAMPLES
        args.remove('lua')
    if 'js' in args:
        targets += JS_SAMPLES
        args.remove('js')
    if 'myapps' in args:
        targets += LUA_MYAPPS
        args.remove('myapps')

    targets += args

    # remove duplicate elements, for example
    # python android-build.py cpp hellocpp
    targets = set(targets)
    return list(targets)

def do_build(app_android_root, build_mode, num_of_cpu):
    command = 'cocos compile -p android -s %s --ndk-mode %s -j %s' % (app_android_root, build_mode, num_of_cpu)
    print command
    
    if os.system(command) != 0:
        raise Exception("Build dynamic library for project [ " + app_android_root + " ] fails!")

def build_samples(target, build_mode, num_of_cpu):

    if build_mode is None:
        build_mode = 'debug'
    elif build_mode != 'release':
        build_mode = 'debug'

    build_targets = caculate_built_samples(target)

    app_android_root = ''

    target_proj_path_map = {
        "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",
        "hellolua":"samples/Lua/HelloLua/project/proj.android",
        "testlua":"samples/Lua/TestLua/project/proj.android",
        "Premix2":"samples/Lua/Premix2/project/proj.android",
        "Cristals":"samples/Lua/Cristals/project/proj.android",
        "Boxit":"samples/Lua/Boxit/project/proj.android",
        "edit":"samples/Lua/edit/project/proj.android",
        "dark":"samples/Lua/dark/project/proj.android",
        "Krazy":"samples/Lua/Krazy/project/proj.android",
        "Catch":"samples/Lua/Catch/project/proj.android",
        "whichword":"samples/Lua/whichword/project/proj.android",
    }

    cocos_root = os.path.join(os.path.dirname(os.path.realpath(__file__)), "..")

    for target in build_targets:
        if target in target_proj_path_map:
            app_android_root = os.path.join(cocos_root, target_proj_path_map[target])
        else:
            print 'unknown target: %s' % target
            continue

        do_build(app_android_root, build_mode, num_of_cpu)

# -------------- main --------------
if __name__ == '__main__':

    #parse the params
    usage = """
    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, I recommend you start with cpp-empty-test, lua-empty-test.

    You can combine these targets like this:

    python android-build.py cpp-empty-test lua-empty-test

    """
    print('cpp', CPP_SAMPLES)
    print('lua', LUA_SAMPLES)
    print('myapps', LUA_MYAPPS)

    parser = OptionParser(usage=usage)
    parser.add_option("-n", "--ndk", dest="ndk_build_param",
                      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')
    parser.add_option("-j", "--joption", dest="num_of_cpu", 
                        help='Parameter for num_of_cpu. Without the parameter,the script just build dynamic library for the projects. Valid android-cpus are:[1|2|3|4]')                      
    (opts, args) = parser.parse_args()

    if opts.num_of_cpu == None:
        try:
            import multiprocessing
            num_of_cpu = multiprocessing.cpu_count()
        except Exception:
            print ("Can't know cpuinfo, use default 1 cpu")
            num_of_cpu = 1
    else:
        num_of_cpu = int(opts.num_of_cpu)
    
    if len(args) == 0:
        parser.print_help()
        sys.exit(1)
    else:
        try:
            build_samples(args, opts.build_mode, num_of_cpu)
        except Exception as e:
            print e
            sys.exit(1)

I hope this help.
Yes it is a bit long.
Andre