From 8ac55a8534b9fc7b9a744e01f789648566fa6cde Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Wolfgang=20St=C3=B6ggl?= Date: Sun, 22 Nov 2020 08:57:07 +0100 Subject: [PATCH] Add find_package(tinyxml2) to CMake builds (#2691) So far, the cmake files of Cppcheck needed to be patched in order to use installed tinyxml2 instead of the bundled version of tinyxml2. - Introduce the CMake option USE_BUNDLED_TINYXML2 with a default value of ON. This preserves the behavior as in the past and uses the bundled version under externals/tinyxml2 by default. - Usage of the installed tinyxml2 version of a system can be enabled now using -DUSE_BUNDLED_TINYXML2=OFF as a cmake parameter. - Some Linux distros do not install tinyxml2*.cmake files, which are required to find tinyxml2 using find_package(). Try first using find_package(tinyxml2 QUIET) and if this fails, try again using find_library(tinyxml2_LIBRARY tinyxml2) --- CMakeLists.txt | 5 ++++- cli/CMakeLists.txt | 17 ++++++++++++++--- cmake/findDependencies.cmake | 13 +++++++++++++ cmake/options.cmake | 1 + cmake/printInfo.cmake | 1 + gui/CMakeLists.txt | 13 +++++++++++-- test/CMakeLists.txt | 13 +++++++++++-- 7 files changed, 55 insertions(+), 8 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index a339447ec..e52750ace 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -33,7 +33,10 @@ if (BUILD_TESTS) enable_testing() endif() -add_subdirectory(externals/tinyxml2) +if(USE_BUNDLED_TINYXML2) + message(STATUS "Using bundled version of tinyxml2") + add_subdirectory(externals/tinyxml2) +endif() add_subdirectory(externals/simplecpp) add_subdirectory(lib) # CppCheck Library add_subdirectory(cli) # Client application diff --git a/cli/CMakeLists.txt b/cli/CMakeLists.txt index c6c055ef9..d8d21aa90 100644 --- a/cli/CMakeLists.txt +++ b/cli/CMakeLists.txt @@ -1,5 +1,7 @@ include_directories(${PROJECT_SOURCE_DIR}/lib/) -include_directories(SYSTEM ${PROJECT_SOURCE_DIR}/externals/tinyxml2/) +if(USE_BUNDLED_TINYXML2) + include_directories(SYSTEM ${PROJECT_SOURCE_DIR}/externals/tinyxml2/) +endif() include_directories(${PROJECT_SOURCE_DIR}/externals/simplecpp/) file(GLOB hdrs "*.h") @@ -7,8 +9,14 @@ file(GLOB srcs "*.cpp") file(GLOB mainfile "main.cpp") list(REMOVE_ITEM srcs ${mainfile}) -add_library(cli_objs OBJECT ${hdrs} ${srcs}) -add_executable(cppcheck ${hdrs} ${mainfile} $ $ $ $) +add_library(cli_objs OBJECT ${hdrs} ${srcs}) + +list(APPEND cppcheck_SOURCES ${hdrs} ${mainfile} $ $ $) +if(USE_BUNDLED_TINYXML2) + list(APPEND cppcheck_SOURCES $) +endif() + +add_executable(cppcheck ${cppcheck_SOURCES}) if (HAVE_RULES) target_link_libraries(cppcheck ${PCRE_LIBRARY}) endif() @@ -18,6 +26,9 @@ endif() if (WIN32 AND NOT BORLAND) target_link_libraries(cppcheck Shlwapi.lib) endif() +if(tinyxml2_FOUND AND NOT USE_BUNDLED_TINYXML2) + target_link_libraries(cppcheck tinyxml2) +endif() install(TARGETS cppcheck RUNTIME DESTINATION ${CMAKE_INSTALL_FULL_BINDIR} diff --git a/cmake/findDependencies.cmake b/cmake/findDependencies.cmake index 18f8499ac..db2608b84 100644 --- a/cmake/findDependencies.cmake +++ b/cmake/findDependencies.cmake @@ -38,3 +38,16 @@ if (NOT USE_MATCHCOMPILER_OPT MATCHES "Off") set(USE_MATCHCOMPILER_OPT "Off") endif() endif() + +if (NOT USE_BUNDLED_TINYXML2) + find_package(tinyxml2 QUIET) + if (NOT tinyxml2_FOUND) + find_library(tinyxml2_LIBRARY tinyxml2) + if (NOT tinyxml2_LIBRARY) + message(FATAL_ERROR "tinyxml2 has not been found") + else() + message(STATUS "tinyxml2_LIBRARY: ${tinyxml2_LIBRARY}") + set(tinyxml2_FOUND 1) + endif() + endif() +endif() diff --git a/cmake/options.cmake b/cmake/options.cmake index 314d000f3..f4a50f3fa 100644 --- a/cmake/options.cmake +++ b/cmake/options.cmake @@ -41,6 +41,7 @@ option(WITH_QCHART "When building GUI(need BUILD_GUI=ON), use Qt5 Chart option(HAVE_RULES "Usage of rules (needs PCRE library and headers)" OFF) option(USE_Z3 "Usage of z3 library" OFF) +option(USE_BUNDLED_TINYXML2 "Usage of bundled tinyxml2 library" ON) # precompiled headers do not emit compiler warnings so we cannot use them right now #if (CMAKE_VERSION VERSION_EQUAL "3.16" OR CMAKE_VERSION VERSION_GREATER "3.16") diff --git a/cmake/printInfo.cmake b/cmake/printInfo.cmake index c6fe4e1b1..3fb2495ee 100644 --- a/cmake/printInfo.cmake +++ b/cmake/printInfo.cmake @@ -52,6 +52,7 @@ if (USE_Z3) message( STATUS "Z3_LIBRARIES = ${Z3_LIBRARIES}" ) message( STATUS "Z3_CXX_INCLUDE_DIRS = ${Z3_CXX_INCLUDE_DIRS}" ) endif() +message( STATUS "USE_BUNDLED_TINYXML2 = ${USE_BUNDLED_TINYXML2}" ) message( STATUS ) if(${ANALYZE_ADDRESS}) diff --git a/gui/CMakeLists.txt b/gui/CMakeLists.txt index 6f818fa33..b096b63e1 100644 --- a/gui/CMakeLists.txt +++ b/gui/CMakeLists.txt @@ -10,7 +10,9 @@ if (BUILD_GUI) endif() include_directories(${PROJECT_SOURCE_DIR}/lib/) - include_directories(SYSTEM ${PROJECT_SOURCE_DIR}/externals/tinyxml2/) + if(USE_BUNDLED_TINYXML2) + include_directories(SYSTEM ${PROJECT_SOURCE_DIR}/externals/tinyxml2/) + endif() file(GLOB hdrs "*.h") file(GLOB srcs "*.cpp") @@ -19,14 +21,21 @@ if (BUILD_GUI) QT5_WRAP_UI(uis_hdrs ${uis}) QT5_ADD_RESOURCES(resources "gui.qrc") QT5_ADD_TRANSLATION(qms ${tss}) + list(APPEND cppcheck-gui_SOURCES ${hdrs} ${srcs} ${uis_hdrs} ${resources} ${qms} $ $) + if(USE_BUNDLED_TINYXML2) + list(APPEND cppcheck-gui_SOURCES $) + endif() - add_executable(cppcheck-gui ${hdrs} ${srcs} ${uis_hdrs} ${resources} ${qms} $ $ $) + add_executable(cppcheck-gui ${cppcheck-gui_SOURCES}) if (HAVE_RULES) target_link_libraries(cppcheck-gui ${PCRE_LIBRARY}) endif() if (USE_Z3) target_link_libraries(cppcheck-gui ${Z3_LIBRARIES}) endif() + if(tinyxml2_FOUND AND NOT USE_BUNDLED_TINYXML2) + target_link_libraries(cppcheck-gui tinyxml2) + endif() target_link_libraries(cppcheck-gui Qt5::Core Qt5::Gui Qt5::Widgets Qt5::PrintSupport Qt5::Help) if(WITH_QCHART) target_compile_definitions (cppcheck-gui PRIVATE HAVE_QCHART ) diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index ed41d2856..7f475534e 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -1,12 +1,18 @@ if (BUILD_TESTS) include_directories(${PROJECT_SOURCE_DIR}/lib/ ${PROJECT_SOURCE_DIR}/cli/) - include_directories(SYSTEM ${PROJECT_SOURCE_DIR}/externals/tinyxml2) + if(USE_BUNDLED_TINYXML2) + include_directories(SYSTEM ${PROJECT_SOURCE_DIR}/externals/tinyxml2) + endif() include_directories(${PROJECT_SOURCE_DIR}/externals/simplecpp/) file(GLOB hdrs "*.h") file(GLOB srcs "*.cpp") + list(APPEND testrunner_SOURCES ${hdrs} ${srcs} $ $ $) + if(USE_BUNDLED_TINYXML2) + list(APPEND testrunner_SOURCES $) + endif() - add_executable(testrunner ${hdrs} ${srcs} $ $ $ $) + add_executable(testrunner ${testrunner_SOURCES}) if (HAVE_RULES) target_link_libraries(testrunner ${PCRE_LIBRARY}) endif() @@ -16,6 +22,9 @@ if (BUILD_TESTS) if (WIN32 AND NOT BORLAND) target_link_libraries(testrunner Shlwapi.lib) endif() + if(tinyxml2_FOUND AND NOT USE_BUNDLED_TINYXML2) + target_link_libraries(testrunner tinyxml2) + endif() if (NOT CMAKE_DISABLE_PRECOMPILE_HEADERS) target_precompile_headers(testrunner PRIVATE precompiled.h)