Merge pull request #5322 from dumganhar/merge5309

merge PR #5309: use python script instead for auto generating binding codes.
This commit is contained in:
James Chen 2014-02-11 14:29:49 +08:00
commit 1bf7b9a5bf
14 changed files with 426 additions and 632 deletions

View File

@ -0,0 +1,9 @@
#!/usr/bin/python
import os
genbindings_dirs = ['tojs', 'tolua']
for item in genbindings_dirs:
os.chdir("tools/" + item)
os.system('python genbindings.py')
os.chdir("../..")

View File

@ -1,58 +0,0 @@
#!/bin/bash
# Generate JS and Lua bindings for Cocos2D-X
# ... using Android NDK system headers
# ... and automatically update submodule references
# ... and push these changes to remote repos
# Dependencies
#
# For bindings generator:
# (see ../../../tojs/genbindings.sh and ../../../tolua/genbindings.sh
# ... for the defaults used if the environment is not customized)
#
# * $PYTHON_BIN
# * $CLANG_ROOT
# * $NDK_ROOT
#
echo "[test]start generate js binding..."
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
COCOS2DX_ROOT="$DIR"/../..
TOJS_ROOT=$COCOS2DX_ROOT/tools/tojs
TOLUA_ROOT=$COCOS2DX_ROOT/tools/tolua
GENERATED_WORKTREE="$COCOS2DX_ROOT"/cocos/scripting/auto-generated
# Exit on error
set -e
generate_bindings_glue_codes()
{
echo "Create auto-generated jsbinding glue codes."
pushd "$TOJS_ROOT"
./genbindings.sh
popd
echo "Create auto-generated luabinding glue codes."
pushd "$TOLUA_ROOT"
./genbindings.sh
popd
}
# Update submodule of auto-gen Binding repo.
pushd "$GENERATED_WORKTREE"
echo "Delete all directories and files except '.git' and 'README.md'."
ls -a | grep -E -v ^\[.\]\{1,2\}$ | grep -E -v ^\.git$ | grep -E -v ^README\.md$ | xargs -I{} rm -rf {}
popd
# 1. Generate JS bindings
generate_bindings_glue_codes
echo
echo Bindings generated successfully
echo

View File

@ -1,36 +0,0 @@
@echo off
::!/bin/bash
:: Generate JS and Lua bindings for Cocos2D-X
:: ... using Android NDK system headers
:: ... and automatically update submodule references
:: ... and push these changes to remote repos
:: Dependencies
::
:: For bindings generator:
:: (see ../../../tojs/genbindings.sh and ../../../tolua/genbindings-win32.bat
:: ... for the defaults used if the environment is not customized)
::
:: * $PYTHON_BIN
:: * $CLANG_ROOT
:: * $NDK_ROOT
::
echo "[test]start generate js binding..."
set COCOS2DX_ROOT=%cd%/../..
set "COCOS2DX_ROOT=%COCOS2DX_ROOT:\=/%"
set TO_JS_ROOT=%COCOS2DX_ROOT%/tools/tojs
set TO_LUA_ROOT=%COCOS2DX_ROOT%/tools/tolua
set GENERATED_WORKTREE=%COCOS2DX_ROOT%/cocos/scripting/auto-generated
set "GENERATED_WORKTREE=%GENERATED_WORKTREE:/=\%"
:: Run to_js genbindings
call %TO_JS_ROOT%/genbindings-win32.bat
:: Run to_lua genbindings
call %TO_LUA_ROOT%/genbindings-win32.bat
echo
echo Bindings generated successfully
echo

View File

@ -93,13 +93,7 @@ def main():
os.system(git_update_submodule)
# Generate binding glue codes
if(branch == 'develop'):
if(platform.system() == 'Darwin'):
os.system("tools/jenkins-scripts/gen_jsb.sh")
elif(platform.system() == 'Windows'):
os.chdir("tools/jenkins-scripts")
os.system("gen_jsb_win32.bat")
os.chdir("../..")
os.system("python tools/jenkins-scripts/gen_jsb.py")
#make temp dir
print "current dir is" + os.environ['WORKSPACE']

View File

@ -9,11 +9,49 @@ On Windows:
* Add the installed path of python (e.g. C:\Python27) to windows environment variable named 'PATH'.
* Download pyyaml from http://pyyaml.org/download/pyyaml/PyYAML-3.10.win32-py2.7.exe and install it.
* Download pyCheetah from https://raw.github.com/dumganhar/cocos2d-x/download/downloads/Cheetah.zip, unzip it to "C:\Python27\Lib\site-packages"
* Set environment variables (`PYTHON_ROOT` and `NDK_ROOT`) or just modify variables (`PYTHON_ROOT` and `NDK_ROOT`) in `genbindings-win32.bat`.
* Go to "cocos2d-x/tools/tojs" folder, and run "genbindings-win32.bat". The generated codes will be under "cocos\scripting\auto-generated\js-bindings".
* Set environment variables (`NDK_ROOT`)
* Go to "cocos2d-x/tools/tojs" folder, and run "genbindings.py". The generated codes will be under "cocos\scripting\auto-generated\js-bindings".
On MAC:
----------
* Please refer to https://github.com/cocos2d/bindings-generator/blob/develop/README.md
* The OSX 10.9 has a built-in python2.7 and if your os don't have python2.7 then use [Homebrew](http://brew.sh/) to install the python and use pip install the python dependencies.
<pre>
brew install python
</pre>
* Install python dependices by pip.
<pre>
sudo easy_install pip
sudo pip install PyYAML
sudo pip install Cheetah
</pre>
* Download [64bit ndk-r9b-x86_64](http://dl.google.com/android/ndk/android-ndk-r9b-darwin-x86_64.tar.bz2) from [google](http://developer.android.com/tools/sdk/ndk/index.html)
* Run
<pre>
export NDK_ROOT=/path/to/android-ndk-r9b
./genbindings.py
</pre>
On Ubuntu Linux 12.04 64bit
------------
* Install python
<pre>
sudo apt-get install python2.7
</pre>
* Install python dependices by pip.
<pre>
sudo apt-get install python-pip
sudo pip install PyYAML
sudo pip install Cheetah
</pre>
* Download [64bit ndk-r9b-x86_64]( https://dl.google.com/android/ndk/android-ndk-r9b-linux-x86_64.tar.bz2) from [google](http://developer.android.com/tools/sdk/ndk/index.html)
* Go to "cocos2d-x/tools/tojs", Run
<pre>
export NDK_ROOT=/path/to/android-ndk-r9b
./genbindings.py
</pre>

View File

@ -1,116 +0,0 @@
@echo off
:: This script is used to generate jsbinding glue codes.
:: Android ndk version must be ndk-r9b.
setlocal
:: You should modify PYTHON_ROOT and NDK_ROOT to work under your environment.
:: or just uncomment the follow 2 lines and set them.
:: set PYTHON_ROOT=C:/Python27
:: set NDK_ROOT=G:/android/android-ndk-r9b
setlocal ENABLEEXTENSIONS
if %errorlevel% neq 0 (
echo Unable to enable extensions
exit /b 1
)
if defined PYTHON_ROOT (echo PYTHON_ROOT is defined.) else (
echo PYTHON_ROOT is NOT defined!
exit /b 1
)
if defined NDK_ROOT (echo NDK_ROOT is defined.) else (
echo NDK_ROOT is NOT defined!
exit /b 1
)
:: Check use 32-bit or 64-bit
:: Assume 64-bit
set TEMP=windows-x86_64
if not exist "%NDK_ROOT%/toolchains/llvm-3.3/prebuilt/%TEMP%" set TEMP=
:: Otherwise fall back to 32-bit make
if "%TEMP%"=="" set TEMP=windows
set NDK_LLVM_ROOT=%NDK_ROOT%/toolchains/llvm-3.3/prebuilt/%TEMP%
set COCOS2DX_ROOT=%cd%/../..
set "COCOS2DX_ROOT=%COCOS2DX_ROOT:\=/%"
set CXX_GENERATOR_ROOT=%COCOS2DX_ROOT%/tools/bindings-generator
set TO_JS_ROOT=%COCOS2DX_ROOT%/tools/tojs
set "CXX_GENERATOR_ROOT=%CXX_GENERATOR_ROOT:\=/%"
set OUTPUT_DIR=%COCOS2DX_ROOT%/cocos/scripting/auto-generated/js-bindings
set "OUTPUT_DIR=%OUTPUT_DIR:/=\%"
set PATH=%PATH%;%CXX_GENERATOR_ROOT%/libclang;%CXX_GENERATOR_ROOT%/tools/win32;%PYTHON_ROOT%
:: write userconf.ini
set _CONF_INI_FILE=%cd%\userconf.ini
if exist %_CONF_INI_FILE% del /Q %_CONF_INI_FILE%
echo
echo generating userconf.ini...
echo ---
echo [DEFAULT] > %_CONF_INI_FILE%
echo androidndkdir=%NDK_ROOT% >> %_CONF_INI_FILE%
echo clangllvmdir=%NDK_LLVM_ROOT% >> %_CONF_INI_FILE%
echo cocosdir=%COCOS2DX_ROOT% >> %_CONF_INI_FILE%
echo cxxgeneratordir=%CXX_GENERATOR_ROOT% >> %_CONF_INI_FILE%
:: fixme: to fix parse error, we must difine __WCHAR_MAX__ and undefine __MINGW32__ .
echo extra_flags=-D__WCHAR_MAX__=0x7fffffff -U__MINGW32__ >> %_CONF_INI_FILE%
echo ---
:: Generate bindings for cocos2dx
echo Generating bindings for cocos2dx...
python %CXX_GENERATOR_ROOT%/generator.py %TO_JS_ROOT%/cocos2dx.ini -s cocos2d-x -t spidermonkey -o %OUTPUT_DIR% -n js_cocos2dx_auto
if %errorlevel% neq 0 goto ERROR
echo "Generating bindings for cocos2dx_extension..."
python %CXX_GENERATOR_ROOT%/generator.py %TO_JS_ROOT%/cocos2dx_extension.ini -s cocos2dx_extension -t spidermonkey -o %OUTPUT_DIR% -n js_cocos2dx_extension_auto
if %errorlevel% neq 0 goto ERROR
echo "Generating bindings for cocos2dx_builder..."
python %CXX_GENERATOR_ROOT%/generator.py %TO_JS_ROOT%/cocos2dx_builder.ini -s cocos2dx_builder -t spidermonkey -o %OUTPUT_DIR% -n jsb_cocos2dx_builder_auto
if %errorlevel% neq 0 goto ERROR
echo "Generating bindings for cocos2dx_gui..."
python %CXX_GENERATOR_ROOT%/generator.py %TO_JS_ROOT%/cocos2dx_gui.ini -s cocos2dx_gui -t spidermonkey -o %OUTPUT_DIR% -n js_cocos2dx_gui_auto
if %errorlevel% neq 0 goto ERROR
echo "Generating bindings for cocos2dx_studio..."
python %CXX_GENERATOR_ROOT%/generator.py %TO_JS_ROOT%/cocos2dx_studio.ini -s cocos2dx_studio -t spidermonkey -o %OUTPUT_DIR% -n js_cocos2dx_studio_auto
if %errorlevel% neq 0 goto ERROR
echo "Generating bindings for cocos2dx_spine..."
python %CXX_GENERATOR_ROOT%/generator.py %TO_JS_ROOT%/cocos2dx_spine.ini -s cocos2dx_spine -t spidermonkey -o %OUTPUT_DIR% -n js_cocos2dx_spine_auto
if %errorlevel% neq 0 goto ERROR
:: Change the generated file format from DOS to UNIX.
pushd "%OUTPUT_DIR%"
dos2unix *
popd
goto PASS
:PASS
echo ---------------------------------
echo Generating bindings succeeds.
echo ---------------------------------
endlocal
exit /b 0
:ERROR
echo ---------------------------------
echo Generating bindings fails.
echo ---------------------------------
endlocal
exit /b 1

164
tools/tojs/genbindings.py Executable file
View File

@ -0,0 +1,164 @@
#!/usr/bin/python
# This script is used to generate luabinding glue codes.
# Android ndk version must be ndk-r9b.
import sys
import os, os.path
import shutil
import ConfigParser
import subprocess
import re
from contextlib import contextmanager
def _check_ndk_root_env():
''' Checking the environment NDK_ROOT, which will be used for building
'''
try:
NDK_ROOT = os.environ['NDK_ROOT']
except Exception:
print "NDK_ROOT not defined. Please define NDK_ROOT in your environment."
sys.exit(1)
return NDK_ROOT
def _check_python_bin_env():
''' Checking the environment PYTHON_BIN, which will be used for building
'''
try:
PYTHON_BIN = os.environ['PYTHON_BIN']
except Exception:
print "PYTHON_BIN not defined, use current python."
PYTHON_BIN = sys.executable
return PYTHON_BIN
class CmdError(Exception):
pass
@contextmanager
def _pushd(newDir):
previousDir = os.getcwd()
os.chdir(newDir)
yield
os.chdir(previousDir)
def _run_cmd(command):
ret = subprocess.call(command, shell=True)
if ret != 0:
message = "Error running command"
raise CmdError(message)
def main():
cur_platform= '??'
llvm_path = '??'
ndk_root = _check_ndk_root_env()
# del the " in the path
ndk_root = re.sub(r"\"", "", ndk_root)
python_bin = _check_python_bin_env()
platform = sys.platform
if platform == 'win32':
cur_platform = 'windows'
elif platform == 'darwin':
cur_platform = platform
elif 'linux' in platform:
cur_platform = 'linux'
else:
print 'Your platform is not supported!'
sys.exit(1)
if platform == 'win32':
x86_llvm_path = os.path.abspath(os.path.join(ndk_root, 'toolchains/llvm-3.3/prebuilt', '%s' % cur_platform))
else:
x86_llvm_path = os.path.abspath(os.path.join(ndk_root, 'toolchains/llvm-3.3/prebuilt', '%s-%s' % (cur_platform, 'x86')))
x64_llvm_path = os.path.abspath(os.path.join(ndk_root, 'toolchains/llvm-3.3/prebuilt', '%s-%s' % (cur_platform, 'x86_64')))
if os.path.isdir(x86_llvm_path):
llvm_path = x86_llvm_path
elif os.path.isdir(x64_llvm_path):
llvm_path = x64_llvm_path
else:
print 'llvm toochain not found!'
print 'path: %s or path: %s are not valid! ' % (x86_llvm_path, x64_llvm_path)
sys.exit(1)
cocos_root = os.path.abspath(os.path.join(os.path.dirname(__file__), '..', '..'))
cxx_generator_root = os.path.abspath(os.path.join(cocos_root, 'tools/bindings-generator'))
# save config to file
config = ConfigParser.ConfigParser()
config.set('DEFAULT', 'androidndkdir', ndk_root)
config.set('DEFAULT', 'clangllvmdir', llvm_path)
config.set('DEFAULT', 'cocosdir', cocos_root)
config.set('DEFAULT', 'cxxgeneratordir', cxx_generator_root)
config.set('DEFAULT', 'extra_flags', '')
# To fix parse error on windows, we must difine __WCHAR_MAX__ and undefine __MINGW32__ .
if platform == 'win32':
config.set('DEFAULT', 'extra_flags', '-D__WCHAR_MAX__=0x7fffffff -U__MINGW32__')
conf_ini_file = os.path.abspath(os.path.join(os.path.dirname(__file__), 'userconf.ini'))
print 'generating userconf.ini...'
with open(conf_ini_file, 'w') as configfile:
config.write(configfile)
# set proper environment variables
if 'linux' in platform or platform == 'darwin':
os.putenv('LD_LIBRARY_PATH', '%s/libclang' % cxx_generator_root)
if platform == 'win32':
path_env = os.environ['PATH']
os.putenv('PATH', r'%s;%s\libclang;%s\tools\win32;' % (path_env, cxx_generator_root, cxx_generator_root))
try:
tojs_root = '%s/tools/tojs' % cocos_root
output_dir = '%s/cocos/scripting/auto-generated/js-bindings' % cocos_root
cmd_args = {'cocos2dx.ini' : ('cocos2d-x', 'jsb_cocos2dx_auto'), \
'cocos2dx_extension.ini' : ('cocos2dx_extension', 'jsb_cocos2dx_extension_auto'), \
'cocos2dx_builder.ini' : ('cocos2dx_builder', 'jsb_cocos2dx_builder_auto'), \
'cocos2dx_gui.ini' : ('cocos2dx_gui', 'jsb_cocos2dx_gui_auto'), \
'cocos2dx_studio.ini' : ('cocos2dx_studio', 'jsb_cocos2dx_studio_auto'), \
'cocos2dx_spine.ini' : ('cocos2dx_spine', 'jsb_cocos2dx_spine_auto'), \
}
target = 'spidermonkey'
generator_py = '%s/generator.py' % cxx_generator_root
for key in cmd_args.keys():
args = cmd_args[key]
cfg = '%s/%s' % (tojs_root, key)
print 'Generating bindings for %s...' % (key[:-4])
command = '%s %s %s -s %s -t %s -o %s -n %s' % (python_bin, generator_py, cfg, args[0], target, output_dir, args[1])
_run_cmd(command)
if platform == 'win32':
with _pushd(output_dir):
_run_cmd('dos2unix *')
print '---------------------------------'
print 'Generating bindings succeeds.'
print '---------------------------------'
except Exception as e:
if e.__class__.__name__ == 'CmdError':
print '---------------------------------'
print 'Generating bindings fails.'
print '---------------------------------'
sys.exit(1)
else:
raise
# -------------- main --------------
if __name__ == '__main__':
main()

View File

@ -1,144 +0,0 @@
#!/bin/bash
#
# Usage:
# export NDK_ROOT=/path/to/ndk-r9b
# ./genbindings.sh
#
# exit this script if any commmand fails
set -e
# read user.cfg if it exists and is readable
_CFG_FILE=$(dirname "$0")"/user.cfg"
if [ -f "$_CFG_FILE" ];
then
if [ ! -r "$_CFG_FILE" ]; then
echo "Fatal Error: $_CFG_FILE exists but is unreadable"
exit 1
fi
fi
# paths
if [ -z "${NDK_ROOT+aaa}" ]; then
# ... if NDK_ROOT is not set, use "$HOME/bin/android-ndk"
NDK_ROOT="$HOME/bin/android-ndk"
fi
if [ -z "${PYTHON_BIN+aaa}" ]; then
# ... if PYTHON_BIN is not set, use "/usr/bin/python2.7"
PYTHON_BIN="/usr/bin/python2.7"
fi
# find current dir
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
# paths with defaults hardcoded to relative paths
if [ -z "${COCOS2DX_ROOT+aaa}" ]; then
COCOS2DX_ROOT="$DIR/../.."
fi
if [ -z "${CXX_GENERATOR_ROOT+aaa}" ]; then
CXX_GENERATOR_ROOT="$COCOS2DX_ROOT/tools/bindings-generator"
fi
if [ -z "${TOJS_ROOT+aaa}" ]; then
TOJS_ROOT="$COCOS2DX_ROOT/tools/tojs"
fi
# set output dir
OUTPUT_DIR=${COCOS2DX_ROOT}/cocos/scripting/auto-generated/js-bindings
echo "Paths"
echo " NDK_ROOT: $NDK_ROOT"
echo " PYTHON_BIN: $PYTHON_BIN"
echo " COCOS2DX_ROOT: $COCOS2DX_ROOT"
echo " CXX_GENERATOR_ROOT: $CXX_GENERATOR_ROOT"
echo " TOJS_ROOT: $TOJS_ROOT"
# check NDK version, must be r9b
if ! grep -q r9b $NDK_ROOT/RELEASE.TXT
then
echo " Fatal Error: NDK r9b must be required!"
exit 1
fi
# check clang include path
OS_NAME=$('uname')
NDK_LLVM_ROOT=$NDK_ROOT/toolchains/llvm-3.3/prebuilt
case "$OS_NAME" in
Darwin | darwin)
echo "in darwin"
if [ -d "$NDK_LLVM_ROOT/darwin-x86_64" ]; then
NDK_LLVM_ROOT=$NDK_LLVM_ROOT/darwin-x86_64
elif [ -d "$NDK_LLVM_ROOT/darwin-x86" ]; then
NDK_LLVM_ROOT=$NDK_LLVM_ROOT/darwin-x86
else
echo $NDK_LLVM_ROOT
echo " Fatal Error: $NDK_LLVM_ROOT doesn't contains prebuilt llvm 3.3"
exit 1
fi
;;
Linux | linux)
echo "in linux"
if [ -d "$NDK_LLVM_ROOT/linux-x86_64" ]; then
NDK_LLVM_ROOT=$NDK_LLVM_ROOT/linux-x86_64
elif [ -d "$NDK_LLVM_ROOT/linux-x86" ]; then
NDK_LLVM_ROOT=$NDK_LLVM_ROOT/linux-x86
else
echo " Fatal Error: $NDK_LLVM_ROOT doesn't contains prebuilt llvm 3.3"
exit 1
fi
;;
*)
echo " Fatal Error: Please run this script in linux or mac osx."
exit 1
;;
esac
# write userconf.ini
_CONF_INI_FILE="$PWD/userconf.ini"
if [ -f "$_CONF_INI_FILE" ]
then
rm "$_CONF_INI_FILE"
fi
_CONTENTS=""
_CONTENTS+="[DEFAULT]"'\n'
_CONTENTS+="androidndkdir=$NDK_ROOT"'\n'
_CONTENTS+="clangllvmdir=$NDK_LLVM_ROOT"'\n'
_CONTENTS+="cocosdir=$COCOS2DX_ROOT"'\n'
_CONTENTS+="cxxgeneratordir=$CXX_GENERATOR_ROOT"'\n'
_CONTENTS+="extra_flags="'\n'
echo
echo "generating userconf.ini..."
echo ---
echo -e "$_CONTENTS"
echo -e "$_CONTENTS" > "$_CONF_INI_FILE"
echo ---
# Generate bindings for cocos2dx
echo "Generating bindings for cocos2dx..."
set -x
LD_LIBRARY_PATH=${CXX_GENERATOR_ROOT}/libclang $PYTHON_BIN ${CXX_GENERATOR_ROOT}/generator.py ${TOJS_ROOT}/cocos2dx.ini -s cocos2d-x -t spidermonkey -o ${OUTPUT_DIR} -n jsb_cocos2dx_auto
echo "Generating bindings for cocos2dx_extension..."
LD_LIBRARY_PATH=${CXX_GENERATOR_ROOT}/libclang $PYTHON_BIN ${CXX_GENERATOR_ROOT}/generator.py ${TOJS_ROOT}/cocos2dx_extension.ini -s cocos2dx_extension -t spidermonkey -o ${OUTPUT_DIR} -n jsb_cocos2dx_extension_auto
echo "Generating bindings for cocos2dx_builder..."
LD_LIBRARY_PATH=${CXX_GENERATOR_ROOT}/libclang $PYTHON_BIN ${CXX_GENERATOR_ROOT}/generator.py ${TOJS_ROOT}/cocos2dx_builder.ini -s cocos2dx_builder -t spidermonkey -o ${OUTPUT_DIR} -n jsb_cocos2dx_builder_auto
echo "Generating bindings for cocos2dx_gui..."
LD_LIBRARY_PATH=${CXX_GENERATOR_ROOT}/libclang $PYTHON_BIN ${CXX_GENERATOR_ROOT}/generator.py ${TOJS_ROOT}/cocos2dx_gui.ini -s cocos2dx_gui -t spidermonkey -o ${OUTPUT_DIR} -n jsb_cocos2dx_gui_auto
echo "Generating bindings for cocos2dx_studio..."
LD_LIBRARY_PATH=${CXX_GENERATOR_ROOT}/libclang $PYTHON_BIN ${CXX_GENERATOR_ROOT}/generator.py ${TOJS_ROOT}/cocos2dx_studio.ini -s cocos2dx_studio -t spidermonkey -o ${OUTPUT_DIR} -n jsb_cocos2dx_studio_auto
echo "Generating bindings for cocos2dx_spine..."
LD_LIBRARY_PATH=${CXX_GENERATOR_ROOT}/libclang $PYTHON_BIN ${CXX_GENERATOR_ROOT}/generator.py ${TOJS_ROOT}/cocos2dx_spine.ini -s cocos2dx_spine -t spidermonkey -o ${OUTPUT_DIR} -n jsb_cocos2dx_spine_auto

View File

@ -9,12 +9,49 @@ On Windows:
* Add the installed path of python (e.g. C:\Python27) to windows environment variable named 'PATH'.
* Download pyyaml from http://pyyaml.org/download/pyyaml/PyYAML-3.10.win32-py2.7.exe and install it.
* Download pyCheetah from https://raw.github.com/dumganhar/cocos2d-x/download/downloads/Cheetah.zip, unzip it to "C:\Python27\Lib\site-packages"
* Set environment variables (`PYTHON_ROOT` and `NDK_ROOT`) or just modify variables (`PYTHON_ROOT` and `NDK_ROOT`) in `genbindings-win32.bat`.
* Go to "cocos2d-x/tools/tolua" folder, and run "genbindings-win32.bat". The generated codes will be under "cocos\scripting\auto-generated\lua-bindings".
* Set environment variables (`NDK_ROOT`)
* Go to "cocos2d-x/tools/tolua" folder, and run "genbindings.py". The generated codes will be under "cocos\scripting\auto-generated\js-bindings".
On MAC:
----------
* Please refer to https://github.com/cocos2d/bindings-generator/blob/develop/README.md
* The OSX 10.9 has a built-in python2.7 and if your os don't have python2.7 then use [Homebrew](http://brew.sh/) to install the python and use pip install the python dependencies.
<pre>
brew install python
</pre>
* Install python dependices by pip.
<pre>
sudo easy_install pip
sudo pip install PyYAML
sudo pip install Cheetah
</pre>
* Download [64bit ndk-r9b-x86_64](http://dl.google.com/android/ndk/android-ndk-r9b-darwin-x86_64.tar.bz2) from [google](http://developer.android.com/tools/sdk/ndk/index.html)
* Run
<pre>
export NDK_ROOT=/path/to/android-ndk-r9b
./genbindings.py
</pre>
On Ubuntu Linux 12.04 64bit
------------
* Install python
<pre>
sudo apt-get install python2.7
</pre>
* Install python dependices by pip.
<pre>
sudo apt-get install python-pip
sudo pip install PyYAML
sudo pip install Cheetah
</pre>
* Download [64bit ndk-r9b-x86_64]( https://dl.google.com/android/ndk/android-ndk-r9b-linux-x86_64.tar.bz2) from [google](http://developer.android.com/tools/sdk/ndk/index.html)
* Go to "cocos2d-x/tools/tolua", Run
<pre>
export NDK_ROOT=/path/to/android-ndk-r9b
./genbindings.py
</pre>

View File

@ -1,117 +0,0 @@
@echo off
:: This script is used to generate jsbinding glue codes.
:: Android ndk version must be ndk-r9b.
setlocal
:: You should modify PYTHON_ROOT and NDK_ROOT to work under your environment.
:: or just uncomment the follow 2 lines and set them.
:: set PYTHON_ROOT=C:/Python27
:: set NDK_ROOT=G:/android/android-ndk-r9b
setlocal ENABLEEXTENSIONS
if %errorlevel% neq 0 (
echo Unable to enable extensions
exit /b 1
)
if defined PYTHON_ROOT (echo PYTHON_ROOT is defined.) else (
echo PYTHON_ROOT is NOT defined!
exit /b 1
)
if defined NDK_ROOT (echo NDK_ROOT is defined.) else (
echo NDK_ROOT is NOT defined!
exit /b 1
)
:: Check use 32-bit or 64-bit
:: Assume 64-bit
set TEMP=windows-x86_64
if not exist "%NDK_ROOT%/toolchains/llvm-3.3/prebuilt/%TEMP%" set TEMP=
:: Otherwise fall back to 32-bit make
if "%TEMP%"=="" set TEMP=windows
set NDK_LLVM_ROOT=%NDK_ROOT%/toolchains/llvm-3.3/prebuilt/%TEMP%
set COCOS2DX_ROOT=%cd%/../..
set "COCOS2DX_ROOT=%COCOS2DX_ROOT:\=/%"
set CXX_GENERATOR_ROOT=%COCOS2DX_ROOT%/tools/bindings-generator
set TO_LUA_ROOT=%COCOS2DX_ROOT%/tools/tolua
set "CXX_GENERATOR_ROOT=%CXX_GENERATOR_ROOT:\=/%"
set OUTPUT_DIR=%COCOS2DX_ROOT%/cocos/scripting/auto-generated/lua-bindings
set "OUTPUT_DIR=%OUTPUT_DIR:/=\%"
set PATH=%PATH%;%CXX_GENERATOR_ROOT%/libclang;%CXX_GENERATOR_ROOT%/tools/win32;%PYTHON_ROOT%
:: write userconf.ini
set _CONF_INI_FILE=%cd%\userconf.ini
if exist %_CONF_INI_FILE% del /Q %_CONF_INI_FILE%
echo
echo generating userconf.ini...
echo ---
echo [DEFAULT] > %_CONF_INI_FILE%
echo androidndkdir=%NDK_ROOT% >> %_CONF_INI_FILE%
echo clangllvmdir=%NDK_LLVM_ROOT% >> %_CONF_INI_FILE%
echo cocosdir=%COCOS2DX_ROOT% >> %_CONF_INI_FILE%
echo cxxgeneratordir=%CXX_GENERATOR_ROOT% >> %_CONF_INI_FILE%
:: fixme: to fix parse error, we must difine __WCHAR_MAX__ and undefine __MINGW32__ .
echo extra_flags=-D__WCHAR_MAX__=0x7fffffff -U__MINGW32__ >> %_CONF_INI_FILE%
echo ---
:: Generate bindings for cocos2dx
echo Generating bindings for cocos2dx...
python %CXX_GENERATOR_ROOT%/generator.py %TO_LUA_ROOT%/cocos2dx.ini -s cocos2d-x -t lua -o %OUTPUT_DIR% -n lua_cocos2dx_auto
if %errorlevel% neq 0 goto ERROR
echo "Generating bindings for cocos2dx_extension..."
python %CXX_GENERATOR_ROOT%/generator.py %TO_LUA_ROOT%/cocos2dx_extension.ini -s cocos2dx_extension -t lua -o %OUTPUT_DIR% -n lua_cocos2dx_extension_auto
if %errorlevel% neq 0 goto ERROR
echo "Generating bindings for cocos2dx_gui..."
python %CXX_GENERATOR_ROOT%/generator.py %TO_LUA_ROOT%/cocos2dx_gui.ini -s cocos2dx_gui -t lua -o %OUTPUT_DIR% -n lua_cocos2dx_gui_auto
if %errorlevel% neq 0 goto ERROR
echo "Generating bindings for cocos2dx_studio..."
python %CXX_GENERATOR_ROOT%/generator.py %TO_LUA_ROOT%/cocos2dx_studio.ini -s cocos2dx_studio -t lua -o %OUTPUT_DIR% -n lua_cocos2dx_studio_auto
if %errorlevel% neq 0 goto ERROR
echo "Generating bindings for cocos2dx_spine..."
python %CXX_GENERATOR_ROOT%/generator.py %TO_LUA_ROOT%/cocos2dx_spine.ini -s cocos2dx_spine -t lua -o %OUTPUT_DIR% -n lua_cocos2dx_spine_auto
if %errorlevel% neq 0 goto ERROR
echo "Generating bindings for cocos2dx_physics..."
python %CXX_GENERATOR_ROOT%/generator.py %TO_LUA_ROOT%/cocos2dx_physics.ini -s cocos2dx_physics -t lua -o %OUTPUT_DIR% -n lua_cocos2dx_physics_auto
if %errorlevel% neq 0 goto ERROR
:: Change the generated file format from DOS to UNIX.
pushd "%OUTPUT_DIR%"
dos2unix *
popd
goto PASS
:PASS
echo ---------------------------------
echo Generating bindings succeeds.
echo ---------------------------------
endlocal
exit /b 0
:ERROR
echo ---------------------------------
echo Generating bindings fails.
echo ---------------------------------
endlocal
exit /b 1

164
tools/tolua/genbindings.py Executable file
View File

@ -0,0 +1,164 @@
#!/usr/bin/python
# This script is used to generate luabinding glue codes.
# Android ndk version must be ndk-r9b.
import sys
import os, os.path
import shutil
import ConfigParser
import subprocess
import re
from contextlib import contextmanager
def _check_ndk_root_env():
''' Checking the environment NDK_ROOT, which will be used for building
'''
try:
NDK_ROOT = os.environ['NDK_ROOT']
except Exception:
print "NDK_ROOT not defined. Please define NDK_ROOT in your environment."
sys.exit(1)
return NDK_ROOT
def _check_python_bin_env():
''' Checking the environment PYTHON_BIN, which will be used for building
'''
try:
PYTHON_BIN = os.environ['PYTHON_BIN']
except Exception:
print "PYTHON_BIN not defined, use current python."
PYTHON_BIN = sys.executable
return PYTHON_BIN
class CmdError(Exception):
pass
@contextmanager
def _pushd(newDir):
previousDir = os.getcwd()
os.chdir(newDir)
yield
os.chdir(previousDir)
def _run_cmd(command):
ret = subprocess.call(command, shell=True)
if ret != 0:
message = "Error running command"
raise CmdError(message)
def main():
cur_platform= '??'
llvm_path = '??'
ndk_root = _check_ndk_root_env()
# del the " in the path
ndk_root = re.sub(r"\"", "", ndk_root)
python_bin = _check_python_bin_env()
platform = sys.platform
if platform == 'win32':
cur_platform = 'windows'
elif platform == 'darwin':
cur_platform = platform
elif 'linux' in platform:
cur_platform = 'linux'
else:
print 'Your platform is not supported!'
sys.exit(1)
if platform == 'win32':
x86_llvm_path = os.path.abspath(os.path.join(ndk_root, 'toolchains/llvm-3.3/prebuilt', '%s' % cur_platform))
else:
x86_llvm_path = os.path.abspath(os.path.join(ndk_root, 'toolchains/llvm-3.3/prebuilt', '%s-%s' % (cur_platform, 'x86')))
x64_llvm_path = os.path.abspath(os.path.join(ndk_root, 'toolchains/llvm-3.3/prebuilt', '%s-%s' % (cur_platform, 'x86_64')))
if os.path.isdir(x86_llvm_path):
llvm_path = x86_llvm_path
elif os.path.isdir(x64_llvm_path):
llvm_path = x64_llvm_path
else:
print 'llvm toochain not found!'
print 'path: %s or path: %s are not valid! ' % (x86_llvm_path, x64_llvm_path)
sys.exit(1)
cocos_root = os.path.abspath(os.path.join(os.path.dirname(__file__), '..', '..'))
cxx_generator_root = os.path.abspath(os.path.join(cocos_root, 'tools/bindings-generator'))
# save config to file
config = ConfigParser.ConfigParser()
config.set('DEFAULT', 'androidndkdir', ndk_root)
config.set('DEFAULT', 'clangllvmdir', llvm_path)
config.set('DEFAULT', 'cocosdir', cocos_root)
config.set('DEFAULT', 'cxxgeneratordir', cxx_generator_root)
config.set('DEFAULT', 'extra_flags', '')
# To fix parse error on windows, we must difine __WCHAR_MAX__ and undefine __MINGW32__ .
if platform == 'win32':
config.set('DEFAULT', 'extra_flags', '-D__WCHAR_MAX__=0x7fffffff -U__MINGW32__')
conf_ini_file = os.path.abspath(os.path.join(os.path.dirname(__file__), 'userconf.ini'))
print 'generating userconf.ini...'
with open(conf_ini_file, 'w') as configfile:
config.write(configfile)
# set proper environment variables
if 'linux' in platform or platform == 'darwin':
os.putenv('LD_LIBRARY_PATH', '%s/libclang' % cxx_generator_root)
if platform == 'win32':
path_env = os.environ['PATH']
os.putenv('PATH', r'%s;%s\libclang;%s\tools\win32;' % (path_env, cxx_generator_root, cxx_generator_root))
try:
tolua_root = '%s/tools/tolua' % cocos_root
output_dir = '%s/cocos/scripting/auto-generated/lua-bindings' % cocos_root
cmd_args = {'cocos2dx.ini' : ('cocos2d-x', 'lua_cocos2dx_auto'), \
'cocos2dx_extension.ini' : ('cocos2dx_extension', 'lua_cocos2dx_extension_auto'), \
'cocos2dx_gui.ini' : ('cocos2dx_gui', 'lua_cocos2dx_gui_auto'), \
'cocos2dx_studio.ini' : ('cocos2dx_studio', 'lua_cocos2dx_studio_auto'), \
'cocos2dx_spine.ini' : ('cocos2dx_spine', 'lua_cocos2dx_spine_auto'), \
'cocos2dx_physics.ini' : ('cocos2dx_physics', 'lua_cocos2dx_physics_auto'), \
}
target = 'lua'
generator_py = '%s/generator.py' % cxx_generator_root
for key in cmd_args.keys():
args = cmd_args[key]
cfg = '%s/%s' % (tolua_root, key)
print 'Generating bindings for %s...' % (key[:-4])
command = '%s %s %s -s %s -t %s -o %s -n %s' % (python_bin, generator_py, cfg, args[0], target, output_dir, args[1])
_run_cmd(command)
if platform == 'win32':
with _pushd(output_dir):
_run_cmd('dos2unix *')
print '---------------------------------'
print 'Generating bindings succeeds.'
print '---------------------------------'
except Exception as e:
if e.__class__.__name__ == 'CmdError':
print '---------------------------------'
print 'Generating bindings fails.'
print '---------------------------------'
sys.exit(1)
else:
raise
# -------------- main --------------
if __name__ == '__main__':
main()

View File

@ -1,144 +0,0 @@
#!/bin/bash
#
# Usage:
# export NDK_ROOT=/path/to/ndk-r9b
# ./genbindings.sh
#
# exit this script if any commmand fails
set -e
# read user.cfg if it exists and is readable
_CFG_FILE=$(dirname "$0")"/user.cfg"
if [ -f "$_CFG_FILE" ];
then
if [ ! -r "$_CFG_FILE" ]; then
echo "Fatal Error: $_CFG_FILE exists but is unreadable"
exit 1
fi
fi
# paths
if [ -z "${NDK_ROOT+aaa}" ]; then
# ... if NDK_ROOT is not set, use "$HOME/bin/android-ndk"
NDK_ROOT="$HOME/bin/android-ndk"
fi
if [ -z "${PYTHON_BIN+aaa}" ]; then
# ... if PYTHON_BIN is not set, use "/usr/bin/python2.7"
PYTHON_BIN="/usr/bin/python2.7"
fi
# find current dir
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
# paths with defaults hardcoded to relative paths
if [ -z "${COCOS2DX_ROOT+aaa}" ]; then
COCOS2DX_ROOT="$DIR/../.."
fi
if [ -z "${CXX_GENERATOR_ROOT+aaa}" ]; then
CXX_GENERATOR_ROOT="$COCOS2DX_ROOT/tools/bindings-generator"
fi
if [ -z "${TOLUA_ROOT+aaa}" ]; then
TOLUA_ROOT="$COCOS2DX_ROOT/tools/tolua"
fi
# set output dir
OUTPUT_DIR=${COCOS2DX_ROOT}/cocos/scripting/auto-generated/lua-bindings
echo "Paths"
echo " NDK_ROOT: $NDK_ROOT"
echo " PYTHON_BIN: $PYTHON_BIN"
echo " COCOS2DX_ROOT: $COCOS2DX_ROOT"
echo " CXX_GENERATOR_ROOT: $CXX_GENERATOR_ROOT"
echo " TOLUA_ROOT: $TOLUA_ROOT"
# check NDK version, must be r9b
if ! grep -q r9b $NDK_ROOT/RELEASE.TXT
then
echo " Fatal Error: NDK r9b must be required!"
exit 1
fi
# check clang include path
OS_NAME=$('uname')
NDK_LLVM_ROOT=$NDK_ROOT/toolchains/llvm-3.3/prebuilt
case "$OS_NAME" in
Darwin | darwin)
echo "in darwin"
if [ -d "$NDK_LLVM_ROOT/darwin-x86_64" ]; then
NDK_LLVM_ROOT=$NDK_LLVM_ROOT/darwin-x86_64
elif [ -d "$NDK_LLVM_ROOT/darwin-x86" ]; then
NDK_LLVM_ROOT=$NDK_LLVM_ROOT/darwin-x86
else
echo $NDK_LLVM_ROOT
echo " Fatal Error: $NDK_LLVM_ROOT doesn't contains prebuilt llvm 3.3"
exit 1
fi
;;
Linux | linux)
echo "in linux"
if [ -d "$NDK_LLVM_ROOT/linux-x86_64" ]; then
NDK_LLVM_ROOT=$NDK_LLVM_ROOT/linux-x86_64
elif [ -d "$NDK_LLVM_ROOT/linux-x86" ]; then
NDK_LLVM_ROOT=$NDK_LLVM_ROOT/linux-x86
else
echo " Fatal Error: $NDK_LLVM_ROOT doesn't contains prebuilt llvm 3.3"
exit 1
fi
;;
*)
echo " Fatal Error: Please run this script in linux or mac osx."
exit 1
;;
esac
# write userconf.ini
_CONF_INI_FILE="$PWD/userconf.ini"
if [ -f "$_CONF_INI_FILE" ]
then
rm "$_CONF_INI_FILE"
fi
_CONTENTS=""
_CONTENTS+="[DEFAULT]"'\n'
_CONTENTS+="androidndkdir=$NDK_ROOT"'\n'
_CONTENTS+="clangllvmdir=$NDK_LLVM_ROOT"'\n'
_CONTENTS+="cocosdir=$COCOS2DX_ROOT"'\n'
_CONTENTS+="cxxgeneratordir=$CXX_GENERATOR_ROOT"'\n'
_CONTENTS+="extra_flags="'\n'
echo
echo "generating userconf.ini..."
echo ---
echo -e "$_CONTENTS"
echo -e "$_CONTENTS" > "$_CONF_INI_FILE"
echo ---
# Generate bindings for cocos2dx
echo "Generating bindings for cocos2dx..."
set -x
LD_LIBRARY_PATH=${CXX_GENERATOR_ROOT}/libclang $PYTHON_BIN ${CXX_GENERATOR_ROOT}/generator.py ${TOLUA_ROOT}/cocos2dx.ini -s cocos2d-x -t lua -o ${OUTPUT_DIR} -n lua_cocos2dx_auto
echo "Generating bindings for cocos2dx_extension..."
LD_LIBRARY_PATH=${CXX_GENERATOR_ROOT}/libclang $PYTHON_BIN ${CXX_GENERATOR_ROOT}/generator.py ${TOLUA_ROOT}/cocos2dx_extension.ini -s cocos2dx_extension -t lua -o ${OUTPUT_DIR} -n lua_cocos2dx_extension_auto
echo "Generating bindings for cocos2dx_gui..."
LD_LIBRARY_PATH=${CXX_GENERATOR_ROOT}/libclang $PYTHON_BIN ${CXX_GENERATOR_ROOT}/generator.py ${TOLUA_ROOT}/cocos2dx_gui.ini -s cocos2dx_gui -t lua -o ${OUTPUT_DIR} -n lua_cocos2dx_gui_auto
echo "Generating bindings for cocos2dx_studio..."
LD_LIBRARY_PATH=${CXX_GENERATOR_ROOT}/libclang $PYTHON_BIN ${CXX_GENERATOR_ROOT}/generator.py ${TOLUA_ROOT}/cocos2dx_studio.ini -s cocos2dx_studio -t lua -o ${OUTPUT_DIR} -n lua_cocos2dx_studio_auto
echo "Generating bindings for cocos2dx_spine..."
LD_LIBRARY_PATH=${CXX_GENERATOR_ROOT}/libclang $PYTHON_BIN ${CXX_GENERATOR_ROOT}/generator.py ${TOLUA_ROOT}/cocos2dx_spine.ini -s cocos2dx_spine -t lua -o ${OUTPUT_DIR} -n lua_cocos2dx_spine_auto
echo "Generating bindings for cocos2dx_physics..."
LD_LIBRARY_PATH=${CXX_GENERATOR_ROOT}/libclang $PYTHON_BIN ${CXX_GENERATOR_ROOT}/generator.py ${TOLUA_ROOT}/cocos2dx_physics.ini -s cocos2dx_physics -t lua -o ${OUTPUT_DIR} -n lua_cocos2dx_physics_auto

View File

@ -8,7 +8,7 @@
# Dependencies
#
# For bindings generator:
# (see ../../../tojs/genbindings.sh and ../../../tolua/genbindings.sh
# (see ../../../tojs/genbindings.py and ../../../tolua/genbindings.py
# ... for the defaults used if the environment is not customized)
#
# * $PYTHON_BIN
@ -47,12 +47,12 @@ generate_bindings_glue_codes()
{
echo "Create auto-generated jsbinding glue codes."
pushd "$TOJS_ROOT"
./genbindings.sh
./genbindings.py
popd
echo "Create auto-generated luabinding glue codes."
pushd "$TOLUA_ROOT"
./genbindings.sh
./genbindings.py
popd
}

View File

@ -5,6 +5,9 @@ set -e
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
COCOS2DX_ROOT="$DIR"/../..
export NDK_ROOT=$HOME/bin/android-ndk
export PYTHON_BIN=/usr/bin/python
if [ "$GEN_JSB"x = "YES"x ]; then
# Re-generation of the javascript bindings can perform push of the new
# version back to github. We don't do this for pull requests, or if
@ -26,7 +29,7 @@ if [ "$GEN_JSB"x = "YES"x ]; then
echo "GH_USER not set"
exit 0
fi
export NDK_ROOT=$HOME/bin/android-ndk
cd $COCOS2DX_ROOT/tools/travis-scripts
./generate-jsbindings.sh
elif [ "$PLATFORM"x = "android"x ]; then