-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Add scaffolding for C# projection of the SDK #40212
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: feature/wsl-for-apps
Are you sure you want to change the base?
Changes from all commits
91d3a71
655d39c
93df6cf
7557e2c
515b555
733ea21
1f67993
17f7b84
9f79a00
67eec8d
94140f9
774cd89
af256d8
f71105f
391ebb0
751fe22
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -70,4 +70,5 @@ directory.build.targets | |
| test-storage/ | ||
| *.vhdx | ||
| *.tar | ||
| *.etl | ||
| *.etl | ||
| *.lscache | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| function(configure_csharp_target TARGET) | ||
| set(TARGET_PLATFORM_MIN_VERSION "10.0.19041.0") | ||
| target_compile_options(${TARGET} PRIVATE "/langversion:latest" "/debug:full") | ||
| set_target_properties( | ||
| ${TARGET} PROPERTIES | ||
| VS_GLOBAL_TargetPlatformVersion "${CMAKE_VS_WINDOWS_TARGET_PLATFORM_VERSION}" | ||
| VS_GLOBAL_TargetPlatformMinVersion "${TARGET_PLATFORM_MIN_VERSION}" | ||
| VS_GLOBAL_WindowsSdkPackageVersion "${WINDOWS_SDK_DOTNET_VERSION}" | ||
| VS_GLOBAL_AppendRuntimeIdentifierToOutputPath false | ||
| VS_GLOBAL_GenerateAssemblyInfo false | ||
| VS_GLOBAL_TargetLatestRuntimePatch false | ||
| DOTNET_SDK "Microsoft.NET.Sdk" | ||
| DOTNET_TARGET_FRAMEWORK "net8.0-windows${CMAKE_VS_WINDOWS_TARGET_PLATFORM_VERSION}" | ||
| ) | ||
| endfunction() | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -81,4 +81,49 @@ function(add_idl target idl_files_with_proxy idl_files_no_proxy) | |
| DEPENDS ${TARGET_OUTPUTS} | ||
| SOURCES ${idl_files_with_proxy} ${idl_files_no_proxy}) | ||
|
|
||
| endfunction() | ||
| endfunction() | ||
|
|
||
| function(add_idl_winrt target idl_file) | ||
| set(OUTPUT_DIR ${CMAKE_CURRENT_BINARY_DIR}/${TARGET_PLATFORM}/${CMAKE_BUILD_TYPE}) | ||
| file(TO_NATIVE_PATH ${OUTPUT_DIR} OUTPUT_DIR) # midl is picky about path formats | ||
| file(MAKE_DIRECTORY ${OUTPUT_DIR}) | ||
|
florelis marked this conversation as resolved.
|
||
|
|
||
| set(IDL_DEFINITIONS "") | ||
|
|
||
| get_directory_property(IDL_DEFS COMPILE_DEFINITIONS ) | ||
| foreach(e ${IDL_DEFS}) | ||
| set(IDL_DEFINITIONS ${IDL_DEFINITIONS} /D${e}) | ||
| endforeach() | ||
|
|
||
| string(TOLOWER ${TARGET_PLATFORM} IDL_ENV) | ||
|
|
||
| cmake_host_system_information( | ||
| RESULT WINDOWS_SDK_DIR | ||
| QUERY WINDOWS_REGISTRY "HKLM/SOFTWARE/Microsoft/Windows Kits/Installed Roots" | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does this mean that developers will have to install additional devkits for this to build ?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is just querying where the Windows SDK is installed to, which I assume is already a requirement to build. |
||
| VALUE "KitsRoot10") | ||
| set(WINRT_METADATA_DIR "${WINDOWS_SDK_DIR}\\UnionMetadata\\${CMAKE_VS_WINDOWS_TARGET_PLATFORM_VERSION}") | ||
| set(WINRT_REFERENCE "${WINRT_METADATA_DIR}\\Windows.winmd") | ||
| set(WINRT_INCLUDE "${WINDOWS_SDK_DIR}\\Include\\${CMAKE_VS_WINDOWS_TARGET_PLATFORM_VERSION}\\winrt") | ||
|
|
||
| cmake_path(GET idl_file STEM IDL_NAME) | ||
|
|
||
| set(IDL_WINMD ${OUTPUT_DIR}/${IDL_NAME}.winmd) | ||
|
|
||
| add_custom_command( | ||
| OUTPUT ${IDL_WINMD} | ||
| COMMAND midl /nologo /nomidl /winrt /metadata_dir "${WINRT_METADATA_DIR}" /reference "${WINRT_REFERENCE}" /I "${WINRT_INCLUDE}" /env "${IDL_ENV}" /h nul /winmd ${IDL_WINMD} ${idl_file} ${IDL_DEFINITIONS} | ||
| COMMAND cppwinrt -input ${IDL_WINMD} -reference "${WINRT_REFERENCE}" -output ${OUTPUT_DIR} -comp ${OUTPUT_DIR}/implementation_base -optimize -pch precomp.h -prefix | ||
| WORKING_DIRECTORY ${CMAKE_CURRENT_LIST_DIR} | ||
|
florelis marked this conversation as resolved.
|
||
| DEPENDS ${idl_file} | ||
| MAIN_DEPENDENCY ${idl_file} | ||
| VERBATIM | ||
| ) | ||
|
|
||
| set_source_files_properties(${IDL_WINMD} PROPERTIES GENERATED TRUE) | ||
|
|
||
| add_custom_target(${target} | ||
| COMMAND ${CMAKE_COMMAND} -E touch ${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/${target} | ||
| DEPENDS ${IDL_WINMD} | ||
| SOURCES ${idl_file}) | ||
|
|
||
| endfunction() | ||
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -11,13 +11,22 @@ | |||||||||
| <language>en-us</language> | ||||||||||
| <license type="expression">MIT</license> | ||||||||||
| <readme>docs\README.MD</readme> | ||||||||||
| <dependencies> | ||||||||||
| <group targetFramework="${NUGET_TARGET_FRAMEWORK}" /> | ||||||||||
|
||||||||||
| <group targetFramework="${NUGET_TARGET_FRAMEWORK}" /> | |
| <group targetFramework="${NUGET_TARGET_FRAMEWORK}"> | |
| <dependency id="WinRT.Runtime" version="[2.2.0,3.0.0)" /> | |
| </group> |
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,10 @@ | ||
| <?xml version="1.0" encoding="utf-8"?> | ||
| <Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> | ||
| <PropertyGroup Condition="'$(WSLCSDK_Platform)' == ''"> | ||
| <WSLCSDK_Platform>$(Platform)</WSLCSDK_Platform> | ||
| <PropertyGroup> | ||
| <WslcPlatform Condition="'$(WslcPlatform)' == ''">$(Platform)</WslcPlatform> | ||
|
florelis marked this conversation as resolved.
|
||
| <_wslcIsInvalidPlatform Condition="'$(WslcPlatform)' != 'x64' and '$(WslcPlatform)' != 'arm64'">true</_wslcIsInvalidPlatform> | ||
| </PropertyGroup> | ||
|
Comment on lines
+3
to
6
|
||
|
|
||
| <ItemDefinitionGroup> | ||
| <ClCompile> | ||
| <AdditionalIncludeDirectories> | ||
|
|
@@ -16,11 +18,17 @@ | |
| %(AdditionalDependencies) | ||
| </AdditionalDependencies> | ||
| <AdditionalLibraryDirectories> | ||
| $(MSBuildThisFileDirectory)..\..\runtimes\win-$(Platform); | ||
| $(MSBuildThisFileDirectory)..\..\runtimes\win-$(WslcPlatform); | ||
| %(AdditionalLibraryDirectories) | ||
| </AdditionalLibraryDirectories> | ||
| </Link> | ||
| </ItemDefinitionGroup> | ||
|
|
||
| <Import Project="$(MSBuildThisFileDirectory)..\Microsoft.WSL.Containers.common.targets" /> | ||
| <ItemGroup> | ||
| <ReferenceCopyLocalPaths Include="$(MSBuildThisFileDirectory)..\..\runtimes\win-$(WslcPlatform)\native\wslcsdk.dll" /> | ||
| </ItemGroup> | ||
|
Comment on lines
+27
to
+29
|
||
|
|
||
| <Target Name="WslcValidatePlatform" BeforeTargets="PrepareForBuild" Condition="'$(_wslcIsInvalidPlatform)' == 'true'"> | ||
| <Error Text="wslcsdk.dll could not be copied because platform '$(WslcPlatform)' is not supported. Only x64 and arm64 platforms are supported. You can override the detected platform by setting the property WslcPlatform." /> | ||
| </Target> | ||
| </Project> | ||
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,14 +1,31 @@ | ||||||||||||||
| <?xml version="1.0" encoding="utf-8"?> | ||||||||||||||
| <Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> | ||||||||||||||
| <PropertyGroup Condition="'$(WSLCSDK_Platform)' == ''"> | ||||||||||||||
| <WSLCSDK_Platform Condition="$(RuntimeIdentifier.EndsWith('-x64'))">x64</WSLCSDK_Platform> | ||||||||||||||
| <WSLCSDK_Platform Condition="$(RuntimeIdentifier.EndsWith('-arm64'))">arm64</WSLCSDK_Platform> | ||||||||||||||
| </PropertyGroup> | ||||||||||||||
| <!-- If we have a RuntimeIdentifier, the DLL is referenced automatically. | ||||||||||||||
| If it is missing, we fall back to PlatformTarget to reference it manually. --> | ||||||||||||||
| <Choose> | ||||||||||||||
| <When Condition="'$(RuntimeIdentifier)' != ''"> | ||||||||||||||
| <PropertyGroup> | ||||||||||||||
| <_wslcPlatform Condition="$(RuntimeIdentifier.EndsWith('-x64'))">x64</_wslcPlatform> | ||||||||||||||
| <_wslcPlatform Condition="$(RuntimeIdentifier.EndsWith('-arm64'))">arm64</_wslcPlatform> | ||||||||||||||
| <_wslcInvalidPlatformProperty Condition="'$(_wslcPlatform)' == ''">RuntimeIdentifier</_wslcInvalidPlatformProperty> | ||||||||||||||
| <_wslcInvalidPlatform Condition="'$(_wslcPlatform)' == ''">$(RuntimeIdentifier)</_wslcInvalidPlatform> | ||||||||||||||
| </PropertyGroup> | ||||||||||||||
| </When> | ||||||||||||||
| <Otherwise> | ||||||||||||||
| <PropertyGroup> | ||||||||||||||
| <_wslcPlatform Condition="'$(PlatformTarget)' == 'x64'">x64</_wslcPlatform> | ||||||||||||||
| <_wslcPlatform Condition="'$(PlatformTarget)' == 'arm64'">arm64</_wslcPlatform> | ||||||||||||||
| <_wslcInvalidPlatformProperty Condition="'$(_wslcPlatform)' == ''">PlatformTarget</_wslcInvalidPlatformProperty> | ||||||||||||||
| <_wslcInvalidPlatform Condition="'$(_wslcPlatform)' == ''">$(PlatformTarget)</_wslcInvalidPlatform> | ||||||||||||||
| </PropertyGroup> | ||||||||||||||
|
|
||||||||||||||
| <PropertyGroup Condition="'$(WSLCSDK_Platform)' == ''"> | ||||||||||||||
| <WSLCSDK_Platform Condition="'$(Platform)' != 'AnyCPU'">$(Platform)</WSLCSDK_Platform> | ||||||||||||||
| </PropertyGroup> | ||||||||||||||
|
|
||||||||||||||
| <Import Project="$(MSBuildThisFileDirectory)..\Microsoft.WSL.Containers.common.targets" /> | ||||||||||||||
| <ItemGroup Condition="'$(_wslcPlatform)' != ''"> | ||||||||||||||
|
||||||||||||||
| <ItemGroup Condition="'$(_wslcPlatform)' != ''"> | |
| <ItemGroup Condition="'$(_wslcPlatform)' != ''"> | |
| <Reference Include="wslcsdkcs"> | |
| <HintPath>$(MSBuildThisFileDirectory)..\..\runtimes\win-$(_wslcPlatform)\lib\$(TargetFramework)\wslcsdkcs.dll</HintPath> | |
| <Private>true</Private> | |
| </Reference> |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,22 @@ | ||||||||||||||||||||||||||||||
| /*++ | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| Copyright (c) Microsoft. All rights reserved. | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| Module Name: | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| Defaults.h | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| Abstract: | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| Contains default values for settings used in the WSL Container SDK. | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| --*/ | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| #pragma once | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| constexpr uint32_t s_DefaultCPUCount = 2; | ||||||||||||||||||||||||||||||
| constexpr uint32_t s_DefaultMemoryMB = 2000; | ||||||||||||||||||||||||||||||
| // Maximum value per use with HVSOCKET_CONNECT_TIMEOUT_MAX | ||||||||||||||||||||||||||||||
| constexpr ULONG s_DefaultBootTimeout = 300000; | ||||||||||||||||||||||||||||||
| // Default to 1 GB | ||||||||||||||||||||||||||||||
| constexpr UINT64 s_DefaultStorageSize = 1000 * 1000 * 1000; | ||||||||||||||||||||||||||||||
|
Comment on lines
+17
to
+22
|
||||||||||||||||||||||||||||||
| constexpr uint32_t s_DefaultCPUCount = 2; | |
| constexpr uint32_t s_DefaultMemoryMB = 2000; | |
| // Maximum value per use with HVSOCKET_CONNECT_TIMEOUT_MAX | |
| constexpr ULONG s_DefaultBootTimeout = 300000; | |
| // Default to 1 GB | |
| constexpr UINT64 s_DefaultStorageSize = 1000 * 1000 * 1000; | |
| #include <cstdint> | |
| constexpr uint32_t s_DefaultCPUCount = 2; | |
| constexpr uint32_t s_DefaultMemoryMB = 2000; | |
| // Maximum value per use with HVSOCKET_CONNECT_TIMEOUT_MAX | |
| constexpr uint32_t s_DefaultBootTimeout = 300000; | |
| // Default to 1 GB | |
| constexpr uint64_t s_DefaultStorageSize = 1000 * 1000 * 1000; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| enable_language(CSharp) | ||
|
|
||
| add_library(wslcsdkcs SHARED) | ||
| configure_csharp_target(wslcsdkcs) | ||
|
|
||
| set(WSLCSDK_WINMD "${CMAKE_CURRENT_BINARY_DIR}/../winrt/${TARGET_PLATFORM}/${CMAKE_BUILD_TYPE}/wslcsdk.winmd") | ||
|
|
||
| target_sources(wslcsdkcs | ||
| PRIVATE | ||
| "${WSLCSDK_WINMD}" | ||
| "${CMAKE_CURRENT_SOURCE_DIR}/WinRTActivation.cs" | ||
| ) | ||
|
|
||
| set_source_files_properties( | ||
| "${WSLCSDK_WINMD}" | ||
| PROPERTIES | ||
| GENERATED TRUE | ||
| VS_TOOL_OVERRIDE "CsWinRTInputs" | ||
| ) | ||
|
|
||
| set_target_properties( | ||
| wslcsdkcs PROPERTIES | ||
| FOLDER windows | ||
| LINKER_LANGUAGE CSharp | ||
| VS_PACKAGE_REFERENCES "Microsoft.Windows.CsWinRT_${CSWINRT_VERSION}" | ||
| VS_GLOBAL_CsWinRTIncludes "Microsoft.WSL.Containers" | ||
| ) | ||
|
|
||
| add_dependencies(wslcsdkcs wslcsdkwinrt) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| // Copyright (C) Microsoft Corporation. All rights reserved. | ||
|
|
||
| using System; | ||
| using System.Runtime.CompilerServices; | ||
| using System.Runtime.InteropServices; | ||
| using WinRT; | ||
|
|
||
| namespace Microsoft.WSL.Containers; | ||
|
|
||
| internal static class WinRTActivation | ||
| { | ||
| [UnmanagedFunctionPointer(CallingConvention.StdCall)] | ||
| private delegate int DllGetActivationFactoryFn(IntPtr classId, out IntPtr factory); | ||
|
|
||
| private static DllGetActivationFactoryFn s_getDllFactory; | ||
|
|
||
| [ModuleInitializer] | ||
| internal static void Initialize() | ||
| { | ||
| s_getDllFactory = Marshal.GetDelegateForFunctionPointer<DllGetActivationFactoryFn>( | ||
| NativeLibrary.GetExport( | ||
| NativeLibrary.Load("wslcsdk.dll", typeof(WinRTActivation).Assembly, DllImportSearchPath.AssemblyDirectory), | ||
| "DllGetActivationFactory")); | ||
|
|
||
| var previousHandler = ActivationFactory.ActivationHandler; | ||
| ActivationFactory.ActivationHandler = (typeName, iid) => | ||
| typeName.StartsWith("Microsoft.WSL.Containers.", StringComparison.Ordinal) | ||
| ? GetActivationFactory(typeName, iid) | ||
| : previousHandler?.Invoke(typeName, iid) ?? IntPtr.Zero; | ||
| } | ||
|
|
||
| private static IntPtr GetActivationFactory(string typeName, Guid iid) | ||
| { | ||
| WindowsCreateString(typeName, (uint)typeName.Length, out var hstring); | ||
| try | ||
| { | ||
| if (s_getDllFactory(hstring, out var factory) < 0) | ||
| { | ||
| return IntPtr.Zero; | ||
| } | ||
|
|
||
| if (iid == IID_IActivationFactory) | ||
| { | ||
| return factory; | ||
| } | ||
|
|
||
| try | ||
| { | ||
| return Marshal.QueryInterface(factory, ref iid, out var queried) >= 0 ? queried : IntPtr.Zero; | ||
| } | ||
| finally | ||
| { | ||
| Marshal.Release(factory); | ||
| } | ||
| } | ||
| finally | ||
| { | ||
| WindowsDeleteString(hstring); | ||
| } | ||
|
Comment on lines
+34
to
+59
|
||
| } | ||
|
|
||
| private static readonly Guid IID_IActivationFactory = new(0x00000035, 0x0000, 0x0000, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x46); | ||
|
|
||
| [DllImport("combase.dll", CharSet = CharSet.Unicode)] | ||
| private static extern int WindowsCreateString(string sourceString, uint length, out IntPtr hstring); | ||
|
|
||
| [DllImport("combase.dll")] | ||
| private static extern int WindowsDeleteString(IntPtr hstring); | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.