pro2cmake: Generate public CMake API code for projects by default

Previously pro2cmake assumed that all .pro were internal Qt projects
like qtbase/src/corelib.pro and generated private api calls like
qt_internal_add_module().

Public CMake API calls (like qt_add_executable) were only generated if
an --is-example command line flag was passed to the script or if the
.pro file was located under the examples subfolder of a qt repo source
directory (indicated by the presence of a .qmake.conf file).

Change the logic to always generate public CMake API code by default,
unless a .cmake.conf/.qmake.conf file is encountered with a valid repo
module version inside, in which case apply the old heuristic of
checking for the ./examples subfolder.

The intention is to have a sensible default so that Qt users can use
the script to more easily migrate their qmake projects without having
to explicitly specify the --is-example flag.

Pick-to: 6.2
Task-number: QTBUG-96799
Change-Id: I38f4f1b349a5b9688cf7bc5914d4fde72e660a98
Reviewed-by: Joerg Bornemann <joerg.bornemann@qt.io>
This commit is contained in:
Alexandru Croitor 2021-09-22 18:45:14 +02:00
parent 124f7c681f
commit 9665929706

View File

@ -241,15 +241,40 @@ def is_top_level_repo_examples_project(project_file_path: str = "") -> bool:
def is_example_project(project_file_path: str = "") -> bool: def is_example_project(project_file_path: str = "") -> bool:
qmake_or_cmake_conf_path = find_qmake_or_cmake_conf(project_file_path) # If there's a .qmake.conf or .cmake.conf file in the parent
qmake_or_cmake_conf_dir_path = os.path.dirname(qmake_or_cmake_conf_path) # directories of the given project path, it is likely that the
# project is an internal Qt project that uses private Qt CMake
# API.
found_qt_repo_version = False
qmake_conf = find_qmake_conf(project_file_path)
if qmake_conf:
repo_version = parse_qt_repo_module_version_from_qmake_conf(qmake_conf)
if repo_version:
found_qt_repo_version = True
cmake_conf = find_cmake_conf(project_file_path)
if cmake_conf:
repo_version = parse_qt_repo_module_version_from_cmake_conf(cmake_conf)
if repo_version:
found_qt_repo_version = True
# If we haven't found a conf file, we assume this is an example
# project and not a project under a qt source repository.
if not found_qt_repo_version:
return True
project_relative_path = os.path.relpath(project_file_path, qmake_or_cmake_conf_dir_path)
# If the project file is found in a subdir called 'examples' # If the project file is found in a subdir called 'examples'
# relative to the repo source dir, then it must be an example, but # relative to the repo source dir, then it must be an example, but
# some examples contain 3rdparty libraries that do not need to be # some examples contain 3rdparty libraries that do not need to be
# built as examples. # built as examples.
return project_relative_path.startswith("examples") and "3rdparty" not in project_relative_path qmake_or_cmake_conf_path = find_qmake_or_cmake_conf(project_file_path)
qmake_or_cmake_conf_dir_path = os.path.dirname(qmake_or_cmake_conf_path)
project_relative_path = os.path.relpath(project_file_path, qmake_or_cmake_conf_dir_path)
is_example_under_repo_sources = (
project_relative_path.startswith("examples") and "3rdparty" not in project_relative_path
)
return is_example_under_repo_sources
def is_config_test_project(project_file_path: str = "") -> bool: def is_config_test_project(project_file_path: str = "") -> bool:
@ -330,6 +355,20 @@ def find_qmake_or_cmake_conf(project_file_path: str = "") -> str:
return cmake_conf return cmake_conf
def parse_qt_repo_module_version_from_qmake_conf(qmake_conf_path: str = "") -> str:
with open(qmake_conf_path) as f:
file_contents = f.read()
m = re.search(fr"MODULE_VERSION\s*=\s*([0-9.]+)", file_contents)
return m.group(1) if m else ""
def parse_qt_repo_module_version_from_cmake_conf(cmake_conf_path: str = "") -> str:
with open(cmake_conf_path) as f:
file_contents = f.read()
m = re.search(fr'set\(QT_REPO_MODULE_VERSION\s*"([0-9.]+)"\)', file_contents)
return m.group(1) if m else ""
def set_up_cmake_api_calls(): def set_up_cmake_api_calls():
def nested_dict(): def nested_dict():
return defaultdict(nested_dict) return defaultdict(nested_dict)