How to make CMake use AUTOMOC without find_package?

585 Views Asked by At

Currently, I have Qt dlls and include headers as a dependency in my repo. I wanted to make use of CMake's CMAKE_AUTOMOC functionality providing a moc.exe binary along with dlls and include files. CMake allows to set a custom path to binary but it fails to use CMAKE_AUTOMOC functionality.

CMake Warning (dev) in myproj/CMakeLists.txt:
  AUTOGEN: No valid Qt version found for target myproj_QT.  AUTOMOC
  disabled.  Consider adding:

    find_package(Qt<QTVERSION> COMPONENTS Core)

  to your CMakeLists.txt file.

I don't want to use find_package all stuff like that. I want to make dependency management as much straightforward and explicit as it can be. Is there a way to enable automoc/autouic/autorcc features with my approach?

1

There are 1 best solutions below

0
IC_ On

Adding that code helped me. Thanks @Tsyvarev for pointing out

set(PROGRAM_EXTENSION)
if (WIN32)
    set(PROGRAM_EXTENSION .exe)
endif()
set(AUTOMOC_EXECUTABLE ${CMAKE_SOURCE_DIR}/3rdparty/qt5/bin/${OS}/moc${PROGRAM_EXTENSION})
add_executable(Qt5::moc IMPORTED)
set_target_properties(Qt5::moc PROPERTIES IMPORTED_LOCATION "${AUTOMOC_EXECUTABLE}")
set(Qt5Core_VERSION_MAJOR 5)
set(Qt5Core_VERSION_MINOR 15)
set(CMAKE_AUTOMOC TRUE)

Qt5CoreMacros.cmake must be included before