关于Visual Studio:如何使用cmake检测64位MSVC?

How to detect if 64 bit MSVC with cmake?

我有一个使用cmake的项目,一个目标设置为仅使用MSVC构建:

1
2
3
 if (MSVC)
     add_library(test SHARED source.cpp)
 endif()

现在另一个问题是该目标仅适用于MSVC 32位。 那么,如何检测生成器是MSVC64并跳过此目标?


检查您是否为64位体系结构生成的通常方法是测试CMAKE_SIZEOF_VOID_P:

1
2
3
4
5
if(CMAKE_SIZEOF_VOID_P EQUAL 8)
    # 64 bits
elseif(CMAKE_SIZEOF_VOID_P EQUAL 4)
    # 32 bits
endif()


CMake本身也使用几种方法来检查"不是64位":

1
2
3
4
5
6
7
8
9
10
11
if(NOT"${CMAKE_GENERATOR}" MATCHES"(Win64|IA64)")
    ...
endif()

if("${CMAKE_SIZEOF_VOID_P}" STREQUAL"4")
    ...
endif()

if(NOT CMAKE_CL_64)
   ...
endif()

参考文献

  • CMAKE_GENERATOR
  • CMAKE_SIZEOF_VOID_P
  • CMAKE_CL_64


在最新版本的CMake / Visual Studio中,位数是通过CMAKE_GENERATOR_PLATFORM选择的,可以在命令行上使用-A选项指定:

1
cmake -G"Visual Studio 16 2019" -A Win32 -DCMAKE_BUILD_TYPE=Release ..

因此,基于此功能,您可以从CMakeLists.txt中查询值:

1
2
3
if(NOT ("${CMAKE_GENERATOR_PLATFORM}" STREQUAL"Win64"))
    ...
ENDIF()


这是我最初开始编写的脚本,用于检测目标是x86(32位x86)还是amd64(64位x86)。然后,我找到了Jake Petroules的脚本,并继续吸收了从Internet上搜出的多个来源的点点滴滴,通常使用C编译器来检测目标体系结构。有许多不同的C编译器,它们给出了许多不一致的答案。该脚本旨在处理所有这些问题(理论上),并赋予CMake更高的检测保真度。它通过使用预处理器传达回CMake脚本来工作。将其放在您的CMake模块文件夹中,并至少include()在您的项目中将其放置一次(它具有包含保护,因此可以包含多个包含物)。然后,在included()此脚本的文件夹以及所有子文件夹中,将有一个变量CMAKE_CPU_ARCHITECTURES。该变量是一个列表(因为XCode项目文件可以一次针对多个体系结构)。您应该检查一下想要的列表中是否有该列表。

1
2
3
4
if("amd64" IN_LIST CMAKE_CPU_ARCHITECTURES) # I am targeting 64-bit x86.
...
if("x86" IN_LIST CMAKE_CPU_ARCHITECTURES) # I am targeting 32-bit x86.
...

x86(32位)或amd64(64位)。这只是目标架构(即CPU芯片)。要检测操作系统,生成器,编译器等是另一回事,但是您可以使用嵌套在各种其他if语句中的体系结构检查,例如if(WIN32)(令人困惑的意思是只针对某种Windows,不一定是32位)或if(MSVC)(如果您对编译器感兴趣)等等,依此类推,视情况而定。

(我有一个类似的脚本来检查特定的操作系统,它能够检测特定的事物并返回诸如watchOS vs. iOS vs. macOSWindows CE之类的值,依此类推,但这是另一回事了。 )。

detect_cpu_architectures.cmake:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
if(NOT detect_cpu_architectures_INC) # include guard
set(detect_cpu_architectures_INC)

# This code was originally written by Jake Petroules,
# https://github.com/axr/solar-cmake/blob/master/TargetArch.cmake

# then edited/extended by Daniel Russell with help from
# https://code.qt.io/cgit/qt/qtbase.git/tree/src/corelib/global/qprocessordetection.h
# https://opensource.apple.com/source/WTFEmbedded/WTFEmbedded-95/wtf/Platform.h.auto.html
# http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.kui0098a/armccref_babjidgc.htm
# https://sourceforge.net/p/predef/wiki/Architectures/
# https://blog.kowalczyk.info/article/j/guide-to-predefined-macros-in-c-compilers-gcc-clang-msvc-etc..html

# Regarding POWER/PowerPC, just as is noted in the Qt source,
#"There are many more known variants/revisions that we do not handle/detect."
# Set ppc_support to TRUE before including this file or ppc and ppc64
# will be treated as invalid architectures since they are no longer supported by Apple

# TODO: add more chips!!!! Tedious though.    
enable_language(C) # this must be at file-scope not function scope. Thus include-guard.

set(archdetect_c_code"
/* -----------------------------------ARM ---------------------------------- */

#if defined(__ARM64_ARCH_8__) || defined(__aarch64__) || defined(__ARMv8__) || defined(__ARMv8_A__) || defined(_M_ARM64)
    #if defined(__thumb__) || defined(_M_ARMT)
        #error cmake_ARCH armthumbv3
    #elif
        #error cmake_ARCH armv8
    #endif

#elif defined(__ARM_ARCH_7__) || defined(__ARM_ARCH_7A__) || defined(__ARM_ARCH_7R__) || defined(__ARM_ARCH_7M__  ) || defined(__ARM_ARCH_7F__) || defined(__ARM_ARCH_7S__) || (defined(__TARGET_ARCH_ARM) && __TARGET_ARCH_ARM-0 >= 7)
    #if defined(__thumb__) || defined(__thumb2__) || defined(_M_ARMT)
        #error cmake_ARCH armthumbv2
    #elif
        #error cmake_ARCH armv7
    #endif

#elif defined(__ARM_ARCH_6__) ||  defined(__ARM_ARCH_6J__) || defined(__ARM_ARCH_6K__) || defined(__ARM_ARCH_6Z__) || defined(__ARM_ARCH_6ZK__) || defined(__ARM_ARCH_6T2__) || defined(__ARM_ARCH_6M__) || (defined(__TARGET_ARCH_ARM) && __TARGET_ARCH_ARM-0 >= 6)
    #if defined(__thumb__) || defined(_M_ARMT)
        #error cmake_ARCH armthumbv1
    #elif
        #error cmake_ARCH armv6
    #endif

#elif defined(__ARM_ARCH_5__) || defined(__ARM_ARCH_5E__ ) || defined(__ARM_ARCH_5T__) || defined(__ARM_ARCH_5TE__) || defined(__ARM_ARCH_5TEJ__) || defined(__MARM_ARMV5__) || (defined(__TARGET_ARCH_ARM) && __TARGET_ARCH_ARM-0 >= 5)
    #error cmake_ARCH armv5

#elif defined(__ARM_ARCH_4__) ||  defined(__ARM_ARCH_4T__) || defined(__MARM_ARMV4__)
    #error cmake_ARCH armv4

#elif defined(__TARGET_ARCH_ARM)
    #error cmake_ARCH armv ## __TARGET_ARCH_ARM

#elif defined(__arm__) || defined(_M_ARM)
    #error arm

/* ----------------------------------- x86 ---------------------------------- */

#elif defined(__i386) || defined(__i386__) || defined(_M_IX86) || defined(i386)  || defined(_M_IX86) || defined(_X86_) || defined(__THW_INTEL)
    #error cmake_ARCH x86

#elif defined(__x86_64) || defined(__x86_64__) || defined(__amd64) || defined(_M_X64) || defined(__amd64__) || defined(_M_AMD64)
    #error cmake_ARCH amd64

// AMD wrote the original x86_64 bit spec after cloning x86 for years.
// Then, in 2003, they renamed it to amd64. So basically x64 ~ amd64 ~ x86_64
// But the trend seems to be to tidy this up and go with the most officialish, amd64.
// Think of it this way, AMD was in fact the one pushing the architecture forwards at times.
// Even if Intel invented it. Therefore, I say, let's stick with amd64 and be over this mess already.

/* ------------------------------- Itanium ---------------------------------- */

#elif defined(__ia64) || defined(__ia64__) || defined(_M_IA64) || defined(_IA64) || defined(__IA64__) || defined(__itanium__)
    /* 32-bit mode on Itanium */
    #if !defined(__LP64__) && !defined(_M_X64) /* <-- Visual Studio macro (it USED to support Itanium) */
        #error cmake_ARCH ia32
    #else
        #error cmake_ARCH ia64
    #endif

/* ------------------------------ Power PC  --------------------------------- */

#elif defined(__ppc__) || defined(__ppc) || defined(__PPC__)  || defined(__PPC) || defined(__powerpc__) || defined(__powerpc) || defined(_ARCH_COM) || defined(_ARCH_PWR) || defined(_ARCH_PPC) || defined(_M_MPPC) || defined(_M_PPC) || defined(__POWERPC__)
    #if defined(__ppc64__) || defined(__PPC64__) || defined(__powerpc64__) || defined(__64BIT__)
        #error cmake_ARCH ppc64
    #else
        #error cmake_ARCH ppc32
    #endif

/* ---------------------------------- Sparc --------------------------------- */

#elif defined(__sparc) && !defined(__arch64__) || defined(__sparcv8)
    #error cmake_ARCH sparc32

#elif defined(__sparc__) && defined(__arch64__) || defined (__sparcv9)
    #error cmake_ARCH sparc64

/* --------------------------- IBM Mainframes ------------------------------- */

/* 64 bit IBM z/Architecture */
#elif defined(__s390x__) || defined(__s390X__) || defined(__zarch__) || defined(__SYSC_ZARCH__)
    #error cmake_ARCH zArch

/* Older 32 bit IBM System/390, maybe useful for Hercules emulation? */
#elif defined(__s390__)
    #error cmake_ARCH s390

/* --------------------------- Hitachi SuperH ------------------------------- */

#elif defined(__sh1__)
    #error cmake_ARCH superH1
#elif defined(__sh2__)
    #error cmake_ARCH superH2
#elif defined(__sh3__) || defined(__SH3__)
    #error cmake_ARCH superH3
#elif defined(__SH4__)
    #error cmake_ARCH superH4
#elif defined(__SH5__)
    #error cmake_ARCH superH5
#elif defined(__sh__) // not sure which version...unlikely?
    #error cmake_ARCH superH

/* -------------------------- Analog Devices --------------------------------- */

#elif defined(__bfin) || defined(__BFIN__)
    #error cmake_ARCH blackfin
#elif defined(__ADSPTS101__)
    #error cmake_ARCH adps-ts101
#elif defined(__ADSPTS201__)
    #error cmake_ARCH adps-ts201
#elif defined(__ADSPTS202__)
    #error cmake_ARCH adps-ts202
#elif defined(__ADSPTS203__)
    #error cmake_ARCH adps-ts203
#elif defined(__ADSPTS20x__) // this will be true for any of the above but we use as  catchall
    #error cmake_ARCH adps-ts20x

/* ------------------------------- Alpha ------------------------------------- */

#elif defined(__alpha_ev4__)
    #error cmake_ARCH alpha_ev4
#elif defined(__alpha_ev5__)
    #error cmake_ARCH alpha_ev5
#elif defined(__alpha_ev6__)
    #error cmake_ARCH alpha_ev6
#elif defined(__alpha__) || defined(__alpha) || defined(_M_ALPHA)
    #error cmake_ARCH alpha

/* ------------------------- Texas Instruments ------------------------------ */

#elif defined(_TMS320C6200)
    #error cmake_ARCH c6200
#elif defined(_TMS320C6400)
    #error cmake_ARCH c6400
#elif defined(_TMS320C6400_PLUS)
    #error cmake_ARCH c6400+
#elif defined(_TMS320C6600)
    #error cmake_ARCH c6600
#elif defined(_TMS320C6700)
    #error cmake_ARCH c6700
#elif defined(_TMS320C6700_PLUS)
    #error cmake_ARCH c6700+
#elif defined(_TMS320C6740)
    #error cmake_ARCH c6740
#elif defined(__TMS470__)
    #error cmake_ARCH tms470

/* -------------------------- Hewlett Packard ------------------------------ */

#elif defined(_PA_RISC1_0)
    #error cmake_ARCH parisc1_0

#elif defined(_PA_RISC1_1) || defined(__HPPA11__) || defined(__PA7100__)
    #error cmake_ARCH parisc1_1

#elif defined(_PA_RISC2_0) || defined(__RISC2_0__) || defined(__HPPA20__) || defined(__PA8000__)
    #error cmake_ARCH parisc2_0

#elif defined(__hppa__) || defined(__HPPA__) || defined(__hppa)
    #error cmake_ARCH parisc

/* ---------------------------- Adapteva --------------------------------- */

#elif defined(__epiphany__)
    #error cmake_ARCH ephiphany

/* ------------------------------ MIPS  ---------------------------------- */

#elif defined(_MIPS_ISA_MIPS1) || (__mips == 1)
    #error cmake_ARCH mips1
#elif defined(_MIPS_ISA_MIPS2) || defined(__MIPS_ISA2__) || (__mips == 2)
    #error cmake_ARCH mips2
#elif defined(_MIPS_ISA_MIPS3) || defined(__MIPS_ISA3__) || (__mips == 3)
    #error cmake_ARCH mips3
#elif defined(_MIPS_ISA_MIPS4) || defined(__MIPS_ISA4__) || (__mips == 4)
    #error cmake_ARCH mips4
#elif defined(__MIPS__) || defined(__mips__) || defined(mips) || defined(__mips)
    #error cmake_ARCH mips

/* ---------------------------- Motorola  -------------------------------- */

#elif defined(__mc68000__) || defined(__MC68000__)
    #error cmake_ARCH mc68000
#elif defined(__mc68010__)
    #error cmake_ARCH mc68010
#elif defined(__mc68020__) || defined(__MC68020__)
    #error cmake_ARCH mc68020
#elif defined(__mc68030__) || defined(__MC68030__)
    #error cmake_ARCH mc68030
#elif defined(__mc68040__) || defined(__MC68040__)
    #error cmake_ARCH mc68040
#elif defined(__mc68060__) || defined(__MC68060__)
    #error cmake_ARCH mc68060
#elif defined(__m68k__) || defined(M68000) || defined(__MC68K__)
    #error cmake_ARCH mc68

#endif
"
)


if(APPLE AND CMAKE_OSX_ARCHITECTURES)
    # On OS X we use CMAKE_OSX_ARCHITECTURES *if* it was set
    # First let's normalize the order of the values

    # Note that it's not possible to compile PowerPC applications if you are using
    # the OS X SDK version 10.6 or later - you'll need 10.4/10.5 for that, so we
    # disable it by default
    # See this page for more information:
    # http://stackoverflow.com/questions/5333490/how-can-we-restore-ppc-ppc64-as-well-as-full-10-4-10-5-sdk-support-to-xcode-4

    # Architecture defaults to x86  or ppc on OS X 10.5 and earlier, depending on the CPU type detected at runtime.
    # On OS X 10.6+ the default is x86_64 if the CPU supports it, x86 otherwise.

    foreach(osx_arch ${CMAKE_OSX_ARCHITECTURES})
        if("${osx_arch}" STREQUAL"ppc" AND ppc_support)
            set(osx_arch_ppc TRUE)
        elseif("${osx_arch}" STREQUAL"i386")
            set(osx_arch_i386 TRUE)
        elseif("${osx_arch}" STREQUAL"x86_64")
            set(osx_arch_x86_64 TRUE)
        elseif("${osx_arch}" STREQUAL"ppc64" AND ppc_support)
            set(osx_arch_ppc64 TRUE)
        else()
            message(FATAL_ERROR"Invalid OS X arch name: ${osx_arch}")
        endif()
    endforeach()

    # Now add all the architectures in our normalized order
    if(osx_arch_ppc)
        list(APPEND ARCH ppc32)
    endif()

    if(osx_arch_i386)
        list(APPEND ARCH x86)
    endif()

    if(osx_arch_x86_64)
        list(APPEND ARCH x64)
    endif()

    if(osx_arch_ppc64)
        list(APPEND ARCH ppc64)
    endif()
else()
    file(WRITE"${CMAKE_BINARY_DIR}/CMakeFiles/detect_cpu_arch.c""${archdetect_c_code}")


    # Detect the architecture in a rather creative way...
    # This compiles a small C program which is a series of ifdefs that selects a
    # particular #error preprocessor directive whose message string contains the
    # target architecture. The program will always fail to compile (both because
    # file is not a valid C program, and obviously because of the presence of the
    # #error preprocessor directives... but by exploiting the preprocessor in this
    # way, we can detect the correct target architecture even when cross-compiling,
    # since the program itself never needs to be run (only the compiler/preprocessor)
    try_run(
        run_result_unused
        compile_result_unused
       "${CMAKE_BINARY_DIR}/CMakeFiles"
       "${CMAKE_BINARY_DIR}/CMakeFiles/detect_cpu_arch.c"
        COMPILE_OUTPUT_VARIABLE ARCH
        CMAKE_FLAGS CMAKE_OSX_ARCHITECTURES=${CMAKE_OSX_ARCHITECTURES}
    )

    # Parse the architecture name from the compiler output
    string(REGEX MATCH"cmake_ARCH ([a-zA-Z0-9_]+)" ARCH"${ARCH}")

    # Get rid of the value marker leaving just the architecture name
    string(REPLACE"cmake_ARCH""" ARCH"${ARCH}")

    # If we are compiling with an unknown architecture this variable should
    # already be set to"unknown" but in the case that it's empty (i.e. due
    # to a typo in the code), then set it to unknown
    if (NOT ARCH)
        set(ARCH unknown)
    endif()
endif()


set(CMAKE_CPU_ARCHITECTURES"${ARCH}")

endif(NOT detect_cpu_architectures_INC) # include guard


我自己也遇到了类似的问题,并发现了CMAKE_VS_PLATFORM_NAME,它是根据生成器计算的,我认为值得一提。

我已经使用cmake 3.16.1(根据文档从3.1.3开始可用)进行了尝试,它使用了-GVisual Studio 15 2017-GVisual Studio 15 2017 Win64-GVisual Studio 15 2017 -Ax64,并将其设置为:Win32x64x64。所以对于这种情况,我认为例如对Win32进行检查应该可以

1
2
3
if ("${CMAKE_VS_PLATFORM_NAME}" STREQUAL"Win32")
    ...
endif ()

这个例子在cmake 3.17.0上为我工作:

1
2
3
4
5
6
7
if("${CMAKE_GENERATOR_PLATFORM}" STREQUAL"x64")
  ....
endif()

if("${CMAKE_GENERATOR_PLATFORM}" STREQUAL"Win32")
  ...
endif()

可选的解决方案是基于使用的编译器名称构建条件。

1
if(CMAKE_CXX_COMPILER_ID MATCHES"MSVC")