From a0b1285f4a443563b0ba90f89f8224682ad2ae7f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oliver=20St=C3=B6neberg?= Date: Thu, 26 Jan 2023 22:13:07 +0100 Subject: [PATCH] added CMake option `BUILD_CORE_DLL` to build lib as `cppcheck-core.dll` with Visual Studio (#4733) --- .github/workflows/iwyu.yml | 2 +- .github/workflows/selfcheck.yml | 6 +++--- cli/CMakeLists.txt | 19 ++++++++++++++++--- cmake/compilerDefinitions.cmake | 2 -- cmake/options.cmake | 7 +++++++ cmake/printInfo.cmake | 1 + externals/simplecpp/CMakeLists.txt | 4 ++++ externals/tinyxml2/CMakeLists.txt | 3 +++ gui/CMakeLists.txt | 13 ++++++++++--- gui/test/xmlreportv2/CMakeLists.txt | 13 +++++++++---- lib/CMakeLists.txt | 24 +++++++++++++++--------- oss-fuzz/CMakeLists.txt | 2 +- releasenotes.txt | 1 + test/CMakeLists.txt | 13 ++++++++++--- 14 files changed, 81 insertions(+), 29 deletions(-) diff --git a/.github/workflows/iwyu.yml b/.github/workflows/iwyu.yml index ce30dd99c..d5afa818e 100644 --- a/.github/workflows/iwyu.yml +++ b/.github/workflows/iwyu.yml @@ -33,7 +33,7 @@ jobs: - name: Prepare CMake dependencies run: | # make sure the precompiled headers exist - #make -C cmake.output lib/CMakeFiles/lib_objs.dir/cmake_pch.hxx.cxx + #make -C cmake.output lib/CMakeFiles/cppcheck-core.dir/cmake_pch.hxx.cxx #make -C cmake.output test/CMakeFiles/testrunner.dir/cmake_pch.hxx.cxx # make sure the auto-generated GUI sources exist make -C cmake.output autogen diff --git a/.github/workflows/selfcheck.yml b/.github/workflows/selfcheck.yml index 38e91441a..1e2307f28 100644 --- a/.github/workflows/selfcheck.yml +++ b/.github/workflows/selfcheck.yml @@ -58,7 +58,7 @@ jobs: - name: Generate dependencies run: | # make sure the precompiled headers exist - make -C cmake.output lib/CMakeFiles/lib_objs.dir/cmake_pch.hxx.cxx + make -C cmake.output lib/CMakeFiles/cppcheck-core.dir/cmake_pch.hxx.cxx make -C cmake.output test/CMakeFiles/testrunner.dir/cmake_pch.hxx.cxx # make sure auto-generated GUI files exist make -C cmake.output autogen @@ -81,7 +81,7 @@ jobs: - name: Generate dependencies (no test) run: | # make sure the precompiled headers exist - make -C cmake.output.notest lib/CMakeFiles/lib_objs.dir/cmake_pch.hxx.cxx + make -C cmake.output.notest lib/CMakeFiles/cppcheck-core.dir/cmake_pch.hxx.cxx # make sure auto-generated GUI files exist make -C cmake.output.notest autogen make -C cmake.output.notest gui-build-deps @@ -106,7 +106,7 @@ jobs: - name: Generate dependencies (corpus) run: | # make sure the precompiled headers exist - make -C cmake.output.notest lib/CMakeFiles/lib_objs.dir/cmake_pch.hxx.cxx + make -C cmake.output.notest lib/CMakeFiles/cppcheck-core.dir/cmake_pch.hxx.cxx # make sure auto-generated GUI files exist make -C cmake.output.corpus autogen make -C cmake.output.corpus gui-build-deps diff --git a/cli/CMakeLists.txt b/cli/CMakeLists.txt index 09244298d..b4f83e4df 100644 --- a/cli/CMakeLists.txt +++ b/cli/CMakeLists.txt @@ -11,15 +11,25 @@ else() target_include_directories(cli_objs SYSTEM PRIVATE ${tinyxml2_INCLUDE_DIRS}) endif() target_include_directories(cli_objs PRIVATE ${PROJECT_SOURCE_DIR}/externals/simplecpp/) +if (BUILD_CORE_DLL) + target_compile_definitions(cli_objs PRIVATE CPPCHECKLIB_IMPORT TINYXML2_IMPORT) +endif() if (CMAKE_CXX_COMPILER_ID MATCHES "Clang" AND CMAKE_CXX_COMPILER_VERSION VERSION_LESS 14) # false positive warning in up to Clang 13 - caused by FD_ZERO macro set_source_files_properties(processexecutor.cpp PROPERTIES COMPILE_FLAGS -Wno-reserved-identifier) endif() -list(APPEND cppcheck_SOURCES ${hdrs} ${mainfile} $ $ $) -if(USE_BUNDLED_TINYXML2) - list(APPEND cppcheck_SOURCES $) +list(APPEND cppcheck_SOURCES ${hdrs} ${mainfile} $) +if (NOT BUILD_CORE_DLL) + list(APPEND cppcheck_SOURCES $) + list(APPEND cppcheck_SOURCES $) + if(USE_BUNDLED_TINYXML2) + list(APPEND cppcheck_SOURCES $) + endif() +endif() +if (WIN32) + list(APPEND cppcheck_SOURCES version.rc) endif() add_executable(cppcheck ${cppcheck_SOURCES}) @@ -44,6 +54,9 @@ if(tinyxml2_FOUND AND NOT USE_BUNDLED_TINYXML2) target_link_libraries(cppcheck ${tinyxml2_LIBRARIES}) endif() target_link_libraries(cppcheck ${CMAKE_THREAD_LIBS_INIT}) +if (BUILD_CORE_DLL) + target_link_libraries(cppcheck cppcheck-core) +endif() add_dependencies(cppcheck copy_cfg) add_dependencies(cppcheck copy_addons) diff --git a/cmake/compilerDefinitions.cmake b/cmake/compilerDefinitions.cmake index 06d2bfdb2..5adad6b58 100644 --- a/cmake/compilerDefinitions.cmake +++ b/cmake/compilerDefinitions.cmake @@ -4,8 +4,6 @@ if (MSVC) add_definitions(-DDEBUG) endif() - #add_definitions(-DCPPCHECKLIB_IMPORT) - #add_definitions(-DTINYXML2_IMPORT) add_definitions(-DWIN32) add_definitions(-D_CRT_SECURE_NO_WARNINGS) add_definitions(-DWIN32_LEAN_MEAN) diff --git a/cmake/options.cmake b/cmake/options.cmake index 09954a9b1..088cd7128 100644 --- a/cmake/options.cmake +++ b/cmake/options.cmake @@ -31,6 +31,10 @@ else() set(USE_MATCHCOMPILER_OPT ${USE_MATCHCOMPILER}) endif() +option(BUILD_CORE_DLL "Build lib as cppcheck-core.dll with Visual Studio" OFF) +if (NOT MSVC) + set(BUILD_CORE_DLL OFF) +endif() option(BUILD_TESTS "Build tests" OFF) option(REGISTER_TESTS "Register tests in CTest" ON) option(ENABLE_CHECK_INTERNAL "Enable internal checks" OFF) @@ -42,6 +46,9 @@ option(USE_QT6 "Prefer Qt6 when available" option(HAVE_RULES "Usage of rules (needs PCRE library and headers)" OFF) option(USE_BUNDLED_TINYXML2 "Usage of bundled tinyxml2 library" ON) +if (BUILD_CORE_DLL) + set(USE_BUNDLED_TINYXML2 ON) +endif() option(CPPCHK_GLIBCXX_DEBUG "Usage of STL debug checks in Debug build" ON) option(USE_THREADS "Usage of threads instead of fork() for -j" OFF) option(USE_BOOST "Usage of Boost" OFF) diff --git a/cmake/printInfo.cmake b/cmake/printInfo.cmake index 61027e1fe..7a2d5666a 100644 --- a/cmake/printInfo.cmake +++ b/cmake/printInfo.cmake @@ -39,6 +39,7 @@ else() message( STATUS "BUILD_SHARED_LIBS = ${BUILD_SHARED_LIBS}" ) endif(NOT DEFINED BUILD_SHARED_LIBS) message( STATUS "LIBXML2_XMLLINT_EXECUTABLE = ${LIBXML2_XMLLINT_EXECUTABLE}" ) +message( STATUS "BUILD_CORE_DLL = ${BUILD_CORE_DLL}" ) message( STATUS "BUILD_TESTS = ${BUILD_TESTS}" ) if(BUILD_TESTS) message( STATUS "REGISTER_TESTS = ${REGISTER_TESTS}" ) diff --git a/externals/simplecpp/CMakeLists.txt b/externals/simplecpp/CMakeLists.txt index 8536a1d6f..18430fea5 100644 --- a/externals/simplecpp/CMakeLists.txt +++ b/externals/simplecpp/CMakeLists.txt @@ -2,6 +2,10 @@ file(GLOB hdrs "*.h") file(GLOB srcs "*.cpp") add_library(simplecpp_objs OBJECT ${srcs} ${hdrs}) +if (BUILD_CORE_DLL) + target_compile_definitions(simplecpp_objs PRIVATE SIMPLECPP_EXPORT) +endif() + if (CMAKE_CXX_COMPILER_ID MATCHES "Clang") target_compile_options_safe(simplecpp_objs -Wno-zero-as-null-pointer-constant) endif() diff --git a/externals/tinyxml2/CMakeLists.txt b/externals/tinyxml2/CMakeLists.txt index a1fe30aa3..6ec93e526 100644 --- a/externals/tinyxml2/CMakeLists.txt +++ b/externals/tinyxml2/CMakeLists.txt @@ -2,6 +2,9 @@ file(GLOB hdrs "*.h") file(GLOB srcs "*.cpp") add_library(tinyxml2_objs OBJECT ${srcs} ${hdrs}) +if (BUILD_CORE_DLL) + target_compile_definitions(tinyxml2_objs PRIVATE TINYXML2_EXPORT) +endif() # TODO: needs to be fixed upstream if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU") diff --git a/gui/CMakeLists.txt b/gui/CMakeLists.txt index d7c6edc47..19953448e 100644 --- a/gui/CMakeLists.txt +++ b/gui/CMakeLists.txt @@ -28,9 +28,12 @@ CheckOptions: list(APPEND cppcheck-gui-deps ${hdrs} ${uis_hdrs} ${resources} ${qms}) add_custom_target(gui-build-deps SOURCES ${cppcheck-gui-deps}) - list(APPEND cppcheck-gui_SOURCES ${srcs} $ $) - if(USE_BUNDLED_TINYXML2) - list(APPEND cppcheck-gui_SOURCES $) + list(APPEND cppcheck-gui_SOURCES ${srcs}) + if (NOT BUILD_CORE_DLL) + list(APPEND cppcheck-gui_SOURCES $ $) + if(USE_BUNDLED_TINYXML2) + list(APPEND cppcheck-gui_SOURCES $) + endif() endif() add_executable(cppcheck-gui ${cppcheck-gui-deps} ${cppcheck-gui_SOURCES}) @@ -52,6 +55,10 @@ CheckOptions: target_compile_definitions (cppcheck-gui PRIVATE HAVE_QCHART) target_link_libraries(cppcheck-gui ${QT_CHARTS_LIB}) endif() + if (BUILD_CORE_DLL) + target_compile_definitions(cppcheck-gui PRIVATE CPPCHECKLIB_IMPORT TINYXML2_IMPORT) + target_link_libraries(cppcheck-gui cppcheck-core) + endif() if (CMAKE_CXX_COMPILER_ID MATCHES "Clang") # Q_UNUSED() in generated code target_compile_options_safe(cppcheck-gui -Wno-extra-semi-stmt) diff --git a/gui/test/xmlreportv2/CMakeLists.txt b/gui/test/xmlreportv2/CMakeLists.txt index 39deb074e..867eabee0 100644 --- a/gui/test/xmlreportv2/CMakeLists.txt +++ b/gui/test/xmlreportv2/CMakeLists.txt @@ -1,8 +1,11 @@ qt_wrap_cpp(test-xmlreportv2_SRC testxmlreportv2.h) add_custom_target(build-xmlreportv2-deps SOURCES ${test-xmlreportv2_SRC}) add_dependencies(gui-build-deps build-xmlreportv2-deps) -if(USE_BUNDLED_TINYXML2) - list(APPEND test-xmlreportv2_SRC $) +if (NOT BUILD_CORE_DLL) + list(APPEND test-xmlreportv2_SRC $ $) + if(USE_BUNDLED_TINYXML2) + list(APPEND test-xmlreportv2_SRC $) + endif() endif() add_executable(test-xmlreportv2 ${test-xmlreportv2_SRC} @@ -11,8 +14,6 @@ add_executable(test-xmlreportv2 ${CMAKE_SOURCE_DIR}/gui/report.cpp ${CMAKE_SOURCE_DIR}/gui/xmlreport.cpp ${CMAKE_SOURCE_DIR}/gui/xmlreportv2.cpp - $ - $ ) target_include_directories(test-xmlreportv2 PRIVATE ${CMAKE_SOURCE_DIR}/gui ${CMAKE_SOURCE_DIR}/lib) target_compile_definitions(test-xmlreportv2 PRIVATE SRCDIR="${CMAKE_CURRENT_SOURCE_DIR}") @@ -23,3 +24,7 @@ endif() if(tinyxml2_FOUND AND NOT USE_BUNDLED_TINYXML2) target_link_libraries(test-xmlreportv2 ${tinyxml2_LIBRARIES}) endif() +if (BUILD_CORE_DLL) + target_compile_definitions(test-xmlreportv2 PRIVATE CPPCHECKLIB_IMPORT TINYXML2_IMPORT) + target_link_libraries(test-xmlreportv2 cppcheck-core) +endif() diff --git a/lib/CMakeLists.txt b/lib/CMakeLists.txt index 4fe6a8f06..741e6c2ad 100644 --- a/lib/CMakeLists.txt +++ b/lib/CMakeLists.txt @@ -32,23 +32,29 @@ else() set(srcs_lib ${srcs}) endif() -add_library(lib_objs OBJECT ${srcs_lib} ${hdrs}) -if(USE_BUNDLED_TINYXML2) - target_include_directories(lib_objs PRIVATE ${PROJECT_SOURCE_DIR}/externals/tinyxml2/) +if (BUILD_CORE_DLL) + add_library(cppcheck-core SHARED ${srcs_lib} ${hdrs} $ $ version.rc) + target_compile_definitions(cppcheck-core PRIVATE CPPCHECKLIB_EXPORT TINYXML2_EXPORT SIMPLECPP_EXPORT) else() - target_include_directories(lib_objs SYSTEM PRIVATE ${tinyxml2_INCLUDE_DIRS}) + add_library(cppcheck-core OBJECT ${srcs_lib} ${hdrs}) endif() -target_include_directories(lib_objs PRIVATE ${PROJECT_SOURCE_DIR}/externals/picojson/) -target_include_directories(lib_objs PRIVATE ${PROJECT_SOURCE_DIR}/externals/simplecpp/) +target_include_directories(cppcheck-core PRIVATE ${PROJECT_SOURCE_DIR}/externals/) +if(USE_BUNDLED_TINYXML2) + target_include_directories(cppcheck-core PRIVATE ${PROJECT_SOURCE_DIR}/externals/tinyxml2/) +else() + target_include_directories(cppcheck-core SYSTEM PRIVATE ${tinyxml2_INCLUDE_DIRS}) +endif() +target_include_directories(cppcheck-core PRIVATE ${PROJECT_SOURCE_DIR}/externals/picojson/) +target_include_directories(cppcheck-core PRIVATE ${PROJECT_SOURCE_DIR}/externals/simplecpp/) if (HAVE_RULES) - target_include_directories(lib_objs SYSTEM PRIVATE ${PCRE_INCLUDE}) + target_include_directories(cppcheck-core SYSTEM PRIVATE ${PCRE_INCLUDE}) endif() if (Boost_FOUND) - target_include_directories(lib_objs SYSTEM PRIVATE ${Boost_INCLUDE_DIRS}) + target_include_directories(cppcheck-core SYSTEM PRIVATE ${Boost_INCLUDE_DIRS}) endif() if (NOT CMAKE_DISABLE_PRECOMPILE_HEADERS) - target_precompile_headers(lib_objs PRIVATE precompiled.h) + target_precompile_headers(cppcheck-core PRIVATE precompiled.h) endif() if (CMAKE_CXX_COMPILER_ID MATCHES "Clang") diff --git a/oss-fuzz/CMakeLists.txt b/oss-fuzz/CMakeLists.txt index dbb28562a..a41168cf7 100644 --- a/oss-fuzz/CMakeLists.txt +++ b/oss-fuzz/CMakeLists.txt @@ -9,7 +9,7 @@ if (ENABLE_OSS_FUZZ AND CMAKE_CXX_COMPILER_ID MATCHES "Clang") add_executable(fuzz-client EXCLUDE_FROM_ALL ${fuzz-client_SRC} $ - $) + $) target_include_directories(fuzz-client PRIVATE ${CMAKE_SOURCE_DIR}/lib ${CMAKE_SOURCE_DIR}/externals/simplecpp ${CMAKE_SOURCE_DIR}/externals) if(USE_BUNDLED_TINYXML2) target_include_directories(fuzz-client PRIVATE ${CMAKE_SOURCE_DIR}/externals/tinyxml2/) diff --git a/releasenotes.txt b/releasenotes.txt index de0c34494..64bfc8f1b 100644 --- a/releasenotes.txt +++ b/releasenotes.txt @@ -3,3 +3,4 @@ release notes for cppcheck-2.10 - the deprecated Makefile option SRCDIR is no longer accepted - if the file provided via "--file-list" cannot be opened it will now error out - add command-line option "--disable=" to individually disable checks +- added CMake option BUILD_CORE_DLL to build lib as cppcheck-core.dll with Visual Studio diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index d5411505d..ab6bcf860 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -1,9 +1,12 @@ if (BUILD_TESTS) file(GLOB hdrs "*.h") file(GLOB srcs "*.cpp") - list(APPEND testrunner_SOURCES ${hdrs} ${srcs} $ $ $) - if(USE_BUNDLED_TINYXML2) - list(APPEND testrunner_SOURCES $) + list(APPEND testrunner_SOURCES ${hdrs} ${srcs} $) + if (NOT BUILD_CORE_DLL) + list(APPEND testrunner_SOURCES $ $) + if(USE_BUNDLED_TINYXML2) + list(APPEND testrunner_SOURCES $) + endif() endif() add_executable(testrunner ${testrunner_SOURCES}) @@ -28,6 +31,10 @@ if (BUILD_TESTS) target_link_libraries(testrunner ${tinyxml2_LIBRARIES}) endif() target_link_libraries(testrunner ${CMAKE_THREAD_LIBS_INIT}) + if (BUILD_CORE_DLL) + target_compile_definitions(testrunner PRIVATE CPPCHECKLIB_IMPORT SIMPLECPP_IMPORT) + target_link_libraries(testrunner cppcheck-core) + endif() if (NOT CMAKE_DISABLE_PRECOMPILE_HEADERS) target_precompile_headers(testrunner PRIVATE precompiled.h)