Native: how to select a specific toolchain from a command line parameter/option?

Hi
I am running a native gradle script to build a multi project application and this applicaiton should be build for all those VisualStudio versions

  1. VS2008
  2. VS2010
  3. VS2012
  4. VS2013
  5. VS2015
  6. VS2017

Thus the script is defining those toolchains as

toolChains {
	VS2008(VisualCpp) {
		installDir "C:/Program Files (x86)/Microsoft Visual Studio 9.0"
	}
	VS2010(VisualCpp) {
		installDir "C:/Program Files (x86)/Microsoft Visual Studio 10.0"
	}
	VS2012(VisualCpp) {
		installDir "C:/Program Files (x86)/Microsoft Visual Studio 11.0"
	}
	VS2013(VisualCpp) {
		installDir "C:/Program Files (x86)/Microsoft Visual Studio 12.0"
	}
	VS2015(VisualCpp) {
		installDir "C:/Program Files (x86)/Microsoft Visual Studio 14.0"
	}
	VS2017(VisualCpp) {
		installDir "C:/Program Files (x86)/Microsoft Visual Studio/2017/Community"
	}
}

How can define the script so that one of these toolchain be selected with a “VS” parameter/option as for exemple

gradlew build -DVS=VS2010

and moreover, when no selection is made, gradle selects a default toolchain?

Thanks @zosrothko for the question. This is indeed a feature that is lacking in Gradle at the moment to choose the target compatibility which ends up being the tool chain that would result in been used.

I suggest having something like the following. Note that here the default is VS2017 but you could do a lightweight discovery to see if the installDir exists in the order you want to default to.

def getSelectedVisualStudioToolChain() {
    String selector = System.properties["VS"]

    if (selector == null) {
        selector = "VS2017"
    }

    String installDir = null
    switch (selector) {
        case "VS2008": installDir = "file://C:/Program Files (x86)/Microsoft Visual Studio 9.0"; break;
        case "VS2010": installDir = "file://C:/Program Files (x86)/Microsoft Visual Studio 10.0"; break;
    	case "VS2012": installDir = "file://C:/Program Files (x86)/Microsoft Visual Studio 11.0"; break;
    	case "VS2013": installDir = "file://C:/Program Files (x86)/Microsoft Visual Studio 12.0"; break;
    	case "VS2015": installDir = "file://C:/Program Files (x86)/Microsoft Visual Studio 14.0"; break;
    	case "VS2017": installDir = "file://C:/Program Files (x86)/Microsoft Visual Studio/2017/Community"; break;
        default:
            throw new IllegalArgumentException()
    }

    return [
        name: selector,
        installDir: installDir,
    ]
}

model {
    toolChains {
        def vsToolChain = getSelectedVisualStudioToolChain()
        create(vsToolChain.name, VisualCpp) {
            installDir vsToolChain.installDir
        }
    }
}

This use case is set to be solved with the new native plugins. Don’t hesitate to ask more question,

Daniel

Update: I removed a leftover println in the code.

1 Like

Thank Daniel for the tip. That’s really awesome :clap: