node-pre-gyp を使用したクロスプラットフォーム対応

Node.jsでプラットフォーム依存の開発を行った時の対応メモ

binding.gypに以下のように分岐を作成すれば、OSごとにビルドの分岐をつくることができる。

{
  "targets": [
    {
      "target_name": "native_rt",
      "sources": [ "native-rt.cc" ],
      "conditions":[
        ["OS=='linux'", {
          "sources": [ "native-rt_linux.cc" ]
          }],
        ["OS=='mac'", {
          "sources": [ "native-rt_mac.cc" ]
        }],
        ["OS=='win'", {
          "sources": [ "native-rt_win.cc" ]
        }]
      ], 
      "include_dirs" : [
        "<!(node -e \"require('nan')\")"
      ]
    }
  ]
}