How do I override the toolChain for 32bit vs 64bit cpp builds

Hello, I’ve stumbled into a bit of a problem regarding a build I’ve inherited. We’ve recently moved to gradle so I’m still getting up to speed. My problem is I need to produce both a 32bit and 64bit version of our dll. This would be easy if it wasn’t for the fact that we have a dependency on another runtime dll.

Unfortunately the 32bit version of the dependency has been built with Visual Studio 2008 (and hence requires the msvc80 runtime) whilst the 64bit version is built with Visual studio 2012.

I can produce a build.gradle to build our dll for each of these targets but I am struggling to have a single build file that produces both 32bit and 64bit versions.

I’ve specified the plaforms as follows

platforms {
	x64 {
		architecture "x86_64"
	}
	x86 {
		architecture "x86"
	}
}

and the component as

model {
	components {
			MyComponent(NativeLibrarySpec) {
		
			targetPlatform "x86"
			targetPlatform "x64"
			
			sources {
				cpp {
					source {

etc but the problem is each platform requires a different toolChain. I can define my toolChains as follows

toolChains {
	visualCpp32(VisualCpp) {
		windowsSdkDir winSdkDir32
		installDir visualCppDir32
	}
	visualCpp64(VisualCpp) {
		windowsSdkDir winSdkDir64
		installDir visualCppDir64
	}
}

but there is no way to select which toolChain to use for each target platform. The visualCpp toolChain does not allow

toolChains {
	visualCpp32(VisualCpp) {
		target("x86") {
			windowsSdkDir winSdkDir32
			installDir visualCppDir32
		}
	}
	visualCpp64(VisualCpp) {
		target("x64") {
			windowsSdkDir winSdkDir64
			installDir visualCppDir64
		}
	}
}

Is there an easy way to achieve this without resorting to 2 build files ?

Have you tried moving the target (“x64”) inside of the the first VisualCpp ?

Here is the sample for the gradle repository https://github.com/gradle/gradle/blob/master/subprojects/docs/src/samples/native-binaries/target-platforms/build.gradle

Unlike gcc, the visualCpp toolChain does not allow you to specify the target. The last sample I posted is what I would like to do but isn’t supported. It is supported by the gcc toolChain but that doesn’t help me.

There is also a eachPlatform construct which lets you loop over available platforms. Have you tried that?

Example of how it could be done you can see here.

Then you would do

visualCpp {
eachPlatform { toolchain ->
def platform = toolchain.getPlatform()
if (platform.name == "x64") {
    }
  }
}

Thanks for the suggestion. I initially thought this might just work but after some experimentation I realized that all this does it iterate over the list of platforms setting windowsSdkDir and installDir in turn. The end result is these settings retain the last matching platform which is then used for both x86 and x64 builds.

Seems I will have to create separate builds for the time being and hope a future version of the cpp plugin allows the selection of the toolChain per platform/flavor

Another thing that you could do is try to set it with the rule source plugin. Rule source plugin that modifies the toolchains model element.