cavis/vsconfig.gradle

93 lines
4.4 KiB
Groovy
Raw Normal View History

/*
*
* ******************************************************************************
* *
* * This program and the accompanying materials are made available under the
* * terms of the Apache License, Version 2.0 which is available at
* * https://www.apache.org/licenses/LICENSE-2.0.
* *
* * See the NOTICE file distributed with this work for additional
* * information regarding copyright ownership.
* * Unless required by applicable law or agreed to in writing, software
* * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* * License for the specific language governing permissions and limitations
* * under the License.
* *
* * SPDX-License-Identifier: Apache-2.0
* *****************************************************************************
*
*/
/****************************************************************************
* Establish Visual Studio configuration environment for Windows native builds
*
* NOTE: We do not rely on the VisualCpp plugin to identify paths since it has
* proven in the past to be unreliable when multiple versions of Visual Studio
* and SDKs are installed.
****************************************************************************/
/*
if (!hasProperty("VISUAL_STUDIO_INSTALL_DIR") && osdetector.os.equals("windows")) {
if (project.hasProperty("skip-native") && !project.getProperty("skip-native").equals("true")) {
configureVisualStudio()
}
}
*/
if(osdetector.os.equals("windows")) {
logger.quiet("Detected windows operating system. Running configureVisualStudio().")
configureVisualStudio()
}
def configureVisualStudio() {
if (org.gradle.internal.os.OperatingSystem.current().isWindows()) {
// Initialize variables
rootProject.ext.VISUAL_STUDIO_INSTALL_DIR = ""
rootProject.ext.VISUAL_STUDIO_TOOLS_VERSION_DEFAULT = ""
rootProject.ext.VISUAL_STUDIO_SDK_DIR_DEFAULT = ""
rootProject.ext.VISUAL_STUDIO_SDK_VERSION_DEFAULT = ""
rootProject.ext.VISUAL_STUDIO_SDK_VERSION_OVERRIDE = ""
rootProject.ext.VISUAL_STUDIO_VCVARS_CMD = ""
// Use vswhere.exe to search for latest Visual Studio installation
logger.quiet("Searching for latest Visual Studio and required components...")
def vswherePath = "C:\\Program Files (x86)\\Microsoft Visual Studio\\Installer\\vswhere.exe"
if (!file(vswherePath).exists()) {
logger.error("Visual Studio not found!")
return
}
def vswhereOutput = "${vswherePath} -latest -format json".execute().text.trim()
2022-10-21 15:19:32 +02:00
def vswhereJson = new groovy.json.JsonSlurper().parseText(vswhereOutput)
if (vswhereJson.isEmpty()) {
logger.error("Visual Studio not found!")
return
}
def vsInstallDir = vswhereJson[0].installationPath
logger.quiet(" -> Installation Directory: ${vsInstallDir}")
// Use vcvarsall.bat to determine the latest Visual Studio's default SDK and tool versions
def vcvarsPath = "${vsInstallDir}\\VC\\Auxiliary\\Build\\vcvarsall.bat"
def vcvarsCmd = "\"${vcvarsPath}\" x64"
def vcvarsEnvCmd = "cmd /v:ON /c ${vcvarsCmd} > nul && cmd /c echo"
def toolsVersion = "${vcvarsEnvCmd} !VCToolsVersion!".execute().text.trim()
logger.quiet(" -> VCTools Version (default): ${toolsVersion}")
def sdkDir = "${vcvarsEnvCmd} !WindowsSdkDir!".execute().text.trim()
logger.quiet(" -> SDK Directory (default): ${sdkDir}")
def sdkVersion = "${vcvarsEnvCmd} !WindowsSDKVersion!".execute().text.trim().replace('\\', '')
logger.quiet(" -> SDK Version (default): ${sdkVersion}")
// Check Gradle properties for override values
def windowsTargetPlatformVersion = findProperty("WindowsTargetPlatformVersion")
logger.quiet(" -> SDK Version (override): " + (windowsTargetPlatformVersion ?: "N/A"))
// Save Visual Studio information so other projects can access it
rootProject.ext.VISUAL_STUDIO_INSTALL_DIR = vsInstallDir
rootProject.ext.VISUAL_STUDIO_TOOLS_VERSION_DEFAULT = toolsVersion
rootProject.ext.VISUAL_STUDIO_SDK_DIR_DEFAULT = sdkDir
rootProject.ext.VISUAL_STUDIO_SDK_VERSION_DEFAULT = sdkVersion
rootProject.ext.VISUAL_STUDIO_SDK_VERSION_OVERRIDE = windowsTargetPlatformVersion
rootProject.ext.VISUAL_STUDIO_VCVARS_CMD = vcvarsCmd
}
}