4

I'm trying to detect a msvc version during node-gyp configure in my binding.gyp file. Basically, I want to be able to link against particular 3rdparty library based on Visual C++ version:

['OS=="win"' and 'toolset="vc12"' , {
    'libraries': [
        "opencv/lib/vc12/opencv_world300.lib"
    ],
}],

['OS=="win"' and 'toolset="vc11"' , {
    'libraries': [
        "opencv/lib/vc11/opencv_world300.lib"
    ],
}],

['OS=="win"' and 'toolset="vc10"' , {
    'libraries': [
        "opencv/lib/vc10/opencv_world300.lib"
    ],
}]

Unfortunately, neither toolset, nor _toolset or even $(TOOLSET) variables are defined in GYP. I wasn't able to find such variable in GYP documentation. Is it possible at all?

1 Answer 1

5

I couldn't figure out from the docs that how to check for toolset version, but only found the top-level settings: https://chromium.googlesource.com/external/gyp/+/master/docs/UserDocumentation.md#Skeleton-of-a-typical-executable-target-in-a-gyp-file.

However, @saper on GitHub figured it out using MSVS_VERSION instead:

['OS=="win"' and 'MSVS_VERSION=="2013"' , {
    'libraries': [
        "opencv/lib/vc12/opencv_world300.lib"
    ],
}],

['OS=="win"' and 'MSVS_VERSION=="2012"' , {
    'libraries': [
        "opencv/lib/vc11/opencv_world300.lib"
    ],
}],

['OS=="win"' and 'MSVS_VERSION=="2010"' , {
    'libraries': [
        "opencv/lib/vc10/opencv_world300.lib"
    ],
}]

(nit: in your example, although toolset token is not identified by gyp, = should be replaced with ==)

Example: https://github.com/saper/node-sass/blob/c7e9cf0f0e0098e8316bd41722fc2edf4a835d9f/src/libsass.gyp#L91-L94.

Limitation 1:

Unfortunately, these conditions are not emitted in the .targets or .vcxproj files (such as this), but it will emit the .vcxproj after post-processing the conditions separate for the given version of MSVS and hence renders the .vcxproj file incompatible with newer/older versions of VCR.

However, the MSVS version can by overridden for gyp in multiple ways, for instance, using environment variable:

In CMD:

SET GYP_MSVS_VERSION=2012

Or in PowerShell:

$env:GYP_MSVS_VERSION=2015

It can also be passed as a command line argument:

node_modules/.bin/node-gyp build --msvs_version=2012

If both env-var and command line argument are present, CLI arg will take the precedence.

This CLI argument can be supplied to npm task, for example, to enforce the constraint for all the Windows consumers of your package to use specific version of MSVCR otherwise error.

and since so forth..

Limitation 2:

From CLI arg, there is no way to specify minimum MSVS version no such flag as: --min-msvs-version.

Limitation 3:

In case of multiple version of MSBUILD installed, node-gyp's MSBUILD discovery (at present) will ignore the preferred/required version of toolset by .vcxproj, but will give precedence to the one in PATH. In this case, you may get errors, for instance if you are using C99/C++1[1/4/7] features only offered by VS2015. To remedy this situation:

  • either reset PATH to the desired version MSBuild bin directory.
  • instead of node-gyp build or rebuild, use node-gyp configure followed by "%ProgramFiles(x86)%\MSBuild\14.0\Bin\MSBuild" build/binding.sln /p:Configuration=Release (or from posh, it would become: &"${env:ProgramFiles(x86)}\MSBuild\14.0\Bin\MSBuild" build\binding.sln /p:Configuration=Release)
  • by sending a Pull Request for node-gyp and pangyp to fix the toolset-version-aware MSBUILD discovery, if your Windows Registry skills are not as rusty as mine. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.