-1

I currently have a basic Cmake file that sets certain library directories. I would like to conditionally intitalise based on the target generator -- in my case the generator determines which base directories to use (64-bit visual studio generator vs a regular visual studio generator).

My CMakeLists file looks as follows:

PROJECT(STAT_AUTH)
CMAKE_MINIMUM_REQUIRED(VERSION 2.8)

SET(BOOST_DIR "c:\\dev_32\\Boost" CACHE PATH "The Boost Directory Path")
SET(PROTOBUF_DIR "c:\\dev_32\\Protobuf" CACHE PATH "The Protobuf directory Path")
SET(OPENSSL_DIR "c:\\dev_32\\OpenSSL" CACHE PATH "The OpenSSL Directory Path"

How do I conditionally initialise the variables so they get set to 64-bit versions when I generate to 64-bit generators. The default setting should show up in the Cmake Gui / ccmake before I choose the "generate" option.

Hassan Syed
  • 20,075
  • 11
  • 87
  • 171

2 Answers2

4

Try:

if(CMAKE_SIZEOF_VOID_P MATCHES 4)
  SET(BOOST_DIR "c:\\dev_32\\Boost" CACHE PATH "The Boost Directory Path")
  SET(PROTOBUF_DIR "c:\\dev_32\\Protobuf" CACHE PATH "The Protobuf directory Path")
  SET(OPENSSL_DIR "c:\\dev_32\\OpenSSL" CACHE PATH "The OpenSSL Directory Path"
else()
  SET(BOOST_DIR "c:\\dev_64\\Boost" CACHE PATH "The Boost Directory Path")
  SET(PROTOBUF_DIR "c:\\dev_64\\Protobuf" CACHE PATH "The Protobuf directory Path")
  SET(OPENSSL_DIR "c:\\dev_64\\OpenSSL" CACHE PATH "The OpenSSL Directory Path"
endif()
tibur
  • 11,531
  • 2
  • 37
  • 39
  • Thanks I am heading home now. But I will try it tommorow. I was trying if(0) or if(off), but it seems the cmake parser doesn't respect conditionals. Ideally I would like to do something like(psuedocode): `if(Generator is Visual studio - 64) //...// endif()`. So anyway what is CMAKE_SIZEOF_VOID_P ? – Hassan Syed Nov 09 '10 at 19:25
  • 1
    `CMAKE_SIZEOF_VOID_P`: http://cmake.org/cmake/help/cmake-2-8-docs.html#variable:CMAKE_SIZEOF_VOID_P . "Size of a void pointer. This is set to the size of a pointer on the machine, and is determined by a try compile. If a 64 bit size is found, then the library search path is modified to look for 64 bit libraries first." – Jack Kelly Nov 09 '10 at 19:38
  • 1
    Be careful with `SET(... CACHE ...)` commands: the data is written only the first time your run cmake. You need to clear the cache if you want to test again your default values. – tibur Nov 09 '10 at 20:05
  • Aah, cunning test, but since windows 64 has WoW64 I will be cross compiling the 32-bit code. Therefore, the test will be an inaccurate representation. Whereas if I chose the 64-bit visual studio generator I explicetly specify the architecture to target. – Hassan Syed Nov 09 '10 at 21:28
  • 2
    When you choose a 32 bits Visual Studio Generator, CMAKE_SIZEOF_VOID_P value is 4. If you choose a 64 bits Visual Studio Generator, CMAKE_SIZEOF_VOID_P equals 8. It is not testing the actual OS value, but the generator itself. – tibur Nov 10 '10 at 00:09
  • (+1) aah yes, that is probable. All these levels of indirection get me all muddled up. I shall test and accept tommorow :D But to keep it interesting what about IA64 ? :D (which cmake doesn't target, but for the sake of argument). – Hassan Syed Nov 10 '10 at 00:34
1

For Windows the following syntax is apt. CMAKE_CL_64 defines the x86_64 compiler specifically.

if(MSVC)
    if(CMAKE_CL_64)
        SET(BOOST_DIR "c:\\dev_64\\Boost" CACHE PATH "The Boost Directory Path")
        SET(PROTOBUF_DIR "c:\\dev_64\\Protobuf" CACHE PATH "The Protobuf directory Path")
        SET(OPENSSL_DIR "c:\\dev_64\\OpenSSL" CACHE PATH "The OpenSSL Directory Path")
        SET(DEPLOY_DIR "c:\\root_64" CACHE PATH "The Deploy Path for the components built" )
    else()
        SET(BOOST_DIR "c:\\dev_32\\Boost" CACHE PATH "The Boost Directory Path")
        SET(PROTOBUF_DIR "c:\\dev_32\\Protobuf" CACHE PATH "The Protobuf directory Path")
        SET(OPENSSL_DIR "c:\\dev_32\\OpenSSL" CACHE PATH "The OpenSSL Directory Path")
        SET(DEPLOY_DIR "c:\\root_32" CACHE PATH 
            "The Deploy Path for the components built" )
    endif()
endif()
Hassan Syed
  • 20,075
  • 11
  • 87
  • 171