summaryrefslogtreecommitdiffstats
path: root/cmake
diff options
context:
space:
mode:
Diffstat (limited to 'cmake')
-rw-r--r--cmake/dotnet.cmake105
1 files changed, 105 insertions, 0 deletions
diff --git a/cmake/dotnet.cmake b/cmake/dotnet.cmake
new file mode 100644
index 0000000..175187b
--- /dev/null
+++ b/cmake/dotnet.cmake
@@ -0,0 +1,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()