From eab2a57b3758af4c8a08194d072ddd47a63fe6dd Mon Sep 17 00:00:00 2001 From: Joerg Bornemann Date: Thu, 3 Mar 2022 17:15:04 +0100 Subject: pro2cmake: Handle optional Qt modules ...and write a separate find_package(Qt6 OPTIONAL_COMPONENTS Foo Bar) call for those. Task-number: QTBUG-96799 Change-Id: I3386487774c386edde6767dca92ce433bfed906e Reviewed-by: Alexandru Croitor --- util/cmake/helper.py | 3 +- util/cmake/pro2cmake.py | 46 +++++++++++++++++++--- .../tests/data/conversion/optional_qt_modules.pro | 4 ++ util/cmake/tests/test_conversion.py | 8 ++++ 4 files changed, 54 insertions(+), 7 deletions(-) create mode 100644 util/cmake/tests/data/conversion/optional_qt_modules.pro (limited to 'util') diff --git a/util/cmake/helper.py b/util/cmake/helper.py index 0a363eb4d3..8382020717 100644 --- a/util/cmake/helper.py +++ b/util/cmake/helper.py @@ -787,13 +787,14 @@ def generate_find_package_info( emit_if: str = "", use_system_package_name: bool = False, remove_REQUIRED_from_extra: bool = True, + components_required: bool = True, module: str = "", ) -> str: isRequired = False extra = lib.extra.copy() if lib.components: - extra.append("COMPONENTS") + extra.append("COMPONENTS" if components_required else "OPTIONAL_COMPONENTS") extra += lib.components if "REQUIRED" in extra and use_qt_find_package: diff --git a/util/cmake/pro2cmake.py b/util/cmake/pro2cmake.py index 39e21e1c2a..7ae1e0610f 100755 --- a/util/cmake/pro2cmake.py +++ b/util/cmake/pro2cmake.py @@ -3731,7 +3731,13 @@ def write_binary(cm_fh: IO[str], scope: Scope, gui: bool = False, *, indent: int def write_find_package_section( - cm_fh: IO[str], public_libs: List[str], private_libs: List[str], *, indent: int = 0 + cm_fh: IO[str], + public_libs: List[str], + private_libs: List[str], + *, + indent: int = 0, + is_required: bool = True, + end_with_extra_newline: bool = True, ): packages = [] # type: List[LibraryMapping] all_libs = public_libs + private_libs @@ -3747,14 +3753,15 @@ def write_find_package_section( qt_components += p.components if qt_components: qt_components = sorted(qt_components) - qt_package = LibraryMapping( - "unknown", "Qt6", "unknown", extra=["REQUIRED"], components=qt_components - ) + qt_package = LibraryMapping("unknown", "Qt6", "unknown", components=qt_components) + if is_required: + qt_package.extra = ["REQUIRED"] cm_fh.write( generate_find_package_info( qt_package, use_qt_find_package=False, remove_REQUIRED_from_extra=False, + components_required=is_required, indent=indent, ) ) @@ -3762,7 +3769,7 @@ def write_find_package_section( for p in itertools.filterfalse(LibraryMapping.is_qt, packages): cm_fh.write(generate_find_package_info(p, use_qt_find_package=False, indent=indent)) - if packages: + if packages and end_with_extra_newline: cm_fh.write("\n") @@ -3901,8 +3908,35 @@ def write_example( handle_source_subtractions(scopes) scopes = merge_scopes(scopes) + # Write find_package calls for required packages. + # We consider packages as required if they appear at the top-level scope. (public_libs, private_libs) = extract_cmake_libraries(scope, is_example=True) - write_find_package_section(cm_fh, public_libs, private_libs, indent=indent) + write_find_package_section( + cm_fh, public_libs, private_libs, indent=indent, end_with_extra_newline=False + ) + + # Write find_package calls for optional packages. + # We consider packages inside scopes other than the top-level one as optional. + optional_public_libs: List[str] = [] + optional_private_libs: List[str] = [] + handling_first_scope = True + for inner_scope in scopes: + if handling_first_scope: + handling_first_scope = False + continue + (public_libs, private_libs) = extract_cmake_libraries(inner_scope, is_example=True) + optional_public_libs += public_libs + optional_private_libs += private_libs + write_find_package_section( + cm_fh, + optional_public_libs, + optional_private_libs, + indent=indent, + is_required=False, + end_with_extra_newline=False, + ) + + cm_fh.write("\n") (resources, standalone_qtquick_compiler_skipped_files) = extract_resources(binary_name, scope) qml_resource = find_qml_resource(resources) if is_qml_plugin else None diff --git a/util/cmake/tests/data/conversion/optional_qt_modules.pro b/util/cmake/tests/data/conversion/optional_qt_modules.pro new file mode 100644 index 0000000000..b9522169fc --- /dev/null +++ b/util/cmake/tests/data/conversion/optional_qt_modules.pro @@ -0,0 +1,4 @@ +TARGET = myapp +QT = core network widgets +win32: QT += opengl +SOURCES = main.cpp diff --git a/util/cmake/tests/test_conversion.py b/util/cmake/tests/test_conversion.py index 62afdc8eff..ad4a852012 100755 --- a/util/cmake/tests/test_conversion.py +++ b/util/cmake/tests/test_conversion.py @@ -71,3 +71,11 @@ def test_qt_modules(): if "find_package(" in line: find_package_lines.append(line.strip()) assert(["find_package(Qt6 REQUIRED COMPONENTS Core Network Widgets)"] == find_package_lines) + + output = convert("optional_qt_modules") + find_package_lines = [] + for line in output.split("\n"): + if "find_package(" in line: + find_package_lines.append(line.strip()) + assert(["find_package(Qt6 REQUIRED COMPONENTS Core Network Widgets)", + "find_package(Qt6 OPTIONAL_COMPONENTS OpenGL)"] == find_package_lines) -- cgit v1.2.3