cmake_minimum_required(VERSION 3.5.2)

project(test_automoc CXX)

# Find the QtWidgets library
find_package(Qt5Widgets)

# header is in the source-tree include directory and
# moc-generated result is in the build-tree include directory.
include_directories(${CMAKE_SOURCE_DIR}/include ${CMAKE_BINARY_DIR}/include) 

option(USE_AUTOMOC "Try using automoc when there is a separate include directory" OFF)

if(USE_AUTOMOC)
  # Instruct CMake to run moc automatically when needed.
  set(CMAKE_AUTOMOC ON)
  # Tell CMake to create the helloworld executable
  add_executable(helloworld main.cpp ${CMAKE_SOURCE_DIR}/include/test_q_object.h)
else(USE_AUTOMOC)
  qt5_wrap_cpp(
    QT_MOC_OUTFILES
    ${CMAKE_SOURCE_DIR}/include/test_q_object.h
    )
  set_source_files_properties(
    ${QT_MOC_OUTFILES}
    PROPERTIES GENERATED "ON"
    )
  add_custom_target(moc_generate DEPENDS ${QT_MOC_OUTFILES})
  # Tell CMake to create the helloworld executable
  add_executable(helloworld main.cpp ${QT_MOC_OUTFILES})
  add_dependencies(helloworld moc_generate)
endif(USE_AUTOMOC)

# Use the Widgets module from Qt 5.
target_link_libraries(helloworld Qt5::Widgets)
