ctkMacroBuildApp.cmake 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. ###########################################################################
  2. #
  3. # Library: CTK
  4. #
  5. # Copyright (c) Kitware Inc.
  6. #
  7. # Licensed under the Apache License, Version 2.0 (the "License");
  8. # you may not use this file except in compliance with the License.
  9. # You may obtain a copy of the License at
  10. #
  11. # http://www.apache.org/licenses/LICENSE-2.0.txt
  12. #
  13. # Unless required by applicable law or agreed to in writing, software
  14. # distributed under the License is distributed on an "AS IS" BASIS,
  15. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. # See the License for the specific language governing permissions and
  17. # limitations under the License.
  18. #
  19. ###########################################################################
  20. #
  21. # Depends on:
  22. # CTK/CMake/ctkMacroParseArguments.cmake
  23. #
  24. #! \ingroup CMakeAPI
  25. macro(ctkMacroBuildApp)
  26. ctkMacroParseArguments(MY
  27. "NAME;SRCS;MOC_SRCS;UI_FORMS;INCLUDE_DIRECTORIES;TARGET_LIBRARIES;RESOURCES"
  28. "INSTALL"
  29. ${ARGN}
  30. )
  31. # Keep parameter 'INCLUDE_DIRECTORIES' for backward compatiblity
  32. # Sanity checks
  33. if(NOT DEFINED MY_NAME)
  34. message(FATAL_ERROR "NAME is mandatory")
  35. endif()
  36. # Make sure either the source or the binary directory associated with the application
  37. # contains a file named ${MY_NAME}Main.cpp
  38. set(expected_mainfile ${MY_NAME}Main.cpp)
  39. if (NOT EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${expected_mainfile} AND
  40. NOT EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${expected_mainfile}.in AND
  41. NOT EXISTS ${CMAKE_CURRENT_BINARY_DIR}/${expected_mainfile})
  42. message(FATAL_ERROR "Application directory: ${MY_NAME} should contain"
  43. " a file named ${expected_mainfile} or ${expected_mainfile}.in")
  44. endif()
  45. # Define library name
  46. set(proj_name ${MY_NAME})
  47. # --------------------------------------------------------------------------
  48. # Include dirs
  49. set(my_includes
  50. ${CMAKE_CURRENT_SOURCE_DIR}
  51. ${CMAKE_CURRENT_BINARY_DIR}
  52. )
  53. # Add the include directories from the library dependencies
  54. ctkFunctionGetIncludeDirs(my_includes ${proj_name})
  55. include_directories(${my_includes})
  56. # Add the library directories from the external project
  57. ctkFunctionGetLibraryDirs(my_library_dirs ${proj_name})
  58. link_directories(
  59. ${my_library_dirs}
  60. )
  61. if(CTK_QT_VERSION VERSION_LESS "5")
  62. # Add Qt include dirs and defines
  63. include(${QT_USE_FILE})
  64. endif()
  65. # Make sure variable are cleared
  66. set(MY_UI_CPP)
  67. set(MY_MOC_CPP)
  68. set(MY_QRC_SRCS)
  69. if (CTK_QT_VERSION VERSION_GREATER "4")
  70. # Wrap
  71. if(MY_MOC_SRCS)
  72. # this is a workaround for Visual Studio. The relative include paths in the generated
  73. # moc files can get very long and can't be resolved by the MSVC compiler.
  74. foreach(moc_src ${MY_MOC_SRCS})
  75. QT5_WRAP_CPP(MY_MOC_CPP ${moc_src} OPTIONS -f${moc_src} OPTIONS -DHAVE_QT5)
  76. endforeach()
  77. endif()
  78. QT5_WRAP_UI(MY_UI_CPP ${MY_UI_FORMS})
  79. if(DEFINED MY_RESOURCES)
  80. QT5_ADD_RESOURCES(MY_QRC_SRCS ${MY_RESOURCES})
  81. endif()
  82. else()
  83. # Wrap
  84. if(MY_MOC_SRCS)
  85. # this is a workaround for Visual Studio. The relative include paths in the generated
  86. # moc files can get very long and can't be resolved by the MSVC compiler.
  87. foreach(moc_src ${MY_MOC_SRCS})
  88. QT4_WRAP_CPP(MY_MOC_CPP ${moc_src} OPTIONS -f${moc_src})
  89. endforeach()
  90. endif()
  91. QT4_WRAP_UI(MY_UI_CPP ${MY_UI_FORMS})
  92. if(DEFINED MY_RESOURCES)
  93. QT4_ADD_RESOURCES(MY_QRC_SRCS ${MY_RESOURCES})
  94. endif()
  95. endif()
  96. source_group("Resources" FILES
  97. ${MY_RESOURCES}
  98. ${MY_UI_FORMS}
  99. )
  100. source_group("Generated" FILES
  101. ${MY_QRC_SRCS}
  102. ${MY_MOC_CPP}
  103. ${MY_UI_CPP}
  104. )
  105. # Create executable
  106. add_executable(${proj_name}
  107. ${MY_SRCS}
  108. ${MY_MOC_CPP}
  109. ${MY_UI_CPP}
  110. ${MY_QRC_SRCS}
  111. )
  112. # Set labels associated with the target.
  113. set_target_properties(${proj_name} PROPERTIES LABELS ${proj_name})
  114. # Install rules
  115. install(TARGETS ${proj_name}
  116. RUNTIME DESTINATION ${CTK_INSTALL_BIN_DIR} COMPONENT RuntimeApplications
  117. )
  118. set(my_libs
  119. ${MY_TARGET_LIBRARIES}
  120. )
  121. target_link_libraries(${proj_name} ${my_libs})
  122. # Install headers
  123. if(MY_INSTALL)
  124. file(GLOB headers "${CMAKE_CURRENT_SOURCE_DIR}/*.h")
  125. install(FILES
  126. ${headers}
  127. ${dynamicHeaders}
  128. DESTINATION ${CTK_INSTALL_INCLUDE_DIR} COMPONENT Development
  129. )
  130. endif()
  131. endmacro()