cmake scripts: fix type issues

Some of the changes are just cosmetic, but a bunch of them must be bugs,
such as mixing lists and str randomly.

Change-Id: Idd8340e17bcea7af3b87e595e251e13f5df1aa3f
Reviewed-by: Alexandru Croitor <alexandru.croitor@qt.io>
This commit is contained in:
Frederik Gladhorn 2019-10-07 12:22:04 +02:00
parent d1542e8a73
commit 3a103b3608
3 changed files with 70 additions and 71 deletions

View File

@ -29,7 +29,7 @@
import re import re
from sympy import simplify_logic, And, Or, Not, SympifyError from sympy import simplify_logic, And, Or, Not, SympifyError # type: ignore
from condition_simplifier_cache import simplify_condition_memoize from condition_simplifier_cache import simplify_condition_memoize

View File

@ -35,7 +35,7 @@ import os
import sys import sys
import time import time
from typing import Callable from typing import Any, Callable, Dict, Union
condition_simplifier_cache_enabled = True condition_simplifier_cache_enabled = True
@ -66,7 +66,7 @@ def get_file_checksum(file_path: str) -> str:
with open(file_path, "r") as content_file: with open(file_path, "r") as content_file:
content = content_file.read() content = content_file.read()
except IOError: except IOError:
content = time.time() content = str(time.time())
checksum = hashlib.md5(content.encode("utf-8")).hexdigest() checksum = hashlib.md5(content.encode("utf-8")).hexdigest()
return checksum return checksum
@ -88,7 +88,7 @@ def init_cache_dict():
def simplify_condition_memoize(f: Callable[[str], str]): def simplify_condition_memoize(f: Callable[[str], str]):
cache_path = get_cache_location() cache_path = get_cache_location()
cache_file_content = None cache_file_content: Dict[str, Any] = {}
if os.path.exists(cache_path): if os.path.exists(cache_path):
try: try:

View File

@ -42,12 +42,7 @@ import collections
from condition_simplifier import simplify_condition from condition_simplifier import simplify_condition
from condition_simplifier_cache import set_condition_simplified_cache_enabled from condition_simplifier_cache import set_condition_simplified_cache_enabled
try: import pyparsing as pp # type: ignore
collectionsAbc = collections.abc
except AttributeError:
collectionsAbc = collections
import pyparsing as pp
import xml.etree.ElementTree as ET import xml.etree.ElementTree as ET
from argparse import ArgumentParser from argparse import ArgumentParser
@ -63,7 +58,6 @@ from typing import (
Set, Set,
IO, IO,
Union, Union,
Mapping,
Any, Any,
Callable, Callable,
FrozenSet, FrozenSet,
@ -243,13 +237,13 @@ def is_config_test_project(project_file_path: str = "") -> bool:
@lru_cache(maxsize=None) @lru_cache(maxsize=None)
def find_qmake_conf(project_file_path: str = "") -> Optional[str]: def find_qmake_conf(project_file_path: str = "") -> str:
if not os.path.isabs(project_file_path): if not os.path.isabs(project_file_path):
print( print(
f"Warning: could not find .qmake.conf file, given path is not an " f"Warning: could not find .qmake.conf file, given path is not an "
f"absolute path: {project_file_path}" f"absolute path: {project_file_path}"
) )
return None return ""
cwd = os.path.dirname(project_file_path) cwd = os.path.dirname(project_file_path)
file_name = ".qmake.conf" file_name = ".qmake.conf"
@ -261,7 +255,8 @@ def find_qmake_conf(project_file_path: str = "") -> Optional[str]:
else: else:
cwd = os.path.dirname(cwd) cwd = os.path.dirname(cwd)
return None print(f"Warning: could not find .qmake.conf file")
return ""
def process_qrc_file( def process_qrc_file(
@ -350,7 +345,7 @@ def write_add_qt_resource_call(
target: str, target: str,
resource_name: str, resource_name: str,
prefix: Optional[str], prefix: Optional[str],
base_dir: Optional[str], base_dir: str,
lang: Optional[str], lang: Optional[str],
files: Dict[str, str], files: Dict[str, str],
skip_qtquick_compiler: bool, skip_qtquick_compiler: bool,
@ -425,27 +420,28 @@ def write_add_qt_resource_call(
class QmlDirFileInfo: class QmlDirFileInfo:
def __init__(self, file_path: str, type_name: str): def __init__(self, file_path: str, type_name: str) -> None:
self.file_path = file_path self.file_path = file_path
self.version = "" self.version = ""
self.type_name = type_name self.type_name = type_name
self.internal = False self.internal = False
self.singleton = False self.singleton = False
self.path = ""
class QmlDir: class QmlDir:
def __init__(self): def __init__(self) -> None:
self.module = "" self.module = ""
self.plugin_name = "" self.plugin_name = ""
self.plugin_path = "" self.plugin_path = ""
self.classname = "" self.classname = ""
self.imports = [] # typing.List[str] self.imports: List[str] = []
self.type_names = {} # typing.Dict[str, QmlDirFileInfo] self.type_names: Dict[str, QmlDirFileInfo] = {}
self.type_infos = [] # typing.List[str] self.type_infos: List[str] = []
self.depends = [] # typing.List[[str,str]] self.depends: List[str] = []
self.designer_supported = False self.designer_supported = False
def __str__(self): def __str__(self) -> str:
type_infos_line = " \n".join(self.type_infos) type_infos_line = " \n".join(self.type_infos)
imports_line = " \n".join(self.imports) imports_line = " \n".join(self.imports)
string = f"""\ string = f"""\
@ -522,8 +518,6 @@ class QmlDir:
self.classname = entries[1] self.classname = entries[1]
elif entries[0] == "typeinfo": elif entries[0] == "typeinfo":
self.type_infos.append(entries[1]) self.type_infos.append(entries[1])
elif entries[0] == "depends":
self.depends.append((entries[1], entries[2]))
elif entries[0] == "designersupported": elif entries[0] == "designersupported":
self.designer_supported = True self.designer_supported = True
elif entries[0] == "import": elif entries[0] == "import":
@ -618,7 +612,7 @@ def handle_vpath(source: str, base_dir: str, vpath: List[str]) -> str:
def flatten_list(l): def flatten_list(l):
""" Flattens an irregular nested list into a simple list.""" """ Flattens an irregular nested list into a simple list."""
for el in l: for el in l:
if isinstance(el, collectionsAbc.Iterable) and not isinstance(el, (str, bytes)): if isinstance(el, collections.abc.Iterable) and not isinstance(el, (str, bytes)):
yield from flatten_list(el) yield from flatten_list(el)
else: else:
yield el yield el
@ -658,7 +652,7 @@ def handle_function_value(group: pp.ParseResults):
class Operation: class Operation:
def __init__(self, value: Union[List[str], str]): def __init__(self, value: Union[List[str], str]) -> None:
if isinstance(value, list): if isinstance(value, list):
self._value = value self._value = value
else: else:
@ -797,19 +791,19 @@ class Scope(object):
self, self,
*, *,
parent_scope: Optional[Scope], parent_scope: Optional[Scope],
file: Optional[str] = None, qmake_file: str,
condition: str = "", condition: str = "",
base_dir: str = "", base_dir: str = "",
operations: Union[Mapping[str, List[Operation]], None] = None, operations: Union[Dict[str, List[Operation]], None] = None,
) -> None: ) -> None:
if operations is None: if not operations:
operations = { operations = {
"QT_SOURCE_TREE": [SetOperation(["${QT_SOURCE_TREE}"])], "QT_SOURCE_TREE": [SetOperation(["${QT_SOURCE_TREE}"])],
"QT_BUILD_TREE": [SetOperation(["${PROJECT_BINARY_DIR}"])], "QT_BUILD_TREE": [SetOperation(["${PROJECT_BINARY_DIR}"])],
"QTRO_SOURCE_TREE": [SetOperation(["${CMAKE_SOURCE_DIR}"])], "QTRO_SOURCE_TREE": [SetOperation(["${CMAKE_SOURCE_DIR}"])],
} }
self._operations = copy.deepcopy(operations) self._operations: Dict[str, List[Operation]] = copy.deepcopy(operations)
if parent_scope: if parent_scope:
parent_scope._add_child(self) parent_scope._add_child(self)
else: else:
@ -820,17 +814,15 @@ class Scope(object):
self._operations["QT"] = [SetOperation(["core", "gui"])] self._operations["QT"] = [SetOperation(["core", "gui"])]
self._basedir = base_dir self._basedir = base_dir
if file: if qmake_file:
self._currentdir = os.path.dirname(file) self._currentdir = os.path.dirname(qmake_file) or "."
if not self._currentdir:
self._currentdir = "."
if not self._basedir: if not self._basedir:
self._basedir = self._currentdir self._basedir = self._currentdir
self._scope_id = Scope.SCOPE_ID self._scope_id = Scope.SCOPE_ID
Scope.SCOPE_ID += 1 Scope.SCOPE_ID += 1
self._file = file self._file = qmake_file
self._file_absolute_path = os.path.abspath(file) self._file_absolute_path = os.path.abspath(qmake_file)
self._condition = map_condition(condition) self._condition = map_condition(condition)
self._children = [] # type: List[Scope] self._children = [] # type: List[Scope]
self._included_children = [] # type: List[Scope] self._included_children = [] # type: List[Scope]
@ -896,7 +888,7 @@ class Scope(object):
def FromDict( def FromDict(
parent_scope: Optional["Scope"], file: str, statements, cond: str = "", base_dir: str = "" parent_scope: Optional["Scope"], file: str, statements, cond: str = "", base_dir: str = ""
) -> Scope: ) -> Scope:
scope = Scope(parent_scope=parent_scope, file=file, condition=cond, base_dir=base_dir) scope = Scope(parent_scope=parent_scope, qmake_file=file, condition=cond, base_dir=base_dir)
for statement in statements: for statement in statements:
if isinstance(statement, list): # Handle skipped parts... if isinstance(statement, list): # Handle skipped parts...
assert not statement assert not statement
@ -1181,7 +1173,7 @@ class Scope(object):
else: else:
# Recursively expand each value from the result list # Recursively expand each value from the result list
# returned from self.get(). # returned from self.get().
result_list = [] result_list: List[str] = []
for entry_value in get_result: for entry_value in get_result:
result_list += self._expand_value(self._replace_env_var_value(entry_value)) result_list += self._expand_value(self._replace_env_var_value(entry_value))
return result_list return result_list
@ -1716,14 +1708,14 @@ def handle_subdir(
c, c,
cm_fh, cm_fh,
indent=indent + 1, indent=indent + 1,
is_example=is_example,
current_conditions=frozenset((*current_conditions, child_condition)), current_conditions=frozenset((*current_conditions, child_condition)),
is_example=is_example,
) )
def group_and_print_sub_dirs(scope: Scope, indent: int = 0): def group_and_print_sub_dirs(scope: Scope, indent: int = 0) -> None:
# Simplify conditions, and group # Simplify conditions, and group
# subdirectories with the same conditions. # subdirectories with the same conditions.
grouped_sub_dirs = {} grouped_sub_dirs: Dict[str, List[str]] = {}
# Wraps each element in the given interable with parentheses, # Wraps each element in the given interable with parentheses,
# to make sure boolean simplification happens correctly. # to make sure boolean simplification happens correctly.
@ -1770,7 +1762,7 @@ def handle_subdir(
condition_simplified = simplify_condition(condition_str) condition_simplified = simplify_condition(condition_str)
condition_key = condition_simplified condition_key = condition_simplified
sub_dir_list_by_key = grouped_sub_dirs.get(condition_key, []) sub_dir_list_by_key: List[str] = grouped_sub_dirs.get(condition_key, [])
sub_dir_list_by_key.append(subdir_name) sub_dir_list_by_key.append(subdir_name)
grouped_sub_dirs[condition_key] = sub_dir_list_by_key grouped_sub_dirs[condition_key] = sub_dir_list_by_key
@ -1793,7 +1785,7 @@ def handle_subdir(
# A set of conditions which will be ANDed together. The set is recreated with more conditions # A set of conditions which will be ANDed together. The set is recreated with more conditions
# as the scope deepens. # as the scope deepens.
current_conditions = frozenset() current_conditions: FrozenSet[str] = frozenset()
# Compute the total condition for scopes. Needed for scopes that # Compute the total condition for scopes. Needed for scopes that
# have 'else' as a condition. # have 'else' as a condition.
@ -2221,24 +2213,26 @@ def write_resources(cm_fh: IO[str], target: str, scope: Scope, indent: int = 0,
else: else:
immediate_files_filtered.append(f) immediate_files_filtered.append(f)
immediate_files = {f: "" for f in immediate_files_filtered} immediate_files = {f: "" for f in immediate_files_filtered}
immediate_prefix = scope.get(f"{r}.prefix") scope_prefix = scope.get(f"{r}.prefix")
if immediate_prefix: if scope_prefix:
immediate_prefix = immediate_prefix[0] immediate_prefix = scope_prefix[0]
else: else:
immediate_prefix = "/" immediate_prefix = "/"
immediate_base = scope.get(f"{r}.base") immediate_base_list = scope.get(f"{r}.base")
assert len(immediate_base_list) < 2, f"immediate base directory must be at most one entry"
immediate_base = "".join(immediate_base_list)
immediate_lang = None immediate_lang = None
immediate_name = f"qmake_{r}" immediate_name = f"qmake_{r}"
qrc_output += write_add_qt_resource_call( qrc_output += write_add_qt_resource_call(
target, target=target,
immediate_name, resource_name=immediate_name,
immediate_prefix, prefix=immediate_prefix,
immediate_base, base_dir=immediate_base,
immediate_lang, lang=immediate_lang,
immediate_files, files=immediate_files,
skip_qtquick_compiler, skip_qtquick_compiler=skip_qtquick_compiler,
retain_qtquick_compiler, retain_qtquick_compiler=retain_qtquick_compiler,
is_example, is_example=is_example,
) )
else: else:
if "*" in r: if "*" in r:
@ -2262,17 +2256,17 @@ def write_resources(cm_fh: IO[str], target: str, scope: Scope, indent: int = 0,
if standalone_files: if standalone_files:
name = "qmake_immediate" name = "qmake_immediate"
prefix = "/" prefix = "/"
base = None base = ""
lang = None lang = None
files = {f: "" for f in standalone_files} files = {f: "" for f in standalone_files}
skip_qtquick_compiler = False skip_qtquick_compiler = False
qrc_output += write_add_qt_resource_call( qrc_output += write_add_qt_resource_call(
target, target=target,
name, resource_name=name,
prefix, prefix=prefix,
base, base_dir=base,
lang, lang=lang,
files, files=files,
skip_qtquick_compiler=False, skip_qtquick_compiler=False,
retain_qtquick_compiler=False, retain_qtquick_compiler=False,
is_example=is_example, is_example=is_example,
@ -2347,6 +2341,7 @@ def write_extend_target(cm_fh: IO[str], target: str, scope: Scope, indent: int =
write_sources_section(extend_qt_io_string, scope) write_sources_section(extend_qt_io_string, scope)
extend_qt_string = extend_qt_io_string.getvalue() extend_qt_string = extend_qt_io_string.getvalue()
assert scope.total_condition, "Cannot write CONDITION when scope.condition is None"
extend_scope = ( extend_scope = (
f"\n{ind}extend_target({target} CONDITION" f"\n{ind}extend_target({target} CONDITION"
f" {map_to_cmake_condition(scope.total_condition)}\n" f" {map_to_cmake_condition(scope.total_condition)}\n"
@ -2372,7 +2367,7 @@ def merge_scopes(scopes: List[Scope]) -> List[Scope]:
result = [] # type: List[Scope] result = [] # type: List[Scope]
# Merge scopes with their parents: # Merge scopes with their parents:
known_scopes = {} # type: Mapping[str, Scope] known_scopes = {} # type: Dict[str, Scope]
for scope in scopes: for scope in scopes:
total_condition = scope.total_condition total_condition = scope.total_condition
assert total_condition assert total_condition
@ -2461,7 +2456,7 @@ def write_android_part(cm_fh: IO[str], target: str, scope: Scope, indent: int =
cm_fh.write(f"{spaces(indent)}endif()\n") cm_fh.write(f"{spaces(indent)}endif()\n")
def write_wayland_part(cm_fh: typing.IO[str], target: str, scope:Scope, indent: int = 0): def write_wayland_part(cm_fh: IO[str], target: str, scope:Scope, indent: int = 0):
client_sources = scope.get_files('WAYLANDCLIENTSOURCES', use_vpath=True) client_sources = scope.get_files('WAYLANDCLIENTSOURCES', use_vpath=True)
server_sources = scope.get_files('WAYLANDSERVERSOURCES', use_vpath=True) server_sources = scope.get_files('WAYLANDSERVERSOURCES', use_vpath=True)
if len(client_sources) == 0 and len(server_sources) == 0: if len(client_sources) == 0 and len(server_sources) == 0:
@ -2559,11 +2554,13 @@ def handle_source_subtractions(scopes: List[Scope]):
modified_sources[file_without_minus] = {} modified_sources[file_without_minus] = {}
subtractions = modified_sources[file_without_minus].get("subtractions", set()) subtractions = modified_sources[file_without_minus].get("subtractions", set())
assert isinstance(subtractions, set)
# Add the condition to the set of conditions and remove # Add the condition to the set of conditions and remove
# the file subtraction from the processed scope, which # the file subtraction from the processed scope, which
# will be later re-added in a new scope. # will be later re-added in a new scope.
if scope.condition: if scope.condition:
assert scope.total_condition
subtractions.add(scope.total_condition) subtractions.add(scope.total_condition)
remove_file_from_operation(scope, "SOURCES", file_without_minus, RemoveOperation) remove_file_from_operation(scope, "SOURCES", file_without_minus, RemoveOperation)
if subtractions: if subtractions:
@ -2580,7 +2577,9 @@ def handle_source_subtractions(scopes: List[Scope]):
for modified_source in modified_sources: for modified_source in modified_sources:
additions = modified_sources[modified_source].get("additions", set()) additions = modified_sources[modified_source].get("additions", set())
assert isinstance(additions, set), f"Additions must be a set, got {additions} instead."
subtractions = modified_sources[modified_source].get("subtractions", set()) subtractions = modified_sources[modified_source].get("subtractions", set())
assert isinstance(subtractions, set), f"Subtractions must be a set, got {additions} instead."
add_to_no_pch_sources = modified_sources[modified_source].get( add_to_no_pch_sources = modified_sources[modified_source].get(
"add_to_no_pch_sources", False "add_to_no_pch_sources", False
) )
@ -2613,7 +2612,7 @@ def handle_source_subtractions(scopes: List[Scope]):
# operations. # operations.
new_scope = Scope( new_scope = Scope(
parent_scope=top_most_scope, parent_scope=top_most_scope,
file=top_most_scope.file, qmake_file=top_most_scope.file,
condition=condition_simplified, condition=condition_simplified,
base_dir=top_most_scope.basedir, base_dir=top_most_scope.basedir,
) )
@ -2957,11 +2956,10 @@ def write_example(
""" """
) )
qmldir_file_path = scope.get_files("qmldir.files") qmldir_file_path_list = scope.get_files("qmldir.files")
if qmldir_file_path: assert len(qmldir_file_path_list) < 2, "File path must only contain one path"
qmldir_file_path = os.path.join(os.getcwd(), qmldir_file_path[0]) qmldir_file_path = qmldir_file_path_list[0] if qmldir_file_path_list else "qmldir"
else: qmldir_file_path = os.path.join(os.getcwd(), qmldir_file_path[0])
qmldir_file_path = os.path.join(os.getcwd(), "qmldir")
if os.path.exists(qmldir_file_path): if os.path.exists(qmldir_file_path):
qml_dir = QmlDir() qml_dir = QmlDir()
@ -3402,6 +3400,7 @@ def handle_config_test_project(scope: Scope, cm_fh: IO[str]):
extend_string = extend_scope_io_string.getvalue() extend_string = extend_scope_io_string.getvalue()
if extend_string: if extend_string:
assert c.total_condition, "Cannot write if with empty condition"
extend_scope = ( extend_scope = (
f"\nif({map_to_cmake_condition(c.total_condition)})\n" f"\nif({map_to_cmake_condition(c.total_condition)})\n"
f"{extend_string}" f"{extend_string}"