c++windowsbuildqbsclang-cl

QBS build system, can't initialize environment with vcvars64.bat


I'm trying to implement my own module to build C++ on Windows with clang-cl toolchain as there's no built-in support in QBS right now.

I chose to use lld-link instead of microsoft linker, so I have to supply it with all the MS library include paths manually. With these paths hardcoded, I manage to build my apps fine. But I'd like to make my module more flexible and use %LIB% environment variable set by vcvars32.bat|vcvars64.bat

As far as I understand, this could (should?) be done inside module's setupBuildEnvironment script. Here's what I try to read the %LIB% and fail:

import qbs.Environment
import qbs.Process

Module
{
    setupBuildEnvironment:
    {
        var p = new Process();
        p.exec("vcvars64.bat", [], true);
        // makes no difference
        // p.exec("cmd", ["/c", "vcvars64.bat"], true);
        var lib = p.getEnv("LIB");
        // this fails too
        // var lib = Environment.getEnv("LIB");
        console.info("LIB = " + lib);
        p.close();
    }
    ...
}

This gives me LIB = so I'm getting nowhere. My guess is that the process is already terminated at the moment of querying the variable (p.getEnv("LIB")), hence the empty result. The QBS docs for Process.getEnv() state nothing in this regard.

What is the correct QBS way to initialize environment with vcvars64.bat, and more broadly, what is the correct way to get environment of a process inside setupBuildEnvironment?


[update] Well, embarassingly, this was easy to work around by creating a simple batch and getting rid of setupBuildEnvironment script altogether:

 @echo off
 call vcvars64 && qbs

But I'd like to avoid batch scripting as much as possible, so the question still stands.


Solution

  • The vars batch files just dump some information onto the console. That does not set an environment on the calling process in any way. You would need to parse the process output. I suggest you take a look at the MsvcProbe item in the qbs sources to see how that is implemented for MSVC. You might be able to adapt the code for clang-cl.