gyp-next icon indicating copy to clipboard operation
gyp-next copied to clipboard

feat(msvs): add support for CL env var

Open mscdex opened this issue 4 years ago • 5 comments

This is probably not the best place for this line to be as I'm not a gyp expert, but it works for me. I'm completely open to suggestions if anyone has better ideas on how to achieve this.

My use case is I wanted to easily set some compiler flags (e.g. _WIN32_WINNT) without having to touch every gyp/gypi file in a project. On *nix you can simply set the CFLAGS/CXXFLAGS/etc. environment variables and it'll just work.

However on Windows it's a different story. Technically you can set a CL environment variable to pass flags to CL.exe, but since gyp uses msbuild, environment variables like CL don't get picked up/used automatically. Because of this, we need to explicitly pass the values to the msbuild project file.

I tried looking to see if node-gyp/gyp had some way of achieving the same thing using the command line, but I could not find anything, so using environment variables seems like the next best bet.

mscdex avatar Nov 03 '21 06:11 mscdex

Also, I'm not entirely sure but "cl" might need to also be added to msvs_emulation.py in the environment variable list in _ExtractImportantEnvironment. It appears that file is mostly for ninja builds, which I can't test right now.

mscdex avatar Nov 03 '21 06:11 mscdex

Please add doctests to this function. Something like:

def _GetValueFormattedForMSBuild(tool_name, name, value):
    """
    >>> _GetValueFormattedForMSBuild(
    ...     tool_name="tool_name", name="name", value=[]
    ... )
    ''
    >>> _GetValueFormattedForMSBuild(
    ...     tool_name="tool_name", name="name", value=["DelayLoadDLLs"]
    ... )
    'DelayLoadDLLs'
    >>> _GetValueFormattedForMSBuild(
    ...     tool_name="Lib", name="AdditionalOptions", value=["DelayLoadDLLs"]
    ... )
    'DelayLoadDLLs %(AdditionalOptions)'
    >>> _GetValueFormattedForMSBuild(
    ...     tool_name="ClCompile", name="AdditionalOptions", value=["DelayLoadDLLs"]
    ... )
    'DelayLoadDLLs %(AdditionalOptions)'
    """

cclauss avatar Nov 03 '21 09:11 cclauss

@cclauss Added.

mscdex avatar Nov 05 '21 00:11 mscdex

It looks like the unrelated windows integration failure is because node is floating changes on top of gyp that do not exist in node-gyp's vendored copy of gyp or in gyp-next itself. Specifically, gyp isn't setting PRODUCT_DIR_ABS. The use of this variable started with the landing of OpenSSL 3.0 I believe.

mscdex avatar Nov 06 '21 03:11 mscdex

It looks like the unrelated windows integration failure is because node is floating changes on top of gyp that do not exist in node-gyp's vendored copy of gyp or in gyp-next itself. Specifically, gyp isn't setting PRODUCT_DIR_ABS. The use of this variable started with the landing of OpenSSL 3.0 I believe.

I opened https://github.com/nodejs/node/issues/40735

targos avatar Nov 06 '21 08:11 targos