ctkMacroBuildApp.cmake 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. # Make sure variable are cleared
  62. set(MY_UI_CPP)
  63. set(MY_MOC_CPP)
  64. set(MY_QRC_SRCS)
  65. # Wrap
  66. if(MY_MOC_SRCS)
  67. # this is a workaround for Visual Studio. The relative include paths in the generated
  68. # moc files can get very long and can't be resolved by the MSVC compiler.
  69. foreach(moc_src ${MY_MOC_SRCS})
  70. QT4_WRAP_CPP(MY_MOC_CPP ${moc_src} OPTIONS -f${moc_src})
  71. endforeach()
  72. endif()
  73. QT4_WRAP_UI(MY_UI_CPP ${MY_UI_FORMS})
  74. if(DEFINED MY_RESOURCES)
  75. QT4_ADD_RESOURCES(MY_QRC_SRCS ${MY_RESOURCES})
  76. endif()
  77. source_group("Resources" FILES
  78. ${MY_RESOURCES}
  79. ${MY_UI_FORMS}
  80. )
  81. source_group("Generated" FILES
  82. ${MY_QRC_SRCS}
  83. ${MY_MOC_CPP}
  84. ${MY_UI_CPP}
  85. )
  86. # Create executable
  87. add_executable(${proj_name}
  88. ${MY_SRCS}
  89. ${MY_MOC_CPP}
  90. ${MY_UI_CPP}
  91. ${MY_QRC_SRCS}
  92. )
  93. # Set labels associated with the target.
  94. set_target_properties(${proj_name} PROPERTIES LABELS ${proj_name})
  95. # Install rules
  96. install(TARGETS ${proj_name}
  97. RUNTIME DESTINATION ${CTK_INSTALL_BIN_DIR} COMPONENT RuntimeApplications
  98. )
  99. set(my_libs
  100. ${MY_TARGET_LIBRARIES}
  101. )
  102. target_link_libraries(${proj_name} ${my_libs})
  103. # Install headers
  104. if(MY_INSTALL)
  105. file(GLOB headers "${CMAKE_CURRENT_SOURCE_DIR}/*.h")
  106. install(FILES
  107. ${headers}
  108. ${dynamicHeaders}
  109. DESTINATION ${CTK_INSTALL_INCLUDE_DIR} COMPONENT Development
  110. )
  111. endif()
  112. endmacro()