CMakeTestCSharpCompiler.cmake 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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
  32. SOURCES "${test_compile_file}"
  33. OUTPUT_VARIABLE __CMAKE_CSharp_COMPILER_OUTPUT
  34. )
  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()