2
# A CMake Module for finding and using C# (.NET and Mono).
4
# The following global variables are assumed to exist:
5
# CSHARP_SOURCE_DIRECTORY - path to C# sources
6
# CSHARP_BINARY_DIRECTORY - path to place resultant C# binary files
8
# The following variables are set:
9
# CSHARP_TYPE - the type of the C# compiler (eg. ".NET" or "Mono")
10
# CSHARP_COMPILER - the path to the C# compiler executable (eg. "C:/Windows/Microsoft.NET/Framework/v4.0.30319/csc.exe")
11
# CSHARP_VERSION - the version number of the C# compiler (eg. "v4.0.30319")
13
# The following macros are defined:
14
# CSHARP_ADD_EXECUTABLE( name references [files] [output_dir] ) - Define C# executable with the given name
15
# CSHARP_ADD_LIBRARY( name references [files] [output_dir] ) - Define C# library with the given name
18
# CSHARP_ADD_EXECUTABLE( MyExecutable "" "Program.cs" )
19
# CSHARP_ADD_EXECUTABLE( MyExecutable "ref1.dll ref2.dll" "Program.cs File1.cs" )
20
# CSHARP_ADD_EXECUTABLE( MyExecutable "ref1.dll;ref2.dll" "Program.cs;File1.cs" )
22
# This file is based on the work of GDCM:
23
# http://gdcm.svn.sf.net/viewvc/gdcm/trunk/CMake/UseCSharp.cmake
24
# Copyright (c) 2006-2010 Mathieu Malaterre <mathieu.malaterre@gmail.com>
27
# TODO: ADD SUPPORT FOR LINK LIBRARIES
29
# Check something was found
30
if( NOT CSHARP_COMPILER )
31
message( WARNING "A C# compiler executable was not found on your system" )
32
endif( NOT CSHARP_COMPILER )
34
# Include type-based USE_FILE
35
if( CSHARP_TYPE MATCHES ".NET" )
36
include( ${DotNetFrameworkSdk_USE_FILE} )
37
elseif ( CSHARP_TYPE MATCHES "Mono" )
38
include( ${Mono_USE_FILE} )
39
endif ( CSHARP_TYPE MATCHES ".NET" )
41
macro( CSHARP_ADD_LIBRARY name )
42
CSHARP_ADD_PROJECT( "library" ${name} ${ARGN} )
43
endmacro( CSHARP_ADD_LIBRARY )
45
macro( CSHARP_ADD_EXECUTABLE name )
46
CSHARP_ADD_PROJECT( "exe" ${name} ${ARGN} )
47
endmacro( CSHARP_ADD_EXECUTABLE )
50
macro( CSHARP_ADD_PROJECT type name )
51
set( refs "/reference:System.dll" )
55
if( ${type} MATCHES "library" )
57
elseif( ${type} MATCHES "exe" )
59
endif( ${type} MATCHES "library" )
61
# Step through each argument
63
if( ${it} MATCHES "(.*)(dll)" )
64
# Argument is a dll, add reference
65
list( APPEND refs /reference:${it} )
67
# Argument is a source file
69
list( APPEND sources ${it} )
70
list( APPEND sources_dep ${it} )
71
elseif( EXISTS ${CSHARP_SOURCE_DIRECTORY}/${it} )
72
list( APPEND sources ${CSHARP_SOURCE_DIRECTORY}/${it} )
73
list( APPEND sources_dep ${CSHARP_SOURCE_DIRECTORY}/${it} )
74
elseif( ${it} MATCHES "[*]" )
75
# For dependencies, we need to expand wildcards
76
FILE( GLOB it_glob ${it} )
77
list( APPEND sources ${it} )
78
list( APPEND sources_dep ${it_glob} )
79
elseif( IS_ABSOLUTE ${it} )
80
list( APPEND sources ${it} )
81
list( APPEND sources_dep ${it} )
86
# Check we have at least one source
87
list( LENGTH sources_dep sources_length )
88
if ( ${sources_length} LESS 1 )
89
MESSAGE( SEND_ERROR "No C# sources were specified for ${type} ${name}" )
91
list( SORT sources_dep )
93
# Perform platform specific actions
95
string( REPLACE "/" "\\" sources ${sources} )
97
string( REPLACE "\\" "/" sources ${sources} )
100
# Add custom target and command
101
# MESSAGE( STATUS "Adding C# ${type} ${name}: '${CSHARP_COMPILER} /t:${type} /out:${name}.${output} /platform:${CSHARP_PLATFORM} ${CSHARP_SDK} ${refs} ${sources}'" )
103
COMMENT "Compiling C# ${type} ${name}: '${CSHARP_COMPILER} /t:${type} /out:${name}.${output} /platform:${CSHARP_PLATFORM} ${CSHARP_SDK} ${refs} ${sources}'"
104
OUTPUT ${CSHARP_BINARY_DIRECTORY}/${name}.${output}
105
COMMAND ${CSHARP_COMPILER}
106
ARGS /t:${type} /out:${name}.${output} /platform:${CSHARP_PLATFORM} ${CSHARP_SDK} ${refs} ${sources}
107
WORKING_DIRECTORY ${CSHARP_BINARY_DIRECTORY}
108
DEPENDS ${sources_dep}
112
DEPENDS ${CSHARP_BINARY_DIRECTORY}/${name}.${output}
113
SOURCES ${sources_dep}
115
endmacro( CSHARP_ADD_PROJECT )