It is a continuation of part 1 [Cocos2d-x] How to make Script Binding (Part 1)
Now let's see how to implement bindings-generator.
First, create a directory for the Lang language under the tools directory of Cococ2d-x. It's hard to create from scratch, so I'll copy the Lua directory.
cp -r tools/tolua tools/lang
cp -r tools/bindings-generator/targets/lua tools/bindings-generator/targets/lang
First, let's take a look at the files in the * tools / lang * directory. I think there are some Python scripts called * genbindings.py * and some ini files that start with "cocos2dx".
Open * genbindings.py * and rewrite the following part of the try ~ except clause.
try:
lang_root = '%s/tools/lang' % project_root
output_dir = '%s/cocos/scripting/lang-bindings/auto' % project_root
cmd_args = {'cocos2dx.ini' : ('cocos2d-x', 'lang_cocos2dx_auto'), \
}
target = 'lang'
generator_py = '%s/generator.py' % cxx_generator_root
for key in cmd_args.keys():
args = cmd_args[key]
cfg = '%s/%s' % (lang_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 lang bindings succeeds.'
print '---------------------------------'
except Exception as e:
if e.__class__.__name__ == 'CmdError':
print '---------------------------------'
print 'Generating lang bindings fails.'
print '---------------------------------'
sys.exit(1)
else:
raise
I feel like this.
In cmd_args, describe the source to be generated and its configuration file. Since it is difficult to handle everything from the beginning, we will limit it to the most basic "cocos2d-x.ini". When "cocos2d-x.ini" is perfect, I think it's a good idea to increase the supported files one by one.
Next, let's take a look at the contents of "cocos2d-x.ini". There are various settings, but I think the items that are likely to be rewritten are as follows.
The C ++ header to parse. Analyze by following include starting from the specified header file.
The class to be generated.
Describe the method to be excluded in the class specified by classes. For example, * Sprite :: [getQuad] * excludes the getQuad method of the Sprite class. [*] Means all methods.
Specify when the method name created on the Lang language is different from the C ++ method name. Shorten long method names.
Specify when the class name created on the Lang language is different from the C ++ method name.
Specify an abstract class. (Do not make a constructor)
After checking the contents of the ini file, close it without changing anything.
Now let's run * bindings-generator * in this state to generate the glue code. Execute * genbindings.py * that you rewrote earlier. After some logs, if "Generating lang bindings succeeds." Is displayed, it is successful. If successful, the following files should have been generated:
cocos2d-x/cocos/scripting/lang-bindings/auto/lang_cocos2dx_auto.hpp
cocos2d-x/cocos/scripting/lang-bindings/auto/lang_cocos2dx_auto.cpp
I haven't changed anything in the generated code yet, so the contents are still written for Lua.
To change the generated code, change the template file under the following directory. Note that the bindings-generator uses a Python template engine called Cheetah. You will write according to Cheetah's grammar.
cocos2d-x/tools/bindings-generator/targets/lang/templates/
Below the templates directory are the following template files:
Output at the beginning of the generated .hpp file. Write #include etc. The prototype of the register_all_cocos2dx function is written here.
Output at the beginning of the generated .cpp file. Write #include etc.
Output at the end of the generated .hpp file.
Output at the end of the generated .cpp file. Implement the register_all_cocos2dx function and Write the process to call the register method defined for each class.
Output at the beginning of a group for each class.
Output at the beginning of a group for each class. Write any code you want to generate for each class.
Implement the lang_register_cocos2dx_ * function. A collection of binding registration functions for each class.
Output for each member function.
Output for each member function. If it is overloaded, this will be output.
Output for each static member function.
Output for each static member function. If it is overloaded, this will be output.
Output when binding std :: function.
Output as a .hpp file for each member function. (* function * .c common file)
If the function is overloaded, it will be in a separate file, but if it is overloaded, this will be a separate file. You need to determine which overloaded function to execute based on the number and type of arguments passed from the Lang language. This is because the process becomes complicated. Also, I think there is a template called apidoc_ *, but this is for apidoc It is for outputting the signature of the function generated on the Lang language as a comment.
You need to edit the following Yaml file related to the template.
cocos2d-x/tools/bindings-generator/targets/lang/conversions.yaml
As you can see from the contents, it is a file that defines the type conversion method. Use the type conversion function between C ++ and Lang languages to define the conversion function to be called for each type. "to_native" is Lang language-> C ++, from_native is the opposite. In addition, I think that there are some key strings that start with "@", but the strings that start with "@" are treated as regular expressions.
The following script is used to analyze the C ++ code.
cocos2d-x/tools/bindings-generator/clang/cindex.py
This is Clang's Python binding, which comes with the Clang source code.
I think that there are many places where the explanation is insufficient, but for the time being, this is the end. As for the correspondence of the cocos command, I am currently implementing it, so It may be written as (Part 3) when it is organized to some extent. I'm currently making a little bit of bindings for the language Squirrel.
Tomorrow's Advent Calendar is giginet. giginet will publish [Cocos2d-x book] on December 24th (http://giginet.hateblo.jp/entry/2014/11/26/185855). Congratulations! I have already booked on Amazon.
that's all. (December 4th was over while I was writing. I'm sorry ...)
Recommended Posts