Skip to content

Commit

Permalink
Works on Windows with static lib nethost.
Browse files Browse the repository at this point in the history
Updated readme with minimum preview build of .NET5 to use.
  • Loading branch information
AaronRobinsonMSFT committed Apr 27, 2020
1 parent 83ff865 commit ac4c203
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 23 deletions.
33 changes: 15 additions & 18 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,15 @@ This work is inspired by work in the [Xamarian][xamarin_embed_link] and [CoreRT]

### Minimum

* [.NET 5.0+](https://dotnet.microsoft.com/).
* [.NET 5.0.100-preview.5.20221.6](https://github.com/dotnet/installer) or greater.
* [C99](https://en.cppreference.com/w/c/language/history) compatible compiler.

### DNNE NuPkg Requirements
### DNNE NuPkg Requirements (Windows only)

**Windows:**
* [Visual Studio 2015](https://visualstudio.microsoft.com/) or greater.
* Windows 10 SDK - Installed with Visual Studio.

**Unix and OSX:**
* [Clang](https://clang.llvm.org/) on path.

## Exporting details

- The exported function must be marked `static` and `public`.
Expand Down Expand Up @@ -72,40 +69,40 @@ Failure to load the runtime or find an export results in the native library call
}
```

2) Adorn the desired managed function with the `DNNE.ExportAttribute`.
1) Adorn the desired managed function with the `DNNE.ExportAttribute`.
- Optionally set the `DNNE.ExportAttribute.EntryPoint` property to indicate the name of the native export.
- If the `EntryPoint` property is `null`, the name of the mananged function is used. This default name will not include the namespace or class containing the function.
- User supplied values in `EntryPoint` will not be modified or validated in any manner. This string will be consume by a C compiler and should therefore adhere to the [C language's restrictions on function names](https://en.cppreference.com/w/c/language/functions).

3) Set the `<EnableDynamicLoading>true</EnableDynamicLoading>` property in the managed project containing the methods to export. This will produce a `*.runtimeconfig.json` that is needed to activate the runtime during export dispatch.
1) Set the `<EnableDynamicLoading>true</EnableDynamicLoading>` property in the managed project containing the methods to export. This will produce a `*.runtimeconfig.json` that is needed to activate the runtime during export dispatch.

### Generate using NuGet package
## Generating a native binary using the DNNE NuPkg

**Note** Currently limited to Windows only

4) Include the DNNE NuGet package in the managed project. The NuGet is currently not on a feed, so a local path must be set as a NuGet source after the package is built.
1) The DNNE NuPkg can be built locally by running `pack` on [`dnne-gen.csproj`](./src/dnne-gen/dnne-gen.csproj).

`> dotnet pack dnne-gen.csproj`

1) Add the NuPkg to the target managed project. Remember to update the projects `nuget.config` to point at the location of the recently built DNNE NuPkg.

```xml
<ItemGroup>
<PackageReference Include="DNNE" Version="1.0.0" />
</ItemGroup>
```

5) The DNNE NuGet package can be built locally by running `pack` on [`dnne-gen.csproj`](./src/dnne-gen/dnne-gen.csproj).

`> dotnet pack dnne-gen.csproj`

6) Build the project from a Visual Studio Developer command prompt for the desired bitness. The generated native binary will be in the output directory with the managed binary. The native binary will a `NE` suffix and the system extension for dynamic/shared native libraries (i.e. `.dll`, `.so`, `.dylib`).
1) Build the managed project to generate the native binary. The native binary will have a `NE` suffix and the system extension for dynamic/shared native libraries (i.e. `.dll`, `.so`, `.dylib`).

7) Deploy the native binary, managed assembly and associated `*.json` files for consumption from a native process.
1) Deploy the native binary, managed assembly and associated `*.json` files for consumption from a native process.

### Generate manually

4) Run the [dnne-gen](./src/dnne-gen) tool on the managed assembly.
1) Run the [dnne-gen](./src/dnne-gen) tool on the managed assembly.

5) Take the generated source from `dnne-gen` and the DNNE [platform](./src/platform) source to compile a native binary with the desired native exports. See the [Native API](#nativeapi) section for build details.
1) Take the generated source from `dnne-gen` and the DNNE [platform](./src/platform) source to compile a native binary with the desired native exports. See the [Native API](#nativeapi) section for build details.

6) Deploy the native binary, managed assembly and associated `*.json` files for consumption from a native process.
1) Deploy the native binary, managed assembly and associated `*.json` files for consumption from a native process.

# Additional References

Expand Down
10 changes: 5 additions & 5 deletions src/msbuild/DNNE.BuildTasks/Windows.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public static void ConstructCommandLine(CreateCompileCommand export, out string
SetConfigurationBasedFlags(isDebug, ref compilerFlags, ref linkerFlags);

// Set compiler flags
compilerFlags.Append($"/TC /GS /Zi ");
compilerFlags.Append($"/TC /MT /GS /Zi ");
compilerFlags.Append($"/D DNNE_ASSEMBLY_NAME={export.AssemblyName} ");
compilerFlags.Append($"/I \"{vcIncDir}\" /I \"{export.PlatformPath}\" /I \"{export.NetHostPath}\" ");
compilerFlags.Append($"/I \"{sharedIncDir}\" /I \"{umIncDir}\" /I \"{ucrtIncDir}\" ");
Expand All @@ -77,8 +77,8 @@ public static void ConstructCommandLine(CreateCompileCommand export, out string
linkerFlags.Append($"/DLL ");
linkerFlags.Append($"/LIBPATH:\"{libDir}\" ");
linkerFlags.Append($"/LIBPATH:\"{umLibDir}\" /LIBPATH:\"{ucrtLibDir}\" ");
linkerFlags.Append($"\"{Path.Combine(export.NetHostPath, "nethost.lib")}\" ");
//linkerFlags.Append($"\"{Path.Combine(export.NetHostPath, "libnethost.lib")}\" ");
linkerFlags.Append($"\"{Path.Combine(export.NetHostPath, "libnethost.lib")}\" Advapi32.lib ");
linkerFlags.Append($"/IGNORE:4099 "); // libnethost.lib doesn't ship PDBs so linker warnings occur.
linkerFlags.Append($"/out:\"{Path.Combine(export.OutputPath, export.OutputName)}\" ");

command = Path.Combine(binDir, "cl.exe");
Expand All @@ -104,12 +104,12 @@ public static void SetConfigurationBasedFlags(bool isDebug, ref StringBuilder co
{
if (isDebug)
{
compiler.Append($"/Od /MTd /LDd ");
compiler.Append($"/Od /LDd ");
linker.Append($"");
}
else
{
compiler.Append($"/O2 /MT /LD ");
compiler.Append($"/O2 /LD ");
linker.Append($"");
}
}
Expand Down

0 comments on commit ac4c203

Please sign in to comment.