diff options
Diffstat (limited to 'util')
40 files changed, 4494 insertions, 0 deletions
diff --git a/util/cmake/Pipfile b/util/cmake/Pipfile new file mode 100644 index 0000000000..7fbf716eb8 --- /dev/null +++ b/util/cmake/Pipfile @@ -0,0 +1,15 @@ +[[source]] +url = "https://pypi.org/simple" +verify_ssl = true +name = "pypi" + +[packages] +pytest = "*" +mypy = "*" +pyparsing = "*" +sympy = "*" + +[dev-packages] + +[requires] +python_version = "3.7" diff --git a/util/cmake/cmakeconversionrate.py b/util/cmake/cmakeconversionrate.py new file mode 100755 index 0000000000..3496ed1b91 --- /dev/null +++ b/util/cmake/cmakeconversionrate.py @@ -0,0 +1,127 @@ +#!/usr/bin/env python3 +############################################################################# +## +## Copyright (C) 2018 The Qt Company Ltd. +## Contact: https://www.qt.io/licensing/ +## +## This file is part of the plugins of the Qt Toolkit. +## +## $QT_BEGIN_LICENSE:GPL-EXCEPT$ +## Commercial License Usage +## Licensees holding valid commercial Qt licenses may use this file in +## accordance with the commercial license agreement provided with the +## Software or, alternatively, in accordance with the terms contained in +## a written agreement between you and The Qt Company. For licensing terms +## and conditions see https://www.qt.io/terms-conditions. For further +## information use the contact form at https://www.qt.io/contact-us. +## +## GNU General Public License Usage +## Alternatively, this file may be used under the terms of the GNU +## General Public License version 3 as published by the Free Software +## Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +## included in the packaging of this file. Please review the following +## information to ensure the GNU General Public License requirements will +## be met: https://www.gnu.org/licenses/gpl-3.0.html. +## +## $QT_END_LICENSE$ +## +############################################################################# + +from argparse import ArgumentParser + +import os +import re +import subprocess +import sys +import typing + + +def _parse_commandline(): + parser = ArgumentParser(description='Calculate the conversion rate to cmake.') + parser.add_argument('--debug', dest='debug', action='store_true', + help='Turn on debug output') + parser.add_argument('source_directory', metavar='<Source Directory>', type=str, + help='The Qt module source directory') + parser.add_argument('binary_directory', metavar='<CMake build direcotry>', type=str, + help='The CMake build directory (might be empty)') + + return parser.parse_args() + + +def calculate_baseline(source_directory: str, *, debug: bool=False) -> int: + if debug: + print('Scanning "{}" for qmake-based tests.'.format(source_directory)) + result = subprocess.run('/usr/bin/git grep -E "^\\s*CONFIG\\s*\\+?=.*\\btestcase\\b" | sort -u | wc -l', + shell=True, capture_output=True, cwd=source_directory) + return int(result.stdout) + + +def build(source_directory: str, binary_directory: str, *, debug=False) -> None: + abs_source = os.path.abspath(source_directory) + if not os.path.isdir(binary_directory): + os.makedirs(binary_directory) + if not os.path.exists(os.path.join(binary_directory, 'CMakeCache.txt')): + + if debug: + print('Running cmake in "{}".'.format(binary_directory)) + result = subprocess.run(['/usr/bin/cmake', '-GNinja', abs_source], cwd=binary_directory) + if debug: + print('CMake return code: {}.'.format(result.returncode)) + + assert result.returncode == 0 + + if debug: + print('Running ninja in "{}".'.format(binary_directory)) + result = subprocess.run('/usr/bin/ninja', cwd=binary_directory) + if debug: + print('Ninja return code: {}.'.format(result.returncode)) + + assert result.returncode == 0 + + +def test(binary_directory: str, *, debug=False) -> typing.Tuple[int, int]: + if debug: + print('Running ctest in "{}".'.format(binary_directory)) + result = subprocess.run('/usr/bin/ctest -j 250 | grep "tests passed, "', + shell=True, capture_output=True, cwd=binary_directory) + summary = result.stdout.decode('utf-8').replace('\n', '') + if debug: + print('Test summary: {} ({}).'.format(summary, result.returncode)) + + matches = re.fullmatch(r'\d+% tests passed, (\d+) tests failed out of (\d+)', summary) + if matches: + if debug: + print('Matches: failed {}, total {}.'.format(matches.group(1), matches.group(2))) + return (int(matches.group(2)), int(matches.group(2)) - int(matches.group(1)), ) + + return (0, 0,) + + +def main() -> int: + args = _parse_commandline() + + base_line = calculate_baseline(args.source_directory, debug=args.debug) + if base_line <= 0: + print('Could not find the qmake baseline in {}.'.format(args.source_directory)) + return 1 + + if args.debug: + print('qmake baseline: {} test binaries.'.format(base_line)) + + cmake_total = 0 + cmake_success = 0 + try: + build(args.source_directory, args.binary_directory, debug=args.debug) + (cmake_total, cmake_success, ) = test(args.binary_directory, debug=args.debug) + finally: + if cmake_total == 0: + print('\n\n\nCould not calculate the cmake state.') + return 2 + else: + print('\n\n\nCMake test conversion rate: {:.2%}.'.format(cmake_total / base_line)) + print('CMake test success rate : {:.2%}.'.format(cmake_success / base_line)) + return 0 + + +if __name__ == '__main__': + main() diff --git a/util/cmake/configurejson2cmake.py b/util/cmake/configurejson2cmake.py new file mode 100755 index 0000000000..58ab106555 --- /dev/null +++ b/util/cmake/configurejson2cmake.py @@ -0,0 +1,1039 @@ +#!/usr/bin/env python3 +############################################################################# +## +## Copyright (C) 2018 The Qt Company Ltd. +## Contact: https://www.qt.io/licensing/ +## +## This file is part of the plugins of the Qt Toolkit. +## +## $QT_BEGIN_LICENSE:GPL-EXCEPT$ +## Commercial License Usage +## Licensees holding valid commercial Qt licenses may use this file in +## accordance with the commercial license agreement provided with the +## Software or, alternatively, in accordance with the terms contained in +## a written agreement between you and The Qt Company. For licensing terms +## and conditions see https://www.qt.io/terms-conditions. For further +## information use the contact form at https://www.qt.io/contact-us. +## +## GNU General Public License Usage +## Alternatively, this file may be used under the terms of the GNU +## General Public License version 3 as published by the Free Software +## Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +## included in the packaging of this file. Please review the following +## information to ensure the GNU General Public License requirements will +## be met: https://www.gnu.org/licenses/gpl-3.0.html. +## +## $QT_END_LICENSE$ +## +############################################################################# + +import json +import os.path +import re +import sys +from typing import Set, Union, List, Dict + +from helper import map_qt_library, featureName, substitute_platform + +knownTests = set() # type: Set[str] + + +class LibraryMapping: + def __init__(self, package: str, resultVariable: str, appendFoundSuffix: bool = True) -> None: + self.package = package + self.resultVariable = resultVariable + self.appendFoundSuffix = appendFoundSuffix + + +def map_library(lib: str) -> Union[str, LibraryMapping, List[str]]: + libmap = { + 'atspi': 'ATSPI2', + 'corewlan': None, # Framework + 'cups': 'Cups', + 'double-conversion': None, + 'drm': 'Libdrm', + 'egl': 'EGL', + 'fontconfig': LibraryMapping(package='Fontconfig', resultVariable="FONTCONFIG"), + 'freetype': 'Freetype', + 'gbm': 'gbm', + 'glib': 'GLIB2', + 'gnu_iconv': None, + 'gtk3': 'GTK3', + 'harfbuzz': 'harfbuzz', + 'host_dbus': None, + 'icu': ['ICU', 'COMPONENTS', 'i18n', 'uc', 'data'], + 'journald': 'Libsystemd', + 'libatomic': 'Atomic', + 'libdl': None, # handled by CMAKE_DL_LIBS + 'libinput': 'Libinput', + 'libjpeg': 'JPEG', + 'libpng': 'PNG', + 'libpng': 'PNG', + 'libproxy': 'Libproxy', + 'librt': 'WrapRt', + 'libudev': 'Libudev', + 'lttng-ust': LibraryMapping(package='LTTngUST', resultVariable="LTTNGUST"), + 'mtdev': 'Mtdev', + 'odbc': 'ODBC', + 'opengl': LibraryMapping(package="OpenGL", resultVariable="OpenGL_OpenGL"), + 'openssl': 'OpenSSL', + 'openssl_headers': LibraryMapping(package="OpenSSL", resultVariable="OPENSSL_INCLUDE_DIR", appendFoundSuffix=False), + 'pcre2': ['PCRE2', 'REQUIRED'], + 'posix_iconv': None, + 'pps': 'PPS', + 'psql': 'PostgreSQL', + 'slog2': 'Slog2', + 'sqlite3': 'SQLite3', + 'sun_iconv': None, + 'tslib': 'Tslib', + 'udev': 'Libudev', + 'vulkan': 'Vulkan', + 'wayland_server': 'Wayland', + 'x11sm': LibraryMapping(package="X11", resultVariable="X11_SM"), + 'xcb_glx': LibraryMapping(package="XCB", resultVariable="XCB_GLX"), + 'xcb_render': LibraryMapping(package="XCB", resultVariable="XCB_RENDER"), + 'xcb': ['XCB', '1.9'], + 'xcb_xinput': LibraryMapping(package="XCB", resultVariable="XCB_XINPUT"), + 'xcb_xkb': LibraryMapping(package="XCB", resultVariable="XCB_XKB"), + 'xcb_xlib': 'X11_XCB', + 'xkbcommon': ['XKB', '0.4.1'], + 'xlib': 'X11', + 'xrender': LibraryMapping(package="XCB", resultVariable="XCB_RENDER"), + 'zlib': 'ZLIB', + 'zstd': 'ZSTD', + 'opengl_es2': 'GLESv2', + } # type: Dict[str, Union[str, List[str], LibraryMapping]] + if lib not in libmap: + raise Exception(' XXXX Unknown library "{}".'.format(lib)) + + return libmap[lib] + + +def map_tests(test: str) -> str: + testmap = { + 'c++11': '$<COMPILE_FEATURES:cxx_std_11>', + 'c++14': '$<COMPILE_FEATURES:cxx_std_14>', + 'c++1z': '$<COMPILE_FEATURES:cxx_std_17>', + 'c99': '$<COMPILE_FEATURES:c_std_99>', + 'c11': '$<COMPILE_FEATURES:c_std_11>', + + 'x86SimdAlways': 'ON', # FIXME: Make this actually do a compile test. + + 'aesni': 'TEST_subarch_aes', + 'avx': 'TEST_subarch_avx', + 'avx2': 'TEST_subarch_avx2', + 'avx512f': 'TEST_subarch_avx512f', + 'avx512cd': 'TEST_subarch_avx512cd', + 'avx512dq': 'TEST_subarch_avx512dq', + 'avx512bw': 'TEST_subarch_avx512bw', + 'avx512er': 'TEST_subarch_avx512er', + 'avx512pf': 'TEST_subarch_avx512pf', + 'avx512vl': 'TEST_subarch_avx512vl', + 'avx512ifma': 'TEST_subarch_avx512ifma', + 'avx512vbmi': 'TEST_subarch_avx512vbmi', + 'avx512vbmi2': 'TEST_subarch_avx512vbmi2', + 'avx512vpopcntdq': 'TEST_subarch_avx512vpopcntdq', + 'avx5124fmaps': 'TEST_subarch_avx5124fmaps', + 'avx5124vnniw': 'TEST_subarch_avx5124vnniw', + 'bmi': 'TEST_subarch_bmi', + 'bmi2': 'TEST_subarch_bmi2', + 'cx16': 'TEST_subarch_cx16', + 'f16c': 'TEST_subarch_f16c', + 'fma': 'TEST_subarch_fma', + 'fma4': 'TEST_subarch_fma4', + 'fsgsbase': 'TEST_subarch_fsgsbase', + 'gfni': 'TEST_subarch_gfni', + 'ibt': 'TEST_subarch_ibt', + 'lwp': 'TEST_subarch_lwp', + 'lzcnt': 'TEST_subarch_lzcnt', + 'mmx': 'TEST_subarch_mmx', + 'movbe': 'TEST_subarch_movbe', + 'mpx': 'TEST_subarch_mpx', + 'no-sahf': 'TEST_subarch_no_shaf', + 'pclmul': 'TEST_subarch_pclmul', + 'popcnt': 'TEST_subarch_popcnt', + 'prefetchwt1': 'TEST_subarch_prefetchwt1', + 'prfchw': 'TEST_subarch_prfchw', + 'pdpid': 'TEST_subarch_rdpid', + 'rdpid': 'TEST_subarch_rdpid', + 'rdseed': 'TEST_subarch_rdseed', + 'rdrnd': 'TEST_subarch_rdseed', # FIXME: Is this the right thing? + 'rtm': 'TEST_subarch_rtm', + 'shani': 'TEST_subarch_sha', + 'shstk': 'TEST_subarch_shstk', + 'sse2': 'TEST_subarch_sse2', + 'sse3': 'TEST_subarch_sse3', + 'ssse3': 'TEST_subarch_ssse3', + 'sse4a': 'TEST_subarch_sse4a', + 'sse4_1': 'TEST_subarch_sse4_1', + 'sse4_2': 'TEST_subarch_sse4_2', + 'tbm': 'TEST_subarch_tbm', + 'xop': 'TEST_subarch_xop', + + 'neon': 'TEST_subarch_neon', + 'iwmmxt': 'TEST_subarch_iwmmxt', + 'crc32': 'TEST_subarch_crc32', + + 'vis': 'TEST_subarch_vis', + 'vis2': 'TEST_subarch_vis2', + 'vis3': 'TEST_subarch_vis3', + + 'dsp': 'TEST_subarch_dsp', + 'dspr2': 'TEST_subarch_dspr2', + + 'altivec': 'TEST_subarch_altivec', + 'spe': 'TEST_subarch_spe', + 'vsx': 'TEST_subarch_vsx', + + 'posix-iconv': 'TEST_posix_iconv', + 'sun-iconv': 'TEST_sun_iconv', + + 'openssl11': '(OPENSSL_VERSION VERSION_GREATER_EQUAL "1.1.0")', + + 'reduce_exports': 'CMAKE_CXX_COMPILE_OPTIONS_VISIBILITY', + + 'libinput_axis_api': 'ON', + "xlib": "X11_FOUND", + } + if test in testmap: + return testmap.get(test, None) + if test in knownTests: + return 'TEST_{}'.format(featureName(test)) + return None + + +def cm(ctx, *output): + txt = ctx['output'] + if txt != '' and not txt.endswith('\n'): + txt += '\n' + txt += '\n'.join(output) + + ctx['output'] = txt + return ctx + + +def readJsonFromDir(dir): + path = os.path.join(dir, 'configure.json') + + print('Reading {}...'.format(path)) + assert os.path.exists(path) + + with open(path, 'r') as fh: + return json.load(fh) + + +def processFiles(ctx, data): + print(' files:') + if 'files' in data: + for (k, v) in data['files'].items(): + ctx[k] = v + return ctx + +def parseLib(ctx, lib, data, cm_fh, cmake_find_packages_set): + extra = [] + try: + newlib = map_library(lib) + if isinstance(newlib, list): + extra = newlib[1:] + newlib = newlib[0] + elif isinstance(newlib, LibraryMapping): + newlib = newlib.package + except Exception: + return ctx + + if newlib is None: + print(' **** Skipping library "{}" -- was masked.'.format(lib)) + return + + print(' mapped library {} to {}.'.format(lib, newlib)) + + # Avoid duplicate find_package calls. + if newlib in cmake_find_packages_set: + return + + cmake_find_packages_set.add(newlib) + + isRequired = False + + if extra: + if "REQUIRED" in extra: + isRequired = True + extra.remove("REQUIRED") + + if extra: + cm_fh.write('find_package({} {})\n'.format(newlib, ' '.join(extra))) + else: + cm_fh.write('find_package({})\n'.format(newlib)) + + cm_fh.write('set_package_properties({} PROPERTIES TYPE {})\n' + .format(newlib, 'REQUIRED' if isRequired else 'OPTIONAL') + ) + +def lineify(label, value, quote=True): + if value: + if quote: + return ' {} "{}"\n'.format(label, value.replace('"', '\\"')) + return ' {} {}\n'.format(label, value) + return '' + +def map_condition(condition): + # Handle NOT: + if isinstance(condition, list): + condition = '(' + ') AND ('.join(condition) + ')' + if isinstance(condition, bool): + if condition: + return 'ON' + else: + return 'OFF' + assert isinstance(condition, str) + + mapped_features = { + 'gbm': 'gbm_FOUND', + "system-xcb": "ON", + "system-freetype": "ON", + 'system-pcre2': 'ON', + } + + # Turn foo != "bar" into (NOT foo STREQUAL 'bar') + condition = re.sub(r"(.+)\s*!=\s*('.+')", '(! \\1 == \\2)', condition) + + condition = condition.replace('!', 'NOT ') + condition = condition.replace('&&', ' AND ') + condition = condition.replace('||', ' OR ') + condition = condition.replace('==', ' STREQUAL ') + + # explicitly handle input.sdk == '': + condition = re.sub(r"input\.sdk\s*==\s*''", 'NOT INPUT_SDK', condition) + + last_pos = 0 + mapped_condition = '' + has_failed = False + for match in re.finditer(r'([a-zA-Z0-9_]+)\.([a-zA-Z0-9_+-]+)', condition): + substitution = None + appendFoundSuffix = True + if match.group(1) == 'libs': + try: + substitution = map_library(match.group(2)) + if isinstance(substitution, list): + substitution = substitution[0] + elif isinstance(substitution, LibraryMapping): + appendFoundSuffix = substitution.appendFoundSuffix + substitution = substitution.resultVariable + except Exception: + substitution = None + + if substitution is not None and appendFoundSuffix: + substitution += '_FOUND' + + elif match.group(1) == 'features': + feature = match.group(2) + if feature in mapped_features: + substitution = mapped_features.get(feature) + else: + substitution = 'QT_FEATURE_{}'.format(featureName(match.group(2))) + + elif match.group(1) == 'subarch': + substitution = 'TEST_arch_{}_subarch_{}'.format("${TEST_architecture_arch}", + match.group(2)) + + elif match.group(1) == 'call': + if match.group(2) == 'crossCompile': + substitution = 'CMAKE_CROSSCOMPILING' + + elif match.group(1) == 'tests': + substitution = map_tests(match.group(2)) + + elif match.group(1) == 'input': + substitution = 'INPUT_{}'.format(featureName(match.group(2))) + + elif match.group(1) == 'config': + substitution = substitute_platform(match.group(2)) + + elif match.group(1) == 'arch': + if match.group(2) == 'i386': + # FIXME: Does this make sense? + substitution = '(TEST_architecture_arch STREQUAL i386)' + elif match.group(2) == 'x86_64': + substitution = '(TEST_architecture_arch STREQUAL x86_64)' + elif match.group(2) == 'arm': + # FIXME: Does this make sense? + substitution = '(TEST_architecture_arch STREQUAL arm)' + elif match.group(2) == 'arm64': + # FIXME: Does this make sense? + substitution = '(TEST_architecture_arch STREQUAL arm64)' + elif match.group(2) == 'mips': + # FIXME: Does this make sense? + substitution = '(TEST_architecture_arch STREQUAL mips)' + + if substitution is None: + print(' XXXX Unknown condition "{}".'.format(match.group(0))) + has_failed = True + else: + mapped_condition += condition[last_pos:match.start(1)] + substitution + last_pos = match.end(2) + + mapped_condition += condition[last_pos:] + + # Space out '(' and ')': + mapped_condition = mapped_condition.replace('(', ' ( ') + mapped_condition = mapped_condition.replace(')', ' ) ') + + # Prettify: + condition = re.sub('\\s+', ' ', mapped_condition) + condition = condition.strip() + + if has_failed: + condition += ' OR FIXME' + + return condition + + +def parseInput(ctx, input, data, cm_fh): + skip_inputs = { + "prefix", "hostprefix", "extprefix", + + "archdatadir", "bindir", "datadir", "docdir", + "examplesdir", "external-hostbindir", "headerdir", + "hostbindir", "hostdatadir", "hostlibdir", + "importdir", "libdir", "libexecdir", + "plugindir", "qmldir", "settingsdir", + "sysconfdir", "testsdir", "translationdir", + + "android-arch", "android-ndk", "android-ndk-host", + "android-ndk-platform", "android-sdk", + "android-toolchain-version", "android-style-assets", + + "appstore-compliant", + + "avx", "avx2", "avx512", "c++std", "ccache", "commercial", + "compile-examples", "confirm-license", + "dbus", + "dbus-runtime", + + "debug", "debug-and-release", + + "developer-build", + + "device", "device-option", + + "f16c", + + "force-asserts", "force-debug-info", "force-pkg-config", + "framework", + + "gc-binaries", + + "gdb-index", + + "gcc-sysroot", + + "gcov", + + "gnumake", + + "gui", + + "harfbuzz", + + "headersclean", + + "incredibuild-xge", + + "libudev", + "ltcg", + "make", + "make-tool", + + "mips_dsp", + "mips_dspr2", + "mp", + + "nomake", + + "opensource", + + "optimize-debug", "optimize-size", "optimized-qmake", "optimized-tools", + + "pch", + + "pkg-config", + + "platform", + + "plugin-manifests", + "profile", + "qreal", + + "reduce-exports", "reduce-relocations", + + "release", + + "rpath", + + "sanitize", + + "sdk", + + "separate-debug-info", + + "shared", + + "silent", + + "qdbus", + + "sse2", + "sse3", + "sse4.1", + "sse4.2", + "ssse3", + "static", + "static-runtime", + "strip", + "syncqt", + "sysroot", + "testcocoon", + "use-gold-linker", + "warnings-are-errors", + "Werror", + "widgets", + "xplatform", + "zlib", + + "doubleconversion", + + "eventfd", + "glib", + "icu", + "inotify", + "journald", + "pcre", + "posix-ipc", + "pps", + "slog2", + "syslog", + + "sqlite", + } + + if input in skip_inputs: + print(' **** Skipping input {}: masked.'.format(input)) + return + + type = data + if isinstance(data, dict): + type = data["type"] + + if type == "boolean": + print(' **** Skipping boolean input {}: masked.'.format(input)) + return + + if type == "enum": + cm_fh.write("# input {}\n".format(input)) + cm_fh.write('set(INPUT_{} "undefined" CACHE STRING "")\n'.format(featureName(input))) + cm_fh.write('set_property(CACHE INPUT_{} PROPERTY STRINGS undefined {})\n\n'.format(featureName(input), " ".join(data["values"]))) + return + + print(' XXXX UNHANDLED INPUT TYPE {} in input description'.format(type)) + return + + +# "tests": { +# "cxx11_future": { +# "label": "C++11 <future>", +# "type": "compile", +# "test": { +# "include": "future", +# "main": [ +# "std::future<int> f = std::async([]() { return 42; });", +# "(void)f.get();" +# ], +# "qmake": "unix:LIBS += -lpthread" +# } +# }, +def parseTest(ctx, test, data, cm_fh): + skip_tests = { + 'c++11', 'c++14', 'c++1y', 'c++1z', + 'c11', 'c99', + 'gc_binaries', + 'posix-iconv', "sun-iconv", + 'precomile_header', + 'reduce_exports', + 'separate_debug_info', # FIXME: see if cmake can do this + 'gc_binaries', + 'libinput_axis_api', + 'xlib', + } + + if test in skip_tests: + print(' **** Skipping features {}: masked.'.format(test)) + return + + if data["type"] == "compile": + knownTests.add(test) + + details = data["test"] + + if isinstance(details, str): + print(' XXXX UNHANDLED TEST SUB-TYPE {} in test description'.format(details)) + return + + head = details.get("head", "") + if isinstance(head, list): + head = "\n".join(head) + + sourceCode = head + '\n' + + include = details.get("include", "") + if isinstance(include, list): + include = '#include <' + '>\n#include <'.join(include) + '>' + elif include: + include = '#include <{}>'.format(include) + + sourceCode += include + '\n' + + tail = details.get("tail", "") + if isinstance(tail, list): + tail = "\n".join(tail) + + sourceCode += tail + '\n' + + sourceCode += "int main(int argc, char **argv)\n" + sourceCode += "{\n" + sourceCode += " (void)argc; (void)argv;\n" + sourceCode += " /* BEGIN TEST: */\n" + + main = details.get("main", "") + if isinstance(main, list): + main = "\n".join(main) + + sourceCode += main + '\n' + + sourceCode += " /* END TEST: */\n" + sourceCode += " return 0;\n" + sourceCode += "}\n" + + sourceCode = sourceCode.replace('"', '\\"') + + librariesCmakeName = "" + qmakeFixme = "" + + cm_fh.write("# {}\n".format(test)) + if "qmake" in details: # We don't really have many so we can just enumerate them all + if details["qmake"] == "unix:LIBS += -lpthread": + librariesCmakeName = format(featureName(test)) + "_TEST_LIBRARIES" + cm_fh.write("if (UNIX)\n") + cm_fh.write(" set(" + librariesCmakeName + " pthread)\n") + cm_fh.write("endif()\n") + elif details["qmake"] == "linux: LIBS += -lpthread -lrt": + librariesCmakeName = format(featureName(test)) + "_TEST_LIBRARIES" + cm_fh.write("if (LINUX)\n") + cm_fh.write(" set(" + librariesCmakeName + " pthread rt)\n") + cm_fh.write("endif()\n") + elif details["qmake"] == "CONFIG += c++11": + # do nothing we're always in c++11 mode + pass + else: + qmakeFixme = "# FIXME: qmake: {}\n".format(details["qmake"]) + + if "use" in data: + if data["use"] == "egl xcb_xlib": + librariesCmakeName = format(featureName(test)) + "_TEST_LIBRARIES" + cm_fh.write("if (HAVE_EGL AND X11_XCB_FOUND AND X11_FOUND)\n") + cm_fh.write(" set(" + librariesCmakeName + " EGL::EGL X11::X11 X11::XCB)\n") + cm_fh.write("endif()\n") + else: + qmakeFixme += "# FIXME: use: {}\n".format(data["use"]) + + cm_fh.write("qt_config_compile_test({}\n".format(featureName(test))) + cm_fh.write(lineify("LABEL", data.get("label", ""))) + if librariesCmakeName != "": + cm_fh.write(lineify("LIBRARIES", "${"+librariesCmakeName+"}")) + cm_fh.write(" CODE\n") + cm_fh.write('"' + sourceCode + '"') + if qmakeFixme != "": + cm_fh.write(qmakeFixme) + cm_fh.write(")\n\n") + + elif data["type"] == "x86Simd": + knownTests.add(test) + + label = data["label"] + + cm_fh.write("# {}\n".format(test)) + cm_fh.write("qt_config_compile_test_x86simd({} \"{}\")\n".format(test, label)) + cm_fh.write("\n") + +# "features": { +# "android-style-assets": { +# "label": "Android Style Assets", +# "condition": "config.android", +# "output": [ "privateFeature" ], +# "comment": "This belongs into gui, but the license check needs it here already." +# }, + else: + print(' XXXX UNHANDLED TEST TYPE {} in test description'.format(data["type"])) + + +def parseFeature(ctx, feature, data, cm_fh): + # This is *before* the feature name gets normalized! So keep - and + chars, etc. + feature_mapping = { + 'alloc_h': None, # handled by alloc target + 'alloc_malloc_h': None, + 'alloc_stdlib_h': None, + 'build_all': None, + 'c++11': None, # C and C++ versions + 'c11': None, + 'c++14': None, + 'c++1y': None, + 'c++1z': None, + 'c89': None, + 'c99': None, + 'ccache': None, + 'compiler-flags': None, + 'cross_compile': None, + 'debug_and_release': None, + 'debug': None, + 'dlopen': { + 'condition': 'UNIX', + }, + 'doubleconversion': None, + 'enable_gdb_index': None, + 'enable_new_dtags': None, + 'force_debug_info': None, + 'framework': { + 'condition': 'APPLE AND BUILD_SHARED_LIBS', + }, + 'gc_binaries': None, + 'gcc-sysroot': None, + 'gcov': None, + 'gnu-libiconv': { + 'condition': 'NOT WIN32 AND NOT QNX AND NOT ANDROID AND NOT APPLE AND TEST_posix_iconv AND NOT TEST_iconv_needlib', + 'enable': 'TEST_posix_iconv AND NOT TEST_iconv_needlib', + 'disable': 'NOT TEST_posix_iconv OR TEST_iconv_needlib', + }, + 'GNUmake': None, + 'harfbuzz': { + 'condition': 'HARFBUZZ_FOUND' + }, + 'host-dbus': None, + 'iconv': { + 'condition': 'NOT QT_FEATURE_icu AND QT_FEATURE_textcodec AND ( TEST_posix_iconv OR TEST_sun_iconv )' + }, + 'incredibuild_xge': None, + 'jpeg': { + 'condition': 'QT_FEATURE_imageformatplugin AND JPEG_FOUND' + }, + 'ltcg': None, + 'msvc_mp': None, + 'optimize_debug': None, + 'optimize_size': None, + # special case to enable implicit feature on WIN32, until ANGLE is ported + 'opengl-desktop': { + 'autoDetect': '' + }, + # special case to disable implicit feature on WIN32, until ANGLE is ported + 'opengl-dynamic': { + 'autoDetect': 'OFF' + }, + 'opengles2': { # special case to disable implicit feature on WIN32, until ANGLE is ported + 'condition': 'NOT WIN32 AND ( NOT APPLE_WATCHOS AND NOT QT_FEATURE_opengl_desktop AND GLESv2_FOUND )' + }, + 'pkg-config': None, + 'posix_fallocate': None, # Only needed for sqlite, which we do not want to build + 'posix-libiconv': { + 'condition': 'NOT WIN32 AND NOT QNX AND NOT ANDROID AND NOT APPLE AND TEST_posix_iconv AND TEST_iconv_needlib', + 'enable': 'TEST_posix_iconv AND TEST_iconv_needlib', + 'disable': 'NOT TEST_posix_iconv OR NOT TEST_iconv_needlib', + }, + 'precompile_header': None, + 'profile': None, + 'qmakeargs': None, + 'qpa_default_platform': None, # Not a bool! + 'reduce_relocations': None, + 'release': None, + 'release_tools': None, + 'rpath_dir': None, # rpath related + 'rpath': None, + 'sanitize_address': None, # sanitizer + 'sanitize_memory': None, + 'sanitizer': None, + 'sanitize_thread': None, + 'sanitize_undefined': None, + 'separate_debug_info': None, + 'shared': None, + 'silent': None, + 'sql-sqlite' : { + 'condition': 'QT_FEATURE_datestring AND SQLite3_FOUND', + }, + 'stack-protector-strong': None, + 'static': None, + 'static_runtime': None, + 'stl': None, # Do we really need to test for this in 2018?! + 'strip': None, + 'sun-libiconv': { + 'condition': 'NOT WIN32 AND NOT QNX AND NOT ANDROID AND NOT APPLE AND TEST_sun_iconv', + 'enable': 'TEST_sun_iconv', + 'disable': 'NOT TEST_sun_iconv', + }, + 'system-doubleconversion': None, # No system libraries anymore! + 'system-freetype': None, + 'system-harfbuzz': None, + 'system-jpeg': None, + 'system-pcre2': None, + 'system-png': None, + 'system-sqlite': None, + 'system-xcb': None, + 'system-zlib': None, + 'use_gold_linker': None, + 'verifyspec': None, # qmake specific... + 'warnings_are_errors': None, # FIXME: Do we need these? + 'xkbcommon-system': None, # another system library, just named a bit different from the rest + } + + mapping = feature_mapping.get(feature, {}) + + if mapping is None: + print(' **** Skipping features {}: masked.'.format(feature)) + return + + handled = { 'autoDetect', 'comment', 'condition', 'description', 'disable', 'emitIf', 'enable', 'label', 'output', 'purpose', 'section' } + label = mapping.get('label', data.get('label', '')) + purpose = mapping.get('purpose', data.get('purpose', data.get('description', label))) + autoDetect = map_condition(mapping.get('autoDetect', data.get('autoDetect', ''))) + condition = map_condition(mapping.get('condition', data.get('condition', ''))) + output = mapping.get('output', data.get('output', [])) + comment = mapping.get('comment', data.get('comment', '')) + section = mapping.get('section', data.get('section', '')) + enable = map_condition(mapping.get('enable', data.get('enable', ''))) + disable = map_condition(mapping.get('disable', data.get('disable', ''))) + emitIf = map_condition(mapping.get('emitIf', data.get('emitIf', ''))) + + for k in [k for k in data.keys() if k not in handled]: + print(' XXXX UNHANDLED KEY {} in feature description'.format(k)) + + if not output: + # feature that is only used in the conditions of other features + output = ["internalFeature"] + + publicFeature = False # #define QT_FEATURE_featurename in public header + privateFeature = False # #define QT_FEATURE_featurename in private header + negativeFeature = False # #define QT_NO_featurename in public header + internalFeature = False # No custom or QT_FEATURE_ defines + publicDefine = False # #define MY_CUSTOM_DEFINE in public header + + for o in output: + outputType = o + outputArgs = {} + if isinstance(o, dict): + outputType = o['type'] + outputArgs = o + + if outputType in ['varAssign', 'varAppend', 'varRemove', 'publicQtConfig', 'privateConfig', 'publicConfig']: + continue + elif outputType == 'define': + publicDefine = True + elif outputType == 'feature': + negativeFeature = True + elif outputType == 'publicFeature': + publicFeature = True + elif outputType == 'privateFeature': + privateFeature = True + elif outputType == 'internalFeature': + internalFeature = True + else: + print(' XXXX UNHANDLED OUTPUT TYPE {} in feature {}.'.format(outputType, feature)) + continue + + if not any([publicFeature, privateFeature, internalFeature, publicDefine, negativeFeature]): + print(' **** Skipping feature {}: Not relevant for C++.'.format(feature)) + return + + cxxFeature = featureName(feature) + + def writeFeature(name, publicFeature=False, privateFeature=False, labelAppend=''): + if comment: + cm_fh.write('# {}\n'.format(comment)) + + cm_fh.write('qt_feature("{}"'.format(name)) + if publicFeature: + cm_fh.write(' PUBLIC') + if privateFeature: + cm_fh.write(' PRIVATE') + cm_fh.write('\n') + + cm_fh.write(lineify('SECTION', section)) + cm_fh.write(lineify('LABEL', label + labelAppend)) + if purpose != label: + cm_fh.write(lineify('PURPOSE', purpose)) + cm_fh.write(lineify('AUTODETECT', autoDetect, quote=False)) + cm_fh.write(lineify('CONDITION', condition, quote=False)) + cm_fh.write(lineify('ENABLE', enable, quote=False)) + cm_fh.write(lineify('DISABLE', disable, quote=False)) + cm_fh.write(lineify('EMIT_IF', emitIf, quote=False)) + cm_fh.write(')\n') + + # Write qt_feature() calls before any qt_feature_definition() calls + + # Default internal feature case. + featureCalls = {} + featureCalls[cxxFeature] = {'name': cxxFeature, 'labelAppend': ''} + + # Go over all outputs to compute the number of features that have to be declared + for o in output: + outputType = o + name = cxxFeature + + # The label append is to provide a unique label for features that have more than one output + # with different names. + labelAppend = '' + + if isinstance(o, dict): + outputType = o['type'] + if 'name' in o: + name = o['name'] + labelAppend = ': {}'.format(o['name']) + + if outputType not in ['feature', 'publicFeature', 'privateFeature']: + continue + if name not in featureCalls: + featureCalls[name] = {'name': name, 'labelAppend': labelAppend} + + if outputType in ['feature', 'publicFeature']: + featureCalls[name]['publicFeature'] = True + elif outputType == 'privateFeature': + featureCalls[name]['privateFeature'] = True + + # Write the qt_feature() calls from the computed feature map + for _, args in featureCalls.items(): + writeFeature(**args) + + # Write qt_feature_definition() calls + for o in output: + outputType = o + outputArgs = {} + if isinstance(o, dict): + outputType = o['type'] + outputArgs = o + + # Map negative feature to define: + if outputType == 'feature': + outputType = 'define' + outputArgs = {'name': 'QT_NO_{}'.format(cxxFeature.upper()), + 'negative': True, + 'value': 1, + 'type': 'define'} + + if outputType != 'define': + continue + + if outputArgs.get('name') is None: + print(' XXXX DEFINE output without name in feature {}.'.format(feature)) + continue + + cm_fh.write('qt_feature_definition("{}" "{}"'.format(cxxFeature, outputArgs.get('name'))) + if outputArgs.get('negative', False): + cm_fh.write(' NEGATE') + if outputArgs.get('value') is not None: + cm_fh.write(' VALUE "{}"'.format(outputArgs.get('value'))) + cm_fh.write(')\n') + + +def processInputs(ctx, data, cm_fh): + print(' inputs:') + if 'commandline' not in data: + return + + commandLine = data['commandline'] + if "options" not in commandLine: + return + + for input in commandLine['options']: + parseInput(ctx, input, commandLine['options'][input], cm_fh) + + +def processTests(ctx, data, cm_fh): + print(' tests:') + if 'tests' not in data: + return + + for test in data['tests']: + parseTest(ctx, test, data['tests'][test], cm_fh) + + +def processFeatures(ctx, data, cm_fh): + print(' features:') + if 'features' not in data: + return + + for feature in data['features']: + parseFeature(ctx, feature, data['features'][feature], cm_fh) + + +def processLibraries(ctx, data, cm_fh): + cmake_find_packages_set = set() + print(' libraries:') + if 'libraries' not in data: + return + + for lib in data['libraries']: + parseLib(ctx, lib, data['libraries'][lib], cm_fh, cmake_find_packages_set) + + +def processSubconfigs(dir, ctx, data): + assert ctx is not None + if 'subconfigs' in data: + for subconf in data['subconfigs']: + subconfDir = os.path.join(dir, subconf) + subconfData = readJsonFromDir(subconfDir) + subconfCtx = ctx + processJson(subconfDir, subconfCtx, subconfData) + + +def processJson(dir, ctx, data): + ctx['module'] = data.get('module', 'global') + + ctx = processFiles(ctx, data) + + with open(os.path.join(dir, "configure.cmake"), 'w') as cm_fh: + cm_fh.write("\n\n#### Inputs\n\n") + + processInputs(ctx, data, cm_fh) + + cm_fh.write("\n\n#### Libraries\n\n") + + processLibraries(ctx, data, cm_fh) + + cm_fh.write("\n\n#### Tests\n\n") + + processTests(ctx, data, cm_fh) + + cm_fh.write("\n\n#### Features\n\n") + + processFeatures(ctx, data, cm_fh) + + if ctx.get('module') == 'global': + cm_fh.write('\nqt_extra_definition("QT_VERSION_STR" "\\\"${PROJECT_VERSION}\\\"" PUBLIC)\n') + cm_fh.write('qt_extra_definition("QT_VERSION_MAJOR" ${PROJECT_VERSION_MAJOR} PUBLIC)\n') + cm_fh.write('qt_extra_definition("QT_VERSION_MINOR" ${PROJECT_VERSION_MINOR} PUBLIC)\n') + cm_fh.write('qt_extra_definition("QT_VERSION_PATCH" ${PROJECT_VERSION_PATCH} PUBLIC)\n') + + # do this late: + processSubconfigs(dir, ctx, data) + + +def main(): + if len(sys.argv) != 2: + print("This scripts needs one directory to process!") + quit(1) + + dir = sys.argv[1] + + print("Processing: {}.".format(dir)) + + data = readJsonFromDir(dir) + processJson(dir, {}, data) + + +if __name__ == '__main__': + main() diff --git a/util/cmake/generate_module_map.sh b/util/cmake/generate_module_map.sh new file mode 100755 index 0000000000..1ca0bfc43c --- /dev/null +++ b/util/cmake/generate_module_map.sh @@ -0,0 +1,38 @@ +#!/usr/bin/bash +############################################################################# +## +## Copyright (C) 2018 The Qt Company Ltd. +## Contact: https://www.qt.io/licensing/ +## +## This file is part of the plugins of the Qt Toolkit. +## +## $QT_BEGIN_LICENSE:GPL-EXCEPT$ +## Commercial License Usage +## Licensees holding valid commercial Qt licenses may use this file in +## accordance with the commercial license agreement provided with the +## Software or, alternatively, in accordance with the terms contained in +## a written agreement between you and The Qt Company. For licensing terms +## and conditions see https://www.qt.io/terms-conditions. For further +## information use the contact form at https://www.qt.io/contact-us. +## +## GNU General Public License Usage +## Alternatively, this file may be used under the terms of the GNU +## General Public License version 3 as published by the Free Software +## Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +## included in the packaging of this file. Please review the following +## information to ensure the GNU General Public License requirements will +## be met: https://www.gnu.org/licenses/gpl-3.0.html. +## +## $QT_END_LICENSE$ +## +############################################################################# + +pro_files=$(find . -name \*.pro) + +for f in ${pro_files}; do + if grep "^load(qt_module)" "${f}" > /dev/null ; then + target=$(grep "TARGET" "${f}" | cut -d'=' -f2 | sed -e "s/\s*//g") + module=$(basename ${f}) + echo "'${module%.pro}': '${target}'," + fi +done diff --git a/util/cmake/helper.py b/util/cmake/helper.py new file mode 100644 index 0000000000..13c1677b52 --- /dev/null +++ b/util/cmake/helper.py @@ -0,0 +1,272 @@ +############################################################################# +## +## Copyright (C) 2018 The Qt Company Ltd. +## Contact: https://www.qt.io/licensing/ +## +## This file is part of the plugins of the Qt Toolkit. +## +## $QT_BEGIN_LICENSE:GPL-EXCEPT$ +## Commercial License Usage +## Licensees holding valid commercial Qt licenses may use this file in +## accordance with the commercial license agreement provided with the +## Software or, alternatively, in accordance with the terms contained in +## a written agreement between you and The Qt Company. For licensing terms +## and conditions see https://www.qt.io/terms-conditions. For further +## information use the contact form at https://www.qt.io/contact-us. +## +## GNU General Public License Usage +## Alternatively, this file may be used under the terms of the GNU +## General Public License version 3 as published by the Free Software +## Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +## included in the packaging of this file. Please review the following +## information to ensure the GNU General Public License requirements will +## be met: https://www.gnu.org/licenses/gpl-3.0.html. +## +## $QT_END_LICENSE$ +## +############################################################################# + +import re + + +def featureName(input: str) -> str: + return re.sub(r'[^a-zA-Z0-9_]', '_', input) + + +def map_qt_base_library(lib: str) -> str: + library_map = { + 'global': 'Qt::Core', # manually added special case + 'accessibility_support': 'Qt::AccessibilitySupport', + 'androidextras': 'Qt::AndroidExtras', + 'animation': 'Qt::3DAnimation', + 'application-lib': 'Qt::AppManApplication', + 'bluetooth': 'Qt::Bluetooth', + 'bootstrap-dbus': 'Qt::BootstrapDBus', + 'bootstrap': 'Qt::Bootstrap', + 'client': 'Qt::WaylandClient', + 'clipboard_support': 'Qt::ClipboardSupport', + 'common-lib': 'Qt::AppManCommon', + 'compositor': 'Qt::WaylandCompositor', + 'concurrent': 'Qt::Concurrent', + 'container': 'Qt::AxContainer', + 'control': 'Qt::AxServer', + 'core_headers': 'Qt::WebEngineCore', + 'core': 'Qt::Core', + 'coretest': 'Qt::3DCoreTest', + 'crypto-lib': 'Qt::AppManCrypto', + 'dbus': 'Qt::DBus', + 'devicediscovery': 'Qt::DeviceDiscoverySupport', + 'devicediscovery_support': 'Qt::DeviceDiscoverySupport', + 'edid': 'Qt::EdidSupport', + 'eglconvenience': 'Qt::EglSupport', + 'eglfsdeviceintegration': 'Qt::EglFSDeviceIntegration', + 'eglfs_kms_support': 'Qt::EglFsKmsSupport', + 'egl_support': 'Qt::EglSupport', + 'enginio_client': 'Enginio', + 'eventdispatchers': 'Qt::EventDispatcherSupport', + 'extras': 'Qt::3DExtras', + 'fb_support': 'Qt::FbSupport', + 'fbconvenience': 'Qt::FbSupport', + 'fontdatabase_support': 'Qt::FontDatabaseSupport', + 'gamepad': 'Qt::Gamepad', + 'glx_support': 'Qt::GlxSupport', + 'graphics_support': 'Qt::GraphicsSupport', + 'gsttools': 'Qt::MultimediaGstTools', + 'gui': 'Qt::Gui', + 'help': 'Qt::Help', + 'hunspellinputmethod': 'Qt::HunspellInputMethod', + 'input': 'Qt::InputSupport', + 'input_support': 'Qt::InputSupport', + 'installer-lib': 'Qt::AppManInstaller', + 'kmsconvenience': 'Qt::KmsSupport', + 'kms_support': 'Qt::KmsSupport', + 'launcher-lib': 'Qt::AppManLauncher', + 'lib': 'Qt::Designer', + 'linuxaccessibility_support': 'Qt::LinuxAccessibilitySupport', + 'location': 'Qt::Location', + 'logic': 'Qt::3DLogic', + 'macextras': 'Qt::MacExtras', + 'main-lib': 'Qt::AppManMain', + 'manager-lib': 'Qt::AppManManager', + 'monitor-lib': 'Qt::AppManMonitor', + 'multimedia': 'Qt::Multimedia', + 'multimediawidgets': 'Qt::MultimediaWidgets', + 'network': 'Qt::Network', + 'nfc': 'Qt::Nfc', + 'oauth': 'Qt::NetworkAuth', + 'openglextensions': 'Qt::OpenGLExtensions', + 'opengl': 'Qt::OpenGL', + 'package-lib': 'Qt::AppManPackage', + 'packetprotocol': 'Qt::PacketProtocol', + 'particles': 'Qt::QuickParticles', + 'platformcompositor': 'Qt::PlatformCompositorSupport', + 'platformcompositor_support': 'Qt::PlatformCompositorSupport', + 'plugin-interfaces': 'Qt::AppManPluginInterfaces', + 'positioning': 'Qt::Positioning', + 'positioningquick': 'Qt::PositioningQuick', + 'printsupport': 'Qt::PrintSupport', + 'purchasing': 'Qt::Purchasing', + 'qmldebug': 'Qt::QmlDebug', + 'qmldevtools': 'Qt::QmlDevTools', + 'qml': 'Qt::Qml', + 'qmltest': 'Qt::QuickTest', + 'qtmultimediaquicktools': 'Qt::MultimediaQuick', + 'qtzlib': 'Qt::Zlib', + 'quick3danimation': 'Qt::3DQuickAnimation', + 'quick3dextras': 'Qt::3DQuickExtras', + 'quick3dinput': 'Qt::3DQuickInput', + 'quick3d': 'Qt::3DQuick', + 'quick3drender': 'Qt::3DQuickRender', + 'quick3dscene2d': 'Qt::3DQuickScene2D', + 'quickcontrols2': 'Qt::QuickControls2', + 'quick': 'Qt::Quick', + 'quickshapes': 'Qt::QuickShapes', + 'quicktemplates2': 'Qt::QuickTemplates2', + 'quickwidgets': 'Qt::QuickWidgets', + 'render': 'Qt::3DRender', + 'script': 'Qt::Script', + 'scripttools': 'Qt::ScriptTools', + 'sensors': 'Qt::Sensors', + 'serialport': 'Qt::SerialPort', + 'services': 'Qt::ServiceSupport', + 'sql': 'Qt::Sql', + 'svg': 'Qt::Svg', + 'testlib': 'Qt::Test', + 'theme_support': 'Qt::ThemeSupport', + 'service_support': 'Qt::ServiceSupport', + 'eventdispatcher_support': 'Qt::EventDispatcherSupport', + 'edid_support': 'Qt::EdidSupport', + 'tts': 'Qt::TextToSpeech', + 'uiplugin': 'Qt::UiPlugin', + 'uitools': 'Qt::UiTools', + 'virtualkeyboard': 'Qt::VirtualKeyboard', + 'vulkan_support': 'Qt::VulkanSupport', + 'webchannel': 'Qt::WebChannel', + 'webengine': 'Qt::WebEngine', + 'webenginewidgets': 'Qt::WebEngineWidgets', + 'websockets': 'Qt::WebSockets', + 'webview': 'Qt::WebView', + 'widgets': 'Qt::Widgets', + 'window-lib': 'Qt::AppManWindow', + 'windowsuiautomation_support': 'Qt::WindowsUIAutomationSupport', + 'winextras': 'Qt::WinExtras', + 'x11extras': 'Qt::X11Extras', + 'xcb_qpa_lib': 'Qt::XcbQpa', + 'xkbcommon_support': 'Qt::XkbCommonSupport', + 'xmlpatterns': 'Qt::XmlPatterns', + 'xml': 'Qt::Xml', + } + return library_map.get(lib, lib) + + +def map_qt_library(lib: str) -> str: + private = False + if lib.endswith('-private'): + private = True + lib = lib[:-8] + mapped = map_qt_base_library(lib) + if private: + mapped += 'Private' + return mapped + + +platform_mapping = { + 'win32': 'WIN32', + 'unix': 'UNIX', + 'darwin': 'APPLE', + 'linux': 'LINUX', + 'integrity': 'INTEGRITY', + 'qnx': 'QNX', + 'vxworks': 'VXWORKS', + 'hpux': 'HPUX', + 'nacl': 'NACL', + 'android': 'ANDROID', + 'android-embedded': 'ANDROID_EMBEDDED', + 'uikit': 'APPLE_UIKIT', + 'tvos': 'APPLE_TVOS', + 'watchos': 'APPLE_WATCHOS', + 'winrt': 'WINRT', + 'wasm': 'WASM', + 'msvc': 'MSVC', + 'clang': 'CLANG', + 'gcc': 'GCC', + 'icc': 'ICC', + 'intel_icc': 'ICC', + 'osx': 'APPLE_OSX', + 'ios': 'APPLE_IOS', + 'freebsd': 'FREEBSD', + 'openbsd': 'OPENBSD', + 'netbsd': 'NETBSD', + 'haiku': 'HAIKU', + 'netbsd': 'NETBSD', + 'mac': 'APPLE_OSX', + 'macx': 'APPLE_OSX', + 'macos': 'APPLE_OSX', + 'macx-icc': '(APPLE_OSX AND ICC)', +} + + +def substitute_platform(platform: str) -> str: + """ Return the qmake platform as cmake platform or the unchanged string. """ + return platform_mapping.get(platform, platform) + + +libray_mapping = { + 'atspi': 'PkgConfig::ATSPI2', + 'cups': 'Cups::Cups', + 'drm': 'Libdrm::Libdrm', + 'doubleconversion': 'double-conversion', + 'fontconfig': 'Fontconfig::Fontconfig', + 'freetype': 'Freetype::Freetype', + 'gbm': 'gbm::gbm', + 'glib': 'GLIB2::GLIB2', + 'glx_support': 'Qt::GlxSupport', + 'glx_supportPrivate': 'Qt::GlxSupportPrivate', + 'harfbuzz': 'harfbuzz::harfbuzz', + 'icu': 'ICU::i18n ICU::uc ICU::data', + 'libatomic': 'Atomic', + 'libdl': '${CMAKE_DL_LIBS}', + 'libinput': 'Libinput::Libinput', + 'libpng' : 'PNG::PNG', + 'libproxy': 'LibProxy::LibProxy', + 'librt': 'WrapRt', + 'libudev': 'PkgConfig::Libudev', + 'mtdev': 'PkgConfig::Mtdev', + 'odbc': 'ODBC::ODBC', + 'openssl': 'OpenSSL::SSL', + 'pcre2': 'PCRE2', + 'psql': 'PostgreSQL::PostgreSQL', + 'sqlite': 'SQLite::SQLite3', + 'SQLite3': 'SQLite::SQLite3', + 'tslib': 'PkgConfig::Tslib', + 'x11sm': '${X11_SM_LIB} ${X11_ICE_LIB}', + 'xcb_icccm': 'XCB::ICCCM', + 'xcb_image': 'XCB::IMAGE', + 'xcb_keysyms': 'XCB::KEYSYMS', + 'xcb_randr': 'XCB::RANDR', + 'xcb_renderutil': 'XCB::RENDERUTIL', + 'xcb_render': 'XCB::RENDER', + 'xcb_shape': 'XCB::SHAPE', + 'xcb_shm': 'XCB::SHM', + 'xcb_sync': 'XCB::SYNC', + 'xcb': 'XCB::XCB', + 'xcb_xfixes': 'XCB::XFIXES', + 'xcb_xinerama': 'XCB::XINERAMA', + 'xcb_xinput': 'XCB::XINPUT', + 'xcb_xkb': 'XCB::XKB', + 'xcb_xlib': 'X11::XCB', + 'xkbcommon_evdev': 'XKB::XKB', + 'xkbcommon_x11': 'XKB::XKB', + 'xkbcommon': 'XKB::XKB', + 'xrender': 'XCB::RENDER', + 'zlib': 'ZLIB::ZLIB', + 'zstd': 'ZSTD::ZSTD', +} + + +def substitute_libs(lib: str) -> str: + libpostfix = '' + if lib.endswith('/nolink'): + lib = lib[:-7] + libpostfix = '_nolink' + return libray_mapping.get(lib, lib) + libpostfix diff --git a/util/cmake/pro2cmake.py b/util/cmake/pro2cmake.py new file mode 100755 index 0000000000..7801c4d103 --- /dev/null +++ b/util/cmake/pro2cmake.py @@ -0,0 +1,1592 @@ +#!/usr/bin/env python3 +############################################################################# +## +## Copyright (C) 2018 The Qt Company Ltd. +## Contact: https://www.qt.io/licensing/ +## +## This file is part of the plugins of the Qt Toolkit. +## +## $QT_BEGIN_LICENSE:GPL-EXCEPT$ +## Commercial License Usage +## Licensees holding valid commercial Qt licenses may use this file in +## accordance with the commercial license agreement provided with the +## Software or, alternatively, in accordance with the terms contained in +## a written agreement between you and The Qt Company. For licensing terms +## and conditions see https://www.qt.io/terms-conditions. For further +## information use the contact form at https://www.qt.io/contact-us. +## +## GNU General Public License Usage +## Alternatively, this file may be used under the terms of the GNU +## General Public License version 3 as published by the Free Software +## Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +## included in the packaging of this file. Please review the following +## information to ensure the GNU General Public License requirements will +## be met: https://www.gnu.org/licenses/gpl-3.0.html. +## +## $QT_END_LICENSE$ +## +############################################################################# + + +from __future__ import annotations + +from argparse import ArgumentParser +import copy +import xml.etree.ElementTree as ET +from itertools import chain +import os.path +import re +import io +import typing + +from sympy.logic import (simplify_logic, And, Or, Not,) +import pyparsing as pp + +from helper import map_qt_library, map_qt_base_library, featureName, \ + substitute_platform, substitute_libs + + +def _parse_commandline(): + parser = ArgumentParser(description='Generate CMakeLists.txt files from .' + 'pro files.') + parser.add_argument('--debug', dest='debug', action='store_true', + help='Turn on all debug output') + parser.add_argument('--debug-parser', dest='debug_parser', + action='store_true', + help='Print debug output from qmake parser.') + parser.add_argument('--debug-parse-result', dest='debug_parse_result', + action='store_true', + help='Dump the qmake parser result.') + parser.add_argument('--debug-parse-dictionary', + dest='debug_parse_dictionary', action='store_true', + help='Dump the qmake parser result as dictionary.') + parser.add_argument('--debug-pro-structure', dest='debug_pro_structure', + action='store_true', + help='Dump the structure of the qmake .pro-file.') + parser.add_argument('--debug-full-pro-structure', + dest='debug_full_pro_structure', action='store_true', + help='Dump the full structure of the qmake .pro-file ' + '(with includes).') + parser.add_argument('files', metavar='<.pro/.pri file>', type=str, + nargs='+', help='The .pro/.pri file to process') + + return parser.parse_args() + + +def process_qrc_file(target: str, filepath: str, base_dir: str = '') -> str: + assert(target) + resource_name = os.path.splitext(os.path.basename(filepath))[0] + base_dir = os.path.join('' if base_dir == '.' else base_dir, os.path.dirname(filepath)) + + tree = ET.parse(filepath) + root = tree.getroot() + assert(root.tag == 'RCC') + + output = '' + + resource_count = 0 + for resource in root: + assert(resource.tag == 'qresource') + lang = resource.get('lang', '') + prefix = resource.get('prefix', '') + + full_resource_name = resource_name + (str(resource_count) if resource_count > 0 else '') + + files: typing.Dict[str, str] = {} + for file in resource: + path = file.text + assert path + + # Get alias: + alias = file.get('alias', '') + files[path] = alias + + sorted_files = sorted(files.keys()) + + assert(sorted_files) + + for source in sorted_files: + alias = files[source] + if alias: + full_source = os.path.join(base_dir, source) + output += 'set_source_files_properties("{}"\n' \ + ' PROPERTIES alias "{}")\n'.format(full_source, alias) + + params = '' + if lang: + params += ' LANG "{}"'.format(lang) + if prefix: + params += ' PREFIX "{}"'.format(prefix) + if base_dir: + params += ' BASE "{}"'.format(base_dir) + output += 'add_qt_resource({} "{}"{} FILES\n {})\n'.format(target, full_resource_name, + params, + '\n '.join(sorted_files)) + + resource_count += 1 + + return output + + +def fixup_linecontinuation(contents: str) -> str: + contents = re.sub(r'([^\t ])\\[ \t]*\n', '\\1 \\\n', contents) + contents = re.sub(r'\\[ \t]*\n', '\\\n', contents) + + return contents + + +def spaces(indent: int) -> str: + return ' ' * indent + + +def trim_leading_dot(file: str) -> str: + while file.startswith('./'): + file = file[2:] + return file + + +def map_to_file(f: str, scope: Scope, *, is_include: bool = False) -> str: + assert('$$' not in f) + + if f.startswith('${'): # Some cmake variable is prepended + return f + + base_dir = scope.currentdir if is_include else scope.basedir + f = os.path.join(base_dir, f) + + return trim_leading_dot(f) + + +def handle_vpath(source: str, base_dir: str, vpath: typing.List[str]) -> str: + assert('$$' not in source) + + if not source: + return '' + + if not vpath: + return source + + if os.path.exists(os.path.join(base_dir, source)): + return source + + variable_pattern = re.compile(r'\$\{[A-Za-z0-9_]+\}') + match = re.match(variable_pattern, source) + if match: + # a complex, variable based path, skipping validation + # or resolving + return source + + for v in vpath: + fullpath = os.path.join(v, source) + if os.path.exists(fullpath): + return trim_leading_dot(os.path.relpath(fullpath, base_dir)) + + print(' XXXX: Source {}: Not found.'.format(source)) + return '{}-NOTFOUND'.format(source) + + +class Operation: + def __init__(self, value): + if isinstance(value, list): + self._value = value + else: + self._value = [str(value), ] + + def process(self, key, input, transformer): + assert(False) + + def __repr__(self): + assert(False) + + def _dump(self): + if not self._value: + return '<NOTHING>' + + if not isinstance(self._value, list): + return '<NOT A LIST>' + + result = [] + for i in self._value: + if not i: + result.append('<NONE>') + else: + result.append(str(i)) + return '"' + '", "'.join(result) + '"' + + +class AddOperation(Operation): + def process(self, key, input, transformer): + return input + transformer(self._value) + + def __repr__(self): + return '+({})'.format(self._dump()) + + +class UniqueAddOperation(Operation): + def process(self, key, input, transformer): + result = input + for v in transformer(self._value): + if v not in result: + result.append(v) + return result + + def __repr__(self): + return '*({})'.format(self._dump()) + + +class SetOperation(Operation): + def process(self, key, input, transformer): + values = [] # typing.List[str] + for v in self._value: + if v != '$$' + key: + values.append(v) + else: + values += input + + if transformer: + return list(transformer(values)) + else: + return values + + def __repr__(self): + return '=({})'.format(self._dump()) + + +class RemoveOperation(Operation): + def __init__(self, value): + super().__init__(value) + + def process(self, key, input, transformer): + input_set = set(input) + value_set = set(self._value) + result = [] + + # Add everything that is not going to get removed: + for v in input: + if v not in value_set: + result += [v,] + + # Add everything else with removal marker: + for v in transformer(self._value): + if v not in input_set: + result += ['-{}'.format(v), ] + + return result + + def __repr__(self): + return '-({})'.format(self._dump()) + + +class Scope(object): + + SCOPE_ID: int = 1 + + def __init__(self, *, + parent_scope: typing.Optional[Scope], + file: typing.Optional[str] = None, condition: str = '', + base_dir: str = '', + operations: typing.Mapping[str, typing.List[Operation]] = { + 'QT_SOURCE_TREE': [SetOperation('${PROJECT_SOURCE_DIR}')], + 'QT_BUILD_TREE': [SetOperation('${PROJECT_BUILD_DIR}')], + }) -> None: + if parent_scope: + parent_scope._add_child(self) + else: + self._parent = None # type: typing.Optional[Scope] + + self._basedir = base_dir + if file: + self._currentdir = os.path.dirname(file) + if not self._currentdir: + self._currentdir = '.' + if not self._basedir: + self._basedir = self._currentdir + + self._scope_id = Scope.SCOPE_ID + Scope.SCOPE_ID += 1 + self._file = file + self._condition = map_condition(condition) + self._children = [] # type: typing.List[Scope] + self._included_children = [] # type: typing.List[Scope] + self._operations = copy.deepcopy(operations) + self._visited_keys = set() # type: typing.Set[str] + self._total_condition = None # type: typing.Optional[str] + + def __repr__(self): + return '{}:{}:{}:{}:{}'.format(self._scope_id, + self._basedir, self._currentdir, + self._file, self._condition or '<TRUE>') + + def reset_visited_keys(self): + self._visited_keys = set() + + def merge(self, other: 'Scope') -> None: + assert self != other + self._included_children.append(other) + + @property + def scope_debug(self) -> bool: + merge = self.get_string('PRO2CMAKE_SCOPE_DEBUG').lower() + return merge and (merge == '1' or merge == 'on' or merge == 'yes' or merge == 'true') + + @property + def parent(self) -> typing.Optional[Scope]: + return self._parent + + @property + def basedir(self) -> str: + return self._basedir + + @property + def currentdir(self) -> str: + return self._currentdir + + def can_merge_condition(self): + if self._condition == 'else': + return False + if self._operations: + return False + + child_count = len(self._children) + if child_count == 0 or child_count > 2: + return False + assert child_count != 1 or self._children[0]._condition != 'else' + return child_count == 1 or self._children[1]._condition == 'else' + + def settle_condition(self): + new_children: typing.List[Scope] = [] + for c in self._children: + c.settle_condition() + + if c.can_merge_condition(): + child = c._children[0] + child._condition = '({}) AND ({})'.format(c._condition, child._condition) + new_children += c._children + else: + new_children.append(c) + self._children = new_children + + @staticmethod + def FromDict(parent_scope: typing.Optional['Scope'], + file: str, statements, cond: str = '', base_dir: str = '') -> Scope: + scope = Scope(parent_scope=parent_scope, file=file, condition=cond, base_dir=base_dir) + for statement in statements: + if isinstance(statement, list): # Handle skipped parts... + assert not statement + continue + + operation = statement.get('operation', None) + if operation: + key = statement.get('key', '') + value = statement.get('value', []) + assert key != '' + + if operation == '=': + scope._append_operation(key, SetOperation(value)) + elif operation == '-=': + scope._append_operation(key, RemoveOperation(value)) + elif operation == '+=': + scope._append_operation(key, AddOperation(value)) + elif operation == '*=': + scope._append_operation(key, UniqueAddOperation(value)) + else: + print('Unexpected operation "{}" in scope "{}".' + .format(operation, scope)) + assert(False) + + continue + + condition = statement.get('condition', None) + if condition: + Scope.FromDict(scope, file, + statement.get('statements'), condition, + scope.basedir) + + else_statements = statement.get('else_statements') + if else_statements: + Scope.FromDict(scope, file, else_statements, + 'else', scope.basedir) + continue + + loaded = statement.get('loaded') + if loaded: + scope._append_operation('_LOADED', UniqueAddOperation(loaded)) + continue + + option = statement.get('option', None) + if option: + scope._append_operation('_OPTION', UniqueAddOperation(option)) + continue + + included = statement.get('included', None) + if included: + scope._append_operation('_INCLUDED', + UniqueAddOperation(included)) + continue + + scope.settle_condition() + + if scope.scope_debug: + print('..... [SCOPE_DEBUG]: Created scope {}:'.format(scope)) + scope.dump(indent=1) + print('..... [SCOPE_DEBUG]: <<END OF SCOPE>>') + return scope + + def _append_operation(self, key: str, op: Operation) -> None: + if key in self._operations: + self._operations[key].append(op) + else: + self._operations[key] = [op, ] + + @property + def file(self) -> str: + return self._file or '' + + @property + def cMakeListsFile(self) -> str: + assert self.basedir + return os.path.join(self.basedir, 'CMakeLists.txt') + + @property + def condition(self) -> str: + return self._condition + + @property + def total_condition(self) -> typing.Optional[str]: + return self._total_condition + + @total_condition.setter + def total_condition(self, condition: str) -> None: + self._total_condition = condition + + def _add_child(self, scope: 'Scope') -> None: + scope._parent = self + self._children.append(scope) + + @property + def children(self) -> typing.List['Scope']: + result = list(self._children) + for include_scope in self._included_children: + result += include_scope._children + return result + + def dump(self, *, indent: int = 0) -> None: + ind = ' ' * indent + print('{}Scope "{}":'.format(ind, self)) + if self.total_condition: + print('{} Total condition = {}'.format(ind, self.total_condition)) + print('{} Keys:'.format(ind)) + keys = self._operations.keys() + if not keys: + print('{} -- NONE --'.format(ind)) + else: + for k in sorted(keys): + print('{} {} = "{}"' + .format(ind, k, self._operations.get(k, []))) + print('{} Children:'.format(ind)) + if not self._children: + print('{} -- NONE --'.format(ind)) + else: + for c in self._children: + c.dump(indent=indent + 1) + print('{} Includes:'.format(ind)) + if not self._included_children: + print('{} -- NONE --'.format(ind)) + else: + for c in self._included_children: + c.dump(indent=indent + 1) + + def dump_structure(self, *, type: str = 'ROOT', indent: int = 0) -> None: + print('{}{}: {}'.format(spaces(indent), type, self)) + for i in self._included_children: + i.dump_structure(type='INCL', indent=indent + 1) + for i in self._children: + i.dump_structure(type='CHLD', indent=indent + 1) + + @property + def keys(self): + return self._operations.keys() + + @property + def visited_keys(self): + return self._visited_keys + + def _evalOps(self, key: str, + transformer: typing.Optional[typing.Callable[[Scope, typing.List[str]], typing.List[str]]], + result: typing.List[str], *, inherrit: bool = False) \ + -> typing.List[str]: + self._visited_keys.add(key) + + # Inherrit values from above: + if self._parent and inherrit: + result = self._parent._evalOps(key, transformer, result) + + if transformer: + op_transformer = lambda files: transformer(self, files) + else: + op_transformer = lambda files: files + + for op in self._operations.get(key, []): + result = op.process(key, result, op_transformer) + + for ic in self._included_children: + result = list(ic._evalOps(key, transformer, result)) + + return result + + def get(self, key: str, *, ignore_includes: bool = False, inherrit: bool = False) -> typing.List[str]: + if key == 'PWD': + return ['${CMAKE_CURRENT_SOURCE_DIR}/' + os.path.relpath(self.currentdir, self.basedir),] + if key == 'OUT_PWD': + return ['${CMAKE_CURRENT_BUILD_DIR}/' + os.path.relpath(self.currentdir, self.basedir),] + + return self._evalOps(key, None, [], inherrit=inherrit) + + def get_string(self, key: str, default: str = '') -> str: + v = self.get(key) + if len(v) == 0: + return default + assert len(v) == 1 + return v[0] + + def _map_files(self, files: typing.List[str], *, + use_vpath: bool = True, is_include: bool = False) -> typing.List[str]: + + expanded_files = [] # typing.List[str] + for f in files: + r = self._expand_value(f) + expanded_files += r + + mapped_files = list(map(lambda f: map_to_file(f, self, is_include=is_include), expanded_files)) + + if use_vpath: + result = list(map(lambda f: handle_vpath(f, self.basedir, self.get('VPATH', inherrit=True)), mapped_files)) + else: + result = mapped_files + + # strip ${CMAKE_CURRENT_SOURCE_DIR}: + result = list(map(lambda f: f[28:] if f.startswith('${CMAKE_CURRENT_SOURCE_DIR}/') else f, result)) + + # strip leading ./: + result = list(map(lambda f: trim_leading_dot(f), result)) + + return result + + def get_files(self, key: str, *, use_vpath: bool = False, + is_include: bool = False) -> typing.List[str]: + transformer = lambda scope, files: scope._map_files(files, use_vpath=use_vpath, is_include=is_include) + return list(self._evalOps(key, transformer, [])) + + def _expand_value(self, value: str) -> typing.List[str]: + result = value + pattern = re.compile(r'\$\$\{?([A-Za-z_][A-Za-z0-9_]*)\}?') + match = re.search(pattern, result) + while match: + old_result = result + if match.group(0) == value: + return self.get(match.group(1)) + + replacement = self.get(match.group(1)) + replacement_str = replacement[0] if replacement else '' + result = result[:match.start()] \ + + replacement_str \ + + result[match.end():] + + if result == old_result: + return [result,] # Do not go into infinite loop + + match = re.search(pattern, result) + return [result,] + + def expand(self, key: str) -> typing.List[str]: + value = self.get(key) + result: typing.List[str] = [] + assert isinstance(value, list) + for v in value: + result += self._expand_value(v) + return result + + def expandString(self, key: str) -> str: + result = self._expand_value(self.get_string(key)) + assert len(result) == 1 + return result[0] + + @property + def TEMPLATE(self) -> str: + return self.get_string('TEMPLATE', 'app') + + def _rawTemplate(self) -> str: + return self.get_string('TEMPLATE') + + @property + def TARGET(self) -> str: + return self.get_string('TARGET') \ + or os.path.splitext(os.path.basename(self.file))[0] + + @property + def _INCLUDED(self) -> typing.List[str]: + return self.get('_INCLUDED') + + +class QmakeParser: + def __init__(self, *, debug: bool = False) -> None: + self._Grammar = self._generate_grammar(debug) + + def _generate_grammar(self, debug: bool): + # Define grammar: + pp.ParserElement.setDefaultWhitespaceChars(' \t') + + LC = pp.Suppress(pp.Literal('\\\n')) + EOL = pp.Suppress(pp.LineEnd()) + Else = pp.Keyword('else') + Identifier = pp.Word(pp.alphas + '_', bodyChars=pp.alphanums+'_-./') + BracedValue = pp.nestedExpr(ignoreExpr=pp.quotedString \ + | pp.QuotedString(quoteChar='$(', + endQuoteChar=')', + escQuote='\\', + unquoteResults=False) + ).setParseAction(lambda s, l, t: ['(', *t[0], ')']) + + Substitution \ + = pp.Combine(pp.Literal('$') + + (((pp.Literal('$') + Identifier + + pp.Optional(pp.nestedExpr())) + | (pp.Literal('(') + Identifier + pp.Literal(')')) + | (pp.Literal('{') + Identifier + pp.Literal('}')) + | (pp.Literal('$') + pp.Literal('{') + + Identifier + pp.Optional(pp.nestedExpr()) + + pp.Literal('}')) + | (pp.Literal('$') + pp.Literal('[') + Identifier + + pp.Literal(']')) + ))) + LiteralValuePart = pp.Word(pp.printables, excludeChars='$#{}()') + SubstitutionValue \ + = pp.Combine(pp.OneOrMore(Substitution | LiteralValuePart + | pp.Literal('$'))) + Value = pp.NotAny(Else | pp.Literal('}') | EOL) \ + + (pp.QuotedString(quoteChar='"', escChar='\\') + | SubstitutionValue + | BracedValue) + + Values = pp.ZeroOrMore(Value + pp.Optional(LC))('value') + + Op = pp.Literal('=') | pp.Literal('-=') | pp.Literal('+=') \ + | pp.Literal('*=') + + Key = Identifier + + Operation = Key('key') + pp.Optional(LC) \ + + Op('operation') + pp.Optional(LC) \ + + Values('value') + CallArgs = pp.Optional(LC) + pp.nestedExpr()\ + + def parse_call_args(results): + out = '' + for item in chain(*results): + if isinstance(item, str): + out += item + else: + out += "(" + parse_call_args(item) + ")" + return out + + CallArgs.setParseAction(parse_call_args) + Load = pp.Keyword('load') + CallArgs('loaded') + Include = pp.Keyword('include') + CallArgs('included') + Option = pp.Keyword('option') + CallArgs('option') + DefineTestDefinition = pp.Suppress(pp.Keyword('defineTest') + CallArgs + + pp.nestedExpr(opener='{', closer='}', ignoreExpr=pp.LineEnd())) # ignore the whole thing... + ForLoop = pp.Suppress(pp.Keyword('for') + CallArgs + + pp.nestedExpr(opener='{', closer='}', ignoreExpr=pp.LineEnd())) # ignore the whole thing... + ForLoopSingleLine = pp.Suppress(pp.Keyword('for') + CallArgs + + pp.Literal(':') + pp.SkipTo(EOL, ignore=LC)) # ignore the whole thing... + FunctionCall = pp.Suppress(Identifier + pp.nestedExpr()) + + Scope = pp.Forward() + + Statement = pp.Group(Load | Include | Option | ForLoop | ForLoopSingleLine \ + | DefineTestDefinition | FunctionCall | Operation) + StatementLine = Statement + (EOL | pp.FollowedBy('}')) + StatementGroup = pp.ZeroOrMore(StatementLine | Scope | pp.Suppress(EOL)) + + Block = pp.Suppress('{') + pp.Optional(LC | EOL) \ + + StatementGroup + pp.Optional(LC | EOL) \ + + pp.Suppress('}') + pp.Optional(LC | EOL) + + ConditionEnd = pp.FollowedBy((pp.Optional(pp.White()) + + pp.Optional(LC) + (pp.Literal(':') \ + | pp.Literal('{') \ + | pp.Literal('|')))) + ConditionPart = ((pp.Optional('!') + Identifier + pp.Optional(BracedValue)) \ + ^ pp.CharsNotIn('#{}|:=\\\n')) + pp.Optional(LC) + ConditionEnd + Condition = pp.Combine(ConditionPart \ + + pp.ZeroOrMore((pp.Literal('|') ^ pp.Literal(':')) \ + + ConditionPart)) + Condition.setParseAction(lambda x: ' '.join(x).strip().replace(':', ' && ').strip(' && ')) + + SingleLineScope = pp.Suppress(pp.Literal(':')) + pp.Optional(LC) \ + + pp.Group(Block | (Statement + EOL))('statements') + MultiLineScope = pp.Optional(LC) + Block('statements') + + SingleLineElse = pp.Suppress(pp.Literal(':')) + pp.Optional(LC) \ + + (Scope | Block | (Statement + pp.Optional(EOL))) + MultiLineElse = Block + ElseBranch = pp.Suppress(Else) + (SingleLineElse | MultiLineElse) + Scope <<= pp.Optional(LC) \ + + pp.Group(Condition('condition') \ + + (SingleLineScope | MultiLineScope) \ + + pp.Optional(ElseBranch)('else_statements')) + + if debug: + for ename in 'LC EOL ' \ + 'Condition ConditionPart ConditionEnd ' \ + 'Else ElseBranch SingleLineElse MultiLineElse ' \ + 'SingleLineScope MultiLineScope ' \ + 'Identifier ' \ + 'Key Op Values Value BracedValue ' \ + 'Scope Block ' \ + 'StatementGroup StatementLine Statement '\ + 'Load Include Option DefineTestDefinition ForLoop ' \ + 'FunctionCall CallArgs Operation'.split(): + expr = locals()[ename] + expr.setName(ename) + expr.setDebug() + + Grammar = StatementGroup('statements') + Grammar.ignore(pp.pythonStyleComment()) + + return Grammar + + def parseFile(self, file: str): + print('Parsing \"{}\"...'.format(file)) + try: + with open(file, 'r') as file_fd: + contents = file_fd.read() + + old_contents = contents + contents = fixup_linecontinuation(contents) + + if old_contents != contents: + print('Warning: Fixed line continuation in .pro-file!\n' + ' Position information in Parsing output might be wrong!') + result = self._Grammar.parseString(contents, parseAll=True) + except pp.ParseException as pe: + print(pe.line) + print(' '*(pe.col-1) + '^') + print(pe) + raise pe + return result + + +def parseProFile(file: str, *, debug=False): + parser = QmakeParser(debug=debug) + return parser.parseFile(file) + + +def map_condition(condition: str) -> str: + condition = re.sub(r'\bif\s*\((.*?)\)', r'\1', condition) + condition = re.sub(r'\bisEmpty\s*\((.*?)\)', r'\1_ISEMPTY', condition) + condition = re.sub(r'\bcontains\s*\((.*?),\s*"?(.*?)"?\)', + r'\1___contains___\2', condition) + condition = re.sub(r'\bequals\s*\((.*?),\s*"?(.*?)"?\)', + r'\1___equals___\2', condition) + condition = re.sub(r'\s*==\s*', '___STREQUAL___', condition) + + condition = condition.replace('*', '_x_') + condition = condition.replace('.$$', '__ss_') + condition = condition.replace('$$', '_ss_') + + condition = condition.replace('!', 'NOT ') + condition = condition.replace('&&', ' AND ') + condition = condition.replace('|', ' OR ') + + cmake_condition = '' + for part in condition.split(): + # some features contain e.g. linux, that should not be + # turned upper case + feature = re.match(r"(qtConfig|qtHaveModule)\(([a-zA-Z0-9_-]+)\)", + part) + if feature: + if (feature.group(1) == "qtHaveModule"): + part = 'TARGET {}'.format(map_qt_library(feature.group(2))) + else: + feature = featureName(feature.group(2)) + if feature.startswith('system_') and substitute_libs(feature[7:]) != feature[7:]: + # Qt6 always uses system libraries! + part = 'ON' + elif feature == 'dlopen': + part = 'ON' + else: + part = 'QT_FEATURE_' + feature + else: + part = substitute_platform(part) + + part = part.replace('true', 'ON') + part = part.replace('false', 'OFF') + cmake_condition += ' ' + part + return cmake_condition.strip() + + +def handle_subdir(scope: Scope, cm_fh: typing.IO[str], *, + indent: int = 0) -> None: + ind = ' ' * indent + for sd in scope.get_files('SUBDIRS'): + if os.path.isdir(sd): + cm_fh.write('{}add_subdirectory({})\n'.format(ind, sd)) + elif os.path.isfile(sd): + subdir_result = parseProFile(sd, debug=False) + subdir_scope \ + = Scope.FromDict(scope, sd, + subdir_result.asDict().get('statements'), + '', scope.basedir) + + do_include(subdir_scope) + cmakeify_scope(subdir_scope, cm_fh, indent=indent) + elif sd.startswith('-'): + cm_fh.write('{}### remove_subdirectory' + '("{}")\n'.format(ind, sd[1:])) + else: + print(' XXXX: SUBDIR {} in {}: Not found.'.format(sd, scope)) + + for c in scope.children: + cond = c.condition + if cond == 'else': + cm_fh.write('\n{}else()\n'.format(ind)) + elif cond: + cm_fh.write('\n{}if({})\n'.format(ind, cond)) + + handle_subdir(c, cm_fh, indent=indent + 1) + + if cond: + cm_fh.write('{}endif()\n'.format(ind)) + + +def sort_sources(sources: typing.List[str]) -> typing.List[str]: + to_sort = {} # type: typing.Dict[str, typing.List[str]] + for s in sources: + if s is None: + continue + + dir = os.path.dirname(s) + base = os.path.splitext(os.path.basename(s))[0] + if base.endswith('_p'): + base = base[:-2] + sort_name = os.path.join(dir, base) + + array = to_sort.get(sort_name, []) + array.append(s) + + to_sort[sort_name] = array + + lines = [] + for k in sorted(to_sort.keys()): + lines.append(' '.join(sorted(to_sort[k]))) + + return lines + + +def write_header(cm_fh: typing.IO[str], name: str, + typename: str, *, indent: int = 0): + cm_fh.write('{}###########################################' + '##########################\n'.format(spaces(indent))) + cm_fh.write('{}## {} {}:\n'.format(spaces(indent), name, typename)) + cm_fh.write('{}###########################################' + '##########################\n\n'.format(spaces(indent))) + + +def write_scope_header(cm_fh: typing.IO[str], *, indent: int = 0): + cm_fh.write('\n{}## Scopes:\n'.format(spaces(indent))) + cm_fh.write('{}###########################################' + '##########################\n'.format(spaces(indent))) + + +def write_source_file_list(cm_fh: typing.IO[str], scope, cmake_parameter: str, + keys: typing.List[str], indent: int = 0, *, + header: str = '', footer: str = ''): + ind = spaces(indent) + + # collect sources + sources: typing.List[str] = [] + for key in keys: + sources += scope.get_files(key, use_vpath=True) + + if not sources: + return + + cm_fh.write(header) + extra_indent = '' + if cmake_parameter: + cm_fh.write('{} {}\n'.format(ind, cmake_parameter)) + extra_indent = ' ' + for s in sort_sources(sources): + cm_fh.write('{} {}{}\n'.format(ind, extra_indent, s)) + cm_fh.write(footer) + + +def write_library_list(cm_fh: typing.IO[str], cmake_keyword: str, + dependencies: typing.List[str], *, indent: int = 0): + dependencies_to_print = [] + is_framework = False + + for d in dependencies: + if d == '-framework': + is_framework = True + continue + if is_framework: + d = '${FW%s}' % d + if d.startswith('-l'): + d = d[2:] + + if d.startswith('-'): + d = '# Remove: {}'.format(d[1:]) + else: + d = substitute_libs(d) + dependencies_to_print.append(d) + is_framework = False + + if dependencies_to_print: + ind = spaces(indent) + cm_fh.write('{} {}\n'.format(ind, cmake_keyword)) + for d in sorted(list(set(dependencies_to_print))): + cm_fh.write('{} {}\n'.format(ind, d)) + + + +def write_library_section(cm_fh: typing.IO[str], scope: Scope, + public: typing.List[str], + private: typing.List[str], + mixed: typing.List[str], *, + indent: int = 0, known_libraries=set()): + public_dependencies = [] # typing.List[str] + private_dependencies = [] # typing.List[str] + + for key in public: + public_dependencies += [map_qt_library(q) for q in scope.expand(key) + if map_qt_library(q) not in known_libraries] + for key in private: + private_dependencies += [map_qt_library(q) for q in scope.expand(key) + if map_qt_library(q) not in known_libraries] + for key in mixed: + for lib in scope.expand(key): + mapped_lib = map_qt_library(lib) + if mapped_lib in known_libraries: + continue + + if mapped_lib.endswith('Private'): + private_dependencies.append(mapped_lib) + public_dependencies.append(mapped_lib[:-7]) + else: + public_dependencies.append(mapped_lib) + + write_library_list(cm_fh, 'LIBRARIES', private_dependencies, indent=indent) + write_library_list(cm_fh, 'PUBLIC_LIBRARIES', public_dependencies, indent=indent) + + + +def write_sources_section(cm_fh: typing.IO[str], scope: Scope, *, + indent: int = 0, known_libraries=set()): + ind = spaces(indent) + + # mark RESOURCES as visited: + scope.get('RESOURCES') + + plugin_type = scope.get_string('PLUGIN_TYPE') + + if plugin_type: + cm_fh.write('{} TYPE {}\n'.format(ind, plugin_type)) + + source_keys: typing.List[str] = [] + write_source_file_list(cm_fh, scope, 'SOURCES', + ['SOURCES', 'HEADERS', 'OBJECTIVE_SOURCES', 'NO_PCH_SOURCES', 'FORMS'], + indent) + + write_source_file_list(cm_fh, scope, 'DBUS_ADAPTOR_SOURCES', ['DBUS_ADAPTORS',], indent) + dbus_adaptor_flags = scope.expand('QDBUSXML2CPP_ADAPTOR_HEADER_FLAGS') + if dbus_adaptor_flags: + cm_fh.write('{} DBUS_ADAPTOR_FLAGS\n'.format(ind)) + cm_fh.write('{} "{}"\n'.format(ind, '" "'.join(dbus_adaptor_flags))) + + write_source_file_list(cm_fh, scope, 'DBUS_INTERFACE_SOURCES', ['DBUS_INTERFACES',], indent) + dbus_interface_flags = scope.expand('QDBUSXML2CPP_INTERFACE_HEADER_FLAGS') + if dbus_interface_flags: + cm_fh.write('{} DBUS_INTERFACE_FLAGS\n'.format(ind)) + cm_fh.write('{} "{}"\n'.format(ind, '" "'.join(dbus_interface_flags))) + + defines = scope.expand('DEFINES') + defines += [d[2:] for d in scope.expand('QMAKE_CXXFLAGS') if d.startswith('-D')] + if defines: + cm_fh.write('{} DEFINES\n'.format(ind)) + for d in defines: + d = d.replace('=\\\\\\"$$PWD/\\\\\\"', + '="${CMAKE_CURRENT_SOURCE_DIR}/"') + cm_fh.write('{} {}\n'.format(ind, d)) + includes = scope.get_files('INCLUDEPATH') + if includes: + cm_fh.write('{} INCLUDE_DIRECTORIES\n'.format(ind)) + for i in includes: + i = i.rstrip('/') or ('/') + cm_fh.write('{} {}\n'.format(ind, i)) + + write_library_section(cm_fh, scope, + ['QMAKE_USE', 'LIBS'], + ['QT_FOR_PRIVATE', 'QMAKE_USE_PRIVATE', 'QMAKE_USE_FOR_PRIVATE', 'LIBS_PRIVATE'], + ['QT',], + indent=indent, known_libraries=known_libraries) + + compile_options = [d for d in scope.expand('QMAKE_CXXFLAGS') if not d.startswith('-D')] + if compile_options: + cm_fh.write('{} COMPILE_OPTIONS\n'.format(ind)) + for co in compile_options: + cm_fh.write('{} "{}"\n'.format(ind, co)) + + link_options = scope.get('QMAKE_LFLAGS') + if link_options: + cm_fh.write('{} LINK_OPTIONS\n'.format(ind)) + for lo in link_options: + cm_fh.write('{} "{}"\n'.format(ind, lo)) + + moc_options = scope.get('QMAKE_MOC_OPTIONS') + if moc_options: + cm_fh.write('{} MOC_OPTIONS\n'.format(ind)) + for mo in moc_options: + cm_fh.write('{} "{}"\n'.format(ind, mo)) + + +def is_simple_condition(condition: str) -> bool: + return ' ' not in condition \ + or (condition.startswith('NOT ') and ' ' not in condition[4:]) + + +def write_ignored_keys(scope: Scope, indent: str) -> str: + result = '' + ignored_keys = scope.keys - scope.visited_keys + for k in sorted(ignored_keys): + if k == '_INCLUDED' or k == 'TARGET' or k == 'QMAKE_DOCS' or k == 'QT_SOURCE_TREE' \ + or k == 'QT_BUILD_TREE' or k == 'TRACEPOINT_PROVIDER': + # All these keys are actually reported already + continue + values = scope.get(k) + value_string = '<EMPTY>' if not values \ + else '"' + '" "'.join(scope.get(k)) + '"' + result += '{}# {} = {}\n'.format(indent, k, value_string) + + if result: + result = '\n#### Keys ignored in scope {}:\n{}'.format(scope, result) + + return result + + +def _iterate_expr_tree(expr, op, matches): + assert expr.func == op + keepers = () + for arg in expr.args: + if arg in matches: + matches = tuple(x for x in matches if x != arg) + elif arg == op: + (matches, extra_keepers) = _iterate_expr_tree(arg, op, matches) + keepers = (*keepers, *extra_keepers) + else: + keepers = (*keepers, arg) + return matches, keepers + + +def _simplify_expressions(expr, op, matches, replacement): + for arg in expr.args: + expr = expr.subs(arg, _simplify_expressions(arg, op, matches, + replacement)) + + if expr.func == op: + (to_match, keepers) = tuple(_iterate_expr_tree(expr, op, matches)) + if len(to_match) == 0: + # build expression with keepers and replacement: + if keepers: + start = replacement + current_expr = None + last_expr = keepers[-1] + for repl_arg in keepers[:-1]: + current_expr = op(start, repl_arg) + start = current_expr + top_expr = op(start, last_expr) + else: + top_expr = replacement + + expr = expr.subs(expr, top_expr) + + return expr + + +def _simplify_flavors_in_condition(base: str, flavors, expr): + ''' Simplify conditions based on the knownledge of which flavors + belong to which OS. ''' + base_expr = simplify_logic(base) + false_expr = simplify_logic('false') + for flavor in flavors: + flavor_expr = simplify_logic(flavor) + expr = _simplify_expressions(expr, And, (base_expr, flavor_expr,), + flavor_expr) + expr = _simplify_expressions(expr, Or, (base_expr, flavor_expr), + base_expr) + expr = _simplify_expressions(expr, And, (Not(base_expr), flavor_expr,), + false_expr) + return expr + + +def _simplify_os_families(expr, family_members, other_family_members): + for family in family_members: + for other in other_family_members: + if other in family_members: + continue # skip those in the sub-family + + f_expr = simplify_logic(family) + o_expr = simplify_logic(other) + + expr = _simplify_expressions(expr, And, (f_expr, Not(o_expr)), f_expr) + expr = _simplify_expressions(expr, And, (Not(f_expr), o_expr), o_expr) + expr = _simplify_expressions(expr, And, (f_expr, o_expr), simplify_logic('false')) + return expr + + +def _recursive_simplify(expr): + ''' Simplify the expression as much as possible based on + domain knowledge. ''' + input_expr = expr + + # Simplify even further, based on domain knowledge: + windowses = ('WIN32', 'WINRT') + apples = ('APPLE_OSX', 'APPLE_UIKIT', 'APPLE_IOS', + 'APPLE_TVOS', 'APPLE_WATCHOS',) + bsds = ('FREEBSD', 'OPENBSD', 'NETBSD',) + androids = ('ANDROID', 'ANDROID_EMBEDDED') + unixes = ('APPLE', *apples, 'BSD', *bsds, 'LINUX', + *androids, 'HAIKU', + 'INTEGRITY', 'VXWORKS', 'QNX', 'WASM') + + unix_expr = simplify_logic('UNIX') + win_expr = simplify_logic('WIN32') + false_expr = simplify_logic('false') + true_expr = simplify_logic('true') + + expr = expr.subs(Not(unix_expr), win_expr) # NOT UNIX -> WIN32 + expr = expr.subs(Not(win_expr), unix_expr) # NOT WIN32 -> UNIX + + # UNIX [OR foo ]OR WIN32 -> ON [OR foo] + expr = _simplify_expressions(expr, Or, (unix_expr, win_expr,), true_expr) + # UNIX [AND foo ]AND WIN32 -> OFF [AND foo] + expr = _simplify_expressions(expr, And, (unix_expr, win_expr,), false_expr) + + expr = _simplify_flavors_in_condition('WIN32', ('WINRT',), expr) + expr = _simplify_flavors_in_condition('APPLE', apples, expr) + expr = _simplify_flavors_in_condition('BSD', bsds, expr) + expr = _simplify_flavors_in_condition('UNIX', unixes, expr) + expr = _simplify_flavors_in_condition('ANDROID', ('ANDROID_EMBEDDED',), expr) + + # Simplify families of OSes against other families: + expr = _simplify_os_families(expr, ('WIN32', 'WINRT'), unixes) + expr = _simplify_os_families(expr, androids, unixes) + expr = _simplify_os_families(expr, ('BSD', *bsds), unixes) + + for family in ('HAIKU', 'QNX', 'INTEGRITY', 'LINUX', 'VXWORKS'): + expr = _simplify_os_families(expr, (family,), unixes) + + # Now simplify further: + expr = simplify_logic(expr) + + while expr != input_expr: + input_expr = expr + expr = _recursive_simplify(expr) + + return expr + + +def simplify_condition(condition: str) -> str: + input_condition = condition.strip() + + # Map to sympy syntax: + condition = ' ' + input_condition + ' ' + condition = condition.replace('(', ' ( ') + condition = condition.replace(')', ' ) ') + + tmp = '' + while tmp != condition: + tmp = condition + + condition = condition.replace(' NOT ', ' ~ ') + condition = condition.replace(' AND ', ' & ') + condition = condition.replace(' OR ', ' | ') + condition = condition.replace(' ON ', ' true ') + condition = condition.replace(' OFF ', ' false ') + + try: + # Generate and simplify condition using sympy: + condition_expr = simplify_logic(condition) + condition = str(_recursive_simplify(condition_expr)) + + # Map back to CMake syntax: + condition = condition.replace('~', 'NOT ') + condition = condition.replace('&', 'AND') + condition = condition.replace('|', 'OR') + condition = condition.replace('True', 'ON') + condition = condition.replace('False', 'OFF') + except: + # sympy did not like our input, so leave this condition alone: + condition = input_condition + + return condition or 'ON' + + +def recursive_evaluate_scope(scope: Scope, parent_condition: str = '', + previous_condition: str = '') -> str: + current_condition = scope.condition + total_condition = current_condition + if total_condition == 'else': + assert previous_condition, \ + "Else branch without previous condition in: %s" % scope.file + total_condition = 'NOT ({})'.format(previous_condition) + if parent_condition: + if not total_condition: + total_condition = parent_condition + else: + total_condition = '({}) AND ({})'.format(parent_condition, + total_condition) + + scope.total_condition = simplify_condition(total_condition) + + prev_condition = '' + for c in scope.children: + prev_condition = recursive_evaluate_scope(c, total_condition, + prev_condition) + + return current_condition + + +def map_to_cmake_condition(condition: str) -> str: + condition = re.sub(r'\bQT_ARCH___equals___([a-zA-Z_0-9]*)', + r'(TEST_architecture_arch STREQUAL "\1")', condition) + condition = re.sub(r'\bQT_ARCH___contains___([a-zA-Z_0-9]*)', + r'(TEST_architecture_arch STREQUAL "\1")', condition) + return condition + + +def write_resources(cm_fh: typing.IO[str], target: str, scope: Scope, indent: int = 0): + vpath = scope.expand('VPATH') + + # Handle QRC files by turning them into add_qt_resource: + resources = scope.get_files('RESOURCES') + qrc_output = '' + if resources: + qrc_only = True + for r in resources: + if r.endswith('.qrc'): + qrc_output += process_qrc_file(target, r, scope.basedir) + else: + qrc_only = False + + if not qrc_only: + print(' XXXX Ignoring non-QRC file resources.') + + if qrc_output: + cm_fh.write('\n# Resources:\n') + for line in qrc_output.split('\n'): + cm_fh.write(' ' * indent + line + '\n') + + +def write_extend_target(cm_fh: typing.IO[str], target: str, + scope: Scope, indent: int = 0): + ind = spaces(indent) + extend_qt_io_string = io.StringIO() + write_sources_section(extend_qt_io_string, scope) + extend_qt_string = extend_qt_io_string.getvalue() + + extend_scope = '\n{}extend_target({} CONDITION {}\n' \ + '{}{})\n'.format(ind, target, + map_to_cmake_condition(scope.total_condition), + extend_qt_string, ind) + + if not extend_qt_string: + extend_scope = '' # Nothing to report, so don't! + + cm_fh.write(extend_scope) + + write_resources(cm_fh, target, scope, indent) + + +def flatten_scopes(scope: Scope) -> typing.List[Scope]: + result = [scope] # type: typing.List[Scope] + for c in scope.children: + result += flatten_scopes(c) + return result + + +def merge_scopes(scopes: typing.List[Scope]) -> typing.List[Scope]: + result = [] # type: typing.List[Scope] + + # Merge scopes with their parents: + known_scopes = {} # type: typing.Mapping[str, Scope] + for scope in scopes: + total_condition = scope.total_condition + if total_condition == 'OFF': + # ignore this scope entirely! + pass + elif total_condition in known_scopes: + known_scopes[total_condition].merge(scope) + else: + # Keep everything else: + result.append(scope) + known_scopes[total_condition] = scope + + return result + + +def write_simd_part(cm_fh: typing.IO[str], target: str, scope: Scope, indent: int = 0): + simd_options = [ 'sse2', 'sse3', 'ssse3', 'sse4_1', 'sse4_2', 'aesni', 'shani', 'avx', 'avx2', + 'avx512f', 'avx512cd', 'avx512er', 'avx512pf', 'avx512dq', 'avx512bw', + 'avx512vl', 'avx512ifma', 'avx512vbmi', 'f16c', 'rdrnd', 'neon', 'mips_dsp', + 'mips_dspr2', + 'arch_haswell', 'avx512common', 'avx512core']; + ind = spaces(indent) + + for simd in simd_options: + SIMD = simd.upper(); + write_source_file_list(cm_fh, scope, 'SOURCES', + ['{}_HEADERS'.format(SIMD), + '{}_SOURCES'.format(SIMD), + '{}_C_SOURCES'.format(SIMD), + '{}_ASM'.format(SIMD)], + indent, + header = '{}add_qt_simd_part({} SIMD {}\n'.format(ind, target, simd), + footer = '{})\n\n'.format(ind)) + + +def write_main_part(cm_fh: typing.IO[str], name: str, typename: str, + cmake_function: str, scope: Scope, *, + extra_lines: typing.List[str] = [], + indent: int = 0, extra_keys: typing.List[str], + **kwargs: typing.Any): + # Evaluate total condition of all scopes: + recursive_evaluate_scope(scope) + + # Get a flat list of all scopes but the main one: + scopes = flatten_scopes(scope) + total_scopes = len(scopes) + # Merge scopes based on their conditions: + scopes = merge_scopes(scopes) + + assert len(scopes) + assert scopes[0].total_condition == 'ON' + + scopes[0].reset_visited_keys() + for k in extra_keys: + scopes[0].get(k) + + # Now write out the scopes: + write_header(cm_fh, name, typename, indent=indent) + + cm_fh.write('{}{}({}\n'.format(spaces(indent), cmake_function, name)) + for extra_line in extra_lines: + cm_fh.write('{} {}\n'.format(spaces(indent), extra_line)) + + write_sources_section(cm_fh, scopes[0], indent=indent, **kwargs) + + # Footer: + cm_fh.write('{})\n'.format(spaces(indent))) + + write_resources(cm_fh, name, scope, indent) + + write_simd_part(cm_fh, name, scope, indent) + + ignored_keys_report = write_ignored_keys(scopes[0], spaces(indent)) + if ignored_keys_report: + cm_fh.write(ignored_keys_report) + + + # Scopes: + if len(scopes) == 1: + return + + write_scope_header(cm_fh, indent=indent) + + for c in scopes[1:]: + c.reset_visited_keys() + write_extend_target(cm_fh, name, c, indent=indent) + ignored_keys_report = write_ignored_keys(c, spaces(indent)) + if ignored_keys_report: + cm_fh.write(ignored_keys_report) + + +def write_module(cm_fh: typing.IO[str], scope: Scope, *, + indent: int = 0) -> None: + module_name = scope.TARGET + if not module_name.startswith('Qt'): + print('XXXXXX Module name {} does not start with Qt!'.format(module_name)) + + extra = [] + if 'static' in scope.get('CONFIG'): + extra.append('STATIC') + if 'no_module_headers' in scope.get('CONFIG'): + extra.append('NO_MODULE_HEADERS') + + write_main_part(cm_fh, module_name[2:], 'Module', 'add_qt_module', scope, + extra_lines=extra, indent=indent, + known_libraries={'Qt::Core', }, extra_keys=[]) + + if 'qt_tracepoints' in scope.get('CONFIG'): + tracepoints = scope.get_files('TRACEPOINT_PROVIDER') + cm_fh.write('\n\n{}qt_create_tracepoints({} {})\n' + .format(spaces(indent), module_name[2:], ' '.join(tracepoints))) + + +def write_tool(cm_fh: typing.IO[str], scope: Scope, *, + indent: int = 0) -> None: + tool_name = scope.TARGET + + extra = ['BOOTSTRAP'] if 'force_bootstrap' in scope.get('CONFIG') else [] + + write_main_part(cm_fh, tool_name, 'Tool', 'add_qt_tool', scope, + indent=indent, known_libraries={'Qt::Core', }, + extra_lines=extra, extra_keys=['CONFIG']) + + +def write_test(cm_fh: typing.IO[str], scope: Scope, *, + indent: int = 0) -> None: + test_name = scope.TARGET + assert test_name + + write_main_part(cm_fh, test_name, 'Test', 'add_qt_test', scope, + indent=indent, known_libraries={'Qt::Core', 'Qt::Test',}, + extra_keys=[]) + + +def write_binary(cm_fh: typing.IO[str], scope: Scope, + gui: bool = False, *, indent: int = 0) -> None: + binary_name = scope.TARGET + assert binary_name + + extra = ['GUI',] if gui else[] + + target_path = scope.get_string('target.path') + if target_path: + target_path = target_path.replace('$$[QT_INSTALL_EXAMPLES]', '${INSTALL_EXAMPLESDIR}') + extra.append('OUTPUT_DIRECTORY "{}"'.format(target_path)) + if 'target' in scope.get('INSTALLS'): + extra.append('INSTALL_DIRECTORY "{}"'.format(target_path)) + + write_main_part(cm_fh, binary_name, 'Binary', 'add_qt_executable', scope, + extra_lines=extra, indent=indent, + known_libraries={'Qt::Core', }, extra_keys=['target.path', 'INSTALLS']) + + +def write_plugin(cm_fh, scope, *, indent: int = 0): + plugin_name = scope.TARGET + assert plugin_name + + write_main_part(cm_fh, plugin_name, 'Plugin', 'add_qt_plugin', scope, + indent=indent, known_libraries={'QtCore', }, extra_keys=[]) + + +def handle_app_or_lib(scope: Scope, cm_fh: typing.IO[str], *, + indent: int = 0) -> None: + assert scope.TEMPLATE in ('app', 'lib') + + is_lib = scope.TEMPLATE == 'lib' + is_plugin = any('qt_plugin' == s for s in scope.get('_LOADED')) + + if is_lib or 'qt_module' in scope.get('_LOADED'): + write_module(cm_fh, scope, indent=indent) + elif is_plugin: + write_plugin(cm_fh, scope, indent=indent) + elif 'qt_tool' in scope.get('_LOADED'): + write_tool(cm_fh, scope, indent=indent) + else: + if 'testcase' in scope.get('CONFIG') \ + or 'testlib' in scope.get('CONFIG'): + write_test(cm_fh, scope, indent=indent) + else: + gui = 'console' not in scope.get('CONFIG') + write_binary(cm_fh, scope, gui, indent=indent) + + ind = spaces(indent) + write_source_file_list(cm_fh, scope, '', + ['QMAKE_DOCS',], + indent, + header = '{}add_qt_docs(\n'.format(ind), + footer = '{})\n'.format(ind)) + + +def cmakeify_scope(scope: Scope, cm_fh: typing.IO[str], *, + indent: int = 0) -> None: + template = scope.TEMPLATE + if template == 'subdirs': + handle_subdir(scope, cm_fh, indent=indent) + elif template in ('app', 'lib'): + handle_app_or_lib(scope, cm_fh, indent=indent) + else: + print(' XXXX: {}: Template type {} not yet supported.' + .format(scope.file, template)) + + +def generate_cmakelists(scope: Scope) -> None: + with open(scope.cMakeListsFile, 'w') as cm_fh: + assert scope.file + cm_fh.write('# Generated from {}.\n\n' + .format(os.path.basename(scope.file))) + cmakeify_scope(scope, cm_fh) + + +def do_include(scope: Scope, *, debug: bool = False) -> None: + for c in scope.children: + do_include(c) + + for include_file in scope.get_files('_INCLUDED', is_include=True): + if not include_file: + continue + if not os.path.isfile(include_file): + print(' XXXX: Failed to include {}.'.format(include_file)) + continue + + include_result = parseProFile(include_file, debug=debug) + include_scope \ + = Scope.FromDict(None, include_file, + include_result.asDict().get('statements'), + '', scope.basedir) # This scope will be merged into scope! + + do_include(include_scope) + + scope.merge(include_scope) + + +def main() -> None: + args = _parse_commandline() + + debug_parsing = args.debug_parser or args.debug + + for file in args.files: + parseresult = parseProFile(file, debug=debug_parsing) + + if args.debug_parse_result or args.debug: + print('\n\n#### Parser result:') + print(parseresult) + print('\n#### End of parser result.\n') + if args.debug_parse_dictionary or args.debug: + print('\n\n####Parser result dictionary:') + print(parseresult.asDict()) + print('\n#### End of parser result dictionary.\n') + + file_scope = Scope.FromDict(None, file, + parseresult.asDict().get('statements')) + + if args.debug_pro_structure or args.debug: + print('\n\n#### .pro/.pri file structure:') + print(file_scope.dump()) + print('\n#### End of .pro/.pri file structure.\n') + + do_include(file_scope, debug=debug_parsing) + + if args.debug_full_pro_structure or args.debug: + print('\n\n#### Full .pro/.pri file structure:') + print(file_scope.dump()) + print('\n#### End of full .pro/.pri file structure.\n') + + generate_cmakelists(file_scope) + + +if __name__ == '__main__': + main() diff --git a/util/cmake/pro_conversion_rate.py b/util/cmake/pro_conversion_rate.py new file mode 100755 index 0000000000..740e834ca5 --- /dev/null +++ b/util/cmake/pro_conversion_rate.py @@ -0,0 +1,218 @@ +#!/usr/bin/env python3 +############################################################################# +## +## Copyright (C) 2019 The Qt Company Ltd. +## Contact: https://www.qt.io/licensing/ +## +## This file is part of the plugins of the Qt Toolkit. +## +## $QT_BEGIN_LICENSE:GPL-EXCEPT$ +## Commercial License Usage +## Licensees holding valid commercial Qt licenses may use this file in +## accordance with the commercial license agreement provided with the +## Software or, alternatively, in accordance with the terms contained in +## a written agreement between you and The Qt Company. For licensing terms +## and conditions see https://www.qt.io/terms-conditions. For further +## information use the contact form at https://www.qt.io/contact-us. +## +## GNU General Public License Usage +## Alternatively, this file may be used under the terms of the GNU +## General Public License version 3 as published by the Free Software +## Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +## included in the packaging of this file. Please review the following +## information to ensure the GNU General Public License requirements will +## be met: https://www.gnu.org/licenses/gpl-3.0.html. +## +## $QT_END_LICENSE$ +## +############################################################################# + +from __future__ import annotations + +""" +This utility script shows statistics about +converted .pro -> CMakeLists.txt files. + +To execute: python3 pro_conversion_rate.py <src dir> +where <src dir> can be any qt source directory. For better statistics, +specify a module root source dir (like ./qtbase or ./qtsvg). + +""" + +from argparse import ArgumentParser + +import os +import typing +from timeit import default_timer + + +def _parse_commandline(): + parser = ArgumentParser(description='Find pro files for which there are no CMakeLists.txt.') + parser.add_argument('source_directory', metavar='<src dir>', type=str, + help='The source directory') + + return parser.parse_args() + + +class Blacklist: + """ Class to check if a certain dir_name / dir_path is blacklisted """ + + def __init__(self, names: typing.List[str], path_parts: typing.List[str]): + self.names = names + self.path_parts = path_parts + + # The lookup algorithm + self.lookup = self.is_blacklisted_part + self.tree = None + + try: + # If package is available, use Aho-Corasick algorithm, + from ahocorapy.keywordtree import KeywordTree + self.tree = KeywordTree(case_insensitive=True) + + for p in self.path_parts: + self.tree.add(p) + self.tree.finalize() + + self.lookup = self.is_blacklisted_part_aho + except ImportError: + pass + + def is_blacklisted(self, dir_name: str, dir_path: str) -> bool: + # First check if exact dir name is blacklisted. + if dir_name in self.names: + return True + + # Check if a path part is blacklisted (e.g. util/cmake) + return self.lookup(dir_path) + + def is_blacklisted_part(self, dir_path: str) -> bool: + if any(part in dir_path for part in self.path_parts): + return True + return False + + def is_blacklisted_part_aho(self, dir_path: str) -> bool: + return self.tree.search(dir_path) is not None + + +def recursive_scan(path: str, extension: str, result_paths: typing.List[str], blacklist: Blacklist): + """ Find files ending with a certain extension, filtering out blacklisted entries """ + try: + for entry in os.scandir(path): + entry: os.DirEntry = entry + + if entry.is_file() and entry.path.endswith(extension): + result_paths.append(entry.path) + elif entry.is_dir(): + if blacklist.is_blacklisted(entry.name, entry.path): + continue + recursive_scan(entry.path, extension, result_paths, blacklist) + except Exception as e: + print(e) + + +def check_for_cmake_project(pro_path: str) -> bool: + pro_dir_name = os.path.dirname(pro_path) + cmake_project_path = os.path.join(pro_dir_name, "CMakeLists.txt") + return os.path.exists(cmake_project_path) + + +def compute_stats(src_path: str, pros_with_missing_project: typing.List[str], + total_pros: int, existing_pros: int, missing_pros: int) -> dict: + stats = {} + stats['total projects'] = {'label': 'Total pro files found', + 'value': total_pros} + stats['existing projects'] = {'label': 'Existing CMakeLists.txt files found', + 'value': existing_pros} + stats['missing projects'] = {'label': 'Missing CMakeLists.txt files found', + 'value': missing_pros} + stats['missing examples'] = {'label': 'Missing examples', 'value': 0} + stats['missing tests'] = {'label': 'Missing tests', 'value': 0} + stats['missing src'] = {'label': 'Missing src/**/**', 'value': 0} + stats['missing plugins'] = {'label': 'Missing plugins', 'value': 0} + + for p in pros_with_missing_project: + rel_path = os.path.relpath(p, src_path) + if rel_path.startswith("examples"): + stats['missing examples']['value'] += 1 + elif rel_path.startswith("tests"): + stats['missing tests']['value'] += 1 + elif rel_path.startswith(os.path.join("src", "plugins")): + stats['missing plugins']['value'] += 1 + elif rel_path.startswith("src"): + stats['missing src']['value'] += 1 + + for stat in stats: + if stats[stat]['value'] > 0: + stats[stat]['percentage'] = round(stats[stat]['value'] * 100 / total_pros, 2) + return stats + + +def print_stats(src_path: str, pros_with_missing_project: typing.List[str], stats: dict, + scan_time: float, script_time: float): + + if stats['total projects']['value'] == 0: + print("No .pro files found. Did you specify a correct source path?") + return + + if stats['total projects']['value'] == stats['existing projects']['value']: + print("All projects were converted.") + else: + print("Missing CMakeLists.txt files for the following projects: \n") + + for p in pros_with_missing_project: + rel_path = os.path.relpath(p, src_path) + print(rel_path) + + print("\nStatistics: \n") + + for stat in stats: + if stats[stat]['value'] > 0: + print("{:<40}: {} ({}%)".format(stats[stat]['label'], + stats[stat]['value'], + stats[stat]['percentage'])) + + print("\n{:<40}: {:.10f} seconds".format("Scan time", scan_time)) + print("{:<40}: {:.10f} seconds".format("Total script time", script_time)) + + +def main(): + args = _parse_commandline() + src_path = os.path.abspath(args.source_directory) + pro_paths = [] + + extension = ".pro" + + blacklist_names = ["config.tests", "doc", "3rdparty", "angle"] + blacklist_path_parts = [ + os.path.join("util", "cmake") + ] + + script_start_time = default_timer() + blacklist = Blacklist(blacklist_names, blacklist_path_parts) + + scan_time_start = default_timer() + recursive_scan(src_path, extension, pro_paths, blacklist) + scan_time_end = default_timer() + scan_time = scan_time_end - scan_time_start + + total_pros = len(pro_paths) + + pros_with_missing_project = [] + for pro_path in pro_paths: + if not check_for_cmake_project(pro_path): + pros_with_missing_project.append(pro_path) + + missing_pros = len(pros_with_missing_project) + existing_pros = total_pros - missing_pros + + stats = compute_stats(src_path, pros_with_missing_project, total_pros, existing_pros, + missing_pros) + script_end_time = default_timer() + script_time = script_end_time - script_start_time + + print_stats(src_path, pros_with_missing_project, stats, scan_time, script_time) + + +if __name__ == '__main__': + main() diff --git a/util/cmake/run_pro2cmake.py b/util/cmake/run_pro2cmake.py new file mode 100755 index 0000000000..47bc6b661f --- /dev/null +++ b/util/cmake/run_pro2cmake.py @@ -0,0 +1,96 @@ +#!/usr/bin/env python3 +############################################################################# +## +## Copyright (C) 2018 The Qt Company Ltd. +## Contact: https://www.qt.io/licensing/ +## +## This file is part of the plugins of the Qt Toolkit. +## +## $QT_BEGIN_LICENSE:GPL-EXCEPT$ +## Commercial License Usage +## Licensees holding valid commercial Qt licenses may use this file in +## accordance with the commercial license agreement provided with the +## Software or, alternatively, in accordance with the terms contained in +## a written agreement between you and The Qt Company. For licensing terms +## and conditions see https://www.qt.io/terms-conditions. For further +## information use the contact form at https://www.qt.io/contact-us. +## +## GNU General Public License Usage +## Alternatively, this file may be used under the terms of the GNU +## General Public License version 3 as published by the Free Software +## Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +## included in the packaging of this file. Please review the following +## information to ensure the GNU General Public License requirements will +## be met: https://www.gnu.org/licenses/gpl-3.0.html. +## +## $QT_END_LICENSE$ +## +############################################################################# + +import glob +import os +import subprocess +import concurrent.futures +import sys +import typing + +script_path = os.path.dirname(os.path.abspath(__file__)) +base_path = os.path.dirname(script_path) +pro2cmake = os.path.join(script_path, 'pro2cmake.py') + +if len(sys.argv) > 1: + base_path = os.path.abspath(sys.argv[1]) + + +def find_all_pro_files(): + + def sorter(pro_file: str) -> str: + """ Sorter that tries to prioritize main pro files in a directory. """ + pro_file_without_suffix = pro_file.rsplit('/', 1)[-1][:-4] + dir_name = os.path.dirname(pro_file) + if dir_name.endswith('/' + pro_file_without_suffix): + return dir_name + return dir_name + "/__" + pro_file + + all_files = [] + previous_dir_name: str = None + for pro_file in sorted(glob.glob(os.path.join(base_path, '**/*.pro'), + recursive=True), + key=sorter): + dir_name = os.path.dirname(pro_file) + if dir_name == previous_dir_name: + print("Skipping:", pro_file) + else: + all_files.append(pro_file) + previous_dir_name = dir_name + return all_files + + +failed_files = [] +all_files = find_all_pro_files() +files_count = len(all_files) + +with concurrent.futures.ThreadPoolExecutor(initializer=os.nice, initargs=(10,)) as pool: + + def _process_a_file(data: typing.Tuple[str, int, int]) -> typing.Tuple[int, str, str]: + filename, index, total = data + result = subprocess.run((pro2cmake, os.path.basename(filename)), + cwd=os.path.dirname(filename), + stdout=subprocess.PIPE, stderr=subprocess.STDOUT) + stdout = 'Converted[{}/{}]: {}\n'.format(index, total, filename) + return result.returncode, filename, stdout + result.stdout.decode() + + for return_code, filename, stdout in pool.map(_process_a_file, + zip(all_files, + range(1, files_count + 1), + (files_count for _ in all_files))): + if return_code: + failed_files.append(filename) + print(stdout) + +if failed_files: + print('The following files were not successfully ' + 'converted ({} of {}):'.format(len(failed_files), files_count)) + for f in failed_files: + print(' "{}"'.format(f)) + diff --git a/util/cmake/tests/__init__.py b/util/cmake/tests/__init__.py new file mode 100644 index 0000000000..e69de29bb2 --- /dev/null +++ b/util/cmake/tests/__init__.py diff --git a/util/cmake/tests/data/comment_scope.pro b/util/cmake/tests/data/comment_scope.pro new file mode 100644 index 0000000000..be43cad37d --- /dev/null +++ b/util/cmake/tests/data/comment_scope.pro @@ -0,0 +1,6 @@ +# QtCore can't be compiled with -Wl,-no-undefined because it uses the "environ" +# variable and on FreeBSD and OpenBSD, this variable is in the final executable itself. +# OpenBSD 6.0 will include environ in libc. +freebsd|openbsd: QMAKE_LFLAGS_NOUNDEF = + +include(animation/animation.pri) diff --git a/util/cmake/tests/data/complex_assign.pro b/util/cmake/tests/data/complex_assign.pro new file mode 100644 index 0000000000..d251afcdd5 --- /dev/null +++ b/util/cmake/tests/data/complex_assign.pro @@ -0,0 +1,2 @@ +qmake-clean.commands += (cd qmake && $(MAKE) clean ":-(==)-:" '(Foo)' ) + diff --git a/util/cmake/tests/data/complex_condition.pro b/util/cmake/tests/data/complex_condition.pro new file mode 100644 index 0000000000..bc3369bd63 --- /dev/null +++ b/util/cmake/tests/data/complex_condition.pro @@ -0,0 +1,4 @@ +!system("dbus-send --session --type=signal / local.AutotestCheck.Hello >$$QMAKE_SYSTEM_NULL_DEVICE 2>&1") { + SOURCES = dbus.cpp +} + diff --git a/util/cmake/tests/data/complex_values.pro b/util/cmake/tests/data/complex_values.pro new file mode 100644 index 0000000000..4d747c1dd7 --- /dev/null +++ b/util/cmake/tests/data/complex_values.pro @@ -0,0 +1,22 @@ +linux:!static { + precompile_header { + # we'll get an error if we just use SOURCES += + no_pch_assembler.commands = $$QMAKE_CC -c $(CFLAGS) $(INCPATH) ${QMAKE_FILE_IN} -o ${QMAKE_FILE_OUT} + no_pch_assembler.dependency_type = TYPE_C + no_pch_assembler.output = ${QMAKE_VAR_OBJECTS_DIR}${QMAKE_FILE_BASE}$${first(QMAKE_EXT_OBJ)} + no_pch_assembler.input = NO_PCH_ASM + no_pch_assembler.name = compiling[no_pch] ${QMAKE_FILE_IN} + silent: no_pch_assembler.commands = @echo compiling[no_pch] ${QMAKE_FILE_IN} && $$no_pch_assembler.commands + CMAKE_ANGLE_GLES2_IMPLIB_RELEASE = libGLESv2.$${QMAKE_EXTENSION_STATICLIB} + HOST_BINS = $$[QT_HOST_BINS] + CMAKE_HOST_DATA_DIR = $$[QT_HOST_DATA/src]/ + TR_EXCLUDE += ../3rdparty/* + + QMAKE_EXTRA_COMPILERS += no_pch_assembler + NO_PCH_ASM += global/minimum-linux.S + } else { + SOURCES += global/minimum-linux.S + } + HEADERS += global/minimum-linux_p.h +} + diff --git a/util/cmake/tests/data/contains_scope.pro b/util/cmake/tests/data/contains_scope.pro new file mode 100644 index 0000000000..0f51350a45 --- /dev/null +++ b/util/cmake/tests/data/contains_scope.pro @@ -0,0 +1,4 @@ +contains(DEFINES,QT_EVAL):include(eval.pri) + +HOST_BINS = $$[QT_HOST_BINS] + diff --git a/util/cmake/tests/data/definetest.pro b/util/cmake/tests/data/definetest.pro new file mode 100644 index 0000000000..76b63d239f --- /dev/null +++ b/util/cmake/tests/data/definetest.pro @@ -0,0 +1,6 @@ +defineTest(pathIsAbsolute) { + p = $$clean_path($$1) + !isEmpty(p):isEqual(p, $$absolute_path($$p)): return(true) + return(false) +} + diff --git a/util/cmake/tests/data/else.pro b/util/cmake/tests/data/else.pro new file mode 100644 index 0000000000..bbf9c5ac9f --- /dev/null +++ b/util/cmake/tests/data/else.pro @@ -0,0 +1,6 @@ + +linux { + SOURCES += a.cpp +} else { + SOURCES += b.cpp +} diff --git a/util/cmake/tests/data/else2.pro b/util/cmake/tests/data/else2.pro new file mode 100644 index 0000000000..f2ef36ec28 --- /dev/null +++ b/util/cmake/tests/data/else2.pro @@ -0,0 +1,4 @@ + +osx: A = 1 +else: win32: B = 2 +else: C = 3 diff --git a/util/cmake/tests/data/else3.pro b/util/cmake/tests/data/else3.pro new file mode 100644 index 0000000000..0de9c2c1d9 --- /dev/null +++ b/util/cmake/tests/data/else3.pro @@ -0,0 +1,7 @@ +qtConfig(timezone) { + A = 1 +} else:win32 { + B = 2 +} else { + C = 3 +} diff --git a/util/cmake/tests/data/else4.pro b/util/cmake/tests/data/else4.pro new file mode 100644 index 0000000000..9ed676ccfa --- /dev/null +++ b/util/cmake/tests/data/else4.pro @@ -0,0 +1,6 @@ +qtConfig(timezone) { + A = 1 +} else:win32: B = 2 +else { + C = 3 +} diff --git a/util/cmake/tests/data/else5.pro b/util/cmake/tests/data/else5.pro new file mode 100644 index 0000000000..3de76af50a --- /dev/null +++ b/util/cmake/tests/data/else5.pro @@ -0,0 +1,10 @@ +# comments +qtConfig(timezone) { # bar + A = 1 +} else:win32 { + B = 2 # foo +} else { C = 3 +# baz + # foobar +} +# endcomment diff --git a/util/cmake/tests/data/else6.pro b/util/cmake/tests/data/else6.pro new file mode 100644 index 0000000000..9eaa834a19 --- /dev/null +++ b/util/cmake/tests/data/else6.pro @@ -0,0 +1,11 @@ +qtConfig(timezone) \ +{ + A = \ +1 +} \ +else:win32: \ +B = 2 +else: \ + C \ += 3 + diff --git a/util/cmake/tests/data/else7.pro b/util/cmake/tests/data/else7.pro new file mode 100644 index 0000000000..e663b1c05e --- /dev/null +++ b/util/cmake/tests/data/else7.pro @@ -0,0 +1,2 @@ +msvc:equals(QT_ARCH, i386): QMAKE_LFLAGS += /BASE:0x65000000 + diff --git a/util/cmake/tests/data/else8.pro b/util/cmake/tests/data/else8.pro new file mode 100644 index 0000000000..6d4d5f01ed --- /dev/null +++ b/util/cmake/tests/data/else8.pro @@ -0,0 +1,5 @@ +qtConfig(timezone) { A = 1 } else:win32: {\ +B = 2 \ +} else: \ + C \ += 3 \ diff --git a/util/cmake/tests/data/escaped_value.pro b/util/cmake/tests/data/escaped_value.pro new file mode 100644 index 0000000000..7c95b1fc30 --- /dev/null +++ b/util/cmake/tests/data/escaped_value.pro @@ -0,0 +1,2 @@ +MODULE_AUX_INCLUDES = \ + \$\$QT_MODULE_INCLUDE_BASE/QtANGLE diff --git a/util/cmake/tests/data/for.pro b/util/cmake/tests/data/for.pro new file mode 100644 index 0000000000..5751432980 --- /dev/null +++ b/util/cmake/tests/data/for.pro @@ -0,0 +1,11 @@ +SOURCES = main.cpp +for (config, SIMD) { + uc = $$upper($$config) + DEFINES += QT_COMPILER_SUPPORTS_$${uc} + + add_cflags { + cflags = QMAKE_CFLAGS_$${uc} + !defined($$cflags, var): error("This compiler does not support $${uc}") + QMAKE_CXXFLAGS += $$eval($$cflags) + } +} diff --git a/util/cmake/tests/data/function_if.pro b/util/cmake/tests/data/function_if.pro new file mode 100644 index 0000000000..9af018f864 --- /dev/null +++ b/util/cmake/tests/data/function_if.pro @@ -0,0 +1,4 @@ +pathIsAbsolute($$CMAKE_HOST_DATA_DIR) { + CMAKE_HOST_DATA_DIR = $$[QT_HOST_DATA/src]/ +} + diff --git a/util/cmake/tests/data/include.pro b/util/cmake/tests/data/include.pro new file mode 100644 index 0000000000..22d8a40919 --- /dev/null +++ b/util/cmake/tests/data/include.pro @@ -0,0 +1,3 @@ +A = 42 +include(foo) # load foo +B=23 diff --git a/util/cmake/tests/data/lc.pro b/util/cmake/tests/data/lc.pro new file mode 100644 index 0000000000..def80e7c95 --- /dev/null +++ b/util/cmake/tests/data/lc.pro @@ -0,0 +1,10 @@ +TEMPLATE=subdirs +SUBDIRS=\ + qmacstyle \ + qstyle \ + qstyleoption \ + qstylesheetstyle \ + +!qtConfig(private_tests): SUBDIRS -= \ + qstylesheetstyle \ + diff --git a/util/cmake/tests/data/load.pro b/util/cmake/tests/data/load.pro new file mode 100644 index 0000000000..c9717e9832 --- /dev/null +++ b/util/cmake/tests/data/load.pro @@ -0,0 +1,3 @@ +A = 42 +load(foo)# load foo +B=23 diff --git a/util/cmake/tests/data/multiline_assign.pro b/util/cmake/tests/data/multiline_assign.pro new file mode 100644 index 0000000000..42a3d0a674 --- /dev/null +++ b/util/cmake/tests/data/multiline_assign.pro @@ -0,0 +1,4 @@ +A = 42 \ + 43 \ + 44 +B=23 diff --git a/util/cmake/tests/data/quoted.pro b/util/cmake/tests/data/quoted.pro new file mode 100644 index 0000000000..61682aa0d0 --- /dev/null +++ b/util/cmake/tests/data/quoted.pro @@ -0,0 +1,5 @@ +if(linux*|hurd*):!cross_compile:!static:!*-armcc* { + prog=$$quote(if (/program interpreter: (.*)]/) { print $1; }) + DEFINES += ELF_INTERPRETER=\\\"$$system(LC_ALL=C readelf -l /bin/ls | perl -n -e \'$$prog\')\\\" +} + diff --git a/util/cmake/tests/data/single_line_for.pro b/util/cmake/tests/data/single_line_for.pro new file mode 100644 index 0000000000..806d08a49c --- /dev/null +++ b/util/cmake/tests/data/single_line_for.pro @@ -0,0 +1,4 @@ +for(d, sd): \ + exists($$d/$${d}.pro): \ + SUBDIRS += $$d + diff --git a/util/cmake/tests/data/sql.pro b/util/cmake/tests/data/sql.pro new file mode 100644 index 0000000000..a9d7fc7c5a --- /dev/null +++ b/util/cmake/tests/data/sql.pro @@ -0,0 +1,3 @@ +TEMPLATE = subdirs +SUBDIRS = \ + kernel \ diff --git a/util/cmake/tests/data/standardpaths.pro b/util/cmake/tests/data/standardpaths.pro new file mode 100644 index 0000000000..4b45788e4f --- /dev/null +++ b/util/cmake/tests/data/standardpaths.pro @@ -0,0 +1,17 @@ +win32 { + !winrt { + SOURCES +=io/qstandardpaths_win.cpp + } else { + SOURCES +=io/qstandardpaths_winrt.cpp + } +} else:unix { + mac { + OBJECTIVE_SOURCES += io/qstandardpaths_mac.mm + } else:android:!android-embedded { + SOURCES += io/qstandardpaths_android.cpp + } else:haiku { + SOURCES += io/qstandardpaths_haiku.cpp + } else { + SOURCES += io/qstandardpaths_unix.cpp + } +} diff --git a/util/cmake/tests/data/unset.pro b/util/cmake/tests/data/unset.pro new file mode 100644 index 0000000000..7ffb0582f1 --- /dev/null +++ b/util/cmake/tests/data/unset.pro @@ -0,0 +1,2 @@ +unset(f16c_cxx) + diff --git a/util/cmake/tests/test_lc_fixup.py b/util/cmake/tests/test_lc_fixup.py new file mode 100755 index 0000000000..d3680e895d --- /dev/null +++ b/util/cmake/tests/test_lc_fixup.py @@ -0,0 +1,46 @@ +#!/usr/bin/env python3 +############################################################################# +## +## Copyright (C) 2018 The Qt Company Ltd. +## Contact: https://www.qt.io/licensing/ +## +## This file is part of the plugins of the Qt Toolkit. +## +## $QT_BEGIN_LICENSE:GPL-EXCEPT$ +## Commercial License Usage +## Licensees holding valid commercial Qt licenses may use this file in +## accordance with the commercial license agreement provided with the +## Software or, alternatively, in accordance with the terms contained in +## a written agreement between you and The Qt Company. For licensing terms +## and conditions see https://www.qt.io/terms-conditions. For further +## information use the contact form at https://www.qt.io/contact-us. +## +## GNU General Public License Usage +## Alternatively, this file may be used under the terms of the GNU +## General Public License version 3 as published by the Free Software +## Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +## included in the packaging of this file. Please review the following +## information to ensure the GNU General Public License requirements will +## be met: https://www.gnu.org/licenses/gpl-3.0.html. +## +## $QT_END_LICENSE$ +## +############################################################################# + +from pro2cmake import fixup_linecontinuation + +from textwrap import dedent + + +def test_no_change(): + input = "test \\\nline2\n line3" + result = fixup_linecontinuation(input) + assert input == result + + +def test_fix(): + input = "test \\\t\nline2\\\n line3\\ \nline4 \\ \t\nline5\\\n\n\n" + result = fixup_linecontinuation(input) + assert 'test \\\nline2 \\\n line3 \\\nline4 \\\nline5 \\\n\n\n' == result + + diff --git a/util/cmake/tests/test_logic_mapping.py b/util/cmake/tests/test_logic_mapping.py new file mode 100755 index 0000000000..c477aa8351 --- /dev/null +++ b/util/cmake/tests/test_logic_mapping.py @@ -0,0 +1,186 @@ +#!/usr/bin/env python3 +############################################################################# +## +## Copyright (C) 2018 The Qt Company Ltd. +## Contact: https://www.qt.io/licensing/ +## +## This file is part of the plugins of the Qt Toolkit. +## +## $QT_BEGIN_LICENSE:GPL-EXCEPT$ +## Commercial License Usage +## Licensees holding valid commercial Qt licenses may use this file in +## accordance with the commercial license agreement provided with the +## Software or, alternatively, in accordance with the terms contained in +## a written agreement between you and The Qt Company. For licensing terms +## and conditions see https://www.qt.io/terms-conditions. For further +## information use the contact form at https://www.qt.io/contact-us. +## +## GNU General Public License Usage +## Alternatively, this file may be used under the terms of the GNU +## General Public License version 3 as published by the Free Software +## Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +## included in the packaging of this file. Please review the following +## information to ensure the GNU General Public License requirements will +## be met: https://www.gnu.org/licenses/gpl-3.0.html. +## +## $QT_END_LICENSE$ +## +############################################################################# + +from pro2cmake import simplify_condition + + +def validate_simplify(input: str, expected: str) -> None: + output = simplify_condition(input) + assert output == expected + + +def validate_simplify_unchanged(input: str) -> None: + validate_simplify(input, input) + + +def test_simplify_on(): + validate_simplify_unchanged('ON') + + +def test_simplify_off(): + validate_simplify_unchanged('OFF') + + +def test_simplify_not_on(): + validate_simplify('NOT ON', 'OFF') + + +def test_simplify_not_off(): + validate_simplify('NOT OFF', 'ON') + + +def test_simplify_isEmpty(): + validate_simplify_unchanged('isEmpty(foo)') + + +def test_simplify_not_isEmpty(): + validate_simplify_unchanged('NOT isEmpty(foo)') + + +def test_simplify_simple_and(): + validate_simplify_unchanged('QT_FEATURE_bar AND QT_FEATURE_foo') + + +def test_simplify_simple_or(): + validate_simplify_unchanged('QT_FEATURE_bar OR QT_FEATURE_foo') + + +def test_simplify_simple_not(): + validate_simplify_unchanged('NOT QT_FEATURE_foo') + + +def test_simplify_simple_and_reorder(): + validate_simplify('QT_FEATURE_foo AND QT_FEATURE_bar', 'QT_FEATURE_bar AND QT_FEATURE_foo') + + +def test_simplify_simple_or_reorder(): + validate_simplify('QT_FEATURE_foo OR QT_FEATURE_bar', 'QT_FEATURE_bar OR QT_FEATURE_foo') + + +def test_simplify_unix_or_win32(): + validate_simplify('WIN32 OR UNIX', 'ON') + + +def test_simplify_unix_or_win32_or_foobar_or_barfoo(): + validate_simplify('WIN32 OR UNIX OR foobar OR barfoo', 'ON') + + +def test_simplify_not_not_bar(): + validate_simplify(' NOT NOT bar ', 'bar') + + +def test_simplify_not_unix(): + validate_simplify('NOT UNIX', 'WIN32') + + +def test_simplify_not_win32(): + validate_simplify('NOT WIN32', 'UNIX') + + +def test_simplify_unix_and_win32(): + validate_simplify('WIN32 AND UNIX', 'OFF') + + +def test_simplify_unix_or_win32(): + validate_simplify('WIN32 OR UNIX', 'ON') + + +def test_simplify_unix_and_win32_or_foobar_or_barfoo(): + validate_simplify('WIN32 AND foobar AND UNIX AND barfoo', 'OFF') + + +def test_simplify_watchos_and_win32(): + validate_simplify('APPLE_WATCHOS AND WIN32', 'OFF') + + +def test_simplify_win32_and_watchos(): + validate_simplify('WIN32 AND APPLE_WATCHOS', 'OFF') + + +def test_simplify_apple_and_appleosx(): + validate_simplify('APPLE AND APPLE_OSX', 'APPLE_OSX') + + +def test_simplify_apple_or_appleosx(): + validate_simplify('APPLE OR APPLE_OSX', 'APPLE') + + +def test_simplify_apple_or_appleosx_level1(): + validate_simplify('foobar AND (APPLE OR APPLE_OSX )', 'APPLE AND foobar') + + +def test_simplify_apple_or_appleosx_level1_double(): + validate_simplify('foobar AND (APPLE OR APPLE_OSX )', 'APPLE AND foobar') + + +def test_simplify_apple_or_appleosx_level1_double_with_extra_spaces(): + validate_simplify('foobar AND (APPLE OR APPLE_OSX ) ' + 'AND ( APPLE_OSX OR APPLE )', 'APPLE AND foobar') + + +def test_simplify_apple_or_appleosx_level2(): + validate_simplify('foobar AND ( ( APPLE OR APPLE_WATCHOS ) ' + 'OR APPLE_OSX ) AND ( APPLE_OSX OR APPLE ) ' + 'AND ( (WIN32 OR WINRT) OR UNIX) ', 'APPLE AND foobar') + + +def test_simplify_not_apple_and_appleosx(): + validate_simplify('NOT APPLE AND APPLE_OSX', 'OFF') + + +def test_simplify_unix_and_bar_or_win32(): + validate_simplify('WIN32 AND bar AND UNIX', 'OFF') + + +def test_simplify_unix_or_bar_or_win32(): + validate_simplify('WIN32 OR bar OR UNIX', 'ON') + + +def test_simplify_complex_true(): + validate_simplify('WIN32 OR ( APPLE OR UNIX)', 'ON') + + +def test_simplify_apple_unix_freebsd(): + validate_simplify('( APPLE OR ( UNIX OR FREEBSD ))', 'UNIX') + + +def test_simplify_apple_unix_freebsd_foobar(): + validate_simplify('( APPLE OR ( UNIX OR FREEBSD ) OR foobar)', + 'UNIX OR foobar') + + +def test_simplify_complex_false(): + validate_simplify('WIN32 AND foobar AND ( ' + 'APPLE OR ( UNIX OR FREEBSD ))', + 'OFF') + + +def test_simplify_android_not_apple(): + validate_simplify('ANDROID AND NOT ANDROID_EMBEDDED AND NOT APPLE_OSX', + 'ANDROID AND NOT ANDROID_EMBEDDED') diff --git a/util/cmake/tests/test_operations.py b/util/cmake/tests/test_operations.py new file mode 100755 index 0000000000..3ea2f76a43 --- /dev/null +++ b/util/cmake/tests/test_operations.py @@ -0,0 +1,57 @@ +#!/usr/bin/env python3 +############################################################################# +## +## Copyright (C) 2018 The Qt Company Ltd. +## Contact: https://www.qt.io/licensing/ +## +## This file is part of the plugins of the Qt Toolkit. +## +## $QT_BEGIN_LICENSE:GPL-EXCEPT$ +## Commercial License Usage +## Licensees holding valid commercial Qt licenses may use this file in +## accordance with the commercial license agreement provided with the +## Software or, alternatively, in accordance with the terms contained in +## a written agreement between you and The Qt Company. For licensing terms +## and conditions see https://www.qt.io/terms-conditions. For further +## information use the contact form at https://www.qt.io/contact-us. +## +## GNU General Public License Usage +## Alternatively, this file may be used under the terms of the GNU +## General Public License version 3 as published by the Free Software +## Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +## included in the packaging of this file. Please review the following +## information to ensure the GNU General Public License requirements will +## be met: https://www.gnu.org/licenses/gpl-3.0.html. +## +## $QT_END_LICENSE$ +## +############################################################################# + +from pro2cmake import AddOperation, SetOperation, UniqueAddOperation, RemoveOperation + +def test_add_operation(): + op = AddOperation(['bar', 'buz']) + + result = op.process(['foo', 'bar']) + assert ['foo', 'bar', 'bar', 'buz'] == result + + +def test_uniqueadd_operation(): + op = UniqueAddOperation(['bar', 'buz']) + + result = op.process(['foo', 'bar']) + assert ['foo', 'bar', 'buz'] == result + + +def test_set_operation(): + op = SetOperation(['bar', 'buz']) + + result = op.process(['foo', 'bar']) + assert ['bar', 'buz'] == result + + +def test_remove_operation(): + op = RemoveOperation(['bar', 'buz']) + + result = op.process(['foo', 'bar']) + assert ['foo', '-buz'] == result diff --git a/util/cmake/tests/test_parsing.py b/util/cmake/tests/test_parsing.py new file mode 100755 index 0000000000..79ad0a4945 --- /dev/null +++ b/util/cmake/tests/test_parsing.py @@ -0,0 +1,307 @@ +#!/usr/bin/env python3 +############################################################################# +## +## Copyright (C) 2018 The Qt Company Ltd. +## Contact: https://www.qt.io/licensing/ +## +## This file is part of the plugins of the Qt Toolkit. +## +## $QT_BEGIN_LICENSE:GPL-EXCEPT$ +## Commercial License Usage +## Licensees holding valid commercial Qt licenses may use this file in +## accordance with the commercial license agreement provided with the +## Software or, alternatively, in accordance with the terms contained in +## a written agreement between you and The Qt Company. For licensing terms +## and conditions see https://www.qt.io/terms-conditions. For further +## information use the contact form at https://www.qt.io/contact-us. +## +## GNU General Public License Usage +## Alternatively, this file may be used under the terms of the GNU +## General Public License version 3 as published by the Free Software +## Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +## included in the packaging of this file. Please review the following +## information to ensure the GNU General Public License requirements will +## be met: https://www.gnu.org/licenses/gpl-3.0.html. +## +## $QT_END_LICENSE$ +## +############################################################################# + +import os +from pro2cmake import QmakeParser + + +_tests_path = os.path.dirname(os.path.abspath(__file__)) + + +def validate_op(key, op, value, to_validate): + assert key == to_validate['key'] + assert op == to_validate['operation'] + assert value == to_validate.get('value', None) + + +def validate_single_op(key, op, value, to_validate): + assert len(to_validate) == 1 + validate_op(key, op, value, to_validate[0]) + + +def evaluate_condition(to_validate): + assert 'condition' in to_validate + assert 'statements' in to_validate + + return (to_validate['condition'], + to_validate['statements'], + to_validate.get('else_statements', {})) + + +def validate_default_else_test(file_name): + result = parse_file(file_name) + assert len(result) == 1 + + (cond, if_branch, else_branch) = evaluate_condition(result[0]) + assert cond == 'qtConfig(timezone)' + validate_single_op('A', '=', ['1'], if_branch) + + assert len(else_branch) == 1 + (cond2, if2_branch, else2_branch) = evaluate_condition(else_branch[0]) + assert cond2 == 'win32' + validate_single_op('B', '=', ['2'], if2_branch) + validate_single_op('C', '=', ['3'], else2_branch) + + +def parse_file(file): + p = QmakeParser(debug=True) + result = p.parseFile(file) + + print('\n\n#### Parser result:') + print(result) + print('\n#### End of parser result.\n') + + print('\n\n####Parser result dictionary:') + print(result.asDict()) + print('\n#### End of parser result dictionary.\n') + + result_dictionary = result.asDict() + + assert len(result_dictionary) == 1 + + return result_dictionary['statements'] + + +def test_else(): + result = parse_file(_tests_path + '/data/else.pro') + assert len(result) == 1 + + (cond, if_branch, else_branch) = evaluate_condition(result[0]) + + assert cond == 'linux' + validate_single_op('SOURCES', '+=', ['a.cpp'], if_branch) + validate_single_op('SOURCES', '+=', ['b.cpp'], else_branch) + + +def test_else2(): + result = parse_file(_tests_path + '/data/else2.pro') + assert len(result) == 1 + + (cond, if_branch, else_branch) = evaluate_condition(result[0]) + assert cond == 'osx' + validate_single_op('A', '=', ['1'], if_branch) + + assert len(else_branch) == 1 + (cond2, if2_branch, else2_branch) = evaluate_condition(else_branch[0]) + assert cond2 == 'win32' + validate_single_op('B', '=', ['2'], if2_branch) + + validate_single_op('C', '=', ['3'], else2_branch) + + +def test_else3(): + validate_default_else_test(_tests_path + '/data/else3.pro') + + +def test_else4(): + validate_default_else_test(_tests_path + '/data/else4.pro') + + +def test_else5(): + validate_default_else_test(_tests_path + '/data/else5.pro') + + +def test_else6(): + validate_default_else_test(_tests_path + '/data/else6.pro') + + +def test_else7(): + result = parse_file(_tests_path + '/data/else7.pro') + assert len(result) == 1 + + +def test_else8(): + validate_default_else_test(_tests_path + '/data/else8.pro') + + +def test_multiline_assign(): + result = parse_file(_tests_path + '/data/multiline_assign.pro') + assert len(result) == 2 + validate_op('A', '=', ['42', '43', '44'], result[0]) + validate_op('B', '=', ['23'], result[1]) + + +def test_include(): + result = parse_file(_tests_path + '/data/include.pro') + assert len(result) == 3 + validate_op('A', '=', ['42'], result[0]) + include = result[1] + assert len(include) == 1 + assert include.get('included', '') == 'foo' + validate_op('B', '=', ['23'], result[2]) + + +def test_load(): + result = parse_file(_tests_path + '/data/load.pro') + assert len(result) == 3 + validate_op('A', '=', ['42'], result[0]) + load = result[1] + assert len(load) == 1 + assert load.get('loaded', '') == 'foo' + validate_op('B', '=', ['23'], result[2]) + + +def test_definetest(): + result = parse_file(_tests_path + '/data/definetest.pro') + assert len(result) == 1 + assert result[0] == [] + + +def test_for(): + result = parse_file(_tests_path + '/data/for.pro') + assert len(result) == 2 + validate_op('SOURCES', '=', ['main.cpp'], result[0]) + assert result[1] == [] + + +def test_single_line_for(): + result = parse_file(_tests_path + '/data/single_line_for.pro') + assert len(result) == 1 + assert result[0] == [] + + +def test_unset(): + result = parse_file(_tests_path + '/data/unset.pro') + assert len(result) == 1 + assert result[0] == [] + + +def test_quoted(): + result = parse_file(_tests_path + '/data/quoted.pro') + assert len(result) == 1 + + +def test_complex_values(): + result = parse_file(_tests_path + '/data/complex_values.pro') + assert len(result) == 1 + + +def test_function_if(): + result = parse_file(_tests_path + '/data/function_if.pro') + assert len(result) == 1 + + +def test_realworld_standardpaths(): + result = parse_file(_tests_path + '/data/standardpaths.pro') + + (cond, if_branch, else_branch) = evaluate_condition(result[0]) + assert cond == 'win32' + assert len(if_branch) == 1 + assert len(else_branch) == 1 + + # win32: + (cond1, if_branch1, else_branch1) = evaluate_condition(if_branch[0]) + assert cond1 == '!winrt' + assert len(if_branch1) == 1 + validate_op('SOURCES', '+=', ['io/qstandardpaths_win.cpp'], if_branch1[0]) + assert len(else_branch1) == 1 + validate_op('SOURCES', '+=', ['io/qstandardpaths_winrt.cpp'], else_branch1[0]) + + # unix: + (cond2, if_branch2, else_branch2) = evaluate_condition(else_branch[0]) + assert cond2 == 'unix' + assert len(if_branch2) == 1 + assert len(else_branch2) == 0 + + # mac / else: + (cond3, if_branch3, else_branch3) = evaluate_condition(if_branch2[0]) + assert cond3 == 'mac' + assert len(if_branch3) == 1 + validate_op('OBJECTIVE_SOURCES', '+=', ['io/qstandardpaths_mac.mm'], if_branch3[0]) + assert len(else_branch3) == 1 + + # android / else: + (cond4, if_branch4, else_branch4) = evaluate_condition(else_branch3[0]) + assert cond4 == 'android && !android-embedded' + assert len(if_branch4) == 1 + validate_op('SOURCES', '+=', ['io/qstandardpaths_android.cpp'], if_branch4[0]) + assert len(else_branch4) == 1 + + # haiku / else: + (cond5, if_branch5, else_branch5) = evaluate_condition(else_branch4[0]) + assert cond5 == 'haiku' + assert len(if_branch5) == 1 + validate_op('SOURCES', '+=', ['io/qstandardpaths_haiku.cpp'], if_branch5[0]) + assert len(else_branch5) == 1 + validate_op('SOURCES', '+=', ['io/qstandardpaths_unix.cpp'], else_branch5[0]) + + +def test_realworld_comment_scope(): + result = parse_file(_tests_path + '/data/comment_scope.pro') + assert len(result) == 2 + (cond, if_branch, else_branch) = evaluate_condition(result[0]) + assert cond == 'freebsd|openbsd' + assert len(if_branch) == 1 + validate_op('QMAKE_LFLAGS_NOUNDEF', '=', None, if_branch[0]) + + assert result[1].get('included', '') == 'animation/animation.pri' + + +def test_realworld_contains_scope(): + result = parse_file(_tests_path + '/data/contains_scope.pro') + assert len(result) == 2 + + +def test_realworld_complex_assign(): + result = parse_file(_tests_path + '/data/complex_assign.pro') + assert len(result) == 1 + validate_op('qmake-clean.commands', '+=', '( cd qmake && $(MAKE) clean ":-(==)-:" \'(Foo)\' )'.split(), + result[0]) + + +def test_realworld_complex_condition(): + result = parse_file(_tests_path + '/data/complex_condition.pro') + assert len(result) == 1 + (cond, if_branch, else_branch) = evaluate_condition(result[0]) + assert cond == '!system("dbus-send --session --type=signal / ' \ + 'local.AutotestCheck.Hello >$$QMAKE_SYSTEM_NULL_DEVICE ' \ + '2>&1")' + assert len(if_branch) == 1 + validate_op('SOURCES', '=', ['dbus.cpp'], if_branch[0]) + + assert len(else_branch) == 0 + + +def test_realworld_sql(): + result = parse_file(_tests_path + '/data/sql.pro') + assert len(result) == 2 + validate_op('TEMPLATE', '=', ['subdirs'], result[0]) + validate_op('SUBDIRS', '=', ['kernel'], result[1]) + + +def test_realworld_qtconfig(): + result = parse_file(_tests_path + '/data/escaped_value.pro') + assert len(result) == 1 + validate_op('MODULE_AUX_INCLUDES', '=', ['\\$\\$QT_MODULE_INCLUDE_BASE/QtANGLE'], result[0]) + + +def test_realworld_lc(): + result = parse_file(_tests_path + '/data/lc.pro') + assert len(result) == 3 + diff --git a/util/cmake/tests/test_scope_handling.py b/util/cmake/tests/test_scope_handling.py new file mode 100755 index 0000000000..8bca8c8ec5 --- /dev/null +++ b/util/cmake/tests/test_scope_handling.py @@ -0,0 +1,338 @@ +#!/usr/bin/env python3 +############################################################################# +## +## Copyright (C) 2018 The Qt Company Ltd. +## Contact: https://www.qt.io/licensing/ +## +## This file is part of the plugins of the Qt Toolkit. +## +## $QT_BEGIN_LICENSE:GPL-EXCEPT$ +## Commercial License Usage +## Licensees holding valid commercial Qt licenses may use this file in +## accordance with the commercial license agreement provided with the +## Software or, alternatively, in accordance with the terms contained in +## a written agreement between you and The Qt Company. For licensing terms +## and conditions see https://www.qt.io/terms-conditions. For further +## information use the contact form at https://www.qt.io/contact-us. +## +## GNU General Public License Usage +## Alternatively, this file may be used under the terms of the GNU +## General Public License version 3 as published by the Free Software +## Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +## included in the packaging of this file. Please review the following +## information to ensure the GNU General Public License requirements will +## be met: https://www.gnu.org/licenses/gpl-3.0.html. +## +## $QT_END_LICENSE$ +## +############################################################################# + +from pro2cmake import Scope, SetOperation, merge_scopes, recursive_evaluate_scope + +import pytest +import typing + +ScopeList = typing.List[Scope] + +def _map_to_operation(**kwargs): + result = {} # type: typing.Mapping[str, typing.List[SetOperation]] + for (key, value) in kwargs.items(): + result[key] = [SetOperation(value)] + return result + + +def _new_scope(*, parent_scope=None, condition='', **kwargs) -> Scope: + return Scope(parent_scope=parent_scope, + file='file1', condition=condition, operations=_map_to_operation(**kwargs)) + + +def _evaluate_scopes(scopes: ScopeList) -> ScopeList: + for s in scopes: + if not s.parent: + recursive_evaluate_scope(s) + return scopes + + +def _validate(input_scopes: ScopeList, output_scopes: ScopeList): + merged_scopes = merge_scopes(input_scopes) + assert merged_scopes == output_scopes + + +def test_evaluate_one_scope(): + scope = _new_scope(condition='QT_FEATURE_foo', test1='bar') + + input_scope = scope + recursive_evaluate_scope(scope) + assert scope == input_scope + + +def test_evaluate_child_scope(): + scope = _new_scope(condition='QT_FEATURE_foo', test1='bar') + _new_scope(parent_scope=scope, condition='QT_FEATURE_bar', test2='bar') + + input_scope = scope + recursive_evaluate_scope(scope) + + assert scope.total_condition == 'QT_FEATURE_foo' + assert len(scope.children) == 1 + assert scope.getString('test1') == 'bar' + assert scope.getString('test2', 'not found') == 'not found' + + child = scope.children[0] + assert child.total_condition == 'QT_FEATURE_bar AND QT_FEATURE_foo' + assert child.getString('test1', 'not found') == 'not found' + assert child.getString('test2') == 'bar' + + +def test_evaluate_two_child_scopes(): + scope = _new_scope(condition='QT_FEATURE_foo', test1='bar') + _new_scope(parent_scope=scope, condition='QT_FEATURE_bar', test2='bar') + _new_scope(parent_scope=scope, condition='QT_FEATURE_buz', test3='buz') + + input_scope = scope + recursive_evaluate_scope(scope) + + assert scope.total_condition == 'QT_FEATURE_foo' + assert len(scope.children) == 2 + assert scope.getString('test1') == 'bar' + assert scope.getString('test2', 'not found') == 'not found' + assert scope.getString('test3', 'not found') == 'not found' + + child1 = scope.children[0] + assert child1.total_condition == 'QT_FEATURE_bar AND QT_FEATURE_foo' + assert child1.getString('test1', 'not found') == 'not found' + assert child1.getString('test2') == 'bar' + assert child1.getString('test3', 'not found') == 'not found' + + child2 = scope.children[1] + assert child2.total_condition == 'QT_FEATURE_buz AND QT_FEATURE_foo' + assert child2.getString('test1', 'not found') == 'not found' + assert child2.getString('test2') == '' + assert child2.getString('test3', 'not found') == 'buz' + + +def test_evaluate_else_child_scopes(): + scope = _new_scope(condition='QT_FEATURE_foo', test1='bar') + _new_scope(parent_scope=scope, condition='QT_FEATURE_bar', test2='bar') + _new_scope(parent_scope=scope, condition='else', test3='buz') + + input_scope = scope + recursive_evaluate_scope(scope) + + assert scope.total_condition == 'QT_FEATURE_foo' + assert len(scope.children) == 2 + assert scope.getString('test1') == 'bar' + assert scope.getString('test2', 'not found') == 'not found' + assert scope.getString('test3', 'not found') == 'not found' + + child1 = scope.children[0] + assert child1.total_condition == 'QT_FEATURE_bar AND QT_FEATURE_foo' + assert child1.getString('test1', 'not found') == 'not found' + assert child1.getString('test2') == 'bar' + assert child1.getString('test3', 'not found') == 'not found' + + child2 = scope.children[1] + assert child2.total_condition == 'QT_FEATURE_foo AND NOT QT_FEATURE_bar' + assert child2.getString('test1', 'not found') == 'not found' + assert child2.getString('test2') == '' + assert child2.getString('test3', 'not found') == 'buz' + + +def test_evaluate_invalid_else_child_scopes(): + scope = _new_scope(condition='QT_FEATURE_foo', test1='bar') + _new_scope(parent_scope=scope, condition='else', test3='buz') + _new_scope(parent_scope=scope, condition='QT_FEATURE_bar', test2='bar') + + input_scope = scope + with pytest.raises(AssertionError): + recursive_evaluate_scope(scope) + + +def test_merge_empty_scope_list(): + _validate([], []) + + +def test_merge_one_scope(): + scopes = [_new_scope(test='foo')] + + recursive_evaluate_scope(scopes[0]) + + _validate(scopes, scopes) + + +def test_merge_one_on_scope(): + scopes = [_new_scope(condition='ON', test='foo')] + + recursive_evaluate_scope(scopes[0]) + + _validate(scopes, scopes) + + +def test_merge_one_off_scope(): + scopes = [_new_scope(condition='OFF', test='foo')] + + recursive_evaluate_scope(scopes[0]) + + _validate(scopes, []) + + +def test_merge_one_conditioned_scope(): + scopes = [_new_scope(condition='QT_FEATURE_foo', test='foo')] + + recursive_evaluate_scope(scopes[0]) + + _validate(scopes, scopes) + + +def test_merge_two_scopes_with_same_condition(): + scopes = [_new_scope(condition='QT_FEATURE_bar', test='foo'), + _new_scope(condition='QT_FEATURE_bar', test2='bar')] + + recursive_evaluate_scope(scopes[0]) + recursive_evaluate_scope(scopes[1]) + + result = merge_scopes(scopes) + + assert len(result) == 1 + r0 = result[0] + assert r0.total_condition == 'QT_FEATURE_bar' + assert r0.getString('test') == 'foo' + assert r0.getString('test2') == 'bar' + + +def test_merge_three_scopes_two_with_same_condition(): + scopes = [_new_scope(condition='QT_FEATURE_bar', test='foo'), + _new_scope(condition='QT_FEATURE_baz', test1='buz'), + _new_scope(condition='QT_FEATURE_bar', test2='bar')] + + recursive_evaluate_scope(scopes[0]) + recursive_evaluate_scope(scopes[1]) + recursive_evaluate_scope(scopes[2]) + + result = merge_scopes(scopes) + + assert len(result) == 2 + r0 = result[0] + assert r0.total_condition == 'QT_FEATURE_bar' + assert r0.getString('test') == 'foo' + assert r0.getString('test2') == 'bar' + + assert result[1] == scopes[1] + + +def test_merge_two_unrelated_on_off_scopes(): + scopes = [_new_scope(condition='ON', test='foo'), + _new_scope(condition='OFF', test2='bar')] + + recursive_evaluate_scope(scopes[0]) + recursive_evaluate_scope(scopes[1]) + + _validate(scopes, [scopes[0]]) + + +def test_merge_two_unrelated_on_off_scopes(): + scopes = [_new_scope(condition='OFF', test='foo'), + _new_scope(condition='ON', test2='bar')] + + recursive_evaluate_scope(scopes[0]) + recursive_evaluate_scope(scopes[1]) + + _validate(scopes, [scopes[1]]) + + +def test_merge_parent_child_scopes_with_different_conditions(): + scope = _new_scope(condition='FOO', test1='parent') + scopes = [scope, _new_scope(parent_scope=scope, condition='bar', test2='child')] + + recursive_evaluate_scope(scope) + + _validate(scopes, scopes) + + +def test_merge_parent_child_scopes_with_same_conditions(): + scope = _new_scope(condition='FOO AND bar', test1='parent') + scopes = [scope, _new_scope(parent_scope=scope, condition='FOO AND bar', test2='child')] + + recursive_evaluate_scope(scope) + + result = merge_scopes(scopes) + + assert len(result) == 1 + r0 = result[0] + assert r0.parent == None + assert r0.total_condition == 'FOO AND bar' + assert r0.getString('test1') == 'parent' + assert r0.getString('test2') == 'child' + + +def test_merge_parent_child_scopes_with_on_child_condition(): + scope = _new_scope(condition='FOO AND bar', test1='parent') + scopes = [scope, _new_scope(parent_scope=scope, condition='ON', test2='child')] + + recursive_evaluate_scope(scope) + + result = merge_scopes(scopes) + + assert len(result) == 1 + r0 = result[0] + assert r0.parent == None + assert r0.total_condition == 'FOO AND bar' + assert r0.getString('test1') == 'parent' + assert r0.getString('test2') == 'child' + + +# Real world examples: + +# qstandardpaths selection: + +def test_qstandardpaths_scopes(): + # top level: + scope1 = _new_scope(condition='ON', scope_id=1) + + # win32 { + scope2 = _new_scope(parent_scope=scope1, condition='WIN32') + # !winrt { + # SOURCES += io/qstandardpaths_win.cpp + scope3 = _new_scope(parent_scope=scope2, condition='NOT WINRT', + SOURCES='qsp_win.cpp') + # } else { + # SOURCES += io/qstandardpaths_winrt.cpp + scope4 = _new_scope(parent_scope=scope2, condition='else', + SOURCES='qsp_winrt.cpp') + # } + # else: unix { + scope5 = _new_scope(parent_scope=scope1, condition='else') + scope6 = _new_scope(parent_scope=scope5, condition='UNIX') + # mac { + # OBJECTIVE_SOURCES += io/qstandardpaths_mac.mm + scope7 = _new_scope(parent_scope=scope6, condition='APPLE_OSX', SOURCES='qsp_mac.mm') + # } else:android:!android-embedded { + # SOURCES += io/qstandardpaths_android.cpp + scope8 = _new_scope(parent_scope=scope6, condition='else') + scope9 = _new_scope(parent_scope=scope8, + condition='ANDROID AND NOT ANDROID_EMBEDDED', + SOURCES='qsp_android.cpp') + # } else:haiku { + # SOURCES += io/qstandardpaths_haiku.cpp + scope10 = _new_scope(parent_scope=scope8, condition='else') + scope11 = _new_scope(parent_scope=scope10, condition='HAIKU', SOURCES='qsp_haiku.cpp') + # } else { + # SOURCES +=io/qstandardpaths_unix.cpp + scope12 = _new_scope(parent_scope=scope10, condition='else', SOURCES='qsp_unix.cpp') + # } + # } + + recursive_evaluate_scope(scope1) + + assert scope1.total_condition == 'ON' + assert scope2.total_condition == 'WIN32' + assert scope3.total_condition == 'WIN32 AND NOT WINRT' + assert scope4.total_condition == 'WINRT' + assert scope5.total_condition == 'UNIX' + assert scope6.total_condition == 'UNIX' + assert scope7.total_condition == 'APPLE_OSX' + assert scope8.total_condition == 'UNIX AND NOT APPLE_OSX' + assert scope9.total_condition == 'ANDROID AND NOT ANDROID_EMBEDDED' + assert scope10.total_condition == 'UNIX AND NOT APPLE_OSX AND (ANDROID_EMBEDDED OR NOT ANDROID)' + assert scope11.total_condition == 'HAIKU AND (ANDROID_EMBEDDED OR NOT ANDROID)' + assert scope12.total_condition == 'UNIX AND NOT APPLE_OSX AND NOT HAIKU AND (ANDROID_EMBEDDED OR NOT ANDROID)' |