PageRenderTime 12ms CodeModel.GetById 2ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 0ms

/indra/cmake/CSharpMacros.cmake

https://bitbucket.org/lindenlab/viewer-beta/
CMake | 142 lines | 100 code | 19 blank | 23 comment | 0 complexity | 050735896456258dcff539459532161d MD5 | raw file
  1# - This is a support module for easy Mono/C# handling with CMake
  2# It defines the following macros:
  3#
  4# ADD_CS_LIBRARY (<target> <source>)
  5# ADD_CS_EXECUTABLE (<target> <source>)
  6# INSTALL_GAC (<target>)
  7#
  8# Note that the order of the arguments is important.
  9#
 10# You can optionally set the variable CS_FLAGS to tell the macros whether
 11# to pass additional flags to the compiler. This is particularly useful to
 12# set assembly references, unsafe code, etc... These flags are always reset
 13# after the target was added so you don't have to care about that.
 14#
 15# copyright (c) 2007 Arno Rehn arno@arnorehn.de
 16#
 17# Redistribution and use is allowed according to the terms of the GPL license.
 18
 19
 20# ----- support macros -----
 21MACRO(GET_CS_LIBRARY_TARGET_DIR)
 22        IF (NOT LIBRARY_OUTPUT_PATH)
 23                SET(CS_LIBRARY_TARGET_DIR ${CMAKE_CURRENT_BINARY_DIR})
 24        ELSE (NOT LIBRARY_OUTPUT_PATH)
 25                SET(CS_LIBRARY_TARGET_DIR ${LIBRARY_OUTPUT_PATH})
 26        ENDIF (NOT LIBRARY_OUTPUT_PATH)
 27ENDMACRO(GET_CS_LIBRARY_TARGET_DIR)
 28
 29MACRO(GET_CS_EXECUTABLE_TARGET_DIR)
 30        IF (NOT EXECUTABLE_OUTPUT_PATH)
 31                SET(CS_EXECUTABLE_TARGET_DIR ${CMAKE_CURRENT_BINARY_DIR})
 32        ELSE (NOT EXECUTABLE_OUTPUT_PATH)
 33                SET(CS_EXECUTABLE_TARGET_DIR ${EXECUTABLE_OUTPUT_PATH})
 34        ENDIF (NOT EXECUTABLE_OUTPUT_PATH)
 35ENDMACRO(GET_CS_EXECUTABLE_TARGET_DIR)
 36
 37MACRO(MAKE_PROPER_FILE_LIST)
 38        FOREACH(file ${ARGN})
 39                # first assume it's a relative path
 40                FILE(GLOB globbed ${CMAKE_CURRENT_SOURCE_DIR}/${file})
 41                IF(globbed)
 42                        FILE(TO_NATIVE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/${file} native)
 43                ELSE(globbed)
 44                        FILE(TO_NATIVE_PATH ${file} native)
 45                ENDIF(globbed)
 46                SET(proper_file_list ${proper_file_list} ${native})
 47                SET(native "")
 48        ENDFOREACH(file)
 49ENDMACRO(MAKE_PROPER_FILE_LIST)
 50# ----- end support macros -----
 51
 52MACRO(ADD_CS_LIBRARY target)
 53        GET_CS_LIBRARY_TARGET_DIR()
 54        
 55        SET(target_DLL "${CS_LIBRARY_TARGET_DIR}/${target}.dll")
 56        MAKE_PROPER_FILE_LIST(${ARGN})
 57        FILE(RELATIVE_PATH relative_path ${CMAKE_BINARY_DIR} ${target_DLL})
 58        
 59        SET(target_KEY "${CMAKE_CURRENT_SOURCE_DIR}/${target}.key")
 60        SET(target_CS_FLAGS "${CS_FLAGS}")
 61        IF(${target}_CS_FLAGS)
 62                LIST(APPEND target_CS_FLAGS ${${target}_CS_FLAGS})
 63        ENDIF(${target}_CS_FLAGS)
 64        IF(EXISTS ${target_KEY})
 65                LIST(APPEND target_CS_FLAGS -keyfile:${target_KEY})
 66        ENDIF(EXISTS ${target_KEY})
 67
 68        FOREACH(ref ${${target}_REFS})
 69                SET(ref_DLL ${CMAKE_CURRENT_BINARY_DIR}/${ref}.dll)
 70                IF(EXISTS ${ref_DLL})
 71                        LIST(APPEND target_CS_FLAGS -r:${ref_DLL})
 72                ELSE(EXISTS ${ref_DLL})
 73                        LIST(APPEND target_CS_FLAGS -r:${ref})
 74                ENDIF(EXISTS ${ref_DLL})
 75        ENDFOREACH(ref ${${target}_REFS})
 76
 77        ADD_CUSTOM_COMMAND (OUTPUT ${target_DLL}
 78                COMMAND ${MCS_EXECUTABLE} ${target_CS_FLAGS} -out:${target_DLL} -target:library ${proper_file_list}
 79                MAIN_DEPENDENCY ${proper_file_list}
 80                DEPENDS ${ARGN}
 81                COMMENT "Building ${relative_path}")
 82        ADD_CUSTOM_TARGET (${target} ALL DEPENDS ${target_DLL})
 83
 84        FOREACH(ref ${${target}_REFS})
 85                GET_TARGET_PROPERTY(is_target ${ref} TYPE)
 86                IF(is_target)
 87                        ADD_DEPENDENCIES(${target} ${ref})
 88                ENDIF(is_target)
 89        ENDFOREACH(ref ${${target}_REFS})
 90
 91        SET(relative_path "")
 92        SET(proper_file_list "")
 93ENDMACRO(ADD_CS_LIBRARY)
 94
 95MACRO(ADD_CS_EXECUTABLE target)
 96        GET_CS_EXECUTABLE_TARGET_DIR()
 97        
 98        # Seems like cmake doesn't like the ".exe" ending for custom commands.
 99        # If we call it ${target}.exe, 'make' will later complain about a missing rule.
100        # Create a fake target instead.
101        SET(target_EXE "${CS_EXECUTABLE_TARGET_DIR}/${target}.exe")
102        SET(target_TOUCH "${CS_EXECUTABLE_TARGET_DIR}/${target}.exe-built")
103        GET_DIRECTORY_PROPERTY(clean ADDITIONAL_MAKE_CLEAN_FILES)
104        LIST(APPEND clean ${target}.exe)
105        SET_DIRECTORY_PROPERTIES(PROPERTIES ADDITIONAL_MAKE_CLEAN_FILES "${clean}")
106        MAKE_PROPER_FILE_LIST(${ARGN})
107        FILE(RELATIVE_PATH relative_path ${CMAKE_BINARY_DIR} ${target_EXE})
108        SET(target_CS_FLAGS "${CS_FLAGS}")
109        
110        FOREACH(ref ${${target}_REFS})
111                SET(ref_DLL ${CMAKE_CURRENT_SOURCE_DIR}/${ref}.dll)
112                IF(EXISTS ${ref_DLL})
113                        LIST(APPEND target_CS_FLAGS -r:${ref_DLL})
114                ELSE(EXISTS ${ref_DLL})
115                        LIST(APPEND target_CS_FLAGS -r:${ref})
116                ENDIF(EXISTS ${ref_DLL})
117        ENDFOREACH(ref ${${target}_REFS})
118
119        ADD_CUSTOM_COMMAND (OUTPUT "${target_TOUCH}"
120                COMMAND ${MCS_EXECUTABLE} ${target_CS_FLAGS} -out:${target_EXE} ${proper_file_list}
121                COMMAND ${CMAKE_COMMAND} -E touch ${target_TOUCH}
122                MAIN_DEPENDENCY ${ARGN}
123                DEPENDS ${ARGN}
124                COMMENT "Building ${relative_path}")
125        ADD_CUSTOM_TARGET ("${target}" ALL DEPENDS "${target_TOUCH}")
126
127        FOREACH(ref ${${target}_REFS})
128                GET_TARGET_PROPERTY(is_target ${ref} TYPE)
129                IF(is_target)
130                        ADD_DEPENDENCIES(${target} ${ref})
131                ENDIF(is_target)
132        ENDFOREACH(ref ${${target}_REFS})
133
134        SET(relative_path "")
135        SET(proper_file_list "")
136ENDMACRO(ADD_CS_EXECUTABLE)
137
138MACRO(INSTALL_GAC target)
139        GET_CS_LIBRARY_TARGET_DIR()
140        
141        INSTALL(CODE "EXECUTE_PROCESS(COMMAND ${GACUTIL_EXECUTABLE} -i ${CS_LIBRARY_TARGET_DIR}/${target}.dll -package 2.0)")
142ENDMACRO(INSTALL_GAC target)