summaryrefslogtreecommitdiff
path: root/cmake/CMakeCSharpInformation.cmake
blob: 2331ccf9cd8379817be11f89641101c3c0ff391b (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
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
# 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. 

# This file adds support for the C# language to cmake.
#
# It adds the following functions:
#
# csharp_add_executable (<name> <source files> [UNSAFE] [WINEXE] [REFERENCES <references>]
#                        [COMPILE_FLAGS <flags to be passed to the compiler>]
#                        [COMPILE_DEFINITIONS <additional definitions>] )
#
# csharp_add_library (<name> <source files> [UNSAFE] [REFERENCES <references>]
#                     [COMPILE_FLAGS <flags to be passed to the compiler>]
#                     [COMPILE_DEFINITIONS <additional definitions>] )
#
# install_assembly (<target name> [NO_GAC] DESTINATION <assembly destination directory>
#                   [PACKAGE <package name>] )
# The assembly destination directory is only used if we compile with Visual C# and thus can't use gacutil.
# If a package is specified and a file called <target>.pc.cmake exists in the current source directory,
# this function will configure the template file. All occurences of @assembly@ will be replaced with
# the path to the assembly. The resulting <target>.pc file will be installed to
# <CMAKE_INSTALL_PREFIX>/lib/pkgconfig/ . If you want to have a different basename for the template file,
# set the 'pkg-config_template_basename' property of the target with set_property.
#
# Example:
# ------------------------------
# cmake code:
# ------------------------------
# csharp_add_library(foo foo.cs)
# install_assembly(foo DESTINATION lib)
#
# ------------------------------
# contents of foo.pc.cmake file:
# ------------------------------
# Name: Foo
# Description: Foo library
# Version: 1.0
# Libs: -r:@assembly@

# ----- support macros -----
macro(GET_LIBRARY_OUTPUT_DIR var)
    if (NOT LIBRARY_OUTPUT_PATH)
        set(${var} ${CMAKE_CURRENT_BINARY_DIR})
    else (NOT LIBRARY_OUTPUT_PATH)
        set(${var} ${LIBRARY_OUTPUT_PATH})
    endif (NOT LIBRARY_OUTPUT_PATH)
endmacro(GET_LIBRARY_OUTPUT_DIR)

macro(GET_EXECUTABLE_OUTPUT_DIR var)
    if (NOT EXECUTABLE_OUTPUT_PATH)
        set(${var} ${CMAKE_CURRENT_BINARY_DIR})
    else (NOT EXECUTABLE_OUTPUT_PATH)
        set(${var} ${EXECUTABLE_OUTPUT_PATH})
    endif (NOT EXECUTABLE_OUTPUT_PATH)
endmacro(GET_EXECUTABLE_OUTPUT_DIR)

# This does just not always work... why?!
# macro(MAKE_PROPER_FILE_LIST var)
#     foreach(file ${ARGN})
#         if (IS_ABSOLUTE "${file}")
#             file(GLOB globbed "${file}")
#         else (IS_ABSOLUTE "${file}")
#             file(GLOB globbed "${CMAKE_CURRENT_SOURCE_DIR}/${file}")
#         endif (IS_ABSOLUTE "${file}")
#         
#         foreach (glob ${globbed})
#             file(TO_NATIVE_PATH "${glob}" native)
#             list(APPEND proper_file_list "${native}")
#         endforeach (glob ${globbed})
#     endforeach(file ${ARGN})
# endmacro(MAKE_PROPER_FILE_LIST)

# ----- actual functions -----

# ----- add an executable -----
function(csharp_add_executable target)
    set(current "s")
    set(dotnet_target "exe")

    foreach (arg ${ARGN})
        file(TO_NATIVE_PATH ${arg} native_path)

        if (arg STREQUAL "UNSAFE")
            set (unsafe "/unsafe")
        elseif (arg STREQUAL "WINEXE")
            set (dotnet_target "winexe")
        elseif (arg STREQUAL "REFERENCES")
            set (current "r")
        elseif (arg STREQUAL "COMPILE_FLAGS")
            set (current "flags")
        elseif (arg STREQUAL "COMPILE_DEFINITIONS")
            set (current "defs")
        else (arg STREQUAL "UNSAFE")
            if (current STREQUAL "s")
                # source file
                list(APPEND sources ${native_path})
            elseif (current STREQUAL "r")
                # reference
                if (TARGET ${arg})
                    # this is an existing target - get the target assembly
                    get_property(prop TARGET ${arg} PROPERTY _assembly)
                    list(APPEND references "/r:${prop}")
                    list(APPEND deps ${arg})
                else (TARGET ${arg})
                    # something different (e.g. assembly name in the gac)
                    list(APPEND references "/r:${native_path}")
                endif (TARGET ${arg})
            elseif (current STREQUAL "flags")
                list(APPEND _csc_opts "${arg}")
            elseif (current STREQUAL "defs")
                list(APPEND _csc_opts "/define:${arg}")
            endif (current STREQUAL "s")
        endif (arg STREQUAL "UNSAFE")
    endforeach (arg ${ARGN})

    if (CMAKE_BUILD_TYPE STREQUAL "Debug")
        list(APPEND _csc_opts "/define:DEBUG")
        list(APPEND _csc_opts "/debug")
    endif (CMAKE_BUILD_TYPE STREQUAL "Debug")

    get_executable_output_dir(outdir)
    if (NOT IS_ABSOLUTE "${outdir}")
        message(FATAL_ERROR "Directory \"${outdir}\" is not an absolute path!")
    endif (NOT IS_ABSOLUTE "${outdir}")

    file(RELATIVE_PATH relative_path "${CMAKE_BINARY_DIR}" "${outdir}/${target}.exe")
    file(TO_NATIVE_PATH "${outdir}/${target}" native_target)
    
    # inlined - this doesn't work as a macro :(
    foreach(file ${sources})
        file(TO_CMAKE_PATH "${file}" cmake_file)
        
        if (IS_ABSOLUTE "${cmake_file}")
            file(GLOB globbed "${cmake_file}")
        else (IS_ABSOLUTE "${cmake_file}")
            file(GLOB globbed "${CMAKE_CURRENT_SOURCE_DIR}/${cmake_file}")
        endif (IS_ABSOLUTE "${cmake_file}")
        
        foreach (glob ${globbed})
            file(TO_CMAKE_PATH "${glob}" cmake_path)
            list(APPEND cmake_file_list "${cmake_path}")
        endforeach (glob ${globbed})
        if (NOT globbed)
            list(APPEND cmake_file_list "${cmake_file}")
        endif (NOT globbed)
        list(APPEND compiler_file_list ${file})
    endforeach(file ${sources})

    get_directory_property(compile_definitions COMPILE_DEFINITIONS)
    foreach (def ${compile_definitions})
        # macros with values aren't supported by C#
        if (NOT def MATCHES ".*=.*")
            list(APPEND _csc_opts "/define:${def}")
        endif (NOT def MATCHES ".*=.*")
    endforeach (def ${compile_definitions})

    get_directory_property(link_dirs LINK_DIRECTORIES)
    foreach (dir ${link_dirs})
        list(APPEND _csc_opts "/lib:${dir}")
    endforeach (dir ${link_dirs})

    add_custom_command(OUTPUT "${outdir}/${target}.stubexe"
                       COMMAND "${CMAKE_COMMAND}" -E make_directory "${outdir}" # create the output dir
                       COMMAND "${CMAKE_CSharp_COMPILER}" /nologo /target:${dotnet_target} "/out:${native_target}.exe" # build the executable
                                                        ${_csc_opts} ${unsafe} ${references} ${compiler_file_list}
                       COMMAND "${CMAKE_COMMAND}" -E touch "${outdir}/${target}.stubexe" # create the stub so that DEPENDS will work
                       WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}" # working directory is the source directory, so we don't have to care about relative paths
                       DEPENDS ${cmake_file_list}
                       COMMENT "Building ${relative_path}" VERBATIM) # nice comment
    add_custom_target(${target} ALL DEPENDS "${outdir}/${target}.stubexe" SOURCES ${cmake_file_list}) # create the actual target
    set_property(TARGET ${target} PROPERTY _assembly "${native_target}.exe")
    set_property(TARGET ${target} PROPERTY _assembly_type "exe")
    if (deps)
        add_dependencies(${target} ${deps})
    endif(deps)
endfunction(csharp_add_executable)

# ----- add a library -----
function(csharp_add_library target)
    set(current "s")
    
    foreach (arg ${ARGN})
        file(TO_NATIVE_PATH ${arg} native_path)
        
        if (arg STREQUAL "UNSAFE")
            set (unsafe "/unsafe")
        elseif (arg STREQUAL "REFERENCES")
            set (current "r")
        elseif (arg STREQUAL "COMPILE_FLAGS")
            set (current "flags")
        elseif (arg STREQUAL "COMPILE_DEFINITIONS")
            set (current "defs")
        else (arg STREQUAL "UNSAFE")
            if (current STREQUAL "s")
                # source file
                list(APPEND sources ${native_path})
            elseif (current STREQUAL "r")
                # reference
                if (TARGET ${arg})
                    # this is an existing target - get the target assembly
                    get_property(prop TARGET ${arg} PROPERTY _assembly)
                    list(APPEND references "/r:${prop}")
                    list(APPEND deps ${arg})
                else (TARGET ${arg})
                    # something different (e.g. assembly name in the gac)
                    list(APPEND references "/r:${native_path}")
                endif (TARGET ${arg})
            elseif (current STREQUAL "flags")
                list(APPEND _csc_opts "${arg}")
            elseif (current STREQUAL "defs")
                list(APPEND _csc_opts "/define:${arg}")
            endif (current STREQUAL "s")
        endif (arg STREQUAL "UNSAFE")
    endforeach (arg ${ARGN})

    if (CMAKE_BUILD_TYPE STREQUAL "Debug")
        list(APPEND _csc_opts "/define:DEBUG")
        list(APPEND _csc_opts "/debug")
    endif (CMAKE_BUILD_TYPE STREQUAL "Debug")

    get_library_output_dir(outdir)
    if (NOT IS_ABSOLUTE "${outdir}")
        message(FATAL_ERROR "Directory \"${outdir}\" is not an absolute path!")
    endif (NOT IS_ABSOLUTE "${outdir}")

    file(RELATIVE_PATH relative_path "${CMAKE_BINARY_DIR}" "${outdir}/${target}.dll")
    file(TO_NATIVE_PATH "${outdir}/${target}" native_target)
    
    # inlined - this doesn't work as a macro :(
    foreach(file ${sources})
        file(TO_CMAKE_PATH "${file}" cmake_file)
        
        if (IS_ABSOLUTE "${cmake_file}")
            file(GLOB globbed "${cmake_file}")
        else (IS_ABSOLUTE "${cmake_file}")
            file(GLOB globbed "${CMAKE_CURRENT_SOURCE_DIR}/${cmake_file}")
        endif (IS_ABSOLUTE "${cmake_file}")
        
        foreach (glob ${globbed})
            file(TO_CMAKE_PATH "${glob}" cmake_path)
            list(APPEND cmake_file_list "${cmake_path}")
        endforeach (glob ${globbed})
        if (NOT globbed)
            list(APPEND cmake_file_list "${cmake_file}")
        endif (NOT globbed)
        list(APPEND compiler_file_list ${file})
    endforeach(file ${sources})

#     message("CMake File List for target ${target}: ${cmake_file_list}")

    get_directory_property(compile_definitions COMPILE_DEFINITIONS)
    foreach (def ${compile_definitions})
        # macros with values aren't supported by C#
        if (NOT def MATCHES ".*=.*")
            list(APPEND _csc_opts "/define:${def}")
        endif (NOT def MATCHES ".*=.*")
    endforeach (def ${compile_definitions})

    get_directory_property(link_dirs LINK_DIRECTORIES)
    foreach (dir ${link_dirs})
        list(APPEND _csc_opts "/lib:${dir}")
    endforeach (dir ${link_dirs})

    add_custom_command(OUTPUT "${outdir}/${target}.dll"
                       COMMAND "${CMAKE_COMMAND}" -E make_directory "${outdir}" # create the output dir
                       COMMAND "${CMAKE_CSharp_COMPILER}" /nologo /target:library "/out:${native_target}.dll" # build the library
                                                        ${_csc_opts} ${unsafe} ${references} ${compiler_file_list}
                       WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}" # working directory is the source directory, so we don't have to care about relative paths
                       DEPENDS ${cmake_file_list}
                       COMMENT "Building ${relative_path}" VERBATIM) # nice comment
    add_custom_target(${target} ALL DEPENDS "${outdir}/${target}.dll" SOURCES ${cmake_file_list}) # create the actual target
    set_property(TARGET ${target} PROPERTY _assembly "${native_target}.dll")
    set_property(TARGET ${target} PROPERTY _assembly_type "dll")
    if (deps)
        add_dependencies(${target} ${deps})
    endif(deps)
endfunction(csharp_add_library)

# ----- install an assembly -----
function(install_assembly)
    set (current "t")

    foreach (arg ${ARGN})
        # flag handling
        if (arg STREQUAL "NO_GAC")
            set(no_gac TRUE)
        # option handling
        elseif (arg STREQUAL DESTINATION)
            set (current "d")
        elseif (arg STREQUAL "PACKAGE")
            set (current "p")
        # value handling
        elseif (current STREQUAL "t")
            set (target ${arg})
        elseif (current STREQUAL "d")
            if (IS_ABSOLUTE "${arg}")
                set (destination_dir "${arg}")
            else (IS_ABSOLUTE "${arg}")
                set (destination_dir "${CMAKE_INSTALL_PREFIX}/${arg}")
            endif (IS_ABSOLUTE "${arg}")
        elseif (current STREQUAL "p")
            set (package ${arg})
        endif (arg STREQUAL "NO_GAC")
    endforeach (arg)

    if (NOT destination_dir)
        message(FATAL_ERROR "The destination directory is mandatory, even if the assembly is installed into the GAC.")
    elseif (NOT target)
        message(FATAL_ERROR "No target given.")
    endif (NOT destination_dir)

    # retrieve the absolute path of the generated assembly
    get_property(filename TARGET ${target} PROPERTY _assembly)
    get_property(type TARGET ${target} PROPERTY _assembly_type)
    get_property(pc_file TARGET ${target} PROPERTY pkg-config_template_basename)

    if (NOT pc_file)
        set (pc_file ${target})
    endif (NOT pc_file)

    # default assembly location (for pkg-config)
    set(assembly "${GAC_DIR}/${package}/${target}.dll")

    if (NOT filename)
        message(FATAL_ERROR "Couldn't retrieve the assembly filename for target ${target}! Are you sure the target is a .NET library assembly?")
    endif (NOT filename)

    if (package)
        set (package_option "-package ${package}")
    endif (package)

    if (NOT MONO_FOUND OR no_gac OR type STREQUAL "exe")
        install(FILES "${filename}" DESTINATION ${destination_dir})
        if (EXISTS "${filename}.config")
            install(FILES "${filename}.config" DESTINATION ${destination_dir})
        endif (EXISTS "${filename}.config")

        # don't install anything into the GAC
        set (no_gac TRUE)
        # set new assembly location for pkg-config
        set(assembly "${destination_dir}/${target}.${type}")
    endif (NOT MONO_FOUND OR no_gac OR type STREQUAL "exe")

    if (EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/${pc_file}.pc.cmake")
        configure_file ("${CMAKE_CURRENT_SOURCE_DIR}/${pc_file}.pc.cmake" "${CMAKE_CURRENT_BINARY_DIR}/${pc_file}.pc" @ONLY)

        if (NOT LIB_INSTALL_DIR)
            set (LIB_INSTALL_DIR ${CMAKE_INSTALL_PREFIX}/lib)
        endif (NOT LIB_INSTALL_DIR)
        install(FILES "${CMAKE_CURRENT_BINARY_DIR}/${pc_file}.pc" DESTINATION ${LIB_INSTALL_DIR}/pkgconfig)
    endif (EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/${pc_file}.pc.cmake")

    if (no_gac)
        return()
    endif (no_gac)

    # So we have the mono runtime and we can use gacutil (it has the -root option, which the MS version doesn't have).
    install(CODE "execute_process(COMMAND ${GACUTIL_EXECUTABLE} -i ${filename} ${package_option} -root ${CMAKE_CURRENT_BINARY_DIR}/tmp_gac)")
    file(REMOVE_RECURSE ${CMAKE_CURRENT_BINARY_DIR}/tmp_gac/mono)
    file(MAKE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/tmp_gac/mono)
    install(DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/tmp_gac/mono/ DESTINATION ${GAC_DIR} )
endfunction(install_assembly)

set(CMAKE_CSharp_INFORMATION_LOADED 1)