Skip to content

Commit 90ec0c7

Browse files
authored
[dotnet] Add support for building binding projects (dotnet#9340).
* Ship bgen * Fix a few issues in the generator. * Modify some of the MSBuild logic to support binding projects. * Port bindings-test[2] to .NET.
2 parents 82c6b03 + 8814e8c commit 90ec0c7

22 files changed

+368
-9
lines changed

dotnet/DefaultCompilationIncludes.md

+7
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,11 @@ the platform-specific variables `EnableDefaultiOSItems=false`,
1717
All \*.plist files in the root directory are included by default (as `None`
1818
items).
1919

20+
## Binding projects
21+
22+
Default compilation includes is turned off for binding projects, because
23+
typically there are C# source files (ApiDefinition.cs, StructsAndEnums.cs,
24+
etc.) in the binding project directory which should be compiled as binding
25+
source code, and not as normal C# source code.
26+
2027
[1]: https://docs.microsoft.com/en-us/dotnet/core/tools/csproj#default-compilation-includes-in-net-core-projects

dotnet/targets/Xamarin.Shared.Sdk.DefaultItems.targets

+3
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313
<_EnableDefaultXamarinItems Condition=" '$(_PlatformName)' == 'tvOS' ">$(EnableDefaulttvOSItems)</_EnableDefaultXamarinItems>
1414
<_EnableDefaultXamarinItems Condition=" '$(_PlatformName)' == 'watchOS' ">$(EnableDefaultwatchOSItems)</_EnableDefaultXamarinItems>
1515
<_EnableDefaultXamarinItems Condition=" '$(_PlatformName)' == 'macOS' ">$(EnableDefaultmacOSItems)</_EnableDefaultXamarinItems>
16+
17+
<!-- Don't include default Compile items for binding projects, because that would pick up ApiDefinition.cs and StructsAndEnums.cs -->
18+
<EnableDefaultCompileItems Condition=" '$(IsBindingProject)' == 'true' ">false</EnableDefaultCompileItems>
1619
</PropertyGroup>
1720

1821
<!-- Default plist file inclusion -->

dotnet/targets/Xamarin.Shared.Sdk.targets

+26-3
Original file line numberDiff line numberDiff line change
@@ -185,17 +185,25 @@
185185

186186
<!-- Native code -->
187187

188-
<Target Name="_ComputeVariables" DependsOnTargets="_GenerateBundleName;ResolveRuntimePackAssets">
188+
<Target Name="_ComputeFrameworkVariables" DependsOnTargets="ResolveRuntimePackAssets">
189189
<ItemGroup>
190190
<!-- Look in the ResolvedFrameworkReference for our Microsoft.* package. This should only find a single package. -->
191191
<_XamarinFrameworkReference Include="@(ResolvedFrameworkReference)" Condition="'%(ResolvedFrameworkReference.Identity)' == 'Microsoft.$(_PlatformName)'" />
192192
</ItemGroup>
193193
<PropertyGroup>
194-
<_IntermediateNativeLibraryDir>$(IntermediateOutputPath)nativelibraries/</_IntermediateNativeLibraryDir>
195-
<_NativeExecutableName>$(_AppBundleName)</_NativeExecutableName>
196194
<_XamarinSdkRuntimePackDirectory>%(_XamarinFrameworkReference.RuntimePackPath)</_XamarinSdkRuntimePackDirectory>
195+
<_XamarinRefPackageDirectory>%(_XamarinFrameworkReference.TargetingPackPath)</_XamarinRefPackageDirectory>
197196
<_XamarinNativeLibraryDirectory>$(_XamarinSdkRuntimePackDirectory)/runtimes/$(RuntimeIdentifier)/native</_XamarinNativeLibraryDirectory>
198197
<_XamarinIncludeDirectory>$(_XamarinSdkRuntimePackDirectory)/runtimes/$(RuntimeIdentifier)/native</_XamarinIncludeDirectory>
198+
<_XamarinRefAssemblyDirectory>$(_XamarinRefPackageDirectory)/ref/net5.0/</_XamarinRefAssemblyDirectory>
199+
<_XamarinRefAssemblyPath>$(_XamarinRefAssemblyDirectory)$(_PlatformAssemblyName).dll</_XamarinRefAssemblyPath>
200+
</PropertyGroup>
201+
</Target>
202+
203+
<Target Name="_ComputeVariables" DependsOnTargets="_GenerateBundleName;_ComputeFrameworkVariables">
204+
<PropertyGroup>
205+
<_IntermediateNativeLibraryDir>$(IntermediateOutputPath)nativelibraries/</_IntermediateNativeLibraryDir>
206+
<_NativeExecutableName>$(_AppBundleName)</_NativeExecutableName>
199207

200208
<_LibMonoLinkMode Condition="'$(_LibMonoLinkMode)' == '' And '$(ComputedPlatform)' != 'iPhone'">dylib</_LibMonoLinkMode>
201209
<_LibMonoLinkMode Condition="'$(_LibMonoLinkMode)' == ''">static</_LibMonoLinkMode>
@@ -223,6 +231,21 @@
223231
</ItemGroup>
224232
</Target>
225233

234+
<PropertyGroup>
235+
<_GenerateBindingsDependsOn>
236+
_ComputeBindingVariables;
237+
$(_GenerateBindingsDependsOn);
238+
</_GenerateBindingsDependsOn>
239+
</PropertyGroup>
240+
241+
<Target Name="_ComputeBindingVariables" DependsOnTargets="_ComputeFrameworkVariables" Condition="'$(IsBindingProject)' == 'true'">
242+
<PropertyGroup>
243+
<BTouchToolPath>$(_XamarinSdkRootDirectory)/tools/bin</BTouchToolPath>
244+
<BaseLibDllPath>$(_XamarinRefAssemblyPath)</BaseLibDllPath>
245+
<_GeneratorAttributeAssembly>$(_XamarinSdkRootDirectory)/tools/lib/Xamarin.Apple.BindingAttributes.dll</_GeneratorAttributeAssembly>
246+
</PropertyGroup>
247+
</Target>
248+
226249
<Target Name="_CompileNativeExecutable"
227250
DependsOnTargets="_DetectSdkLocations;_ComputeTargetArchitectures;_GenerateBundleName;_ComputeVariables;_GetMinimumOSVersion"
228251
Inputs="@(_MainFile)"

msbuild/Xamarin.Mac.Tasks/Xamarin.Mac.ObjCBinding.CSharp.targets

+4-4
Original file line numberDiff line numberDiff line change
@@ -23,25 +23,25 @@ Copyright (C) 2014 Xamarin Inc. All rights reserved.
2323
<IsBindingProject>true</IsBindingProject>
2424
</PropertyGroup>
2525

26-
<!-- Due to IDE/template bugs, many bindings projects exist in the wild withtout correct TFI/TFV tags.
26+
<!-- Due to IDE/template bugs, many bindings projects exist in the wild without correct TFI/TFV tags.
2727
In addition, System is not supported, so treat System as Modern or Full, depending on TFV being set.
2828
Microsoft.CSharp.targets gives TargetFrameworkVersion / TargetFrameworkIdentifier default values, so we _must_ do this _before_
2929
import Microsoft.CSharp.targets. However, we can't do most of Xamarin.Mac.ObjCBinding.CSharp.props before it. -->
3030

3131
<Choose>
32-
<When Condition=" '$(UseXamMacFullFramework)' == '' And ( '$(TargetFrameworkVersion)' == 'v2.0' Or '$(TargetFrameworkVersion)' == '' )">
32+
<When Condition=" '$(UseXamMacFullFramework)' == '' And ( '$(TargetFrameworkVersion)' == 'v2.0' Or '$(TargetFrameworkVersion)' == '' ) And '$(_UsingXamarinSdk)' != 'true'">
3333
<PropertyGroup>
3434
<TargetFrameworkVersion>v2.0</TargetFrameworkVersion>
3535
<TargetFrameworkIdentifier>Xamarin.Mac</TargetFrameworkIdentifier>
3636
</PropertyGroup>
3737
</When>
38-
<Otherwise>
38+
<When Condition="'$(_UsingXamarinSdk)' != 'true'">
3939
<PropertyGroup>
4040
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
4141
<UseXamMacFullFramework>true</UseXamMacFullFramework>
4242
<TargetFrameworkIdentifier></TargetFrameworkIdentifier>
4343
</PropertyGroup>
44-
</Otherwise>
44+
</When>
4545
</Choose>
4646

4747
<PropertyGroup>

msbuild/Xamarin.MacDev.Tasks.Core/Tasks/BTouchTaskBase.cs

+4-1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ public abstract class BTouchTaskBase : XamarinToolTask {
3333
[Required]
3434
public ITaskItem[] ApiDefinitions { get; set; }
3535

36+
public string AttributeAssembly { get; set; }
37+
3638
public ITaskItem[] CoreSources { get; set; }
3739

3840
public string DefineConstants { get; set; }
@@ -93,11 +95,12 @@ protected override string GenerateCommandLineCommands ()
9395
cmd.AppendSwitchIfNotNull ("/baselib:", BaseLibDll);
9496
cmd.AppendSwitchIfNotNull ("/out:", OutputAssembly);
9597

98+
cmd.AppendSwitchIfNotNull ("/attributelib:", AttributeAssembly);
99+
96100
string dir;
97101
if (!string.IsNullOrEmpty (BaseLibDll)) {
98102
dir = Path.GetDirectoryName (BaseLibDll);
99103
cmd.AppendSwitchIfNotNull ("/lib:", dir);
100-
cmd.AppendSwitchIfNotNull ("/r:", Path.Combine (dir, "mscorlib.dll"));
101104
}
102105

103106
if (ProcessEnums)

msbuild/Xamarin.Shared/Xamarin.Shared.targets

+3
Original file line numberDiff line numberDiff line change
@@ -428,6 +428,8 @@ Copyright (C) 2018 Microsoft. All rights reserved.
428428
<BTouchEmitDebugInformation Condition="'$(Debug)' != ''">true</BTouchEmitDebugInformation>
429429
</PropertyGroup>
430430

431+
<MakeDir SessionId="$(BuildSessionId)" Condition="'$(IsMacEnabled)' == 'true'" Directories="$(GeneratedSourcesDir)" />
432+
431433
<BTouch
432434
Condition="'$(IsMacEnabled)' == 'true'"
433435
SessionId="$(BuildSessionId)"
@@ -436,6 +438,7 @@ Copyright (C) 2018 Microsoft. All rights reserved.
436438
AdditionalLibPaths="$(AdditionalLibPaths)"
437439
AllowUnsafeBlocks="$(AllowUnsafeBlocks)"
438440
ApiDefinitions="@(ObjcBindingApiDefinition)"
441+
AttributeAssembly="$(_GeneratorAttributeAssembly)"
439442
BaseLibDll="$(BaseLibDllPath)"
440443
CoreSources="@(ObjcBindingCoreSource)"
441444
DefineConstants="$(DefineConstants)"

src/Makefile.generator

+16
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,28 @@ $(DOTNET_BUILD_DIR)/bgen/bgen: $(generator_dependencies) Makefile.generator $(B
1717
$(Q_DOTNET_BUILD) $(DOTNET) build $(XBUILD_VERBOSITY) /p:Configuration=Debug bgen/bgen.csproj /p:IntermediateOutputPath=$(abspath $(DOTNET_BUILD_DIR)/IDE/obj/common)/ /p:OutputPath=$(abspath $(DOTNET_BUILD_DIR)/IDE/bin/common)/
1818
$(Q) $(CP) $(DOTNET_BUILD_DIR)/IDE/bin/common/bgen* $(dir $@)
1919

20+
$(DOTNET_DESTDIR)/%.Sdk/tools/lib/bgen/bgen: $(DOTNET_BUILD_DIR)/bgen/bgen | $(DOTNET_DESTDIR)/%.Sdk/tools/lib/bgen
21+
$(Q) rm -f $(dir $@)/bgen*
22+
$(Q) $(CP) $<* $(dir $@)
23+
24+
$(DOTNET_DESTDIR)/%.Sdk/tools/bin/bgen: bgen/bgen.dotnet | $(DOTNET_DESTDIR)/%.Sdk/tools/bin
25+
$(Q) $(CP) $< $@
26+
27+
$(DOTNET_DESTDIR)/%.Sdk/tools/lib/Xamarin.Apple.BindingAttributes.dll: $(DOTNET_BUILD_DIR)/Xamarin.Apple.BindingAttributes.dll | $(DOTNET_DESTDIR)/%.Sdk/tools/lib
28+
$(Q) $(CP) $< $@
29+
2030
DOTNET_TARGETS += \
2131
$(DOTNET_BUILD_DIR)/bgen/bgen \
32+
$(foreach platform,$(DOTNET_PLATFORMS),$(DOTNET_DESTDIR)/Microsoft.$(platform).Sdk/tools/bin/bgen) \
33+
$(foreach platform,$(DOTNET_PLATFORMS),$(DOTNET_DESTDIR)/Microsoft.$(platform).Sdk/tools/lib/bgen/bgen) \
34+
$(foreach platform,$(DOTNET_PLATFORMS),$(DOTNET_DESTDIR)/Microsoft.$(platform).Sdk/tools/lib/Xamarin.Apple.BindingAttributes.dll) \
2235

2336
DOTNET_TARGETS_DIRS += \
2437
$(DOTNET_BUILD_DIR) \
2538
$(DOTNET_BUILD_DIR)/bgen \
39+
$(foreach platform,$(DOTNET_PLATFORMS),$(DOTNET_DESTDIR)/Microsoft.$(platform).Sdk/tools/bin) \
40+
$(foreach platform,$(DOTNET_PLATFORMS),$(DOTNET_DESTDIR)/Microsoft.$(platform).Sdk/tools/lib) \
41+
$(foreach platform,$(DOTNET_PLATFORMS),$(DOTNET_DESTDIR)/Microsoft.$(platform).Sdk/tools/lib/bgen) \
2642

2743
#
2844
# Common

src/bgen/bgen.dotnet

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!/bin/bash -e
2+
3+
pushd "$(dirname "$0")/.." > /dev/null
4+
MONOTOUCH_PREFIX=$(pwd -P)
5+
popd > /dev/null
6+
7+
exec "$MONOTOUCH_PREFIX/lib/bgen/bgen" "$@"

src/btouch.cs

+8-1
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ public class BindingTouch {
5151
string attributedll;
5252

5353
List<string> libs = new List<string> ();
54+
List<string> references = new List<string> ();
5455

5556
public Universe universe;
5657
public TypeManager TypeManager = new TypeManager ();
@@ -169,6 +170,13 @@ string LocateAssembly (string name)
169170
return path;
170171
}
171172

173+
// Look in our references to see if we were explicity passed a path to the library we're looking for
174+
foreach (var reference in references) {
175+
var refname = Path.GetFileName (reference);
176+
if (refname == name || refname == name + ".dll")
177+
return reference;
178+
}
179+
172180
throw new FileNotFoundException ($"Could not find the assembly '{name}' in any of the directories: {string.Join (", ", GetLibraryDirectories ())}");
173181
}
174182

@@ -225,7 +233,6 @@ int Main3 (string [] args)
225233
List<string> sources;
226234
var resources = new List<string> ();
227235
var linkwith = new List<string> ();
228-
var references = new List<string> ();
229236
var api_sources = new List<string> ();
230237
var core_sources = new List<string> ();
231238
var extra_sources = new List<string> ();

tests/bindings-test/dotnet/.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
global.json
2+
NuGet.config
3+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project Sdk="Microsoft.iOS.Sdk">
3+
<PropertyGroup>
4+
<NativeLibName>ios-fat</NativeLibName>
5+
</PropertyGroup>
6+
<Import Project="..\shared.targets" />
7+
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project Sdk="Microsoft.macOS.Sdk">
3+
<PropertyGroup>
4+
<NativeLibName>macos</NativeLibName>
5+
</PropertyGroup>
6+
<Import Project="..\shared.targets" />
7+
</Project>
+86
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project>
3+
<PropertyGroup>
4+
<TargetFramework>net5.0</TargetFramework>
5+
<RootNamespace>bindingstest</RootNamespace>
6+
<SignAssembly>true</SignAssembly>
7+
<AssemblyOriginatorKeyFile>..\..\..\..\product.snk</AssemblyOriginatorKeyFile>
8+
<LangVersion>latest</LangVersion>
9+
<IsBindingProject>true</IsBindingProject>
10+
11+
<RootTestsDirectory>$(MSBuildThisFileDirectory)\..\..</RootTestsDirectory>
12+
<BindingsTestDirectory>$(RootTestsDirectory)\bindings-test</BindingsTestDirectory>
13+
<TestLibrariesDirectory>$(RootTestsDirectory)\test-libraries</TestLibrariesDirectory>
14+
</PropertyGroup>
15+
16+
<ItemGroup>
17+
<PackageReference Include="NUnitLite" Version="3.12.0" />
18+
</ItemGroup>
19+
20+
<ItemGroup>
21+
<ObjcBindingApiDefinition Include="$(BindingsTestDirectory)\ApiDefinition.cs" />
22+
<ObjcBindingApiDefinition Include="$(RootTestsDirectory)\generator\tests\ref-out-parameters.cs" />
23+
<ObjcBindingApiDefinition Include="$(BindingsTestDirectory)\ApiDefinition.generated.cs" />
24+
<ObjcBindingApiDefinition Include="$(BindingsTestDirectory)\ApiProtocol.cs" />
25+
</ItemGroup>
26+
<ItemGroup>
27+
<ObjcBindingCoreSource Include="$(BindingsTestDirectory)\StructsAndEnums.cs" />
28+
<ObjcBindingCoreSource Include="$(BindingsTestDirectory)\StructsAndEnums.generated.cs" />
29+
</ItemGroup>
30+
31+
<ItemGroup>
32+
<ObjcBindingNativeLibrary Include="$(TestLibrariesDirectory)\.libs\$(NativeLibName)\libtest.a">
33+
<Link>libtest.a</Link>
34+
</ObjcBindingNativeLibrary>
35+
</ItemGroup>
36+
37+
<ItemGroup>
38+
<Compile Include="$(RootTestsDirectory)\common\ConditionalCompilation.cs">
39+
<Link>ConditionalCompilation.cs</Link>
40+
</Compile>
41+
<Compile Include="$(BindingsTestDirectory)\libtest.linkwith.cs">
42+
<DependentUpon>libtest.a</DependentUpon>
43+
</Compile>
44+
<Compile Include="$(BindingsTestDirectory)\ProtocolTest.cs" />
45+
<Compile Include="$(RootTestsDirectory)\api-shared\ObjCRuntime\Registrar.cs">
46+
<Link>Registrar.cs</Link>
47+
</Compile>
48+
<Compile Include="..\..\RegistrarBindingTest.cs" />
49+
<Compile Include="$(RootTestsDirectory)\common\TestRuntime.cs">
50+
<Link>TestRuntime.cs</Link>
51+
</Compile>
52+
<Compile Include="$(BindingsTestDirectory)\RuntimeTest.cs" />
53+
<Compile Include="$(BindingsTestDirectory)\CodeBehind.cs" />
54+
<Compile Include="$(BindingsTestDirectory)\Messaging.cs" />
55+
</ItemGroup>
56+
<ItemGroup>
57+
<None Include="$(TestLibrariesDirectory)\libtest.m">
58+
<Link>libtest.m</Link>
59+
</None>
60+
<None Include="$(TestLibrariesDirectory)\libtest.h">
61+
<Link>libtest.h</Link>
62+
</None>
63+
<None Include="$(TestLibrariesDirectory)\libtest.structs.h">
64+
<Link>libtest.structs.h</Link>
65+
</None>
66+
<None Include="$(TestLibrariesDirectory)\libtest.properties.h">
67+
<Link>libtest.properties.h</Link>
68+
</None>
69+
<None Include="$(TestLibrariesDirectory)\testgenerator.cs">
70+
<Link>testgenerator.cs</Link>
71+
</None>
72+
</ItemGroup>
73+
<ItemGroup>
74+
<GeneratedTestInput Include="$(TestLibrariesDirectory)\*.m" />
75+
<GeneratedTestInput Include="$(TestLibrariesDirectory)\*.h" />
76+
<GeneratedTestInput Include="$(TestLibrariesDirectory)\*.cs" />
77+
<GeneratedTestInput Include="$(TestLibrariesDirectory)\Makefile" />
78+
<GeneratedTestOutput Include="$(TestLibrariesDirectory)\.libs\$(NativeLibName)\libtest.a" />
79+
<GeneratedTestOutput Include="$(BindingsTestDirectory)\ApiDefinition.generated.cs" />
80+
<GeneratedTestOutput Include="$(BindingsTestDirectory)\StructsAndEnums.generated.cs" />
81+
</ItemGroup>
82+
83+
<Target Name="BuildTestLibraries" Inputs="@(GeneratedTestInput)" Outputs="@(GeneratedTestOutput)" BeforeTargets="BeforeBuild">
84+
<Exec Command="make -j8 -C $(TestLibrariesDirectory)" />
85+
</Target>
86+
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project Sdk="Microsoft.tvOS.Sdk">
3+
<PropertyGroup>
4+
<NativeLibName>tvos-fat</NativeLibName>
5+
</PropertyGroup>
6+
<Import Project="..\shared.targets" />
7+
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project Sdk="Microsoft.watchOS.Sdk">
3+
<PropertyGroup>
4+
<NativeLibName>watchos-fat</NativeLibName>
5+
</PropertyGroup>
6+
<Import Project="..\shared.targets" />
7+
</Project>
+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
global.json
2+
NuGet.config
3+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project Sdk="Microsoft.iOS.Sdk">
3+
<PropertyGroup>
4+
<NativeLibName>ios-fat</NativeLibName>
5+
<PlatformName>iOS</PlatformName>
6+
</PropertyGroup>
7+
<Import Project="..\shared.targets" />
8+
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project Sdk="Microsoft.macOS.Sdk">
3+
<PropertyGroup>
4+
<NativeLibName>macos</NativeLibName>
5+
<PlatformName>macOS</PlatformName>
6+
</PropertyGroup>
7+
<Import Project="..\shared.targets" />
8+
</Project>

0 commit comments

Comments
 (0)