OTBApplicationMacros.cmake 7.22 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
#
# Copyright (C) 2005-2017 Centre National d'Etudes Spatiales (CNES)
#
# This file is part of Orfeo Toolbox
#
#     https://www.orfeo-toolbox.org/
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

21 22 23 24 25 26 27 28 29
macro(otb_create_application)
   cmake_parse_arguments(APPLICATION  "" "NAME;BUILD_PATH;INSTALL_PATH" "SOURCES;INCLUDE_DIRS;LINK_LIBRARIES" ${ARGN} )

   set( APPLICATION_TARGET_NAME otbapp_${APPLICATION_NAME} )

   # Build the library as a MODULE (shared lib even if OTB is built statically)
   include_directories(${APPLICATION_INCLUDE_DIRS})
   add_library(${APPLICATION_TARGET_NAME} MODULE ${APPLICATION_SOURCES})
   target_link_libraries(${APPLICATION_TARGET_NAME} OTBApplicationEngine ${APPLICATION_LINK_LIBRARIES})
30 31 32
   if(otb-module)
     otb_module_target_label(${APPLICATION_TARGET_NAME})
   endif()
33

34 35 36 37
   # Setup build output location
   # Do not output in the standard lib folder where all shared libs goes.
   # This is to avoid the application factory to look into each and every shared lib
   # for itkLoad symbol
38 39 40
   if(otb-module)
     set_property(TARGET ${APPLICATION_TARGET_NAME} PROPERTY LIBRARY_OUTPUT_DIRECTORY ${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/otb/applications)
   endif()
41

42
   # Remove the usual 'lib' prefix to make it clear it is a plugin
43
   # and not a shared library to link against
44 45 46 47 48 49 50 51 52 53 54 55 56 57
   set_property(TARGET ${APPLICATION_TARGET_NAME} PROPERTY PREFIX "")

   # on Apple platform, a "MODULE" library gets a ".so" extension
   # but the ITK factory mechanism looks for ".dylib"
   if (APPLE)
     set_property(TARGET ${APPLICATION_TARGET_NAME} PROPERTY SUFFIX ".dylib")
   endif()

   # When called from the OTB build system, use OTB_INSTALL_APP_DIR
   if (NOT APPLICATION_INSTALL_PATH AND OTB_INSTALL_APP_DIR)
     set(APPLICATION_INSTALL_PATH ${OTB_INSTALL_APP_DIR})
   endif()

   if (APPLICATION_INSTALL_PATH)
58 59 60 61 62 63 64 65 66 67 68
     if(otb-module)
       install(TARGETS ${APPLICATION_TARGET_NAME}
               EXPORT ${${otb-module}-targets}
               LIBRARY DESTINATION ${APPLICATION_INSTALL_PATH}
               COMPONENT RuntimeLibraries)
     else()
       install(TARGETS ${APPLICATION_TARGET_NAME}
               LIBRARY DESTINATION ${APPLICATION_INSTALL_PATH}
               COMPONENT RuntimeLibraries)
     endif()
   else()
69
     install(TARGETS ${APPLICATION_TARGET_NAME}
70
             LIBRARY DESTINATION lib
71
             COMPONENT RuntimeLibraries)
72 73
   endif()

74 75 76 77 78 79 80 81
   # What is the path to the applications
   # a MODULE target is always treated as LIBRARY
   get_target_property(APPLICATION_BINARY_PATH ${APPLICATION_TARGET_NAME} LIBRARY_OUTPUT_DIRECTORY)

   if (NOT APPLICATION_BINARY_PATH)
     set(APPLICATION_BINARY_PATH ${CMAKE_CURRENT_BINARY_DIR})
   endif()

82 83 84 85 86 87 88 89 90 91
   if(CMAKE_RUNTIME_OUTPUT_DIRECTORY)
     set(_script_output_dir ${CMAKE_RUNTIME_OUTPUT_DIRECTORY})
   else()
     set(_script_output_dir ${CMAKE_BINARY_DIR}/bin)
   endif()
   if(OTB_INSTALL_RUNTIME_DIR)
     set(_script_install_dir ${OTB_INSTALL_RUNTIME_DIR})
   else()
     set(_script_install_dir bin)
   endif()
92 93
   set(INTERMEDIATE_DIR ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY})
   set(SCRIPT_EXT "")
94
   if(WIN32)
95 96 97 98 99 100 101 102 103 104 105 106 107
     set(SCRIPT_EXT ".bat")
   endif()

   # ----- Create and install launcher scripts ------
   foreach(type CLI GUI)
     string(TOLOWER "${type}" type_lower)
     set(SCRIPT_NAME otb${type_lower}_${APPLICATION_NAME}${SCRIPT_EXT})
     otb_write_app_launcher(
       NAME ${APPLICATION_NAME}
       OUTPUT ${INTERMEDIATE_DIR}/${SCRIPT_NAME}
       TYPE ${type})
     # Copy it next to the application shared lib, and give executable rights
     file(COPY ${INTERMEDIATE_DIR}/${SCRIPT_NAME}
108
          DESTINATION ${_script_output_dir}
109 110
          FILE_PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE)
     # Install a version of this script if we are inside the OTB build
111 112
     install(PROGRAMS ${_script_output_dir}/${SCRIPT_NAME}
             DESTINATION ${_script_install_dir}
113 114
             COMPONENT Runtime)
   endforeach()
115 116

   list(APPEND OTB_APPLICATIONS_NAME_LIST ${APPLICATION_NAME})
117
   list(REMOVE_DUPLICATES OTB_APPLICATIONS_NAME_LIST)
118 119
   set(OTB_APPLICATIONS_NAME_LIST ${OTB_APPLICATIONS_NAME_LIST}
       CACHE STRING "List of all applications" FORCE)
120
   mark_as_advanced(OTB_APPLICATIONS_NAME_LIST)
121 122 123 124

endmacro()

macro(otb_test_application)
125 126 127 128
  cmake_parse_arguments(TESTAPPLICATION  "" "NAME;APP" "OPTIONS;TESTENVOPTIONS;VALID" ${ARGN} )
  if(otb-module)
    otb_add_test(NAME ${TESTAPPLICATION_NAME}
                  COMMAND otbTestDriver
129 130 131 132 133 134
                  ${TESTAPPLICATION_VALID}
                  Execute $<TARGET_FILE:otbApplicationLauncherCommandLine>
                  ${TESTAPPLICATION_APP}
                  $<TARGET_FILE_DIR:otbapp_${TESTAPPLICATION_APP}>
                  ${TESTAPPLICATION_OPTIONS}
                  -testenv ${TESTAPPLICATION_TESTENVOPTIONS})
135 136 137
    # Be sure that the ${otb-module}-all target triggers the build of commandline launcher and testdriver
    add_dependencies(${otb-module}-all otbApplicationLauncherCommandLine)
    add_dependencies(${otb-module}-all otbTestDriver)
138 139 140 141 142 143 144 145 146
  else()
    add_test(NAME ${TESTAPPLICATION_NAME}
            COMMAND otbTestDriver
            ${TESTAPPLICATION_VALID}
            Execute $<TARGET_FILE:otbApplicationLauncherCommandLine>
            ${TESTAPPLICATION_APP}
            $<TARGET_FILE_DIR:otbapp_${TESTAPPLICATION_APP}>
            ${TESTAPPLICATION_OPTIONS}
            -testenv ${TESTAPPLICATION_TESTENVOPTIONS})
147 148
  endif()
endmacro()
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201

macro(otb_write_app_launcher)
  cmake_parse_arguments(APPLAUNCHER  "" "NAME;OUTPUT;TYPE" "" ${ARGN} )
  if("${APPLAUNCHER_TYPE}" STREQUAL "CLI")
    set(_launcher_type "otbcli")
  elseif("${APPLAUNCHER_TYPE}" STREQUAL "GUI")
    set(_launcher_type "otbgui")
  else()
    message(FATAL_ERROR "Unknown launcher type : ${APPLAUNCHER_TYPE}, only support CLI and GUI")
  endif()

  if(WIN32)
    # Launcher script in Batch format
    file(WRITE "${APPLAUNCHER_OUTPUT}"
"@echo off
::
:: Autogenerated by OTB installation process
:: DO NOT MODIFY
::
set CURRENT_SCRIPT_DIR=%~dp0
if exist %CURRENT_SCRIPT_DIR%${_launcher_type}.bat (
  :: Prefer using the launcher inside the script dir
  set OTB_LAUNCHER=%CURRENT_SCRIPT_DIR%${_launcher_type}.bat
) else (
  :: Use the one from the PATH
  set OTB_LAUNCHER=${_launcher_type}.bat
)

:: start the application
%OTB_LAUNCHER% ${APPLAUNCHER_NAME} %*
")
  else()
    # Launcher script in Shell format
    file(WRITE "${APPLAUNCHER_OUTPUT}"
"#!/bin/sh
#
# Autogenerated by OTB installation process
# DO NOT MODIFY
#
CURRENT_SCRIPT_DIR=$(dirname \"\$0\")
if [ -e \"\$CURRENT_SCRIPT_DIR/${_launcher_type}\" ] ; then
  # Prefer using the launcher inside the script dir
  OTB_LAUNCHER=$CURRENT_SCRIPT_DIR/${_launcher_type}
else
  # Use the one from the PATH
  OTB_LAUNCHER=${_launcher_type}
fi

# start the application
\$OTB_LAUNCHER ${APPLAUNCHER_NAME} \"$@\"
")
  endif()
endmacro()