Compile Unity jslib in command

文章讲述了在Unity项目中遇到jslib文件存在语法错误的问题,导致WebGL构建失败。错误日志显示因import令牌引起的SyntaxError。作者建议使用Node.js运行命令来快速定位和检查jslib文件中的错误。经过修复所有错误后,发现另一个引用错误,即mergeInto未定义。
摘要由CSDN通过智能技术生成

Compile Unity jslib in command

(Jin Qing’s Column, May., 2023)

My Unity project has a jslib file, which has many syntax errors.
But Unity does not report these jslib errors like cs files, until the WebGL build fails with a error like this:

Failed process stderr log:
error: failure to execute js library "E:\Test\Unity\My project (1)\Assets\Plugins\without_a_bundler.jslib": SyntaxError: Unexpected token import,,SyntaxError: Unexpected token import
    ...
    at run (bootstrap_node.js:394:7)
preprocessed source (you can run a js engine on this to get a clearer error message sometimes):
=============
let wasm;

...

lib = {
    ...
};

mergeInto(LibraryManager.library, lib);


=============

Internal compiler error in src/compiler.js! Please raise a bug report at https://github.com/kripken/emscripten/issues/ with a log of the build and the input files used to run. Exception message: "SyntaxError: Unexpected token import" | SyntaxError: Unexpected token import
...
Traceback (most recent call last):
  ...
  File "C:\Program Files\Unity\Hub\Editor\2020.3.26f1\Editor\Data\PlaybackEngines\WebGLSupport\BuildTools\Emscripten\tools\jsrun.py", line 132, in run_js
    raise Exception('Expected the command ' + str(command) + ' to finish with return code ' + str(assert_returncode) + ', but it returned with code ' + str(proc.returncode) + ' instead! Output: ' + str(ret)[:error_limit])
Exception: Expected the command ['C:/Program Files/Unity/Hub/.../Editor/Data\\Tools\\nodejs\\node.exe', '--stack_size=8192', '--max-old-space-size=4096', ..., 'E:\\Test\\Unity\\My project (1)\\Assets\\Plugins\\without_a_bundler.jslib', ...] to finish with return code 0, but it returned with code 1 instead! Output: // The Module object: Our interface to the outside world. We import
...

It is hard to locate the error from the message, and the WebGL building takes a long time as well.

Try to run the command, which is hinted in the message:

"C:/Program Files/Unity/Hub/.../Editor/Data\\Tools\\nodejs\\node.exe" ^
  --stack_size=8192 --max-old-space-size=4096 ^
  "E:\\Test\\Unity\\My project (1)\\Assets\\Plugins\\without_a_bundler.jslib"

Ouput:

E:\Test\Unity\My project (1)\Assets\Plugins\without_a_bundler.jslib:205
        input = new URL('without_a_bundler_bg.wasm', import.meta.url);
                                                     ^^^^^^
SyntaxError: Unexpected token import
...

It is a quick way to using nodejs to check the jslib file.
When all errors are fixed, the result output should be like:

E:\Test\Unity\My project (1)\Assets\Plugins\without_a_bundler.jslib:226
mergeInto(LibraryManager.library, lib);
^

ReferenceError: mergeInto is not defined
...
SCons是一个强大的构建工具,它支持自定义构建过程。如果你想通过SCons生成`compile_command.json`文件,这通常用于静态分析工具,如Clang-Tidy或Pylint等,它们需要命令行构建信息以便分析源代码,这个过程涉及到配置SCons来生成合适的命令。 首先,你需要确保你的SConstruct文件(SCons的基本配置文件)中包含了对构建目标的规则,比如编译、链接步骤。当运行`scons`命令时,SCons会生成一系列实际执行的构建指令。例如: ```python env = Environment(CCFLAGS='-std=c++11', LDFLAGS='-lmylib') cpp_sources = Glob('src/*.cpp') program = env.Program('myapp', cpp_sources) ``` 然后,你可以选择创建一个自定义的动作(Action)或回调函数,在该函数中手动收集并保存构建命令到`compile_command.json`。这里有一个简单的例子: ```python def save_commands(target, source, env): compile_commands = [] for src in source: compile_commands.append(env.Command( 'json/%s' % os.path.basename(src), # 输出文件路径 src, str(env.Command(target=target[0], source=src, action='')) # 这里的action可能是空字符串 )) with open('compile_command.json', 'w') as f: json.dump([c.abspath for c in compile_commands], f) program.add_post_action(save_commands) ``` 当你运行`scons`时,SCons会先执行常规的构建,然后触发`save_commands`动作来创建`compile_command.json`。 注意:这并不是SCons的标准功能,而是根据特定需求定制的。如果你正在寻找更直接的方式,可能需要查阅具体的SCons插件或第三方工具,如scons-buildinfo这类库。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值