CMakeTestCSharpCompiler.cmake 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. # Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. # file Copyright.txt or https://cmake.org/licensing for details.
  3. if(CMAKE_CSharp_COMPILER_FORCED)
  4. # The compiler configuration was forced by the user.
  5. # Assume the user has configured all compiler information.
  6. set(CMAKE_CSharp_COMPILER_WORKS TRUE)
  7. return()
  8. endif()
  9. include(CMakeTestCompilerCommon)
  10. unset(CMAKE_CSharp_COMPILER_WORKS CACHE)
  11. # This file is used by EnableLanguage in cmGlobalGenerator to
  12. # determine that the selected C# compiler can actually compile
  13. # and link the most basic of programs. If not, a fatal error
  14. # is set and cmake stops processing commands and will not generate
  15. # any makefiles or projects.
  16. if(NOT CMAKE_CSharp_COMPILER_WORKS)
  17. # Don't call PrintTestCompilerStatus() because the "C#" we want to pass
  18. # as the LANG doesn't match with the variable name "CMAKE_CSharp_COMPILER"
  19. message(CHECK_START "Check for working C# compiler: ${CMAKE_CSharp_COMPILER}")
  20. string(CONCAT __TestCompiler_testCSharpCompilerSource
  21. "namespace Test {\n"
  22. " public class CSharp {\n"
  23. " static void Main(string[] args) {}\n"
  24. " }\n"
  25. "}\n"
  26. )
  27. # Clear result from normal variable.
  28. unset(CMAKE_CSharp_COMPILER_WORKS)
  29. # Puts test result in cache variable.
  30. try_compile(CMAKE_CSharp_COMPILER_WORKS
  31. SOURCE_FROM_VAR testCSharpCompiler.cs __TestCompiler_testCSharpCompilerSource
  32. OUTPUT_VARIABLE __CMAKE_CSharp_COMPILER_OUTPUT
  33. )
  34. unset(__TestCompiler_testCSharpCompilerSource)
  35. # Move result from cache to normal variable.
  36. set(CMAKE_CSharp_COMPILER_WORKS ${CMAKE_CSharp_COMPILER_WORKS})
  37. unset(CMAKE_CSharp_COMPILER_WORKS CACHE)
  38. set(CSharp_TEST_WAS_RUN 1)
  39. endif()
  40. if(NOT CMAKE_CSharp_COMPILER_WORKS)
  41. PrintTestCompilerResult(CHECK_FAIL "broken")
  42. file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
  43. "Determining if the C# compiler works failed with "
  44. "the following output:\n${__CMAKE_CSharp_COMPILER_OUTPUT}\n\n")
  45. string(REPLACE "\n" "\n " _output "${__CMAKE_CSharp_COMPILER_OUTPUT}")
  46. message(FATAL_ERROR "The C# compiler\n \"${CMAKE_CSharp_COMPILER}\"\n"
  47. "is not able to compile a simple test program.\nIt fails "
  48. "with the following output:\n ${_output}\n\n"
  49. "CMake will not be able to correctly generate this project.")
  50. else()
  51. if(CSharp_TEST_WAS_RUN)
  52. PrintTestCompilerResult(CHECK_PASS "works")
  53. file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
  54. "Determining if the C# compiler works passed with "
  55. "the following output:\n${__CMAKE_CSharp_COMPILER_OUTPUT}\n\n")
  56. endif()
  57. # Re-configure to save learned information.
  58. configure_file(
  59. ${CMAKE_ROOT}/Modules/CMakeCSharpCompiler.cmake.in
  60. ${CMAKE_PLATFORM_INFO_DIR}/CMakeCSharpCompiler.cmake
  61. @ONLY
  62. )
  63. include(${CMAKE_PLATFORM_INFO_DIR}/CMakeCSharpCompiler.cmake)
  64. endif()