diff --git a/CHANGELOG.md b/CHANGELOG.md index 045e87fd08..4cadfc6a76 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -62,16 +62,17 @@ different versioning scheme, following the Haskell community's * **Breaking change** The code generation MSBuild targets no longer support Mono's xbuild: only MSBuild is supported. Mono has - [deprecated xbuild in favor MSBuild](http://www.mono-project.com/docs/about-mono/releases/5.0.0/#msbuild) + [deprecated xbuild in favor of MSBuild](http://www.mono-project.com/docs/about-mono/releases/5.0.0/#msbuild) now that [MSBuild is open source and cross-platform](https://github.com/Microsoft/msbuild). - These are the MSBuild targets that run `gbc` on `BondCodegen` items. * **Breaking change** The code generation MSBuild targets now automatically compile the generated `_grpc.cs` files if `--grpc` is passed to `gbc`. Explicit `` lines in MSBuild projects will need to be removed to fix error MSB3105 about duplicate items. See commit TBD for an example of how to fix this. [Issue #448](https://github.com/Microsoft/bond/issues/448) +* The code generation MSBuild targets will now skip compiling the + `_types.cs` files when `--structs=false` is passed to `gbc`. * Added `Bond.Box.Create` helper method to create `Bond.Box` instances. * Reflection.IsBonded now recognizes custom IBonded implementations. * Use Newtonsoft's JSON.NET BigInteger support -- when available -- to diff --git a/cs/build/nuget/Common.targets b/cs/build/nuget/Common.targets index ed482ef246..ead864e7eb 100644 --- a/cs/build/nuget/Common.targets +++ b/cs/build/nuget/Common.targets @@ -8,19 +8,20 @@ * $BOND_COMPILER_PATH : Path to directory containing gbc.exe * * User-Defines: -* @BondCodegen : A bond idl file (usually with a .bond extension) to compile +* @BondCodegen : A Bond IDL file (usually with a .bond extension) to compile * %Options : Any gbc options unique to this file (prefer $BondOptions than per-file) * @BondImportDirectory : Directory for other schemas imported within a .bond file * $BondOutputDirectory : Output directory for the generated files, by default IntermediateOutputPath * $BondOptions : Additional options to pass to the gbc generator * $BondCodegenMode : Code generation mode for gbc.exe to use (default c#) * +* These targets understand the structs/grpc switches that can be passed +* to gbc and adjust the files that are added to the Compile item as +* needed. --> - + $(IntermediateOutputPath) @@ -72,7 +73,7 @@ BondCodegen item. --> @@ -115,31 +116,50 @@ - <_BondGeneratedFiles Include="@(BondCodegen -> '$(BondOutputDirectory)\%(FileName)_types.cs')"> + <_BondGen_Structs_BondOptions + Include="@(BondCodegen -> '$(BondOutputDirectory)\%(FileName)_types.cs')" + Condition="'%(BondCodegen.Options)' == '' + AND !$(BondOptions.Contains('--structs=false'))"> true %(BondCodegen.Identity) - + - <_BondGeneratedFiles Include="@(_BondCodegenWithDefaultOptions -> '$(BondOutputDirectory)\%(FileName)_grpc.cs')" - Condition="$(BondOptions.Contains('--grpc')) AND !$(BondOptions.Contains('--grpc=false'))"> + <_BondGen_Structs_Options + Include="@(BondCodegen -> '$(BondOutputDirectory)\%(FileName)_types.cs')" + Condition="'%(BondCodegen.Options)' != '' + AND !$([System.String]::new('%(BondCodegen.Options)').Contains('--structs=false'))"> true %(BondCodegen.Identity) - + - <_BondGeneratedFiles Include="@(BondCodegen -> '$(BondOutputDirectory)\%(FileName)_grpc.cs')" - Condition="$([System.String]::new('%(BondCodegen.Options)').Contains('--grpc')) AND !$([System.String]::new('%(BondCodegen.Options)').Contains('--grpc=false'))"> + <_BondGen_Grpc_BondOptions + Include="@(_BondCodegenWithDefaultOptions -> '$(BondOutputDirectory)\%(FileName)_grpc.cs')" + Condition="$(BondOptions.Contains('--grpc')) + AND !$(BondOptions.Contains('--grpc=false'))"> true %(BondCodegen.Identity) - + - <_BondGeneratedFileNames Include="@(_BondGeneratedFiles)"/> + <_BondGen_Grpc_Options + Include="@(BondCodegen -> '$(BondOutputDirectory)\%(FileName)_grpc.cs')" + Condition="$([System.String]::new('%(BondCodegen.Options)').Contains('--grpc')) + AND !$([System.String]::new('%(BondCodegen.Options)').Contains('--grpc=false'))"> + true + %(BondCodegen.Identity) + + + <_BondGeneratedFileNames + Include="@(_BondGen_Structs_BondOptions); + @(_BondGen_Structs_Options); + @(_BondGen_Grpc_BondOptions); + @(_BondGen_Grpc_Options)" /> - - --grpc - + diff --git a/examples/cs/grpc/shared-types-assembly/README.md b/examples/cs/grpc/shared-types-assembly/README.md new file mode 100644 index 0000000000..892ac94a62 --- /dev/null +++ b/examples/cs/grpc/shared-types-assembly/README.md @@ -0,0 +1,19 @@ +# Using a shared types assembly + +Sometimes you want to compile the types from a .bond file separately from +the gRPC services and clients. This example shows how to split the types +into their own assembly. + +The [`gbc`](https://microsoft.github.io/bond/manual/compiler.html) options +`--structs` and `--grpc` can be used to control whether codegen is performed +for structs and services. + +In the `types` directory, codegen is performed with out the `--grpc` switch, +so just types are generated and compiled. + +In the `client` and `service` directories, however, codegen is performed +with the `--grpc` switch. Also, `--structs=false` is passed to disable the +default behavior of generating C# code for the types. If `--structs=false` +were not specified, there would be duplicate types in different assemblies, +resulting in a conflict that would need to be resolved via +[`extern alias`](https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/extern-alias). diff --git a/examples/cs/grpc/shared-types-assembly/client/StaClient.cs b/examples/cs/grpc/shared-types-assembly/client/StaClient.cs new file mode 100644 index 0000000000..11c99e45b1 --- /dev/null +++ b/examples/cs/grpc/shared-types-assembly/client/StaClient.cs @@ -0,0 +1,32 @@ +// Copyright (c) Microsoft. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +namespace Examples.SharedTypes +{ + using System; + using Grpc.Core; + + public static class Client + { + const string Address = "127.0.0.1"; + const int Port = 50051; + + public static void Main() + { + var channel = new Channel(Address, Port, ChannelCredentials.Insecure); + var client = new Calc.CalcClient(channel); + + var request = new Request + { + Num1 = 40, + Num2 = 2 + }; + + var response = client.AddAsync(request).GetAwaiter().GetResult(); + + Console.WriteLine($"Addition result: {response.Payload.Deserialize().Result}"); + + channel.ShutdownAsync().GetAwaiter().GetResult(); + } + } +} diff --git a/examples/cs/grpc/shared-types-assembly/client/grpc_sta-client.csproj b/examples/cs/grpc/shared-types-assembly/client/grpc_sta-client.csproj new file mode 100644 index 0000000000..891fca5944 --- /dev/null +++ b/examples/cs/grpc/shared-types-assembly/client/grpc_sta-client.csproj @@ -0,0 +1,82 @@ + + + + + + {9DCEAD87-C61F-40D5-989E-0E44F56E01BE} + Exe + Examples.SharedTypes + grpc_sta-client + v4.5 + + + AnyCPU + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + AnyCPU + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + + ..\..\..\..\..\cs\packages\Grpc.Core.1.3.0\lib\net45\Grpc.Core.dll + + + ..\..\..\..\..\cs\packages\System.Interactive.Async.3.1.1\lib\net45\System.Interactive.Async.dll + + + + + + + + + + --grpc --structs=false + + + + + + + + $(BOND_BINARY_PATH)\net45\Bond.Attributes.dll + + + $(BOND_BINARY_PATH)\net45\Bond.dll + + + + + + + + {AF03BAE6-2470-4E1B-A683-3EBDCDC595FA} + grpc + + + {723baa6b-b309-48cf-b2a9-ecddc91909d1} + grpc_sta-types + + + + + + diff --git a/examples/cs/grpc/shared-types-assembly/client/packages.config b/examples/cs/grpc/shared-types-assembly/client/packages.config new file mode 100644 index 0000000000..a9c5cf8e8b --- /dev/null +++ b/examples/cs/grpc/shared-types-assembly/client/packages.config @@ -0,0 +1,5 @@ + + + + + \ No newline at end of file diff --git a/examples/cs/grpc/shared-types-assembly/server/CalcService.cs b/examples/cs/grpc/shared-types-assembly/server/CalcService.cs new file mode 100644 index 0000000000..cd2df26036 --- /dev/null +++ b/examples/cs/grpc/shared-types-assembly/server/CalcService.cs @@ -0,0 +1,23 @@ +// Copyright (c) Microsoft. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +namespace Examples.SharedTypes +{ + using System.Threading.Tasks; + using Bond.Grpc; + using Grpc.Core; + + public class CalcService : Calc.CalcBase + { + public override Task> Add(IMessage request, ServerCallContext context) + { + var r = request.Payload.Deserialize(); + var response = new Response + { + Result = r.Num1 + r.Num2 + }; + + return Task.FromResult(Message.From(response)); + } + } +} diff --git a/examples/cs/grpc/shared-types-assembly/server/StaServer.cs b/examples/cs/grpc/shared-types-assembly/server/StaServer.cs new file mode 100644 index 0000000000..6d944b5405 --- /dev/null +++ b/examples/cs/grpc/shared-types-assembly/server/StaServer.cs @@ -0,0 +1,32 @@ +// Copyright (c) Microsoft. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +namespace Examples.SharedTypes +{ + using System; + using Grpc.Core; + + public static class Server + { + const string Address = "127.0.0.1"; + const int Port = 50051; + + public static void Main() + { + var server = new Grpc.Core.Server + { + Services = + { + Calc.BindService(new CalcService()) + }, + Ports = { new ServerPort(Address, Port, ServerCredentials.Insecure) } + }; + server.Start(); + + Console.WriteLine("Press ENTER to stop the service."); + Console.ReadLine(); + + server.ShutdownAsync().GetAwaiter().GetResult(); + } + } +} diff --git a/examples/cs/grpc/shared-types-assembly/server/grpc_sta-server.csproj b/examples/cs/grpc/shared-types-assembly/server/grpc_sta-server.csproj new file mode 100644 index 0000000000..e091a7c220 --- /dev/null +++ b/examples/cs/grpc/shared-types-assembly/server/grpc_sta-server.csproj @@ -0,0 +1,77 @@ + + + + + + {A390B660-5F81-4CD3-B5EF-EB7C1C6B6AFE} + Exe + Examples.SharedTypes + grpc_sta-service + v4.5 + + --grpc --structs=false + + + AnyCPU + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + AnyCPU + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + + ..\..\..\..\..\cs\packages\Grpc.Core.1.3.0\lib\net45\Grpc.Core.dll + + + ..\..\..\..\..\cs\packages\System.Interactive.Async.3.1.1\lib\net45\System.Interactive.Async.dll + + + + + + + + + + + + + + + $(BOND_BINARY_PATH)\net45\Bond.Attributes.dll + + + $(BOND_BINARY_PATH)\net45\Bond.dll + + + + + + + + {AF03BAE6-2470-4E1B-A683-3EBDCDC595FA} + grpc + + + {723BAA6B-B309-48CF-B2A9-ECDDC91909D1} + grpc_sta-types + + + + + + diff --git a/examples/cs/grpc/shared-types-assembly/server/packages.config b/examples/cs/grpc/shared-types-assembly/server/packages.config new file mode 100644 index 0000000000..a9c5cf8e8b --- /dev/null +++ b/examples/cs/grpc/shared-types-assembly/server/packages.config @@ -0,0 +1,5 @@ + + + + + \ No newline at end of file diff --git a/examples/cs/grpc/shared-types-assembly/sta.bond b/examples/cs/grpc/shared-types-assembly/sta.bond new file mode 100644 index 0000000000..42d987c7a5 --- /dev/null +++ b/examples/cs/grpc/shared-types-assembly/sta.bond @@ -0,0 +1,20 @@ +// Copyright (c) Microsoft. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +namespace Examples.SharedTypes; + +struct Request +{ + 0: int32 Num1; + 1: int32 Num2; +} + +struct Response +{ + 0: int64 Result; +} + +service Calc +{ + Response Add(Request); +} diff --git a/examples/cs/grpc/shared-types-assembly/types/grpc_sta-types.csproj b/examples/cs/grpc/shared-types-assembly/types/grpc_sta-types.csproj new file mode 100644 index 0000000000..1f78c316fb --- /dev/null +++ b/examples/cs/grpc/shared-types-assembly/types/grpc_sta-types.csproj @@ -0,0 +1,47 @@ + + + + + + {723BAA6B-B309-48CF-B2A9-ECDDC91909D1} + Library + Examples.SharedTypes + grpc_sta-types + v4.5 + + + AnyCPU + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + AnyCPU + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + + + + + + + + + + $(BOND_BINARY_PATH)\net45\Bond.Attributes.dll + + + + +