blob: 175187b73703de9b165588bd0736cc0439770a36 (
plain) (
blame)
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
103
104
105
|
function(add_dotnet_library NAME)
set(multiValueArgs SOURCES REFERENCES)
cmake_parse_arguments(PARSE_ARGV 1 arg
"" "" "${multiValueArgs}"
)
if(NOT arg_SOURCES)
message(FATAL_ERROR "add_dotnet_library: SOURCES is required")
endif()
set(OUTPUT_FILE ${CMAKE_BINARY_DIR}/${NAME}.dll)
set(CSC_FLAGS -target:library)
foreach(ref IN LISTS arg_REFERENCES)
list(APPEND CSC_FLAGS "-reference:${ref}")
endforeach()
add_custom_command(
OUTPUT ${OUTPUT_FILE}
COMMAND dotnet ${CSC_DLL}
${CSC_FLAGS}
-out:${OUTPUT_FILE}
-lib:${RUNTIME_DIR}
-reference:System.Private.CoreLib.dll
-reference:System.Runtime.dll
-reference:System.Console.dll
${arg_SOURCES}
DEPENDS ${arg_SOURCES}
COMMENT "Compiling ${NAME} library with Roslyn csc..."
)
add_custom_target(${NAME} ALL DEPENDS ${OUTPUT_FILE})
endfunction()
function(add_dotnet_executable NAME)
set(oneValueArgs MAIN)
set(multiValueArgs SOURCES REFERENCES)
cmake_parse_arguments(PARSE_ARGV 1 arg
"" "${oneValueArgs}" "${multiValueArgs}"
)
if(NOT arg_SOURCES)
message(FATAL_ERROR "add_dotnet_executable: SOURCES is required")
endif()
if(NOT arg_MAIN)
set(arg_MAIN "Main")
endif()
set(OUTPUT_FILE ${CMAKE_BINARY_DIR}/${NAME}.dll)
set(CSC_FLAGS -target:exe -main:${arg_MAIN})
foreach(ref IN LISTS arg_REFERENCES)
list(APPEND CSC_FLAGS "-reference:${ref}")
endforeach()
add_custom_command(
OUTPUT ${OUTPUT_FILE}
COMMAND dotnet ${CSC_DLL}
${CSC_FLAGS}
$<TARGET_PROPERTY:${NAME},DOTNET_REFERENCES>
-out:${OUTPUT_FILE}
-lib:${RUNTIME_DIR}
-reference:System.Private.CoreLib.dll
-reference:System.Runtime.dll
-reference:System.Console.dll
${arg_SOURCES}
COMMAND ${CMAKE_COMMAND} -E copy_if_different
${CMAKE_SOURCE_DIR}/HelloCMake.runtimeconfig.json
${CMAKE_BINARY_DIR}/${NAME}.runtimeconfig.json
DEPENDS ${arg_SOURCES} $<TARGET_PROPERTY:${NAME},DOTNET_DEPENDS>
COMMENT "Compiling ${NAME} with Roslyn csc..."
)
add_custom_target(${NAME} ALL DEPENDS ${OUTPUT_FILE})
endfunction()
function(target_link_dotnet_libraries TARGET)
set(multiValueArgs LIBRARIES)
cmake_parse_arguments(PARSE_ARGV 1 arg
"" "" "${multiValueArgs}"
)
if(NOT arg_LIBRARIES)
return()
endif()
set(REFS "")
set(DEPS "")
foreach(lib IN LISTS arg_LIBRARIES)
list(APPEND REFS "-reference:${CMAKE_BINARY_DIR}/${lib}.dll")
list(APPEND DEPS "${CMAKE_BINARY_DIR}/${lib}.dll")
add_dependencies(${TARGET} ${lib})
endforeach()
set_target_properties(${TARGET} PROPERTIES
DOTNET_REFERENCES "${REFS}"
DOTNET_DEPENDS "${DEPS}"
)
endfunction()
|