From ff2d1274fd0d634e423e12be0160bab3da4a006c Mon Sep 17 00:00:00 2001 From: Leandro Pereira Date: Thu, 16 Apr 2020 01:08:50 +0000 Subject: [PATCH 1/5] hostfxr: Build most of hostfxr as a static library This is part of the work to create an apphost that bundles both hostfxr and hostpolicy. The main distinction between the static and shared versions of hostfxr is that the static version contains a hostpolicy resolver that references the hostpolicy symbols directly rather than loading them from a DLL. --- src/installer/corehost/cli/CMakeLists.txt | 4 +- .../corehost/cli/comhost/CMakeLists.txt | 1 + src/installer/corehost/cli/exe.cmake | 1 + src/installer/corehost/cli/fxr/CMakeLists.txt | 45 +---------- .../corehost/cli/fxr/shared/CMakeLists.txt | 33 ++++++++ .../fxr/{ => shared}/hostpolicy_resolver.cpp | 0 .../corehost/cli/fxr/static/CMakeLists.txt | 44 ++++++++++ .../cli/fxr/{ => static}/command_line.cpp | 0 .../cli/fxr/{ => static}/command_line.h | 0 .../cli/fxr/{ => static}/corehost_init.cpp | 0 .../cli/fxr/{ => static}/corehost_init.h | 0 .../cli/fxr/{ => static}/framework_info.cpp | 0 .../cli/fxr/{ => static}/framework_info.h | 0 .../cli/fxr/{ => static}/fx_muxer.cpp | 2 +- .../corehost/cli/fxr/{ => static}/fx_muxer.h | 0 .../cli/fxr/{ => static}/fx_resolver.cpp | 0 .../cli/fxr/{ => static}/fx_resolver.h | 0 .../fxr/{ => static}/fx_resolver.messages.cpp | 0 .../corehost/cli/fxr/{ => static}/fx_ver.cpp | 0 .../corehost/cli/fxr/{ => static}/fx_ver.h | 0 .../cli/fxr/{ => static}/host_context.cpp | 0 .../cli/fxr/{ => static}/host_context.h | 0 .../corehost/cli/fxr/{ => static}/hostfxr.cpp | 0 .../cli/fxr/static/hostpolicy_resolver.cpp | 80 +++++++++++++++++++ .../fxr/{ => static}/hostpolicy_resolver.h | 0 .../cli/fxr/{ => static}/sdk_info.cpp | 0 .../corehost/cli/fxr/{ => static}/sdk_info.h | 0 .../cli/fxr/{ => static}/sdk_resolver.cpp | 0 .../cli/fxr/{ => static}/sdk_resolver.h | 0 .../corehost/cli/hostcommon/CMakeLists.txt | 5 +- .../corehost/cli/hostmisc/CMakeLists.txt | 4 +- .../corehost/cli/hostpolicy/CMakeLists.txt | 1 + .../corehost/cli/ijwhost/CMakeLists.txt | 1 + .../corehost/cli/nethost/CMakeLists.txt | 1 + .../corehost/cli/test_fx_ver/CMakeLists.txt | 1 + .../corehost/cli/winrthost/CMakeLists.txt | 1 + 36 files changed, 174 insertions(+), 50 deletions(-) create mode 100644 src/installer/corehost/cli/fxr/shared/CMakeLists.txt rename src/installer/corehost/cli/fxr/{ => shared}/hostpolicy_resolver.cpp (100%) create mode 100644 src/installer/corehost/cli/fxr/static/CMakeLists.txt rename src/installer/corehost/cli/fxr/{ => static}/command_line.cpp (100%) rename src/installer/corehost/cli/fxr/{ => static}/command_line.h (100%) rename src/installer/corehost/cli/fxr/{ => static}/corehost_init.cpp (100%) rename src/installer/corehost/cli/fxr/{ => static}/corehost_init.h (100%) rename src/installer/corehost/cli/fxr/{ => static}/framework_info.cpp (100%) rename src/installer/corehost/cli/fxr/{ => static}/framework_info.h (100%) rename src/installer/corehost/cli/fxr/{ => static}/fx_muxer.cpp (99%) rename src/installer/corehost/cli/fxr/{ => static}/fx_muxer.h (100%) rename src/installer/corehost/cli/fxr/{ => static}/fx_resolver.cpp (100%) rename src/installer/corehost/cli/fxr/{ => static}/fx_resolver.h (100%) rename src/installer/corehost/cli/fxr/{ => static}/fx_resolver.messages.cpp (100%) rename src/installer/corehost/cli/fxr/{ => static}/fx_ver.cpp (100%) rename src/installer/corehost/cli/fxr/{ => static}/fx_ver.h (100%) rename src/installer/corehost/cli/fxr/{ => static}/host_context.cpp (100%) rename src/installer/corehost/cli/fxr/{ => static}/host_context.h (100%) rename src/installer/corehost/cli/fxr/{ => static}/hostfxr.cpp (100%) create mode 100644 src/installer/corehost/cli/fxr/static/hostpolicy_resolver.cpp rename src/installer/corehost/cli/fxr/{ => static}/hostpolicy_resolver.h (100%) rename src/installer/corehost/cli/fxr/{ => static}/sdk_info.cpp (100%) rename src/installer/corehost/cli/fxr/{ => static}/sdk_info.h (100%) rename src/installer/corehost/cli/fxr/{ => static}/sdk_resolver.cpp (100%) rename src/installer/corehost/cli/fxr/{ => static}/sdk_resolver.h (100%) diff --git a/src/installer/corehost/cli/CMakeLists.txt b/src/installer/corehost/cli/CMakeLists.txt index 4945f6722598f9..56320251bbf348 100644 --- a/src/installer/corehost/cli/CMakeLists.txt +++ b/src/installer/corehost/cli/CMakeLists.txt @@ -2,10 +2,10 @@ add_subdirectory(hostmisc) add_subdirectory(hostcommon) add_subdirectory(apphost) add_subdirectory(dotnet) -add_subdirectory(fxr) -add_subdirectory(hostpolicy) add_subdirectory(nethost) add_subdirectory(test_fx_ver) +add_subdirectory(fxr) +add_subdirectory(hostpolicy) add_subdirectory(test) diff --git a/src/installer/corehost/cli/comhost/CMakeLists.txt b/src/installer/corehost/cli/comhost/CMakeLists.txt index b0f4759ffc682c..de8094a194f93c 100644 --- a/src/installer/corehost/cli/comhost/CMakeLists.txt +++ b/src/installer/corehost/cli/comhost/CMakeLists.txt @@ -8,6 +8,7 @@ set(DOTNET_PROJECT_NAME "comhost") # Include directories include_directories(../fxr) +include_directories(../fxr/static) include_directories(../json) # CMake does not recommend using globbing since it messes with the freshness checks diff --git a/src/installer/corehost/cli/exe.cmake b/src/installer/corehost/cli/exe.cmake index de8cd49396bd80..d713c9bd021e96 100644 --- a/src/installer/corehost/cli/exe.cmake +++ b/src/installer/corehost/cli/exe.cmake @@ -11,6 +11,7 @@ include(${CMAKE_CURRENT_LIST_DIR}/common.cmake) # Include directories include_directories(${CMAKE_CURRENT_LIST_DIR}/fxr) +include_directories(${CMAKE_CURRENT_LIST_DIR}/fxr/static) # CMake does not recommend using globbing since it messes with the freshness checks list(APPEND SOURCES diff --git a/src/installer/corehost/cli/fxr/CMakeLists.txt b/src/installer/corehost/cli/fxr/CMakeLists.txt index 216ecbf076bb7b..886c9c33d996bb 100644 --- a/src/installer/corehost/cli/fxr/CMakeLists.txt +++ b/src/installer/corehost/cli/fxr/CMakeLists.txt @@ -2,46 +2,5 @@ # The .NET Foundation licenses this file to you under the MIT license. # See the LICENSE file in the project root for more information. -project(hostfxr) - -set(DOTNET_PROJECT_NAME "hostfxr") - -# Include directories -include_directories(../json) - -# CMake does not recommend using globbing since it messes with the freshness checks -set(SOURCES - ./command_line.cpp - ./corehost_init.cpp - ./hostfxr.cpp - ./fx_muxer.cpp - ./fx_resolver.cpp - ./fx_resolver.messages.cpp - ./framework_info.cpp - ./host_context.cpp - ./hostpolicy_resolver.cpp - ./sdk_info.cpp - ./sdk_resolver.cpp -) - -set(HEADERS - ../corehost_context_contract.h - ../hostpolicy.h - ../fx_definition.h - ../fx_reference.h - ../roll_fwd_on_no_candidate_fx_option.h - ./command_line.h - ./corehost_init.h - ./fx_muxer.h - ./fx_resolver.h - ./framework_info.h - ./host_context.h - ./hostpolicy_resolver.h - ./sdk_info.h - ./sdk_resolver.h -) - -include(../lib.cmake) - -install_with_stripped_symbols(hostfxr TARGETS corehost) -target_link_libraries(hostfxr libhostcommon) +add_subdirectory(static) +add_subdirectory(shared) diff --git a/src/installer/corehost/cli/fxr/shared/CMakeLists.txt b/src/installer/corehost/cli/fxr/shared/CMakeLists.txt new file mode 100644 index 00000000000000..d8d2bce4fe8c9a --- /dev/null +++ b/src/installer/corehost/cli/fxr/shared/CMakeLists.txt @@ -0,0 +1,33 @@ +# Licensed to the .NET Foundation under one or more agreements. +# The .NET Foundation licenses this file to you under the MIT license. +# See the LICENSE file in the project root for more information. + +project(hostfxr) + +set(DOTNET_PROJECT_NAME "hostfxr") + +# Include directories +include_directories(../../json) +include_directories(../static) + +# CMake does not recommend using globbing since it messes with the freshness checks +set(SOURCES + ./hostpolicy_resolver.cpp +) + +set(HEADERS + ../static/command_line.h + ../static/corehost_init.h + ../static/fx_muxer.h + ../static/fx_resolver.h + ../static/framework_info.h + ../static/host_context.h + ../static/hostpolicy_resolver.h + ../static/sdk_info.h + ../static/sdk_resolver.h +) + +include(../../lib.cmake) + +install_with_stripped_symbols(hostfxr TARGETS corehost) +target_link_libraries(hostfxr libhostcommon libhostfxr_static) diff --git a/src/installer/corehost/cli/fxr/hostpolicy_resolver.cpp b/src/installer/corehost/cli/fxr/shared/hostpolicy_resolver.cpp similarity index 100% rename from src/installer/corehost/cli/fxr/hostpolicy_resolver.cpp rename to src/installer/corehost/cli/fxr/shared/hostpolicy_resolver.cpp diff --git a/src/installer/corehost/cli/fxr/static/CMakeLists.txt b/src/installer/corehost/cli/fxr/static/CMakeLists.txt new file mode 100644 index 00000000000000..39f7d1a5a2de9e --- /dev/null +++ b/src/installer/corehost/cli/fxr/static/CMakeLists.txt @@ -0,0 +1,44 @@ +# Licensed to the .NET Foundation under one or more agreements. +# The .NET Foundation licenses this file to you under the MIT license. +# See the LICENSE file in the project root for more information. + +project(hostfxr_static) + +set(DOTNET_PROJECT_NAME "hostfxr_static") + +# Include directories +include_directories(../../json) + +# CMake does not recommend using globbing since it messes with the freshness checks +set(SOURCES + ./command_line.cpp + ./corehost_init.cpp + ./hostfxr.cpp + ./fx_muxer.cpp + ./fx_resolver.cpp + ./fx_resolver.messages.cpp + ./framework_info.cpp + ./host_context.cpp + ./sdk_info.cpp + ./sdk_resolver.cpp + ./hostpolicy_resolver.cpp +) + +set(HEADERS + ../../corehost_context_contract.h + ../../hostpolicy.h + ../../fx_definition.h + ../../fx_reference.h + ../../roll_fwd_on_no_candidate_fx_option.h + ./command_line.h + ./corehost_init.h + ./fx_muxer.h + ./fx_resolver.h + ./framework_info.h + ./host_context.h + ./sdk_info.h + ./sdk_resolver.h +) + +set(SKIP_VERSIONING 1) +include(../../lib_static.cmake) diff --git a/src/installer/corehost/cli/fxr/command_line.cpp b/src/installer/corehost/cli/fxr/static/command_line.cpp similarity index 100% rename from src/installer/corehost/cli/fxr/command_line.cpp rename to src/installer/corehost/cli/fxr/static/command_line.cpp diff --git a/src/installer/corehost/cli/fxr/command_line.h b/src/installer/corehost/cli/fxr/static/command_line.h similarity index 100% rename from src/installer/corehost/cli/fxr/command_line.h rename to src/installer/corehost/cli/fxr/static/command_line.h diff --git a/src/installer/corehost/cli/fxr/corehost_init.cpp b/src/installer/corehost/cli/fxr/static/corehost_init.cpp similarity index 100% rename from src/installer/corehost/cli/fxr/corehost_init.cpp rename to src/installer/corehost/cli/fxr/static/corehost_init.cpp diff --git a/src/installer/corehost/cli/fxr/corehost_init.h b/src/installer/corehost/cli/fxr/static/corehost_init.h similarity index 100% rename from src/installer/corehost/cli/fxr/corehost_init.h rename to src/installer/corehost/cli/fxr/static/corehost_init.h diff --git a/src/installer/corehost/cli/fxr/framework_info.cpp b/src/installer/corehost/cli/fxr/static/framework_info.cpp similarity index 100% rename from src/installer/corehost/cli/fxr/framework_info.cpp rename to src/installer/corehost/cli/fxr/static/framework_info.cpp diff --git a/src/installer/corehost/cli/fxr/framework_info.h b/src/installer/corehost/cli/fxr/static/framework_info.h similarity index 100% rename from src/installer/corehost/cli/fxr/framework_info.h rename to src/installer/corehost/cli/fxr/static/framework_info.h diff --git a/src/installer/corehost/cli/fxr/fx_muxer.cpp b/src/installer/corehost/cli/fxr/static/fx_muxer.cpp similarity index 99% rename from src/installer/corehost/cli/fxr/fx_muxer.cpp rename to src/installer/corehost/cli/fxr/static/fx_muxer.cpp index 31c8c177978078..0357513f114073 100644 --- a/src/installer/corehost/cli/fxr/fx_muxer.cpp +++ b/src/installer/corehost/cli/fxr/static/fx_muxer.cpp @@ -471,7 +471,7 @@ namespace if (!hostpolicy_resolver::try_get_dir(mode, host_info.dotnet_root, fx_definitions, app_candidate, deps_file, probe_realpaths, &hostpolicy_dir)) { - return CoreHostLibMissingFailure; + return StatusCode::CoreHostLibMissingFailure; } init.reset(new corehost_init_t(host_command, host_info, deps_file, additional_deps_serialized, probe_realpaths, mode, fx_definitions)); diff --git a/src/installer/corehost/cli/fxr/fx_muxer.h b/src/installer/corehost/cli/fxr/static/fx_muxer.h similarity index 100% rename from src/installer/corehost/cli/fxr/fx_muxer.h rename to src/installer/corehost/cli/fxr/static/fx_muxer.h diff --git a/src/installer/corehost/cli/fxr/fx_resolver.cpp b/src/installer/corehost/cli/fxr/static/fx_resolver.cpp similarity index 100% rename from src/installer/corehost/cli/fxr/fx_resolver.cpp rename to src/installer/corehost/cli/fxr/static/fx_resolver.cpp diff --git a/src/installer/corehost/cli/fxr/fx_resolver.h b/src/installer/corehost/cli/fxr/static/fx_resolver.h similarity index 100% rename from src/installer/corehost/cli/fxr/fx_resolver.h rename to src/installer/corehost/cli/fxr/static/fx_resolver.h diff --git a/src/installer/corehost/cli/fxr/fx_resolver.messages.cpp b/src/installer/corehost/cli/fxr/static/fx_resolver.messages.cpp similarity index 100% rename from src/installer/corehost/cli/fxr/fx_resolver.messages.cpp rename to src/installer/corehost/cli/fxr/static/fx_resolver.messages.cpp diff --git a/src/installer/corehost/cli/fxr/fx_ver.cpp b/src/installer/corehost/cli/fxr/static/fx_ver.cpp similarity index 100% rename from src/installer/corehost/cli/fxr/fx_ver.cpp rename to src/installer/corehost/cli/fxr/static/fx_ver.cpp diff --git a/src/installer/corehost/cli/fxr/fx_ver.h b/src/installer/corehost/cli/fxr/static/fx_ver.h similarity index 100% rename from src/installer/corehost/cli/fxr/fx_ver.h rename to src/installer/corehost/cli/fxr/static/fx_ver.h diff --git a/src/installer/corehost/cli/fxr/host_context.cpp b/src/installer/corehost/cli/fxr/static/host_context.cpp similarity index 100% rename from src/installer/corehost/cli/fxr/host_context.cpp rename to src/installer/corehost/cli/fxr/static/host_context.cpp diff --git a/src/installer/corehost/cli/fxr/host_context.h b/src/installer/corehost/cli/fxr/static/host_context.h similarity index 100% rename from src/installer/corehost/cli/fxr/host_context.h rename to src/installer/corehost/cli/fxr/static/host_context.h diff --git a/src/installer/corehost/cli/fxr/hostfxr.cpp b/src/installer/corehost/cli/fxr/static/hostfxr.cpp similarity index 100% rename from src/installer/corehost/cli/fxr/hostfxr.cpp rename to src/installer/corehost/cli/fxr/static/hostfxr.cpp diff --git a/src/installer/corehost/cli/fxr/static/hostpolicy_resolver.cpp b/src/installer/corehost/cli/fxr/static/hostpolicy_resolver.cpp new file mode 100644 index 00000000000000..8bf2371988ba1e --- /dev/null +++ b/src/installer/corehost/cli/fxr/static/hostpolicy_resolver.cpp @@ -0,0 +1,80 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +#include +#include +#include +#include "hostpolicy_resolver.h" +#include +#include + +extern "C" +{ + int corehost_load(const host_interface_t* init); + int corehost_unload(); + corehost_error_writer_fn corehost_set_error_writer(corehost_error_writer_fn error_writer); + int corehost_initialize(const corehost_initialize_request_t *init_request, int32_t options, /*out*/ corehost_context_contract *context_contract); +} + +int hostpolicy_resolver::load( + const pal::string_t& lib_dir, + pal::dll_t* dll, + hostpolicy_contract_t &hostpolicy_contract) +{ + static hostpolicy_contract_t contract; + + trace::info(_X("Using internal hostpolicy")); + + contract.load = corehost_load; + contract.unload = corehost_unload; + contract.set_error_writer = corehost_set_error_writer; + contract.initialize = corehost_initialize; + + hostpolicy_contract = contract; + *dll = nullptr; + + return StatusCode::Success; +} + +bool hostpolicy_resolver::try_get_dir( + host_mode_t mode, + const pal::string_t& dotnet_root, + const fx_definition_vector_t& fx_definitions, + const pal::string_t& app_candidate, + const pal::string_t& specified_deps_file, + const std::vector& probe_realpaths, + pal::string_t* impl_dir) +{ + // Get the expected directory that would contain hostpolicy. + pal::string_t expected; + if (get_app(fx_definitions).get_runtime_config().get_is_framework_dependent()) + { + // The hostpolicy is required to be in the root framework's location + expected.assign(get_root_framework(fx_definitions).get_dir()); + assert(pal::directory_exists(expected)); + } + else + { + // Native apps can be activated by muxer, native exe host or "corehost" + // 1. When activated with dotnet.exe or corehost.exe, check for hostpolicy in the deps dir or + // app dir. + // 2. When activated with native exe, the standalone host, check own directory. + assert(mode != host_mode_t::invalid); + switch (mode) + { + case host_mode_t::apphost: + case host_mode_t::libhost: + expected = dotnet_root; + break; + + default: + expected = get_directory(specified_deps_file.empty() ? app_candidate : specified_deps_file); + break; + } + } + + // Assume the internal hostpolicy is in the expected location. + impl_dir->assign(expected); + return true; +} diff --git a/src/installer/corehost/cli/fxr/hostpolicy_resolver.h b/src/installer/corehost/cli/fxr/static/hostpolicy_resolver.h similarity index 100% rename from src/installer/corehost/cli/fxr/hostpolicy_resolver.h rename to src/installer/corehost/cli/fxr/static/hostpolicy_resolver.h diff --git a/src/installer/corehost/cli/fxr/sdk_info.cpp b/src/installer/corehost/cli/fxr/static/sdk_info.cpp similarity index 100% rename from src/installer/corehost/cli/fxr/sdk_info.cpp rename to src/installer/corehost/cli/fxr/static/sdk_info.cpp diff --git a/src/installer/corehost/cli/fxr/sdk_info.h b/src/installer/corehost/cli/fxr/static/sdk_info.h similarity index 100% rename from src/installer/corehost/cli/fxr/sdk_info.h rename to src/installer/corehost/cli/fxr/static/sdk_info.h diff --git a/src/installer/corehost/cli/fxr/sdk_resolver.cpp b/src/installer/corehost/cli/fxr/static/sdk_resolver.cpp similarity index 100% rename from src/installer/corehost/cli/fxr/sdk_resolver.cpp rename to src/installer/corehost/cli/fxr/static/sdk_resolver.cpp diff --git a/src/installer/corehost/cli/fxr/sdk_resolver.h b/src/installer/corehost/cli/fxr/static/sdk_resolver.h similarity index 100% rename from src/installer/corehost/cli/fxr/sdk_resolver.h rename to src/installer/corehost/cli/fxr/static/sdk_resolver.h diff --git a/src/installer/corehost/cli/hostcommon/CMakeLists.txt b/src/installer/corehost/cli/hostcommon/CMakeLists.txt index 8ad0c8d09f47c2..2bac08df03e7c0 100644 --- a/src/installer/corehost/cli/hostcommon/CMakeLists.txt +++ b/src/installer/corehost/cli/hostcommon/CMakeLists.txt @@ -8,6 +8,7 @@ set(DOTNET_PROJECT_NAME "hostcommon") # Include directories include_directories(../fxr) +include_directories(../fxr/static) include_directories(../json) # CMake does not recommend using globbing since it messes with the freshness checks @@ -19,7 +20,7 @@ set(SOURCES ../roll_forward_option.cpp ../fx_definition.cpp ../fx_reference.cpp - ../fxr/fx_ver.cpp + ../fxr/static/fx_ver.cpp ../version.cpp ../version_compatibility_range.cpp ../runtime_config.cpp @@ -36,7 +37,7 @@ set(HEADERS ../roll_forward_option.h ../fx_definition.h ../fx_reference.h - ../fxr/fx_ver.h + ../fxr/static/fx_ver.h ../version.h ../version_compatibility_range.h ../runtime_config.h diff --git a/src/installer/corehost/cli/hostmisc/CMakeLists.txt b/src/installer/corehost/cli/hostmisc/CMakeLists.txt index 1b67c65ef7e784..1b7fb590b7a895 100644 --- a/src/installer/corehost/cli/hostmisc/CMakeLists.txt +++ b/src/installer/corehost/cli/hostmisc/CMakeLists.txt @@ -13,7 +13,7 @@ include_directories(${CMAKE_CURRENT_BINARY_DIR}) set(SOURCES trace.cpp utils.cpp - ../fxr/fx_ver.cpp + ../fxr/static/fx_ver.cpp ) set(HEADERS @@ -21,7 +21,7 @@ set(HEADERS utils.h pal.h ../../error_codes.h - ../fxr/fx_ver.h + ../fxr/static/fx_ver.h ) if(CLR_CMAKE_TARGET_WIN32) diff --git a/src/installer/corehost/cli/hostpolicy/CMakeLists.txt b/src/installer/corehost/cli/hostpolicy/CMakeLists.txt index ca2c78fa27951d..bc22953a2de119 100644 --- a/src/installer/corehost/cli/hostpolicy/CMakeLists.txt +++ b/src/installer/corehost/cli/hostpolicy/CMakeLists.txt @@ -8,6 +8,7 @@ set(DOTNET_PROJECT_NAME "hostpolicy") # Include directories include_directories(../fxr) +include_directories(../fxr_base) include_directories(../json) # CMake does not recommend using globbing since it messes with the freshness checks diff --git a/src/installer/corehost/cli/ijwhost/CMakeLists.txt b/src/installer/corehost/cli/ijwhost/CMakeLists.txt index 46387c0f29299d..d19b4797852483 100644 --- a/src/installer/corehost/cli/ijwhost/CMakeLists.txt +++ b/src/installer/corehost/cli/ijwhost/CMakeLists.txt @@ -8,6 +8,7 @@ set(DOTNET_PROJECT_NAME "ijwhost") # Include directories include_directories(../fxr) +include_directories(../fxr/static) include_directories(${ARCH_SOURCES_DIR}) # CMake does not recommend using globbing since it messes with the freshness checks diff --git a/src/installer/corehost/cli/nethost/CMakeLists.txt b/src/installer/corehost/cli/nethost/CMakeLists.txt index 864129970512cf..1e2688ea2aec0f 100644 --- a/src/installer/corehost/cli/nethost/CMakeLists.txt +++ b/src/installer/corehost/cli/nethost/CMakeLists.txt @@ -8,6 +8,7 @@ set(DOTNET_PROJECT_NAME "nethost") # Include directories include_directories(../fxr) +include_directories(../fxr/static) # CMake does not recommend using globbing since it messes with the freshness checks set(SOURCES diff --git a/src/installer/corehost/cli/test_fx_ver/CMakeLists.txt b/src/installer/corehost/cli/test_fx_ver/CMakeLists.txt index 87778202f25eb2..92472845edbff3 100644 --- a/src/installer/corehost/cli/test_fx_ver/CMakeLists.txt +++ b/src/installer/corehost/cli/test_fx_ver/CMakeLists.txt @@ -6,6 +6,7 @@ project(test_fx_ver) set(EXE_NAME "test_fx_ver") include_directories(../fxr) +include_directories(../fxr/static) include_directories(../hostmisc) set(SOURCES diff --git a/src/installer/corehost/cli/winrthost/CMakeLists.txt b/src/installer/corehost/cli/winrthost/CMakeLists.txt index 9b3e2b44521c54..9d1763835790e7 100644 --- a/src/installer/corehost/cli/winrthost/CMakeLists.txt +++ b/src/installer/corehost/cli/winrthost/CMakeLists.txt @@ -7,6 +7,7 @@ set(DOTNET_PROJECT_NAME "winrthost") # Include directories include_directories(../fxr) +include_directories(../fxr/static) # CMake does not recommend using globbing since it messes with the freshness checks From 06fc02173150470f4f1d93493f91f97205e8220d Mon Sep 17 00:00:00 2001 From: Leandro Pereira Date: Thu, 16 Apr 2020 01:09:41 +0000 Subject: [PATCH 2/5] hostpolicy: Build as a static library This change is part of the work to enable an apphost that bundles both hostfxr and hostpolicy. There's no distinction between hostpolicy that's built as a shared library and as a static library: the shared library is built by linking an empty object file with the static library. --- .../corehost/cli/hostpolicy/CMakeLists.txt | 47 +----------------- .../cli/hostpolicy/shared/CMakeLists.txt | 27 +++++++++++ .../cli/hostpolicy/static/CMakeLists.txt | 48 +++++++++++++++++++ .../cli/hostpolicy/{ => static}/args.cpp | 0 .../cli/hostpolicy/{ => static}/args.h | 0 .../hostpolicy/{ => static}/breadcrumbs.cpp | 0 .../cli/hostpolicy/{ => static}/breadcrumbs.h | 0 .../cli/hostpolicy/{ => static}/coreclr.cpp | 0 .../cli/hostpolicy/{ => static}/coreclr.h | 0 .../hostpolicy/{ => static}/deps_resolver.cpp | 0 .../hostpolicy/{ => static}/deps_resolver.h | 0 .../hostpolicy/{ => static}/hostpolicy.cpp | 0 .../{ => static}/hostpolicy_context.cpp | 0 .../{ => static}/hostpolicy_context.h | 0 .../{ => static}/hostpolicy_init.cpp | 0 .../hostpolicy/{ => static}/hostpolicy_init.h | 0 src/installer/corehost/cli/lib_static.cmake | 6 ++- 17 files changed, 82 insertions(+), 46 deletions(-) create mode 100644 src/installer/corehost/cli/hostpolicy/shared/CMakeLists.txt create mode 100644 src/installer/corehost/cli/hostpolicy/static/CMakeLists.txt rename src/installer/corehost/cli/hostpolicy/{ => static}/args.cpp (100%) rename src/installer/corehost/cli/hostpolicy/{ => static}/args.h (100%) rename src/installer/corehost/cli/hostpolicy/{ => static}/breadcrumbs.cpp (100%) rename src/installer/corehost/cli/hostpolicy/{ => static}/breadcrumbs.h (100%) rename src/installer/corehost/cli/hostpolicy/{ => static}/coreclr.cpp (100%) rename src/installer/corehost/cli/hostpolicy/{ => static}/coreclr.h (100%) rename src/installer/corehost/cli/hostpolicy/{ => static}/deps_resolver.cpp (100%) rename src/installer/corehost/cli/hostpolicy/{ => static}/deps_resolver.h (100%) rename src/installer/corehost/cli/hostpolicy/{ => static}/hostpolicy.cpp (100%) rename src/installer/corehost/cli/hostpolicy/{ => static}/hostpolicy_context.cpp (100%) rename src/installer/corehost/cli/hostpolicy/{ => static}/hostpolicy_context.h (100%) rename src/installer/corehost/cli/hostpolicy/{ => static}/hostpolicy_init.cpp (100%) rename src/installer/corehost/cli/hostpolicy/{ => static}/hostpolicy_init.h (100%) diff --git a/src/installer/corehost/cli/hostpolicy/CMakeLists.txt b/src/installer/corehost/cli/hostpolicy/CMakeLists.txt index bc22953a2de119..20704101148287 100644 --- a/src/installer/corehost/cli/hostpolicy/CMakeLists.txt +++ b/src/installer/corehost/cli/hostpolicy/CMakeLists.txt @@ -2,48 +2,5 @@ # The .NET Foundation licenses this file to you under the MIT license. # See the LICENSE file in the project root for more information. -project(hostpolicy) - -set(DOTNET_PROJECT_NAME "hostpolicy") - -# Include directories -include_directories(../fxr) -include_directories(../fxr_base) -include_directories(../json) - -# CMake does not recommend using globbing since it messes with the freshness checks -set(SOURCES - ./args.cpp - ./breadcrumbs.cpp - ./coreclr.cpp - ./deps_resolver.cpp - ./hostpolicy_context.cpp - ./hostpolicy.cpp - ./hostpolicy_init.cpp - ../bundle/dir_utils.cpp - ../bundle/extractor.cpp - ../bundle/file_entry.cpp - ../bundle/manifest.cpp - ../bundle/runner.cpp -) - -set(HEADERS - ./args.h - ./breadcrumbs.h - ./coreclr.h - ../corehost_context_contract.h - ./deps_resolver.h - ./hostpolicy_context.h - ../hostpolicy.h - ./hostpolicy_init.h - ../bundle/dir_utils.h - ../bundle/extractor.h - ../bundle/file_entry.h - ../bundle/manifest.h - ../bundle/runner.h -) - -include(../lib.cmake) - -install_with_stripped_symbols(hostpolicy TARGETS corehost) -target_link_libraries(hostpolicy libhostcommon) +add_subdirectory(static) +add_subdirectory(shared) \ No newline at end of file diff --git a/src/installer/corehost/cli/hostpolicy/shared/CMakeLists.txt b/src/installer/corehost/cli/hostpolicy/shared/CMakeLists.txt new file mode 100644 index 00000000000000..a2ae28e7a17039 --- /dev/null +++ b/src/installer/corehost/cli/hostpolicy/shared/CMakeLists.txt @@ -0,0 +1,27 @@ +# Licensed to the .NET Foundation under one or more agreements. +# The .NET Foundation licenses this file to you under the MIT license. +# See the LICENSE file in the project root for more information. + +project(hostpolicy) + +set(DOTNET_PROJECT_NAME "hostpolicy") + +# CMake does not recommend using globbing since it messes with the freshness checks + +# Can't call add_library() without source files. Create an empty .c file, +# then link with the static library just recently built. +if (NOT EXISTS "${CMAKE_CURRENT_BINARY_DIR}/empty.cpp") + file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/empty.cpp" "") +endif () + +set(SOURCES + ${CMAKE_CURRENT_BINARY_DIR}/empty.cpp +) + +set(HEADERS +) + +include(../../lib.cmake) + +install_with_stripped_symbols(hostpolicy TARGETS corehost) +target_link_libraries(hostpolicy libhostcommon libhostpolicy_static) diff --git a/src/installer/corehost/cli/hostpolicy/static/CMakeLists.txt b/src/installer/corehost/cli/hostpolicy/static/CMakeLists.txt new file mode 100644 index 00000000000000..f683ce7319d6bd --- /dev/null +++ b/src/installer/corehost/cli/hostpolicy/static/CMakeLists.txt @@ -0,0 +1,48 @@ +# Licensed to the .NET Foundation under one or more agreements. +# The .NET Foundation licenses this file to you under the MIT license. +# See the LICENSE file in the project root for more information. + +project(hostpolicy_static) + +set(DOTNET_PROJECT_NAME "hostpolicy_static") + +# Include directories +include_directories(../../fxr/shared) +include_directories(../../fxr/static) +include_directories(../../json) + +# CMake does not recommend using globbing since it messes with the freshness checks +set(SOURCES + ./args.cpp + ./breadcrumbs.cpp + ./coreclr.cpp + ./deps_resolver.cpp + ./hostpolicy_context.cpp + ./hostpolicy.cpp + ./hostpolicy_init.cpp + ../../bundle/dir_utils.cpp + ../../bundle/extractor.cpp + ../../bundle/file_entry.cpp + ../../bundle/manifest.cpp + ../../bundle/runner.cpp +) + +set(HEADERS + ./args.h + ./breadcrumbs.h + ./coreclr.h + ../../corehost_context_contract.h + ./deps_resolver.h + ./hostpolicy_context.h + ../../hostpolicy.h + ./hostpolicy_init.h + ../../bundle/dir_utils.h + ../../bundle/extractor.h + ../../bundle/file_entry.h + ../../bundle/manifest.h + ../../bundle/runner.h +) + +set(SKIP_VERSIONING 1) +set(BUILD_OBJECT_LIBRARY 1) +include(../../lib_static.cmake) diff --git a/src/installer/corehost/cli/hostpolicy/args.cpp b/src/installer/corehost/cli/hostpolicy/static/args.cpp similarity index 100% rename from src/installer/corehost/cli/hostpolicy/args.cpp rename to src/installer/corehost/cli/hostpolicy/static/args.cpp diff --git a/src/installer/corehost/cli/hostpolicy/args.h b/src/installer/corehost/cli/hostpolicy/static/args.h similarity index 100% rename from src/installer/corehost/cli/hostpolicy/args.h rename to src/installer/corehost/cli/hostpolicy/static/args.h diff --git a/src/installer/corehost/cli/hostpolicy/breadcrumbs.cpp b/src/installer/corehost/cli/hostpolicy/static/breadcrumbs.cpp similarity index 100% rename from src/installer/corehost/cli/hostpolicy/breadcrumbs.cpp rename to src/installer/corehost/cli/hostpolicy/static/breadcrumbs.cpp diff --git a/src/installer/corehost/cli/hostpolicy/breadcrumbs.h b/src/installer/corehost/cli/hostpolicy/static/breadcrumbs.h similarity index 100% rename from src/installer/corehost/cli/hostpolicy/breadcrumbs.h rename to src/installer/corehost/cli/hostpolicy/static/breadcrumbs.h diff --git a/src/installer/corehost/cli/hostpolicy/coreclr.cpp b/src/installer/corehost/cli/hostpolicy/static/coreclr.cpp similarity index 100% rename from src/installer/corehost/cli/hostpolicy/coreclr.cpp rename to src/installer/corehost/cli/hostpolicy/static/coreclr.cpp diff --git a/src/installer/corehost/cli/hostpolicy/coreclr.h b/src/installer/corehost/cli/hostpolicy/static/coreclr.h similarity index 100% rename from src/installer/corehost/cli/hostpolicy/coreclr.h rename to src/installer/corehost/cli/hostpolicy/static/coreclr.h diff --git a/src/installer/corehost/cli/hostpolicy/deps_resolver.cpp b/src/installer/corehost/cli/hostpolicy/static/deps_resolver.cpp similarity index 100% rename from src/installer/corehost/cli/hostpolicy/deps_resolver.cpp rename to src/installer/corehost/cli/hostpolicy/static/deps_resolver.cpp diff --git a/src/installer/corehost/cli/hostpolicy/deps_resolver.h b/src/installer/corehost/cli/hostpolicy/static/deps_resolver.h similarity index 100% rename from src/installer/corehost/cli/hostpolicy/deps_resolver.h rename to src/installer/corehost/cli/hostpolicy/static/deps_resolver.h diff --git a/src/installer/corehost/cli/hostpolicy/hostpolicy.cpp b/src/installer/corehost/cli/hostpolicy/static/hostpolicy.cpp similarity index 100% rename from src/installer/corehost/cli/hostpolicy/hostpolicy.cpp rename to src/installer/corehost/cli/hostpolicy/static/hostpolicy.cpp diff --git a/src/installer/corehost/cli/hostpolicy/hostpolicy_context.cpp b/src/installer/corehost/cli/hostpolicy/static/hostpolicy_context.cpp similarity index 100% rename from src/installer/corehost/cli/hostpolicy/hostpolicy_context.cpp rename to src/installer/corehost/cli/hostpolicy/static/hostpolicy_context.cpp diff --git a/src/installer/corehost/cli/hostpolicy/hostpolicy_context.h b/src/installer/corehost/cli/hostpolicy/static/hostpolicy_context.h similarity index 100% rename from src/installer/corehost/cli/hostpolicy/hostpolicy_context.h rename to src/installer/corehost/cli/hostpolicy/static/hostpolicy_context.h diff --git a/src/installer/corehost/cli/hostpolicy/hostpolicy_init.cpp b/src/installer/corehost/cli/hostpolicy/static/hostpolicy_init.cpp similarity index 100% rename from src/installer/corehost/cli/hostpolicy/hostpolicy_init.cpp rename to src/installer/corehost/cli/hostpolicy/static/hostpolicy_init.cpp diff --git a/src/installer/corehost/cli/hostpolicy/hostpolicy_init.h b/src/installer/corehost/cli/hostpolicy/static/hostpolicy_init.h similarity index 100% rename from src/installer/corehost/cli/hostpolicy/hostpolicy_init.h rename to src/installer/corehost/cli/hostpolicy/static/hostpolicy_init.h diff --git a/src/installer/corehost/cli/lib_static.cmake b/src/installer/corehost/cli/lib_static.cmake index 8135e9c19e0e09..00204df3ce9f40 100644 --- a/src/installer/corehost/cli/lib_static.cmake +++ b/src/installer/corehost/cli/lib_static.cmake @@ -10,7 +10,11 @@ add_definitions(-D_NO_ASYNCRTIMP) add_definitions(-D_NO_PPLXIMP) add_definitions(-DEXPORT_SHARED_API=1) -add_library(lib${DOTNET_PROJECT_NAME} STATIC ${SOURCES} ${RESOURCES}) +if (BUILD_OBJECT_LIBRARY) + add_library(lib${DOTNET_PROJECT_NAME} OBJECT ${SOURCES} ${RESOURCES}) +else () + add_library(lib${DOTNET_PROJECT_NAME} STATIC ${SOURCES} ${RESOURCES}) +endif () set_target_properties(lib${DOTNET_PROJECT_NAME} PROPERTIES MACOSX_RPATH TRUE) set_target_properties(lib${DOTNET_PROJECT_NAME} PROPERTIES PREFIX "") From 3866534051478735ab6833cd8bcf83b653a0a322 Mon Sep 17 00:00:00 2001 From: Leandro Pereira Date: Thu, 16 Apr 2020 20:48:08 +0000 Subject: [PATCH 3/5] corehost: Allow linking of hostfxr and hostpolicy with apphost Provide a hostfxr_iface class, that abstracts how the hostfxr functions called by the early stage in the hosting layer is resolved. --- src/installer/corehost/cli/exe.cmake | 3 ++ src/installer/corehost/corehost.cpp | 60 +++++++++++--------------- src/installer/corehost/hostfxr_iface.h | 40 +++++++++++++++++ 3 files changed, 69 insertions(+), 34 deletions(-) create mode 100644 src/installer/corehost/hostfxr_iface.h diff --git a/src/installer/corehost/cli/exe.cmake b/src/installer/corehost/cli/exe.cmake index d713c9bd021e96..06846506bbcfc8 100644 --- a/src/installer/corehost/cli/exe.cmake +++ b/src/installer/corehost/cli/exe.cmake @@ -18,6 +18,9 @@ list(APPEND SOURCES ${CMAKE_CURRENT_LIST_DIR}/fxr_resolver.cpp ${CMAKE_CURRENT_LIST_DIR}/../corehost.cpp ) +list(APPEND HEADERS + ${CMAKE_CURRENT_LIST_DIR}/../hostfxr_iface.h +) add_executable(${DOTNET_PROJECT_NAME} ${SOURCES} ${RESOURCES}) target_link_libraries(${DOTNET_PROJECT_NAME} libhostmisc) diff --git a/src/installer/corehost/corehost.cpp b/src/installer/corehost/corehost.cpp index 55b8ed629827ce..bd73110e837f20 100644 --- a/src/installer/corehost/corehost.cpp +++ b/src/installer/corehost/corehost.cpp @@ -9,6 +9,7 @@ #include "fx_ver.h" #include "trace.h" #include "utils.h" +#include "hostfxr_iface.h" #if defined(FEATURE_APPHOST) #include "bundle_marker.h" @@ -176,51 +177,41 @@ int exe_start(const int argc, const pal::char_t* argv[]) app_path.append(_X(".dll")); #endif - pal::string_t dotnet_root; - pal::string_t fxr_path; - if (!fxr_resolver::try_get_path(app_root, &dotnet_root, &fxr_path)) - { - return StatusCode::CoreHostLibMissingFailure; - } + hostfxr fxr{app_root}; - // Load library - pal::dll_t fxr; - if (!pal::load_library(&fxr_path, &fxr)) + // Obtain the entrypoints. + int rc = fxr.status_code(); + if (rc != StatusCode::Success) { - trace::error(_X("The library %s was found, but loading it from %s failed"), LIBFXR_NAME, fxr_path.c_str()); - trace::error(_X(" - Installing .NET prerequisites might help resolve this problem.")); - trace::error(_X(" %s"), DOTNET_CORE_INSTALL_PREREQUISITES_URL); - return StatusCode::CoreHostLibLoadFailure; + return rc; } - // Obtain the entrypoints. - int rc; #if defined(FEATURE_APPHOST) if (bundle_marker_t::is_bundle()) { - hostfxr_main_bundle_startupinfo_fn hostfxr_main_bundle_startupinfo = reinterpret_cast(pal::get_symbol(fxr, "hostfxr_main_bundle_startupinfo")); + auto hostfxr_main_bundle_startupinfo = fxr.resolve_main_bundle_startupinfo(); if (hostfxr_main_bundle_startupinfo != nullptr) { const pal::char_t* host_path_cstr = host_path.c_str(); - const pal::char_t* dotnet_root_cstr = dotnet_root.empty() ? nullptr : dotnet_root.c_str(); + const pal::char_t* dotnet_root_cstr = fxr.dotnet_root().empty() ? nullptr : fxr.dotnet_root().c_str(); const pal::char_t* app_path_cstr = app_path.empty() ? nullptr : app_path.c_str(); int64_t bundle_header_offset = bundle_marker_t::header_offset(); - trace::info(_X("Invoking fx resolver [%s] hostfxr_main_bundle_startupinfo"), fxr_path.c_str()); + trace::info(_X("Invoking fx resolver [%s] hostfxr_main_bundle_startupinfo"), fxr.fxr_path().c_str()); trace::info(_X("Host path: [%s]"), host_path.c_str()); - trace::info(_X("Dotnet path: [%s]"), dotnet_root.c_str()); + trace::info(_X("Dotnet path: [%s]"), fxr.dotnet_root().c_str()); trace::info(_X("App path: [%s]"), app_path.c_str()); trace::info(_X("Bundle Header Offset: [%lx]"), bundle_header_offset); - hostfxr_set_error_writer_fn set_error_writer_fn = reinterpret_cast(pal::get_symbol(fxr, "hostfxr_set_error_writer")); - propagate_error_writer_t propagate_error_writer_to_hostfxr(set_error_writer_fn); + auto set_error_writer = fxr.resolve_set_error_writer(); + propagate_error_writer_t propagate_error_writer_to_hostfxr(set_error_writer); rc = hostfxr_main_bundle_startupinfo(argc, argv, host_path_cstr, dotnet_root_cstr, app_path_cstr, bundle_header_offset); } else { // The host components will be statically linked with the app-host: https://github.com/dotnet/runtime/issues/32823 // Once this work is completed, an outdated hostfxr can only be found for framework-related apps. - trace::error(_X("The required library %s does not support single-file apps."), fxr_path.c_str()); + trace::error(_X("The required library %s does not support single-file apps."), fxr.fxr_path().c_str()); need_newer_framework_error(); rc = StatusCode::FrameworkMissingFailure; } @@ -228,60 +219,61 @@ int exe_start(const int argc, const pal::char_t* argv[]) else #endif // defined(FEATURE_APPHOST) { - hostfxr_main_startupinfo_fn hostfxr_main_startupinfo = reinterpret_cast(pal::get_symbol(fxr, "hostfxr_main_startupinfo")); + auto hostfxr_main_startupinfo = fxr.resolve_main_startupinfo(); if (hostfxr_main_startupinfo != nullptr) { const pal::char_t* host_path_cstr = host_path.c_str(); - const pal::char_t* dotnet_root_cstr = dotnet_root.empty() ? nullptr : dotnet_root.c_str(); + const pal::char_t* dotnet_root_cstr = fxr.dotnet_root().empty() ? nullptr : fxr.dotnet_root().c_str(); const pal::char_t* app_path_cstr = app_path.empty() ? nullptr : app_path.c_str(); - trace::info(_X("Invoking fx resolver [%s] hostfxr_main_startupinfo"), fxr_path.c_str()); + trace::info(_X("Invoking fx resolver [%s] hostfxr_main_startupinfo"), fxr.fxr_path().c_str()); trace::info(_X("Host path: [%s]"), host_path.c_str()); - trace::info(_X("Dotnet path: [%s]"), dotnet_root.c_str()); + trace::info(_X("Dotnet path: [%s]"), fxr.dotnet_root().c_str()); trace::info(_X("App path: [%s]"), app_path.c_str()); - hostfxr_set_error_writer_fn set_error_writer_fn = reinterpret_cast(pal::get_symbol(fxr, "hostfxr_set_error_writer")); - propagate_error_writer_t propagate_error_writer_to_hostfxr(set_error_writer_fn); + auto set_error_writer = fxr.resolve_set_error_writer(); + propagate_error_writer_t propagate_error_writer_to_hostfxr(set_error_writer); rc = hostfxr_main_startupinfo(argc, argv, host_path_cstr, dotnet_root_cstr, app_path_cstr); // This check exists to provide an error message for UI apps when running 3.0 apps on 2.0 only hostfxr, which doesn't support error writer redirection. - if (trace::get_error_writer() != nullptr && rc == static_cast(StatusCode::FrameworkMissingFailure) && !set_error_writer_fn) + if (trace::get_error_writer() != nullptr && rc == static_cast(StatusCode::FrameworkMissingFailure) && set_error_writer == nullptr) { need_newer_framework_error(); } } +#if !defined(FEATURE_STATIC_HOST) else { if (requires_hostfxr_startupinfo_interface) { - trace::error(_X("The required library %s does not support relative app dll paths."), fxr_path.c_str()); + trace::error(_X("The required library %s does not support relative app dll paths."), fxr.fxr_path().c_str()); rc = StatusCode::CoreHostEntryPointFailure; } else { - trace::info(_X("Invoking fx resolver [%s] v1"), fxr_path.c_str()); + trace::info(_X("Invoking fx resolver [%s] v1"), fxr.fxr_path().c_str()); // Previous corehost trace messages must be printed before calling trace::setup in hostfxr trace::flush(); // For compat, use the v1 interface. This requires additional file I\O to re-parse parameters and // for apphost, does not support DOTNET_ROOT or dll with different name for exe. - hostfxr_main_fn main_fn_v1 = reinterpret_cast(pal::get_symbol(fxr, "hostfxr_main")); + auto main_fn_v1 = fxr.resolve_main_v1(); if (main_fn_v1 != nullptr) { rc = main_fn_v1(argc, argv); } else { - trace::error(_X("The required library %s does not contain the expected entry point."), fxr_path.c_str()); + trace::error(_X("The required library %s does not contain the expected entry point."), fxr.fxr_path().c_str()); rc = StatusCode::CoreHostEntryPointFailure; } } } +#endif // defined(FEATURE_STATIC_HOST) } - pal::unload_library(fxr); return rc; } diff --git a/src/installer/corehost/hostfxr_iface.h b/src/installer/corehost/hostfxr_iface.h new file mode 100644 index 00000000000000..93acb8fef201cb --- /dev/null +++ b/src/installer/corehost/hostfxr_iface.h @@ -0,0 +1,40 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +#ifndef __HOSTFXR_IFACE_H__ +#define __HOSTFXR_IFACE_H__ + +#include "hostfxr.h" +#include "pal.h" +#include "error_codes.h" + +class hostfxr +{ + public: + hostfxr(const pal::string_t& app_root); + ~hostfxr(); + + StatusCode status_code() const { return m_status_code; } + + const pal::string_t& host_path() const { return m_host_path; } + const pal::string_t& dotnet_root() const { return m_dotnet_root; } + const pal::string_t& fxr_path() const { return m_fxr_path; } + + hostfxr_main_bundle_startupinfo_fn resolve_main_bundle_startupinfo(); + hostfxr_set_error_writer_fn resolve_set_error_writer(); + hostfxr_main_startupinfo_fn resolve_main_startupinfo(); + hostfxr_main_fn resolve_main_v1(); + + private: + pal::dll_t m_hostfxr_dll{nullptr}; + + pal::string_t m_host_path; + pal::string_t m_dotnet_root; + pal::string_t m_fxr_path; + + bool m_requires_startupinfo_iface{false}; + StatusCode m_status_code; +}; + +#endif // __HOSTFXR_IFACE_H__ From df43b0e0647010da26bf94487b99861f0cf11b95 Mon Sep 17 00:00:00 2001 From: Leandro Pereira Date: Thu, 16 Apr 2020 22:21:18 +0000 Subject: [PATCH 4/5] dotnet: Teach the muxer binary about hostfxr_iface --- src/installer/corehost/cli/dotnet/CMakeLists.txt | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/installer/corehost/cli/dotnet/CMakeLists.txt b/src/installer/corehost/cli/dotnet/CMakeLists.txt index 4527986acfe3c6..8bf75e8cff873e 100644 --- a/src/installer/corehost/cli/dotnet/CMakeLists.txt +++ b/src/installer/corehost/cli/dotnet/CMakeLists.txt @@ -10,4 +10,8 @@ if(CLR_CMAKE_TARGET_WIN32) dotnet.manifest) endif() +list(APPEND SOURCES + ../apphost/shared/hostfxr_iface.cpp +) + include(../exe.cmake) From 69687f82d73353e3197e1233417512c4f98e1c07 Mon Sep 17 00:00:00 2001 From: Leandro Pereira Date: Thu, 16 Apr 2020 20:47:35 +0000 Subject: [PATCH 5/5] apphost: Teach apphost about hostfxr_iface This provides two implementations of hostfxr_iface: one for the static apphost, which bundles hostfxr and hostpolicy, and another for the conventional apphost, which loads them dynamically on startup. --- .../corehost/cli/apphost/CMakeLists.txt | 48 +------------- .../cli/apphost/shared/CMakeLists.txt | 54 ++++++++++++++++ .../cli/apphost/shared/hostfxr_iface.cpp | 61 ++++++++++++++++++ .../cli/apphost/static/CMakeLists.txt | 61 ++++++++++++++++++ .../cli/apphost/static/hostfxr_iface.cpp | 63 +++++++++++++++++++ 5 files changed, 241 insertions(+), 46 deletions(-) create mode 100644 src/installer/corehost/cli/apphost/shared/CMakeLists.txt create mode 100644 src/installer/corehost/cli/apphost/shared/hostfxr_iface.cpp create mode 100644 src/installer/corehost/cli/apphost/static/CMakeLists.txt create mode 100644 src/installer/corehost/cli/apphost/static/hostfxr_iface.cpp diff --git a/src/installer/corehost/cli/apphost/CMakeLists.txt b/src/installer/corehost/cli/apphost/CMakeLists.txt index ba01e32ec47c74..886c9c33d996bb 100644 --- a/src/installer/corehost/cli/apphost/CMakeLists.txt +++ b/src/installer/corehost/cli/apphost/CMakeLists.txt @@ -2,49 +2,5 @@ # The .NET Foundation licenses this file to you under the MIT license. # See the LICENSE file in the project root for more information. -project(apphost) -set(DOTNET_PROJECT_NAME "apphost") - -# Add RPATH to the apphost binary that allows using local copies of shared libraries -# dotnet core depends on for special scenarios when system wide installation of such -# dependencies is not possible for some reason. -# This cannot be enabled for MacOS (Darwin) since its RPATH works in a different way, -# doesn't apply to libraries loaded via dlopen and most importantly, it is not transitive. -if (NOT CLR_CMAKE_TARGET_OSX) - set(CMAKE_BUILD_WITH_INSTALL_RPATH TRUE) - set(CMAKE_INSTALL_RPATH "\$ORIGIN/netcoredeps") -endif() - -set(SKIP_VERSIONING 1) - -set(SOURCES - ./bundle_marker.cpp -) - -set(HEADERS - ./bundle_marker.h -) - -if(CLR_CMAKE_TARGET_WIN32) - list(APPEND SOURCES - apphost.windows.cpp) - - list(APPEND HEADERS - apphost.windows.h) -endif() - -include(../exe.cmake) - -add_definitions(-DFEATURE_APPHOST=1) - -# Disable manifest generation into the file .exe on Windows -if(CLR_CMAKE_TARGET_WIN32) - set_property(TARGET ${PROJECT_NAME} PROPERTY - LINK_FLAGS "/MANIFEST:NO" - ) -endif() - -# Specify non-default Windows libs to be used for Arm/Arm64 builds -if (CLR_CMAKE_TARGET_WIN32 AND (CLR_CMAKE_TARGET_ARCH_ARM OR CLR_CMAKE_TARGET_ARCH_ARM64)) - target_link_libraries(apphost Advapi32.lib shell32.lib) -endif() +add_subdirectory(static) +add_subdirectory(shared) diff --git a/src/installer/corehost/cli/apphost/shared/CMakeLists.txt b/src/installer/corehost/cli/apphost/shared/CMakeLists.txt new file mode 100644 index 00000000000000..e760a857b0eebb --- /dev/null +++ b/src/installer/corehost/cli/apphost/shared/CMakeLists.txt @@ -0,0 +1,54 @@ +# Licensed to the .NET Foundation under one or more agreements. +# The .NET Foundation licenses this file to you under the MIT license. +# See the LICENSE file in the project root for more information. + +project(apphost) +set(DOTNET_PROJECT_NAME "apphost") + +# Add RPATH to the apphost binary that allows using local copies of shared libraries +# dotnet core depends on for special scenarios when system wide installation of such +# dependencies is not possible for some reason. +# This cannot be enabled for MacOS (Darwin) since its RPATH works in a different way, +# doesn't apply to libraries loaded via dlopen and most importantly, it is not transitive. +if (NOT CLR_CMAKE_TARGET_OSX) + set(CMAKE_BUILD_WITH_INSTALL_RPATH TRUE) + set(CMAKE_INSTALL_RPATH "\$ORIGIN/netcoredeps") +endif() + +set(SKIP_VERSIONING 1) + +include_directories(..) + +set(SOURCES + ../bundle_marker.cpp + ./hostfxr_iface.cpp +) + +set(HEADERS + ../bundle_marker.h + ../../../hostfxr_iface.h +) + +if(CLR_CMAKE_TARGET_WIN32) + list(APPEND SOURCES + ../apphost.windows.cpp) + + list(APPEND HEADERS + ../apphost.windows.h) +endif() + +include(../../exe.cmake) + +add_definitions(-DFEATURE_APPHOST=1) + +# Disable manifest generation into the file .exe on Windows +if(CLR_CMAKE_TARGET_WIN32) + set_property(TARGET ${PROJECT_NAME} PROPERTY + LINK_FLAGS "/MANIFEST:NO" + ) +endif() + +# Specify non-default Windows libs to be used for Arm/Arm64 builds +if (CLR_CMAKE_TARGET_WIN32 AND (CLR_CMAKE_TARGET_ARCH_ARM OR CLR_CMAKE_TARGET_ARCH_ARM64)) + target_link_libraries(apphost Advapi32.lib shell32.lib) +endif() diff --git a/src/installer/corehost/cli/apphost/shared/hostfxr_iface.cpp b/src/installer/corehost/cli/apphost/shared/hostfxr_iface.cpp new file mode 100644 index 00000000000000..044d3bf831a1d5 --- /dev/null +++ b/src/installer/corehost/cli/apphost/shared/hostfxr_iface.cpp @@ -0,0 +1,61 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +#include + +#include "pal.h" +#include "fxr_resolver.h" +#include "trace.h" +#include "hostfxr_iface.h" + +hostfxr_main_bundle_startupinfo_fn hostfxr::resolve_main_bundle_startupinfo() +{ + assert(m_hostfxr_dll != nullptr); + return reinterpret_cast(pal::get_symbol(m_hostfxr_dll, "hostfxr_main_bundle_startupinfo")); +} + +hostfxr_set_error_writer_fn hostfxr::resolve_set_error_writer() +{ + assert(m_hostfxr_dll != nullptr); + return reinterpret_cast(pal::get_symbol(m_hostfxr_dll, "hostfxr_set_error_writer")); +} + +hostfxr_main_startupinfo_fn hostfxr::resolve_main_startupinfo() +{ + assert(m_hostfxr_dll != nullptr); + return reinterpret_cast(pal::get_symbol(m_hostfxr_dll, "hostfxr_main_startupinfo")); +} + +hostfxr_main_fn hostfxr::resolve_main_v1() +{ + assert(m_hostfxr_dll != nullptr); + return reinterpret_cast(pal::get_symbol(m_hostfxr_dll, "hostfxr_main")); +} + +hostfxr::hostfxr(const pal::string_t& app_root) +{ + if (!fxr_resolver::try_get_path(app_root, &m_dotnet_root, &m_fxr_path)) + { + m_status_code = StatusCode::CoreHostLibMissingFailure; + } + else if (pal::load_library(&m_fxr_path, &m_hostfxr_dll)) + { + m_status_code = StatusCode::Success; + } + else + { + trace::error(_X("The library %s was found, but loading it from %s failed"), LIBFXR_NAME, m_fxr_path.c_str()); + trace::error(_X(" - Installing .NET prerequisites might help resolve this problem.")); + trace::error(_X(" %s"), DOTNET_CORE_INSTALL_PREREQUISITES_URL); + m_status_code = StatusCode::CoreHostLibLoadFailure; + } +} + +hostfxr::~hostfxr() +{ + if (m_hostfxr_dll != nullptr) + { + pal::unload_library(m_hostfxr_dll); + } +} diff --git a/src/installer/corehost/cli/apphost/static/CMakeLists.txt b/src/installer/corehost/cli/apphost/static/CMakeLists.txt new file mode 100644 index 00000000000000..8e0119720a682b --- /dev/null +++ b/src/installer/corehost/cli/apphost/static/CMakeLists.txt @@ -0,0 +1,61 @@ +# Licensed to the .NET Foundation under one or more agreements. +# The .NET Foundation licenses this file to you under the MIT license. +# See the LICENSE file in the project root for more information. + +project(static_apphost) +set(DOTNET_PROJECT_NAME "static_apphost") + +# Add RPATH to the apphost binary that allows using local copies of shared libraries +# dotnet core depends on for special scenarios when system wide installation of such +# dependencies is not possible for some reason. +# This cannot be enabled for MacOS (Darwin) since its RPATH works in a different way, +# doesn't apply to libraries loaded via dlopen and most importantly, it is not transitive. +if (NOT CLR_CMAKE_TARGET_OSX) + set(CMAKE_BUILD_WITH_INSTALL_RPATH TRUE) + set(CMAKE_INSTALL_RPATH "\$ORIGIN/netcoredeps") +endif() + +set(SKIP_VERSIONING 1) + +include_directories(..) + +set(SOURCES + ../bundle_marker.cpp + ./hostfxr_iface.cpp +) + +set(HEADERS + ../bundle_marker.h + ../../../hostfxr_iface.h +) + +if(CLR_CMAKE_TARGET_WIN32) + list(APPEND SOURCES + ../apphost.windows.cpp) + + list(APPEND HEADERS + ../apphost.windows.h) +endif() + +include(../../exe.cmake) + +add_definitions(-DFEATURE_APPHOST=1) +add_definitions(-DFEATURE_STATIC_HOST=1) + +# Disable manifest generation into the file .exe on Windows +if(CLR_CMAKE_TARGET_WIN32) + set_property(TARGET ${PROJECT_NAME} PROPERTY + LINK_FLAGS "/MANIFEST:NO" + ) +endif() + +# Specify non-default Windows libs to be used for Arm/Arm64 builds +if (CLR_CMAKE_TARGET_WIN32 AND (CLR_CMAKE_TARGET_ARCH_ARM OR CLR_CMAKE_TARGET_ARCH_ARM64)) + target_link_libraries(static_apphost Advapi32.lib shell32.lib) +endif() + +target_link_libraries(static_apphost + libhostfxr_static + libhostpolicy_static + libhostcommon +) diff --git a/src/installer/corehost/cli/apphost/static/hostfxr_iface.cpp b/src/installer/corehost/cli/apphost/static/hostfxr_iface.cpp new file mode 100644 index 00000000000000..83dfa626aa98f8 --- /dev/null +++ b/src/installer/corehost/cli/apphost/static/hostfxr_iface.cpp @@ -0,0 +1,63 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +#include +#include "trace.h" +#include "hostfxr.h" +#include "hostfxr_iface.h" + +extern "C" +{ + int HOSTFXR_CALLTYPE hostfxr_main_bundle_startupinfo(const int argc, const pal::char_t* argv[], const pal::char_t* host_path, const pal::char_t* dotnet_root, const pal::char_t* app_path, int64_t bundle_header_offset); + int HOSTFXR_CALLTYPE hostfxr_main_startupinfo(const int argc, const pal::char_t* argv[], const pal::char_t* host_path, const pal::char_t* dotnet_root, const pal::char_t* app_path); + int HOSTFXR_CALLTYPE hostfxr_main(const int argc, const pal::char_t* argv[]); + hostfxr_error_writer_fn HOSTFXR_CALLTYPE hostfxr_set_error_writer(hostfxr_error_writer_fn error_writer); +} + +hostfxr_main_bundle_startupinfo_fn hostfxr::resolve_main_bundle_startupinfo() +{ + assert(m_hostfxr_dll == nullptr); + return hostfxr_main_bundle_startupinfo; +} + +hostfxr_set_error_writer_fn hostfxr::resolve_set_error_writer() +{ + assert(m_hostfxr_dll == nullptr); + return hostfxr_set_error_writer; +} + +hostfxr_main_startupinfo_fn hostfxr::resolve_main_startupinfo() +{ + assert(m_hostfxr_dll == nullptr); + return hostfxr_main_startupinfo; +} + +hostfxr_main_fn hostfxr::resolve_main_v1() +{ + assert(m_hostfxr_dll == nullptr); + assert(!"This function should not be called in a static host"); + return nullptr; +} + +hostfxr::hostfxr(const pal::string_t& app_root) +{ + if (app_root.length() == 0) + { + trace::info(_X("Application root path is empty. This shouldn't happen")); + m_status_code = StatusCode::CoreHostLibMissingFailure; + } + else + { + trace::info(_X("Using internal fxr")); + + m_dotnet_root.assign(app_root); + m_fxr_path.assign(app_root); + + m_status_code = StatusCode::Success; + } +} + +hostfxr::~hostfxr() +{ +}