CMAKE:根据发生器类型有条件地初始化缓存变量

时间:2010-11-09 18:45:07

标签: build-process cmake configuration-management

我目前有一个基本的Cmake文件,用于设置某些库目录。我想基于目标生成器有条件地进行初始化 - 在我的情况下,生成器确定使用哪些基本目录(64位可视工作室生成器与常规可视工作室生成器)。

我的CMakeLists文件如下所示:

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"

如何有条件地初始化变量,以便在生成64位生成器时将它们设置为64位版本。在选择“生成”选项之前,默认设置应显示在Cmake Gui / ccmake中。

2 个答案:

答案 0 :(得分:4)

尝试:

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()

答案 1 :(得分:1)

对于Windows,以下语法是apt。 CMAKE_CL_64专门定义了x86_64编译器。

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()