summaryrefslogtreecommitdiffstats
path: root/cmake/dotnet.cmake
blob: cd848e2ed1d9cbee33cc2c0ab177c41d0b1ff390 (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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
function(add_dotnet_library NAME)
    set(multiValueArgs SOURCES REFERENCES DEPENDS)

    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_RUNTIME_OUTPUT_DIRECTORY}/${NAME}.dll)

    set(CSC_FLAGS -target:library)
    foreach(ref IN LISTS arg_REFERENCES)
        list(APPEND CSC_FLAGS "-reference:${ref}")
    endforeach()

    set(DEPS "")
    foreach(dep IN LISTS arg_DEPENDS)
        list(APPEND DEPS ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/${dep}.dll)
    endforeach()

    add_custom_command(
        OUTPUT ${OUTPUT_FILE}
        COMMAND dotnet ${CSC_DLL}
            ${CSC_FLAGS}
            $<TARGET_PROPERTY:${NAME},DOTNET_ALL_REFS>
            -out:${OUTPUT_FILE}
            -lib:${RUNTIME_DIR}
            -reference:System.Private.CoreLib.dll
            -reference:System.Runtime.dll
            -reference:System.Console.dll
            ${arg_SOURCES}
        DEPENDS ${arg_SOURCES} ${DEPS}
        COMMENT "Compiling ${NAME} library with Roslyn csc..."
        COMMAND_EXPAND_LISTS
    )

    add_custom_target(${NAME} ALL DEPENDS ${OUTPUT_FILE})

    foreach(dep IN LISTS arg_DEPENDS)
        add_dependencies(${NAME} ${dep})
    endforeach()

    set_target_properties(${NAME} PROPERTIES
        DOTNET_ALL_REFS ""
        DOTNET_PUBLIC_REFS ""
    )
endfunction()


function(add_dotnet_executable NAME)
    set(oneValueArgs MAIN)
    set(multiValueArgs SOURCES REFERENCES DEPENDS)

    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_RUNTIME_OUTPUT_DIRECTORY}/${NAME}.dll)
    set(RUNTIMECONFIG ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/${NAME}.runtimeconfig.json)

    set(CSC_FLAGS -target:exe -main:${arg_MAIN})
    foreach(ref IN LISTS arg_REFERENCES)
        list(APPEND CSC_FLAGS "-reference:${ref}")
    endforeach()

    configure_file(
        ${CMAKE_SOURCE_DIR}/cmake/runtimeconfig.json.in
        ${RUNTIMECONFIG}
        COPYONLY
    )

    set(DEPS ${RUNTIMECONFIG})
    foreach(dep IN LISTS arg_DEPENDS)
        list(APPEND DEPS ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/${dep}.dll)
    endforeach()

    add_custom_command(
        OUTPUT ${OUTPUT_FILE}
        COMMAND dotnet ${CSC_DLL}
            ${CSC_FLAGS}
            $<TARGET_PROPERTY:${NAME},DOTNET_ALL_REFS>
            -out:${OUTPUT_FILE}
            -lib:${RUNTIME_DIR}
            -reference:System.Private.CoreLib.dll
            -reference:System.Runtime.dll
            -reference:System.Console.dll
            ${arg_SOURCES}
        DEPENDS ${arg_SOURCES} ${DEPS}
        COMMENT "Compiling ${NAME} with Roslyn csc..."
        COMMAND_EXPAND_LISTS
    )

    add_custom_target(${NAME} ALL DEPENDS ${OUTPUT_FILE})

    foreach(dep IN LISTS arg_DEPENDS)
        add_dependencies(${NAME} ${dep})
    endforeach()

    set_target_properties(${NAME} PROPERTIES
        DOTNET_ALL_REFS ""
        DOTNET_PUBLIC_REFS ""
    )
endfunction()


function(target_link_dotnet_libraries TARGET)
    set(multiValueArgs PUBLIC PRIVATE INTERFACE)

    cmake_parse_arguments(PARSE_ARGV 1 arg
        "" "" "${multiValueArgs}"
    )

    foreach(scope PUBLIC PRIVATE INTERFACE)
        set(libs "${arg_${scope}}")
        if(NOT libs)
            continue()
        endif()

        foreach(lib IN LISTS libs)
            add_dependencies(${TARGET} ${lib})

            get_target_property(LIB_PUB_REFS ${lib} DOTNET_PUBLIC_REFS)
            if(NOT LIB_PUB_REFS)
                set(LIB_PUB_REFS "")
            endif()

            get_target_property(CURRENT_ALL ${TARGET} DOTNET_ALL_REFS)
            if(NOT CURRENT_ALL)
                set(CURRENT_ALL "")
            endif()
            list(APPEND CURRENT_ALL "-reference:${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/${lib}.dll")
            list(APPEND CURRENT_ALL ${LIB_PUB_REFS})
            set_target_properties(${TARGET} PROPERTIES DOTNET_ALL_REFS "${CURRENT_ALL}")

            if("${scope}" STREQUAL "PUBLIC" OR "${scope}" STREQUAL "INTERFACE")
                get_target_property(CURRENT_PUB ${TARGET} DOTNET_PUBLIC_REFS)
                if(NOT CURRENT_PUB)
                    set(CURRENT_PUB "")
                endif()
                list(APPEND CURRENT_PUB "-reference:${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/${lib}.dll")
                list(APPEND CURRENT_PUB ${LIB_PUB_REFS})
                list(REMOVE_DUPLICATES CURRENT_PUB)
                set_target_properties(${TARGET} PROPERTIES DOTNET_PUBLIC_REFS "${CURRENT_PUB}")
            endif()
        endforeach()
    endforeach()
endfunction()