Skip to content
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

[Rgen] Provide all the wiring to allow to emit code. #21460

Open
wants to merge 24 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
e5b75d2
[Rgen] Provide all the wiring to allow to emit code.
mandel-macaque Oct 16, 2024
1ac8974
Auto-format source code
Oct 16, 2024
89aef62
Fix makefile.
mandel-macaque Oct 16, 2024
f6f8811
Merge branch 'main' into dev/mandel/rgen-wiring
mandel-macaque Oct 16, 2024
c8ddb0a
Merge TryValidate and Emit since it makes the API cleaner.
mandel-macaque Oct 16, 2024
56c7ed2
enuable nullable first so that emitters can add extra namespaces.
mandel-macaque Oct 16, 2024
ae1b14a
Fix the sample projects.
mandel-macaque Oct 16, 2024
d4fb40e
Fix analyzer tests.
mandel-macaque Oct 16, 2024
ad83462
Auto-format source code
Oct 16, 2024
7f32f87
Apply suggestions from code review
mandel-macaque Oct 17, 2024
bca5fca
Fix tab usage, add misssing closing brace
mandel-macaque Oct 17, 2024
5f90c0e
Auto-format source code
Oct 17, 2024
d95ed58
Add AppendRaw implementation with fewer string allocations.
mandel-macaque Oct 17, 2024
2e3ff80
Provide Appends that take an interpolated string.
mandel-macaque Oct 17, 2024
7e96a68
Merge branch 'dev/mandel/rgen-wiring' of github.com:xamarin/xamarin-m…
mandel-macaque Oct 17, 2024
cc24fa9
Auto-format source code
Oct 17, 2024
4da5c5d
Use a better approach fro the new line in the append raw.
mandel-macaque Oct 17, 2024
721b29e
Auto-format source code
Oct 17, 2024
1eaadfd
Update src/rgen/Microsoft.Macios.Generator.Sample/Microsoft.Macios.Ge…
mandel-macaque Oct 17, 2024
b6924fb
Apply suggestions from code review
mandel-macaque Oct 17, 2024
a208851
Apply suggestions from code review
mandel-macaque Oct 18, 2024
c59f13c
Fix tabbeb string build that broke after applying comments from PR.
mandel-macaque Oct 18, 2024
1662432
Remove method that will be re-added in the following rgen pr.
mandel-macaque Oct 18, 2024
dbbdf51
Auto-format source code
Oct 18, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/Makefile.generator
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,6 @@ $(RSP_DIR)/dotnet/%-defines-dotnet.rsp: frameworks.sources Makefile.generator ge
$(Q) ./generate-defines.csharp [email protected] '$(filter-out $(DOTNET_REMOVED_$(shell echo $* | tr a-z A-Z)_FRAMEWORKS),$($(shell echo $* | tr a-z A-Z)_FRAMEWORKS))'
$(Q) mv [email protected] $@

$(DOTNET_BUILD_DIR)/Xamarin.Apple.BindingAttributes.dll: bgen/Attributes.cs Makefile.generator | $(DOTNET_BUILD_DIR)
$(Q_DOTNET_BUILD) $(SYSTEM_CSC) $(DOTNET_FLAGS) -out:$@ $<
$(DOTNET_BUILD_DIR)/Xamarin.Apple.BindingAttributes.dll: bgen/Attributes.cs bgen/PlatformName.cs Makefile.generator | $(DOTNET_BUILD_DIR)
$(Q_DOTNET_BUILD) $(SYSTEM_CSC) $(DOTNET_FLAGS) -out:$@ bgen/Attributes.cs bgen/PlatformName.cs

1 change: 1 addition & 0 deletions src/Makefile.rgenerator
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Roslyn code generator
ROSLYN_GENERATOR=$(DOTNET_BUILD_DIR)/common/rgen/Microsoft.Macios.Generator.dll
ROSLYN_GENERATOR_FILES := $(wildcard rgen/Microsoft.Macios.Generator/*.cs)
ROSLYN_GENERATOR_FILES += $(wildcard rgen/Microsoft.Macios.Generator/*/*.cs)

$(ROSLYN_GENERATOR): Makefile.rgenerator $(ROSLYN_GENERATOR_FILES)
$(Q_DOTNET_BUILD) $(DOTNET) publish rgen/Microsoft.Macios.Generator/Microsoft.Macios.Generator.csproj $(DOTNET_BUILD_VERBOSITY) /p:Configuration=Debug /p:IntermediateOutputPath=$(abspath $(DOTNET_BUILD_DIR)/IDE/obj/common/rgen)/ /p:OutputPath=$(abspath $(DOTNET_BUILD_DIR)/IDE/bin/common/rgen/)/
Expand Down
9 changes: 0 additions & 9 deletions src/bgen/Attributes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -883,15 +883,6 @@ public class NoMethodAttribute : Attribute {
}

#if NET
public enum PlatformName : byte {
None,
MacOSX,
iOS,
WatchOS,
TvOS,
MacCatalyst,
}

public enum AvailabilityKind {
Introduced,
Deprecated,
Expand Down
116 changes: 29 additions & 87 deletions src/bgen/Extensions/PlatformNameExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,132 +1,74 @@
using System;
using System.IO;
using ObjCRuntime;
using Xamarin.Utils;
using System.Diagnostics.CodeAnalysis;

public static class PlatformNameExtensions {

public static string GetApplicationClassName (this PlatformName currentPlatform)
public static bool TryGetApplicationClassName (this PlatformName currentPlatform, [NotNullWhen (true)] out string? className)
{
switch (currentPlatform) {
case PlatformName.iOS:
case PlatformName.WatchOS:
case PlatformName.TvOS:
case PlatformName.MacCatalyst:
return "UIApplication";
className = "UIApplication";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Question - would these ever have to be fully qualified?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is just a map, that will happen later in the code emitter. This class only provides helper methods that return the mapping.

return true;
case PlatformName.MacOSX:
return "NSApplication";
className = "NSApplication";
return true;
default:
throw new BindingException (1047, currentPlatform);
className = null;
return false;
}
}

public static int GetXamcoreVersion (this PlatformName currentPlatform)
{
#if NET
return 4;
#else
switch (currentPlatform) {
case PlatformName.MacOSX:
case PlatformName.iOS:
return 2;
case PlatformName.TvOS:
case PlatformName.WatchOS:
return 3;
default:
return 4;
}
#endif
}

public static string GetCoreImageMap (this PlatformName currentPlatform)
public static bool TryGetCoreImageMap (this PlatformName currentPlatform, [NotNullWhen (true)] out string? coreImageMap)
{
switch (currentPlatform) {
case PlatformName.iOS:
case PlatformName.WatchOS:
case PlatformName.TvOS:
case PlatformName.MacCatalyst:
return "CoreImage";
coreImageMap = "CoreImage";
return true;
case PlatformName.MacOSX:
return "Quartz";
coreImageMap = "Quartz";
return true;
default:
throw new BindingException (1047, currentPlatform);
coreImageMap = null;
return false;
}
}

public static string GetCoreServicesMap (this PlatformName currentPlatform)
public static bool TryGetCoreServicesMap (this PlatformName currentPlatform, [NotNullWhen (true)] out string? coreServicesMap)
{
switch (currentPlatform) {
case PlatformName.iOS:
case PlatformName.WatchOS:
case PlatformName.TvOS:
case PlatformName.MacCatalyst:
return "MobileCoreServices";
case PlatformName.MacOSX:
return "CoreServices";
default:
throw new BindingException (1047, currentPlatform);
}
}

public static string GetPDFKitMap (this PlatformName currentPlatform)
{
switch (currentPlatform) {
case PlatformName.iOS:
case PlatformName.MacCatalyst:
return "PDFKit";
case PlatformName.MacOSX:
return "Quartz";
default:
throw new BindingException (1047, currentPlatform);
}
}

public static ApplePlatform AsApplePlatform (this PlatformName platform)
{
switch (platform) {
case PlatformName.iOS:
return ApplePlatform.iOS;
case PlatformName.TvOS:
return ApplePlatform.TVOS;
case PlatformName.MacCatalyst:
return ApplePlatform.MacCatalyst;
coreServicesMap = "MobileCoreServices";
return true;
case PlatformName.MacOSX:
return ApplePlatform.MacOSX;
case PlatformName.WatchOS:
return ApplePlatform.WatchOS;
case PlatformName.None:
return ApplePlatform.None;
coreServicesMap = "CoreServices";
return true;
default:
throw new ArgumentOutOfRangeException (nameof (platform), platform, $"Unknown platform: {platform}");
coreServicesMap = null;
return false;
}
}

static string GetSdkRoot (this PlatformName currentPlatform)
public static bool TryGetPDFKitMap (this PlatformName currentPlatform, [NotNullWhen (true)] out string? pdfKitMap)
{
switch (currentPlatform) {
case PlatformName.iOS:
case PlatformName.WatchOS:
case PlatformName.TvOS:
case PlatformName.MacCatalyst:
var sdkRoot = Environment.GetEnvironmentVariable ("MD_MTOUCH_SDK_ROOT");
if (string.IsNullOrEmpty (sdkRoot))
sdkRoot = "/Library/Frameworks/Xamarin.iOS.framework/Versions/Current";
return sdkRoot;
pdfKitMap = "PDFKit";
return true;
case PlatformName.MacOSX:
var macSdkRoot = Environment.GetEnvironmentVariable ("XamarinMacFrameworkRoot");
if (string.IsNullOrEmpty (macSdkRoot))
macSdkRoot = "/Library/Frameworks/Xamarin.Mac.framework/Versions/Current";
return macSdkRoot;
pdfKitMap = "Quartz";
return true;
default:
throw new BindingException (1047, currentPlatform);
pdfKitMap = null;
return false;
}
}

public static string GetPath (this PlatformName currentPlatform, params string [] paths)
{
var fullPaths = new string [paths.Length + 1];
fullPaths [0] = currentPlatform.GetSdkRoot ();
Array.Copy (paths, 0, fullPaths, 1, paths.Length);
return Path.Combine (fullPaths);
}
}
102 changes: 102 additions & 0 deletions src/bgen/Extensions/PlatformNameExtensionsBgen.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
using System;
using System.IO;
using Xamarin.Utils;

public static class PlatformNameExtensionsBgen {

// wrapper that allows us to use the same code for rgen and bgen
public static string GetApplicationClassName (this PlatformName currentPlatform)
{
if (currentPlatform.TryGetApplicationClassName (out var applicationClassName))
return applicationClassName;
throw new BindingException (1047, currentPlatform);
}

public static string GetCoreImageMap (this PlatformName currentPlatform)
{
if (currentPlatform.TryGetCoreImageMap (out var coreImageMap))
return coreImageMap;
throw new BindingException (1047, currentPlatform);
}

public static string GetCoreServicesMap (this PlatformName currentPlatform)
{
if (currentPlatform.TryGetCoreServicesMap (out var coreServicesMap))
return coreServicesMap;
throw new BindingException (1047, currentPlatform);
}

public static string GetPDFKitMap (this PlatformName currentPlatform)
{
if (currentPlatform.TryGetPDFKitMap (out var pdfKitMap))
return pdfKitMap;
throw new BindingException (1047, currentPlatform);
}

public static int GetXamcoreVersion (this PlatformName currentPlatform)
{
#if NET
return 4;
#else
switch (currentPlatform) {
case PlatformName.MacOSX:
case PlatformName.iOS:
return 2;
case PlatformName.TvOS:
case PlatformName.WatchOS:
return 3;
default:
return 4;
}
#endif
}

public static ApplePlatform AsApplePlatform (this PlatformName platform)
{
switch (platform) {
case PlatformName.iOS:
return ApplePlatform.iOS;
case PlatformName.TvOS:
return ApplePlatform.TVOS;
case PlatformName.MacCatalyst:
return ApplePlatform.MacCatalyst;
case PlatformName.MacOSX:
return ApplePlatform.MacOSX;
case PlatformName.WatchOS:
return ApplePlatform.WatchOS;
case PlatformName.None:
return ApplePlatform.None;
default:
throw new ArgumentOutOfRangeException (nameof (platform), platform, $"Unknown platform: {platform}");
}
}

static string GetSdkRoot (this PlatformName currentPlatform)
{
switch (currentPlatform) {
case PlatformName.iOS:
case PlatformName.WatchOS:
case PlatformName.TvOS:
case PlatformName.MacCatalyst:
var sdkRoot = Environment.GetEnvironmentVariable ("MD_MTOUCH_SDK_ROOT");
if (string.IsNullOrEmpty (sdkRoot))
sdkRoot = "/Library/Frameworks/Xamarin.iOS.framework/Versions/Current";
return sdkRoot;
case PlatformName.MacOSX:
var macSdkRoot = Environment.GetEnvironmentVariable ("XamarinMacFrameworkRoot");
if (string.IsNullOrEmpty (macSdkRoot))
macSdkRoot = "/Library/Frameworks/Xamarin.Mac.framework/Versions/Current";
return macSdkRoot;
default:
throw new BindingException (1047, currentPlatform);
}
}

public static string GetPath (this PlatformName currentPlatform, params string [] paths)
{
var fullPaths = new string [paths.Length + 1];
fullPaths [0] = currentPlatform.GetSdkRoot ();
Array.Copy (paths, 0, fullPaths, 1, paths.Length);
return Path.Combine (fullPaths);
}
}
8 changes: 8 additions & 0 deletions src/bgen/PlatformName.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
public enum PlatformName : byte {
None,
MacOSX,
iOS,
WatchOS,
TvOS,
MacCatalyst,
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using ObjCBindings;

namespace Microsoft.Macios.Bindings.Analyzer.Sample;

// If you don't see warnings, build the Analyzers Project.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<PropertyGroup>
<TargetFramework>net$(BundledNETCoreAppTargetFrameworkVersion)</TargetFramework>
<Nullable>enable</Nullable>
<NoWarn>APL0003</NoWarn>
</PropertyGroup>

<ItemGroup>
Expand All @@ -11,9 +12,15 @@
</ItemGroup>

<ItemGroup>
<Compile Include="..\..\..\src\ObjCBindings\BindingTypeAttribute.cs" >
<Link>external\BindginTypeAttribute.cs</Link>
</Compile>
<Compile Include="..\..\..\src\bgen\Attributes.cs" >
<Link>external\Attributes.cs</Link>
</Compile>
<Compile Include="..\..\..\src\bgen\PlatformName.cs" >
<Link>external\PlatformName.cs</Link>
</Compile>
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,23 @@
<TargetFramework>net$(BundledNETCoreAppTargetFrameworkVersion)</TargetFramework>
<Nullable>enable</Nullable>
<RootNamespace>Microsoft.Macios.Generator.Sample</RootNamespace>
<NoWarn>APL0003</NoWarn>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\Microsoft.Macios.Generator\Microsoft.Macios.Generator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false"/>
</ItemGroup>

<ItemGroup>
<Compile Include="..\..\..\src\ObjCBindings\BindingTypeAttribute.cs" >
<Link>external\BindingTypeAttribute.cs</Link>
</Compile>
<Compile Include="..\..\..\src\bgen\Attributes.cs" >
<Link>external\Attributes.cs</Link>
</Compile>
<Compile Include="..\..\..\src\bgen\PlatformName.cs" >
<Link>external\PlatformName.cs</Link>
</Compile>
</ItemGroup>

</Project>
2 changes: 2 additions & 0 deletions src/rgen/Microsoft.Macios.Generator.Sample/SampleBinding.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using ObjCBindings;

namespace Microsoft.Macios.Generator.Sample;

// This code will not compile until you build the project with the Source Generators
Expand Down
9 changes: 9 additions & 0 deletions src/rgen/Microsoft.Macios.Generator/AttributesNames.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace Microsoft.Macios.Generator;

/// <summary>
/// Contains all the names of the attributes that are used by the binding generator.
/// </summary>
public static class AttributesNames {

public static readonly string BindingAttribute = "ObjCBindings.BindingTypeAttribute";
}
Loading