Option to force either 32-bit or 64-bit build with cmake

For Visual Studio and per https://cmake.org/cmake/help/latest/variable/CMAKE_GENERATOR_PLATFORM.html

For Visual Studio Generators with VS 2005 and above this specifies the target architecture.

cmake . -DCMAKE_GENERATOR_PLATFORM=x64

thanks for all your inputs, but on my side i've finally chosen the -m32 hack with cmake

# cmake windows 32 bits mode bug: 32 bits link mode must be explicit (otherwise cmake will always guess a 64 bits target)
#  1- run "vcbarsall.bat x86" to setup Visual Studio build profile
#  2- "set cflags=-m32" and "set cxxflags=-m32"
#  3- let the cmake plugin "automatically" guess the target platform

TL;DR

Use toolchain

In depth

  1. an option (-DUSE32bit=true)

This is not scalable I guess. So what if you want to build N projects? You have to add N options.

  1. build types (-DCMAKE_BUILD_TYPE=release32)

This may work well. But in my opinion you're mixing unrelated stuff. Also I'm sure you have to adapt find_package behaviour by setting some *_ROOT CMake variables. It's not possible to do it with CMAKE_BUILD_TYPE (at least, again, in a scalable fashion).

  1. a tool chain (-DCMAKE_TOOLCHAIN_FILE=64bit.toolchain)

The best variant. If you want to build two projects - just use same toolchain:

cmake -Hproj-1 -B_builds/proj-1 -DCMAKE_TOOLCHAIN_FILE=/.../64bit.toolchain
cmake -Hproj-2 -B_builds/proj-2 -DCMAKE_TOOLCHAIN_FILE=/.../64bit.toolchain

If you want to build your 3rd party ExternalProject_Add with 64 bit architecture - just pass toolchain to CMAKE_ARGS:

ExternalProject_Add(
    ...
    CMAKE_ARGS ... -DCMAKE_TOOLCHAIN_FILE=/.../64bit.toolchain
    ...
)

Want to adapt find_package - just add any CMake variables to toolchain file.