Skip to content

Commit

Permalink
Rewrite the corehost CMake system to be more "Modern CMake"y and less…
Browse files Browse the repository at this point in the history
… MSBuild-y (#102475)

Co-authored-by: Elinor Fung <elfung@microsoft.com>
  • Loading branch information
jkoritzinsky and elinor-fung authored Jun 5, 2024
1 parent 9b74f42 commit 8f2ca60
Show file tree
Hide file tree
Showing 35 changed files with 303 additions and 388 deletions.
65 changes: 65 additions & 0 deletions src/native/corehost/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ project(corehost)
include(../../../eng/native/configurepaths.cmake)
include(${CLR_ENG_NATIVE_DIR}/configurecompiler.cmake)

set(COREHOST_ROOT_DIR "${CMAKE_CURRENT_SOURCE_DIR}")

if (MSVC)
# Host components don't try to handle asynchronous exceptions
set_property(DIRECTORY PROPERTY CLR_EH_OPTION /EHsc)
Expand All @@ -19,7 +21,70 @@ elseif (CMAKE_CXX_COMPILER_ID MATCHES GNU)
add_compile_options($<$<COMPILE_LANGUAGE:CXX>:-fno-use-cxa-atexit>)
endif()

set(CMAKE_INCLUDE_CURRENT_DIR ON)
include_directories(${CMAKE_CURRENT_BINARY_DIR})
include_directories(${CMAKE_CURRENT_SOURCE_DIR})
include_directories(${CLR_ARTIFACTS_OBJ_DIR}) # Generated version files

if (NOT ${CLR_SINGLE_FILE_HOST_ONLY})
if("${CLI_CMAKE_PKG_RID}" STREQUAL "")
message(FATAL_ERROR "A minimum supported package rid is not specified (ex: win7-x86 or ubuntu.14.04-x64, osx.10.12-x64, rhel.7-x64)")
endif()
if("${CLI_CMAKE_COMMIT_HASH}" STREQUAL "")
message(FATAL_ERROR "Commit hash needs to be specified to build the host")
endif()
endif()

if("${CLI_CMAKE_FALLBACK_OS}" STREQUAL "")
message(FATAL_ERROR "Fallback rid needs to be specified to build the host")
endif()

if("${CLI_CMAKE_FALLBACK_OS}" STREQUAL "${CLR_CMAKE_TARGET_OS}")
add_compile_definitions(FALLBACK_OS_IS_SAME_AS_TARGET_OS)
endif()

# Find support libraries that we need if they're present on the system.
find_library(PTHREAD_LIB pthread)

# Prefer libatomic.a over libatomic.so.
set(_current_CMAKE_FIND_LIBRARY_SUFFIXES ${CMAKE_FIND_LIBRARY_SUFFIXES})
list(PREPEND CMAKE_FIND_LIBRARY_SUFFIXES .a)
find_library(ATOMIC_SUPPORT_LIB atomic)
set(CMAKE_FIND_LIBRARY_SUFFIXES ${_current_CMAKE_FIND_LIBRARY_SUFFIXES})
unset(_current_CMAKE_FIND_LIBRARY_SUFFIXES)

configure_file(configure.h.in ${CMAKE_CURRENT_BINARY_DIR}/configure.h)

# add_version_info_to_target(targetName [resourceDirName])
function(add_version_info_to_target targetName)
set(RESOURCE_INCLUDE_DIR ${targetName})
if (${ARGC} GREATER 1)
set(RESOURCE_INCLUDE_DIR ${ARGV1})
endif()

if (CLR_CMAKE_TARGET_WIN32)
target_sources(${targetName} PRIVATE ${CMAKE_CURRENT_FUNCTION_LIST_DIR}/native.rc)
set_property(SOURCE ${CMAKE_CURRENT_FUNCTION_LIST_DIR}/native.rc
TARGET_DIRECTORY ${targetName}
APPEND PROPERTY INCLUDE_DIRECTORIES
${CLI_CMAKE_RESOURCE_DIR}/${RESOURCE_INCLUDE_DIR})
else()
target_sources(${targetName} PRIVATE ${VERSION_FILE_PATH})
endif()
endfunction()

# This is required to map a symbol reference to a matching definition local to the module (.so)
# containing the reference instead of using definitions from other modules.
if(CLR_CMAKE_TARGET_LINUX OR CLR_CMAKE_TARGET_SUNOS)
add_link_options(LINKER:-Bsymbolic)
endif()

add_library(fxr_resolver INTERFACE)
target_sources(fxr_resolver INTERFACE fxr_resolver.cpp)
target_include_directories(fxr_resolver INTERFACE fxr)

add_subdirectory(hostcommon)
add_subdirectory(hostmisc)
add_subdirectory(fxr)
add_subdirectory(hostpolicy)

Expand Down
35 changes: 21 additions & 14 deletions src/native/corehost/apphost/standalone/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
# Licensed to the .NET Foundation under one or more agreements.
# The .NET Foundation licenses this file to you under the MIT license.

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.
Expand All @@ -14,13 +11,12 @@ if (NOT CLR_CMAKE_TARGET_OSX)
set(CMAKE_INSTALL_RPATH "\$ORIGIN/netcoredeps")
endif()

set(SKIP_VERSIONING 1)

include_directories(..)

set(SOURCES
../bundle_marker.cpp
./hostfxr_resolver.cpp
../../corehost.cpp
)

set(HEADERS
Expand All @@ -29,28 +25,39 @@ set(HEADERS
)

if(CLR_CMAKE_TARGET_WIN32)
add_definitions(-DUNICODE)
add_compile_definitions(UNICODE)
list(APPEND SOURCES
../apphost.windows.cpp)

list(APPEND HEADERS
../apphost.windows.h)
endif()

include(../../exe.cmake)
if(CLR_CMAKE_TARGET_WIN32)
list(APPEND SOURCES ${HEADERS})
endif()

add_compile_definitions(FEATURE_APPHOST)

add_executable(apphost ${SOURCES} ${RESOURCES})

target_link_libraries(apphost PRIVATE hostmisc fxr_resolver)

add_sanitizer_runtime_support(apphost)

if(NOT CLR_CMAKE_TARGET_WIN32)
disable_pax_mprotect(apphost)
endif()

add_definitions(-DFEATURE_APPHOST=1)
install_with_stripped_symbols(apphost TARGETS corehost)

# Disable manifest generation into the file .exe on Windows
if(CLR_CMAKE_TARGET_WIN32)
set_property(TARGET ${PROJECT_NAME} PROPERTY
LINK_FLAGS "/MANIFEST:NO"
)
target_link_options(apphost PRIVATE "/MANIFEST:NO")
endif()

# Specify non-default Windows libs to be used for Arm64 builds
if (CLR_CMAKE_TARGET_WIN32 AND CLR_CMAKE_TARGET_ARCH_ARM64)
target_link_libraries(apphost PRIVATE shell32.lib)
if (CLR_CMAKE_TARGET_WIN32)
target_link_libraries(apphost PRIVATE shell32)
endif()

if (CLR_CMAKE_HOST_APPLE)
Expand Down
34 changes: 24 additions & 10 deletions src/native/corehost/apphost/static/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,29 +14,35 @@ if (NOT CLR_CMAKE_TARGET_APPLE)
set(CMAKE_INSTALL_RPATH "\$ORIGIN/netcoredeps")
endif()

set(SKIP_VERSIONING 1)

include_directories(..)
include_directories(../..)
include_directories(../../hostmisc)
include_directories(../../json)
include_directories(${CLR_SRC_NATIVE_DIR}/libs/System.IO.Compression.Native)
include_directories(${CLR_SRC_NATIVE_DIR}/libs/Common)
include_directories(${CLR_ARTIFACTS_OBJ_DIR}) # Generated version files

add_subdirectory(../../hostmisc hostmisc)

configure_file(${CLR_SRC_NATIVE_DIR}/corehost/configure.h.in ${GENERATED_INCLUDE_DIR}/corehost/configure.h)
target_include_directories(hostmisc PUBLIC ${GENERATED_INCLUDE_DIR}/corehost)

set(SOURCES
../bundle_marker.cpp
./hostfxr_resolver.cpp
./hostpolicy_resolver.cpp
../../hostpolicy/static/coreclr_resolver.cpp
../../fxr_resolver.cpp
../../corehost.cpp
)

set(HEADERS
../bundle_marker.h
../../hostfxr_resolver.h
../../fxr_resolver.h
)

add_definitions(-D_NO_ASYNCRTIMP)
add_definitions(-D_NO_PPLXIMP)
remove_definitions(-DEXPORT_SHARED_API)
add_definitions(-DNATIVE_LIBS_EMBEDDED)
add_compile_definitions(NATIVE_LIBS_EMBEDDED)

include(../../fxr/files.cmake)
include(../../hostpolicy/files.cmake)
Expand All @@ -45,14 +51,15 @@ include(../../hostcommon/files.cmake)
if(MSVC)
# Host components don't try to handle asynchronous exceptions
set_property(DIRECTORY PROPERTY CLR_EH_OPTION /EHsc)
set_property(TARGET hostmisc PROPERTY CLR_EH_OPTION /EHsc)
elseif (CMAKE_CXX_COMPILER_ID MATCHES GNU)
# Prevents libc from calling pthread_cond_destroy on static objects in
# dlopen()'ed library which we dlclose() in pal::unload_library.
add_compile_options($<$<COMPILE_LANGUAGE:CXX>:-fno-use-cxa-atexit>)
endif()

if(CLR_CMAKE_TARGET_WIN32)
add_definitions(-DUNICODE)
add_compile_definitions(UNICODE)
list(APPEND SOURCES
../apphost.windows.cpp
${CLR_SRC_NATIVE_DIR}/libs/Common/delayloadhook_windows.cpp
Expand All @@ -78,11 +85,16 @@ else()
set_exports_linker_option(${EXPORTS_FILE})
endif()

if (CLR_SINGLE_FILE_HOST_ONLY)
set(ADDITIONAL_INSTALL_ARGUMENTS COMPONENT runtime)
add_executable(singlefilehost ${SOURCES})

add_sanitizer_runtime_support(singlefilehost)

if(NOT CLR_CMAKE_TARGET_WIN32)
disable_pax_mprotect(singlefilehost)
endif()

include(../../exe.cmake)
install_with_stripped_symbols(singlefilehost TARGETS corehost COMPONENT runtime)

include(configure.cmake)

if(CLR_CMAKE_HOST_UNIX)
Expand Down Expand Up @@ -262,4 +274,6 @@ target_link_libraries(
${END_WHOLE_ARCHIVE}
)

target_link_libraries(singlefilehost PRIVATE hostmisc)

add_sanitizer_runtime_support(singlefilehost)
15 changes: 6 additions & 9 deletions src/native/corehost/comhost/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,18 +1,12 @@
# Licensed to the .NET Foundation under one or more agreements.
# The .NET Foundation licenses this file to you under the MIT license.

project(comhost)

set(DOTNET_PROJECT_NAME "comhost")

# Include directories
include_directories(../fxr)
include_directories(../json)

# CMake does not recommend using globbing since it messes with the freshness checks
set(SOURCES
comhost.cpp
../fxr_resolver.cpp
clsidmap.cpp
../redirected_error_writer.cpp
)
Expand All @@ -26,9 +20,12 @@ if(CLR_CMAKE_TARGET_WIN32)
Exports.def)
endif()

include(../lib.cmake)
add_compile_definitions(FEATURE_LIBHOST)
add_compile_definitions(EXPORT_SHARED_API)

add_library(comhost SHARED ${SOURCES})

add_definitions(-DFEATURE_LIBHOST=1)
add_version_info_to_target(comhost)

if (CLR_CMAKE_TARGET_WIN32)
set(WINLIBS wintrust.lib)
Expand All @@ -42,4 +39,4 @@ if (CLR_CMAKE_TARGET_WIN32)
endif()

install_with_stripped_symbols(comhost TARGETS corehost)
target_link_libraries(comhost PRIVATE libhostcommon)
target_link_libraries(comhost PRIVATE libhostcommon fxr_resolver)
58 changes: 0 additions & 58 deletions src/native/corehost/common.cmake

This file was deleted.

23 changes: 23 additions & 0 deletions src/native/corehost/configure.h.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#ifndef PAL_HOST_CONFIGURE_H_INCLUDED
#define PAL_HOST_CONFIGURE_H_INCLUDED

#cmakedefine01 CLR_SINGLE_FILE_HOST_ONLY

#ifdef CLR_SINGLE_FILE_HOST_ONLY
// When hosting components are all statically linked,
// the versioning information is irrelevant and may only come up in tracing.
// so we will use "static"
#define HOST_POLICY_PKG_NAME "static"
#define HOST_POLICY_PKG_REL_DIR "static"
#define REPO_COMMIT_HASH "static"
#else
#define HOST_POLICY_PKG_NAME "runtime.@CLI_CMAKE_PKG_RID@.Microsoft.NETCore.DotNetHostPolicy"
#define HOST_POLICY_PKG_REL_DIR "runtime.@CLI_CMAKE_PKG_RID@/native"
#define REPO_COMMIT_HASH "@CLI_CMAKE_COMMIT_HASH@"
#endif

#define FALLBACK_HOST_OS "@CLI_CMAKE_FALLBACK_OS@"
#define CURRENT_OS_NAME "@CLR_CMAKE_TARGET_OS@"
#define CURRENT_ARCH_NAME "@CLR_CMAKE_TARGET_ARCH@"

#endif // PAL_HOST_CONFIGURE_H_INCLUDED
5 changes: 4 additions & 1 deletion src/native/corehost/corehost.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,10 @@ void need_newer_framework_error(const pal::string_t& dotnet_root, const pal::str

int exe_start(const int argc, const pal::char_t* argv[])
{
pal::initialize_createdump();
#if defined(FEATURE_STATIC_HOST) && (defined(TARGET_OSX) || defined(TARGET_LINUX)) && !defined(TARGET_X86)
extern void initialize_static_createdump();
initialize_static_createdump();
#endif

pal::string_t host_path;
if (!pal::get_own_executable_path(&host_path) || !pal::realpath(&host_path))
Expand Down
Loading

0 comments on commit 8f2ca60

Please sign in to comment.