post_install.cmake 3.35 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
function(sanitize_system_paths input_file)
22 23 24 25 26 27 28 29
  # does not support Windows ...
  if(APPLE)
    set(SHARED_EXT "\\.dylib")
  elseif(UNIX)
    set(SHARED_EXT "\\.so")
  endif()
  set(filtered_content)

30
  file(STRINGS "${input_file}" source_file_content NEWLINE_CONSUME)
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
  string(REGEX REPLACE "\n" "\n;" source_file_content "${source_file_content}")
  set(SEARCH_REGEX "(^[^#\"]+(LIBRARIES|INCLUDE_DIR[A-Z]*) +\")(.+)(\"[^\"]*)")

  foreach(line ${source_file_content})
    set(filtered_line "${line}")
    if(line MATCHES "${SEARCH_REGEX}")
      string(REGEX REPLACE "${SEARCH_REGEX}" "\\3" extract_str "${line}" )
      string(REGEX REPLACE ";;" ";" extract_str "${extract_str}")
      set(_to_be_removed)
      set(_to_be_added)
      foreach(path ${extract_str})
        if(IS_ABSOLUTE ${path})
          list(APPEND _to_be_removed "${path}")
          if(NOT IS_DIRECTORY ${path})
            if(path MATCHES "^.*/lib[^\\.]+${SHARED_EXT}.*")
              string(REGEX REPLACE "^.*/lib([^\\.]+)${SHARED_EXT}.*" "\\1" _lib_name "${path}")
              list(APPEND _to_be_added "${_lib_name}")
            endif()
          endif()
        endif()
      endforeach()
      if(_to_be_removed)
        list(REMOVE_ITEM extract_str ${_to_be_removed})
      endif()
      if(_to_be_added)
        list(INSERT extract_str 0 ${_to_be_added})
      endif()
      if(extract_str)
        list(REMOVE_DUPLICATES extract_str)
      endif()
      string(REGEX REPLACE "${SEARCH_REGEX}" "\\1${extract_str}\\4" filtered_line "${line}" )
    endif()
    list(APPEND filtered_content "${filtered_line}")
  endforeach()

  string(REGEX REPLACE "\n;" "\n" filtered_content "${filtered_content}")
67
  file(WRITE "${input_file}" "${filtered_content}")
68 69
endfunction()

70 71 72 73 74 75 76
#check variables are set
foreach(var P_DIRS P_MATCH P_REPLACE)
  if(NOT ${var})
    message(FATAL_ERROR "${var} not set")
  endif()
endforeach()

77 78
string(REPLACE "|" ";" P_DIRS ${P_DIRS})

79

80
foreach( p_dir ${P_DIRS} )
81 82 83 84 85 86 87 88 89 90 91
  set(file_list)
  file( GLOB file_list "${p_dir}/*.cmake" )
  list(SORT file_list)
  foreach( cmake_file ${file_list} )
    file(STRINGS "${cmake_file}" MATCH_FOUND REGEX "${P_MATCH}")
    if(MATCH_FOUND)
      message(STATUS "Patching: ${cmake_file}")
      #message("Patch: Replace '${P_MATCH}' with '${P_REPLACE}' in ${cmake_file}")
      file(STRINGS "${cmake_file}" cmake_file_CONTENTS NEWLINE_CONSUME)
      string(REPLACE "${P_MATCH}" "$${}{${P_REPLACE}}" cmake_file_CONTENTS ${cmake_file_CONTENTS})
      file(WRITE "${cmake_file}"  "# This file is modified by OTB after installation.
92
      \n${cmake_file_CONTENTS}")
93
    endif()
94
    if(UNIX)
95
      sanitize_system_paths(${cmake_file})
96
    endif()
97
  endforeach() # foreach( cmake_file
98
endforeach() # foreach( p_dir