summaryrefslogtreecommitdiff
path: root/cmake/CMakeDetermineCSharpCompiler.cmake
blob: 86532eb986a8f59df81f6c56d06922c07797c339 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# copyright (c) 2007, 2009 Arno Rehn arno@arnorehn.de
# copyright (c) 2008 Helio castro helio@kde.org
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

# determine the compiler to use for C# programs
# NOTE, a generator may set CMAKE_CSharp_COMPILER before
# loading this file to force a compiler.

if(NOT CMAKE_CSharp_COMPILER)
    # prefer the environment variable CSC
    if($ENV{CSC} MATCHES ".+")
        if (EXISTS $ENV{CSC})
            message(STATUS "Found compiler set in environment variable CSC: $ENV{CSC}.")
            set(CMAKE_CSharp_COMPILER $ENV{CSC})
        else (EXISTS $ENV{CSC})
            message(SEND_ERROR "Could not find compiler set in environment variable CSC:\n$ENV{CSC}.")
        endif (EXISTS $ENV{CSC})
    endif($ENV{CSC} MATCHES ".+")

    # if no compiler has been specified yet, then look for one
    if (NOT CMAKE_CSharp_COMPILER)
        find_package(Mono)
        set (CMAKE_CSharp_COMPILER "${GMCS_EXECUTABLE}")

        # still not found, try csc.exe
        if (NOT CMAKE_CSharp_COMPILER)
            get_filename_component(dotnet_path "[HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\.NETFramework;InstallRoot]" PATH)
            find_program(CMAKE_CSharp_COMPILER NAMES csc PATHS "${dotnet_path}/Framework/v2.0.50727")
            file(TO_NATIVE_PATH "${dotnet_path}/Framework/v2.0.50727" native_path)
            message(STATUS "Looking for csc: ${CMAKE_CSharp_COMPILER}")

            # give up
            if (NOT CMAKE_CSharp_COMPILER)
                message (STATUS "Couldn't find a valid C# compiler. Set either CMake_CSharp_COMPILER or the CSC environment variable to a valid path.")
            endif (NOT CMAKE_CSharp_COMPILER)
        endif (NOT CMAKE_CSharp_COMPILER)
    endif (NOT CMAKE_CSharp_COMPILER)

endif(NOT CMAKE_CSharp_COMPILER)

# now try to find the gac location
if (CMAKE_CSharp_COMPILER AND NOT GAC_DIR AND MONO_FOUND)
    find_package(PkgConfig)

    if (PKG_CONFIG_FOUND)
        pkg_search_module(MONO_CECIL mono-cecil)
        if(MONO_CECIL_FOUND)
            execute_process(COMMAND ${PKG_CONFIG_EXECUTABLE} mono-cecil --variable=assemblies_dir OUTPUT_VARIABLE GAC_DIR OUTPUT_STRIP_TRAILING_WHITESPACE)
        endif(MONO_CECIL_FOUND)

        pkg_search_module(CECIL cecil)
        if(CECIL_FOUND)
            execute_process(COMMAND ${PKG_CONFIG_EXECUTABLE} cecil --variable=assemblies_dir OUTPUT_VARIABLE GAC_DIR OUTPUT_STRIP_TRAILING_WHITESPACE)
        endif(CECIL_FOUND)

        if (NOT GAC_DIR)
            execute_process(COMMAND ${PKG_CONFIG_EXECUTABLE} mono --variable=libdir OUTPUT_VARIABLE MONO_LIB_DIR OUTPUT_STRIP_TRAILING_WHITESPACE)
            if (MONO_LIB_DIR)
                set (GAC_DIR "${MONO_LIB_DIR}/mono")
                message (STATUS "Could not find cecil, guessing GAC dir from mono prefix: ${GAC_DIR}")
            endif (MONO_LIB_DIR)
        endif (NOT GAC_DIR)
    endif (PKG_CONFIG_FOUND)

    if (NOT GAC_DIR)
        set (GAC_DIR "/usr/lib/mono")
        message(STATUS "Could not find cecil or mono. Using default GAC dir: ${GAC_DIR}")
    endif (NOT GAC_DIR)
endif (CMAKE_CSharp_COMPILER AND NOT GAC_DIR AND MONO_FOUND)

# Create a cache entry so the user can modify this.
set(GAC_DIR "${GAC_DIR}" CACHE PATH "Location of the GAC")
message(STATUS "Using GAC dir: ${GAC_DIR}")

mark_as_advanced(CMAKE_CSharp_COMPILER)

if (CMAKE_CSharp_COMPILER)
    set (CMAKE_CSharp_COMPILER_LOADED 1)
endif (CMAKE_CSharp_COMPILER)

# configure variables set in this file for fast reload later on
if(NOT CMAKE_PLATFORM_INFO_DIR) # pre-2.8.10
  set(CMAKE_PLATFORM_INFO_DIR ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY})
endif()

configure_file(${CMAKE_SOURCE_DIR}/cmake/CMakeCSharpCompiler.cmake.in 
  ${CMAKE_PLATFORM_INFO_DIR}/CMakeCSharpCompiler.cmake IMMEDIATE @ONLY)
set(CMAKE_CSharp_COMPILER_ENV_VAR "CSC")