cmake_minimum_required(VERSION 3.6.2)

project(test_automoc CXX)

# Find the QtWidgets library
find_package(Qt5Widgets)

#_______________________________________________

# Example using qt5_wrap
qt5_wrap_cpp(
  QT_MOC_OUTFILES
  ${CMAKE_SOURCE_DIR}/include/test_q_object.h
  )
add_executable(helloworld_qt5_wrap src_noinclude/helloworld.cpp ${QT_MOC_OUTFILES})
target_link_libraries(helloworld_qt5_wrap Qt5::Widgets)

# header is in the source-tree include directory and the
# moc-generated result is part of the source files so does
# not need to be part of INCLUDE_DIRECTORIES.
target_include_directories(helloworld_qt5_wrap PRIVATE "${CMAKE_SOURCE_DIR}/include")

#_______________________________________________

# Example using automoc while adding header in separate directory to target source files

add_executable(helloworld_automoc_noinclude src_noinclude/helloworld.cpp ${CMAKE_SOURCE_DIR}/include/test_q_object.h)
# Use the Widgets module from Qt 5.
target_link_libraries(helloworld_automoc_noinclude Qt5::Widgets)

# Set automoc to ON.
set_target_properties(helloworld_automoc_noinclude
  PROPERTIES
  AUTOMOC ON
  )

# Note that the required test_q_object.h header is in the
# source-tree include directory.
# Also note that ${CMAKE_CURRENT_BUILD_DIR} is not required for g++
# and many other C++ compilers, but still best practice for AUTOMOC
# just in case there is a compiler out there that does not look in the
# source directory for the quoted form of #included headers in the
# automoc-generated source code
# ${CMAKE_CURRENT_BINARY_DIR}/<targetname>_automoc.cpp that is
# specifically added by automoc to the list of source files for the
# target.
target_include_directories(helloworld_automoc_noinclude PRIVATE
  "${CMAKE_SOURCE_DIR}/include;${CMAKE_CURRENT_BINARY_DIR}"
  )

#_______________________________________________

# Example using automoc with header with same basename in same directory, but no include
# of automoc result.

add_executable(helloworld_automoc_same_noinclude src_same_noinclude/helloworld.cpp )
# Use the Widgets module from Qt 5.
target_link_libraries(helloworld_automoc_same_noinclude Qt5::Widgets)

# Set automoc to ON.
set_target_properties(helloworld_automoc_same_noinclude
  PROPERTIES
  AUTOMOC ON
  )

# Note that the required helloworld.h header is in the
# same source directory.
# Also note that ${CMAKE_CURRENT_BUILD_DIR} is not required for g++
# and many other C++ compilers, but still best practice for AUTOMOC
# just in case there is a compiler out there that does not look in the
# source directory for the quoted form of #included headers in the
# automoc-generated source code
# ${CMAKE_CURRENT_BINARY_DIR}/<targetname>_automoc.cpp that is
# specifically added by automoc to the list of source files for the
# target.
target_include_directories(helloworld_automoc_same_noinclude PRIVATE
  "${CMAKE_SOURCE_DIR}/src_same_noinclude;${CMAKE_CURRENT_BINARY_DIR}"
  )

#_______________________________________________

# Example using automoc with header with different basename in same directory and with
# specific include of moc result that is generated by automoc.

add_executable(helloworld_automoc_same_include src_same_include/helloworld.cpp )
# Use the Widgets module from Qt 5.
target_link_libraries(helloworld_automoc_same_include Qt5::Widgets)

# Set automoc to ON.
set_target_properties(helloworld_automoc_same_include
  PROPERTIES
  AUTOMOC ON
  )

# Note that the required test_q_object.h header is in the same
# source-tree directory and the source file #includes the specific moc
# result produced by automoc so need to specify
# ${CMAKE_CURRENT_BINARY_DIR} as an include directory.
target_include_directories(helloworld_automoc_same_include PRIVATE
  "${CMAKE_SOURCE_DIR}/src_same_include;${CMAKE_CURRENT_BINARY_DIR}"
  )

#_______________________________________________

# Second (identical except for target name to demonstrate nameclash)
# example using automoc with header with different basename in same directory and with
# specific include of moc result that is generated by automoc.

add_executable(helloworld_automoc_same_include1 src_same_include/helloworld.cpp )
# Use the Widgets module from Qt 5.
target_link_libraries(helloworld_automoc_same_include1 Qt5::Widgets)

# Set automoc to ON.
set_target_properties(helloworld_automoc_same_include1
  PROPERTIES
  AUTOMOC ON
  )

# Note that the required test_q_object.h header is in the same
# source-tree directory and the source file #includes the specific moc
# result produced by automoc so need to specify
# ${CMAKE_CURRENT_BINARY_DIR} as an include directory.
target_include_directories(helloworld_automoc_same_include1 PRIVATE
  "${CMAKE_SOURCE_DIR}/src_same_include;${CMAKE_CURRENT_BINARY_DIR}"
  )

#_______________________________________________

# Third (identical except for target name to demonstrate nameclash)
# example using automoc with header with different basename in same directory and with
# specific include of moc result that is generated by automoc.  However, instead
# of using the same directory for the header and implementation file
# for this target we use a distinct src_same_include_alt directory
# that contains an identical copy of the same header and implementation
# file.

add_executable(helloworld_automoc_same_include2 src_same_include_alt/helloworld.cpp )
# Use the Widgets module from Qt 5.
target_link_libraries(helloworld_automoc_same_include2 Qt5::Widgets)

# Set automoc to ON.
set_target_properties(helloworld_automoc_same_include2
  PROPERTIES
  AUTOMOC ON
  )

# Note that the required test_q_object.h header is in the same
# source-tree directory and the source file #includes the specific moc
# result produced by automoc so need to specify
# ${CMAKE_CURRENT_BINARY_DIR} as an include directory.
target_include_directories(helloworld_automoc_same_include2 PRIVATE
  "${CMAKE_SOURCE_DIR}/src_same_include_alt;${CMAKE_CURRENT_BINARY_DIR}"
  )
