title: Building Qt Qml Plugin using CMake
body: <p>I want to compile <a href="http://doc.qt.io/qt-5/qtqml-modules-cppplugins.html" rel="nofollow noreferrer">Qt Qml Plugin</a> using <code>CMake</code> (instead of <code>QMake</code>) and add this plugin to the some application. Plugin and application script should be in separate <code>CMakeLists.txt</code>.</p>
  
  <p>File structure:</p>
  
  <pre><code>CMakeLists.txt
  main.cpp
  main.qml
  qml.qrc
  plugin/
      CMakeLists.txt
      MyItem.cpp
      MyItem.h
      Plugin.h
      qmldir
  </code></pre>
  
  <p>I have a script <code>plugin/CMakeLists.txt</code> that generates <code>libmyplugin.dylib</code> and <code>qmldir</code> and put it to the <code>plugin/MyPlugin</code> subfolder of plugin binary dir:</p>
  
  <p><code>plugin/CMakeLists.txt</code>:</p>
  
  <pre><code>cmake_minimum_required(VERSION 3.8)
  
  add_library(myplugin SHARED
      Item.cpp
      Item.h
      Plugin.h
  )
  
  set_target_properties(myplugin PROPERTIES
      AUTOMOC TRUE
      LIBRARY_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/package/MyPlugin
  )
  
  # Copy qmldir file
  add_custom_command(
      OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/package/MyPlugin/qmldir
      DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/qmldir
      COMMAND "${CMAKE_COMMAND}" -E copy "${CMAKE_CURRENT_SOURCE_DIR}/qmldir" "${CMAKE_CURRENT_BINARY_DIR}/package/MyPlugin/qmldir"
      COMMENT "Copy qmldir"
  )
  target_sources(myplugin PRIVATE
      ${CMAKE_CURRENT_BINARY_DIR}/package/MyPlugin/qmldir
  )
  
  find_package(Qt5 REQUIRED COMPONENTS Core Qml Quick)
  target_link_libraries(viewWorld PUBLIC Qt5::Core Qt5::Qml Qt5::Quick)
  </code></pre>
  
  <p>Now I need to build <code>myplugin</code> target before/after <code>app</code> target and copy plugin files from  <code>&lt;myplugin binary dir&gt;/plugin</code> into location of <code>app</code> target binary (in case of Windows and Linux) or into <code>&lt;app bundle&gt;/Contents/PlugIns</code> (in case of macOS):</p>
  
  <p><code>CMakeLists.txt</code>:</p>
  
  <pre><code>cmake_minimum_required(VERSION 3.8)
  
  add_executable(app
      main.cpp
      qml.qrc
  )
  
  set_target_properties(app PROPERTIES
      AUTORCC TRUE
  )
  
  add_subdirectory(plugin)
  add_dependencies(app plugin)
  # Needs to copy plugin files...
  
  find_package(Qt5 REQUIRED COMPONENTS Core Qml Quick)
  target_link_libraries(viewWorld PUBLIC Qt5::Core Qt5::Qml Qt5::Quick)
  </code></pre>
  
  <p>I need to copy plugin files only if they changed. It would be great to write function that will take <code>app</code> target and <code>plugin</code> target and create appropriate dependencies.</p>