CTK  0.1.0
The Common Toolkit is a community effort to provide support code for medical image analysis, surgical navigation, and related projects.
ctkMacroBuildApp.cmake
Go to the documentation of this file.
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 #
22 # Depends on:
23 # CTK/CMake/ctkMacroParseArguments.cmake
24 #
25 
26 #! \ingroup CMakeAPI
27 macro(ctkMacroBuildApp)
28  ctkMacroParseArguments(MY
29  "NAME;SRCS;MOC_SRCS;UI_FORMS;INCLUDE_DIRECTORIES;TARGET_LIBRARIES;RESOURCES"
30  "INSTALL"
31  ${ARGN}
32  )
33 
34  # Keep parameter 'INCLUDE_DIRECTORIES' for backward compatiblity
35 
36  # Sanity checks
37  if(NOT DEFINED MY_NAME)
38  message(FATAL_ERROR "NAME is mandatory")
39  endif()
40 
41  # Make sure either the source or the binary directory associated with the application
42  # contains a file named ${MY_NAME}Main.cpp
43  set(expected_mainfile ${MY_NAME}Main.cpp)
44  if (NOT EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${expected_mainfile} AND
45  NOT EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${expected_mainfile}.in AND
46  NOT EXISTS ${CMAKE_CURRENT_BINARY_DIR}/${expected_mainfile})
47  message(FATAL_ERROR "Application directory: ${MY_NAME} should contain"
48  " a file named ${expected_mainfile} or ${expected_mainfile}.in")
49  endif()
50 
51  # Define library name
52  set(proj_name ${MY_NAME})
53 
54  # --------------------------------------------------------------------------
55  # Include dirs
56  set(my_includes
57  ${CMAKE_CURRENT_SOURCE_DIR}
58  ${CMAKE_CURRENT_BINARY_DIR}
59  )
60 
61  # Add the include directories from the library dependencies
62  ctkFunctionGetIncludeDirs(my_includes ${proj_name})
63 
64  include_directories(${my_includes})
65 
66  # Add the library directories from the external project
67  ctkFunctionGetLibraryDirs(my_library_dirs ${proj_name})
68 
69  link_directories(
70  ${my_library_dirs}
71  )
72 
73  if(CTK_QT_VERSION VERSION_LESS "5")
74  # Add Qt include dirs and defines
75  include(${QT_USE_FILE})
76  endif()
77 
78  # Make sure variable are cleared
79  set(MY_UI_CPP)
80  set(MY_MOC_CPP)
81  set(MY_QRC_SRCS)
82 
83  if (CTK_QT_VERSION VERSION_GREATER "4")
84  # Wrap
85  if(MY_MOC_SRCS)
86  # this is a workaround for Visual Studio. The relative include paths in the generated
87  # moc files can get very long and can't be resolved by the MSVC compiler.
88  foreach(moc_src ${MY_MOC_SRCS})
89  QT5_WRAP_CPP(MY_MOC_CPP ${moc_src} OPTIONS -f${moc_src} OPTIONS -DHAVE_QT5)
90  endforeach()
91  endif()
92  QT5_WRAP_UI(MY_UI_CPP ${MY_UI_FORMS})
93  if(DEFINED MY_RESOURCES)
94  QT5_ADD_RESOURCES(MY_QRC_SRCS ${MY_RESOURCES})
95  endif()
96  else()
97  # Wrap
98  if(MY_MOC_SRCS)
99  # this is a workaround for Visual Studio. The relative include paths in the generated
100  # moc files can get very long and can't be resolved by the MSVC compiler.
101  foreach(moc_src ${MY_MOC_SRCS})
102  QT4_WRAP_CPP(MY_MOC_CPP ${moc_src} OPTIONS -f${moc_src})
103  endforeach()
104  endif()
105  QT4_WRAP_UI(MY_UI_CPP ${MY_UI_FORMS})
106  if(DEFINED MY_RESOURCES)
107  QT4_ADD_RESOURCES(MY_QRC_SRCS ${MY_RESOURCES})
108  endif()
109  endif()
110 
111  source_group("Resources" FILES
112  ${MY_RESOURCES}
113  ${MY_UI_FORMS}
114  )
115 
116  source_group("Generated" FILES
117  ${MY_QRC_SRCS}
118  ${MY_MOC_CPP}
119  ${MY_UI_CPP}
120  )
121 
122  # Create executable
123  add_executable(${proj_name}
124  ${MY_SRCS}
125  ${MY_MOC_CPP}
126  ${MY_UI_CPP}
127  ${MY_QRC_SRCS}
128  )
129 
130  # Set labels associated with the target.
131  set_target_properties(${proj_name} PROPERTIES LABELS ${proj_name})
132 
133  # Install rules
134  install(TARGETS ${proj_name}
135  RUNTIME DESTINATION ${CTK_INSTALL_BIN_DIR} COMPONENT RuntimeApplications
136  )
137 
138  set(my_libs
139  ${MY_TARGET_LIBRARIES}
140  )
141  target_link_libraries(${proj_name} ${my_libs})
142 
143  # Install headers
144  if(MY_INSTALL)
145  file(GLOB headers "${CMAKE_CURRENT_SOURCE_DIR}/*.h")
146  install(FILES
147  ${headers}
148  ${dynamicHeaders}
149  DESTINATION ${CTK_INSTALL_INCLUDE_DIR} COMPONENT Development
150  )
151  endif()
152 
153 endmacro()
154 
155