OpenSC / OpenSC

Open source smart card tools and middleware. PKCS#11/MiniDriver/Tokend
https://github.com/OpenSC/OpenSC/wiki
GNU Lesser General Public License v2.1
2.57k stars 733 forks source link

Compiling on Windows ignores CNGSDK_INCL_DIR and CPDK_INCL_DIR env. variables values #3037

Closed Kajusn closed 8 months ago

Kajusn commented 8 months ago

Problem Description

I've been trying to set up a CI/CD workflow to build OpenSC from source on a Windows machine in a docker container. I followed the Compiling on Windows guide My docker container CPDK installation is at C:\CPDK and WiX v3.14 at C:\Program Files (x86)\WiX Toolset v3.14. I have set the following environment variables prior to building:

Building OpenSC using nmake /f Makefile.mak results in CNGSDK_INCL_DIR and CPDK_INCL_DIR env. variables not being used. This results in minidriver compilation failing due to cardmod.h not being found, because the search happens in C:\Program Files (x86)\Windows Kits\10\Cryptographic Provider Development Kit\Include, which is not where my CPDK installation is.

Building OpenSC installer immediately afterwards (cd win32 and nmake /f Makefile.mak OpenSC.msi) results in CNGSDK_INCL_DIR and CPDK_INCL_DIR env. variables being used correctly, for some reason.

Oddly enough, it can be seen in the logs that the WIX environment variable is identified properly in both calls of nmake.

Steps to reproduce

  1. Install CPDK at C:\CPDK
  2. Install WiX v3.14 at C:\Program Files (x86)\WiX Toolset v3.14
  3. Set the following env. variables:
    • CNGSDK_INCL_DIR="/IC:\CPDK\Cryptographic Provider Development Kit\Include"
    • CPDK_INCL_DIR="/IC:\CPDK\Cryptographic Provider Development Kit\Include"
    • set "WIX=C:\Program Files (x86)\WiX Toolset v3.14"
    • set WIXVSVER=VS2017
  4. Run nmake /f Makefile.mak
  5. Run cd win32
  6. Run nmake /f Makefile.mak OpenSC.msi

Logs

nmake /f Makefile.mak:

...
minidriver.c(74): fatal error C1083: Cannot open include file: 'cardmod.h': No such file or directory

NMAKE : fatal error U1077: 'cl /O1 /GS /W3 /WX /D_CRT_SECURE_NO_DEPRECATE /D_CRT_NONSTDC_NO_WARNINGS /MT /nologo /DHAVE_CONFIG_H /I..\..\win32 /I..\..\src       "/IC:\Program Files (x86)\Windows Kits\10\Cryptographic Provider Development Kit\Include" "/IC:\Program Files (x86)\Microsoft CNG Development Kit\Include" "/IC:\Program Files (x86)\WiX Toolset v3.14\SDK\VS2017\inc" /DWINVER=0x0601 /D_WIN32_WINNT=0x0601 /DWIN32_LEAN_AND_MEAN    /DENABLE_MINIDRIVER /DENABLE_SM  /DOPENSC_FEATURES="\"pcsc\"" /Zi /c minidriver.c ' : return code '0x2'

cd win32 and nmake /f Makefile.mak OpenSC.msi result in the following:

...
cl /O1 /GS /W3 /WX /D_CRT_SECURE_NO_DEPRECATE /D_CRT_NONSTDC_NO_WARNINGS /MT /nologo /DHAVE_CONFIG_H /I..\win32 /I..\src       "/IC:\CPDK\Cryptographic Provider Development Kit\Include" "/IC:\CPDK\Cryptographic Provider Development Kit\Include" "/IC:\Program Files (x86)\WiX Toolset v3.14\SDK\VS2017\inc" /DWINVER=0x0601 /D_WIN32_WINNT=0x0601 /DWIN32_LEAN_AND_MEAN    /DENABLE_MINIDRIVER /DENABLE_SM  /DOPENSC_FEATURES="\"pcsc\"" /Zi /c customactions.cpp
...
light.exe : error LGHT0103 : The system cannot find the file '..\src\minidriver\opensc-minidriver.dll' with type ''.

NMAKE : fatal error U1077: '"C:\Program Files (x86)\WiX Toolset v3.14\bin\light.exe" -sh -ext WixUIExtension -ext WiXUtilExtension OpenSC.wixobj' : return code '0x67'

Stop.
frankmorgner commented 8 months ago

Adapt Make.rules.mak to your needs instead of setting environment variables

Kajusn commented 8 months ago

Took @frankmorgner's suggestion and it works! Thanks, I shall close this issue as it does not seem to be a bug, but rather my mistake not modifying Make.rules.mak prior to building.

For anyone coming across this in the future, my solution was to modify Make.rules.mak with the following, not so pretty commands in Windows CMD:

powershell -command "(get-content Make.rules.mak) -replace 'WIX\s*=\s*.*?$','WIX = C:\Program Files (x86)\WiX Toolset v3.14' | Set-Content Make.rules.mak"
powershell -command "(get-content Make.rules.mak) -replace 'CPDK_INCL_DIR\s*=\s*.*$','CPDK_INCL_DIR = \"/IC:\CPDK\Cryptographic Provider Development Kit\Include\"' | Set-Content Make.rules.mak"
powershell -command "(get-content Make.rules.mak) -replace 'CNGSDK_INCL_DIR\s*=\s*.*$','CNGSDK_INCL_DIR = \"/IC:\CPDK\Cryptographic Provider Development Kit\Include\"' | Set-Content Make.rules.mak"