Can an environment variable or property be used to select the toolchain used by the native plugin?

I’m using travis-ci to test my native code. I would like to compile against both gcc and clang on that environment. The way this information is provided from travis-ci is via altering $CXX. From what I’ve read in the documentation for gradle it is selected by the ordering in the build.gradle of the toolchains.

In case it may be of use here is my build.gradle

apply plugin: 'cpp'
  model {
 repositories {
  libs(PrebuiltLibraries) {
   jvm {
    def javaHome = "${org.gradle.internal.jvm.Jvm.current().javaHome}"
    headers.srcDirs = ["${javaHome}/include"]
    binaries.withType(SharedLibraryBinary) {
     def os = targetPlatform.operatingSystem
     if(os.macOsX) {
      headers.srcDirs += "${javaHome}/include/darwin"
      sharedLibraryFile = file("${javaHome}/jre/lib/server/libjvm.dylib")
     } else {
      headers.srcDirs += "${javaHome}/include/linux"
      sharedLibraryFile = file("${javaHome}/jre/lib/amd64/server/libjvm.so")
     }
      }
   }
  }
 }
 toolChains {
  clang(Clang) {
   cppCompiler.withArguments { args ->
    args << "-std=c++11"
   }
  }
  gcc(Gcc)
 }
}
  executables {
    main {}
}
  sources {
 main {
  cpp {
   lib library: 'jvm', linkage: 'shared'
   source {
    srcDir "src"
    include "**/*.cpp"
   }
   exportedHeaders {
    srcDirs "include"
   }
  }
 }
}
  task wrapper(type: Wrapper) {
    gradleVersion = '2.0'
}

As always, find the answer as soon as you ask.

You can conditionally include toolchains ie:

toolChains {
 if(false)
  clang(Clang)
 gcc(Gcc)
}

Full solution:

apply plugin: 'cpp'
  // if CXX is explicitly defined, only include that toolchain.
enum CppSupport {
 CLANG("clang"),
 GCC("g++"),
 ALL("");
   final String comparison
 CppSupport(final String comparison) {
  this.comparison = comparison
 }
   public static CppSupport getSupport(final String cxx) {
  if (cxx?.toLowerCase().contains(CLANG.comparison)) {
   return CLANG;
  }
  else if(cxx?.toLowerCase().contains(GCC.comparison)) {
   return GCC;
  }
  else {
   return ALL;
  }
 }
}
  def cppSupport = CppSupport.getSupport("$System.env.CXX");
  model {
 repositories {
  libs(PrebuiltLibraries) {
   jvm {
    def javaHome = "${org.gradle.internal.jvm.Jvm.current().javaHome}"
    headers.srcDirs = ["${javaHome}/include"]
    binaries.withType(SharedLibraryBinary) {
     def os = targetPlatform.operatingSystem
     if(os.macOsX) {
      headers.srcDirs += "${javaHome}/include/darwin"
      sharedLibraryFile = file("${javaHome}/jre/lib/server/libjvm.dylib")
     } else if(os.linux){
      headers.srcDirs += "${javaHome}/include/linux"
      sharedLibraryFile = file("${javaHome}/jre/lib/amd64/server/libjvm.so")
     } else {
      throw new IllegalArgumentException("Unable to support target platform operating system.")
     }
    }
   }
  }
 }
 toolChains {
  if(cppSupport == CppSupport.ALL || cppSupport == CppSupport.CLANG) {
   logger.info("Including CLANG in possible toolchains")
   clang(Clang) {
    cppCompiler.withArguments { args ->
     args << "-std=c++11"
    }
   }
  }
  if(cppSupport == CppSupport.ALL || cppSupport == CppSupport.GCC) {
   logger.info("Including GCC in possible toolchains")
   gcc(Gcc) {
    cppCompiler.withArguments { args ->
     args << "-std=c++11"
    }
   }
  }
 }
}
  executables {
    main {}
}
  sources {
 main {
  cpp {
   lib library: 'jvm', linkage: 'shared'
   source {
    srcDir "src"
    include "**/*.cpp"
   }
   exportedHeaders {
    srcDirs "include"
   }
  }
 }
}
  task wrapper(type: Wrapper) {
    gradleVersion = '2.0'
}