1

I'm writing a PowerShell script that is supposed to use CMake to create a Ninja Build for a Fortran project:

    Set-PSDebug -Trace 1

#MOVE TO load_modules
$env:CC="C:/Program Files (x86)/IntelSWTools/compilers_and_libraries_2018.1.156/windows/bin/intel64/icl.exe"
$env:CXX="C:/Program Files (x86)/IntelSWTools/compilers_and_libraries_2018.1.156/windows/bin/intel64/icl.exe"
#$env:FC="C:/Program Files (x86)/IntelSWTools/compilers_and_libraries_2018.1.156/windows/bin/intel64/ifort.exe"
$env:Path+=";C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\VC\Tools\MSVC\14.11.25503\bin\Hostx86\x86\"
$env:Path+=";C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\VC\Tools\MSVC\14.11.25503\bin\Hostx64\x64\"
$env:Path+=";C:\Program Files (x86)\Windows Kits\10\bin\10.0.16299.0\x86\"
$env:Path+=";C:\Program Files (x86)\IntelSWTools\compilers_and_libraries_2018.1.156\windows\bin\intel64\"
#$env:Path+=";C:\Windows\System32\kernel32.dll"

& 'C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\Common7\Tools\vsdevcmd'

$env:Path+=";C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\SDK\ScopeCppSDK\SDK\lib\kernel32.Lib"
echo $env:Path

# Install
mkdir build
mkdir install
pushd build
Start-Process $env:CMAKE -ArgumentList "-G ""Ninja"" 
      -DCMAKE_Fortran_COMPILER=""C:/Program Files (x86)/IntelSWTools/compilers_and_libraries_2018.1.156/windows/bin/intel64/ifort.exe""
      -DCMAKE_INSTALL_PREFIX=../install ../src" -NoNewWindow

However, CMake cannot sucessfully compile a C test program because 'kernel32.lib' cannot be found:

...
PS C:\Users\fnick\modflow> -- The C compiler identification is Intel
18.0.1.20171018
-- The CXX compiler identification is Intel 18.0.1.20171018
-- The Fortran compiler identification is unknown
-- Check for working C compiler: C:/Program Files (x86)/IntelSWTools/compilers_and_libraries_2018.1.156/windows/bin/inte l64/icl.exe
-- Check for working C compiler: C:/Program Files (x86)/IntelSWTools/compilers_and_libraries_2018.1.156/windows/bin/inte l64/icl.exe -- broken CMake Error at C:/Program Files/CMake/share/cmake-3.10/Modules/CMakeTestCCompiler.cmake:52 (message):   The C compiler

    "C:/Program Files (x86)/IntelSWTools/compilers_and_libraries_2018.1.156/windows/bin/intel64/icl.exe"

  is not able to compile a simple test program.

  It fails with the following output:

    Change Dir: C:/Users/fnick/modflow/build/CMakeFiles/CMakeTmp

    Run Build Command:"C:/Users/fnick/ninja.exe" "cmTC_27e84"
    [1/2] Building C object CMakeFiles\cmTC_27e84.dir\testCCompiler.c.obj
    [2/2] Linking C executable cmTC_27e84.exe
    FAILED: cmTC_27e84.exe
    cmd.exe /C "cd . && "C:\Program Files\CMake\bin\cmake.exe" -E vs_link_exe --intdir=CMakeFiles\cmTC_27e84.dir --manif ests  -- xilink /nologo CMakeFiles\cmTC_27e84.dir\testCCompiler.c.obj  /out:cmTC_27e84.exe /implib:cmTC_27e84.lib /pdb:c mTC_27e84.pdb /version:0.0  /machine:x64  /debug /INCREMENTAL /subsystem:console  kernel32.lib user32.lib gdi32.lib wins pool.lib shell32.lib ole32.lib oleaut32.lib uuid.lib comdlg32.lib advapi32.lib && cd ."
    LINK Pass 1: command "xilink /nologo CMakeFiles\cmTC_27e84.dir\testCCompiler.c.obj /out:cmTC_27e84.exe /implib:cmTC_ 27e84.lib /pdb:cmTC_27e84.pdb /version:0.0 /machine:x64 /debug /INCREMENTAL /subsystem:console kernel32.lib user32.lib g di32.lib winspool.lib shell32.lib ole32.lib oleaut32.lib uuid.lib comdlg32.lib advapi32.lib /MANIFEST /MANIFESTFILE:CMak eFiles\cmTC_27e84.dir/intermediate.manifest CMakeFiles\cmTC_27e84.dir/manifest.res" failed (exit code 1104) with the fol lowing output:
    LINK : fatal error LNK1104: Datei "kernel32.lib" kann nicht ge├Àffnet werden.
    ninja: build stopped: subcommand failed.




  CMake will not be able to correctly generate this project. Call Stack (most recent call first):   CMakeLists.txt:34 (project)
  ...

Datei "kernel32.lib" kann nicht ge├Àffnet werden.

means "File "kernel32.lib" cannot be opened". I'm assuming this means it could not be found.

Multiple things I don't get here: I thought that

& 'C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\Common7\Tools\vsdevcmd'

would basically set up the environment for me?! And also, I pointed the PATH directly to the necessary library:

$env:Path+=";C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\SDK\ScopeCppSDK\SDK\lib\kernel32.Lib"

but that didn't help (I tried without the explicit kernel32.Lib - didn't help; and why is there a capital 'L'?!)

From what I read, it looks like the Linux environment variables PATH and LD_LIBRARY_PATH both correspond to PATH on Windows?!

What am I missing here? What's the best way to set up a working build environment in PowerShell?

EDIT: Calling

& 'C:\Program Files (x86)\IntelSWTools\compilers_and_libraries_2018\windows\bin\compilervars.bat' intel64

Also doesn't help.

EDIT2:

Windows Version is "Windows Server 2012 R2 Standard" 64bit

Compilers: Intel Compilers 2018 Update 1

CMake version 3.10-rc3

Update: A colleague gave me the following script:

function StartVSNativeCommandPromt($VSVersion, $VSName, $ArchName, $VSKind, $Options)
{
    if(($VSVersion -Eq "12.0") -Or ($VSVersion -Eq "14.0"))
    {
        $dir = "c:\Program Files (x86)\Microsoft Visual Studio $VSVersion\VC"
    }
    else
    {
        $dir = "C:\Program Files (x86)\Microsoft Visual Studio\$VSName\$VSKind\VC\Auxiliary\Build"
    }

    pushd $dir
    cmd.exe /c "vcvarsall.bat $Options $ArchName&set" |
    foreach {
        if ($_ -match "=") {
            $v = $_.split("="); set-item -force -path "ENV:\$($v[0])"  -value "$($v[1])"
        }
    }
    popd

    write-host "`nVisual Studio $VSName Command Prompt variables set." -ForegroundColor Yellow
}



function StartIntelNativeCommandPromt($IntelVersion, $Arch, $VSVersion)
{
    Invoke-BatchFile "C:\Program Files (x86)\IntelSWTools\compilers_and_libraries_$IntelVersion\windows\bin\compilervars.bat" $Arch $VSVersion
}



function StartVS12x64NativeCommandPromt { StartVSNativeCommandPromt "12.0" "2013" "amd64"}

function StartVS12x86NativeCommandPromt { StartVSNativeCommandPromt "12.0" "2013" "x86"}



function StartVS14x64NativeCommandPromt { StartVSNativeCommandPromt "14.0" "2015" "amd64"}

function StartVS14x86NativeCommandPromt { StartVSNativeCommandPromt "14.0" "2015" "x86"}



function StartVS15x64NativeCommandPromt { StartVSNativeCommandPromt "15.0" "2017" "amd64" "Professional"}

function StartVS15x86NativeCommandPromt { StartVSNativeCommandPromt "15.0" "2017" "x86" "Professional"}



function StartVS15x64140NativeCommandPromt { StartVSNativeCommandPromt "15.0" "2017" "amd64" "Professional" "-vcvars_ver=14.0"}

function StartVS15x86140NativeCommandPromt { StartVSNativeCommandPromt "15.0" "2017" "x86" "Professional" "-vcvars_ver=14.0"}

function StartIntel17VS15x64NativeCommandPromt {

    if (-not (Test-Path env:VS2017INSTALLDIR)) { $env:VS2017INSTALLDIR = $env:VSINSTALLDIR }

    StartIntelNativeCommandPromt "2017.5.267" "intel64" "vs2017"

}

function StartIntel18VS15x64NativeCommandPromt {

    if (-not (Test-Path env:VS2017INSTALLDIR)) { $env:VS2017INSTALLDIR = $env:VSINSTALLDIR }

    StartIntelNativeCommandPromt "2018.1.156" "intel64" "vs2017"

}



Set-Alias vs12x64 StartVS12x64NativeCommandPromt

Set-Alias vs12x86 StartVS12x86NativeCommandPromt



Set-Alias vs14x64 StartVS14x64NativeCommandPromt

Set-Alias vs14x86 StartVS14x86NativeCommandPromt



Set-Alias vs15x64 StartVS15x64NativeCommandPromt

Set-Alias vs15x86 StartVS15x86NativeCommandPromt



Set-Alias vs15x64v14 StartVS15x64140NativeCommandPromt

Set-Alias vs15x86v14 StartVS15x86140NativeCommandPromt



Set-Alias intel17x64vs15 StartIntel17VS15x64NativeCommandPromt



Set-Alias intel18x64vs15 StartIntel18VS15x64NativeCommandPromt

which allows him to set up a working environment in PowerShell by calling "vs15x64". If I include that in my script, kernel32.lib is found, however then in the same step, libmmdd.lib isn't found. So the journey continues...

fpnick
  • 419
  • 2
  • 6
  • 18
  • Hi, the first thing that comes to mind is whether you have a recent version of cmake, can you give this information in your original post? Also: version of windows, 32-bit or 64-bit Windows and compilers? – Pierre de Buyl Feb 02 '18 at 12:16
  • Thanks for having a look. Updated. Also, I made some progress but I'm still not quite there... – fpnick Feb 02 '18 at 12:50
  • I hope some actual Windows user will help, I do not use it as a development platform. Also, I have no idea what is libmmdd and how it relates to your project :-) – Pierre de Buyl Feb 02 '18 at 12:56
  • I would start using the Visual Studio command prompt. After that I would run the Intels compilervars.bat from there with the correct settings. And then I would start manually cmake -G "Ninja" ..\src from your build directory. If that works you can sort out what you need to do in your powershell script. – vre Feb 02 '18 at 13:10
  • Try running vcvarsall.bat before you start building. That will set the environment variables for the libs. – cup Feb 02 '18 at 13:55
  • @vre : That idea was pretty good. Starting with the Visual Studio command prompt, I can call powershell in there and the env is set correctly. Thanks! I'll see if that's usable in my use case. – fpnick Feb 02 '18 at 13:59
  • @cup: The script I added under "Update" calls vcvarsall.bat. So I guess that's not all you need to do... – fpnick Feb 02 '18 at 13:59
  • But you need the compilervars.bat call too! I suppose this adds the library dirs of the Intel compiler to the environment. BTW, can you post relevant parts of your CMakeLists.txt file? I am wondering why a Fortran project needs the C and C++ compilers. – vre Feb 02 '18 at 14:07
  • @vre also tried compilervars.bat (and various combinations of all the different batch files...) The Project has both Fortran (mostly) and some C / C++ sources. So all the compilers are needed. – fpnick Feb 02 '18 at 14:12
  • Can you try the solutions from this thread? https://stackoverflow.com/questions/27623110/how-to-generate-a-visual-studio-project-that-uses-the-intel-compiler-using-cmake – vre Feb 02 '18 at 14:20
  • @vre Ninja doesn't support toolset specification... – fpnick Feb 02 '18 at 14:41
  • After calling vcvars.bat, add **get-children env:**. This will print out all the environment variables. Has the path etc been modified accordingly? – cup Feb 02 '18 at 14:43
  • Have a look at https://stackoverflow.com/questions/20077820/how-can-i-source-variables-from-a-bat-file-into-a-powershell-script to set the variables within your powershell from a batch file – cup Feb 02 '18 at 14:49
  • I would first try to make it run with the Visual Studio solution. If build times are an issue I would try to use MSBuild with the `maxcpucount` switch. BTW, you can run the solution from the commandline with `cmake --build . --target ALL_BUILD --config Debug`. This calls the underlying default build tool (MSBuild in this case). – vre Feb 02 '18 at 14:50

0 Answers0