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

add preprocessor define to cmake external project

我想使用 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)

Devenv 切换到 msbuild 命令行秘诀。它更强大,您可以使用 /property:DefineConstants=XERCES_STATIC_LIBRARY 定义常量。请注意:它会清除项目文件 中定义的其他常量 ... 但是您不会傻到在 cmake 构建的 .vcxproj 文件中定义常量,对吗?

所以无论如何,这是在 windows 上构建 xerces-c 的完整解决方案。请注意,对于静态构建,他们在其配置名称中使用(上帝遗弃的)空格。最简单的解决方法是用下划线替换它们并继续使用。我用了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)