From 295d089477d76c42159810ec094481d167ec64d6 Mon Sep 17 00:00:00 2001 From: Maarten Bent Date: Wed, 10 Sep 2025 14:24:09 +0200 Subject: [PATCH] CMake: Remove duplicate find_package Packages of third-party dependencies were searched twice. Once when adding them via wx_add_thirdparty_library to determine if they are available or of the built-in library should be used. And then again when including the third-party cmake file. Move all the logic into the third-party library cmake file. This also removes the need to move the WebP variables into the parent scope. --- build/cmake/functions.cmake | 8 -------- build/cmake/lib/expat.cmake | 10 ++++++++-- build/cmake/lib/jpeg.cmake | 10 ++++++++-- build/cmake/lib/nanosvg.cmake | 10 ++++++++-- build/cmake/lib/png.cmake | 10 ++++++++-- build/cmake/lib/regex.cmake | 15 +++++++++++---- build/cmake/lib/tiff.cmake | 10 ++++++++-- build/cmake/lib/webp.cmake | 8 ++++++++ build/cmake/lib/zlib.cmake | 10 ++++++++-- build/cmake/modules/FindWebP.cmake | 3 --- 10 files changed, 67 insertions(+), 27 deletions(-) diff --git a/build/cmake/functions.cmake b/build/cmake/functions.cmake index 3c3f87984a..2617ff5c83 100644 --- a/build/cmake/functions.cmake +++ b/build/cmake/functions.cmake @@ -718,14 +718,6 @@ function(wx_add_thirdparty_library var_name lib_name help_str) wx_option(${var_name} ${help_str} ${thirdparty_lib_default} STRINGS builtin sys OFF) - if(${var_name} STREQUAL "sys") - # If the sys library can not be found use builtin - find_package(${lib_name}) - if(NOT ${lib_name}_FOUND) - wx_option_force_value(${var_name} builtin) - endif() - endif() - set(wxTHIRD_PARTY_LIBRARIES ${wxTHIRD_PARTY_LIBRARIES} ${var_name} "${help_str}" PARENT_SCOPE) endfunction() diff --git a/build/cmake/lib/expat.cmake b/build/cmake/lib/expat.cmake index 7200dd5dab..de7a9d69c2 100644 --- a/build/cmake/lib/expat.cmake +++ b/build/cmake/lib/expat.cmake @@ -7,6 +7,14 @@ # Licence: wxWindows licence ############################################################################# +if(wxUSE_EXPAT STREQUAL "sys") + find_package(EXPAT) + if(NOT EXPAT_FOUND) + # If the sys library can not be found use builtin + wx_option_force_value(wxUSE_EXPAT builtin) + endif() +endif() + if(wxUSE_EXPAT STREQUAL "builtin") # TODO: implement building expat via its CMake file, using # add_subdirectory or ExternalProject_Add @@ -17,6 +25,4 @@ if(wxUSE_EXPAT STREQUAL "builtin") ) set(EXPAT_LIBRARIES wxexpat) set(EXPAT_INCLUDE_DIRS ${wxSOURCE_DIR}/src/expat/expat/lib) -elseif(wxUSE_EXPAT) - find_package(EXPAT REQUIRED) endif() diff --git a/build/cmake/lib/jpeg.cmake b/build/cmake/lib/jpeg.cmake index 77014944a7..5146788673 100644 --- a/build/cmake/lib/jpeg.cmake +++ b/build/cmake/lib/jpeg.cmake @@ -7,6 +7,14 @@ # Licence: wxWindows licence ############################################################################# +if(wxUSE_LIBJPEG STREQUAL "sys") + find_package(JPEG) + if(NOT JPEG_FOUND) + # If the sys library can not be found use builtin + wx_option_force_value(wxUSE_LIBJPEG builtin) + endif() +endif() + if(wxUSE_LIBJPEG STREQUAL "builtin") wx_add_builtin_library(wxjpeg src/jpeg/jaricom.c @@ -62,6 +70,4 @@ if(wxUSE_LIBJPEG STREQUAL "builtin") ) set(JPEG_LIBRARIES wxjpeg) set(JPEG_INCLUDE_DIR ${wxSOURCE_DIR}/src/jpeg) -elseif(wxUSE_LIBJPEG) - find_package(JPEG REQUIRED) endif() diff --git a/build/cmake/lib/nanosvg.cmake b/build/cmake/lib/nanosvg.cmake index 401bf48fcf..2135799a3e 100644 --- a/build/cmake/lib/nanosvg.cmake +++ b/build/cmake/lib/nanosvg.cmake @@ -7,6 +7,14 @@ # Licence: wxWindows licence ############################################################################# +if(wxUSE_NANOSVG STREQUAL "sys") + find_package(NanoSVG) + if(NOT NanoSVG_FOUND) + # If the sys library can not be found use builtin + wx_option_force_value(wxUSE_NANOSVG builtin) + endif() +endif() + if(wxUSE_NANOSVG STREQUAL "builtin") set(wxUSE_NANOSVG_EXTERNAL 0 PARENT_SCOPE) elseif(wxUSE_NANOSVG) @@ -16,8 +24,6 @@ elseif(wxUSE_NANOSVG) set(NANOSVG_INCLUDE_DIRS ) set(wxUSE_NANOSVG_EXTERNAL_ENABLE_IMPL TRUE) - find_package(NanoSVG REQUIRED) - foreach(TARGETNAME NanoSVG::nanosvg NanoSVG::nanosvgrast unofficial::nanosvg) if(NOT TARGET ${TARGETNAME}) continue() diff --git a/build/cmake/lib/png.cmake b/build/cmake/lib/png.cmake index 13ddab58e0..8f64bf4a2e 100644 --- a/build/cmake/lib/png.cmake +++ b/build/cmake/lib/png.cmake @@ -7,6 +7,14 @@ # Licence: wxWindows licence ############################################################################# +if(wxUSE_LIBPNG STREQUAL "sys") + find_package(PNG) + if(NOT PNG_FOUND) + # If the sys library can not be found use builtin + wx_option_force_value(wxUSE_LIBPNG builtin) + endif() +endif() + if(wxUSE_LIBPNG STREQUAL "builtin") # TODO: implement building libpng via its CMake file, using # add_subdirectory or ExternalProject_Add @@ -53,6 +61,4 @@ if(wxUSE_LIBPNG STREQUAL "builtin") target_link_libraries(wxpng PRIVATE ${ZLIB_LIBRARIES}) set(PNG_LIBRARIES wxpng) set(PNG_INCLUDE_DIRS ${wxSOURCE_DIR}/src/png) -elseif(wxUSE_LIBPNG) - find_package(PNG REQUIRED) endif() diff --git a/build/cmake/lib/regex.cmake b/build/cmake/lib/regex.cmake index cc8e46d39f..c629ded06b 100644 --- a/build/cmake/lib/regex.cmake +++ b/build/cmake/lib/regex.cmake @@ -7,6 +7,17 @@ # Licence: wxWindows licence ############################################################################# +if(wxUSE_REGEX STREQUAL "sys") + find_package(PCRE2) + if(NOT PCRE2_FOUND) + # If the sys library can not be found use builtin + wx_option_force_value(wxUSE_REGEX builtin) + else() + set(REGEX_LIBRARIES ${PCRE2_LIBRARIES}) + set(REGEX_INCLUDE_DIRS ${PCRE2_INCLUDE_DIRS}) + endif() +endif() + if(wxUSE_REGEX STREQUAL "builtin") # TODO: implement building PCRE2 via its CMake file, using # add_subdirectory or ExternalProject_Add @@ -45,8 +56,4 @@ if(wxUSE_REGEX STREQUAL "builtin") set(REGEX_INCLUDE_DIRS ${wxSOURCE_DIR}/3rdparty/pcre/src/wx) target_compile_definitions(wxregex PRIVATE __WX__ HAVE_CONFIG_H) target_include_directories(wxregex PRIVATE ${wxSETUP_HEADER_PATH} ${wxSOURCE_DIR}/include ${REGEX_INCLUDE_DIRS}) -elseif(wxUSE_REGEX) - find_package(PCRE2 REQUIRED) - set(REGEX_LIBRARIES ${PCRE2_LIBRARIES}) - set(REGEX_INCLUDE_DIRS ${PCRE2_INCLUDE_DIRS}) endif() diff --git a/build/cmake/lib/tiff.cmake b/build/cmake/lib/tiff.cmake index c1610a0f14..1bfc13c1c5 100644 --- a/build/cmake/lib/tiff.cmake +++ b/build/cmake/lib/tiff.cmake @@ -7,6 +7,14 @@ # Licence: wxWindows licence ############################################################################# +if(wxUSE_LIBTIFF STREQUAL "sys") + find_package(TIFF) + if(NOT TIFF_FOUND) + # If the sys library can not be found use builtin + wx_option_force_value(wxUSE_LIBTIFF builtin) + endif() +endif() + if(wxUSE_LIBTIFF STREQUAL "builtin") # TODO: implement building libtiff via its CMake file, using # add_subdirectory or ExternalProject_Add @@ -76,6 +84,4 @@ if(wxUSE_LIBTIFF STREQUAL "builtin") target_link_libraries(wxtiff PRIVATE ${ZLIB_LIBRARIES} ${JPEG_LIBRARIES}) set(TIFF_LIBRARIES wxtiff) set(TIFF_INCLUDE_DIRS ${wxSOURCE_DIR}/src/tiff/libtiff) -elseif(wxUSE_LIBTIFF) - find_package(TIFF REQUIRED) endif() diff --git a/build/cmake/lib/webp.cmake b/build/cmake/lib/webp.cmake index 9573887ef4..d4704f22a1 100644 --- a/build/cmake/lib/webp.cmake +++ b/build/cmake/lib/webp.cmake @@ -7,6 +7,14 @@ # Licence: wxWindows licence ############################################################################# +if(wxUSE_LIBWEBP STREQUAL "sys") + find_package(WebP) + if(NOT WebP_FOUND) + # If the sys library can not be found use builtin + wx_option_force_value(wxUSE_LIBWEBP builtin) + endif() +endif() + if(wxUSE_LIBWEBP STREQUAL "builtin") set(WEBP_ROOT "${wxSOURCE_DIR}/3rdparty/libwebp") set(WEBP_BUILD_ROOT "${CMAKE_CURRENT_BINARY_DIR}/webp-build") diff --git a/build/cmake/lib/zlib.cmake b/build/cmake/lib/zlib.cmake index b0a106fa4d..b3c9f552db 100644 --- a/build/cmake/lib/zlib.cmake +++ b/build/cmake/lib/zlib.cmake @@ -7,6 +7,14 @@ # Licence: wxWindows licence ############################################################################# +if(wxUSE_ZLIB STREQUAL "sys") + find_package(ZLIB) + if(NOT ZLIB_FOUND) + # If the sys library can not be found use builtin + wx_option_force_value(wxUSE_ZLIB builtin) + endif() +endif() + if(wxUSE_ZLIB STREQUAL "builtin") # TODO: implement building zlib via its CMake file, using # add_subdirectory or ExternalProject_Add @@ -40,6 +48,4 @@ if(wxUSE_ZLIB STREQUAL "builtin") endif() set(ZLIB_LIBRARIES wxzlib) set(ZLIB_INCLUDE_DIRS ${wxSOURCE_DIR}/src/zlib) -elseif(wxUSE_ZLIB) - find_package(ZLIB REQUIRED) endif() diff --git a/build/cmake/modules/FindWebP.cmake b/build/cmake/modules/FindWebP.cmake index 54119f7c2e..d28f530503 100644 --- a/build/cmake/modules/FindWebP.cmake +++ b/build/cmake/modules/FindWebP.cmake @@ -6,6 +6,3 @@ if(NOT WebP_FOUND) pkg_check_modules(WebP libwebp) endif() endif() - -set(WebP_LIBRARIES ${WebP_LIBRARIES} PARENT_SCOPE) -set(WebP_INCLUDE_DIRS ${WebP_INCLUDE_DIRS} PARENT_SCOPE)