Skip to content

Commit 8f2ca60

Browse files
Rewrite the corehost CMake system to be more "Modern CMake"y and less MSBuild-y (#102475)
Co-authored-by: Elinor Fung <[email protected]>
1 parent 9b74f42 commit 8f2ca60

35 files changed

+303
-388
lines changed

src/native/corehost/CMakeLists.txt

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ project(corehost)
44
include(../../../eng/native/configurepaths.cmake)
55
include(${CLR_ENG_NATIVE_DIR}/configurecompiler.cmake)
66

7+
set(COREHOST_ROOT_DIR "${CMAKE_CURRENT_SOURCE_DIR}")
8+
79
if (MSVC)
810
# Host components don't try to handle asynchronous exceptions
911
set_property(DIRECTORY PROPERTY CLR_EH_OPTION /EHsc)
@@ -19,7 +21,70 @@ elseif (CMAKE_CXX_COMPILER_ID MATCHES GNU)
1921
add_compile_options($<$<COMPILE_LANGUAGE:CXX>:-fno-use-cxa-atexit>)
2022
endif()
2123

24+
set(CMAKE_INCLUDE_CURRENT_DIR ON)
25+
include_directories(${CMAKE_CURRENT_BINARY_DIR})
26+
include_directories(${CMAKE_CURRENT_SOURCE_DIR})
27+
include_directories(${CLR_ARTIFACTS_OBJ_DIR}) # Generated version files
28+
29+
if (NOT ${CLR_SINGLE_FILE_HOST_ONLY})
30+
if("${CLI_CMAKE_PKG_RID}" STREQUAL "")
31+
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)")
32+
endif()
33+
if("${CLI_CMAKE_COMMIT_HASH}" STREQUAL "")
34+
message(FATAL_ERROR "Commit hash needs to be specified to build the host")
35+
endif()
36+
endif()
37+
38+
if("${CLI_CMAKE_FALLBACK_OS}" STREQUAL "")
39+
message(FATAL_ERROR "Fallback rid needs to be specified to build the host")
40+
endif()
41+
42+
if("${CLI_CMAKE_FALLBACK_OS}" STREQUAL "${CLR_CMAKE_TARGET_OS}")
43+
add_compile_definitions(FALLBACK_OS_IS_SAME_AS_TARGET_OS)
44+
endif()
45+
46+
# Find support libraries that we need if they're present on the system.
47+
find_library(PTHREAD_LIB pthread)
48+
49+
# Prefer libatomic.a over libatomic.so.
50+
set(_current_CMAKE_FIND_LIBRARY_SUFFIXES ${CMAKE_FIND_LIBRARY_SUFFIXES})
51+
list(PREPEND CMAKE_FIND_LIBRARY_SUFFIXES .a)
52+
find_library(ATOMIC_SUPPORT_LIB atomic)
53+
set(CMAKE_FIND_LIBRARY_SUFFIXES ${_current_CMAKE_FIND_LIBRARY_SUFFIXES})
54+
unset(_current_CMAKE_FIND_LIBRARY_SUFFIXES)
55+
56+
configure_file(configure.h.in ${CMAKE_CURRENT_BINARY_DIR}/configure.h)
57+
58+
# add_version_info_to_target(targetName [resourceDirName])
59+
function(add_version_info_to_target targetName)
60+
set(RESOURCE_INCLUDE_DIR ${targetName})
61+
if (${ARGC} GREATER 1)
62+
set(RESOURCE_INCLUDE_DIR ${ARGV1})
63+
endif()
64+
65+
if (CLR_CMAKE_TARGET_WIN32)
66+
target_sources(${targetName} PRIVATE ${CMAKE_CURRENT_FUNCTION_LIST_DIR}/native.rc)
67+
set_property(SOURCE ${CMAKE_CURRENT_FUNCTION_LIST_DIR}/native.rc
68+
TARGET_DIRECTORY ${targetName}
69+
APPEND PROPERTY INCLUDE_DIRECTORIES
70+
${CLI_CMAKE_RESOURCE_DIR}/${RESOURCE_INCLUDE_DIR})
71+
else()
72+
target_sources(${targetName} PRIVATE ${VERSION_FILE_PATH})
73+
endif()
74+
endfunction()
75+
76+
# This is required to map a symbol reference to a matching definition local to the module (.so)
77+
# containing the reference instead of using definitions from other modules.
78+
if(CLR_CMAKE_TARGET_LINUX OR CLR_CMAKE_TARGET_SUNOS)
79+
add_link_options(LINKER:-Bsymbolic)
80+
endif()
81+
82+
add_library(fxr_resolver INTERFACE)
83+
target_sources(fxr_resolver INTERFACE fxr_resolver.cpp)
84+
target_include_directories(fxr_resolver INTERFACE fxr)
85+
2286
add_subdirectory(hostcommon)
87+
add_subdirectory(hostmisc)
2388
add_subdirectory(fxr)
2489
add_subdirectory(hostpolicy)
2590

src/native/corehost/apphost/standalone/CMakeLists.txt

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
# Licensed to the .NET Foundation under one or more agreements.
22
# The .NET Foundation licenses this file to you under the MIT license.
33

4-
project(apphost)
5-
set(DOTNET_PROJECT_NAME "apphost")
6-
74
# Add RPATH to the apphost binary that allows using local copies of shared libraries
85
# dotnet core depends on for special scenarios when system wide installation of such
96
# dependencies is not possible for some reason.
@@ -14,13 +11,12 @@ if (NOT CLR_CMAKE_TARGET_OSX)
1411
set(CMAKE_INSTALL_RPATH "\$ORIGIN/netcoredeps")
1512
endif()
1613

17-
set(SKIP_VERSIONING 1)
18-
1914
include_directories(..)
2015

2116
set(SOURCES
2217
../bundle_marker.cpp
2318
./hostfxr_resolver.cpp
19+
../../corehost.cpp
2420
)
2521

2622
set(HEADERS
@@ -29,28 +25,39 @@ set(HEADERS
2925
)
3026

3127
if(CLR_CMAKE_TARGET_WIN32)
32-
add_definitions(-DUNICODE)
28+
add_compile_definitions(UNICODE)
3329
list(APPEND SOURCES
3430
../apphost.windows.cpp)
3531

3632
list(APPEND HEADERS
3733
../apphost.windows.h)
3834
endif()
3935

40-
include(../../exe.cmake)
36+
if(CLR_CMAKE_TARGET_WIN32)
37+
list(APPEND SOURCES ${HEADERS})
38+
endif()
39+
40+
add_compile_definitions(FEATURE_APPHOST)
41+
42+
add_executable(apphost ${SOURCES} ${RESOURCES})
43+
44+
target_link_libraries(apphost PRIVATE hostmisc fxr_resolver)
45+
46+
add_sanitizer_runtime_support(apphost)
47+
48+
if(NOT CLR_CMAKE_TARGET_WIN32)
49+
disable_pax_mprotect(apphost)
50+
endif()
4151

42-
add_definitions(-DFEATURE_APPHOST=1)
52+
install_with_stripped_symbols(apphost TARGETS corehost)
4353

4454
# Disable manifest generation into the file .exe on Windows
4555
if(CLR_CMAKE_TARGET_WIN32)
46-
set_property(TARGET ${PROJECT_NAME} PROPERTY
47-
LINK_FLAGS "/MANIFEST:NO"
48-
)
56+
target_link_options(apphost PRIVATE "/MANIFEST:NO")
4957
endif()
5058

51-
# Specify non-default Windows libs to be used for Arm64 builds
52-
if (CLR_CMAKE_TARGET_WIN32 AND CLR_CMAKE_TARGET_ARCH_ARM64)
53-
target_link_libraries(apphost PRIVATE shell32.lib)
59+
if (CLR_CMAKE_TARGET_WIN32)
60+
target_link_libraries(apphost PRIVATE shell32)
5461
endif()
5562

5663
if (CLR_CMAKE_HOST_APPLE)

src/native/corehost/apphost/static/CMakeLists.txt

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,29 +14,35 @@ if (NOT CLR_CMAKE_TARGET_APPLE)
1414
set(CMAKE_INSTALL_RPATH "\$ORIGIN/netcoredeps")
1515
endif()
1616

17-
set(SKIP_VERSIONING 1)
18-
1917
include_directories(..)
18+
include_directories(../..)
19+
include_directories(../../hostmisc)
2020
include_directories(../../json)
2121
include_directories(${CLR_SRC_NATIVE_DIR}/libs/System.IO.Compression.Native)
2222
include_directories(${CLR_SRC_NATIVE_DIR}/libs/Common)
23+
include_directories(${CLR_ARTIFACTS_OBJ_DIR}) # Generated version files
24+
25+
add_subdirectory(../../hostmisc hostmisc)
26+
27+
configure_file(${CLR_SRC_NATIVE_DIR}/corehost/configure.h.in ${GENERATED_INCLUDE_DIR}/corehost/configure.h)
28+
target_include_directories(hostmisc PUBLIC ${GENERATED_INCLUDE_DIR}/corehost)
2329

2430
set(SOURCES
2531
../bundle_marker.cpp
2632
./hostfxr_resolver.cpp
2733
./hostpolicy_resolver.cpp
2834
../../hostpolicy/static/coreclr_resolver.cpp
35+
../../fxr_resolver.cpp
36+
../../corehost.cpp
2937
)
3038

3139
set(HEADERS
3240
../bundle_marker.h
3341
../../hostfxr_resolver.h
42+
../../fxr_resolver.h
3443
)
3544

36-
add_definitions(-D_NO_ASYNCRTIMP)
37-
add_definitions(-D_NO_PPLXIMP)
38-
remove_definitions(-DEXPORT_SHARED_API)
39-
add_definitions(-DNATIVE_LIBS_EMBEDDED)
45+
add_compile_definitions(NATIVE_LIBS_EMBEDDED)
4046

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

5461
if(CLR_CMAKE_TARGET_WIN32)
55-
add_definitions(-DUNICODE)
62+
add_compile_definitions(UNICODE)
5663
list(APPEND SOURCES
5764
../apphost.windows.cpp
5865
${CLR_SRC_NATIVE_DIR}/libs/Common/delayloadhook_windows.cpp
@@ -78,11 +85,16 @@ else()
7885
set_exports_linker_option(${EXPORTS_FILE})
7986
endif()
8087

81-
if (CLR_SINGLE_FILE_HOST_ONLY)
82-
set(ADDITIONAL_INSTALL_ARGUMENTS COMPONENT runtime)
88+
add_executable(singlefilehost ${SOURCES})
89+
90+
add_sanitizer_runtime_support(singlefilehost)
91+
92+
if(NOT CLR_CMAKE_TARGET_WIN32)
93+
disable_pax_mprotect(singlefilehost)
8394
endif()
8495

85-
include(../../exe.cmake)
96+
install_with_stripped_symbols(singlefilehost TARGETS corehost COMPONENT runtime)
97+
8698
include(configure.cmake)
8799

88100
if(CLR_CMAKE_HOST_UNIX)
@@ -262,4 +274,6 @@ target_link_libraries(
262274
${END_WHOLE_ARCHIVE}
263275
)
264276

277+
target_link_libraries(singlefilehost PRIVATE hostmisc)
278+
265279
add_sanitizer_runtime_support(singlefilehost)

src/native/corehost/comhost/CMakeLists.txt

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,12 @@
11
# Licensed to the .NET Foundation under one or more agreements.
22
# The .NET Foundation licenses this file to you under the MIT license.
33

4-
project(comhost)
5-
6-
set(DOTNET_PROJECT_NAME "comhost")
7-
84
# Include directories
9-
include_directories(../fxr)
105
include_directories(../json)
116

127
# CMake does not recommend using globbing since it messes with the freshness checks
138
set(SOURCES
149
comhost.cpp
15-
../fxr_resolver.cpp
1610
clsidmap.cpp
1711
../redirected_error_writer.cpp
1812
)
@@ -26,9 +20,12 @@ if(CLR_CMAKE_TARGET_WIN32)
2620
Exports.def)
2721
endif()
2822

29-
include(../lib.cmake)
23+
add_compile_definitions(FEATURE_LIBHOST)
24+
add_compile_definitions(EXPORT_SHARED_API)
25+
26+
add_library(comhost SHARED ${SOURCES})
3027

31-
add_definitions(-DFEATURE_LIBHOST=1)
28+
add_version_info_to_target(comhost)
3229

3330
if (CLR_CMAKE_TARGET_WIN32)
3431
set(WINLIBS wintrust.lib)
@@ -42,4 +39,4 @@ if (CLR_CMAKE_TARGET_WIN32)
4239
endif()
4340

4441
install_with_stripped_symbols(comhost TARGETS corehost)
45-
target_link_libraries(comhost PRIVATE libhostcommon)
42+
target_link_libraries(comhost PRIVATE libhostcommon fxr_resolver)

src/native/corehost/common.cmake

Lines changed: 0 additions & 58 deletions
This file was deleted.

src/native/corehost/configure.h.in

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#ifndef PAL_HOST_CONFIGURE_H_INCLUDED
2+
#define PAL_HOST_CONFIGURE_H_INCLUDED
3+
4+
#cmakedefine01 CLR_SINGLE_FILE_HOST_ONLY
5+
6+
#ifdef CLR_SINGLE_FILE_HOST_ONLY
7+
// When hosting components are all statically linked,
8+
// the versioning information is irrelevant and may only come up in tracing.
9+
// so we will use "static"
10+
#define HOST_POLICY_PKG_NAME "static"
11+
#define HOST_POLICY_PKG_REL_DIR "static"
12+
#define REPO_COMMIT_HASH "static"
13+
#else
14+
#define HOST_POLICY_PKG_NAME "runtime.@[email protected]"
15+
#define HOST_POLICY_PKG_REL_DIR "runtime.@CLI_CMAKE_PKG_RID@/native"
16+
#define REPO_COMMIT_HASH "@CLI_CMAKE_COMMIT_HASH@"
17+
#endif
18+
19+
#define FALLBACK_HOST_OS "@CLI_CMAKE_FALLBACK_OS@"
20+
#define CURRENT_OS_NAME "@CLR_CMAKE_TARGET_OS@"
21+
#define CURRENT_ARCH_NAME "@CLR_CMAKE_TARGET_ARCH@"
22+
23+
#endif // PAL_HOST_CONFIGURE_H_INCLUDED

src/native/corehost/corehost.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,10 @@ void need_newer_framework_error(const pal::string_t& dotnet_root, const pal::str
9999

100100
int exe_start(const int argc, const pal::char_t* argv[])
101101
{
102-
pal::initialize_createdump();
102+
#if defined(FEATURE_STATIC_HOST) && (defined(TARGET_OSX) || defined(TARGET_LINUX)) && !defined(TARGET_X86)
103+
extern void initialize_static_createdump();
104+
initialize_static_createdump();
105+
#endif
103106

104107
pal::string_t host_path;
105108
if (!pal::get_own_executable_path(&host_path) || !pal::realpath(&host_path))

0 commit comments

Comments
 (0)