function(add_dotnet_library NAME) set(multiValueArgs SOURCES REFERENCES DEPENDS NATIVE_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() foreach(dep IN LISTS arg_NATIVE_DEPENDS) list(APPEND DEPS ${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/lib${dep}.so) 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} ${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() foreach(dep IN LISTS arg_NATIVE_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 NATIVE_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() foreach(dep IN LISTS arg_NATIVE_DEPENDS) list(APPEND DEPS ${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/lib${dep}.so) 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} ${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() foreach(dep IN LISTS arg_NATIVE_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()