CMakeLists.txt 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. cmake_minimum_required(VERSION 3.1)
  2. project(QtAutogen)
  3. # Tell find_package(Qt5) where to find Qt.
  4. if(QT_QMAKE_EXECUTABLE)
  5. get_filename_component(Qt_BIN_DIR "${QT_QMAKE_EXECUTABLE}" PATH)
  6. get_filename_component(Qt_PREFIX_DIR "${Qt_BIN_DIR}" PATH)
  7. set(CMAKE_PREFIX_PATH ${Qt_PREFIX_DIR})
  8. endif()
  9. if (QT_TEST_VERSION STREQUAL 4)
  10. find_package(Qt4 REQUIRED)
  11. # Include this directory before using the UseQt4 file.
  12. add_subdirectory(defines_test)
  13. include(UseQt4)
  14. set(QT_QTCORE_TARGET Qt4::QtCore)
  15. macro(qtx_wrap_cpp)
  16. qt4_wrap_cpp(${ARGN})
  17. endmacro()
  18. else()
  19. if (NOT QT_TEST_VERSION STREQUAL 5)
  20. message(SEND_ERROR "Invalid Qt version specified.")
  21. endif()
  22. find_package(Qt5Widgets REQUIRED)
  23. set(QT_QTCORE_TARGET Qt5::Core)
  24. include_directories(${Qt5Widgets_INCLUDE_DIRS})
  25. set(QT_LIBRARIES Qt5::Widgets)
  26. if(Qt5_POSITION_INDEPENDENT_CODE AND CMAKE_CXX_COMPILE_OPTIONS_PIC)
  27. add_definitions(${CMAKE_CXX_COMPILE_OPTIONS_PIC})
  28. endif()
  29. macro(qtx_wrap_cpp)
  30. qt5_wrap_cpp(${ARGN})
  31. endmacro()
  32. endif()
  33. add_executable(rcconly rcconly.cpp second_resource.qrc)
  34. set_property(TARGET rcconly PROPERTY AUTORCC ON)
  35. target_link_libraries(rcconly ${QT_QTCORE_TARGET})
  36. include_directories(${CMAKE_CURRENT_BINARY_DIR})
  37. add_definitions(-DFOO -DSomeDefine="Barx")
  38. # enable relaxed mode so automoc can handle all the special cases:
  39. set(CMAKE_AUTOMOC_RELAXED_MODE TRUE)
  40. set(CMAKE_AUTOUIC ON)
  41. set(CMAKE_AUTORCC ON)
  42. # create an executable and two library targets, each requiring automoc:
  43. add_library(codeeditorLib STATIC codeeditor.cpp)
  44. add_library(privateSlot OBJECT private_slot.cpp)
  45. configure_file(generated_resource.qrc.in generated_resource.qrc @ONLY)
  46. add_custom_command(
  47. OUTPUT generated.txt
  48. COMMAND ${CMAKE_COMMAND} -E copy "${CMAKE_CURRENT_SOURCE_DIR}/generated.txt.in" "${CMAKE_CURRENT_BINARY_DIR}/generated.txt"
  49. DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/generated.txt.in"
  50. )
  51. add_custom_target(generate_moc_input
  52. DEPENDS generated.txt
  53. COMMAND ${CMAKE_COMMAND} -E copy "${CMAKE_CURRENT_SOURCE_DIR}/myinterface.h.in" "${CMAKE_CURRENT_BINARY_DIR}"
  54. COMMAND ${CMAKE_COMMAND} -E rename "${CMAKE_CURRENT_BINARY_DIR}/myinterface.h.in" "${CMAKE_CURRENT_BINARY_DIR}/myinterface.h"
  55. )
  56. add_custom_command(
  57. OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/myotherinterface.h"
  58. COMMAND ${CMAKE_COMMAND} -E copy "${CMAKE_CURRENT_SOURCE_DIR}/myotherinterface.h.in" "${CMAKE_CURRENT_BINARY_DIR}/myotherinterface.h"
  59. DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/myotherinterface.h.in"
  60. )
  61. if (NOT CMAKE_CONFIGURATION_TYPES AND NOT CMAKE_GENERATOR STREQUAL Ninja)
  62. set(debug_srcs "$<$<CONFIG:Debug>:debug_class.cpp>" $<$<CONFIG:Debug>:debug_resource.qrc>)
  63. set_property(DIRECTORY APPEND PROPERTY COMPILE_DEFINITIONS $<$<CONFIG:Debug>:TEST_DEBUG_CLASS>)
  64. endif()
  65. # The -no-protection option disables the generation of include guards. Verify
  66. # that setting the source file property has an effect by using this and
  67. # issue an error in the preprocessor in calwidget.cpp if the include guard
  68. # is defined.
  69. set_source_files_properties(calwidget.ui PROPERTIES AUTOUIC_OPTIONS "-no-protection")
  70. add_executable(QtAutogen main.cpp calwidget.cpp second_widget.cpp foo.cpp blub.cpp bar.cpp abc.cpp
  71. multiplewidgets.cpp
  72. xyz.cpp yaf.cpp gadget.cpp $<TARGET_OBJECTS:privateSlot>
  73. test.qrc second_resource.qrc resourcetester.cpp generated.cpp ${debug_srcs}
  74. ${CMAKE_CURRENT_BINARY_DIR}/generated_resource.qrc
  75. )
  76. set_property(TARGET QtAutogen APPEND PROPERTY AUTOGEN_TARGET_DEPENDS generate_moc_input "${CMAKE_CURRENT_BINARY_DIR}/myotherinterface.h")
  77. add_executable(targetObjectsTest targetObjectsTest.cpp $<TARGET_OBJECTS:privateSlot>)
  78. target_link_libraries(targetObjectsTest ${QT_LIBRARIES})
  79. set_target_properties(
  80. QtAutogen codeeditorLib privateSlot targetObjectsTest
  81. PROPERTIES
  82. AUTOMOC TRUE
  83. )
  84. include(GenerateExportHeader)
  85. # The order is relevant here. B depends on A, and B headers depend on A
  86. # headers both subdirectories use CMAKE_INCLUDE_CURRENT_DIR_IN_INTERFACE and we
  87. # test that CMAKE_AUTOMOC successfully reads the include directories
  88. # for the build interface from those targets. There has previously been
  89. # a bug where caching of the include directories happened before
  90. # extracting the includes to pass to moc.
  91. add_subdirectory(Bdir)
  92. add_subdirectory(Adir)
  93. add_library(libC SHARED libC.cpp)
  94. set_target_properties(libC PROPERTIES AUTOMOC TRUE)
  95. generate_export_header(libC)
  96. target_link_libraries(libC LINK_PUBLIC libB)
  97. target_link_libraries(QtAutogen codeeditorLib ${QT_LIBRARIES} libC)
  98. # Add not_generated_file.qrc to the source list to get the file-level
  99. # dependency, but don't generate a c++ file from it. Disable the AUTORCC
  100. # feature for this target. This tests that qrc files in the sources don't
  101. # have an effect on generation if AUTORCC is off.
  102. add_library(empty STATIC empty.cpp not_generated_file.qrc)
  103. set_target_properties(empty PROPERTIES AUTORCC OFF)
  104. set_target_properties(empty PROPERTIES AUTOMOC TRUE)
  105. target_link_libraries(empty no_link_language)
  106. add_library(no_link_language STATIC empty.h)
  107. set_target_properties(no_link_language PROPERTIES AUTOMOC TRUE)
  108. qtx_wrap_cpp(uicOnlyMoc sub/uiconly.h)
  109. add_executable(uiconly sub/uiconly.cpp ${uicOnlyMoc})
  110. target_link_libraries(uiconly ${QT_LIBRARIES})
  111. try_compile(RCC_DEPENDS
  112. "${CMAKE_CURRENT_BINARY_DIR}/autorcc_depends"
  113. "${CMAKE_CURRENT_SOURCE_DIR}/autorcc_depends"
  114. autorcc_depends
  115. CMAKE_FLAGS "-DQT_QMAKE_EXECUTABLE:FILEPATH=${QT_QMAKE_EXECUTABLE}" "-DQT_TEST_VERSION=${QT_TEST_VERSION}"
  116. "-DCMAKE_PREFIX_PATH=${Qt_PREFIX_DIR}"
  117. OUTPUT_VARIABLE output
  118. )
  119. if (NOT RCC_DEPENDS)
  120. message(SEND_ERROR "Initial build of autorcc_depends failed. Output: ${output}")
  121. endif()
  122. file(STRINGS "${CMAKE_CURRENT_BINARY_DIR}/autorcc_depends/info_file.txt" qrc_files)
  123. list(GET qrc_files 0 qrc_file1)
  124. set(timeformat "%Y%j%H%M%S")
  125. file(TIMESTAMP "${qrc_file1}" file1_before "${timeformat}")
  126. execute_process(COMMAND "${CMAKE_COMMAND}" -E sleep 1) # Ensure that the timestamp will change.
  127. execute_process(COMMAND "${CMAKE_COMMAND}" -E touch "${CMAKE_CURRENT_BINARY_DIR}/autorcc_depends/res1/input.txt")
  128. execute_process(COMMAND "${CMAKE_COMMAND}" --build .
  129. WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/autorcc_depends"
  130. )
  131. file(TIMESTAMP "${qrc_file1}" file1_step1 "${timeformat}")
  132. if (NOT file1_step1 GREATER file1_before)
  133. message(SEND_ERROR "file1 (${qrc_file1}) should have changed in the first step!")
  134. endif()
  135. #-----------------------------------------------------------------------------
  136. try_compile(MOC_RERUN
  137. "${CMAKE_CURRENT_BINARY_DIR}/automoc_rerun"
  138. "${CMAKE_CURRENT_SOURCE_DIR}/automoc_rerun"
  139. automoc_rerun
  140. CMAKE_FLAGS "-DQT_QMAKE_EXECUTABLE:FILEPATH=${QT_QMAKE_EXECUTABLE}" "-DQT_TEST_VERSION=${QT_TEST_VERSION}"
  141. "-DCMAKE_PREFIX_PATH=${Qt_PREFIX_DIR}"
  142. OUTPUT_VARIABLE output
  143. )
  144. if (NOT MOC_RERUN)
  145. message(SEND_ERROR "Initial build of automoc_rerun failed. Output: ${output}")
  146. endif()
  147. configure_file(automoc_rerun/test1.h.in2 automoc_rerun/test1.h COPYONLY)
  148. execute_process(COMMAND "${CMAKE_COMMAND}" --build .
  149. WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/automoc_rerun"
  150. RESULT_VARIABLE automoc_rerun_result
  151. )
  152. if (automoc_rerun_result)
  153. message(SEND_ERROR "Second build of automoc_rerun failed.")
  154. endif()