2015-09-29 89 views
0

我想在Windows上使用cmake ExternalProject將xerces-c構建爲靜態庫。然而,爲了這個工作,我需要將XERCES_STATIC_LIBRARY預處理器定義添加到項目中。在Linux上,似乎我可以用補丁文件輕鬆完成,但我不確定如何在Windows上執行此操作。將預處理器定義添加到cmake外部項目

這裏是我當前的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) 
+0

'ExternalProject_Add'不提供命令行上面的功能。由於你有VS解決方案,它已經*配置*,所以編譯器定義只能通過補丁來添加。 – Tsyvarev

回答

1

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) 
相關問題