Skip to content

Commit 7bf1070

Browse files
committed
fix: prefer MSVC dev variables over CMake generator platform
1 parent b3530ae commit 7bf1070

File tree

3 files changed

+24
-11
lines changed

3 files changed

+24
-11
lines changed

src/argumentBuilder.ts

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export class ArgumentBuilder {
1818
baseCommand += ` ${defines.map((d) => `-D${d[0]}="${d[1]}"`).join(" ")}`
1919
if (this.config.generatorToUse !== "native") {
2020
baseCommand += ` -G"${this.config.generatorToUse}"`
21-
if (this.config.generatorFlags !== undefined) {
21+
if (this.config.generatorFlags !== undefined && defines.some((d) => d[0] === "CMAKE_GENERATOR_PLATFORM")) {
2222
baseCommand += ` ${this.config.generatorFlags.map((f) => `"${f}"`).join(" ")}`
2323
}
2424
}
@@ -111,13 +111,15 @@ export class ArgumentBuilder {
111111
}
112112

113113
if (this.config.os === "win32") {
114-
const hostArch = getMsvcArch(this.config.arch)
115-
const targetArch = getMsvcArch(this.config.arch)
116-
const msvcArch = hostArch === targetArch ? hostArch : `${hostArch}_${targetArch}`
117-
setupMSVCDevCmd(msvcArch)
118-
119-
// set the CMake generator platform to the target architecture
120-
retVal.push(["CMAKE_GENERATOR_PLATFORM", cmakeArch])
114+
try {
115+
setupMSVCDevCmd(this.config.arch)
116+
} catch (e) {
117+
logger.warn(
118+
`Failed to setup MSVC variables for ${this.config.arch}: ${e}. Using CMake generator platform instead.`,
119+
)
120+
// set the CMake generator platform to the target architecture
121+
retVal.push(["CMAKE_GENERATOR_PLATFORM", cmakeArch])
122+
}
121123
}
122124
}
123125

src/generator.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,14 @@ export const getCmakeGenerator = memoizee(
4747
if (matchedGeneratorLine !== undefined) {
4848
const [_line, parsedGenerator, archBracket] = matchedGeneratorLine
4949
const useArchSwitch = (archBracket as string | undefined) === undefined
50-
const generator = useArchSwitch ? parsedGenerator : `${parsedGenerator} ${arch === "x64" ? "Win64" : arch === "ia32" ? "Win32" : ""}`
51-
const generatorFlags = useArchSwitch ? ["-A", getCMakeArchitecture(arch, os)] : undefined;
50+
const archString = arch === "x64" ? " Win64" : arch === "ia32" ? " Win32" : ""
51+
if (archString === "") {
52+
logger.warn(
53+
`Unsupported architecture: ${arch} for generator ${parsedGenerator}. Using without arch specification.`,
54+
)
55+
}
56+
const generator = useArchSwitch ? parsedGenerator : `${parsedGenerator}${archString}`
57+
const generatorFlags = useArchSwitch ? ["-A", getCMakeArchitecture(arch, os)] : undefined
5258

5359
logger.debug(`Using generator: ${generator} ${generatorFlags} for ${os} ${arch}`)
5460
return {

src/vcvarsall.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,11 @@ export function setupMSVCDevCmd(arch: string, vsversion?: string) {
153153
if (process.platform !== "win32") {
154154
return
155155
}
156-
const msvcArch = getMsvcArch(arch)
156+
const hostArch = getMsvcArch(process.arch)
157+
const targetArch = getMsvcArch(arch)
158+
const msvcArch = hostArch === targetArch ? targetArch : `${hostArch}_${targetArch}`
159+
logger.debug(`Setting up MSVC for ${msvcArch}`)
160+
console.group()
157161

158162
// Add standard location of "vswhere" to PATH, in case it's not there.
159163
process.env.PATH += delimiter + VSWHERE_PATH
@@ -221,4 +225,5 @@ export function setupMSVCDevCmd(arch: string, vsversion?: string) {
221225
process.env[name] = new_value
222226
}
223227
}
228+
console.groupEnd()
224229
}

0 commit comments

Comments
 (0)