diff --git a/src/installer/tests/HostActivation.Tests/NativeHosting/Nethost.cs b/src/installer/tests/HostActivation.Tests/NativeHosting/Nethost.cs index cf950ca14b669e..a12150b6af6b72 100644 --- a/src/installer/tests/HostActivation.Tests/NativeHosting/Nethost.cs +++ b/src/installer/tests/HostActivation.Tests/NativeHosting/Nethost.cs @@ -357,6 +357,20 @@ public void TestOnlyDisabledByDefault() } } + [Fact] + public void GetHostFxrPath_StaticNativeHost() + { + string dotNetRoot = Path.Combine(sharedState.ValidInstallRoot, "dotnet"); + CommandResult result = Command.Create(sharedState.NativeHostStaticPath, $"{GetHostFxrPath} false nullptr {dotNetRoot}") + .EnableTracingAndCaptureOutputs() + .DotNetRoot(null) + .Execute(); + + result.Should().Pass() + .And.HaveStdErrContaining("Using dotnet root parameter") + .And.HaveStdOutContaining($"hostfxr_path: {sharedState.HostFxrPath}".ToLower()); + } + public class SharedTestState : SharedTestStateBase { public string HostFxrPath { get; } @@ -367,6 +381,8 @@ public class SharedTestState : SharedTestStateBase public string ProductHostFxrPath { get; } + public string NativeHostStaticPath { get; } + public SharedTestState() { InvalidInstallRoot = Path.Combine(BaseDirectory, "invalid"); @@ -385,6 +401,11 @@ public SharedTestState() Directory.CreateDirectory(productDir); ProductHostFxrPath = Path.Combine(productDir, HostFxrName); File.Copy(Binaries.HostFxr.FilePath, ProductHostFxrPath); + + // Copy over the static native host - this executable has nethost statically linked, + // so it does not require the nethost shared library at runtime. + NativeHostStaticPath = Path.Combine(BaseDirectory, Binaries.NativeHostStatic.FileName); + File.Copy(Binaries.NativeHostStatic.FilePath, NativeHostStaticPath); } private string CreateHostFxr(string destinationDirectory) diff --git a/src/installer/tests/TestUtils/Binaries.cs b/src/installer/tests/TestUtils/Binaries.cs index 1fbda4fa30501b..a252ceee081f44 100644 --- a/src/installer/tests/TestUtils/Binaries.cs +++ b/src/installer/tests/TestUtils/Binaries.cs @@ -82,6 +82,12 @@ public static class NetHost public static string FilePath = Path.Combine(RepoDirectoriesProvider.Default.HostArtifacts, FileName); } + public static class NativeHostStatic + { + public static string FileName = GetExeName("nativehost_static"); + public static string FilePath = Path.Combine(RepoDirectoriesProvider.Default.HostTestArtifacts, FileName); + } + public static class SingleFileHost { public static string FileName = GetExeName("singlefilehost"); diff --git a/src/native/corehost/test/nativehost/CMakeLists.txt b/src/native/corehost/test/nativehost/CMakeLists.txt index a3c9ed9fa24a30..d31e3de417fb23 100644 --- a/src/native/corehost/test/nativehost/CMakeLists.txt +++ b/src/native/corehost/test/nativehost/CMakeLists.txt @@ -55,3 +55,22 @@ if (CLR_CMAKE_TARGET_WIN32) target_link_libraries(nativehost PRIVATE delayimp.lib ole32 oleaut32) endif() +# Build a second executable that links against the static nethost library (libnethost) +# instead of the shared one. This validates the static library consumption scenario. +add_executable(nativehost_static ${SOURCES}) + +add_sanitizer_runtime_support(nativehost_static) + +install_with_stripped_symbols(nativehost_static TARGETS corehost_test) + +target_compile_definitions(nativehost_static PRIVATE NETHOST_USE_AS_STATIC) + +target_link_libraries(nativehost_static PRIVATE + libnethost + hostmisc + $<$:${PTHREAD_LIB}>) + +if (CLR_CMAKE_TARGET_WIN32) + target_link_libraries(nativehost_static PRIVATE ole32 oleaut32) +endif() +