retlat's blog

cmake-js を CLion で快適に扱う

Node Native Addon を作ってみようと思ったので、まずは node-addon-examples の first project を node-gyp から cmake-js に置き換えてやってみました
普段から使い慣れているので IDE は CLion を使って
ただ NODE_API_MODULE()C++ requires a type specifier for all declarations だったり、いくつかエラーが出てしまってどうにもおかしい

C++ をろくに書いたことがなくてどこが悪いのかさっぱり思いつかないので、まずは以下のコマンドを実行してコンパイルしてみた

$ ./node_modules/.bin/cmake-js compile

これは成功してちゃんと動くものができてたので実装は問題ないはず
じゃあ何が違うんだろうということで、綺麗なところからビルドしてディレクトリを比較してみる

まずは元の綺麗な状態がこう

.
|- lib
|  `- binding.js
|- node_modules
|- package.json
|- src
|  `- hello_world.cc
|- test
|  `- test_binding.js
|- CMakeLists.txt
`- yarn.lock

cmake-js compile 実行後にはこんな感じ

.
|- build
|  |- Release
|  |  `- out.node
|  `- (省略)
|- lib
|  `- binding.js
|- node_modules
|- package.json
|- src
|  `- hello_world.cc
|- test
|  `- test_binding.js
|- CMakeLists.txt
`- yarn.lock

次に CLion で開いた時にはこうなる

.
|- cmake-build-debug
|  |- Release
|  |  `- out.node
|  `- (省略)
|- lib
|  `- binding.js
|- node_modules
|- package.json
|- src
|  `- hello_world.cc
|- test
|  `- test_binding.js
|- CMakeLists.txt
`- yarn.lock

ということで生成されるフォルダが build か cmake-build-debug かというところが違った

これを調整したらエラーが消えるのかもということで、 CLion の設定で
Build, Execution, Deployment > CMake
の順にたどって ProfileBuild directory を build に設定してみる

設定を適用してロードを待ったらエラーが消えたのでめでたしめでたし
CLion は build directory に生成されたデータを元に解析対象を探してるっぽい