How can I conditionally compile native code for node.js based on the node version using node-gyp?

StackOverflow https://stackoverflow.com/questions/11787553

  •  24-06-2021
  •  | 
  •  

Вопрос

I have some C++ code for a node.js module that is written for node 0.8 and its enhanced libuv. Specifically I am using the uv_mutex_* functions, which don't exist in the libuv included in node 0.6. I want to conditionally embed pthreads equivalents of these functions, but only when built on the old version.

This should give me a cross-platform build (Windows and Linux) when built on node 0.8 and a functioning Linux version if the node version is 0.6.

Is there a preprocessor #define for the node version? I can't find one. The best I can come up with is to do some kludgy stuff in binding.gyp to try to guess the version based on <@(node_root) and somehow massage that into a 'defines' value. There must be a better way!

Это было полезно?

Решение

The binding.gyp file of the Memwatch module does what you're looking for.

It defines a variable to capture the node version:

'variables': {
  'node_ver': '<!(node --version | sed -e "s/^v\([0-9]*\\.[0-9]*\).*$/\\1/")'
},

And then use it in a condition:

'target_conditions': [
  ['node_ver=="0.8"', { 'defines': ['NEW_COMPACTION_BEHAVIOR'] } ]
]

Not the most elegant solution, but apparently, there's nothing better right now.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top