|
| 1 | +#============================================================================= |
| 2 | +# Copyright 2016 Sam Hanes |
| 3 | +# |
| 4 | +# Distributed under the OSI-approved BSD License (the "License"); |
| 5 | +# see accompanying file COPYING.txt for details. |
| 6 | +# |
| 7 | +# This software is distributed WITHOUT ANY WARRANTY; without even the |
| 8 | +# implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 9 | +# See the License for more information. |
| 10 | +#============================================================================= |
| 11 | +# (To distribute this file outside of CMake-Microchip, |
| 12 | +# substitute the full License text for the above reference.) |
| 13 | + |
| 14 | +function(MICROCHIP_PATH_SEARCH outvar target) |
| 15 | + set(options) |
| 16 | + list(APPEND oneValueArgs "CACHE") |
| 17 | + set(multiValueArgs BAD_VERSIONS) |
| 18 | + cmake_parse_arguments(SEARCH |
| 19 | + "${options}" "${oneValueArgs}" "${multiValueArgs}" |
| 20 | + ${ARGN} |
| 21 | + ) |
| 22 | + |
| 23 | + if(SEARCH_CACHE) |
| 24 | + set(${outvar} "" CACHE PATH "${SEARCH_CACHE}") |
| 25 | + if(${outvar}) |
| 26 | + if(EXISTS "${${outvar}}") |
| 27 | + set(${outvar} "${${outvar}}" PARENT_SCOPE) |
| 28 | + else() |
| 29 | + message(FATAL_ERROR |
| 30 | + "given path '${${outvar}}' does not exist" |
| 31 | + " in ${outvar} (${SEARCH_CACHE})" |
| 32 | + ) |
| 33 | + set(${outvar} "${outvar}-NOTFOUND" PARENT_SCOPE) |
| 34 | + endif() |
| 35 | + return() |
| 36 | + endif() |
| 37 | + endif() |
| 38 | + |
| 39 | + set(MICROCHIP_SEARCH_PATH "" |
| 40 | + CACHE STRING "the search path for Microchip tool installations" |
| 41 | + ) |
| 42 | + |
| 43 | + if(CMAKE_HOST_SYSTEM MATCHES "Linux") |
| 44 | + list(APPEND MICROCHIP_SEARCH_PATH /opt/microchip) |
| 45 | + elseif(CMAKE_HOST_SYSTEM MATCHES "Windows") |
| 46 | + list(APPEND MICROCHIP_SEARCH_PATH |
| 47 | + "C:/Program Files/Microchip" |
| 48 | + "C:/Program Files (x86)/Microchip" |
| 49 | + ) |
| 50 | + endif() |
| 51 | + |
| 52 | + set(candidate_paths) |
| 53 | + foreach(path ${MICROCHIP_SEARCH_PATH}) |
| 54 | + if(IS_DIRECTORY "${path}/${target}") |
| 55 | + list(APPEND candidate_paths "${path}/${target}") |
| 56 | + endif() |
| 57 | + endforeach() |
| 58 | + |
| 59 | + set(best_good_path) |
| 60 | + set(best_good_version) |
| 61 | + set(best_bad_path) |
| 62 | + set(best_bad_version) |
| 63 | + |
| 64 | + set(msg "\nSearching for Microchip tool '${target}' (${outvar}):") |
| 65 | + |
| 66 | + foreach(path ${candidate_paths}) |
| 67 | + file(GLOB versions RELATIVE "${path}" "${path}/v*") |
| 68 | + foreach(version_path ${versions}) |
| 69 | + string(REGEX REPLACE "^v" "" version "${version_path}") |
| 70 | + |
| 71 | + set(type good) |
| 72 | + if(${version} IN_LIST SEARCH_BAD_VERSIONS) |
| 73 | + set(type bad) |
| 74 | + endif() |
| 75 | + |
| 76 | + string(APPEND msg |
| 77 | + "\n ${type} ${version} = ${path}/${version_path}" |
| 78 | + ) |
| 79 | + |
| 80 | + if(NOT best_${type}_version |
| 81 | + OR version VERSION_GREATER best_${type}_version) |
| 82 | + set(best_${type}_version "${version}") |
| 83 | + set(best_${type}_path "${path}/${version_path}") |
| 84 | + endif() |
| 85 | + endforeach() |
| 86 | + endforeach() |
| 87 | + |
| 88 | + if(best_good_path) |
| 89 | + set(result "${best_good_path}") |
| 90 | + elseif(best_bad_path) |
| 91 | + set(result "${best_bad_path}") |
| 92 | + |
| 93 | + message(WARNING |
| 94 | + "Version ${best_bad_version} of ${target} is known" |
| 95 | + " to be problematic. If you encounter issues, set" |
| 96 | + " ${outvar} to a different version." |
| 97 | + ) |
| 98 | + else() |
| 99 | + set(result "${outvar}-NOTFOUND") |
| 100 | + endif() |
| 101 | + |
| 102 | + string(APPEND msg |
| 103 | + "\n Best good version: ${best_good_version}" |
| 104 | + "\n Best bad version: ${best_bad_version}" |
| 105 | + "\n Chose: ${result}" |
| 106 | + ) |
| 107 | + file(APPEND |
| 108 | + "${CMAKE_BINARY_DIR}/${CMAKE_FILES_DIRECTORY}/CMakeOutput.log" |
| 109 | + "${msg}\n\n" |
| 110 | + ) |
| 111 | + |
| 112 | + if(SEARCH_CACHE) |
| 113 | + set(${outvar} "${result}" |
| 114 | + CACHE PATH "${SEARCH_CACHE}" FORCE |
| 115 | + ) |
| 116 | + endif() |
| 117 | + set(${outvar} "${result}" PARENT_SCOPE) |
| 118 | +endfunction() |
0 commit comments