将预处理器定义添加到cmake外部项目

时间:2015-09-29 15:26:28

标签: c++ cmake xerces xerces-c

我想使用cmake ExternalProject在Windows上构建xerces-c作为静态库。但是,要使其工作,我需要将XERCES_STATIC_LIBRARY预处理器定义添加到项目中。在linux上,似乎我可以使用补丁文件轻松地完成它,但我不确定如何在Windows上执行此操作。

这是我目前的CMakeLists.txt:

cmake_minimum_required(VERSION 3.0.0 FATAL_ERROR)
PROJECT(xercesc)
SET(TARGET_NAME xercesc)

IF(WIN32)
    # find the msvc version
    if(${MSVC_VERSION} EQUAL 1400)
        SET(VC_PROJ_DIR VC8)
    elseif(${MSVC_VERSION} EQUAL 1500)
        SET(VC_PROJ_DIR VC9)
    elseif(${MSVC_VERSION} EQUAL 1600)
        SET(VC_PROJ_DIR VC10)
    elseif(${MSVC_VERSION} EQUAL 1700)
        SET(VC_PROJ_DIR VC11)
    elseif(${MSVC_VERSION} EQUAL 1800)
        SET(VC_PROJ_DIR VC12)
    endif(${MSVC_VERSION} EQUAL 1400)

    include(ExternalProject)
    ExternalProject_Add(
      ${TARGET_NAME}
      URL ${CMAKE_CURRENT_SOURCE_DIR}/xerces-c-${XERCESC_VERSION}.tar.gz
      DOWNLOAD_DIR ${CMAKE_CURRENT_SOURCE_DIR}/xercesc
      SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/xercesc
      CONFIGURE_COMMAND ""
      BUILD_COMMAND Devenv "${CMAKE_CURRENT_SOURCE_DIR}/xercesc/projects/Win32/${VC_PROJ_DIR}/xerces-all/xerces-all.sln" /build Release
      INSTALL_COMMAND ""
      BUILD_IN_SOURCE 1
    )
ENDIF(WIN32)

1 个答案:

答案 0 :(得分:2)

Devenv切换到msbuild命令行秘密酱。它功能更强大,您可以使用/property:DefineConstants=XERCES_STATIC_LIBRARY定义常量。请注意:它会吹走项目文件中定义的其他常量 ...但是你在cmake构建中的.vcxproj文件中定义常量是不够傻的,不是吗? / p>

无论如何,这里是在Windows上构建xerces-c的完整解决方案。请注意,对于静态构建,它们在其配置名称中使用(God forsaken)空格。最简单的解决方法是用下划线替换它们并继续生活。我用了the find and replace text (FART) tool

另外,我正在进行DLL运行时库构建,因此我替换了所有/ MT选项。

cmake_minimum_required(VERSION 3.0.0 FATAL_ERROR)
PROJECT(xercesc)
SET(TARGET_NAME xercesc)

IF(WIN32)
    # find the msvc version
    if(${MSVC_VERSION} EQUAL 1400)
        SET(VC_PROJ_DIR VC8)
    elseif(${MSVC_VERSION} EQUAL 1500)
        SET(VC_PROJ_DIR VC9)
    elseif(${MSVC_VERSION} EQUAL 1600)
        SET(VC_PROJ_DIR VC10)
    elseif(${MSVC_VERSION} EQUAL 1700)
        SET(VC_PROJ_DIR VC11)
    elseif(${MSVC_VERSION} EQUAL 1800)
        SET(VC_PROJ_DIR VC12)
    endif(${MSVC_VERSION} EQUAL 1400)

    # Use FART to replace spaces in the project files, ugh. FART requires windows style slashes.
    SET(XERCES_SOLUTION_DIR ${CMAKE_CURRENT_SOURCE_DIR}/xercesc/projects/Win32/${VC_PROJ_DIR}/xerces-all)   
    SET(XERCES_SOLUTION "${XERCES_SOLUTION_DIR}/xerces-all.sln")
    SET(XERCES_SOLUTIONS "${XERCES_SOLUTION_DIR}/*.sln")
    SET(XERCES_PROJECTS "${XERCES_SOLUTION_DIR}/*.vcxproj")
    SET(XERCES_LIB_PROJECT "${XERCES_SOLUTION_DIR}/XercesLib/XercesLib.vcxproj")
    STRING(REGEX REPLACE "/" "\\\\" XERCES_SOLUTIONS_WIN ${XERCES_SOLUTIONS})
    STRING(REGEX REPLACE "/" "\\\\" XERCES_PROJECTS_WIN ${XERCES_PROJECTS})

    include(ExternalProject)
    ExternalProject_Add(
        ${TARGET_NAME}
        DEPENDS fart
        URL ${CMAKE_CURRENT_SOURCE_DIR}/xerces-c-${XERCESC_VERSION}.tar.gz
        DOWNLOAD_DIR ${CMAKE_CURRENT_SOURCE_DIR}/xercesc
        SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/xercesc
        PATCH_COMMAND ${FART_DIR}/bin/$<CONFIGURATION>/fart.exe -v -r --svn -- ${XERCES_SOLUTIONS_WIN},${XERCES_PROJECTS_WIN} "Static " Static_ 
        COMMAND ${FART_DIR}/bin/$<CONFIGURATION>/fart.exe -v -r --svn -- ${XERCES_SOLUTIONS_WIN},${XERCES_PROJECTS_WIN} <RuntimeLibrary>MultiThreaded</RuntimeLibrary> <RuntimeLibrary>MultiThreadedDLL</RuntimeLibrary>
        COMMAND ${FART_DIR}/bin/$<CONFIGURATION>/fart.exe -v -r --svn -- ${XERCES_SOLUTIONS_WIN},${XERCES_PROJECTS_WIN} <RuntimeLibrary>MultiThreadedDebug</RuntimeLibrary> <RuntimeLibrary>MultiThreadedDebugDLL</RuntimeLibrary>
        CONFIGURE_COMMAND ""
        BUILD_COMMAND msbuild ${XERCES_LIB_PROJECT} /m /property:Configuration=Static_Release,Platform=x64 /property:DefineConstants=XERCES_STATIC_LIBRARY
        COMMAND msbuild ${XERCES_LIB_PROJECT} /m /property:Configuration=Static_Debug,Platform=x64 /property:DefineConstants=XERCES_STATIC_LIBRARY /property:DefineDebug=true
        INSTALL_COMMAND ""
        BUILD_IN_SOURCE 1
    )
    SET(LIBRARIES debug ${CMAKE_CURRENT_SOURCE_DIR}/xercesc/Build/Win64/${VC_PROJ_DIR}/Static_Debug/xerces-c_static_3D.lib)
    SET(LIBRARIES ${LIBRARIES} optimized ${CMAKE_CURRENT_SOURCE_DIR}/xercesc/Build/Win64/${VC_PROJ_DIR}/Static_Release/xerces-c_static_3.lib)
    SET(INCLUDE_DIRS ${CMAKE_CURRENT_SOURCE_DIR}/xercesc/src/)

ELSE(WIN32)