SMTKMacros.cmake 7.46 KB
Newer Older
Bob Obara's avatar
Bob Obara committed
1 2 3 4 5 6 7 8
#=========================================================================
#
#  This software is distributed WITHOUT ANY WARRANTY; without even
#  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
#  PURPOSE.  See the above copyright notice for more information.
#
#=========================================================================

T.J. Corona's avatar
T.J. Corona committed
9 10
include(GenerateExportHeader)

Bob Obara's avatar
Bob Obara committed
11
# Utility to build a kit name from the current directory.
12
function(smtk_get_kit_name kitvar)
13
  string(REPLACE "${${PROJECT_NAME}_SOURCE_DIR}/" "" dir_prefix ${CMAKE_CURRENT_SOURCE_DIR})
Bob Obara's avatar
Bob Obara committed
14 15 16 17 18 19
  string(REPLACE "/" "_" kit "${dir_prefix}")
  set(${kitvar} "${kit}" PARENT_SCOPE)
  # Optional second argument to get dir_prefix.
  if (${ARGC} GREATER 1)
    set(${ARGV1} "${dir_prefix}" PARENT_SCOPE)
  endif (${ARGC} GREATER 1)
20
endfunction(smtk_get_kit_name)
Bob Obara's avatar
Bob Obara committed
21

22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
# Computes the name for the header Test Build cxx file given a header file. If
# the operating system is Windows and if the include path is too long, a hash of
# the path is used in the cxx name.
function(smtk_header_test_cxx_name name header_name return_cxx_name)

  set(max_len 100)
  set(suffix ".cxx")
  set(cxx_name ${CMAKE_CURRENT_BINARY_DIR}/TestBuild_${name}_${headername}${suffix})
  string(LENGTH ${cxx_name} len)
  if(len GREATER max_len AND WIN32)
    string(MD5 hashed_cxx_name ${CMAKE_CURRENT_BINARY_DIR}/TestBuild_${name}_${headername})
    set(cxx_name HT/TB_${hashed_cxx_name}${suffix})
  endif()

  set(${return_cxx_name} ${cxx_name} PARENT_SCOPE)

endfunction(smtk_header_test_cxx_name header_name)
Bob Obara's avatar
Bob Obara committed
39 40 41

# Builds a source file and an executable that does nothing other than
# compile the given header files.
42
function(smtk_add_header_test name dir_prefix lib)
Bob Obara's avatar
Bob Obara committed
43 44 45 46 47
  set(hfiles ${ARGN})
  set(cxxfiles)
  foreach (header ${ARGN})
    string(REPLACE "${CMAKE_CURRENT_BINARY_DIR}" "" header "${header}")
    get_filename_component(headername ${header} NAME_WE)
48 49
    smtk_header_test_cxx_name(${name} ${headername} src)
#    set(src ${CMAKE_CURRENT_BINARY_DIR}/TestBuild_${name}_${headername}${suffix})
50
    configure_file(${smtk_cmake_dir}/TestBuild.cxx.in ${src} @ONLY)
Bob Obara's avatar
Bob Obara committed
51 52
    set(cxxfiles ${cxxfiles} ${src})
  endforeach (header)
53

Bob Obara's avatar
Bob Obara committed
54
  add_library(TestBuild_${name} ${cxxfiles} ${hfiles})
55
  # target_link_libraries(TestBuild_${name} sysTools)
Bob Obara's avatar
Bob Obara committed
56 57 58
  set_source_files_properties(${hfiles}
    PROPERTIES HEADER_FILE_ONLY TRUE
    )
59 60 61

  #include the build directory for the export header
  target_include_directories(TestBuild_${name}
62
    PRIVATE ${CMAKE_CURRENT_BINARY_DIR})
63
  #also link against the associated library so we build properly
64
  target_link_libraries(TestBuild_${name}
65
    PRIVATE ${lib})
66 67


68
endfunction(smtk_add_header_test)
Bob Obara's avatar
Bob Obara committed
69 70 71 72

# Declare a list of header files.  Will make sure the header files get
# compiled and show up in an IDE. Also makes sure we install the headers
# into the include folder
73
function(smtk_public_headers lib)
74
  smtk_get_kit_name(name dir_prefix)
75 76 77 78
  foreach (header IN LISTS ARGN)
    if (IS_ABSOLUTE "${header}")
      file(RELATIVE_PATH header_sub "${CMAKE_CURRENT_BINARY_DIR}" "${header}")
      if (NOT header STREQUAL "${CMAKE_CURRENT_BINARY_DIR}/${header_sub}")
79
        message(FATAL_ERROR "Could not determine subdirectory path for '${header}' relative to '${CMAKE_CURRENT_BINARY_DIR}'")
80 81 82 83 84 85 86 87 88 89
      endif ()
    else ()
      set(header_sub "${header}")
    endif ()
    get_filename_component(subdir "${header_sub}" DIRECTORY)
    if (subdir)
      set(suffix "/${subdir}")
    else ()
      set(suffix "")
    endif ()
90
    install (FILES ${header} DESTINATION include/${PROJECT_NAME}/${PROJECT_VERSION}/${dir_prefix}${suffix})
91
  endforeach ()
92
  if (BUILD_TESTING)
93
    smtk_add_header_test("${name}" "${dir_prefix}" "${lib}" ${ARGN})
94
  endif()
95
endfunction(smtk_public_headers)
Bob Obara's avatar
Bob Obara committed
96 97 98

# Declare a list of header files.  Will make sure the header files get
# compiled and show up in an IDE.
99
function(smtk_private_headers)
100
  smtk_get_kit_name(name dir_prefix)
101
  if (BUILD_TESTING)
102 103
    smtk_add_header_test("${name}" "${dir_prefix}" ${ARGN})
  endif()
104
endfunction(smtk_private_headers)
Bob Obara's avatar
Bob Obara committed
105 106

# Declare a library as needed to be installed
107 108 109
# supports the signature
#  smtk_install_library(target [DEPENDS <targets>])
# which allows you to export a target that has dependencies
110
function(smtk_install_library target)
111
  set_target_properties(${target} PROPERTIES CXX_VISIBILITY_PRESET hidden)
112
  install(TARGETS ${target}
113
    EXPORT ${PROJECT_NAME}
114
    RUNTIME DESTINATION bin
115 116
    LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
    ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
117
  )
118
  export(TARGETS ${target} APPEND FILE ${CMAKE_BINARY_DIR}/${PROJECT_NAME}Targets.cmake)
119
endfunction(smtk_install_library)
Bob Obara's avatar
Bob Obara committed
120 121

#generate an export header and create an install target for it
122
function(smtk_export_header target file)
123
  smtk_get_kit_name(name dir_prefix)
Bob Obara's avatar
Bob Obara committed
124
  generate_export_header(${target} EXPORT_FILE_NAME ${file})
125
  install(FILES ${CMAKE_CURRENT_BINARY_DIR}/${file}  DESTINATION include/${PROJECT_NAME}/${PROJECT_VERSION}/${dir_prefix})
126 127 128 129
endfunction(smtk_export_header)

# Builds a source file and an executable that does nothing other than
# compile the given header files.
130
function(smtk_prepend_string prefix result)
131 132 133
  set(names ${ARGN})
  set(newNames "")
  foreach (name ${ARGN})
134 135 136 137 138
    if (IS_ABSOLUTE "${name}")
      set(newName "${name}")
    else ()
      set(newName "${prefix}/${name}")
    endif ()
139 140 141
    set(newNames ${newNames} ${newName})
  endforeach (name)
  set(${result} ${newNames} PARENT_SCOPE)
142
endfunction(smtk_prepend_string)
143

T.J. Corona's avatar
T.J. Corona committed
144
include(SMTKOperationXML)
145 146 147 148 149 150 151 152

# Builds source groups for the smtk files so that they show up nicely in
# Visual Studio.
# this function will set also set two variable in the parent scope.
# they will be ${source_dir}Srcs and ${source_dir}Headers. So for
# example if you call smtk_source_group(model) we will set the vars:
#   modelSrcs and modelHeaders
function(smtk_source_group source_dir)
153

154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
  set(src_prop_name ${source_dir}Srcs)
  set(header_prop_name ${source_dir}Headers)

  get_directory_property(sources DIRECTORY ${source_dir} DEFINITION ${src_prop_name})
  get_directory_property(headers DIRECTORY ${source_dir} DEFINITION ${header_prop_name})

  smtk_prepend_string("${source_dir}" sources ${sources})
  smtk_prepend_string("${source_dir}" headers ${headers})

  source_group("${source_dir}_Source" FILES ${sources})
  source_group("${source_dir}_Header" FILES ${headers})

  set(${source_dir}Srcs ${sources} PARENT_SCOPE)
  set(${source_dir}Headers ${headers} PARENT_SCOPE)

endfunction(smtk_source_group)

171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
# create implementation for a new ui view interface
# ADD_SMTK_UI_VIEW(
#    OUTIFACES
#    OUTSRCS
#    CLASS_NAME classname
#    [VIEW_NAME viewname]
#
#  CLASS_NAME: is the name of the class that implements a qtBaseView
#  VIEW_NAME: option to a name of the view which will be registered to ui-manager
MACRO(ADD_SMTK_UI_VIEW OUTIFACES OUTSRCS)

  SET(ARG_VIEW_NAME)

  PV_PLUGIN_PARSE_ARGUMENTS(ARG "CLASS_NAME;VIEW_NAME" "" ${ARGN} )

  IF(NOT ARG_VIEW_NAME)
    SET(ARG_VIEW_NAME myNoNameView)
  ENDIF()
  SET(${OUTIFACES} ${ARG_CLASS_NAME})

191
  CONFIGURE_FILE(${smtk_cmake_dir}/qtSMTKViewImplementation.h.in
192
                 ${CMAKE_CURRENT_BINARY_DIR}/${ARG_CLASS_NAME}Implementation.h @ONLY)
193
  CONFIGURE_FILE(${smtk_cmake_dir}/qtSMTKViewImplementation.cxx.in
194 195
                 ${CMAKE_CURRENT_BINARY_DIR}/${ARG_CLASS_NAME}Implementation.cxx @ONLY)

Ben Boeckel's avatar
Ben Boeckel committed
196
  qt5_wrap_cpp(VIEW_MOC_SRCS ${CMAKE_CURRENT_BINARY_DIR}/${ARG_CLASS_NAME}Implementation.h)
197 198 199 200 201 202 203

  SET(${OUTSRCS}
      ${CMAKE_CURRENT_BINARY_DIR}/${ARG_CLASS_NAME}Implementation.cxx
      ${CMAKE_CURRENT_BINARY_DIR}/${ARG_CLASS_NAME}Implementation.h
      ${VIEW_MOC_SRCS}
      )
ENDMACRO()