Cocos2d-x JSB auto binding tool "binding-generator"(B-G) tips

Cocos2d-x JSB auto binding tool “binding-generator”(B-G) tips

What’s B-G?

When dealing with JSB, if the classes or functions in C*+ that you plan to use is not yet bound to the existing JSB, you’d probably need B-G. It can generate the bound files of C*+ and JS API correspondingly, and all you have to do is to call the generated JS API before it automatically change back to the original C*+ class, just like if you’d called the original C*+ class or function.

B-G Generating Process

1.Get necessary tools ready
2.Prepare the C++ files that you want to bind
3.Write .ini configuration files and .sh script program for generated files
4.Run the .sh script file to generate bound files
5.Test and regenerate them until they are ready to use

Necessary tools

Cocos2d-x engine: http://cocos2d-x.org/projects/cocos2d-x/wiki/Download
Python 2.7: http://www.python.org/download/ (only v2.7 is supported by the bindings-generator)
py-ymal:  http://pyyaml.org/wiki/PyYAML (a third-party package of python that would be called by generator.py;there are installation tutorials at the link for different systems)
cheetah: http://www.cheetahtemplate.org/ (another third-party package of python; a template that generator.py would be calling; the installing process is pretty much like that of py-ymal)
libclang: http://llvm.org/releases/3.1/clang+llvm-3.1-x86_64-apple-darwin11.tar.gz  (a lib that would be needed during the generating process; it needs to be v3.1 for Cocos2d-x v2.2.2 and v3.3for Cocos2d-x v3.0 beta)
Android NDK: http://dl.google.com/android/ndk/android-ndk-r8c-darwin-x86.tar.bz2

.ini files examples:

the root directory of Cocos2d-x/tools/bindings-generator/test/test.ini 
the root directory of Cocos2d-x /tools/tojs/cocos2dx_extension.ini
the root directory of Cocos2d-x /tools/tojs/cocos2dx.ini

How to use some common parameters of .ini

name: just the name
prefix: names of all generated files with be prefixed as “prefix”,like prefix.cpp, prefix.hpp, prefix_api.js

classes: the names of the classes needed to be bounded, which have to include all classes of imported head files; you can add multiple classes with regular expressions; you can refer to cocos2dx.ini
extra_arguments: systematic parameters needed by some APIS.

headers: route of the head file that you want to bind.
target_namespace: name space. The final classes generated will start with the name space. For example if your class was sqlite, and the name space was cocos2dx, then the final file would be cocos2dx.sqlite.
rename_functions: this can rename the function that you are binding; if you want to rename multiple, separate them with a comma; refer to  SqliteCpp::[sqlite3_execCpp=sqlite3_exec], that is the function to rename sqlite3_execCpp of SqliteCpp as sqlite3_exec.
rename_classes: ditto except that it renames classes.
skip: skip the classes and functions that need not to be bounded.

.sh files:

the main calling program used to read .ini configuration files and to write some parameters.
Eg. cocos2dx root directory/tools/tojs/genbindings.sh 
cocos2dx root directory/tools/ bindings-generator/test/test.sh

Below would decide that you call the configuration files via calling generator.py and the bound files would be generated in the set location.

*set x LD_LIBRARY_PATH=${CLANG_ROOT}/lib $PYTHON_BIN ${CXX_GENERATOR_ROOT}/generator.py ${TO_JS_ROOT}/sqlite.inis sqlite -o ${TO_JS_ROOT}/sqliteBinding
*

When finished with .ini and .sh, run sudo your .sh route in cmd and you’ll find the generated files in where you set before.

:stuck_out_tongue: :stuck_out_tongue: :stuck_out_tongue: :stuck_out_tongue:

useful. thx!