From ae55d2d526908636edd75e14a24e7e271c602bd8 Mon Sep 17 00:00:00 2001 From: Ben Rosser Date: Sun, 26 Jun 2016 19:48:55 -0400 Subject: [PATCH] Support linking against an external tinyxml if EXTERNAL_TINYXML is set As best as I can tell, the copy of tinyxml dfhack uses is unmodified from whenever it was first bundled. This commit adds an option to CMake, EXTERNAL_TINYXML, that if set to ON, will attempt to link against a system tinyxml instead of using the dfhack-bundled one. It defaults to OFF, so there is no change in default behavior. The DFHACK_TINYXML variable is then set to either "tinyxml" or "dfhack-tinyxml" so the library (and any plugins that need updating) can link against one or the other. The FindTinyXML.cmake script was taken from https://github.com/ros/cmake_modules (licensed under the 3-clause BSD license). Add license text to new CMake file. --- CMake/Modules/FindTinyXML.cmake | 107 ++++++++++++++++++++++++++++++++ CMakeLists.txt | 17 ++++- depends/CMakeLists.txt | 7 ++- depends/tinyxml/CMakeLists.txt | 8 ++- library/CMakeLists.txt | 6 +- 5 files changed, 137 insertions(+), 8 deletions(-) create mode 100644 CMake/Modules/FindTinyXML.cmake diff --git a/CMake/Modules/FindTinyXML.cmake b/CMake/Modules/FindTinyXML.cmake new file mode 100644 index 000000000..b0466fbd5 --- /dev/null +++ b/CMake/Modules/FindTinyXML.cmake @@ -0,0 +1,107 @@ +# Sourced from: +# https://raw.githubusercontent.com/ros/cmake_modules/0.4-devel/cmake/Modules/FindTinyXML.cmake +# under the following license: https://github.com/ros/cmake_modules/blob/0.4-devel/LICENSE, +# reproduced here: + +# Copyright (c) 2013, Open Source Robotics Foundation +# All rights reserved. + +# Redistribution and use in source and binary forms, with or without modification, +# are permitted provided that the following conditions are met: + +# Redistributions of source code must retain the above copyright notice, this +# list of conditions and the following disclaimer. + +# Redistributions in binary form must reproduce the above copyright notice, this +# list of conditions and the following disclaimer in the documentation and/or +# other materials provided with the distribution. + +# Neither the name of the {organization} nor the names of its +# contributors may be used to endorse or promote products derived from +# this software without specific prior written permission. + +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND +# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR +# ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +# ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +################################################################################################## +# +# CMake script for finding TinyXML. +# +# Input variables: +# +# - TinyXML_ROOT_DIR (optional): When specified, header files and libraries will be searched for in +# ${TinyXML_ROOT_DIR}/include +# ${TinyXML_ROOT_DIR}/libs +# respectively, and the default CMake search order will be ignored. When unspecified, the default +# CMake search order is used. +# This variable can be specified either as a CMake or environment variable. If both are set, +# preference is given to the CMake variable. +# Use this variable for finding packages installed in a nonstandard location, or for enforcing +# that one of multiple package installations is picked up. +# +# +# Cache variables (not intended to be used in CMakeLists.txt files) +# +# - TinyXML_INCLUDE_DIR: Absolute path to package headers. +# - TinyXML_LIBRARY: Absolute path to library. +# +# +# Output variables: +# +# - TinyXML_FOUND: Boolean that indicates if the package was found +# - TinyXML_INCLUDE_DIRS: Paths to the necessary header files +# - TinyXML_LIBRARIES: Package libraries +# +# +# Example usage: +# +# find_package(TinyXML) +# if(NOT TinyXML_FOUND) +# # Error handling +# endif() +# ... +# include_directories(${TinyXML_INCLUDE_DIRS} ...) +# ... +# target_link_libraries(my_target ${TinyXML_LIBRARIES}) +# +################################################################################################## + +# Get package location hint from environment variable (if any) +if(NOT TinyXML_ROOT_DIR AND DEFINED ENV{TinyXML_ROOT_DIR}) + set(TinyXML_ROOT_DIR "$ENV{TinyXML_ROOT_DIR}" CACHE PATH + "TinyXML base directory location (optional, used for nonstandard installation paths)") +endif() + +# Search path for nonstandard package locations +if(TinyXML_ROOT_DIR) + set(TinyXML_INCLUDE_PATH PATHS "${TinyXML_ROOT_DIR}/include" NO_DEFAULT_PATH) + set(TinyXML_LIBRARY_PATH PATHS "${TinyXML_ROOT_DIR}/lib" NO_DEFAULT_PATH) +endif() + +# Find headers and libraries +find_path(TinyXML_INCLUDE_DIR NAMES tinyxml.h PATH_SUFFIXES "tinyxml" ${TinyXML_INCLUDE_PATH}) +find_library(TinyXML_LIBRARY NAMES tinyxml PATH_SUFFIXES "tinyxml" ${TinyXML_LIBRARY_PATH}) + +mark_as_advanced(TinyXML_INCLUDE_DIR + TinyXML_LIBRARY) + +# Output variables generation +include(FindPackageHandleStandardArgs) +find_package_handle_standard_args(TinyXML DEFAULT_MSG TinyXML_LIBRARY + TinyXML_INCLUDE_DIR) + +set(TinyXML_FOUND ${TINYXML_FOUND}) # Enforce case-correctness: Set appropriately cased variable... +unset(TINYXML_FOUND) # ...and unset uppercase variable generated by find_package_handle_standard_args + +if(TinyXML_FOUND) + set(TinyXML_INCLUDE_DIRS ${TinyXML_INCLUDE_DIR}) + set(TinyXML_LIBRARIES ${TinyXML_LIBRARY}) +endif() diff --git a/CMakeLists.txt b/CMakeLists.txt index 4bd89edce..533fba9ce 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -186,7 +186,22 @@ include_directories(depends/protobuf) include_directories(depends/lua/include) include_directories(depends/md5) include_directories(depends/jsoncpp) -include_directories(depends/tinyxml) + +# Support linking against external tinyxml +# If we find an external tinyxml, set the DFHACK_TINYXML variable to "tinyxml" +# Otherwise, set it to "dfhack-tinyxml" +option(EXTERNAL_TINYXML "Choose to link against external TinyXML" OFF) +if(EXTERNAL_TINYXML) + find_package(TinyXML REQUIRED) + if(NOT TinyXML_FOUND) + message(SEND_ERROR "Could not find an external TinyXML, consider setting EXTERNAL_TINYXML to OFF.") + endif() + SET(DFHACK_TINYXML "tinyxml") +else() + include_directories(depends/tinyxml) + SET(DFHACK_TINYXML "dfhack-tinyxml") +endif() + include_directories(depends/tthread) include_directories(${ZLIB_INCLUDE_DIRS}) include_directories(depends/clsocket/src) diff --git a/depends/CMakeLists.txt b/depends/CMakeLists.txt index bf0345bfb..d8442b12a 100644 --- a/depends/CMakeLists.txt +++ b/depends/CMakeLists.txt @@ -2,7 +2,12 @@ add_subdirectory(lua) add_subdirectory(md5) add_subdirectory(protobuf) -add_subdirectory(tinyxml) + +# Don't build tinyxml if it's being externally linked against. +if(NOT TinyXML_FOUND) + add_subdirectory(tinyxml) +endif() + add_subdirectory(tthread) add_subdirectory(jsoncpp) # build clsocket static and only as a dependency. Setting those options here overrides its own default settings. diff --git a/depends/tinyxml/CMakeLists.txt b/depends/tinyxml/CMakeLists.txt index 7d924924f..313837cac 100644 --- a/depends/tinyxml/CMakeLists.txt +++ b/depends/tinyxml/CMakeLists.txt @@ -1,3 +1,5 @@ -project(dfhack-tinyxml) -ADD_LIBRARY(dfhack-tinyxml STATIC EXCLUDE_FROM_ALL tinystr.cpp tinyxml.cpp tinyxmlerror.cpp tinyxmlparser.cpp) -IDE_FOLDER(dfhack-tinyxml "Depends") \ No newline at end of file +if(NOT TinyXML_FOUND) + project(dfhack-tinyxml) + ADD_LIBRARY(dfhack-tinyxml STATIC EXCLUDE_FROM_ALL tinystr.cpp tinyxml.cpp tinyxmlerror.cpp tinyxmlparser.cpp) + IDE_FOLDER(dfhack-tinyxml "Depends") +endif() diff --git a/library/CMakeLists.txt b/library/CMakeLists.txt index 3a6cd4dc8..346776865 100644 --- a/library/CMakeLists.txt +++ b/library/CMakeLists.txt @@ -269,12 +269,12 @@ IF(UNIX) ENDIF() IF(APPLE) - SET(PROJECT_LIBS dl dfhack-md5 dfhack-tinyxml dfhack-tinythread) + SET(PROJECT_LIBS dl dfhack-md5 ${DFHACK_TINYXML} dfhack-tinythread) ELSEIF(UNIX) - SET(PROJECT_LIBS rt dl dfhack-md5 dfhack-tinyxml dfhack-tinythread) + SET(PROJECT_LIBS rt dl dfhack-md5 ${DFHACK_TINYXML} dfhack-tinythread) ELSE(WIN32) #FIXME: do we really need psapi? - SET(PROJECT_LIBS psapi dfhack-md5 dfhack-tinyxml dfhack-tinythread) + SET(PROJECT_LIBS psapi dfhack-md5 ${DFHACK_TINYXML} dfhack-tinythread) ENDIF() ADD_LIBRARY(dfhack-version STATIC DFHackVersion.cpp)