|
| 1 | +# Copyright 2021 Google |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +# Locates the Mono launcher and build tool. |
| 16 | +# |
| 17 | +# Optional variable arguments for this module: |
| 18 | +# MONO_DIR: Variable to specify a search path for Mono. |
| 19 | +# MONO_DISABLE_VISUAL_STUDIO_FALLBACK: Do not use Visual Studio's msbuild |
| 20 | +# as a fallback on Windows when using the Visual Studio project generator. |
| 21 | +# |
| 22 | +# Cache variables set by this module: |
| 23 | +# MONO_EXE: Location of the Mono interpreter executable. |
| 24 | +# MONO_VERSION: Version of the detected Mono installation. |
| 25 | +# MONO_CSHARP_BUILD_EXE: Location of the tool (e.g msbuild or xbuild) that |
| 26 | +# can build .csproj projects. |
| 27 | + |
| 28 | +# If MONO_DIR is specified, only search that path. |
| 29 | +set(FIND_MONO_OPTIONS "") |
| 30 | +if(EXISTS "${MONO_DIR}") |
| 31 | + set(FIND_MONO_OPTIONS |
| 32 | + PATHS |
| 33 | + "${MONO_DIR}" |
| 34 | + "${MONO_DIR}/lib/mono" |
| 35 | + PATH_SUFFIXES |
| 36 | + bin |
| 37 | + NO_DEFAULT_PATH |
| 38 | + ) |
| 39 | +endif() |
| 40 | + |
| 41 | +# Search for Mono tools. |
| 42 | +find_program(MONO_EXE mono |
| 43 | + ${FIND_MONO_OPTIONS} |
| 44 | +) |
| 45 | +find_program(MONO_CSHARP_BUILD_EXE |
| 46 | + NAMES |
| 47 | + msbuild |
| 48 | + xbuild |
| 49 | + ${FIND_MONO_OPTIONS} |
| 50 | +) |
| 51 | + |
| 52 | +if(CMAKE_HOST_WIN32 AND |
| 53 | + NOT MONO_DISABLE_VISUAL_STUDIO_FALLBACK AND |
| 54 | + EXISTS "${CMAKE_VS_MSBUILD_COMMAND}") |
| 55 | + if(NOT MONO_CSHARP_BUILD_EXE) |
| 56 | + # If Mono's build tool isn't found, fallback to Visual Studio. |
| 57 | + message(STATUS "Mono installation not found, trying to fallback " |
| 58 | + "to Visual Studio's msbuild ${CMAKE_VS_MSBUILD_COMMAND}." |
| 59 | + ) |
| 60 | + set(MONO_CSHARP_BUILD_EXE "${CMAKE_VS_MSBUILD_COMMAND}" |
| 61 | + CACHE STRING "" FORCE |
| 62 | + ) |
| 63 | + endif() |
| 64 | + if(NOT MONO_EXE) |
| 65 | + # Just use cmake to launch the C# executable which should run on Windows if |
| 66 | + # the .NET framework is installed. |
| 67 | + # NOTE: This does not use cmd as CTest passes a path with / path separators |
| 68 | + # to CreateProcess() causing cmd to fail to start. |
| 69 | + set(MONO_EXE "${CMAKE_COMMAND};-E;env" CACHE STRING "" FORCE) |
| 70 | + endif() |
| 71 | +endif() |
| 72 | + |
| 73 | +# If the mono executable is found, and retrieve the version. |
| 74 | +if(MONO_EXE) |
| 75 | + if(EXISTS "${MONO_EXE}") |
| 76 | + execute_process( |
| 77 | + COMMAND ${MONO_EXE} -V |
| 78 | + OUTPUT_VARIABLE MONO_VERSION_STRING |
| 79 | + RESULT_VARIABLE RESULT |
| 80 | + ) |
| 81 | + if(NOT ${RESULT} EQUAL "0") |
| 82 | + message(FATAL_ERROR "${MONO_EXE} -V returned ${RESULT}.") |
| 83 | + endif() |
| 84 | + string(REGEX MATCH "([0-9]*)([.])([0-9]*)([.]*)([0-9]*)" |
| 85 | + MONO_VERSION_MATCH "${MONO_VERSION_STRING}") |
| 86 | + set(MONO_VERSION "${MONO_VERSION_MATCH}" CACHE STRING "Mono version.") |
| 87 | + else() |
| 88 | + set(MONO_VERSION "0.0.0" CACHE STRING "Mono version.") |
| 89 | + endif() |
| 90 | +endif() |
| 91 | + |
| 92 | +if(NOT MONO_EXE OR NOT MONO_CSHARP_BUILD_EXE) |
| 93 | + message(FATAL_ERROR "Cannot find mono and msbuild/xbuild executables.") |
| 94 | +endif() |
| 95 | + |
| 96 | +include(FindPackageHandleStandardArgs) |
| 97 | +find_package_handle_standard_args( |
| 98 | + Mono |
| 99 | + DEFAULT_MSG |
| 100 | + MONO_EXE |
| 101 | + MONO_CSHARP_BUILD_EXE |
| 102 | + MONO_VERSION |
| 103 | +) |
| 104 | +mark_as_advanced( |
| 105 | + MONO_EXE |
| 106 | + MONO_CSHARP_BUILD_EXE |
| 107 | + MONO_VERSION |
| 108 | +) |
0 commit comments