自动绑定jsb mac版本
Python 版本 是2.7
运行 》
curl https://bootstrap.pypa.io/pip/2.7/get-pip.py > get-pip.py
sudo python get-pip.py
sudo easy_install pip
pip --version
sudo pip install PyYAML
sudo pip install Cheetah
报错: ImportError: No module named clang
解决: pip install clang
报错: sudo pip install Cheetah
解决:使用 pip install Cheetah
命令打开 open ~/.bash_profile
在该文件添加
export NDK_ROOT="/Users/cf/Library/Android/sdk/ndk/19.2.5345600"
export PYTHON_BIN="/usr/bin/python"
添加完成后 可能需要重启
在该目录下新建文件夹test/
ABCJSBBridge.h
ABCJSBBridge.cpp
/*
* @Author: cc
* @Date: 2023-12-19 17:03:58
* @LastEditTime: 2023-12-20 11:53:57
* @Description: file content
*/
#include "ABCJSBBridge.h"
// include platform specific implement class
// #if (CC_TARGET_PLATFORM == CC_PLATFORM_MAC || CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
// #include "ABCJSBBridge-apple.h"
// #define JSBBridgeImpl JSBBridgeApple
// #elif (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
// // #include "ABCJSBBridge-android.h"
// #define JSBBridgeImpl JSBBridgeAndroid
// #endif
namespace abc
{
// singleton stuff
static JSBBridge *s_SharedJSBBridge = nullptr;
int JSBBridge::test_add_num(int a){
return a + 1;
}
JSBBridge::JSBBridge()
{
// DLLOG("Construct JSBBridge %p", this);
init();
}
JSBBridge::~JSBBridge()
{
// DLLOG("Destruct JSBBridge %p", this);
s_SharedJSBBridge = nullptr;
}
JSBBridge* JSBBridge::getInstance()
{
if (!s_SharedJSBBridge)
{
// printf("getInstance JSBBridge ");
s_SharedJSBBridge = new JSBBridge();
// CCASSERT(s_SharedJSBBridge, "FATAL: Not enough memory for create JSBBridge");
}
return s_SharedJSBBridge;
}
bool JSBBridge::init(void)
{
// printf("init JSBBridge ");
//_impl.reset(new JSBBridgeImpl());
return true;
}
int JSBBridge::get_total_num(int a,int b){
return a + b;
}
}
#pragma once
#include "base/ccConfig.h"
#ifndef PROJ_ANDROID_STUDIO_ABCJSBBRIDGE_H
#define PROJ_ANDROID_STUDIO_ABCJSBBRIDGE_H
// #define DLLOG(format) printf(format, ##__VA_ARGS__)
#endif //PROJ_ANDROID_STUDIO_ABCJSBBRIDGE_H
namespace abc
{
class JSBBridge
{
public:
/**
* Returns a shared instance of the director.
* @js _getInstance
*/
static JSBBridge* getInstance();
/** @private */
JSBBridge();
/** @private */
~JSBBridge();
bool init();
int get_total_num(int a,int b);
static int test_add_num(int a);
};
}
新建文件 genbindings_test.py 仿照 genbindings.py 写
执行命令 python genbindings_test.py
即可在该目录下看到
frameworks/cocos2d-x/cocos/scripting/js-bindings/auto/api/jsb_cocos2dx_test_auto_api.js
我们需要的文件
#!/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)
x86_llvm_path = ""
x64_llvm_path = os.path.abspath(os.path.join(ndk_root, 'toolchains/llvm/prebuilt', '%s-%s' % (cur_platform, 'x86_64')))
if not os.path.exists(x64_llvm_path):
x86_llvm_path = os.path.abspath(os.path.join(ndk_root, 'toolchains/llvm/prebuilt', '%s' % (cur_platform)))
if not os.path.exists(x86_llvm_path):
x86_llvm_path = os.path.abspath(os.path.join(ndk_root, 'toolchains/llvm/prebuilt', '%s-%s' % (cur_platform, 'x86')))
if os.path.isdir(x64_llvm_path):
llvm_path = x64_llvm_path
elif os.path.isdir(x86_llvm_path):
llvm_path = x86_llvm_path
else:
print 'llvm toolchain not found!'
print 'path: %s or path: %s are not valid! ' % (x86_llvm_path, x64_llvm_path)
sys.exit(1)
x86_gcc_toolchain_path = ""
x64_gcc_toolchain_path = os.path.abspath(os.path.join(ndk_root, 'toolchains/arm-linux-androideabi-4.9/prebuilt', '%s-%s' % (cur_platform, 'x86_64')))
if not os.path.exists(x64_gcc_toolchain_path):
x86_gcc_toolchain_path = os.path.abspath(os.path.join(ndk_root, 'toolchains/arm-linux-androideabi-4.9/prebuilt', '%s' % (cur_platform)))
if not os.path.exists(x86_gcc_toolchain_path):
x86_gcc_toolchain_path = os.path.abspath(os.path.join(ndk_root, 'toolchains/arm-linux-androideabi-4.9/prebuilt', '%s-%s' % (cur_platform, 'x86')))
if os.path.isdir(x64_gcc_toolchain_path):
gcc_toolchain_path = x64_gcc_toolchain_path
elif os.path.isdir(x86_gcc_toolchain_path):
gcc_toolchain_path = x86_gcc_toolchain_path
else:
print 'gcc toolchain not found!'
print 'path: %s or path: %s are not valid! ' % (x64_gcc_toolchain_path, x86_gcc_toolchain_path)
sys.exit(1)
project_root = os.path.abspath(os.path.join(os.path.dirname(__file__), '..', '..'))
cocos_root = os.path.abspath(project_root)
jsb_root = os.path.abspath(os.path.join(project_root, 'js-bindings'))
cxx_generator_root = os.path.abspath(os.path.join(project_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', 'gcc_toolchain_dir', gcc_toolchain_path)
config.set('DEFAULT', 'cocosdir', cocos_root)
config.set('DEFAULT', 'cxxgeneratordir', cxx_generator_root)
config.set('DEFAULT', 'extra_flags', '')
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)
print '%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' % project_root
output_dir = '%s/cocos/scripting/js-bindings/auto' % project_root
custom_cmd_args = {
'cocos2dx_test.ini': ('cocos2dx_test', 'jsb_cocos2dx_test_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])
# print(command)
# _run_cmd(command)
if len(custom_cmd_args) > 0:
for key in custom_cmd_args.keys():
args = custom_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)
print '----------------------------------------'
print 'Generating javascript bindings succeeds.'
print '----------------------------------------'
except Exception as e:
if e.__class__.__name__ == 'CmdError':
print '-------------------------------------'
print 'Generating javascript bindings fails.'
print '-------------------------------------'
sys.exit(1)
else:
raise
# -------------- main --------------
if __name__ == '__main__':
main()
frameworks/cocos2d-x/cocos/Android.mk
在 ts文件 直接调用 即可 (需要生成android.apk)
let a = abc.JSBBridge.test_add_num(1);
let s = abc.JSBBridge.getInstance().get_total_num(1,2);
注意:官方文档内 jsb_boot.js 文件可以忽略,不用操作