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

Consider using CMake AUTOMOC property instead of qt_wrap_cpp #460

Open
uilianries opened this issue Feb 7, 2025 · 0 comments
Open

Consider using CMake AUTOMOC property instead of qt_wrap_cpp #460

uilianries opened this issue Feb 7, 2025 · 0 comments

Comments

@uilianries
Copy link

uilianries commented Feb 7, 2025

Hello!

We are just packaging the Node Editor project in Conan (PR conan-io/conan-center-index#24751) and it fails to be built when using Qt full dynamic libraries, so moc executable can not find its dependencies libraries due to a misconfiguration of ld library path. Of course, it will not happen in case Qt is fully static, or moc and rcc executables at least.

However, Nodeeditor is using qt_wrap_cpp which is marked as deprecated by CMake page, but still working. The CMake's page also recommends:

Consider updating the project to use the AUTOMOC target property instead for a more automated way of invoking the moc tool.

The CMake's suggestion here will not only reduce your CMakeLists.txt size, but also its maintenance as well.

I did a patch locally to validate this theory and it looks to be working: 0001-configure-automoc.patch

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 9c9285b..a2e366c 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -60,16 +60,6 @@ if (${QT_VERSION} VERSION_LESS 5.11.0)
   message(FATAL_ERROR "Requires qt version >= 5.11.0, Your current version is ${QT_VERSION}")
 endif()
 
-if (${QT_VERSION_MAJOR} EQUAL 6)
-  qt_add_resources(RESOURCES ./resources/resources.qrc)
-else()
-  qt5_add_resources(RESOURCES ./resources/resources.qrc)
-endif()
-
-# Unfortunately, as we have a split include/src, AUTOMOC doesn't work.
-# We'll have to manually specify some files
-set(CMAKE_AUTOMOC ON)
-
 set(CPP_SOURCE_FILES
   src/AbstractGraphModel.cpp
   src/AbstractNodeGeometry.cpp
@@ -95,6 +85,7 @@ set(CPP_SOURCE_FILES
   src/StyleCollection.cpp
   src/UndoCommands.cpp
   src/locateNode.cpp
+  resources/resources.qrc
 )
 
 set(HPP_HEADER_FILES
@@ -203,24 +194,8 @@ set_target_properties(QtNodes
 ######
 # Moc
 ##
+set_target_properties(QtNodes PROPERTIES AUTOMOC ON AUTORCC ON)
 
-file(GLOB_RECURSE HEADERS_TO_MOC include/QtNodes/internal/*.hpp)
-
-if (${QT_VERSION_MAJOR} EQUAL 6)
-  qt_wrap_cpp(nodes_moc
-      ${HEADERS_TO_MOC}
-      TARGET QtNodes
-    OPTIONS --no-notes # Don't display a note for the headers which don't produce a moc_*.cpp
-  )
-else()
-  qt5_wrap_cpp(nodes_moc
-  ${HEADERS_TO_MOC}
-  TARGET QtNodes
-  OPTIONS --no-notes # Don't display a note for the headers which don't produce a moc_*.cpp
-  )
-endif()
-
-target_sources(QtNodes PRIVATE ${nodes_moc})
 
 ###########
 # Examples

As a result, the build log still generates the qrc_resources.cpp, but now is automatically done by CMake:

[21/29] /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++ -DNODE_EDITOR_EXPORTS -DNODE_EDITOR_SHARED -DQT_CORE_LIB -DQT_GUI_LIB -DQT_NO_DEBUG -DQT_NO_KEYWORDS -DQT_OPENGL_LIB -DQT_WIDGETS_LIB -DQtNodes_EXPORTS -I/Users/uilian/.conan2/p/b/nodee61d0ab45eaa0f/b/build/Release/QtNodes_autogen/include -I/Users/uilian/.conan2/p/b/nodee61d0ab45eaa0f/b/src/include -I/Users/uilian/.conan2/p/b/nodee61d0ab45eaa0f/b/src/src -I/Users/uilian/.conan2/p/b/nodee61d0ab45eaa0f/b/src/include/QtNodes/internal -isystem /Users/uilian/.conan2/p/b/qt47cbbe2bd005f/p/include -isystem /Users/uilian/.conan2/p/b/qt47cbbe2bd005f/p/include/QtCore -isystem /Users/uilian/.conan2/p/b/qt47cbbe2bd005f/p/include/QtWidgets -isystem /Users/uilian/.conan2/p/b/qt47cbbe2bd005f/p/include/QtGui -isystem /Users/uilian/.conan2/p/b/qt47cbbe2bd005f/p/include/QtOpenGL -stdlib=libc++ -O3 -DNDEBUG -std=c++17 -arch arm64 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX15.2.sdk -fPIC -Wall -Wextra -Werror -fPIC -MD -MT CMakeFiles/QtNodes.dir/QtNodes_autogen/3YJK5W5UP7/qrc_resources.cpp.o -MF CMakeFiles/QtNodes.dir/QtNodes_autogen/3YJK5W5UP7/qrc_resources.cpp.o.d -o CMakeFiles/QtNodes.dir/QtNodes_autogen/3YJK5W5UP7/qrc_resources.cpp.o -c /Users/uilian/.conan2/p/b/nodee61d0ab45eaa0f/b/build/Release/QtNodes_autogen/3YJK5W5UP7/qrc_resources.cpp

Here is my full build log, so you can visualize everything:

nodeeditor-3.0.11-macos-shared-patched.log

I see the comment in your CMakeLists.txt as well:

Unfortunately, as we have a split include/src, AUTOMOC doesn't work.

However, it was 7 years ago (PR #142) and many features changed in CMake since then.

Please, feel free to share your ideas about using AUTOMOC now. I could open a PR if needed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant