From 928828b54905c361ba52c8ff043c9247a25e9375 Mon Sep 17 00:00:00 2001 From: Joerg Bornemann Date: Fri, 4 Mar 2022 12:49:44 +0100 Subject: [PATCH] pro2cmake: Handle QT_(MAJOR_|MINOR_|PATCH_)VERSION conditions Task-number: QTBUG-98852 Change-Id: I4c86fff7bbcc6c42cd04094f2409c3d04779597c Reviewed-by: Alexandru Croitor --- util/cmake/pro2cmake.py | 22 +++++++++++++++++++ .../data/conversion/qt_version_check.pro | 8 +++++++ util/cmake/tests/test_conversion.py | 8 +++++++ 3 files changed, 38 insertions(+) create mode 100644 util/cmake/tests/data/conversion/qt_version_check.pro diff --git a/util/cmake/pro2cmake.py b/util/cmake/pro2cmake.py index 95027c738a..c30591591b 100755 --- a/util/cmake/pro2cmake.py +++ b/util/cmake/pro2cmake.py @@ -1669,6 +1669,28 @@ def map_condition(condition: str) -> str: pattern = r"(equals|greaterThan|lessThan)\(WINDOWS_SDK_VERSION,[ ]*([0-9]+)\)" condition = re.sub(pattern, windows_sdk_version_handler, condition) + def qt_version_handler(match_obj: Match): + operator = match_obj.group(1) + if operator == "equals": + operator = "EQUAL" + elif operator == "greaterThan": + operator = "GREATER" + elif operator == "lessThan": + operator = "LESS" + + operator_prefix = "VERSION_" + version_variable = "QT_VERSION" + version_flavor = match_obj.group(2) + if version_flavor: + version_variable += "_" + version_flavor[:-1] + operator_prefix = "" + + version = match_obj.group(3) + return f"({version_variable} {operator_prefix}{operator} {version})" + + pattern = r"(equals|greaterThan|lessThan)\(QT_(MAJOR_|MINOR_|PATCH_)?VERSION,[ ]*([0-9.]+)\)" + condition = re.sub(pattern, qt_version_handler, condition) + # Generic lessThan|equals|lessThan() def generic_version_handler(match_obj: Match): diff --git a/util/cmake/tests/data/conversion/qt_version_check.pro b/util/cmake/tests/data/conversion/qt_version_check.pro new file mode 100644 index 0000000000..cf3697bb64 --- /dev/null +++ b/util/cmake/tests/data/conversion/qt_version_check.pro @@ -0,0 +1,8 @@ +QT += core gui +SOURCES += main.cpp +greaterThan(QT_MAJOR_VERSION, 5):lessThan(QT_MINOR_VERSION, 1):equals(QT_PATCH_VERSION, 0) { + DEFINES += SUPER_FRESH_MAJOR_QT_RELEASE +} +greaterThan(QT_VERSION, 6.6.5):lessThan(QT_VERSION, 6.6.7):equals(QT_VERSION, 6.6.6): { + DEFINES += QT_VERSION_OF_THE_BEAST +} diff --git a/util/cmake/tests/test_conversion.py b/util/cmake/tests/test_conversion.py index 54ddd35d9c..be5fdc90dd 100755 --- a/util/cmake/tests/test_conversion.py +++ b/util/cmake/tests/test_conversion.py @@ -81,3 +81,11 @@ def test_qt_modules(): assert(["find_package(QT NAMES Qt5 Qt6 REQUIRED COMPONENTS Core)", "find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS Network Widgets)", "find_package(Qt${QT_VERSION_MAJOR} OPTIONAL_COMPONENTS OpenGL)"] == find_package_lines) + +def test_qt_version_check(): + output = convert("qt_version_check") + interesting_lines = [] + for line in output.split("\n"): + if line.startswith("if(") and "QT_VERSION" in line: + interesting_lines.append(line.strip()) + assert(["if(( ( (QT_VERSION_MAJOR GREATER 5) ) AND (QT_VERSION_MINOR LESS 1) ) AND (QT_VERSION_PATCH EQUAL 0))", "if(( ( (QT_VERSION VERSION_GREATER 6.6.5) ) AND (QT_VERSION VERSION_LESS 6.6.7) ) AND (QT_VERSION VERSION_EQUAL 6.6.6))"] == interesting_lines)