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