ctkMacroBuildApp.cmake 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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.commontk.org/LICENSE
  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. MACRO(ctkMacroBuildApp)
  25. ctkMacroParseArguments(MY
  26. "NAME;SRCS;MOC_SRCS;UI_FORMS;INCLUDE_DIRECTORIES;TARGET_LIBRARIES;RESOURCES"
  27. ""
  28. ${ARGN}
  29. )
  30. # Sanity checks
  31. IF(NOT DEFINED MY_NAME)
  32. MESSAGE(SEND_ERROR "NAME is mandatory")
  33. ENDIF()
  34. # IF(NOT DEFINED MY_EXPORT_DIRECTIVE)
  35. # MESSAGE(SEND_ERROR "EXPORT_DIRECTIVE is mandatory")
  36. # ENDIF()
  37. # IF(NOT DEFINED MY_LIBRARY_TYPE)
  38. # SET(MY_LIBRARY_TYPE "SHARED")
  39. # ENDIF()
  40. # Make sure either the source or the binary directory associated with the application
  41. # contains a file named ${MY_NAME}Main.cpp
  42. set(expected_mainfile ${MY_NAME}Main.cpp)
  43. if (NOT EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${expected_mainfile} AND
  44. NOT EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${expected_mainfile}.in AND
  45. NOT EXISTS ${CMAKE_CURRENT_BINARY_DIR}/${expected_mainfile})
  46. MESSAGE(FATAL_ERROR "Application directory: ${MY_NAME} should contain"
  47. " a file named ${expected_mainfile} or ${expected_mainfile}.in")
  48. endif()
  49. # Define library name
  50. SET(proj_name ${MY_NAME})
  51. # --------------------------------------------------------------------------
  52. # Include dirs
  53. SET(my_includes
  54. ${CMAKE_CURRENT_SOURCE_DIR}
  55. ${CMAKE_CURRENT_BINARY_DIR}
  56. ${MY_INCLUDE_DIRECTORIES}
  57. )
  58. # Add the include directories from the library dependencies
  59. ctkFunctionGetIncludeDirs(my_includes ${proj_name})
  60. INCLUDE_DIRECTORIES(${my_includes})
  61. # Add the library directories from the external project
  62. ctkFunctionGetLibraryDirs(my_library_dirs ${proj_name})
  63. LINK_DIRECTORIES(
  64. ${my_library_dirs}
  65. )
  66. # SET(MY_LIBRARY_EXPORT_DIRECTIVE ${MY_EXPORT_DIRECTIVE})
  67. # SET(MY_EXPORT_HEADER_PREFIX ${MY_NAME})
  68. # STRING(REGEX REPLACE "^CTK" "ctk" MY_EXPORT_HEADER_PREFIX ${MY_EXPORT_HEADER_PREFIX})
  69. # SET(MY_LIBNAME ${lib_name})
  70. # CONFIGURE_FILE(
  71. # ${CTK_SOURCE_DIR}/Libs/ctkExport.h.in
  72. # ${CMAKE_CURRENT_BINARY_DIR}/${MY_EXPORT_HEADER_PREFIX}Export.h
  73. # )
  74. # SET(dynamicHeaders
  75. # "${dynamicHeaders};${CMAKE_CURRENT_BINARY_DIR}/${MY_EXPORT_HEADER_PREFIX}Export.h")
  76. # Make sure variable are cleared
  77. SET(MY_UI_CPP)
  78. SET(MY_MOC_CPP)
  79. SET(MY_QRC_SRCS)
  80. # Wrap
  81. IF(MY_MOC_SRCS)
  82. # this is a workaround for Visual Studio. The relative include paths in the generated
  83. # moc files can get very long and can't be resolved by the MSVC compiler.
  84. FOREACH(moc_src ${MY_MOC_SRCS})
  85. QT4_WRAP_CPP(MY_MOC_CPP ${moc_src} OPTIONS -f${moc_src})
  86. ENDFOREACH()
  87. ENDIF()
  88. QT4_WRAP_UI(MY_UI_CPP ${MY_UI_FORMS})
  89. IF(DEFINED MY_RESOURCES)
  90. QT4_ADD_RESOURCES(MY_QRC_SRCS ${MY_RESOURCES})
  91. ENDIF()
  92. SOURCE_GROUP("Resources" FILES
  93. ${MY_RESOURCES}
  94. ${MY_UI_FORMS}
  95. )
  96. SOURCE_GROUP("Generated" FILES
  97. ${MY_QRC_SRCS}
  98. ${MY_MOC_CPP}
  99. ${MY_UI_CPP}
  100. )
  101. # Create executable
  102. ADD_EXECUTABLE(${proj_name}
  103. ${MY_SRCS}
  104. ${MY_MOC_CPP}
  105. ${MY_UI_CPP}
  106. ${MY_QRC_SRCS}
  107. )
  108. # ADD_LIBRARY(${lib_name} ${MY_LIBRARY_TYPE}
  109. # ${MY_SRCS}
  110. # ${MY_MOC_CPP}
  111. # ${MY_UI_CPP}
  112. # ${MY_QRC_SRCS}
  113. # )
  114. # Set labels associated with the target.
  115. SET_TARGET_PROPERTIES(${proj_name} PROPERTIES LABELS ${proj_name})
  116. # Install rules
  117. IF(CTK_BUILD_SHARED_LIBS)
  118. INSTALL(TARGETS ${proj_name}
  119. RUNTIME DESTINATION ${CTK_INSTALL_BIN_DIR} COMPONENT Runtime
  120. LIBRARY DESTINATION ${CTK_INSTALL_LIB_DIR} COMPONENT Runtime
  121. ARCHIVE DESTINATION ${CTK_INSTALL_LIB_DIR} COMPONENT Development)
  122. ENDIF()
  123. SET(my_libs
  124. ${MY_TARGET_LIBRARIES}
  125. )
  126. TARGET_LINK_LIBRARIES(${proj_name} ${my_libs})
  127. # Update CTK_BASE_LIBRARIES
  128. # SET(CTK_BASE_LIBRARIES ${my_libs} ${lib_name} CACHE INTERNAL "CTK base libraries" FORCE)
  129. # Install headers
  130. FILE(GLOB headers "${CMAKE_CURRENT_SOURCE_DIR}/*.h")
  131. INSTALL(FILES
  132. ${headers}
  133. ${dynamicHeaders}
  134. DESTINATION ${CTK_INSTALL_INCLUDE_DIR} COMPONENT Development
  135. )
  136. ENDMACRO()