Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add includemocs.py and create one moc file per cpp file #59230

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
1 change: 0 additions & 1 deletion .docker/docker-qgis-build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,6 @@ cmake \
-DDISABLE_DEPRECATED=ON \
-DPYTHON_TEST_WRAPPER="timeout -sSIGSEGV 55s" \
-DCXX_EXTRA_FLAGS="${CLANG_WARNINGS}" \
-DWERROR=TRUE \
-DAGGRESSIVE_SAFE_MODE=ON \
-DWITH_CLAZY=${WITH_CLAZY} \
"${CMAKE_EXTRA_ARGS[@]}" ..
Expand Down
12 changes: 12 additions & 0 deletions .github/workflows/code_layout.yml
Original file line number Diff line number Diff line change
Expand Up @@ -222,3 +222,15 @@ jobs:

- name: Run cppcheck test
run: ./scripts/cppcheck.sh

moc_check:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: '3.12'
- name: Run Check
run: python3 scripts/includemocs.py src --dry-run
259 changes: 259 additions & 0 deletions scripts/includemocs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,259 @@
#!/usr/bin/env python3

#
# This file is part of KDToolBox.
#
# SPDX-FileCopyrightText: 2022 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.com>
# Author: Jesper K. Pedersen <jesper.pedersen@kdab.com>
#
# SPDX-License-Identifier: MIT
#

"""
Script to add inclusion of mocs to files recursively.
"""

# pylint: disable=redefined-outer-name

import os
import re
import argparse
import sys

dirty = False


def stripInitialSlash(path):
if path and path.startswith("/"):
path = path[1:]
return path


# Returns true if the path is to be excluded from the search


def shouldExclude(root, path):
# pylint: disable=used-before-assignment,possibly-used-before-assignment
if not args.excludes:
return False # No excludes provided

assert root.startswith(args.root)
root = stripInitialSlash(root[len(args.root):])

if args.headerPrefix:
assert root.startswith(args.headerPrefix)
root = stripInitialSlash(root[len(args.headerPrefix):])

return (path in args.excludes) or (root + "/" + path in args.excludes)


regexp = re.compile("\\s*(Q_OBJECT|Q_GADGET|Q_NAMESPACE)\\s*")
# Returns true if the header file provides contains a Q_OBJECT, Q_GADGET or Q_NAMESPACE macro


def hasMacro(fileName):
with open(fileName, "r", encoding="ISO-8859-1") as fileHandle:
for line in fileHandle:
if regexp.match(line):
return True
return False


# returns the matching .cpp file for the given .h file


def matchingCPPFile(root, fileName):
assert root.startswith(args.root)
root = stripInitialSlash(root[len(args.root):])

if args.headerPrefix:
assert root.startswith(args.headerPrefix)
root = stripInitialSlash(root[len(args.headerPrefix):])

if args.sourcePrefix:
root = args.sourcePrefix + "/" + root

return (
args.root
+ "/"
+ root
+ ("/" if root != "" else "")
+ fileNameWithoutExtension(fileName)
+ ".cpp"
)


def fileNameWithoutExtension(fileName):
return os.path.splitext(os.path.basename(fileName))[0]


# returns true if the specifies .cpp file already has the proper include


def cppHasMOCInclude(fileName):
includeStatement = '#include "moc_%s.cpp"' % fileNameWithoutExtension(fileName)
with open(fileName, encoding="utf8") as fileHandle:
return includeStatement in fileHandle.read()


def getMocInsertionLocation(filename, content):
headerIncludeRegex = re.compile(
r'#include "%s\.h".*\n' % fileNameWithoutExtension(filename), re.M
)
match = headerIncludeRegex.search(content)
if match:
return match.end()
return 0


def trimExistingMocInclude(content, cppFileName):
mocStrRegex = re.compile(
r'#include "moc_%s\.cpp"\n' % fileNameWithoutExtension(cppFileName)
)
match = mocStrRegex.search(content)
if match:
return content[: match.start()] + content[match.end():]
return content


def processFile(root, fileName):
# pylint: disable=global-statement
global dirty
macroFound = hasMacro(root + "/" + fileName)
logVerbose(
"Inspecting %s %s"
% (
root + "/" + fileName,
"[Has Q_OBJECT / Q_GADGET / Q_NAMESPACE]" if macroFound else "",
)
)

if macroFound:
cppFileName = matchingCPPFile(root, fileName)
logVerbose(" -> %s" % cppFileName)

if not os.path.exists(cppFileName):
log("file %s didn't exist (which might not be an error)" % cppFileName)
return

if args.replaceExisting or not cppHasMOCInclude(cppFileName):
dirty = True
if args.dryRun:
log("Missing moc include file: %s" % cppFileName)
else:
log("Updating %s" % cppFileName)

with open(cppFileName, "r", encoding="utf8") as f:
content = f.read()

if args.replaceExisting:
content = trimExistingMocInclude(content, cppFileName)

loc = getMocInsertionLocation(cppFileName, content)
if args.insertAtEnd:
with open(cppFileName, "a", encoding="utf8") as f:
f.write(
'\n#include "moc_%s.cpp"\n'
% fileNameWithoutExtension(cppFileName)
)
else:
with open(cppFileName, "w", encoding="utf8") as f:
f.write(
content[:loc]
+ (
'#include "moc_%s.cpp"\n'
% fileNameWithoutExtension(cppFileName)
)
+ content[loc:]
)


def log(content):
if not args.quiet:
print(content)


def logVerbose(content):
if args.verbose:
print(content)


# MAIN
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="""Script to add inclusion of mocs to files recursively.
The source files either need to be in the same directories as the header files or in parallel directories,
where the root of the headers are specified using --header-prefix and the root of the sources are specified using --source-prefix.
If either header-prefix or source-prefix is the current directory, then they may be omitted."""
)
parser.add_argument(
"--dry-run",
"-n",
dest="dryRun",
action="store_true",
help="only report files to be updated",
)
parser.add_argument(
"--quiet", "-q", dest="quiet", action="store_true", help="suppress output"
)
parser.add_argument("--verbose", "-v", dest="verbose", action="store_true")
parser.add_argument(
"--header-prefix",
metavar="directory",
dest="headerPrefix",
help="This directory will be replaced with source-prefix when "
"searching for matching source files",
)
parser.add_argument(
"--source-prefix",
metavar="directory",
dest="sourcePrefix",
help="see --header-prefix",
)
parser.add_argument(
"--excludes",
metavar="directory",
dest="excludes",
nargs="*",
help="directories to be excluded, might either be in the form of a directory name, "
"e.g. 3rdparty or a partial directory prefix from the root, e.g 3rdparty/parser",
)
parser.add_argument(
"--insert-at-end",
dest="insertAtEnd",
action="store_true",
help="insert the moc include at the end of the file instead of the beginning",
)
parser.add_argument(
"--replace-existing",
dest="replaceExisting",
action="store_true",
help="delete and readd existing MOC include statements",
)
parser.add_argument(
dest="root",
default=".",
metavar="directory",
nargs="?",
help="root directory for the operation",
)

args = parser.parse_args()

root = args.root
if args.headerPrefix:
root += "/" + args.headerPrefix

path = os.walk(root)
for root, directories, files in path:
# Filter out directories specified in --exclude
directories[:] = [d for d in directories if not shouldExclude(root, d)]

for file in files:
if file.endswith(".h") or file.endswith(".hpp"):
processFile(root, file)

if not dirty:
log("No changes needed")

sys.exit(-1 if dirty else 0)
1 change: 1 addition & 0 deletions src/3d/chunks/qgschunkboundsentity_p.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
***************************************************************************/

#include "qgschunkboundsentity_p.h"
#include "moc_qgschunkboundsentity_p.cpp"

#include <Qt3DExtras/QPhongMaterial>

Expand Down
1 change: 1 addition & 0 deletions src/3d/chunks/qgschunkedentity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
***************************************************************************/

#include "qgschunkedentity.h"
#include "moc_qgschunkedentity.cpp"

#include <QElapsedTimer>
#include <QVector4D>
Expand Down
1 change: 1 addition & 0 deletions src/3d/chunks/qgschunkloader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
***************************************************************************/

#include "qgschunkloader.h"
#include "moc_qgschunkloader.cpp"
#include "qgschunknode.h"

#include <QVector>
Expand Down
1 change: 1 addition & 0 deletions src/3d/chunks/qgschunkqueuejob.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
***************************************************************************/

#include "qgschunkqueuejob.h"
#include "moc_qgschunkqueuejob.cpp"

///@cond PRIVATE

Expand Down
1 change: 1 addition & 0 deletions src/3d/materials/qgsmaterial.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
***************************************************************************/

#include "qgsmaterial.h"
#include "moc_qgsmaterial.cpp"
#include "qgs3dutils.h"

#include <Qt3DRender/QEffect>
Expand Down
1 change: 1 addition & 0 deletions src/3d/materials/qgsmetalroughmaterial.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
***************************************************************************/

#include "qgsmetalroughmaterial.h"
#include "moc_qgsmetalroughmaterial.cpp"
#include "qgs3dutils.h"
#include <Qt3DRender/QParameter>
#include <Qt3DRender/QRenderPass>
Expand Down
1 change: 1 addition & 0 deletions src/3d/materials/qgsphongtexturedmaterial.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include <Qt3DRender/QTechnique>

#include "qgsphongtexturedmaterial.h"
#include "moc_qgsphongtexturedmaterial.cpp"

///@cond PRIVATE
QgsPhongTexturedMaterial::QgsPhongTexturedMaterial( QNode *parent )
Expand Down
1 change: 1 addition & 0 deletions src/3d/materials/qgstexturematerial.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include <Qt3DRender/QTexture>

#include "qgstexturematerial.h"
#include "moc_qgstexturematerial.cpp"

///@cond PRIVATE
QgsTextureMaterial::QgsTextureMaterial( QNode *parent )
Expand Down
1 change: 1 addition & 0 deletions src/3d/mesh/qgsmesh3dentity_p.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
***************************************************************************/

#include "qgsmesh3dentity_p.h"
#include "moc_qgsmesh3dentity_p.cpp"

#include <Qt3DRender/QGeometryRenderer>

Expand Down
1 change: 1 addition & 0 deletions src/3d/mesh/qgsmesh3dgeometry_p.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
***************************************************************************/

#include "qgsmesh3dgeometry_p.h"
#include "moc_qgsmesh3dgeometry_p.cpp"

#include <QFutureWatcher>
#include <QtConcurrentRun>
Expand Down
1 change: 1 addition & 0 deletions src/3d/mesh/qgsmesh3dmaterial_p.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
***************************************************************************/

#include "qgsmesh3dmaterial_p.h"
#include "moc_qgsmesh3dmaterial_p.cpp"

#include <Qt3DRender/QEffect>
#include <Qt3DRender/QGraphicsApiFilter>
Expand Down
1 change: 1 addition & 0 deletions src/3d/mesh/qgsmeshterraingenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
***************************************************************************/

#include "qgsmeshterraingenerator.h"
#include "moc_qgsmeshterraingenerator.cpp"
#include "qgsmeshterraintileloader_p.h"

#include "qgsmesh3dentity_p.h"
Expand Down
1 change: 1 addition & 0 deletions src/3d/processing/qgs3dalgorithms.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
***************************************************************************/

#include "qgs3dalgorithms.h"
#include "moc_qgs3dalgorithms.cpp"
#include "qgsalgorithmtessellate.h"
#include "qgsapplication.h"

Expand Down
1 change: 1 addition & 0 deletions src/3d/qgs3daxis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
***************************************************************************/

#include "qgs3daxis.h"
#include "moc_qgs3daxis.cpp"

#include <Qt3DCore/QTransform>
#include <Qt3DExtras/QCylinderMesh>
Expand Down
Loading
Loading