From 017f8af492571623bf9a351fcefbecacf8f6744b Mon Sep 17 00:00:00 2001 From: Filip Navara Date: Tue, 25 Jul 2023 17:10:45 +0200 Subject: [PATCH 01/24] Make it possible to compile CoreCLR on iOS-like platforms with './build.sh clr+clr.nativeaotlibs+libs+packs -os [iossimulator/maccatalyst/ios] -arch arm64 -cross' --- src/coreclr/CMakeLists.txt | 60 ++- src/coreclr/debug/daccess/CMakeLists.txt | 6 +- src/coreclr/dlls/mscordac/CMakeLists.txt | 4 +- src/coreclr/inc/crosscomp.h | 4 +- src/coreclr/inc/executableallocator.h | 4 +- src/coreclr/minipal/Unix/doublemapping.cpp | 36 +- src/coreclr/pal/inc/pal.h | 4 +- src/coreclr/pal/src/CMakeLists.txt | 21 +- src/coreclr/pal/src/configure.cmake | 6 +- src/coreclr/pal/src/debug/debug.cpp | 4 + src/coreclr/pal/src/exception/seh-unwind.cpp | 8 +- src/coreclr/pal/src/include/pal/context.h | 12 +- src/coreclr/pal/src/include/pal/dbgmsg.h | 6 +- src/coreclr/pal/src/map/virtual.cpp | 30 +- src/coreclr/pal/src/misc/dbgmsg.cpp | 4 +- src/coreclr/pal/src/misc/sysinfo.cpp | 431 +++++++++++++++++- src/coreclr/pal/src/thread/context.cpp | 12 +- src/coreclr/pal/src/thread/process.cpp | 4 +- src/coreclr/pal/src/thread/thread.cpp | 4 +- .../ObjectWriter/TargetExtensions.cs | 5 + .../aot/crossgen2/Crossgen2RootCommand.cs | 2 +- .../tools/dotnet-pgo/dotnet-pgo.csproj | 2 +- src/coreclr/utilcode/executableallocator.cpp | 4 +- src/coreclr/vm/amd64/asmhelpers.S | 6 +- src/coreclr/vm/arm64/asmhelpers.S | 8 +- src/coreclr/vm/jitinterface.cpp | 8 +- src/coreclr/vm/threads.h | 2 +- .../corehost/apphost/static/CMakeLists.txt | 12 +- src/native/corehost/hostmisc/pal.h | 2 +- .../hostpolicy/hostpolicy_context.cpp | 4 +- src/native/external/libunwind.cmake | 4 +- .../external/libunwind_extras/CMakeLists.txt | 10 +- src/native/libs/System.Native/CMakeLists.txt | 2 +- src/native/libs/System.Native/entrypoints.c | 2 + .../System.Net.Security.Native/CMakeLists.txt | 2 +- .../CMakeLists.txt | 2 +- .../entrypoints.c | 27 +- src/native/libs/configure.cmake | 2 +- 38 files changed, 628 insertions(+), 138 deletions(-) diff --git a/src/coreclr/CMakeLists.txt b/src/coreclr/CMakeLists.txt index 7ed0d509212cc4..1c76cd3643da4c 100644 --- a/src/coreclr/CMakeLists.txt +++ b/src/coreclr/CMakeLists.txt @@ -29,10 +29,6 @@ if(CORECLR_SET_RPATH) set(MACOSX_RPATH ON) endif(CORECLR_SET_RPATH) -if(CLR_CMAKE_HOST_MACCATALYST OR CLR_CMAKE_HOST_IOS OR CLR_CMAKE_HOST_TVOS) - set(FEATURE_STANDALONE_GC 0) -endif() - OPTION(CLR_CMAKE_ENABLE_CODE_COVERAGE "Enable code coverage" OFF) #---------------------------------------------------- @@ -59,7 +55,7 @@ include(components.cmake) #--------------------------- # Build the single file host #--------------------------- -if(NOT CLR_CROSS_COMPONENTS_BUILD) +if(NOT CLR_CROSS_COMPONENTS_BUILD AND NOT CLR_CMAKE_HOST_MACCATALYST AND NOT CLR_CMAKE_HOST_IOS AND NOT CLR_CMAKE_HOST_TVOS) set(CLR_SINGLE_FILE_HOST_ONLY 1) add_subdirectory(${CLR_SRC_NATIVE_DIR}/corehost/apphost/static Corehost.Static) add_dependencies(runtime singlefilehost) @@ -101,8 +97,9 @@ if(CLR_CMAKE_HOST_UNIX) add_linker_flag(-Wl,-z,notext) endif() + add_subdirectory(pal) + if(NOT CLR_CMAKE_HOST_MACCATALYST AND NOT CLR_CMAKE_HOST_IOS AND NOT CLR_CMAKE_HOST_TVOS) - add_subdirectory(pal) add_subdirectory(hosts) endif() else() @@ -125,9 +122,7 @@ add_subdirectory(pal/prebuilt/inc) set(EP_GENERATED_HEADER_PATH "${GENERATED_INCLUDE_DIR}") include (${CLR_SRC_NATIVE_DIR}/eventpipe/configure.cmake) -if(NOT CLR_CMAKE_HOST_MACCATALYST AND NOT CLR_CMAKE_HOST_IOS AND NOT CLR_CMAKE_HOST_TVOS) - add_subdirectory(debug/debug-pal) -endif() +add_subdirectory(debug/debug-pal) add_subdirectory(minipal) @@ -242,33 +237,34 @@ if(CLR_CMAKE_HOST_UNIX) add_subdirectory(nativeresources) endif(CLR_CMAKE_HOST_UNIX) +add_subdirectory(utilcode) +add_subdirectory(inc) + +if(CLR_CMAKE_HOST_UNIX) + add_subdirectory(palrt) +endif(CLR_CMAKE_HOST_UNIX) + +add_subdirectory(ilasm) +add_subdirectory(ildasm) +add_subdirectory(gcinfo) +add_subdirectory(jit) +add_subdirectory(vm) +add_subdirectory(md) +add_subdirectory(debug) +add_subdirectory(binder) +add_subdirectory(classlibnative) +add_subdirectory(dlls) +add_subdirectory(unwinder) +add_subdirectory(interop) + if(NOT CLR_CMAKE_HOST_MACCATALYST AND NOT CLR_CMAKE_HOST_IOS AND NOT CLR_CMAKE_HOST_TVOS) - add_subdirectory(utilcode) - add_subdirectory(inc) - - if(CLR_CMAKE_HOST_UNIX) - add_subdirectory(palrt) - endif(CLR_CMAKE_HOST_UNIX) - - add_subdirectory(ilasm) - add_subdirectory(ildasm) - add_subdirectory(gcinfo) - add_subdirectory(jit) - add_subdirectory(vm) - add_subdirectory(md) - add_subdirectory(debug) - add_subdirectory(binder) - add_subdirectory(classlibnative) - add_subdirectory(dlls) add_subdirectory(tools) - add_subdirectory(unwinder) - add_subdirectory(interop) - - if(CLR_CMAKE_HOST_WIN32) - add_subdirectory(hosts) - endif(CLR_CMAKE_HOST_WIN32) endif(NOT CLR_CMAKE_HOST_MACCATALYST AND NOT CLR_CMAKE_HOST_IOS AND NOT CLR_CMAKE_HOST_TVOS) +if(CLR_CMAKE_HOST_WIN32) + add_subdirectory(hosts) +endif(CLR_CMAKE_HOST_WIN32) + #---------------------------------------------------- # Cross target Component install configuration #---------------------------------------------------- diff --git a/src/coreclr/debug/daccess/CMakeLists.txt b/src/coreclr/debug/daccess/CMakeLists.txt index 9ed71521d4283d..fa26f55cc37fa2 100644 --- a/src/coreclr/debug/daccess/CMakeLists.txt +++ b/src/coreclr/debug/daccess/CMakeLists.txt @@ -47,10 +47,10 @@ target_link_libraries(daccess PRIVATE cdacreader_api) add_dependencies(daccess eventing_headers) -if(CLR_CMAKE_HOST_FREEBSD OR CLR_CMAKE_HOST_NETBSD OR CLR_CMAKE_HOST_SUNOS) +if(CLR_CMAKE_HOST_FREEBSD OR CLR_CMAKE_HOST_NETBSD OR CLR_CMAKE_HOST_SUNOS OR CLR_CMAKE_HOST_APPLE) add_definitions(-DUSE_DAC_TABLE_RVA) - set(args $<$>:--dynamic> $ ${GENERATED_INCLUDE_DIR}/dactablerva.h) + set(args $<$>:--dynamic> $ ${GENERATED_INCLUDE_DIR}/dactablerva.h) add_custom_command( OUTPUT ${GENERATED_INCLUDE_DIR}/dactablerva.h @@ -72,4 +72,4 @@ if(CLR_CMAKE_HOST_FREEBSD OR CLR_CMAKE_HOST_NETBSD OR CLR_CMAKE_HOST_SUNOS) ) add_dependencies(daccess dactablerva_header) -endif(CLR_CMAKE_HOST_FREEBSD OR CLR_CMAKE_HOST_NETBSD OR CLR_CMAKE_HOST_SUNOS) +endif(CLR_CMAKE_HOST_FREEBSD OR CLR_CMAKE_HOST_NETBSD OR CLR_CMAKE_HOST_SUNOS OR CLR_CMAKE_HOST_APPLE) diff --git a/src/coreclr/dlls/mscordac/CMakeLists.txt b/src/coreclr/dlls/mscordac/CMakeLists.txt index f031e3fc4b60f7..13584c5cf5d657 100644 --- a/src/coreclr/dlls/mscordac/CMakeLists.txt +++ b/src/coreclr/dlls/mscordac/CMakeLists.txt @@ -31,9 +31,9 @@ else(CLR_CMAKE_HOST_WIN32) # Add dependency on export file add_custom_target(mscordaccore_exports DEPENDS ${EXPORTS_FILE}) - if(CLR_CMAKE_HOST_OSX OR CLR_CMAKE_HOST_FREEBSD OR CLR_CMAKE_HOST_NETBSD OR CLR_CMAKE_HOST_SUNOS OR CLR_CMAKE_HOST_HAIKU) + if(CLR_CMAKE_HOST_APPLE OR CLR_CMAKE_HOST_FREEBSD OR CLR_CMAKE_HOST_NETBSD OR CLR_CMAKE_HOST_SUNOS OR CLR_CMAKE_HOST_HAIKU) generate_exports_file(${DEF_SOURCES} ${EXPORTS_FILE}) - endif(CLR_CMAKE_HOST_OSX OR CLR_CMAKE_HOST_FREEBSD OR CLR_CMAKE_HOST_NETBSD OR CLR_CMAKE_HOST_SUNOS OR CLR_CMAKE_HOST_HAIKU) + endif(CLR_CMAKE_HOST_APPLE OR CLR_CMAKE_HOST_FREEBSD OR CLR_CMAKE_HOST_NETBSD OR CLR_CMAKE_HOST_SUNOS OR CLR_CMAKE_HOST_HAIKU) if(CORECLR_SET_RPATH AND CLR_CMAKE_HOST_OSX AND CLR_CMAKE_HOST_ARCH_ARM64) set(CMAKE_BUILD_WITH_INSTALL_NAME_DIR ON) diff --git a/src/coreclr/inc/crosscomp.h b/src/coreclr/inc/crosscomp.h index aeb061ca5ba479..df24acc83afbd0 100644 --- a/src/coreclr/inc/crosscomp.h +++ b/src/coreclr/inc/crosscomp.h @@ -686,9 +686,9 @@ typedef struct _T_KNONVOLATILE_CONTEXT_POINTERS { #if defined(TARGET_OSX) && defined(TARGET_X86) #define DAC_CS_NATIVE_DATA_SIZE 76 -#elif defined(TARGET_OSX) && defined(TARGET_AMD64) +#elif defined(TARGET_APPLE) && defined(TARGET_AMD64) #define DAC_CS_NATIVE_DATA_SIZE 120 -#elif defined(TARGET_OSX) && defined(TARGET_ARM64) +#elif defined(TARGET_APPLE) && defined(TARGET_ARM64) #define DAC_CS_NATIVE_DATA_SIZE 120 #elif defined(TARGET_FREEBSD) && defined(TARGET_X86) #define DAC_CS_NATIVE_DATA_SIZE 12 diff --git a/src/coreclr/inc/executableallocator.h b/src/coreclr/inc/executableallocator.h index 5b9c0d81dc5a2b..11caf3a6857d2d 100644 --- a/src/coreclr/inc/executableallocator.h +++ b/src/coreclr/inc/executableallocator.h @@ -285,7 +285,7 @@ class ExecutableWriterHolder void Unmap() { -#if defined(HOST_OSX) && defined(HOST_ARM64) && !defined(DACCESS_COMPILE) +#if defined(HOST_APPLE) && defined(HOST_ARM64) && !defined(DACCESS_COMPILE) if (m_addressRX != NULL) { PAL_JitWriteProtect(false); @@ -321,7 +321,7 @@ class ExecutableWriterHolder ExecutableWriterHolder(T* addressRX, size_t size, ExecutableAllocator::CacheableMapping cacheMapping = ExecutableAllocator::AddToCache) { m_addressRX = addressRX; -#if defined(HOST_OSX) && defined(HOST_ARM64) +#if defined(HOST_APPLE) && defined(HOST_ARM64) m_addressRW = addressRX; PAL_JitWriteProtect(true); #else diff --git a/src/coreclr/minipal/Unix/doublemapping.cpp b/src/coreclr/minipal/Unix/doublemapping.cpp index cb65e5e284e2b3..70090d2f5465e6 100644 --- a/src/coreclr/minipal/Unix/doublemapping.cpp +++ b/src/coreclr/minipal/Unix/doublemapping.cpp @@ -21,7 +21,7 @@ #endif // TARGET_LINUX && !MFD_CLOEXEC #include "minipal.h" -#if defined(TARGET_OSX) && defined(TARGET_AMD64) +#if defined(TARGET_APPLE) && defined(TARGET_AMD64) #include #include @@ -35,9 +35,9 @@ bool IsProcessTranslated() } return ret == 1; } -#endif // TARGET_OSX && TARGET_AMD64 +#endif // TARGET_APPLE && TARGET_AMD64 -#ifndef TARGET_OSX +#ifndef TARGET_APPLE #ifdef TARGET_64BIT static const off_t MaxDoubleMappedSize = 2048ULL*1024*1024*1024; @@ -45,11 +45,11 @@ static const off_t MaxDoubleMappedSize = 2048ULL*1024*1024*1024; static const off_t MaxDoubleMappedSize = UINT_MAX; #endif -#endif // TARGET_OSX +#endif // TARGET_APPLE bool VMToOSInterface::CreateDoubleMemoryMapper(void** pHandle, size_t *pMaxExecutableCodeSize) { -#ifndef TARGET_OSX +#ifndef TARGET_APPLE #ifdef TARGET_FREEBSD int fd = shm_open(SHM_ANON, O_RDWR | O_CREAT, S_IRWXU); @@ -76,7 +76,7 @@ bool VMToOSInterface::CreateDoubleMemoryMapper(void** pHandle, size_t *pMaxExecu *pMaxExecutableCodeSize = MaxDoubleMappedSize; *pHandle = (void*)(size_t)fd; -#else // !TARGET_OSX +#else // !TARGET_APPLE #ifdef TARGET_AMD64 if (IsProcessTranslated()) @@ -88,21 +88,21 @@ bool VMToOSInterface::CreateDoubleMemoryMapper(void** pHandle, size_t *pMaxExecu *pMaxExecutableCodeSize = SIZE_MAX; *pHandle = NULL; -#endif // !TARGET_OSX +#endif // !TARGET_APPLE return true; } void VMToOSInterface::DestroyDoubleMemoryMapper(void *mapperHandle) { -#ifndef TARGET_OSX +#ifndef TARGET_APPLE close((int)(size_t)mapperHandle); #endif } extern "C" void* PAL_VirtualReserveFromExecutableMemoryAllocatorWithinRange(const void* lpBeginAddress, const void* lpEndAddress, size_t dwSize, int fStoreAllocationInfo); -#ifdef TARGET_OSX +#ifdef TARGET_APPLE bool IsMapJitFlagNeeded() { static volatile int isMapJitFlagNeeded = -1; @@ -133,7 +133,7 @@ bool IsMapJitFlagNeeded() return (bool)isMapJitFlagNeeded; } -#endif // TARGET_OSX +#endif // TARGET_APPLE void* VMToOSInterface::ReserveDoubleMappedMemory(void *mapperHandle, size_t offset, size_t size, const void *rangeStart, const void* rangeEnd) { @@ -147,7 +147,7 @@ void* VMToOSInterface::ReserveDoubleMappedMemory(void *mapperHandle, size_t offs } void* result = PAL_VirtualReserveFromExecutableMemoryAllocatorWithinRange(rangeStart, rangeEnd, size, 0 /* fStoreAllocationInfo */); -#ifndef TARGET_OSX +#ifndef TARGET_APPLE if (result != NULL) { // Map the shared memory over the range reserved from the executable memory allocator. @@ -158,7 +158,7 @@ void* VMToOSInterface::ReserveDoubleMappedMemory(void *mapperHandle, size_t offs result = NULL; } } -#endif // TARGET_OSX +#endif // TARGET_APPLE // For requests with limited range, don't try to fall back to reserving at any address if ((result != NULL) || !isUnlimitedRange) @@ -166,7 +166,7 @@ void* VMToOSInterface::ReserveDoubleMappedMemory(void *mapperHandle, size_t offs return result; } -#ifndef TARGET_OSX +#ifndef TARGET_APPLE result = mmap(NULL, size, PROT_NONE, MAP_SHARED, fd, offset); #else int mmapFlags = MAP_ANON | MAP_PRIVATE; @@ -196,20 +196,20 @@ void *VMToOSInterface::CommitDoubleMappedMemory(void* pStart, size_t size, bool bool VMToOSInterface::ReleaseDoubleMappedMemory(void *mapperHandle, void* pStart, size_t offset, size_t size) { -#ifndef TARGET_OSX +#ifndef TARGET_APPLE int fd = (int)(size_t)mapperHandle; if (mmap(pStart, size, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_FIXED, fd, offset) == MAP_FAILED) { return false; } memset(pStart, 0, size); -#endif // TARGET_OSX +#endif // TARGET_APPLE return munmap(pStart, size) != -1; } void* VMToOSInterface::GetRWMapping(void *mapperHandle, void* pStart, size_t offset, size_t size) { -#ifndef TARGET_OSX +#ifndef TARGET_APPLE int fd = (int)(size_t)mapperHandle; void* result = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, offset); if (result == MAP_FAILED) @@ -217,7 +217,7 @@ void* VMToOSInterface::GetRWMapping(void *mapperHandle, void* pStart, size_t off result = NULL; } return result; -#else // TARGET_OSX +#else // TARGET_APPLE #ifdef TARGET_AMD64 vm_address_t startRW; vm_prot_t curProtection, maxProtection; @@ -242,7 +242,7 @@ void* VMToOSInterface::GetRWMapping(void *mapperHandle, void* pStart, size_t off assert(false); return NULL; #endif // TARGET_AMD64 -#endif // TARGET_OSX +#endif // TARGET_APPLE } bool VMToOSInterface::ReleaseRWMapping(void* pStart, size_t size) diff --git a/src/coreclr/pal/inc/pal.h b/src/coreclr/pal/inc/pal.h index c64cf8ee020adc..d271f7dc9e604e 100644 --- a/src/coreclr/pal/inc/pal.h +++ b/src/coreclr/pal/inc/pal.h @@ -2921,14 +2921,14 @@ VirtualFree( IN DWORD dwFreeType); -#if defined(HOST_OSX) && defined(HOST_ARM64) +#if defined(HOST_APPLE) && defined(HOST_ARM64) PALIMPORT VOID PALAPI PAL_JitWriteProtect(bool writeEnable); -#endif // defined(HOST_OSX) && defined(HOST_ARM64) +#endif // defined(HOST_APPLE) && defined(HOST_ARM64) PALIMPORT diff --git a/src/coreclr/pal/src/CMakeLists.txt b/src/coreclr/pal/src/CMakeLists.txt index 2398334e899897..6631719af961d0 100644 --- a/src/coreclr/pal/src/CMakeLists.txt +++ b/src/coreclr/pal/src/CMakeLists.txt @@ -1,8 +1,8 @@ -if(CLR_CMAKE_TARGET_OSX OR CLR_CMAKE_TARGET_FREEBSD OR CLR_CMAKE_TARGET_HAIKU) +if(CLR_CMAKE_TARGET_APPLE OR CLR_CMAKE_TARGET_FREEBSD OR CLR_CMAKE_TARGET_HAIKU) # On OSX and *BSD, we use the libunwind that's part of the OS # On Haiku, we used a special port of libunwind set(CLR_CMAKE_USE_SYSTEM_LIBUNWIND 1) -endif(CLR_CMAKE_TARGET_OSX OR CLR_CMAKE_TARGET_FREEBSD OR CLR_CMAKE_TARGET_HAIKU) +endif(CLR_CMAKE_TARGET_APPLE OR CLR_CMAKE_TARGET_FREEBSD OR CLR_CMAKE_TARGET_HAIKU) if(NOT DEFINED ENV{ROOTFS_DIR}) include_directories(SYSTEM /usr/local/include) @@ -17,7 +17,7 @@ if(NOT CLR_CMAKE_USE_SYSTEM_LIBUNWIND) include_directories(${CLR_ARTIFACTS_OBJ_DIR}/external/libunwind/include/tdep) add_subdirectory(${CLR_SRC_NATIVE_DIR}/external/libunwind_extras ${CLR_ARTIFACTS_OBJ_DIR}/external/libunwind) -elseif(NOT CLR_CMAKE_TARGET_OSX) +elseif(NOT CLR_CMAKE_TARGET_APPLE) find_unwind_libs(UNWIND_LIBS) else() add_subdirectory(${CLR_SRC_NATIVE_DIR}/external/libunwind_extras ${CLR_ARTIFACTS_OBJ_DIR}/external/libunwind) @@ -70,13 +70,18 @@ if(CLR_CMAKE_TARGET_OSX) if(CLR_CMAKE_TARGET_ARCH_AMD64) add_definitions(-DXSTATE_SUPPORTED) endif() +endif() + +if(CLR_CMAKE_TARGET_APPLE) + add_definitions(-DTARGET_APPLE) + add_definitions(-D_XOPEN_SOURCE) set(PLATFORM_SOURCES arch/${PAL_ARCH_SOURCES_DIR}/context.S arch/${PAL_ARCH_SOURCES_DIR}/dispatchexceptionwrapper.S exception/machexception.cpp exception/machmessage.cpp ) -endif(CLR_CMAKE_TARGET_OSX) +endif(CLR_CMAKE_TARGET_APPLE) if (FEATURE_ENABLE_NO_ADDRESS_SPACE_RANDOMIZATION) add_definitions(-DFEATURE_ENABLE_NO_ADDRESS_SPACE_RANDOMIZATION) @@ -111,12 +116,12 @@ set(ARCH_SOURCES arch/${PAL_ARCH_SOURCES_DIR}/exceptionhelper.S ) -if(NOT CLR_CMAKE_TARGET_OSX) +if(NOT CLR_CMAKE_TARGET_APPLE) list(APPEND PLATFORM_SOURCES arch/${PAL_ARCH_SOURCES_DIR}/callsignalhandlerwrapper.S arch/${PAL_ARCH_SOURCES_DIR}/signalhandlerhelper.cpp ) -endif(NOT CLR_CMAKE_TARGET_OSX) +endif(NOT CLR_CMAKE_TARGET_APPLE) if(CLR_CMAKE_HOST_ARCH_ARM) if (CMAKE_CXX_COMPILER_ID MATCHES "Clang") @@ -213,7 +218,7 @@ add_library(coreclrpal ) # Build separate pal library for DAC (addition to regular pal library) -if(CLR_CMAKE_TARGET_OSX) +if(CLR_CMAKE_TARGET_APPLE) set(LIBUNWIND_DAC_OBJECTS $) add_library(coreclrpal_dac STATIC @@ -235,7 +240,7 @@ else() exception/remote-unwind.cpp ) endif(NOT FEATURE_CROSSBITNESS) -endif(CLR_CMAKE_TARGET_OSX) +endif(CLR_CMAKE_TARGET_APPLE) # There is only one function exported in 'tracepointprovider.cpp' namely 'PAL_InitializeTracing', # which is guarded with '#if defined(__linux__)'. On macOS, Xcode issues the following warning: diff --git a/src/coreclr/pal/src/configure.cmake b/src/coreclr/pal/src/configure.cmake index ed730378074c3c..624ed59d8e765e 100644 --- a/src/coreclr/pal/src/configure.cmake +++ b/src/coreclr/pal/src/configure.cmake @@ -14,7 +14,7 @@ elseif(CLR_CMAKE_TARGET_SUNOS) set(CMAKE_REQUIRED_INCLUDES /opt/local/include) endif() -if(CLR_CMAKE_TARGET_OSX) +if(CLR_CMAKE_TARGET_APPLE) set(CMAKE_REQUIRED_DEFINITIONS -D_XOPEN_SOURCE) elseif(NOT CLR_CMAKE_TARGET_FREEBSD AND NOT CLR_CMAKE_TARGET_NETBSD) set(CMAKE_REQUIRED_DEFINITIONS "-D_BSD_SOURCE -D_SVID_SOURCE -D_DEFAULT_SOURCE -D_POSIX_C_SOURCE=200809L") @@ -1002,7 +1002,7 @@ if(NOT CLR_CMAKE_HOST_ARCH_ARM AND NOT CLR_CMAKE_HOST_ARCH_ARM64) set(CMAKE_REQUIRED_LIBRARIES) endif() -if(CLR_CMAKE_TARGET_OSX) +if(CLR_CMAKE_TARGET_APPLE) set(HAVE__NSGETENVIRON 1) set(DEADLOCK_WHEN_THREAD_IS_SUSPENDED_WHILE_BLOCKED_ON_MUTEX 1) set(PAL_PTRACE "ptrace((cmd), (pid), (caddr_t)(addr), (data))") @@ -1066,7 +1066,7 @@ else() # Anything else is Linux set(PAL_PT_WRITE_D PTRACE_POKEDATA) set(HAS_FTRUNCATE_LENGTH_ISSUE 0) set(HAVE_SCHED_OTHER_ASSIGNABLE 1) -endif(CLR_CMAKE_TARGET_OSX) +endif(CLR_CMAKE_TARGET_APPLE) check_struct_has_member( "struct statfs" diff --git a/src/coreclr/pal/src/debug/debug.cpp b/src/coreclr/pal/src/debug/debug.cpp index b38810864a587f..6d022237f8821c 100644 --- a/src/coreclr/pal/src/debug/debug.cpp +++ b/src/coreclr/pal/src/debug/debug.cpp @@ -46,7 +46,9 @@ SET_DEFAULT_DEBUG_CHANNEL(DEBUG); // some headers have code with asserts, so do #elif defined(HAVE_TTRACE) // HAVE_PROCFS_CTL #include #else // defined(HAVE_TTRACE) +#if !defined(TARGET_APPLE) || defined(TARGET_OSX) #include +#endif #endif // HAVE_PROCFS_CTL #if HAVE_VM_READ #include @@ -61,7 +63,9 @@ SET_DEFAULT_DEBUG_CHANNEL(DEBUG); // some headers have code with asserts, so do #ifdef __APPLE__ #include +#if defined(TARGET_OSX) #include +#endif #endif // __APPLE__ #if HAVE_MACH_EXCEPTIONS diff --git a/src/coreclr/pal/src/exception/seh-unwind.cpp b/src/coreclr/pal/src/exception/seh-unwind.cpp index 5b12af8aa027bc..66b304cca370f4 100644 --- a/src/coreclr/pal/src/exception/seh-unwind.cpp +++ b/src/coreclr/pal/src/exception/seh-unwind.cpp @@ -270,7 +270,7 @@ static void WinContextToUnwindContext(CONTEXT *winContext, unw_context_t *unwCon { unwContext->fpregs[i] = winContext->D[i]; } -#elif defined(HOST_ARM64) && !defined(TARGET_OSX) +#elif defined(HOST_ARM64) && !defined(TARGET_APPLE) unwContext->uc_mcontext.pc = winContext->Pc; unwContext->uc_mcontext.sp = winContext->Sp; unwContext->uc_mcontext.regs[29] = winContext->Fp; @@ -312,7 +312,7 @@ static void WinContextToUnwindCursor(CONTEXT *winContext, unw_cursor_t *cursor) unw_set_reg(cursor, UNW_X86_EBX, winContext->Ebx); unw_set_reg(cursor, UNW_X86_ESI, winContext->Esi); unw_set_reg(cursor, UNW_X86_EDI, winContext->Edi); -#elif defined(HOST_ARM64) && defined(TARGET_OSX) +#elif defined(HOST_ARM64) && defined(TARGET_APPLE) // unw_cursor_t is an opaque data structure on macOS // As noted in WinContextToUnwindContext this didn't work for Linux // TBD whether this will work for macOS. @@ -436,12 +436,12 @@ void UnwindContextToWinContext(unw_cursor_t *cursor, CONTEXT *winContext) unw_get_fpreg(cursor, UNW_AARCH64_V30, (unw_fpreg_t*)&winContext->V[30].Low); unw_get_fpreg(cursor, UNW_AARCH64_V31, (unw_fpreg_t*)&winContext->V[31].Low); -#if defined(TARGET_OSX) && defined(TARGET_ARM64) +#if defined(TARGET_APPLE) && defined(TARGET_ARM64) // Strip pointer authentication bits which seem to be leaking out of libunwind // Seems like ptrauth_strip() / __builtin_ptrauth_strip() should work, but currently // errors with "this target does not support pointer authentication" winContext->Pc = winContext->Pc & 0x7fffffffffffull; -#endif // defined(TARGET_OSX) && defined(TARGET_ARM64) +#endif // defined(TARGET_APPLE) && defined(TARGET_ARM64) #elif (defined(HOST_UNIX) && defined(HOST_S390X)) unw_get_reg(cursor, UNW_REG_SP, (unw_word_t *) &winContext->R15); unw_get_reg(cursor, UNW_REG_IP, (unw_word_t *) &winContext->PSWAddr); diff --git a/src/coreclr/pal/src/include/pal/context.h b/src/coreclr/pal/src/include/pal/context.h index 6eeeaa6fed7453..e802a61a4158d4 100644 --- a/src/coreclr/pal/src/include/pal/context.h +++ b/src/coreclr/pal/src/include/pal/context.h @@ -624,7 +624,7 @@ const struct fpregs* GetConstNativeSigSimdContext(const native_context_t *mc) return GetNativeSigSimdContext(const_cast(mc)); } -#elif !defined(TARGET_OSX) // TARGET_FREEBSD +#elif !defined(TARGET_APPLE) // TARGET_FREEBSD #define MCREG_X0(mc) ((mc).regs[0]) #define MCREG_X1(mc) ((mc).regs[1]) @@ -699,7 +699,7 @@ const fpsimd_context* GetConstNativeSigSimdContext(const native_context_t *mc) return GetNativeSigSimdContext(const_cast(mc)); } -#else // TARGET_OSX +#else // TARGET_APPLE #define MCREG_X0(mc) ((mc)->__ss.__x[0]) #define MCREG_X1(mc) ((mc)->__ss.__x[1]) @@ -749,7 +749,7 @@ const _STRUCT_ARM_NEON_STATE64* GetConstNativeSigSimdContext(const native_contex return GetNativeSigSimdContext(const_cast(mc)); } -#endif // TARGET_OSX +#endif // TARGET_APPLE #elif defined(HOST_LOONGARCH64) @@ -789,7 +789,7 @@ const _STRUCT_ARM_NEON_STATE64* GetConstNativeSigSimdContext(const native_contex #else // HOST_ARM64 -#ifdef TARGET_OSX +#ifdef TARGET_APPLE #define MCREG_Rbp(mc) ((mc)->__ss.__rbp) #define MCREG_Rip(mc) ((mc)->__ss.__rip) @@ -874,7 +874,7 @@ inline void *FPREG_Xstate_Hi16Zmm(const ucontext_t *uc, uint32_t *featureSize) *featureSize = sizeof(_STRUCT_ZMM_REG) * 16; return reinterpret_cast(&((_STRUCT_X86_AVX512_STATE64&)FPSTATE(uc)).__fpu_zmm16); } -#else //TARGET_OSX +#else //TARGET_APPLE // For FreeBSD, as found in x86/ucontext.h #define MCREG_Rbp(mc) ((mc).mc_rbp) @@ -911,7 +911,7 @@ inline void *FPREG_Xstate_Hi16Zmm(const ucontext_t *uc, uint32_t *featureSize) #define FPREG_Xmm(uc, index) *(M128A*) &(FPSTATE(uc)->sv_xmm[index]) #define FPREG_St(uc, index) *(M128A*) &(FPSTATE(uc)->sv_fp[index].fp_acc) -#endif // TARGET_OSX +#endif // TARGET_APPLE #endif // HOST_ARM64 #else // HOST_64BIT diff --git a/src/coreclr/pal/src/include/pal/dbgmsg.h b/src/coreclr/pal/src/include/pal/dbgmsg.h index ca8d1454de9143..0d0c5e4aed4cee 100644 --- a/src/coreclr/pal/src/include/pal/dbgmsg.h +++ b/src/coreclr/pal/src/include/pal/dbgmsg.h @@ -494,7 +494,7 @@ if new_level is -1, the nesting level will not be modified --*/ int DBG_change_entrylevel(int new_level); -#ifdef __APPLE__ +#ifdef TARGET_OSX /*++ Function : PAL_DisplayDialog @@ -514,10 +514,10 @@ Function : --*/ void PAL_DisplayDialogFormatted(const char *szTitle, const char *szTextFormat, ...); -#else // __APPLE__ +#else // TARGET_OSX #define PAL_DisplayDialog(_szTitle, _szText) #define PAL_DisplayDialogFormatted(_szTitle, _szTextFormat, args...) -#endif // __APPLE__ +#endif // TARGET_OSX #ifdef __cplusplus } diff --git a/src/coreclr/pal/src/map/virtual.cpp b/src/coreclr/pal/src/map/virtual.cpp index 3145faac5f5056..6947266f55dfcf 100644 --- a/src/coreclr/pal/src/map/virtual.cpp +++ b/src/coreclr/pal/src/map/virtual.cpp @@ -40,12 +40,17 @@ SET_DEFAULT_DEBUG_CHANNEL(VIRTUAL); // some headers have code with asserts, so d #include #include #include +#include #if HAVE_VM_ALLOCATE #include #include #endif // HAVE_VM_ALLOCATE +#if defined(TARGET_APPLE) && !defined(TARGET_OSX) +#include +#endif + using namespace CorUnix; CRITICAL_SECTION virtual_critsec; @@ -1231,7 +1236,30 @@ VirtualProtect( return bRetVal; } -#if defined(HOST_OSX) && defined(HOST_ARM64) +#if defined(HOST_APPLE) && defined(HOST_ARM64) + +#if defined(TARGET_APPLE) && !defined(TARGET_OSX) + +void +(*jit_write_protect_np)(int enabled); + +static struct jit_write_protect_helper +{ + jit_write_protect_helper() + { + jit_write_protect_np = (void (*)(int))dlsym(RTLD_DEFAULT, "pthread_jit_write_protect_np"); + if (jit_write_protect_np == NULL) + { + ERROR( "pthread_jit_write_protect_np not available.\n" ); + exit(1); + } + } +} jit_write_protect_helper; + +#define pthread_jit_write_protect_np jit_write_protect_np + +#endif + PALAPI VOID PAL_JitWriteProtect(bool writeEnable) { thread_local int enabledCount = 0; diff --git a/src/coreclr/pal/src/misc/dbgmsg.cpp b/src/coreclr/pal/src/misc/dbgmsg.cpp index 38f101d147f67d..d49671dd571b00 100644 --- a/src/coreclr/pal/src/misc/dbgmsg.cpp +++ b/src/coreclr/pal/src/misc/dbgmsg.cpp @@ -734,7 +734,7 @@ bool DBG_ShouldCheckStackAlignment() } #endif // _DEBUG && __APPLE__ -#ifdef __APPLE__ +#if defined(TARGET_OSX) #include "CoreFoundation/CFUserNotification.h" #include "CoreFoundation/CFString.h" #include "Security/AuthSession.h" @@ -854,4 +854,4 @@ void PAL_DisplayDialogFormatted(const char *szTitle, const char *szTextFormat, . va_end(args); } -#endif // __APPLE__ +#endif // TARGET_OSX diff --git a/src/coreclr/pal/src/misc/sysinfo.cpp b/src/coreclr/pal/src/misc/sysinfo.cpp index d20fa7b2aabec1..a9ae481cba2915 100644 --- a/src/coreclr/pal/src/misc/sysinfo.cpp +++ b/src/coreclr/pal/src/misc/sysinfo.cpp @@ -58,13 +58,14 @@ Revision History: #include #endif // HAVE_MACHINE_VMPARAM_H -#if defined(TARGET_OSX) +#if defined(TARGET_APPLE) #include #include #include #include -#endif // defined(TARGET_OSX) +#endif // defined(TARGET_APPLE) +#if !defined(TARGET_IOS) // On some platforms sys/user.h ends up defining _DEBUG; if so // remove the definition before including the header and put // back our definition afterwards @@ -78,6 +79,7 @@ Revision History: #define _DEBUG OLD_DEBUG #undef OLD_DEBUG #endif +#endif // !TARGET_IOS #include "pal/dbgmsg.h" #include "pal/process.h" @@ -211,6 +213,8 @@ GetSystemInfo( lpSystemInfo->lpMaximumApplicationAddress = (PVOID) (1ull << 47); #elif defined(__sun) lpSystemInfo->lpMaximumApplicationAddress = (PVOID) 0xfffffd7fffe00000ul; +#elif defined(VM_MAX_PAGE_ADDRESS) + lpSystemInfo->lpMaximumApplicationAddress = (PVOID) VM_MAX_PAGE_ADDRESS; #elif defined(USERLIMIT) lpSystemInfo->lpMaximumApplicationAddress = (PVOID) USERLIMIT; #elif defined(HOST_64BIT) @@ -237,3 +241,426 @@ GetSystemInfo( LOGEXIT("GetSystemInfo returns VOID\n"); PERF_EXIT(GetSystemInfo); } + +// Get memory size multiplier based on the passed in units (k = kilo, m = mega, g = giga) +static uint64_t GetMemorySizeMultiplier(char units) +{ + switch(units) + { + case 'g': + case 'G': return 1024 * 1024 * 1024; + case 'm': + case 'M': return 1024 * 1024; + case 'k': + case 'K': return 1024; + } + + // No units multiplier + return 1; +} + +#ifndef __APPLE__ +// Try to read the MemAvailable entry from /proc/meminfo. +// Return true if the /proc/meminfo existed, the entry was present and we were able to parse it. +static bool ReadMemAvailable(uint64_t* memAvailable) +{ + bool foundMemAvailable = false; + FILE* memInfoFile = fopen("/proc/meminfo", "r"); + if (memInfoFile != NULL) + { + char *line = nullptr; + size_t lineLen = 0; + + while (getline(&line, &lineLen, memInfoFile) != -1) + { + char units = '\0'; + uint64_t available; + int fieldsParsed = sscanf(line, "MemAvailable: %" SCNu64 " %cB", &available, &units); + + if (fieldsParsed >= 1) + { + uint64_t multiplier = GetMemorySizeMultiplier(units); + *memAvailable = available * multiplier; + foundMemAvailable = true; + break; + } + } + + free(line); + fclose(memInfoFile); + } + + return foundMemAvailable; +} +#endif // __APPLE__ + +/*++ +Function: + GlobalMemoryStatusEx + +GlobalMemoryStatusEx + +Retrieves information about the system's current usage of both physical and virtual memory. + +Return Values + +This function returns a BOOL to indicate its success status. + +--*/ +BOOL +PALAPI +GlobalMemoryStatusEx( + IN OUT LPMEMORYSTATUSEX lpBuffer) +{ + + PERF_ENTRY(GlobalMemoryStatusEx); + ENTRY("GlobalMemoryStatusEx (lpBuffer=%p)\n", lpBuffer); + + lpBuffer->dwMemoryLoad = 0; + lpBuffer->ullTotalPhys = 0; + lpBuffer->ullAvailPhys = 0; + lpBuffer->ullTotalPageFile = 0; + lpBuffer->ullAvailPageFile = 0; + lpBuffer->ullTotalVirtual = 0; + lpBuffer->ullAvailVirtual = 0; + lpBuffer->ullAvailExtendedVirtual = 0; + + BOOL fRetVal = FALSE; + int rc; + + // Get the physical memory size +#if HAVE_SYSCONF && HAVE__SC_PHYS_PAGES + uint64_t physical_memory; + + // Get the Physical memory size + physical_memory = ((uint64_t)sysconf( _SC_PHYS_PAGES )) * ((uint64_t) sysconf( _SC_PAGE_SIZE )); + lpBuffer->ullTotalPhys = (DWORDLONG)physical_memory; + fRetVal = TRUE; +#elif HAVE_SYSCTL + int64_t physical_memory; + size_t length; + // Get the Physical memory size + int mib[] = { CTL_HW, HW_MEMSIZE }; + length = sizeof(INT64); + rc = sysctl(mib, 2, &physical_memory, &length, NULL, 0); + if (rc != 0) + { + ASSERT("sysctl failed for HW_MEMSIZE (%d)\n", errno); + } + else + { + lpBuffer->ullTotalPhys = (DWORDLONG)physical_memory; + fRetVal = TRUE; + } + +#endif // HAVE_SYSCTL + + // Get swap file size, consider the ability to get the values optional + // (don't return FALSE from the GlobalMemoryStatusEx) +#if HAVE_XSW_USAGE + // This is available on OSX + struct xsw_usage xsu; + int mib[] = { CTL_HW, VM_SWAPUSAGE }; + size_t length = sizeof(xsu); + rc = sysctl(mib, 2, &xsu, &length, NULL, 0); + if (rc == 0) + { + lpBuffer->ullTotalPageFile = xsu.xsu_total; + lpBuffer->ullAvailPageFile = xsu.xsu_avail; + } +#elif HAVE_XSWDEV + // E.g. FreeBSD + struct xswdev xsw; + int mib[3]; + size_t length = 2; + rc = sysctlnametomib("vm.swap_info", mib, &length); + if (rc == 0) + { + int pagesize = getpagesize(); + // Aggregate the information for all swap files on the system + for (mib[2] = 0; ; mib[2]++) + { + length = sizeof(xsw); + rc = sysctl(mib, 3, &xsw, &length, NULL, 0); + if ((rc < 0) || (xsw.xsw_version != XSWDEV_VERSION)) + { + // All the swap files were processed or coreclr was built against + // a version of headers not compatible with the current XSWDEV_VERSION. + break; + } + + DWORDLONG avail = xsw.xsw_nblks - xsw.xsw_used; + lpBuffer->ullTotalPageFile += (DWORDLONG)xsw.xsw_nblks * pagesize; + lpBuffer->ullAvailPageFile += (DWORDLONG)avail * pagesize; + } + } +#elif HAVE_SWAPCTL + struct anoninfo ai; + if (swapctl(SC_AINFO, &ai) != -1) + { + int pagesize = getpagesize(); + lpBuffer->ullTotalPageFile = ai.ani_max * pagesize; + lpBuffer->ullAvailPageFile = ai.ani_free * pagesize; + } +#elif HAVE_SYSINFO + // Linux + struct sysinfo info; + rc = sysinfo(&info); + if (rc == 0) + { + lpBuffer->ullTotalPageFile = info.totalswap; + lpBuffer->ullAvailPageFile = info.freeswap; +#if HAVE_SYSINFO_WITH_MEM_UNIT + // A newer version of the sysinfo structure represents all the sizes + // in mem_unit instead of bytes + lpBuffer->ullTotalPageFile *= info.mem_unit; + lpBuffer->ullAvailPageFile *= info.mem_unit; +#endif // HAVE_SYSINFO_WITH_MEM_UNIT + } +#endif // HAVE_SYSINFO + + // Get the physical memory in use - from it, we can get the physical memory available. + // We do this only when we have the total physical memory available. + if (lpBuffer->ullTotalPhys > 0) + { +#ifndef __APPLE__ + static volatile bool tryReadMemInfo = true; + + if (tryReadMemInfo) + { + // Ensure that we don't try to read the /proc/meminfo in successive calls to the GlobalMemoryStatusEx + // if we have failed to access the file or the file didn't contain the MemAvailable value. + tryReadMemInfo = ReadMemAvailable((uint64_t*)&lpBuffer->ullAvailPhys); + } + + if (!tryReadMemInfo) + { + // The /proc/meminfo doesn't exist or it doesn't contain the MemAvailable row or the format of the row is invalid + // Fall back to getting the available pages using sysconf. + lpBuffer->ullAvailPhys = sysconf(SYSCONF_PAGES) * sysconf(_SC_PAGE_SIZE); + } + + INT64 used_memory = lpBuffer->ullTotalPhys - lpBuffer->ullAvailPhys; + lpBuffer->dwMemoryLoad = (DWORD)((used_memory * 100) / lpBuffer->ullTotalPhys); +#else + vm_size_t page_size; + mach_port_t mach_port; + mach_msg_type_number_t count; + vm_statistics_data_t vm_stats; + mach_port = mach_host_self(); + count = sizeof(vm_stats) / sizeof(natural_t); + if (KERN_SUCCESS == host_page_size(mach_port, &page_size)) + { + if (KERN_SUCCESS == host_statistics(mach_port, HOST_VM_INFO, (host_info_t)&vm_stats, &count)) + { + lpBuffer->ullAvailPhys = (int64_t)vm_stats.free_count * (int64_t)page_size; + INT64 used_memory = ((INT64)vm_stats.active_count + (INT64)vm_stats.inactive_count + (INT64)vm_stats.wire_count) * (INT64)page_size; + lpBuffer->dwMemoryLoad = (DWORD)((used_memory * 100) / lpBuffer->ullTotalPhys); + } + } + mach_port_deallocate(mach_task_self(), mach_port); +#endif // __APPLE__ + } + +#ifndef TARGET_RISCV64 + // There is no API to get the total virtual address space size on + // Unix, so we use a constant value representing 128TB, which is + // the approximate size of total user virtual address space on + // the currently supported Unix systems. + static const UINT64 VMSize = (1ull << 47); +#else // TARGET_RISCV64 + // For RISC-V Linux Kernel SV39 virtual memory limit is 256gb. + static const UINT64 VMSize = (1ull << 38); +#endif // TARGET_RISCV64 + lpBuffer->ullTotalVirtual = VMSize; + lpBuffer->ullAvailVirtual = lpBuffer->ullAvailPhys; + + LOGEXIT("GlobalMemoryStatusEx returns %d\n", fRetVal); + PERF_EXIT(GlobalMemoryStatusEx); + + return fRetVal; +} + +bool +PAL_ReadMemoryValueFromFile(const char* filename, uint64_t* val) +{ + bool result = false; + char *line = nullptr; + size_t lineLen = 0; + char* endptr = nullptr; + uint64_t num = 0, multiplier; + + if (val == nullptr) + return false; + + FILE* file = fopen(filename, "r"); + if (file == nullptr) + goto done; + + if (getline(&line, &lineLen, file) == -1) + goto done; + + errno = 0; + num = strtoull(line, &endptr, 0); + if (errno != 0) + goto done; + + multiplier = GetMemorySizeMultiplier(*endptr); + *val = num * multiplier; + result = true; + if (*val/multiplier != num) + result = false; +done: + if (file) + fclose(file); + free(line); + return result; +} + +#define UPDATE_CACHE_SIZE_AND_LEVEL(NEW_CACHE_SIZE, NEW_CACHE_LEVEL) if (NEW_CACHE_SIZE > cacheSize) { cacheSize = NEW_CACHE_SIZE; cacheLevel = NEW_CACHE_LEVEL; } + +size_t +PALAPI +PAL_GetLogicalProcessorCacheSizeFromOS() +{ + size_t cacheLevel = 0; + size_t cacheSize = 0; + size_t size; + +#ifdef _SC_LEVEL1_DCACHE_SIZE + size = ( size_t) sysconf(_SC_LEVEL1_DCACHE_SIZE); + UPDATE_CACHE_SIZE_AND_LEVEL(size, 1) +#endif +#ifdef _SC_LEVEL2_CACHE_SIZE + size = ( size_t) sysconf(_SC_LEVEL2_CACHE_SIZE); + UPDATE_CACHE_SIZE_AND_LEVEL(size, 2) +#endif +#ifdef _SC_LEVEL3_CACHE_SIZE + size = ( size_t) sysconf(_SC_LEVEL3_CACHE_SIZE); + UPDATE_CACHE_SIZE_AND_LEVEL(size, 3) +#endif +#ifdef _SC_LEVEL4_CACHE_SIZE + size = ( size_t) sysconf(_SC_LEVEL4_CACHE_SIZE); + UPDATE_CACHE_SIZE_AND_LEVEL(size, 4) +#endif + +#if defined(TARGET_LINUX) && !defined(HOST_ARM) && !defined(HOST_X86) + if (cacheSize == 0) + { + // + // Fallback to retrieve cachesize via /sys/.. if sysconf was not available + // for the platform. Currently musl and arm64 should be only cases to use + // this method to determine cache size. + // + size_t level; + char path_to_size_file[] = "/sys/devices/system/cpu/cpu0/cache/index-/size"; + char path_to_level_file[] = "/sys/devices/system/cpu/cpu0/cache/index-/level"; + int index = 40; + _ASSERTE(path_to_size_file[index] == '-'); + _ASSERTE(path_to_level_file[index] == '-'); + + for (int i = 0; i < 5; i++) + { + path_to_size_file[index] = (char)(48 + i); + + if (PAL_ReadMemoryValueFromFile(path_to_size_file, &size)) + { + path_to_level_file[index] = (char)(48 + i); + + if (PAL_ReadMemoryValueFromFile(path_to_level_file, &level)) + { + UPDATE_CACHE_SIZE_AND_LEVEL(size, level) + } + else + { + cacheSize = std::max(cacheSize, size); + } + } + } + } +#endif + +#if (defined(HOST_ARM64) || defined(HOST_LOONGARCH64)) && !defined(TARGET_APPLE) + if (cacheSize == 0) + { + // We expect to get the L3 cache size for Arm64 but currently expected to be missing that info + // from most of the machines with an exceptions on some machines. + // + // _SC_LEVEL*_*CACHE_SIZE is not yet present. Work is in progress to enable this for arm64 + // + // /sys/devices/system/cpu/cpu*/cache/index*/ is also not yet present in most systems. + // Arm64 patch is in Linux kernel tip. + // + // midr_el1 is available in "/sys/devices/system/cpu/cpu0/regs/identification/midr_el1", + // but without an exhaustive list of ARM64 processors any decode of midr_el1 + // Would likely be incomplete + + // Published information on ARM64 architectures is limited. + // If we use recent high core count chips as a guide for state of the art, we find + // total L3 cache to be 1-2MB/core. As always, there are exceptions. + + // Estimate cache size based on CPU count + // Assume lower core count are lighter weight parts which are likely to have smaller caches + // Assume L3$/CPU grows linearly from 256K to 1.5M/CPU as logicalCPUs grows from 2 to 12 CPUs + DWORD logicalCPUs = PAL_GetLogicalCpuCountFromOS(); + + cacheSize = logicalCPUs*std::min(1536, std::max(256, (int)logicalCPUs*128))*1024; + } +#endif + +#if HAVE_SYSCTLBYNAME + if (cacheSize == 0) + { + int64_t cacheSizeFromSysctl = 0; + size_t sz = sizeof(cacheSizeFromSysctl); + const bool success = false + // macOS: Since macOS 12.0, Apple added ".perflevelX." to determinate cache sizes for efficiency + // and performance cores separately. "perflevel0" stands for "performance" + || sysctlbyname("hw.perflevel0.l3cachesize", &cacheSizeFromSysctl, &sz, nullptr, 0) == 0 + || sysctlbyname("hw.perflevel0.l2cachesize", &cacheSizeFromSysctl, &sz, nullptr, 0) == 0 + // macOS: these report cache sizes for efficiency cores only: + || sysctlbyname("hw.l3cachesize", &cacheSizeFromSysctl, &sz, nullptr, 0) == 0 + || sysctlbyname("hw.l2cachesize", &cacheSizeFromSysctl, &sz, nullptr, 0) == 0 + || sysctlbyname("hw.l1dcachesize", &cacheSizeFromSysctl, &sz, nullptr, 0) == 0; + if (success) + { + _ASSERTE(cacheSizeFromSysctl > 0); + cacheSize = ( size_t) cacheSizeFromSysctl; + } + } +#endif + +#if (defined(HOST_ARM64) || defined(HOST_LOONGARCH64)) && !defined(TARGET_APPLE) + if (cacheLevel != 3) + { + // We expect to get the L3 cache size for Arm64 but currently expected to be missing that info + // from most of the machines. + // Hence, just use the following heuristics at best depending on the CPU count + // 1 ~ 4 : 4 MB + // 5 ~ 16 : 8 MB + // 17 ~ 64 : 16 MB + // 65+ : 32 MB + DWORD logicalCPUs = PAL_GetLogicalCpuCountFromOS(); + if (logicalCPUs < 5) + { + cacheSize = 4; + } + else if (logicalCPUs < 17) + { + cacheSize = 8; + } + else if (logicalCPUs < 65) + { + cacheSize = 16; + } + else + { + cacheSize = 32; + } + + cacheSize *= (1024 * 1024); + } +#endif + + return cacheSize; +} diff --git a/src/coreclr/pal/src/thread/context.cpp b/src/coreclr/pal/src/thread/context.cpp index 04fabab0e7253e..c8695edb2727d6 100644 --- a/src/coreclr/pal/src/thread/context.cpp +++ b/src/coreclr/pal/src/thread/context.cpp @@ -688,7 +688,7 @@ void CONTEXTToNativeContext(CONST CONTEXT *lpContext, native_context_t *native) FPREG_Xmm(native, i) = lpContext->FltSave.XmmRegisters[i]; } #elif defined(HOST_ARM64) -#ifdef TARGET_OSX +#ifdef TARGET_APPLE _STRUCT_ARM_NEON_STATE64* fp = GetNativeSigSimdContext(native); fp->__fpsr = lpContext->Fpsr; fp->__fpcr = lpContext->Fpcr; @@ -707,7 +707,7 @@ void CONTEXTToNativeContext(CONST CONTEXT *lpContext, native_context_t *native) *(NEON128*) &fp->fp_q[i] = lpContext->V[i]; } } -#else // TARGET_OSX +#else // TARGET_APPLE fpsimd_context* fp = GetNativeSigSimdContext(native); if (fp) { @@ -718,7 +718,7 @@ void CONTEXTToNativeContext(CONST CONTEXT *lpContext, native_context_t *native) *(NEON128*) &fp->vregs[i] = lpContext->V[i]; } } -#endif // TARGET_OSX +#endif // TARGET_APPLE #elif defined(HOST_ARM) VfpSigFrame* fp = GetNativeSigSimdContext(native); if (fp) @@ -897,7 +897,7 @@ void CONTEXTFromNativeContext(const native_context_t *native, LPCONTEXT lpContex lpContext->FltSave.XmmRegisters[i] = FPREG_Xmm(native, i); } #elif defined(HOST_ARM64) -#ifdef TARGET_OSX +#ifdef TARGET_APPLE const _STRUCT_ARM_NEON_STATE64* fp = GetConstNativeSigSimdContext(native); lpContext->Fpsr = fp->__fpsr; lpContext->Fpcr = fp->__fpcr; @@ -916,7 +916,7 @@ void CONTEXTFromNativeContext(const native_context_t *native, LPCONTEXT lpContex lpContext->V[i] = *(NEON128*) &fp->fp_q[i]; } } -#else // TARGET_OSX +#else // TARGET_APPLE const fpsimd_context* fp = GetConstNativeSigSimdContext(native); if (fp) { @@ -927,7 +927,7 @@ void CONTEXTFromNativeContext(const native_context_t *native, LPCONTEXT lpContex lpContext->V[i] = *(NEON128*) &fp->vregs[i]; } } -#endif // TARGET_OSX +#endif // TARGET_APPLE #elif defined(HOST_ARM) const VfpSigFrame* fp = GetConstNativeSigSimdContext(native); if (fp) diff --git a/src/coreclr/pal/src/thread/process.cpp b/src/coreclr/pal/src/thread/process.cpp index 033996645cb5ec..88f34411133606 100644 --- a/src/coreclr/pal/src/thread/process.cpp +++ b/src/coreclr/pal/src/thread/process.cpp @@ -84,10 +84,8 @@ SET_DEFAULT_DEBUG_CHANNEL(PROCESS); // some headers have code with asserts, so d #endif #ifdef __APPLE__ -#include #include #include -#include #include #include extern "C" @@ -229,7 +227,7 @@ PathCharString* gSharedFilesPath = nullptr; #if defined(__NetBSD__) #define CLR_SEM_MAX_NAMELEN 15 #elif defined(__APPLE__) -#define CLR_SEM_MAX_NAMELEN PSEMNAMLEN +#define CLR_SEM_MAX_NAMELEN 31 #elif defined(NAME_MAX) #define CLR_SEM_MAX_NAMELEN (NAME_MAX - 4) #else diff --git a/src/coreclr/pal/src/thread/thread.cpp b/src/coreclr/pal/src/thread/thread.cpp index 07be09edaf252c..8c66057a3b5d40 100644 --- a/src/coreclr/pal/src/thread/thread.cpp +++ b/src/coreclr/pal/src/thread/thread.cpp @@ -2584,7 +2584,7 @@ void * CPalThread::GetStackBase() { void* stackBase; -#ifdef TARGET_OSX +#ifdef TARGET_APPLE // This is a Mac specific method stackBase = pthread_get_stackaddr_np(pthread_self()); #else @@ -2624,7 +2624,7 @@ void * CPalThread::GetStackLimit() { void* stackLimit; -#ifdef TARGET_OSX +#ifdef TARGET_APPLE // This is a Mac specific method stackLimit = ((BYTE *)pthread_get_stackaddr_np(pthread_self()) - pthread_get_stacksize_np(pthread_self())); diff --git a/src/coreclr/tools/aot/ILCompiler.ReadyToRun/ObjectWriter/TargetExtensions.cs b/src/coreclr/tools/aot/ILCompiler.ReadyToRun/ObjectWriter/TargetExtensions.cs index 59a0567d790f20..ef76a9abcce5bb 100644 --- a/src/coreclr/tools/aot/ILCompiler.ReadyToRun/ObjectWriter/TargetExtensions.cs +++ b/src/coreclr/tools/aot/ILCompiler.ReadyToRun/ObjectWriter/TargetExtensions.cs @@ -112,6 +112,11 @@ public static MachineOSOverride MachineOSOverrideFromTarget(this TargetDetails t return MachineOSOverride.Linux; case TargetOS.OSX: + case TargetOS.MacCatalyst: + case TargetOS.iOS: + case TargetOS.iOSSimulator: + case TargetOS.tvOS: + case TargetOS.tvOSSimulator: return MachineOSOverride.Apple; case TargetOS.FreeBSD: diff --git a/src/coreclr/tools/aot/crossgen2/Crossgen2RootCommand.cs b/src/coreclr/tools/aot/crossgen2/Crossgen2RootCommand.cs index 5159f474f01010..4a6b6a2714c659 100644 --- a/src/coreclr/tools/aot/crossgen2/Crossgen2RootCommand.cs +++ b/src/coreclr/tools/aot/crossgen2/Crossgen2RootCommand.cs @@ -295,7 +295,7 @@ public static IEnumerable> GetExtendedHelp(HelpContext _ Console.WriteLine(); string[] ValidArchitectures = new string[] {"arm", "armel", "arm64", "x86", "x64", "riscv64"}; - string[] ValidOS = new string[] {"windows", "linux", "osx"}; + string[] ValidOS = new string[] {"windows", "linux", "osx", "ios", "iossimulator", "tvos", "tvossimulator", "maccatalyst"}; Console.WriteLine(String.Format(SR.SwitchWithDefaultHelp, "--targetos", String.Join("', '", ValidOS), Helpers.GetTargetOS(null).ToString().ToLowerInvariant())); Console.WriteLine(); diff --git a/src/coreclr/tools/dotnet-pgo/dotnet-pgo.csproj b/src/coreclr/tools/dotnet-pgo/dotnet-pgo.csproj index ac3720d4f7e059..a3c667c998bff1 100644 --- a/src/coreclr/tools/dotnet-pgo/dotnet-pgo.csproj +++ b/src/coreclr/tools/dotnet-pgo/dotnet-pgo.csproj @@ -13,7 +13,7 @@ true true dotnet-pgo - win-x64;win-x86;osx-x64 + $(OutputPath) false .NET Performance Guided Optimization Tool diff --git a/src/coreclr/utilcode/executableallocator.cpp b/src/coreclr/utilcode/executableallocator.cpp index 09a4801d2d5dbc..561579ddcae543 100644 --- a/src/coreclr/utilcode/executableallocator.cpp +++ b/src/coreclr/utilcode/executableallocator.cpp @@ -124,7 +124,7 @@ bool ExecutableAllocator::IsDoubleMappingEnabled() { LIMITED_METHOD_CONTRACT; -#if defined(HOST_OSX) && defined(HOST_ARM64) +#if defined(HOST_APPLE) && defined(HOST_ARM64) return false; #else return g_isWXorXEnabled; @@ -135,7 +135,7 @@ bool ExecutableAllocator::IsWXORXEnabled() { LIMITED_METHOD_CONTRACT; -#if defined(HOST_OSX) && defined(HOST_ARM64) +#if defined(HOST_APPLE) && defined(HOST_ARM64) return true; #else return g_isWXorXEnabled; diff --git a/src/coreclr/vm/amd64/asmhelpers.S b/src/coreclr/vm/amd64/asmhelpers.S index 8d83938246a2c9..a02959566b8816 100644 --- a/src/coreclr/vm/amd64/asmhelpers.S +++ b/src/coreclr/vm/amd64/asmhelpers.S @@ -271,7 +271,7 @@ NESTED_ENTRY ProfileTailcallNaked, _TEXT, NoHandler ret NESTED_END ProfileTailcallNaked, _TEXT -#ifdef TARGET_OSX +#ifdef TARGET_APPLE # EXTERN_C void* GetThreadVarsAddress() # # Helper to calculate the address of relevant __thread_vars section that holds the address of symbol tlv_get_address for thread @@ -283,9 +283,9 @@ LEAF_ENTRY GetThreadVarsAddress, _TEXT ret LEAF_END GetThreadVarsAddress, _TEXT // ------------------------------------------------------------------ -#endif // TARGET_OSX +#endif // TARGET_APPLE -#ifndef TARGET_OSX +#ifndef TARGET_APPLE # EXTERN_C void* GetTlsIndexObjectDescOffset(); # diff --git a/src/coreclr/vm/arm64/asmhelpers.S b/src/coreclr/vm/arm64/asmhelpers.S index 0edbb3fdf92fc7..c2c12c7a99e305 100644 --- a/src/coreclr/vm/arm64/asmhelpers.S +++ b/src/coreclr/vm/arm64/asmhelpers.S @@ -777,7 +777,7 @@ LEAF_ENTRY JIT_DispatchIndirectCall, _TEXT br x9 LEAF_END JIT_DispatchIndirectCall, _TEXT -#ifdef TARGET_OSX +#ifdef TARGET_APPLE // ------------------------------------------------------------------ // void* GetThreadVarsAddress() @@ -791,9 +791,9 @@ LEAF_ENTRY GetThreadVarsAddress, _TEXT ret LEAF_END GetThreadVarsAddress, _TEXT // ------------------------------------------------------------------ -#endif // TARGET_OSX +#endif // TARGET_APPLE -#ifndef TARGET_OSX +#ifndef TARGET_APPLE // ------------------------------------------------------------------ // size_t GetThreadStaticsVariableOffset() @@ -811,4 +811,4 @@ LEAF_ENTRY GetThreadStaticsVariableOffset, _TEXT EPILOG_RETURN LEAF_END GetThreadStaticsVariableOffset, _TEXT // ------------------------------------------------------------------ -#endif // !TARGET_OSX +#endif // !TARGET_APPLE diff --git a/src/coreclr/vm/jitinterface.cpp b/src/coreclr/vm/jitinterface.cpp index 442bdd01627ad2..ce954a9e8f9ee1 100644 --- a/src/coreclr/vm/jitinterface.cpp +++ b/src/coreclr/vm/jitinterface.cpp @@ -1338,7 +1338,7 @@ static uint32_t ThreadLocalOffset(void* p) uint8_t* pOurTls = pTls[_tls_index]; return (uint32_t)((uint8_t*)p - pOurTls); } -#elif defined(TARGET_OSX) +#elif defined(TARGET_APPLE) extern "C" void* GetThreadVarsAddress(); static void* GetThreadVarsSectionAddressFromDesc(uint8_t* p) @@ -1432,7 +1432,7 @@ void CEEInfo::getThreadLocalStaticBlocksInfo (CORINFO_THREAD_STATIC_BLOCKS_INFO* pInfo->offsetOfThreadLocalStoragePointer = offsetof(_TEB, ThreadLocalStoragePointer); threadStaticBaseOffset = ThreadLocalOffset(&t_ThreadStatics); -#elif defined(TARGET_OSX) +#elif defined(TARGET_APPLE) pInfo->threadVarsSection = GetThreadVarsSectionAddress(); @@ -1583,7 +1583,7 @@ void CEEInfo::getFieldInfo (CORINFO_RESOLVED_TOKEN * pResolvedToken, // Optimization is disabled for FreeBSD/arm64 #else bool optimizeThreadStaticAccess = true; -#if !defined(TARGET_OSX) && defined(TARGET_UNIX) && defined(TARGET_AMD64) +#if !defined(TARGET_APPLE) && defined(TARGET_UNIX) && defined(TARGET_AMD64) // For linux/x64, check if compiled coreclr as .so file and not single file. // For single file, the `tls_index` might not be accurate. // Do not perform this optimization in such case. @@ -10095,7 +10095,7 @@ void InlinedCallFrame::GetEEInfo(CORINFO_EE_INFO::InlinedCallFrameInfo *pInfo) CORINFO_OS getClrVmOs() { -#ifdef TARGET_OSX +#ifdef TARGET_APPLE return CORINFO_APPLE; #elif defined(TARGET_UNIX) return CORINFO_UNIX; diff --git a/src/coreclr/vm/threads.h b/src/coreclr/vm/threads.h index c84fd8a4dec3be..b1f230a978276e 100644 --- a/src/coreclr/vm/threads.h +++ b/src/coreclr/vm/threads.h @@ -5707,7 +5707,7 @@ inline BOOL IsWriteBarrierCopyEnabled() #ifdef DACCESS_COMPILE return FALSE; #else // DACCESS_COMPILE -#ifdef HOST_OSX +#ifdef HOST_APPLE return TRUE; #else return ExecutableAllocator::IsWXORXEnabled(); diff --git a/src/native/corehost/apphost/static/CMakeLists.txt b/src/native/corehost/apphost/static/CMakeLists.txt index 7a14c7c92ed668..abfc3d3b6d17c4 100644 --- a/src/native/corehost/apphost/static/CMakeLists.txt +++ b/src/native/corehost/apphost/static/CMakeLists.txt @@ -146,7 +146,6 @@ else() System.IO.Compression.Native-Static System.Net.Security.Native-Static System.Native-Static - System.Security.Cryptography.Native.OpenSsl-Static palrt coreclrpal_dac @@ -156,6 +155,10 @@ else() nativeresourcestring ) + if(NOT CLR_CMAKE_TARGET_MACCATALYST AND NOT CLR_CMAKE_TARGET_IOS AND NOT CLR_CMAKE_TARGET_TVOS AND NOT CLR_CMAKE_HOST_ANDROID) + LIST(APPEND NATIVE_LIBS System.Security.Cryptography.Native.OpenSsl-Static) + endif() + # additional requirements for System.IO.Compression.Native include(${CLR_SRC_NATIVE_DIR}/libs/System.IO.Compression.Native/extra_libs.cmake) append_extra_compression_libs(NATIVE_LIBS) @@ -177,7 +180,6 @@ else() endif() set(RUNTIMEINFO_LIB runtimeinfo) - endif() if(CLR_CMAKE_TARGET_APPLE) @@ -195,13 +197,13 @@ endif() # if(CLR_CMAKE_TARGET_APPLE) find_library(COREFOUNDATION CoreFoundation) - find_library(CORESERVICES CoreServices) find_library(SECURITY Security) find_library(SYSTEM System) - LIST(APPEND NATIVE_LIBS + target_link_libraries( + singlefilehost + PUBLIC ${COREFOUNDATION} - ${CORESERVICES} ${SECURITY} ${SYSTEM} ) diff --git a/src/native/corehost/hostmisc/pal.h b/src/native/corehost/hostmisc/pal.h index f482b6df63456b..b0d6eed145044c 100644 --- a/src/native/corehost/hostmisc/pal.h +++ b/src/native/corehost/hostmisc/pal.h @@ -69,7 +69,7 @@ #if defined(TARGET_WINDOWS) #define LIB_PREFIX "" #define LIB_FILE_EXT ".dll" -#elif defined(TARGET_OSX) +#elif defined(TARGET_APPLE) #define LIB_PREFIX "lib" #define LIB_FILE_EXT ".dylib" #else diff --git a/src/native/corehost/hostpolicy/hostpolicy_context.cpp b/src/native/corehost/hostpolicy/hostpolicy_context.cpp index 1a1f63dae073c4..a51e4677605987 100644 --- a/src/native/corehost/hostpolicy/hostpolicy_context.cpp +++ b/src/native/corehost/hostpolicy/hostpolicy_context.cpp @@ -70,10 +70,12 @@ namespace return SystemResolveDllImport(entry_point_name); } +#if !defined(TARGET_APPLE) || defined(TARGET_OSX) if (strcmp(library_name, LIB_NAME("System.Security.Cryptography.Native.OpenSsl")) == 0) { return CryptoResolveDllImport(entry_point_name); } +#endif #endif if (strcmp(library_name, LIB_NAME("System.IO.Compression.Native")) == 0) @@ -95,7 +97,7 @@ namespace } } -#if defined(TARGET_OSX) +#if defined(TARGET_APPLE) if (strcmp(library_name, LIB_NAME("System.Security.Cryptography.Native.Apple")) == 0) { return CryptoAppleResolveDllImport(entry_point_name); diff --git a/src/native/external/libunwind.cmake b/src/native/external/libunwind.cmake index 5dcca157e23ae6..6d4a03fdff4daf 100644 --- a/src/native/external/libunwind.cmake +++ b/src/native/external/libunwind.cmake @@ -447,7 +447,7 @@ if(CLR_CMAKE_HOST_UNIX) list(APPEND libunwind_setjmp_la_SOURCES riscv/siglongjmp.S) endif() - if(CLR_CMAKE_HOST_OSX) + if(CLR_CMAKE_HOST_APPLE) set(LIBUNWIND_SOURCES_BASE remote/mac/missing-functions.c ${libunwind_remote_la_SOURCES} @@ -463,7 +463,7 @@ if(CLR_CMAKE_HOST_UNIX) ${libunwind_dwarf_generic_la_SOURCES} ${libunwind_elf_la_SOURCES} ) - endif(CLR_CMAKE_HOST_OSX) + endif(CLR_CMAKE_HOST_APPLE) else(CLR_CMAKE_HOST_UNIX) if(CLR_CMAKE_TARGET_ARCH_ARM64) diff --git a/src/native/external/libunwind_extras/CMakeLists.txt b/src/native/external/libunwind_extras/CMakeLists.txt index 2bfd2194c969e2..b08325f09e3bf8 100644 --- a/src/native/external/libunwind_extras/CMakeLists.txt +++ b/src/native/external/libunwind_extras/CMakeLists.txt @@ -78,7 +78,7 @@ if(CLR_CMAKE_HOST_UNIX) ###TODO: maybe add options for RISCV64 endif() - if (CLR_CMAKE_HOST_OSX) + if (CLR_CMAKE_HOST_APPLE) add_definitions(-DUNW_REMOTE_ONLY) add_compile_options(-Wno-sometimes-uninitialized) add_compile_options(-Wno-implicit-function-declaration) @@ -86,7 +86,7 @@ if(CLR_CMAKE_HOST_UNIX) # Our posix abstraction layer will provide these headers set(HAVE_ELF_H 1) set(HAVE_ENDIAN_H 1) - endif(CLR_CMAKE_HOST_OSX) + endif(CLR_CMAKE_HOST_APPLE) endif(CLR_CMAKE_HOST_UNIX) @@ -146,7 +146,7 @@ if(CLR_CMAKE_HOST_WIN32) add_compile_options(-wd4311) # pointer truncation from 'unw_word_t *' to 'long' add_compile_options(-wd4475) # 'fprintf' : length modifier 'L' cannot be used add_compile_options(-wd4477) # fprintf argument type -elseif(CLR_CMAKE_HOST_OSX) +elseif(CLR_CMAKE_HOST_APPLE) include_directories(${CLR_SRC_NATIVE_DIR}/external/libunwind/include/remote) include_directories(${CLR_SRC_NATIVE_DIR}/external/libunwind/include/remote/mac) endif (CLR_CMAKE_HOST_WIN32) @@ -166,11 +166,11 @@ if(CLR_CMAKE_HOST_WIN32) endif(CLR_CMAKE_HOST_WIN32) if(CLR_CMAKE_HOST_UNIX) - if(CLR_CMAKE_HOST_OSX) + if(CLR_CMAKE_HOST_APPLE) add_library(libunwind_dac OBJECT ${LIBUNWIND_SOURCES}) else() add_library(libunwind OBJECT ${LIBUNWIND_SOURCES}) - endif(CLR_CMAKE_HOST_OSX) + endif(CLR_CMAKE_HOST_APPLE) else(CLR_CMAKE_HOST_UNIX) set_source_files_properties(${CLR_DIR}/pal/src/exception/remote-unwind.cpp PROPERTIES COMPILE_FLAGS /TP INCLUDE_DIRECTORIES ${CLR_DIR}/inc) diff --git a/src/native/libs/System.Native/CMakeLists.txt b/src/native/libs/System.Native/CMakeLists.txt index 4cac1051f6739b..ac757b4fef7a61 100644 --- a/src/native/libs/System.Native/CMakeLists.txt +++ b/src/native/libs/System.Native/CMakeLists.txt @@ -144,7 +144,7 @@ if (GEN_SHARED_LIB) install_with_stripped_symbols (System.Native PROGRAMS .) endif () -if (NOT GEN_SHARED_LIB AND NOT CLR_CMAKE_TARGET_MACCATALYST AND NOT CLR_CMAKE_TARGET_IOS AND NOT CLR_CMAKE_TARGET_TVOS AND NOT CLR_CMAKE_TARGET_ANDROID AND NOT CLR_CMAKE_TARGET_BROWSER AND NOT CLR_CMAKE_TARGET_WASI) +if (NOT GEN_SHARED_LIB AND NOT CLR_CMAKE_TARGET_ANDROID AND NOT CLR_CMAKE_TARGET_BROWSER AND NOT CLR_CMAKE_TARGET_WASI) set(NATIVE_SOURCES ${NATIVE_SOURCES} entrypoints.c) endif() diff --git a/src/native/libs/System.Native/entrypoints.c b/src/native/libs/System.Native/entrypoints.c index 30cc86f2aff976..4280d5aa6a9eec 100644 --- a/src/native/libs/System.Native/entrypoints.c +++ b/src/native/libs/System.Native/entrypoints.c @@ -34,6 +34,7 @@ static const Entry s_sysNative[] = { DllImportEntry(SystemNative_FStat) +#if !defined(TARGET_APPLE) || defined(TARGET_OSX) DllImportEntry(SystemNative_GetWindowSize) DllImportEntry(SystemNative_SetWindowSize) DllImportEntry(SystemNative_IsATty) @@ -47,6 +48,7 @@ static const Entry s_sysNative[] = DllImportEntry(SystemNative_ReadStdin) DllImportEntry(SystemNative_GetSignalForBreak) DllImportEntry(SystemNative_SetSignalForBreak) +#endif DllImportEntry(SystemNative_GetSystemTimeAsTicks) DllImportEntry(SystemNative_GetDefaultTimeZone) DllImportEntry(SystemNative_GetTimeZoneData) diff --git a/src/native/libs/System.Net.Security.Native/CMakeLists.txt b/src/native/libs/System.Net.Security.Native/CMakeLists.txt index b0e02f41509b58..e8efae3d33a795 100644 --- a/src/native/libs/System.Net.Security.Native/CMakeLists.txt +++ b/src/native/libs/System.Net.Security.Native/CMakeLists.txt @@ -19,7 +19,7 @@ if (GEN_SHARED_LIB) ) endif() -if (NOT GEN_SHARED_LIB AND NOT CLR_CMAKE_TARGET_MACCATALYST AND NOT CLR_CMAKE_TARGET_IOS AND NOT CLR_CMAKE_TARGET_TVOS AND NOT CLR_CMAKE_TARGET_ANDROID AND NOT CLR_CMAKE_TARGET_BROWSER AND NOT CLR_CMAKE_TARGET_WASI) +if (NOT GEN_SHARED_LIB AND NOT CLR_CMAKE_TARGET_ANDROID AND NOT CLR_CMAKE_TARGET_BROWSER AND NOT CLR_CMAKE_TARGET_WASI) set(NATIVEGSS_SOURCES ${NATIVEGSS_SOURCES} entrypoints.c) endif() diff --git a/src/native/libs/System.Security.Cryptography.Native.Apple/CMakeLists.txt b/src/native/libs/System.Security.Cryptography.Native.Apple/CMakeLists.txt index b847f5c3cd68b9..e621e3c3091210 100644 --- a/src/native/libs/System.Security.Cryptography.Native.Apple/CMakeLists.txt +++ b/src/native/libs/System.Security.Cryptography.Native.Apple/CMakeLists.txt @@ -77,7 +77,7 @@ if (GEN_SHARED_LIB) ) endif() -if (NOT GEN_SHARED_LIB AND NOT CLR_CMAKE_TARGET_MACCATALYST AND NOT CLR_CMAKE_TARGET_IOS AND NOT CLR_CMAKE_TARGET_TVOS) +if (NOT GEN_SHARED_LIB) set(NATIVECRYPTO_SOURCES ${NATIVECRYPTO_SOURCES} entrypoints.c) endif() diff --git a/src/native/libs/System.Security.Cryptography.Native.Apple/entrypoints.c b/src/native/libs/System.Security.Cryptography.Native.Apple/entrypoints.c index 099fb343947110..d7ce3025fe1cf0 100644 --- a/src/native/libs/System.Security.Cryptography.Native.Apple/entrypoints.c +++ b/src/native/libs/System.Security.Cryptography.Native.Apple/entrypoints.c @@ -8,7 +8,6 @@ #include "pal_ecc.h" #include "pal_hmac.h" #include "pal_keyagree.h" -#include "pal_keychain_macos.h" #include "pal_keyderivation.h" #include "pal_random.h" #include "pal_rsa.h" @@ -19,17 +18,25 @@ #include "pal_ssl.h" #include "pal_swiftbindings.h" #include "pal_symmetric.h" -#include "pal_trust_macos.h" #include "pal_x509.h" -#include "pal_x509_macos.h" #include "pal_x509chain.h" +#if defined(TARGET_OSX) +#include "pal_trust_macos.h" +#include "pal_keychain_macos.h" +#include "pal_x509_macos.h" +#else +#include "pal_keychain_ios.h" +#include "pal_x509_ios.h" +#endif static const Entry s_cryptoAppleNative[] = { +#ifdef TARGET_OSX DllImportEntry(AppleCryptoNative_AesGcmEncrypt) DllImportEntry(AppleCryptoNative_AesGcmDecrypt) DllImportEntry(AppleCryptoNative_ChaCha20Poly1305Encrypt) DllImportEntry(AppleCryptoNative_ChaCha20Poly1305Decrypt) +#endif DllImportEntry(AppleCryptoNative_DigestFree) DllImportEntry(AppleCryptoNative_DigestCreate) DllImportEntry(AppleCryptoNative_DigestUpdate) @@ -48,15 +55,21 @@ static const Entry s_cryptoAppleNative[] = DllImportEntry(AppleCryptoNative_HmacFinal) DllImportEntry(AppleCryptoNative_HmacCurrent) DllImportEntry(AppleCryptoNative_HmacOneShot) +#ifdef TARGET_OSX DllImportEntry(AppleCryptoNative_SecKeychainItemCopyKeychain) DllImportEntry(AppleCryptoNative_SecKeychainCopyDefault) DllImportEntry(AppleCryptoNative_SecKeychainCreate) DllImportEntry(AppleCryptoNative_SecKeychainDelete) +#endif DllImportEntry(AppleCryptoNative_SecKeychainEnumerateCerts) +#ifdef TARGET_OSX DllImportEntry(AppleCryptoNative_SecKeychainOpen) DllImportEntry(AppleCryptoNative_SecKeychainUnlock) +#endif DllImportEntry(AppleCryptoNative_SecKeychainEnumerateIdentities) +#ifdef TARGET_OSX DllImportEntry(AppleCryptoNative_SetKeychainNeverLock) +#endif DllImportEntry(AppleCryptoNative_SslCopyCADistinguishedNames) DllImportEntry(AppleCryptoNative_SslCopyCertChain) DllImportEntry(AppleCryptoNative_SslIsHostnameMatch) @@ -77,8 +90,10 @@ static const Entry s_cryptoAppleNative[] = DllImportEntry(AppleCryptoNative_RsaEncryptionPrimitive) DllImportEntry(AppleCryptoNative_RsaVerificationPrimitive) DllImportEntry(AppleCryptoNative_SecCopyErrorMessageString) +#ifdef TARGET_OSX DllImportEntry(AppleCryptoNative_SecKeyExport) DllImportEntry(AppleCryptoNative_SecKeyImportEphemeral) +#endif DllImportEntry(AppleCryptoNative_SecKeyGetSimpleKeySizeInBytes) DllImportEntry(AppleCryptoNative_SecKeyCreateSignature) DllImportEntry(AppleCryptoNative_SecKeyVerifySignature) @@ -105,10 +120,12 @@ static const Entry s_cryptoAppleNative[] = DllImportEntry(AppleCryptoNative_CryptorCreate) DllImportEntry(AppleCryptoNative_CryptorUpdate) DllImportEntry(AppleCryptoNative_CryptorReset) +#ifdef TARGET_OSX DllImportEntry(AppleCryptoNative_StoreEnumerateUserRoot) DllImportEntry(AppleCryptoNative_StoreEnumerateMachineRoot) DllImportEntry(AppleCryptoNative_StoreEnumerateUserDisallowed) DllImportEntry(AppleCryptoNative_StoreEnumerateMachineDisallowed) +#endif DllImportEntry(AppleCryptoNative_X509ChainCreate) DllImportEntry(AppleCryptoNative_X509DemuxAndRetainHandle) DllImportEntry(AppleCryptoNative_X509GetContentType) @@ -117,10 +134,14 @@ static const Entry s_cryptoAppleNative[] = DllImportEntry(AppleCryptoNative_X509CopyPrivateKeyFromIdentity) DllImportEntry(AppleCryptoNative_X509ImportCollection) DllImportEntry(AppleCryptoNative_X509ImportCertificate) +#ifdef TARGET_OSX DllImportEntry(AppleCryptoNative_X509ExportData) +#endif DllImportEntry(AppleCryptoNative_X509GetRawData) +#ifdef TARGET_OSX DllImportEntry(AppleCryptoNative_X509CopyWithPrivateKey) DllImportEntry(AppleCryptoNative_X509MoveToKeychain) +#endif DllImportEntry(AppleCryptoNative_X509ChainCreateDefaultPolicy) DllImportEntry(AppleCryptoNative_X509ChainCreateRevocationPolicy) DllImportEntry(AppleCryptoNative_X509ChainEvaluate) diff --git a/src/native/libs/configure.cmake b/src/native/libs/configure.cmake index bba930e0c0072f..2044acab84875c 100644 --- a/src/native/libs/configure.cmake +++ b/src/native/libs/configure.cmake @@ -9,7 +9,7 @@ include(CheckTypeSize) include(CheckLibraryExists) include(CheckFunctionExists) -if (CLR_CMAKE_TARGET_OSX) +if (CLR_CMAKE_TARGET_APPLE) # Xcode's clang does not include /usr/local/include by default, but brew's does. # This ensures an even playing field. include_directories(SYSTEM /usr/local/include) From 684d6018757d565f7ee6009e34ac5563ed574507 Mon Sep 17 00:00:00 2001 From: Filip Navara Date: Sat, 29 Jul 2023 09:04:24 +0200 Subject: [PATCH 02/24] Build the singlefilehost even if we don't currently use it --- src/coreclr/CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/coreclr/CMakeLists.txt b/src/coreclr/CMakeLists.txt index 1c76cd3643da4c..27a71f84997550 100644 --- a/src/coreclr/CMakeLists.txt +++ b/src/coreclr/CMakeLists.txt @@ -55,7 +55,7 @@ include(components.cmake) #--------------------------- # Build the single file host #--------------------------- -if(NOT CLR_CROSS_COMPONENTS_BUILD AND NOT CLR_CMAKE_HOST_MACCATALYST AND NOT CLR_CMAKE_HOST_IOS AND NOT CLR_CMAKE_HOST_TVOS) +if(NOT CLR_CROSS_COMPONENTS_BUILD) set(CLR_SINGLE_FILE_HOST_ONLY 1) add_subdirectory(${CLR_SRC_NATIVE_DIR}/corehost/apphost/static Corehost.Static) add_dependencies(runtime singlefilehost) From 5cc117bdd0b2a604a3f37566ebebbbf6dfc27f85 Mon Sep 17 00:00:00 2001 From: Filip Navara Date: Sat, 29 Jul 2023 14:59:14 +0200 Subject: [PATCH 03/24] Make coreclrhost.h compatible v C(not++) --- src/coreclr/hosts/inc/coreclrhost.h | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/coreclr/hosts/inc/coreclrhost.h b/src/coreclr/hosts/inc/coreclrhost.h index 01eeac600a2240..2988ea0452acfd 100644 --- a/src/coreclr/hosts/inc/coreclrhost.h +++ b/src/coreclr/hosts/inc/coreclrhost.h @@ -19,9 +19,15 @@ // For each hosting API, we define a function prototype and a function pointer // The prototype is useful for implicit linking against the dynamic coreclr // library and the pointer for explicit dynamic loading (dlopen, LoadLibrary) +#ifdef __cplusplus #define CORECLR_HOSTING_API(function, ...) \ extern "C" int CORECLR_CALLING_CONVENTION function(__VA_ARGS__); \ typedef int (CORECLR_CALLING_CONVENTION *function##_ptr)(__VA_ARGS__) +#else +#define CORECLR_HOSTING_API(function, ...) \ + int CORECLR_CALLING_CONVENTION function(__VA_ARGS__); \ + typedef int (CORECLR_CALLING_CONVENTION *function##_ptr)(__VA_ARGS__) +#endif // // Initialize the CoreCLR. Creates and starts CoreCLR host and creates an app domain From ff2af8adbdc482e93db4a72f809eae78e2bc3cf2 Mon Sep 17 00:00:00 2001 From: Filip Navara Date: Sat, 29 Jul 2023 14:59:32 +0200 Subject: [PATCH 04/24] Fix TargetOS::IsMacOS --- src/coreclr/inc/targetosarch.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/coreclr/inc/targetosarch.h b/src/coreclr/inc/targetosarch.h index 06f22d8ee487b0..00fe5b70647e7d 100644 --- a/src/coreclr/inc/targetosarch.h +++ b/src/coreclr/inc/targetosarch.h @@ -22,7 +22,7 @@ class TargetOS static bool OSSettingConfigured; static bool IsApplePlatform; #else -#if defined(TARGET_OSX) +#if defined(TARGET_APPLE) static const bool IsApplePlatform = true; #else static const bool IsApplePlatform = false; From 24a5a44970783b75d47558a30f61c052c7843c82 Mon Sep 17 00:00:00 2001 From: Filip Navara Date: Sat, 29 Jul 2023 15:00:21 +0200 Subject: [PATCH 05/24] WIP: AppleAppBuilder support for CoreCLR --- .../apple/build/AppleBuild.InTree.targets | 7 + .../msbuild/apple/build/AppleBuild.targets | 8 +- src/tasks/AppleAppBuilder/AppleAppBuilder.cs | 37 ++--- .../AppleAppBuilder/AppleAppBuilder.csproj | 1 + .../Templates/CMakeLists.txt.template | 7 +- .../Templates/runtime-coreclr.m | 108 +++++++++++++++ src/tasks/AppleAppBuilder/Xcode.cs | 127 ++++++++++++------ src/tests/Directory.Build.props | 2 +- 8 files changed, 231 insertions(+), 66 deletions(-) create mode 100644 src/tasks/AppleAppBuilder/Templates/runtime-coreclr.m diff --git a/src/mono/msbuild/apple/build/AppleBuild.InTree.targets b/src/mono/msbuild/apple/build/AppleBuild.InTree.targets index 3d982701bf8b66..1b30b9510572fe 100644 --- a/src/mono/msbuild/apple/build/AppleBuild.InTree.targets +++ b/src/mono/msbuild/apple/build/AppleBuild.InTree.targets @@ -12,6 +12,13 @@ + + + + + diff --git a/src/mono/msbuild/apple/build/AppleBuild.targets b/src/mono/msbuild/apple/build/AppleBuild.targets index 35d0f16c35b4b8..7b8f91d79536ad 100644 --- a/src/mono/msbuild/apple/build/AppleBuild.targets +++ b/src/mono/msbuild/apple/build/AppleBuild.targets @@ -53,12 +53,12 @@ $([MSBuild]::NormalizeDirectory('$(OutDir)', 'Bundle')) $(AppleBundleDir) - <_MonoHeaderPath Condition="'$(UseNativeAOTRuntime)' != 'true'">$([MSBuild]::NormalizeDirectory($(MicrosoftNetCoreAppRuntimePackRidNativeDir), 'include', 'mono-2.0')) + <_MonoHeaderPath Condition="'$(AppleAppBuilderRuntime)' == 'MonoVM'">$([MSBuild]::NormalizeDirectory($(MicrosoftNetCoreAppRuntimePackRidNativeDir), 'include', 'mono-2.0')) <_AotModuleTablePath>$(AppleBundleDir)\modules.m $(AssemblyName) - + @@ -294,6 +294,7 @@ + UseConsoleUITemplate="$(UseConsoleUITemplate)"> diff --git a/src/tasks/AppleAppBuilder/AppleAppBuilder.cs b/src/tasks/AppleAppBuilder/AppleAppBuilder.cs index 0e16db616d62e8..231e3a6653e3ca 100644 --- a/src/tasks/AppleAppBuilder/AppleAppBuilder.cs +++ b/src/tasks/AppleAppBuilder/AppleAppBuilder.cs @@ -177,9 +177,9 @@ public string TargetOS public bool StripSymbolTable { get; set; } /// - /// Bundles the application for NativeAOT runtime. Default runtime is Mono. + /// Bundles the application for specific runtime. Valid values: MonoVM, NativeAOT, CoreCLR. /// - public bool UseNativeAOTRuntime { get; set; } + public string Runtime { get; set; } = "MonoVM"; /// /// Extra native dependencies to link into the app @@ -193,39 +193,44 @@ public string TargetOS public void ValidateRuntimeSelection() { - if (UseNativeAOTRuntime) + if (Runtime == "NativeAOT" || Runtime == "CoreCLR") { if (!string.IsNullOrEmpty(MonoRuntimeHeaders)) - throw new ArgumentException($"Property \"{nameof(MonoRuntimeHeaders)}\" is not supported with NativeAOT runtime and will be ignored."); + throw new ArgumentException($"Property \"{nameof(MonoRuntimeHeaders)}\" is not supported with {Runtime} runtime and will be ignored."); - if (!string.IsNullOrEmpty(MainLibraryFileName)) - throw new ArgumentException($"Property \"{nameof(MainLibraryFileName)}\" is not supported with NativeAOT runtime and will be ignored."); + if (!string.IsNullOrEmpty(MainLibraryFileName) && Runtime == "NativeAOT") + throw new ArgumentException($"Property \"{nameof(MainLibraryFileName)}\" is not supported with {Runtime} runtime and will be ignored."); if (ForceInterpreter) - throw new ArgumentException($"Property \"{nameof(ForceInterpreter)}\" is not supported with NativeAOT runtime and will be ignored."); + throw new ArgumentException($"Property \"{nameof(ForceInterpreter)}\" is not supported with {Runtime} runtime and will be ignored."); if (ForceAOT) - throw new ArgumentException($"Property \"{nameof(ForceAOT)}\" is not supported with NativeAOT runtime and will be ignored."); + throw new ArgumentException($"Property \"{nameof(ForceAOT)}\" is not supported with {Runtime} runtime and will be ignored."); - if (RuntimeComponents.Length > 0) + if (RuntimeComponents.Length > 0 && Runtime == "NativeAOT") throw new ArgumentException($"Item \"{nameof(RuntimeComponents)}\" is not supported with NativeAOT runtime and will be ignored."); if (!string.IsNullOrEmpty(DiagnosticPorts)) - throw new ArgumentException($"Property \"{nameof(DiagnosticPorts)}\" is not supported with NativeAOT runtime and will be ignored."); + throw new ArgumentException($"Property \"{nameof(DiagnosticPorts)}\" is not supported with {Runtime} runtime and will be ignored."); if (EnableRuntimeLogging) - throw new ArgumentException($"Property \"{nameof(EnableRuntimeLogging)}\" is not supported with NativeAOT runtime and will be ignored."); + throw new ArgumentException($"Property \"{nameof(EnableRuntimeLogging)}\" is not supported with {Runtime} runtime and will be ignored."); } - else + else if (Runtime == "MonoVM" || Runtime == "") { if (string.IsNullOrEmpty(MonoRuntimeHeaders)) throw new ArgumentException($"The \"{nameof(AppleAppBuilderTask)}\" task was not given a value for the required parameter \"{nameof(MonoRuntimeHeaders)}\" when using Mono runtime."); + Runtime = "MonoVM"; + } + else + { + throw new ArgumentException($"The \"{nameof(AppleAppBuilderTask)}\" task was not given an invalid value for parameter \"{nameof(Runtime)}\"."); } } public override bool Execute() { - bool shouldStaticLink = !EnableAppSandbox; + bool shouldStaticLink = Runtime != "CoreCLR" && !EnableAppSandbox; bool isDevice = (TargetOS == TargetNames.iOS || TargetOS == TargetNames.tvOS); ValidateRuntimeSelection(); @@ -288,7 +293,7 @@ public override bool Execute() } } - if (!ForceInterpreter && (shouldStaticLink || ForceAOT) && (assemblerFiles.Count == 0 && !UseNativeAOTRuntime)) + if (!ForceInterpreter && (shouldStaticLink || ForceAOT) && (assemblerFiles.Count == 0 && Runtime == "MonoVM")) { throw new InvalidOperationException("Need list of AOT files for static linked builds."); } @@ -320,7 +325,7 @@ public override bool Execute() if (GenerateXcodeProject) { XcodeProjectPath = generator.GenerateXCode(ProjectName, MainLibraryFileName, assemblerFiles, assemblerDataFiles, assemblerFilesToLink, extraLinkerArgs, excludes, - AppDir, binDir, MonoRuntimeHeaders, !shouldStaticLink, UseConsoleUITemplate, ForceAOT, ForceInterpreter, InvariantGlobalization, HybridGlobalization, Optimized, EnableRuntimeLogging, EnableAppSandbox, DiagnosticPorts, RuntimeComponents, NativeMainSource, UseNativeAOTRuntime, IsLibraryMode); + AppDir, binDir, MonoRuntimeHeaders, !shouldStaticLink, UseConsoleUITemplate, ForceAOT, ForceInterpreter, InvariantGlobalization, HybridGlobalization, Optimized, EnableRuntimeLogging, EnableAppSandbox, DiagnosticPorts, RuntimeComponents, NativeMainSource, Runtime, IsLibraryMode); if (BuildAppBundle) { @@ -346,7 +351,7 @@ public override bool Execute() else if (GenerateCMakeProject) { generator.GenerateCMake(ProjectName, MainLibraryFileName, assemblerFiles, assemblerDataFiles, assemblerFilesToLink, extraLinkerArgs, excludes, - AppDir, binDir, MonoRuntimeHeaders, !shouldStaticLink, UseConsoleUITemplate, ForceAOT, ForceInterpreter, InvariantGlobalization, HybridGlobalization, Optimized, EnableRuntimeLogging, EnableAppSandbox, DiagnosticPorts, RuntimeComponents, NativeMainSource, UseNativeAOTRuntime, IsLibraryMode); + AppDir, binDir, MonoRuntimeHeaders, !shouldStaticLink, UseConsoleUITemplate, ForceAOT, ForceInterpreter, InvariantGlobalization, HybridGlobalization, Optimized, EnableRuntimeLogging, EnableAppSandbox, DiagnosticPorts, RuntimeComponents, NativeMainSource, Runtime, IsLibraryMode); } return true; diff --git a/src/tasks/AppleAppBuilder/AppleAppBuilder.csproj b/src/tasks/AppleAppBuilder/AppleAppBuilder.csproj index fe87320a2da81f..f02dd024f14a1f 100644 --- a/src/tasks/AppleAppBuilder/AppleAppBuilder.csproj +++ b/src/tasks/AppleAppBuilder/AppleAppBuilder.csproj @@ -9,6 +9,7 @@ + diff --git a/src/tasks/AppleAppBuilder/Templates/CMakeLists.txt.template b/src/tasks/AppleAppBuilder/Templates/CMakeLists.txt.template index 614d9a7cf64fbe..9355e30fb39aea 100644 --- a/src/tasks/AppleAppBuilder/Templates/CMakeLists.txt.template +++ b/src/tasks/AppleAppBuilder/Templates/CMakeLists.txt.template @@ -3,6 +3,9 @@ cmake_minimum_required(VERSION 3.16) project(%ProjectName%) enable_language(OBJC ASM) +set(CMAKE_BUILD_WITH_INSTALL_RPATH ON) +set(CMAKE_INSTALL_RPATH "@executable_path") + set(APP_RESOURCES %AppResources% ) @@ -28,8 +31,8 @@ endif() %Defines% -if(NOT %UseNativeAOTRuntime%) - include_directories("%MonoInclude%") +if(%UseMonoRuntime%) + include_directories("%RuntimeInclude%") endif() set_target_properties(%ProjectName% %AotTargetsList% PROPERTIES diff --git a/src/tasks/AppleAppBuilder/Templates/runtime-coreclr.m b/src/tasks/AppleAppBuilder/Templates/runtime-coreclr.m new file mode 100644 index 00000000000000..142b88c73ee50a --- /dev/null +++ b/src/tasks/AppleAppBuilder/Templates/runtime-coreclr.m @@ -0,0 +1,108 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +#import +#include "coreclrhost.h" +#include +#import +#include +#include +#include +#include + +#import "util.h" + +#define APPLE_RUNTIME_IDENTIFIER "//%APPLE_RUNTIME_IDENTIFIER%" + +const char * +get_bundle_path (void) +{ + static char *bundle_path = NULL; + if (bundle_path) + return bundle_path; + NSBundle* main_bundle = [NSBundle mainBundle]; + NSString* path = [main_bundle bundlePath]; + +#if TARGET_OS_MACCATALYST + path = [path stringByAppendingString:@"/Contents/Resources"]; +#endif + + bundle_path = strdup ([path UTF8String]); + + return bundle_path; +} + +void +mono_ios_runtime_init (void) +{ +#if INVARIANT_GLOBALIZATION + setenv ("DOTNET_SYSTEM_GLOBALIZATION_INVARIANT", "1", TRUE); +#endif + +#if HYBRID_GLOBALIZATION + setenv ("DOTNET_SYSTEM_GLOBALIZATION_HYBRID", "1", TRUE); +#endif + + + // build using DiagnosticPorts property in AppleAppBuilder + // or set DOTNET_DiagnosticPorts env via mlaunch, xharness when undefined. + // NOTE, using DOTNET_DiagnosticPorts requires app build using AppleAppBuilder and RuntimeComponents=diagnostics_tracing +#ifdef DIAGNOSTIC_PORTS + setenv ("DOTNET_DiagnosticPorts", DIAGNOSTIC_PORTS, true); +#endif + + char **managed_argv; + int argi = get_managed_args (&managed_argv); + + bool wait_for_debugger = FALSE; + + const char* bundle = get_bundle_path (); + chdir (bundle); + + char icu_dat_path [1024]; + int res; +#if defined(HYBRID_GLOBALIZATION) + res = snprintf (icu_dat_path, sizeof (icu_dat_path) - 1, "%s/%s", bundle, "icudt_hybrid.dat"); +#else + res = snprintf (icu_dat_path, sizeof (icu_dat_path) - 1, "%s/%s", bundle, "icudt.dat"); +#endif + assert (res > 0); + + // TODO: set TRUSTED_PLATFORM_ASSEMBLIES, APP_PATHS and NATIVE_DLL_SEARCH_DIRECTORIES + const char *appctx_keys [] = { + "RUNTIME_IDENTIFIER", + "APP_CONTEXT_BASE_DIRECTORY", +#if !defined(INVARIANT_GLOBALIZATION) + "ICU_DAT_FILE_PATH" +#endif + }; + const char *appctx_values [] = { + APPLE_RUNTIME_IDENTIFIER, + bundle, +#if !defined(INVARIANT_GLOBALIZATION) + icu_dat_path +#endif + }; + + const char* executable = "%EntryPointLibName%"; + const char *executablePath = [[[[NSBundle mainBundle] executableURL] path] UTF8String]; + unsigned int coreclr_domainId = 0; + void *coreclr_handle = NULL; + + coreclr_initialize ( + executablePath, executable, + sizeof (appctx_keys) / sizeof (appctx_keys [0]), appctx_keys, appctx_values, + &coreclr_handle, &coreclr_domainId); + + char path [1024]; + res = snprintf (path, sizeof (path) - 1, "%s/%s", bundle, executable); + assert (res > 0); + + coreclr_execute_assembly (coreclr_handle, coreclr_domainId, argi, managed_argv, path, &res); + // Print this so apps parsing logs can detect when we exited + os_log_info (OS_LOG_DEFAULT, EXIT_CODE_TAG ": %d", res); + + free_managed_args (&managed_argv, argi); + + exit (res); +} diff --git a/src/tasks/AppleAppBuilder/Xcode.cs b/src/tasks/AppleAppBuilder/Xcode.cs index aed4bf7dfaeba3..f2bb963b71a723 100644 --- a/src/tasks/AppleAppBuilder/Xcode.cs +++ b/src/tasks/AppleAppBuilder/Xcode.cs @@ -177,7 +177,7 @@ public string GenerateXCode( IEnumerable excludes, string workspace, string binDir, - string monoInclude, + string runtimeInclude, bool preferDylibs, bool useConsoleUiTemplate, bool forceAOT, @@ -190,10 +190,10 @@ public string GenerateXCode( string? diagnosticPorts, IEnumerable runtimeComponents, string? nativeMainSource = null, - bool useNativeAOTRuntime = false, + string? runtime = "MonoVM", bool isLibraryMode = false) { - var cmakeDirectoryPath = GenerateCMake(projectName, entryPointLib, asmFiles, asmDataFiles, asmLinkFiles, extraLinkerArgs, excludes, workspace, binDir, monoInclude, preferDylibs, useConsoleUiTemplate, forceAOT, forceInterpreter, invariantGlobalization, hybridGlobalization, optimized, enableRuntimeLogging, enableAppSandbox, diagnosticPorts, runtimeComponents, nativeMainSource, useNativeAOTRuntime, isLibraryMode); + var cmakeDirectoryPath = GenerateCMake(projectName, entryPointLib, asmFiles, asmDataFiles, asmLinkFiles, extraLinkerArgs, excludes, workspace, binDir, monoInclude, preferDylibs, useConsoleUiTemplate, forceAOT, forceInterpreter, invariantGlobalization, hybridGlobalization, optimized, enableRuntimeLogging, enableAppSandbox, diagnosticPorts, runtimeComponents, nativeMainSource, runtime, isLibraryMode); CreateXcodeProject(projectName, cmakeDirectoryPath); return Path.Combine(binDir, projectName, projectName + ".xcodeproj"); } @@ -250,7 +250,7 @@ public string GenerateCMake( IEnumerable excludes, string workspace, string binDir, - string monoInclude, + string runtimeInclude, bool preferDylibs, bool useConsoleUiTemplate, bool forceAOT, @@ -263,14 +263,14 @@ public string GenerateCMake( string? diagnosticPorts, IEnumerable runtimeComponents, string? nativeMainSource = null, - bool useNativeAOTRuntime = false, + string? runtime = "MonoVM", bool isLibraryMode = false) { // bundle everything as resources excluding native files - var predefinedExcludes = new List { ".dll.o", ".dll.s", ".dwarf", ".m", ".h", ".a", ".bc", "libmonosgen-2.0.dylib", "libcoreclr.dylib", "icudt*" }; + var predefinedExcludes = new List { ".dll.o", ".dll.s", ".dwarf", ".m", ".h", ".a", ".bc", "libmonosgen-2.0.dylib", "icudt*" }; // TODO: All of these exclusions shouldn't be needed once we carefully construct the publish folder on Helix - if (useNativeAOTRuntime) + if (runtime == "NativeAOT") { predefinedExcludes.Add(".dll"); predefinedExcludes.Add(".pdb"); @@ -289,6 +289,10 @@ public string GenerateCMake( { predefinedExcludes.Add(".pdb"); } + if (runtime != "CoreCLR") + { + predefinedExcludes.Add("libcoreclr.dylib"); + } string[] resources = Directory.GetFileSystemEntries(workspace, "", SearchOption.TopDirectoryOnly) .Where(f => !predefinedExcludes.Any(e => (!e.EndsWith('*') && f.EndsWith(e, StringComparison.InvariantCultureIgnoreCase)) || (e.EndsWith('*') && Path.GetFileName(f).StartsWith(e.TrimEnd('*'), StringComparison.InvariantCultureIgnoreCase) && @@ -338,11 +342,12 @@ public string GenerateCMake( string cmakeTemplateName = (isLibraryMode) ? "CMakeLists-librarymode.txt.template" : "CMakeLists.txt.template"; string cmakeLists = Utils.GetEmbeddedResource(cmakeTemplateName) - .Replace("%UseNativeAOTRuntime%", useNativeAOTRuntime ? "TRUE" : "FALSE") + .Replace("%UseMonoRuntime%", runtime == "MonoVM" ? "TRUE" : "FALSE") + .Replace("%UseNativeAOTRuntime%", runtime == "NativeAOT" ? "TRUE" : "FALSE") .Replace("%ProjectName%", projectName) .Replace("%AppResources%", appResources) .Replace("%MainSource%", nativeMainSource) - .Replace("%MonoInclude%", monoInclude) + .Replace("%RuntimeInclude%", runtimeInclude) .Replace("%HardenedRuntime%", hardenedRuntime ? "TRUE" : "FALSE"); string toLink = ""; @@ -407,26 +412,36 @@ public string GenerateCMake( } string[] dylibs = Directory.GetFiles(workspace, "*.dylib"); - foreach (string lib in Directory.GetFiles(workspace, "*.a")) + if (runtime == "CoreCLR") { - // all component libs already added to linker. - if (allComponentLibs.Any(lib.Contains)) - continue; - - string libName = Path.GetFileNameWithoutExtension(lib); - // libmono must always be statically linked, for other librarires we can use dylibs - bool dylibExists = libName != "libmonosgen-2.0" && dylibs.Any(dylib => Path.GetFileName(dylib) == libName + ".dylib"); - - if (useNativeAOTRuntime) + foreach (string lib in dylibs) { - // link NativeAOT framework libs without '-force_load' - toLink += $" {lib}{Environment.NewLine}"; + toLink += $" \"-force_load {lib}\"{Environment.NewLine}"; } - else if (forceAOT || !(preferDylibs && dylibExists)) + } + else + { + foreach (string lib in Directory.GetFiles(workspace, "*.a")) { - // these libraries are pinvoked - // -force_load will be removed once we enable direct-pinvokes for AOT - toLink += $" \"-force_load {lib}\"{Environment.NewLine}"; + // all component libs already added to linker. + if (allComponentLibs.Any(lib.Contains)) + continue; + + string libName = Path.GetFileNameWithoutExtension(lib); + // libmono must always be statically linked, for other librarires we can use dylibs + bool dylibExists = libName != "libmonosgen-2.0" && dylibs.Any(dylib => Path.GetFileName(dylib) == libName + ".dylib"); + + if (runtime == "NativeAOT") + { + // link NativeAOT framework libs without '-force_load' + toLink += $" {lib}{Environment.NewLine}"; + } + else if (forceAOT || !(preferDylibs && dylibExists)) + { + // these libraries are pinvoked + // -force_load will be removed once we enable direct-pinvokes for AOT + toLink += $" \"-force_load {lib}\"{Environment.NewLine}"; + } } } @@ -460,7 +475,14 @@ public string GenerateCMake( cmakeLists = cmakeLists.Replace("%EXTRA_LINKER_ARGS%", extraLinkerArgsConcat); cmakeLists = cmakeLists.Replace("%AotSources%", aotSources); cmakeLists = cmakeLists.Replace("%AotTargetsList%", aotList); - cmakeLists = cmakeLists.Replace("%AotModulesSource%", string.IsNullOrEmpty(aotSources) ? "" : "modules.m"); + if (runtime == "CoreCLR") + { + cmakeLists = cmakeLists.Replace("%AotModulesSource%", "coreclrhost.h"); + } + else + { + cmakeLists = cmakeLists.Replace("%AotModulesSource%", string.IsNullOrEmpty(aotSources) ? "" : "modules.m"); + } var defines = new StringBuilder(); if (forceInterpreter) @@ -493,10 +515,14 @@ public string GenerateCMake( defines.AppendLine($"\nadd_definitions(-DDIAGNOSTIC_PORTS=\"{diagnosticPorts}\")"); } - if (useNativeAOTRuntime) + if (runtime == "NativeAOT") { defines.AppendLine("add_definitions(-DUSE_NATIVE_AOT=1)"); } + else if (runtime == "CoreCLR") + { + defines.AppendLine("add_definitions(-DCORECLR=1)"); + } if (isLibraryMode) { @@ -531,30 +557,45 @@ public string GenerateCMake( File.WriteAllText(Path.Combine(binDir, "runtime-librarymode.h"), Utils.GetEmbeddedResource("runtime-librarymode.h")); File.WriteAllText(Path.Combine(binDir, "runtime-librarymode.m"), Utils.GetEmbeddedResource("runtime-librarymode.m")); } - else if (!useNativeAOTRuntime) + else if (runtime != "NativeAOT") { File.WriteAllText(Path.Combine(binDir, "runtime.h"), Utils.GetEmbeddedResource("runtime.h")); - // lookup statically linked libraries via dlsym(), see handle_pinvoke_override() in runtime.m - var pinvokeOverrides = new StringBuilder(); - foreach (string aFile in Directory.GetFiles(workspace, "*.a")) + if (runtime == "MonoVM") { - string aFileName = Path.GetFileNameWithoutExtension(aFile); - pinvokeOverrides.AppendLine($" \"{aFileName}\","); + // lookup statically linked libraries via dlsym(), see handle_pinvoke_override() in runtime.m + var pinvokeOverrides = new StringBuilder(); + foreach (string aFile in Directory.GetFiles(workspace, "*.a")) + { + string aFileName = Path.GetFileNameWithoutExtension(aFile); + pinvokeOverrides.AppendLine($" \"{aFileName}\","); - // also register with or without "lib" prefix - aFileName = aFileName.StartsWith("lib") ? aFileName.Remove(0, 3) : "lib" + aFileName; - pinvokeOverrides.AppendLine($" \"{aFileName}\","); - } + // also register with or without "lib" prefix + aFileName = aFileName.StartsWith("lib") ? aFileName.Remove(0, 3) : "lib" + aFileName; + pinvokeOverrides.AppendLine($" \"{aFileName}\","); + } - pinvokeOverrides.AppendLine($" \"System.Globalization.Native\","); + pinvokeOverrides.AppendLine($" \"System.Globalization.Native\","); - File.WriteAllText(Path.Combine(binDir, "runtime.m"), - Utils.GetEmbeddedResource("runtime.m") - .Replace("//%PInvokeOverrideLibraries%", pinvokeOverrides.ToString()) - .Replace("//%APPLE_RUNTIME_IDENTIFIER%", RuntimeIdentifier) - .Replace("%EntryPointLibName%", Path.GetFileName(entryPointLib))); + string runtimeTemplateName = (isLibraryMode) ? "runtime-librarymode.m" : "runtime.m"; + File.WriteAllText(Path.Combine(binDir, "runtime.m"), + Utils.GetEmbeddedResource(runtimeTemplateName) + .Replace("//%PInvokeOverrideLibraries%", pinvokeOverrides.ToString()) + .Replace("//%APPLE_RUNTIME_IDENTIFIER%", RuntimeIdentifier) + .Replace("%EntryPointLibName%", Path.GetFileName(entryPointLib))); + } + else + { + File.WriteAllText(Path.Combine(binDir, "coreclrhost.h"), + Utils.GetEmbeddedResource("coreclrhost.h")); + + // NOTE: Library mode is not supported yet + File.WriteAllText(Path.Combine(binDir, "runtime.m"), + Utils.GetEmbeddedResource("runtime-coreclr.m") + .Replace("//%APPLE_RUNTIME_IDENTIFIER%", RuntimeIdentifier) + .Replace("%EntryPointLibName%", Path.GetFileName(entryPointLib))); + } } File.WriteAllText(Path.Combine(binDir, "util.h"), Utils.GetEmbeddedResource("util.h")); diff --git a/src/tests/Directory.Build.props b/src/tests/Directory.Build.props index 8f85e3a0c293f2..942ae96af5c23a 100644 --- a/src/tests/Directory.Build.props +++ b/src/tests/Directory.Build.props @@ -214,7 +214,7 @@ <_targetOS>$(TargetOS) <_targetArchitecture>$(TargetArchitecture) - true + NativeAOT static true <_IsApplePlatform Condition="'$(TargetsAppleMobile)' == 'true'">true From 968031fb5038fef797538105cdc569f54b6b9b9e Mon Sep 17 00:00:00 2001 From: Filip Navara Date: Sat, 29 Jul 2023 15:50:28 +0200 Subject: [PATCH 06/24] Make the sample app working: dotnet publish src/mono/sample/iOS/Program.csproj -c Release /p:TargetOS=iossimulator /p:TargetArchitecture=arm64 /p:AppleAppBuilderRuntime=CoreCLR /p:DeployAndRun=true /p:UseMonoRuntime=false /p:RunAOTCompilation=false /p:MonoForceInterpreter=false --- src/coreclr/jit/CMakeLists.txt | 2 +- src/tasks/AppleAppBuilder/AppleAppBuilder.cs | 5 ++ .../Templates/CMakeLists.txt.template | 3 - .../Templates/runtime-coreclr.m | 69 ++++++++++++++++--- 4 files changed, 67 insertions(+), 12 deletions(-) diff --git a/src/coreclr/jit/CMakeLists.txt b/src/coreclr/jit/CMakeLists.txt index 766a0a0150e873..067039a1a67abc 100644 --- a/src/coreclr/jit/CMakeLists.txt +++ b/src/coreclr/jit/CMakeLists.txt @@ -626,7 +626,7 @@ set(JIT_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}) # Creates a static library "clrjit_static" to link into the VM. add_subdirectory(static) -if (CLR_CMAKE_TARGET_OSX) +if (CLR_CMAKE_TARGET_APPLE) set(TARGET_OS_NAME unix_osx) elseif (CLR_CMAKE_TARGET_UNIX) set(TARGET_OS_NAME unix) diff --git a/src/tasks/AppleAppBuilder/AppleAppBuilder.cs b/src/tasks/AppleAppBuilder/AppleAppBuilder.cs index 231e3a6653e3ca..6e6562e16f93bb 100644 --- a/src/tasks/AppleAppBuilder/AppleAppBuilder.cs +++ b/src/tasks/AppleAppBuilder/AppleAppBuilder.cs @@ -320,6 +320,11 @@ public override bool Execute() extraLinkerArgs.Add(item.ItemSpec); } + if (Runtime == "CoreCLR") + { + extraLinkerArgs.Add("-rpath @executable_path"); + } + var generator = new Xcode(Log, TargetOS, Arch); if (GenerateXcodeProject) diff --git a/src/tasks/AppleAppBuilder/Templates/CMakeLists.txt.template b/src/tasks/AppleAppBuilder/Templates/CMakeLists.txt.template index 9355e30fb39aea..893df033672183 100644 --- a/src/tasks/AppleAppBuilder/Templates/CMakeLists.txt.template +++ b/src/tasks/AppleAppBuilder/Templates/CMakeLists.txt.template @@ -3,9 +3,6 @@ cmake_minimum_required(VERSION 3.16) project(%ProjectName%) enable_language(OBJC ASM) -set(CMAKE_BUILD_WITH_INSTALL_RPATH ON) -set(CMAKE_INSTALL_RPATH "@executable_path") - set(APP_RESOURCES %AppResources% ) diff --git a/src/tasks/AppleAppBuilder/Templates/runtime-coreclr.m b/src/tasks/AppleAppBuilder/Templates/runtime-coreclr.m index 142b88c73ee50a..582f041a44d3ee 100644 --- a/src/tasks/AppleAppBuilder/Templates/runtime-coreclr.m +++ b/src/tasks/AppleAppBuilder/Templates/runtime-coreclr.m @@ -9,10 +9,11 @@ #include #include #include +#include #import "util.h" -#define APPLE_RUNTIME_IDENTIFIER "//%APPLE_RUNTIME_IDENTIFIER%" +#define APPLE_RUNTIME_IDENTIFIER "iossimulator-arm64" const char * get_bundle_path (void) @@ -32,6 +33,51 @@ return bundle_path; } +char * +compute_trusted_platform_assemblies () +{ + const char *bundle_path = get_bundle_path (); + + NSMutableArray *files = [NSMutableArray array]; + NSMutableArray *exes = [NSMutableArray array]; + + NSFileManager *manager = [NSFileManager defaultManager]; + NSString *dir = [NSString stringWithUTF8String: bundle_path]; + NSDirectoryEnumerator *enumerator = [manager enumeratorAtURL:[NSURL fileURLWithPath: dir] + includingPropertiesForKeys:@[NSURLNameKey, NSURLIsDirectoryKey] + options:NSDirectoryEnumerationSkipsSubdirectoryDescendants + errorHandler:nil]; + for (NSURL *file in enumerator) { + // skip subdirectories + NSNumber *isDirectory = nil; + if (![file getResourceValue:&isDirectory forKey:NSURLIsDirectoryKey error:nil] || [isDirectory boolValue]) + continue; + + NSString *name = nil; + if (![file getResourceValue:&name forKey:NSURLNameKey error:nil]) + continue; + if ([name length] < 4) + continue; + if ([name compare: @".dll" options: NSCaseInsensitiveSearch range: NSMakeRange ([name length] - 4, 4)] == NSOrderedSame) { + [files addObject: [dir stringByAppendingPathComponent: name]]; + } + } + + // Join them all together with a colon separating them + NSString *joined = [files componentsJoinedByString: @":"]; + return strdup([joined UTF8String]); +} + +void* +pinvoke_override (const char *libraryName, const char *entrypointName) +{ + if (!strcmp (libraryName, "__Internal")) + { + return dlsym (RTLD_DEFAULT, entrypointName); + } + return NULL; +} + void mono_ios_runtime_init (void) { @@ -43,7 +89,6 @@ setenv ("DOTNET_SYSTEM_GLOBALIZATION_HYBRID", "1", TRUE); #endif - // build using DiagnosticPorts property in AppleAppBuilder // or set DOTNET_DiagnosticPorts env via mlaunch, xharness when undefined. // NOTE, using DOTNET_DiagnosticPorts requires app build using AppleAppBuilder and RuntimeComponents=diagnostics_tracing @@ -67,11 +112,16 @@ res = snprintf (icu_dat_path, sizeof (icu_dat_path) - 1, "%s/%s", bundle, "icudt.dat"); #endif assert (res > 0); + + char pinvoke_override_addr [16]; + sprintf (pinvoke_override_addr, "%p", &pinvoke_override); // TODO: set TRUSTED_PLATFORM_ASSEMBLIES, APP_PATHS and NATIVE_DLL_SEARCH_DIRECTORIES const char *appctx_keys [] = { "RUNTIME_IDENTIFIER", "APP_CONTEXT_BASE_DIRECTORY", + "TRUSTED_PLATFORM_ASSEMBLIES", + "PINVOKE_OVERRIDE", #if !defined(INVARIANT_GLOBALIZATION) "ICU_DAT_FILE_PATH" #endif @@ -79,25 +129,28 @@ const char *appctx_values [] = { APPLE_RUNTIME_IDENTIFIER, bundle, + compute_trusted_platform_assemblies(), + pinvoke_override_addr, #if !defined(INVARIANT_GLOBALIZATION) icu_dat_path #endif }; - const char* executable = "%EntryPointLibName%"; + const char* executable = "Program.dll"; const char *executablePath = [[[[NSBundle mainBundle] executableURL] path] UTF8String]; unsigned int coreclr_domainId = 0; void *coreclr_handle = NULL; - coreclr_initialize ( - executablePath, executable, - sizeof (appctx_keys) / sizeof (appctx_keys [0]), appctx_keys, appctx_values, - &coreclr_handle, &coreclr_domainId); - char path [1024]; res = snprintf (path, sizeof (path) - 1, "%s/%s", bundle, executable); assert (res > 0); + res = coreclr_initialize ( + executablePath, executable, + sizeof (appctx_keys) / sizeof (appctx_keys [0]), appctx_keys, appctx_values, + &coreclr_handle, &coreclr_domainId); + assert (res == 0); + coreclr_execute_assembly (coreclr_handle, coreclr_domainId, argi, managed_argv, path, &res); // Print this so apps parsing logs can detect when we exited os_log_info (OS_LOG_DEFAULT, EXIT_CODE_TAG ": %d", res); From 1530eba818dd313988a886be6a4cb0580e4381af Mon Sep 17 00:00:00 2001 From: Filip Navara Date: Sat, 29 Jul 2023 15:59:55 +0200 Subject: [PATCH 07/24] Cleanup --- src/coreclr/tools/dotnet-pgo/dotnet-pgo.csproj | 2 +- src/tasks/AppleAppBuilder/Templates/CMakeLists.txt.template | 4 ++-- src/tasks/AppleAppBuilder/Xcode.cs | 6 +++--- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/coreclr/tools/dotnet-pgo/dotnet-pgo.csproj b/src/coreclr/tools/dotnet-pgo/dotnet-pgo.csproj index a3c667c998bff1..ac3720d4f7e059 100644 --- a/src/coreclr/tools/dotnet-pgo/dotnet-pgo.csproj +++ b/src/coreclr/tools/dotnet-pgo/dotnet-pgo.csproj @@ -13,7 +13,7 @@ true true dotnet-pgo - + win-x64;win-x86;osx-x64 $(OutputPath) false .NET Performance Guided Optimization Tool diff --git a/src/tasks/AppleAppBuilder/Templates/CMakeLists.txt.template b/src/tasks/AppleAppBuilder/Templates/CMakeLists.txt.template index 893df033672183..20633351254174 100644 --- a/src/tasks/AppleAppBuilder/Templates/CMakeLists.txt.template +++ b/src/tasks/AppleAppBuilder/Templates/CMakeLists.txt.template @@ -28,8 +28,8 @@ endif() %Defines% -if(%UseMonoRuntime%) - include_directories("%RuntimeInclude%") +if(NOT "%MonoInclude%" STREQUAL "") + include_directories("%MonoInclude%") endif() set_target_properties(%ProjectName% %AotTargetsList% PROPERTIES diff --git a/src/tasks/AppleAppBuilder/Xcode.cs b/src/tasks/AppleAppBuilder/Xcode.cs index f2bb963b71a723..0da2f5527d0431 100644 --- a/src/tasks/AppleAppBuilder/Xcode.cs +++ b/src/tasks/AppleAppBuilder/Xcode.cs @@ -177,7 +177,7 @@ public string GenerateXCode( IEnumerable excludes, string workspace, string binDir, - string runtimeInclude, + string monoInclude, bool preferDylibs, bool useConsoleUiTemplate, bool forceAOT, @@ -250,7 +250,7 @@ public string GenerateCMake( IEnumerable excludes, string workspace, string binDir, - string runtimeInclude, + string monoInclude, bool preferDylibs, bool useConsoleUiTemplate, bool forceAOT, @@ -347,7 +347,7 @@ public string GenerateCMake( .Replace("%ProjectName%", projectName) .Replace("%AppResources%", appResources) .Replace("%MainSource%", nativeMainSource) - .Replace("%RuntimeInclude%", runtimeInclude) + .Replace("%MonoInclude%", monoInclude) .Replace("%HardenedRuntime%", hardenedRuntime ? "TRUE" : "FALSE"); string toLink = ""; From f9b31b2c7a7a0b1992a53b441db6845b64c5777c Mon Sep 17 00:00:00 2001 From: Filip Navara Date: Sat, 29 Jul 2023 21:17:30 +0200 Subject: [PATCH 08/24] Update src/tasks/AppleAppBuilder/AppleAppBuilder.csproj Co-authored-by: Adeel Mujahid <3840695+am11@users.noreply.github.com> --- src/tasks/AppleAppBuilder/AppleAppBuilder.csproj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/tasks/AppleAppBuilder/AppleAppBuilder.csproj b/src/tasks/AppleAppBuilder/AppleAppBuilder.csproj index f02dd024f14a1f..77d9c80f3a4e57 100644 --- a/src/tasks/AppleAppBuilder/AppleAppBuilder.csproj +++ b/src/tasks/AppleAppBuilder/AppleAppBuilder.csproj @@ -9,7 +9,7 @@ - + From eaed19b544f3969dee2d60ccb00c95bb1fdef68f Mon Sep 17 00:00:00 2001 From: Filip Navara Date: Mon, 31 Jul 2023 10:19:09 +0200 Subject: [PATCH 09/24] Use sys_icache_invalidate for all iOS-like platforms --- src/coreclr/pal/src/thread/context.cpp | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/coreclr/pal/src/thread/context.cpp b/src/coreclr/pal/src/thread/context.cpp index c8695edb2727d6..108b90a9a2bcf3 100644 --- a/src/coreclr/pal/src/thread/context.cpp +++ b/src/coreclr/pal/src/thread/context.cpp @@ -29,6 +29,9 @@ SET_DEFAULT_DEBUG_CHANNEL(THREAD); // some headers have code with asserts, so do #endif #include #include +#if defined(HOST_APPLE) && !defined(HOST_OSX) +#include +#endif extern PGET_GCMARKER_EXCEPTION_CODE g_getGcMarkerExceptionCode; @@ -1912,6 +1915,8 @@ DBG_FlushInstructionCache( #endif syscall(__NR_riscv_flush_icache, (char *)lpBaseAddress, (char *)((INT_PTR)lpBaseAddress + dwSize), 0 /* all harts */); +#elif defined(HOST_APPLE) && !defined(HOST_OSX) + sys_icache_invalidate((void *)lpBaseAddress, dwSize); #else __builtin___clear_cache((char *)lpBaseAddress, (char *)((INT_PTR)lpBaseAddress + dwSize)); #endif From 970f34cce511f08656a57950dd7cbcbdcb213968 Mon Sep 17 00:00:00 2001 From: Filip Navara Date: Mon, 31 Jul 2023 10:22:28 +0200 Subject: [PATCH 10/24] Consolidate macOS-only entrypoints in a single #if block --- .../entrypoints.c | 58 +++++++------------ 1 file changed, 22 insertions(+), 36 deletions(-) diff --git a/src/native/libs/System.Security.Cryptography.Native.Apple/entrypoints.c b/src/native/libs/System.Security.Cryptography.Native.Apple/entrypoints.c index d7ce3025fe1cf0..d98498586e3fff 100644 --- a/src/native/libs/System.Security.Cryptography.Native.Apple/entrypoints.c +++ b/src/native/libs/System.Security.Cryptography.Native.Apple/entrypoints.c @@ -31,12 +31,6 @@ static const Entry s_cryptoAppleNative[] = { -#ifdef TARGET_OSX - DllImportEntry(AppleCryptoNative_AesGcmEncrypt) - DllImportEntry(AppleCryptoNative_AesGcmDecrypt) - DllImportEntry(AppleCryptoNative_ChaCha20Poly1305Encrypt) - DllImportEntry(AppleCryptoNative_ChaCha20Poly1305Decrypt) -#endif DllImportEntry(AppleCryptoNative_DigestFree) DllImportEntry(AppleCryptoNative_DigestCreate) DllImportEntry(AppleCryptoNative_DigestUpdate) @@ -55,21 +49,8 @@ static const Entry s_cryptoAppleNative[] = DllImportEntry(AppleCryptoNative_HmacFinal) DllImportEntry(AppleCryptoNative_HmacCurrent) DllImportEntry(AppleCryptoNative_HmacOneShot) -#ifdef TARGET_OSX - DllImportEntry(AppleCryptoNative_SecKeychainItemCopyKeychain) - DllImportEntry(AppleCryptoNative_SecKeychainCopyDefault) - DllImportEntry(AppleCryptoNative_SecKeychainCreate) - DllImportEntry(AppleCryptoNative_SecKeychainDelete) -#endif DllImportEntry(AppleCryptoNative_SecKeychainEnumerateCerts) -#ifdef TARGET_OSX - DllImportEntry(AppleCryptoNative_SecKeychainOpen) - DllImportEntry(AppleCryptoNative_SecKeychainUnlock) -#endif DllImportEntry(AppleCryptoNative_SecKeychainEnumerateIdentities) -#ifdef TARGET_OSX - DllImportEntry(AppleCryptoNative_SetKeychainNeverLock) -#endif DllImportEntry(AppleCryptoNative_SslCopyCADistinguishedNames) DllImportEntry(AppleCryptoNative_SslCopyCertChain) DllImportEntry(AppleCryptoNative_SslIsHostnameMatch) @@ -90,10 +71,6 @@ static const Entry s_cryptoAppleNative[] = DllImportEntry(AppleCryptoNative_RsaEncryptionPrimitive) DllImportEntry(AppleCryptoNative_RsaVerificationPrimitive) DllImportEntry(AppleCryptoNative_SecCopyErrorMessageString) -#ifdef TARGET_OSX - DllImportEntry(AppleCryptoNative_SecKeyExport) - DllImportEntry(AppleCryptoNative_SecKeyImportEphemeral) -#endif DllImportEntry(AppleCryptoNative_SecKeyGetSimpleKeySizeInBytes) DllImportEntry(AppleCryptoNative_SecKeyCreateSignature) DllImportEntry(AppleCryptoNative_SecKeyVerifySignature) @@ -120,12 +97,6 @@ static const Entry s_cryptoAppleNative[] = DllImportEntry(AppleCryptoNative_CryptorCreate) DllImportEntry(AppleCryptoNative_CryptorUpdate) DllImportEntry(AppleCryptoNative_CryptorReset) -#ifdef TARGET_OSX - DllImportEntry(AppleCryptoNative_StoreEnumerateUserRoot) - DllImportEntry(AppleCryptoNative_StoreEnumerateMachineRoot) - DllImportEntry(AppleCryptoNative_StoreEnumerateUserDisallowed) - DllImportEntry(AppleCryptoNative_StoreEnumerateMachineDisallowed) -#endif DllImportEntry(AppleCryptoNative_X509ChainCreate) DllImportEntry(AppleCryptoNative_X509DemuxAndRetainHandle) DllImportEntry(AppleCryptoNative_X509GetContentType) @@ -134,14 +105,7 @@ static const Entry s_cryptoAppleNative[] = DllImportEntry(AppleCryptoNative_X509CopyPrivateKeyFromIdentity) DllImportEntry(AppleCryptoNative_X509ImportCollection) DllImportEntry(AppleCryptoNative_X509ImportCertificate) -#ifdef TARGET_OSX - DllImportEntry(AppleCryptoNative_X509ExportData) -#endif DllImportEntry(AppleCryptoNative_X509GetRawData) -#ifdef TARGET_OSX - DllImportEntry(AppleCryptoNative_X509CopyWithPrivateKey) - DllImportEntry(AppleCryptoNative_X509MoveToKeychain) -#endif DllImportEntry(AppleCryptoNative_X509ChainCreateDefaultPolicy) DllImportEntry(AppleCryptoNative_X509ChainCreateRevocationPolicy) DllImportEntry(AppleCryptoNative_X509ChainEvaluate) @@ -155,6 +119,28 @@ static const Entry s_cryptoAppleNative[] = DllImportEntry(AppleCryptoNative_X509StoreRemoveCertificate) DllImportEntry(AppleCryptoNative_Pbkdf2) DllImportEntry(AppleCryptoNative_X509GetSubjectSummary) +#ifdef TARGET_OSX + DllImportEntry(AppleCryptoNative_AesGcmEncrypt) + DllImportEntry(AppleCryptoNative_AesGcmDecrypt) + DllImportEntry(AppleCryptoNative_ChaCha20Poly1305Encrypt) + DllImportEntry(AppleCryptoNative_ChaCha20Poly1305Decrypt) + DllImportEntry(AppleCryptoNative_SecKeychainItemCopyKeychain) + DllImportEntry(AppleCryptoNative_SecKeychainCopyDefault) + DllImportEntry(AppleCryptoNative_SecKeychainCreate) + DllImportEntry(AppleCryptoNative_SecKeychainDelete) + DllImportEntry(AppleCryptoNative_SecKeychainOpen) + DllImportEntry(AppleCryptoNative_SecKeychainUnlock) + DllImportEntry(AppleCryptoNative_SetKeychainNeverLock) + DllImportEntry(AppleCryptoNative_SecKeyExport) + DllImportEntry(AppleCryptoNative_SecKeyImportEphemeral) + DllImportEntry(AppleCryptoNative_StoreEnumerateUserRoot) + DllImportEntry(AppleCryptoNative_StoreEnumerateMachineRoot) + DllImportEntry(AppleCryptoNative_StoreEnumerateUserDisallowed) + DllImportEntry(AppleCryptoNative_StoreEnumerateMachineDisallowed) + DllImportEntry(AppleCryptoNative_X509ExportData) + DllImportEntry(AppleCryptoNative_X509CopyWithPrivateKey) + DllImportEntry(AppleCryptoNative_X509MoveToKeychain) +#endif }; EXTERN_C const void* CryptoAppleResolveDllImport(const char* name); From cf686b6f0972e1154c00b2999a536ec03171cebe Mon Sep 17 00:00:00 2001 From: Filip Navara Date: Mon, 31 Jul 2023 10:25:15 +0200 Subject: [PATCH 11/24] Apply feedback --- src/tasks/AppleAppBuilder/AppleAppBuilder.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/tasks/AppleAppBuilder/AppleAppBuilder.cs b/src/tasks/AppleAppBuilder/AppleAppBuilder.cs index 6e6562e16f93bb..3b1bda2d97e36d 100644 --- a/src/tasks/AppleAppBuilder/AppleAppBuilder.cs +++ b/src/tasks/AppleAppBuilder/AppleAppBuilder.cs @@ -216,7 +216,7 @@ public void ValidateRuntimeSelection() if (EnableRuntimeLogging) throw new ArgumentException($"Property \"{nameof(EnableRuntimeLogging)}\" is not supported with {Runtime} runtime and will be ignored."); } - else if (Runtime == "MonoVM" || Runtime == "") + else if (Runtime == "MonoVM" || string.IsNullOrEmpty(Runtime)) { if (string.IsNullOrEmpty(MonoRuntimeHeaders)) throw new ArgumentException($"The \"{nameof(AppleAppBuilderTask)}\" task was not given a value for the required parameter \"{nameof(MonoRuntimeHeaders)}\" when using Mono runtime."); From bd7eaacf21e3832ed319de06c2808e1d92079aa4 Mon Sep 17 00:00:00 2001 From: Filip Navara Date: Mon, 31 Jul 2023 11:07:59 +0200 Subject: [PATCH 12/24] Fix jitinterface cross-build --- src/coreclr/tools/aot/jitinterface/CMakeLists.txt | 8 ++++++-- src/coreclr/tools/aot/jitinterface/jitwrapper.cpp | 4 ++++ 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/src/coreclr/tools/aot/jitinterface/CMakeLists.txt b/src/coreclr/tools/aot/jitinterface/CMakeLists.txt index 6a612e14869852..7dc878f56c8515 100644 --- a/src/coreclr/tools/aot/jitinterface/CMakeLists.txt +++ b/src/coreclr/tools/aot/jitinterface/CMakeLists.txt @@ -5,10 +5,14 @@ set(NATIVE_SOURCES jitinterface.cpp jitwrapper.cpp corinfoexception.cpp - - ${CLR_SRC_NATIVE_DIR}/minipal/cpufeatures.c ) +if(NOT CLR_CROSS_COMPONENTS_BUILD) + list(APPEND NATIVE_SOURCES + ${CLR_SRC_NATIVE_DIR}/minipal/cpufeatures.c + ) +endif() + if(CLR_CMAKE_TARGET_WIN32) set(JITINTERFACE_RESOURCES Native.rc) else() diff --git a/src/coreclr/tools/aot/jitinterface/jitwrapper.cpp b/src/coreclr/tools/aot/jitinterface/jitwrapper.cpp index 8fd38d192f84e9..48cf7ece4a12b9 100644 --- a/src/coreclr/tools/aot/jitinterface/jitwrapper.cpp +++ b/src/coreclr/tools/aot/jitinterface/jitwrapper.cpp @@ -55,5 +55,9 @@ DLL_EXPORT void JitProcessShutdownWork(ICorJitCompiler * pJit) DLL_EXPORT int JitGetProcessorFeatures() { +#ifndef CROSS_COMPILE return minipal_getcpufeatures(); +#else + return 0; +#endif } From 1a34be8d6baa1909ad36acf4cf9a48b1a31c44b8 Mon Sep 17 00:00:00 2001 From: Filip Navara Date: Mon, 31 Jul 2023 11:32:49 +0200 Subject: [PATCH 13/24] Infer AppleAppBuilderRuntime from UseMonoRuntime and UseNativeAOTRuntime --- src/mono/msbuild/apple/build/AppleBuild.InTree.targets | 2 +- src/mono/msbuild/apple/build/AppleBuild.targets | 8 +++++++- src/tests/Directory.Build.props | 3 ++- 3 files changed, 10 insertions(+), 3 deletions(-) diff --git a/src/mono/msbuild/apple/build/AppleBuild.InTree.targets b/src/mono/msbuild/apple/build/AppleBuild.InTree.targets index 1b30b9510572fe..2bf671ed1e5ce9 100644 --- a/src/mono/msbuild/apple/build/AppleBuild.InTree.targets +++ b/src/mono/msbuild/apple/build/AppleBuild.InTree.targets @@ -16,7 +16,7 @@ + Condition="'$(UseMonoRuntime)' == 'false'" /> diff --git a/src/mono/msbuild/apple/build/AppleBuild.targets b/src/mono/msbuild/apple/build/AppleBuild.targets index 7b8f91d79536ad..fda81eefb40658 100644 --- a/src/mono/msbuild/apple/build/AppleBuild.targets +++ b/src/mono/msbuild/apple/build/AppleBuild.targets @@ -53,7 +53,7 @@ $([MSBuild]::NormalizeDirectory('$(OutDir)', 'Bundle')) $(AppleBundleDir) - <_MonoHeaderPath Condition="'$(AppleAppBuilderRuntime)' == 'MonoVM'">$([MSBuild]::NormalizeDirectory($(MicrosoftNetCoreAppRuntimePackRidNativeDir), 'include', 'mono-2.0')) + <_MonoHeaderPath Condition="'$(UseMonoRuntime)' != 'false' and '$(UseNativeAOTRuntime)' != 'true'">$([MSBuild]::NormalizeDirectory($(MicrosoftNetCoreAppRuntimePackRidNativeDir), 'include', 'mono-2.0')) <_AotModuleTablePath>$(AppleBundleDir)\modules.m $(AssemblyName) @@ -292,6 +292,12 @@ + + + NativeAOT + CoreCLR + MonoVM + <_targetOS>$(TargetOS) <_targetArchitecture>$(TargetArchitecture) - NativeAOT + NativeAOT + CoreCLR static true <_IsApplePlatform Condition="'$(TargetsAppleMobile)' == 'true'">true From 209a7624905f65a51c6f8c88dc3b0c43a7f33e4f Mon Sep 17 00:00:00 2001 From: Filip Navara Date: Mon, 31 Jul 2023 12:41:52 +0200 Subject: [PATCH 14/24] Minor changes to test infrastructure --- src/mono/msbuild/apple/build/AppleBuild.props | 2 +- src/tests/build.proj | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/src/mono/msbuild/apple/build/AppleBuild.props b/src/mono/msbuild/apple/build/AppleBuild.props index 22984e921c9681..80446718bc325b 100644 --- a/src/mono/msbuild/apple/build/AppleBuild.props +++ b/src/mono/msbuild/apple/build/AppleBuild.props @@ -1,7 +1,7 @@ - true + true diff --git a/src/tests/build.proj b/src/tests/build.proj index 0222d6b95a6bc0..b349d99b8363cf 100644 --- a/src/tests/build.proj +++ b/src/tests/build.proj @@ -613,6 +613,7 @@ $(GroupBuildCmd) "/p:IlcMultiModule=true" $(GroupBuildCmd) "/p:IlcUseServerGc=false" $(GroupBuildCmd) "/p:BuildNativeAotFrameworkObjects=true" + $(GroupBuildCmd) "/p:UseMonoRuntime=$(UseMonoRuntime)" From a9778ce8dab3725c8f3006ffa18ed9994317b0d3 Mon Sep 17 00:00:00 2001 From: Filip Navara Date: Mon, 31 Jul 2023 12:42:15 +0200 Subject: [PATCH 15/24] Add basic documentation --- docs/workflow/building/coreclr/ios.md | 33 +++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 docs/workflow/building/coreclr/ios.md diff --git a/docs/workflow/building/coreclr/ios.md b/docs/workflow/building/coreclr/ios.md new file mode 100644 index 00000000000000..b56816a8e9d22b --- /dev/null +++ b/docs/workflow/building/coreclr/ios.md @@ -0,0 +1,33 @@ +# Cross Compilation for iOS/tvOS Simulator on macOS + +## Requirements + +Build requirements are the same as for building native CoreCLR on macOS. iPhone SDK has to be enabled in Xcode installation. + +## Cross compiling CoreCLR + +Build the runtime pack and tools with + +``` +./build.sh clr+clr.nativeaotlibs+libs+packs -os [iossimulator/tvossimulator/maccatalyst] -arch [x64/arm64] -cross +``` + +## Running the sample iOS app + +Build and run the sample app with + +``` +./dotnet.sh publish src/mono/sample/iOS/Program.csproj -c Release /p:TargetOS=iossimulator /p:TargetArchitecture=arm64 /p:DeployAndRun=true /p:UseMonoRuntime=false /p:RunAOTCompilation=false /p:MonoForceInterpreter=false +``` + +The command also produces an XCode project that can be opened with `open ./src/mono/sample/iOS/bin/iossimulator-arm64/Bundle/HelloiOS/HelloiOS.xcodeproj` and debugged in Xcode. + +## Running the runtime tests + +Build the runtime tests with + +``` +./src/tests/build.sh -os iossimulator arm64 Release -p:UseMonoRuntime=false +``` + +Running the tests is not implemented yet. It will likely need similar app bundle infrastructure as NativeAOT/iOS uses. \ No newline at end of file From f58daa152c4c91010f6df020cc74407a580485cf Mon Sep 17 00:00:00 2001 From: Filip Navara Date: Mon, 31 Jul 2023 14:10:14 +0200 Subject: [PATCH 16/24] Add nearly complete support for building runtime tests --- src/mono/msbuild/apple/build/AppleBuild.props | 2 +- src/tests/build.proj | 22 ++++++++++++++----- 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/src/mono/msbuild/apple/build/AppleBuild.props b/src/mono/msbuild/apple/build/AppleBuild.props index 80446718bc325b..51ad3cacee67de 100644 --- a/src/mono/msbuild/apple/build/AppleBuild.props +++ b/src/mono/msbuild/apple/build/AppleBuild.props @@ -12,7 +12,7 @@ true $(TargetOS)-$(TargetArchitecture.ToLowerInvariant()) - true + true true false diff --git a/src/tests/build.proj b/src/tests/build.proj index b349d99b8363cf..b8d8b0b61fb1fd 100644 --- a/src/tests/build.proj +++ b/src/tests/build.proj @@ -418,8 +418,7 @@ - - + $([System.IO.Path]::GetDirectoryName($([System.IO.Path]::GetDirectoryName($(_CMDDIR))))) $([System.IO.Path]::GetFileName($(CategoryPath))) @@ -438,7 +437,10 @@ false false + + + @@ -449,6 +451,10 @@ + + $(IntermediateOutputPath)\..\$(TestRelativePath)\$(TestName)\native\$(TestName).o + + @@ -482,14 +488,21 @@ Projects="@(RunProj)" Targets="BuildMonoiOSApp" BuildInParallel="true" - Condition="'@(TestDirectories)' != '' and '$(TestBuildMode)' != 'nativeaot'" + Condition="'@(TestDirectories)' != '' and '$(RuntimeFlavor)' != 'coreclr'" + /> + + @@ -613,7 +626,6 @@ $(GroupBuildCmd) "/p:IlcMultiModule=true" $(GroupBuildCmd) "/p:IlcUseServerGc=false" $(GroupBuildCmd) "/p:BuildNativeAotFrameworkObjects=true" - $(GroupBuildCmd) "/p:UseMonoRuntime=$(UseMonoRuntime)" From 91c2b2ee0eb13b2c25885727b333c0b4e86a1a24 Mon Sep 17 00:00:00 2001 From: Filip Navara Date: Mon, 31 Jul 2023 16:23:37 +0200 Subject: [PATCH 17/24] Fix building seh-unwind with older iOS SDK --- src/coreclr/pal/src/exception/seh-unwind.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/coreclr/pal/src/exception/seh-unwind.cpp b/src/coreclr/pal/src/exception/seh-unwind.cpp index 66b304cca370f4..f5e56409b1462b 100644 --- a/src/coreclr/pal/src/exception/seh-unwind.cpp +++ b/src/coreclr/pal/src/exception/seh-unwind.cpp @@ -54,7 +54,7 @@ Module Name: #endif // HOST_UNIX -#if defined(TARGET_OSX) && defined(HOST_ARM64) && !defined(HAVE_UNW_AARCH64_X19) +#if defined(TARGET_APPLE) && defined(HOST_ARM64) && !defined(HAVE_UNW_AARCH64_X19) // MacOS uses ARM64 instead of AARCH64 to describe these registers // Create aliases to reuse more code enum From 1ef800582c5c46355be1739ff0f0df7918c9fc11 Mon Sep 17 00:00:00 2001 From: Filip Navara Date: Tue, 1 Aug 2023 10:39:41 +0200 Subject: [PATCH 18/24] Use enum for TargetRuntime in AppleAppBuilder --- src/tasks/AppleAppBuilder/AppleAppBuilder.cs | 26 ++++++++++--------- .../AppleAppBuilder/AppleAppBuilder.csproj | 1 + src/tasks/AppleAppBuilder/TargetRuntime.cs | 9 +++++++ src/tasks/AppleAppBuilder/Xcode.cs | 26 +++++++++---------- 4 files changed, 37 insertions(+), 25 deletions(-) create mode 100644 src/tasks/AppleAppBuilder/TargetRuntime.cs diff --git a/src/tasks/AppleAppBuilder/AppleAppBuilder.cs b/src/tasks/AppleAppBuilder/AppleAppBuilder.cs index 3b1bda2d97e36d..3fcf32778f2cf9 100644 --- a/src/tasks/AppleAppBuilder/AppleAppBuilder.cs +++ b/src/tasks/AppleAppBuilder/AppleAppBuilder.cs @@ -13,6 +13,7 @@ public class AppleAppBuilderTask : Task { private string targetOS = TargetNames.iOS; + private TargetRuntime targetRuntime; /// /// The Apple OS we are targeting (ios, tvos, iossimulator, tvossimulator) @@ -193,7 +194,12 @@ public string TargetOS public void ValidateRuntimeSelection() { - if (Runtime == "NativeAOT" || Runtime == "CoreCLR") + if (!Enum.TryParse(Runtime, out targetRuntime)) + { + throw new ArgumentException($"The \"{nameof(AppleAppBuilderTask)}\" task was not given an invalid value for parameter \"{nameof(Runtime)}\"."); + } + + if (targetRuntime == TargetRuntime.NativeAOT || targetRuntime == TargetRuntime.CoreCLR) { if (!string.IsNullOrEmpty(MonoRuntimeHeaders)) throw new ArgumentException($"Property \"{nameof(MonoRuntimeHeaders)}\" is not supported with {Runtime} runtime and will be ignored."); @@ -216,21 +222,16 @@ public void ValidateRuntimeSelection() if (EnableRuntimeLogging) throw new ArgumentException($"Property \"{nameof(EnableRuntimeLogging)}\" is not supported with {Runtime} runtime and will be ignored."); } - else if (Runtime == "MonoVM" || string.IsNullOrEmpty(Runtime)) + else { if (string.IsNullOrEmpty(MonoRuntimeHeaders)) throw new ArgumentException($"The \"{nameof(AppleAppBuilderTask)}\" task was not given a value for the required parameter \"{nameof(MonoRuntimeHeaders)}\" when using Mono runtime."); - Runtime = "MonoVM"; - } - else - { - throw new ArgumentException($"The \"{nameof(AppleAppBuilderTask)}\" task was not given an invalid value for parameter \"{nameof(Runtime)}\"."); } } public override bool Execute() { - bool shouldStaticLink = Runtime != "CoreCLR" && !EnableAppSandbox; + bool shouldStaticLink = !EnableAppSandbox; bool isDevice = (TargetOS == TargetNames.iOS || TargetOS == TargetNames.tvOS); ValidateRuntimeSelection(); @@ -293,7 +294,7 @@ public override bool Execute() } } - if (!ForceInterpreter && (shouldStaticLink || ForceAOT) && (assemblerFiles.Count == 0 && Runtime == "MonoVM")) + if (!ForceInterpreter && (shouldStaticLink || ForceAOT) && (assemblerFiles.Count == 0 && targetRuntime == TargetRuntime.MonoVM)) { throw new InvalidOperationException("Need list of AOT files for static linked builds."); } @@ -320,9 +321,10 @@ public override bool Execute() extraLinkerArgs.Add(item.ItemSpec); } - if (Runtime == "CoreCLR") + if (targetRuntime == TargetRuntime.CoreCLR) { extraLinkerArgs.Add("-rpath @executable_path"); + shouldStaticLink = false; } var generator = new Xcode(Log, TargetOS, Arch); @@ -330,7 +332,7 @@ public override bool Execute() if (GenerateXcodeProject) { XcodeProjectPath = generator.GenerateXCode(ProjectName, MainLibraryFileName, assemblerFiles, assemblerDataFiles, assemblerFilesToLink, extraLinkerArgs, excludes, - AppDir, binDir, MonoRuntimeHeaders, !shouldStaticLink, UseConsoleUITemplate, ForceAOT, ForceInterpreter, InvariantGlobalization, HybridGlobalization, Optimized, EnableRuntimeLogging, EnableAppSandbox, DiagnosticPorts, RuntimeComponents, NativeMainSource, Runtime, IsLibraryMode); + AppDir, binDir, MonoRuntimeHeaders, !shouldStaticLink, UseConsoleUITemplate, ForceAOT, ForceInterpreter, InvariantGlobalization, HybridGlobalization, Optimized, EnableRuntimeLogging, EnableAppSandbox, DiagnosticPorts, RuntimeComponents, NativeMainSource, targetRuntime, IsLibraryMode); if (BuildAppBundle) { @@ -356,7 +358,7 @@ public override bool Execute() else if (GenerateCMakeProject) { generator.GenerateCMake(ProjectName, MainLibraryFileName, assemblerFiles, assemblerDataFiles, assemblerFilesToLink, extraLinkerArgs, excludes, - AppDir, binDir, MonoRuntimeHeaders, !shouldStaticLink, UseConsoleUITemplate, ForceAOT, ForceInterpreter, InvariantGlobalization, HybridGlobalization, Optimized, EnableRuntimeLogging, EnableAppSandbox, DiagnosticPorts, RuntimeComponents, NativeMainSource, Runtime, IsLibraryMode); + AppDir, binDir, MonoRuntimeHeaders, !shouldStaticLink, UseConsoleUITemplate, ForceAOT, ForceInterpreter, InvariantGlobalization, HybridGlobalization, Optimized, EnableRuntimeLogging, EnableAppSandbox, DiagnosticPorts, RuntimeComponents, NativeMainSource, targetRuntime, IsLibraryMode); } return true; diff --git a/src/tasks/AppleAppBuilder/AppleAppBuilder.csproj b/src/tasks/AppleAppBuilder/AppleAppBuilder.csproj index 77d9c80f3a4e57..721e2c67738236 100644 --- a/src/tasks/AppleAppBuilder/AppleAppBuilder.csproj +++ b/src/tasks/AppleAppBuilder/AppleAppBuilder.csproj @@ -18,6 +18,7 @@ + diff --git a/src/tasks/AppleAppBuilder/TargetRuntime.cs b/src/tasks/AppleAppBuilder/TargetRuntime.cs new file mode 100644 index 00000000000000..81fb9b1c956d18 --- /dev/null +++ b/src/tasks/AppleAppBuilder/TargetRuntime.cs @@ -0,0 +1,9 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +internal enum TargetRuntime +{ + MonoVM, + CoreCLR, + NativeAOT, +} diff --git a/src/tasks/AppleAppBuilder/Xcode.cs b/src/tasks/AppleAppBuilder/Xcode.cs index 0da2f5527d0431..4552d08223ee99 100644 --- a/src/tasks/AppleAppBuilder/Xcode.cs +++ b/src/tasks/AppleAppBuilder/Xcode.cs @@ -190,10 +190,10 @@ public string GenerateXCode( string? diagnosticPorts, IEnumerable runtimeComponents, string? nativeMainSource = null, - string? runtime = "MonoVM", + TargetRuntime targetRuntime = TargetRuntime.MonoVM, bool isLibraryMode = false) { - var cmakeDirectoryPath = GenerateCMake(projectName, entryPointLib, asmFiles, asmDataFiles, asmLinkFiles, extraLinkerArgs, excludes, workspace, binDir, monoInclude, preferDylibs, useConsoleUiTemplate, forceAOT, forceInterpreter, invariantGlobalization, hybridGlobalization, optimized, enableRuntimeLogging, enableAppSandbox, diagnosticPorts, runtimeComponents, nativeMainSource, runtime, isLibraryMode); + var cmakeDirectoryPath = GenerateCMake(projectName, entryPointLib, asmFiles, asmDataFiles, asmLinkFiles, extraLinkerArgs, excludes, workspace, binDir, monoInclude, preferDylibs, useConsoleUiTemplate, forceAOT, forceInterpreter, invariantGlobalization, hybridGlobalization, optimized, enableRuntimeLogging, enableAppSandbox, diagnosticPorts, runtimeComponents, nativeMainSource, targetRuntime, isLibraryMode); CreateXcodeProject(projectName, cmakeDirectoryPath); return Path.Combine(binDir, projectName, projectName + ".xcodeproj"); } @@ -263,7 +263,7 @@ public string GenerateCMake( string? diagnosticPorts, IEnumerable runtimeComponents, string? nativeMainSource = null, - string? runtime = "MonoVM", + TargetRuntime targetRuntime = TargetRuntime.MonoVM, bool isLibraryMode = false) { // bundle everything as resources excluding native files @@ -289,7 +289,7 @@ public string GenerateCMake( { predefinedExcludes.Add(".pdb"); } - if (runtime != "CoreCLR") + if (targetRuntime != TargetRuntime.CoreCLR) { predefinedExcludes.Add("libcoreclr.dylib"); } @@ -342,8 +342,8 @@ public string GenerateCMake( string cmakeTemplateName = (isLibraryMode) ? "CMakeLists-librarymode.txt.template" : "CMakeLists.txt.template"; string cmakeLists = Utils.GetEmbeddedResource(cmakeTemplateName) - .Replace("%UseMonoRuntime%", runtime == "MonoVM" ? "TRUE" : "FALSE") - .Replace("%UseNativeAOTRuntime%", runtime == "NativeAOT" ? "TRUE" : "FALSE") + .Replace("%UseMonoRuntime%", targetRuntime == TargetRuntime.MonoVM ? "TRUE" : "FALSE") + .Replace("%UseNativeAOTRuntime%", targetRuntime == TargetRuntime.NativeAOT ? "TRUE" : "FALSE") .Replace("%ProjectName%", projectName) .Replace("%AppResources%", appResources) .Replace("%MainSource%", nativeMainSource) @@ -412,7 +412,7 @@ public string GenerateCMake( } string[] dylibs = Directory.GetFiles(workspace, "*.dylib"); - if (runtime == "CoreCLR") + if (targetRuntime == TargetRuntime.CoreCLR) { foreach (string lib in dylibs) { @@ -431,7 +431,7 @@ public string GenerateCMake( // libmono must always be statically linked, for other librarires we can use dylibs bool dylibExists = libName != "libmonosgen-2.0" && dylibs.Any(dylib => Path.GetFileName(dylib) == libName + ".dylib"); - if (runtime == "NativeAOT") + if (targetRuntime == TargetRuntime.NativeAOT) { // link NativeAOT framework libs without '-force_load' toLink += $" {lib}{Environment.NewLine}"; @@ -475,7 +475,7 @@ public string GenerateCMake( cmakeLists = cmakeLists.Replace("%EXTRA_LINKER_ARGS%", extraLinkerArgsConcat); cmakeLists = cmakeLists.Replace("%AotSources%", aotSources); cmakeLists = cmakeLists.Replace("%AotTargetsList%", aotList); - if (runtime == "CoreCLR") + if (targetRuntime == TargetRuntime.CoreCLR) { cmakeLists = cmakeLists.Replace("%AotModulesSource%", "coreclrhost.h"); } @@ -515,11 +515,11 @@ public string GenerateCMake( defines.AppendLine($"\nadd_definitions(-DDIAGNOSTIC_PORTS=\"{diagnosticPorts}\")"); } - if (runtime == "NativeAOT") + if (targetRuntime == TargetRuntime.NativeAOT) { defines.AppendLine("add_definitions(-DUSE_NATIVE_AOT=1)"); } - else if (runtime == "CoreCLR") + else if (targetRuntime == TargetRuntime.CoreCLR) { defines.AppendLine("add_definitions(-DCORECLR=1)"); } @@ -557,12 +557,12 @@ public string GenerateCMake( File.WriteAllText(Path.Combine(binDir, "runtime-librarymode.h"), Utils.GetEmbeddedResource("runtime-librarymode.h")); File.WriteAllText(Path.Combine(binDir, "runtime-librarymode.m"), Utils.GetEmbeddedResource("runtime-librarymode.m")); } - else if (runtime != "NativeAOT") + else if (targetRuntime != TargetRuntime.NativeAOT) { File.WriteAllText(Path.Combine(binDir, "runtime.h"), Utils.GetEmbeddedResource("runtime.h")); - if (runtime == "MonoVM") + if (targetRuntime == TargetRuntime.MonoVM) { // lookup statically linked libraries via dlsym(), see handle_pinvoke_override() in runtime.m var pinvokeOverrides = new StringBuilder(); From bdd51195df75e96cbf5efee2acb8727ce230dd4c Mon Sep 17 00:00:00 2001 From: Filip Navara Date: Tue, 1 Aug 2023 11:08:48 +0200 Subject: [PATCH 19/24] Fix LibraryBuilder build --- src/tasks/LibraryBuilder/LibraryBuilder.csproj | 1 + 1 file changed, 1 insertion(+) diff --git a/src/tasks/LibraryBuilder/LibraryBuilder.csproj b/src/tasks/LibraryBuilder/LibraryBuilder.csproj index 50cd1c06867ef8..618895fc4c7b3a 100644 --- a/src/tasks/LibraryBuilder/LibraryBuilder.csproj +++ b/src/tasks/LibraryBuilder/LibraryBuilder.csproj @@ -23,6 +23,7 @@ + From d7aa5973e5b66a6751c87767db2925a1f1f7be28 Mon Sep 17 00:00:00 2001 From: Filip Navara Date: Sat, 2 Sep 2023 22:11:41 +0200 Subject: [PATCH 20/24] Cleanup --- .../msbuild/apple/build/AppleBuild.InTree.targets | 12 +++++++----- src/tasks/AppleAppBuilder/AppleAppBuilder.cs | 4 ++-- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/src/mono/msbuild/apple/build/AppleBuild.InTree.targets b/src/mono/msbuild/apple/build/AppleBuild.InTree.targets index 2bf671ed1e5ce9..bf0b75fc060b08 100644 --- a/src/mono/msbuild/apple/build/AppleBuild.InTree.targets +++ b/src/mono/msbuild/apple/build/AppleBuild.InTree.targets @@ -13,11 +13,13 @@ - - - + + + + + diff --git a/src/tasks/AppleAppBuilder/AppleAppBuilder.cs b/src/tasks/AppleAppBuilder/AppleAppBuilder.cs index 3fcf32778f2cf9..9635fe02dc22d9 100644 --- a/src/tasks/AppleAppBuilder/AppleAppBuilder.cs +++ b/src/tasks/AppleAppBuilder/AppleAppBuilder.cs @@ -204,7 +204,7 @@ public void ValidateRuntimeSelection() if (!string.IsNullOrEmpty(MonoRuntimeHeaders)) throw new ArgumentException($"Property \"{nameof(MonoRuntimeHeaders)}\" is not supported with {Runtime} runtime and will be ignored."); - if (!string.IsNullOrEmpty(MainLibraryFileName) && Runtime == "NativeAOT") + if (!string.IsNullOrEmpty(MainLibraryFileName) && targetRuntime == TargetRuntime.NativeAOT) throw new ArgumentException($"Property \"{nameof(MainLibraryFileName)}\" is not supported with {Runtime} runtime and will be ignored."); if (ForceInterpreter) @@ -213,7 +213,7 @@ public void ValidateRuntimeSelection() if (ForceAOT) throw new ArgumentException($"Property \"{nameof(ForceAOT)}\" is not supported with {Runtime} runtime and will be ignored."); - if (RuntimeComponents.Length > 0 && Runtime == "NativeAOT") + if (RuntimeComponents.Length > 0 && targetRuntime == TargetRuntime.NativeAOT) throw new ArgumentException($"Item \"{nameof(RuntimeComponents)}\" is not supported with NativeAOT runtime and will be ignored."); if (!string.IsNullOrEmpty(DiagnosticPorts)) From f8c704bbbda09b6b0876327e8d57df8a474c6b12 Mon Sep 17 00:00:00 2001 From: Filip Navara Date: Wed, 7 Feb 2024 22:18:20 +0100 Subject: [PATCH 21/24] Fix after rebase --- docs/workflow/building/coreclr/ios.md | 2 +- eng/Subsets.props | 6 +++--- .../ILCompiler.Reflection.ReadyToRun.Experimental.pkgproj | 2 +- src/coreclr/dlls/mscoree/coreclr/CMakeLists.txt | 3 +++ .../pkg/sfx/Microsoft.NETCore.App/Directory.Build.props | 1 + src/mono/msbuild/apple/build/AppleBuild.props | 6 +++--- src/native/libs/System.Globalization.Native/CMakeLists.txt | 6 ++++++ src/tasks/AppleAppBuilder/Xcode.cs | 6 +++--- 8 files changed, 21 insertions(+), 11 deletions(-) diff --git a/docs/workflow/building/coreclr/ios.md b/docs/workflow/building/coreclr/ios.md index b56816a8e9d22b..1b62ed751464b2 100644 --- a/docs/workflow/building/coreclr/ios.md +++ b/docs/workflow/building/coreclr/ios.md @@ -9,7 +9,7 @@ Build requirements are the same as for building native CoreCLR on macOS. iPhone Build the runtime pack and tools with ``` -./build.sh clr+clr.nativeaotlibs+libs+packs -os [iossimulator/tvossimulator/maccatalyst] -arch [x64/arm64] -cross +./build.sh clr+clr.runtime+libs+packs -os [iossimulator/tvossimulator/maccatalyst] -arch [x64/arm64] -cross -c Release ``` ## Running the sample iOS app diff --git a/eng/Subsets.props b/eng/Subsets.props index 41f63f11bbd795..656ca0c08568bd 100644 --- a/eng/Subsets.props +++ b/eng/Subsets.props @@ -55,8 +55,8 @@ - Mono - CoreCLR + CoreCLR + Mono Mono $(PrimaryRuntimeFlavor) @@ -65,7 +65,7 @@ clr.native+linuxdac+clr.corelib+clr.tools+clr.nativecorelib+clr.packages+clr.nativeaotlibs+clr.crossarchtools+host.native clr.native+linuxdac+clr.corelib+clr.tools+clr.nativecorelib+clr.packages+clr.nativeaotlibs+clr.crossarchtools - clr.iltools+clr.packages + clr.iltools+clr.packages clr.alljits+clr.tools+clr.nativeaotlibs+clr.nativeaotruntime diff --git a/src/coreclr/.nuget/ILCompiler.Reflection.ReadyToRun.Experimental/ILCompiler.Reflection.ReadyToRun.Experimental.pkgproj b/src/coreclr/.nuget/ILCompiler.Reflection.ReadyToRun.Experimental/ILCompiler.Reflection.ReadyToRun.Experimental.pkgproj index b2945e29260152..0480327017db55 100644 --- a/src/coreclr/.nuget/ILCompiler.Reflection.ReadyToRun.Experimental/ILCompiler.Reflection.ReadyToRun.Experimental.pkgproj +++ b/src/coreclr/.nuget/ILCompiler.Reflection.ReadyToRun.Experimental/ILCompiler.Reflection.ReadyToRun.Experimental.pkgproj @@ -11,7 +11,7 @@ - + \lib\netstandard2.0\ diff --git a/src/coreclr/dlls/mscoree/coreclr/CMakeLists.txt b/src/coreclr/dlls/mscoree/coreclr/CMakeLists.txt index 7ba58d0297f9a9..c85dd3ebc91ead 100644 --- a/src/coreclr/dlls/mscoree/coreclr/CMakeLists.txt +++ b/src/coreclr/dlls/mscoree/coreclr/CMakeLists.txt @@ -221,6 +221,9 @@ endif(CLR_CMAKE_TARGET_WIN32) # add the install targets install_clr(TARGETS coreclr DESTINATIONS . sharedFramework COMPONENT runtime) +if(CLR_CMAKE_HOST_MACCATALYST OR CLR_CMAKE_HOST_IOS OR CLR_CMAKE_HOST_TVOS) + install_clr(TARGETS coreclr_static DESTINATIONS . sharedFramework COMPONENT runtime) +endif() # Enable profile guided optimization add_pgo(coreclr) diff --git a/src/installer/pkg/sfx/Microsoft.NETCore.App/Directory.Build.props b/src/installer/pkg/sfx/Microsoft.NETCore.App/Directory.Build.props index 0de334e7ff2113..b89ad091fb1ff8 100644 --- a/src/installer/pkg/sfx/Microsoft.NETCore.App/Directory.Build.props +++ b/src/installer/pkg/sfx/Microsoft.NETCore.App/Directory.Build.props @@ -104,6 +104,7 @@ + diff --git a/src/mono/msbuild/apple/build/AppleBuild.props b/src/mono/msbuild/apple/build/AppleBuild.props index 51ad3cacee67de..6625f122c63efb 100644 --- a/src/mono/msbuild/apple/build/AppleBuild.props +++ b/src/mono/msbuild/apple/build/AppleBuild.props @@ -24,7 +24,7 @@ <_AotCompileTargetName Condition="'$(UseNativeAOTRuntime)' == 'true'">_AppleNativeAotCompile <_AotCompileTargetName Condition="'$(UseNativeAOTRuntime)' != 'true'">_AppleAotCompile ComputeIlcCompileInputs;SetupOSSpecificProps;PrepareForILLink - <_ReadRuntimeComponentsManifestTargetName Condition="'$(UseNativeAOTRuntime)' != 'true'">_MonoReadAvailableComponentsManifest + <_ReadRuntimeComponentsManifestTargetName Condition="'$(UseNativeAOTRuntime)' != 'true' and '$(UseMonoRuntime)' != 'false'">_MonoReadAvailableComponentsManifest Publish @@ -44,6 +44,6 @@ <_CommonTargetsDir Condition="'$(_CommonTargetsDir)' == ''">$([MSBuild]::NormalizeDirectory($(MSBuildThisFileDirectory), '..', '..', 'common')) - - + + \ No newline at end of file diff --git a/src/native/libs/System.Globalization.Native/CMakeLists.txt b/src/native/libs/System.Globalization.Native/CMakeLists.txt index 07080ea9056bb5..1cdc1cb9231784 100644 --- a/src/native/libs/System.Globalization.Native/CMakeLists.txt +++ b/src/native/libs/System.Globalization.Native/CMakeLists.txt @@ -188,6 +188,12 @@ add_library(System.Globalization.Native-Static entrypoints.c ) +if (DEFINED CMAKE_ICU_DIR) + include_directories(${CMAKE_ICU_DIR}/include) + target_link_libraries(System.Globalization.Native-Static ${CMAKE_ICU_DIR}/lib/libicuuc.a ${CMAKE_ICU_DIR}/lib/libicui18n.a ${CMAKE_ICU_DIR}/lib/libicudata.a) + target_link_libraries(System.Globalization.Native-Static stdc++) +endif() + if(CLR_CMAKE_TARGET_UNIX OR CLR_CMAKE_TARGET_WASI) set_target_properties(System.Globalization.Native-Static PROPERTIES OUTPUT_NAME System.Globalization.Native CLEAN_DIRECT_OUTPUT 1) endif() diff --git a/src/tasks/AppleAppBuilder/Xcode.cs b/src/tasks/AppleAppBuilder/Xcode.cs index 4552d08223ee99..733e766d18b355 100644 --- a/src/tasks/AppleAppBuilder/Xcode.cs +++ b/src/tasks/AppleAppBuilder/Xcode.cs @@ -270,7 +270,7 @@ public string GenerateCMake( var predefinedExcludes = new List { ".dll.o", ".dll.s", ".dwarf", ".m", ".h", ".a", ".bc", "libmonosgen-2.0.dylib", "icudt*" }; // TODO: All of these exclusions shouldn't be needed once we carefully construct the publish folder on Helix - if (runtime == "NativeAOT") + if (targetRuntime == TargetRuntime.NativeAOT) { predefinedExcludes.Add(".dll"); predefinedExcludes.Add(".pdb"); @@ -361,7 +361,7 @@ public string GenerateCMake( // Current differences: // - NativeAOT produces {ProjectName}.dylib, while MonoAOT produces lib{ProjectName}.dylib // - NativeAOT places the library in the 'workspace' location ie 'publish' folder, while MonoAOT places it in 'binDir' ie 'AppBundle' - if (useNativeAOTRuntime) + if (targetRuntime == TargetRuntime.NativeAOT) { libraryPath = Path.Combine(workspace, $"{projectName}.dylib"); } @@ -378,7 +378,7 @@ public string GenerateCMake( cmakeLists = cmakeLists.Replace("%DYLIB_PATH%", libraryPath); // pass the shared library to the linker for dynamic linking - if (useNativeAOTRuntime) + if (targetRuntime == TargetRuntime.NativeAOT) toLink += $" {libraryPath}{Environment.NewLine}"; } else From 337751be1a70eadeb69824478927b7f16d4916ce Mon Sep 17 00:00:00 2001 From: Filip Navara Date: Wed, 7 Feb 2024 22:38:56 +0100 Subject: [PATCH 22/24] Revert accidental change --- src/coreclr/pal/src/misc/sysinfo.cpp | 423 --------------------------- 1 file changed, 423 deletions(-) diff --git a/src/coreclr/pal/src/misc/sysinfo.cpp b/src/coreclr/pal/src/misc/sysinfo.cpp index a9ae481cba2915..1239b962766f2e 100644 --- a/src/coreclr/pal/src/misc/sysinfo.cpp +++ b/src/coreclr/pal/src/misc/sysinfo.cpp @@ -241,426 +241,3 @@ GetSystemInfo( LOGEXIT("GetSystemInfo returns VOID\n"); PERF_EXIT(GetSystemInfo); } - -// Get memory size multiplier based on the passed in units (k = kilo, m = mega, g = giga) -static uint64_t GetMemorySizeMultiplier(char units) -{ - switch(units) - { - case 'g': - case 'G': return 1024 * 1024 * 1024; - case 'm': - case 'M': return 1024 * 1024; - case 'k': - case 'K': return 1024; - } - - // No units multiplier - return 1; -} - -#ifndef __APPLE__ -// Try to read the MemAvailable entry from /proc/meminfo. -// Return true if the /proc/meminfo existed, the entry was present and we were able to parse it. -static bool ReadMemAvailable(uint64_t* memAvailable) -{ - bool foundMemAvailable = false; - FILE* memInfoFile = fopen("/proc/meminfo", "r"); - if (memInfoFile != NULL) - { - char *line = nullptr; - size_t lineLen = 0; - - while (getline(&line, &lineLen, memInfoFile) != -1) - { - char units = '\0'; - uint64_t available; - int fieldsParsed = sscanf(line, "MemAvailable: %" SCNu64 " %cB", &available, &units); - - if (fieldsParsed >= 1) - { - uint64_t multiplier = GetMemorySizeMultiplier(units); - *memAvailable = available * multiplier; - foundMemAvailable = true; - break; - } - } - - free(line); - fclose(memInfoFile); - } - - return foundMemAvailable; -} -#endif // __APPLE__ - -/*++ -Function: - GlobalMemoryStatusEx - -GlobalMemoryStatusEx - -Retrieves information about the system's current usage of both physical and virtual memory. - -Return Values - -This function returns a BOOL to indicate its success status. - ---*/ -BOOL -PALAPI -GlobalMemoryStatusEx( - IN OUT LPMEMORYSTATUSEX lpBuffer) -{ - - PERF_ENTRY(GlobalMemoryStatusEx); - ENTRY("GlobalMemoryStatusEx (lpBuffer=%p)\n", lpBuffer); - - lpBuffer->dwMemoryLoad = 0; - lpBuffer->ullTotalPhys = 0; - lpBuffer->ullAvailPhys = 0; - lpBuffer->ullTotalPageFile = 0; - lpBuffer->ullAvailPageFile = 0; - lpBuffer->ullTotalVirtual = 0; - lpBuffer->ullAvailVirtual = 0; - lpBuffer->ullAvailExtendedVirtual = 0; - - BOOL fRetVal = FALSE; - int rc; - - // Get the physical memory size -#if HAVE_SYSCONF && HAVE__SC_PHYS_PAGES - uint64_t physical_memory; - - // Get the Physical memory size - physical_memory = ((uint64_t)sysconf( _SC_PHYS_PAGES )) * ((uint64_t) sysconf( _SC_PAGE_SIZE )); - lpBuffer->ullTotalPhys = (DWORDLONG)physical_memory; - fRetVal = TRUE; -#elif HAVE_SYSCTL - int64_t physical_memory; - size_t length; - // Get the Physical memory size - int mib[] = { CTL_HW, HW_MEMSIZE }; - length = sizeof(INT64); - rc = sysctl(mib, 2, &physical_memory, &length, NULL, 0); - if (rc != 0) - { - ASSERT("sysctl failed for HW_MEMSIZE (%d)\n", errno); - } - else - { - lpBuffer->ullTotalPhys = (DWORDLONG)physical_memory; - fRetVal = TRUE; - } - -#endif // HAVE_SYSCTL - - // Get swap file size, consider the ability to get the values optional - // (don't return FALSE from the GlobalMemoryStatusEx) -#if HAVE_XSW_USAGE - // This is available on OSX - struct xsw_usage xsu; - int mib[] = { CTL_HW, VM_SWAPUSAGE }; - size_t length = sizeof(xsu); - rc = sysctl(mib, 2, &xsu, &length, NULL, 0); - if (rc == 0) - { - lpBuffer->ullTotalPageFile = xsu.xsu_total; - lpBuffer->ullAvailPageFile = xsu.xsu_avail; - } -#elif HAVE_XSWDEV - // E.g. FreeBSD - struct xswdev xsw; - int mib[3]; - size_t length = 2; - rc = sysctlnametomib("vm.swap_info", mib, &length); - if (rc == 0) - { - int pagesize = getpagesize(); - // Aggregate the information for all swap files on the system - for (mib[2] = 0; ; mib[2]++) - { - length = sizeof(xsw); - rc = sysctl(mib, 3, &xsw, &length, NULL, 0); - if ((rc < 0) || (xsw.xsw_version != XSWDEV_VERSION)) - { - // All the swap files were processed or coreclr was built against - // a version of headers not compatible with the current XSWDEV_VERSION. - break; - } - - DWORDLONG avail = xsw.xsw_nblks - xsw.xsw_used; - lpBuffer->ullTotalPageFile += (DWORDLONG)xsw.xsw_nblks * pagesize; - lpBuffer->ullAvailPageFile += (DWORDLONG)avail * pagesize; - } - } -#elif HAVE_SWAPCTL - struct anoninfo ai; - if (swapctl(SC_AINFO, &ai) != -1) - { - int pagesize = getpagesize(); - lpBuffer->ullTotalPageFile = ai.ani_max * pagesize; - lpBuffer->ullAvailPageFile = ai.ani_free * pagesize; - } -#elif HAVE_SYSINFO - // Linux - struct sysinfo info; - rc = sysinfo(&info); - if (rc == 0) - { - lpBuffer->ullTotalPageFile = info.totalswap; - lpBuffer->ullAvailPageFile = info.freeswap; -#if HAVE_SYSINFO_WITH_MEM_UNIT - // A newer version of the sysinfo structure represents all the sizes - // in mem_unit instead of bytes - lpBuffer->ullTotalPageFile *= info.mem_unit; - lpBuffer->ullAvailPageFile *= info.mem_unit; -#endif // HAVE_SYSINFO_WITH_MEM_UNIT - } -#endif // HAVE_SYSINFO - - // Get the physical memory in use - from it, we can get the physical memory available. - // We do this only when we have the total physical memory available. - if (lpBuffer->ullTotalPhys > 0) - { -#ifndef __APPLE__ - static volatile bool tryReadMemInfo = true; - - if (tryReadMemInfo) - { - // Ensure that we don't try to read the /proc/meminfo in successive calls to the GlobalMemoryStatusEx - // if we have failed to access the file or the file didn't contain the MemAvailable value. - tryReadMemInfo = ReadMemAvailable((uint64_t*)&lpBuffer->ullAvailPhys); - } - - if (!tryReadMemInfo) - { - // The /proc/meminfo doesn't exist or it doesn't contain the MemAvailable row or the format of the row is invalid - // Fall back to getting the available pages using sysconf. - lpBuffer->ullAvailPhys = sysconf(SYSCONF_PAGES) * sysconf(_SC_PAGE_SIZE); - } - - INT64 used_memory = lpBuffer->ullTotalPhys - lpBuffer->ullAvailPhys; - lpBuffer->dwMemoryLoad = (DWORD)((used_memory * 100) / lpBuffer->ullTotalPhys); -#else - vm_size_t page_size; - mach_port_t mach_port; - mach_msg_type_number_t count; - vm_statistics_data_t vm_stats; - mach_port = mach_host_self(); - count = sizeof(vm_stats) / sizeof(natural_t); - if (KERN_SUCCESS == host_page_size(mach_port, &page_size)) - { - if (KERN_SUCCESS == host_statistics(mach_port, HOST_VM_INFO, (host_info_t)&vm_stats, &count)) - { - lpBuffer->ullAvailPhys = (int64_t)vm_stats.free_count * (int64_t)page_size; - INT64 used_memory = ((INT64)vm_stats.active_count + (INT64)vm_stats.inactive_count + (INT64)vm_stats.wire_count) * (INT64)page_size; - lpBuffer->dwMemoryLoad = (DWORD)((used_memory * 100) / lpBuffer->ullTotalPhys); - } - } - mach_port_deallocate(mach_task_self(), mach_port); -#endif // __APPLE__ - } - -#ifndef TARGET_RISCV64 - // There is no API to get the total virtual address space size on - // Unix, so we use a constant value representing 128TB, which is - // the approximate size of total user virtual address space on - // the currently supported Unix systems. - static const UINT64 VMSize = (1ull << 47); -#else // TARGET_RISCV64 - // For RISC-V Linux Kernel SV39 virtual memory limit is 256gb. - static const UINT64 VMSize = (1ull << 38); -#endif // TARGET_RISCV64 - lpBuffer->ullTotalVirtual = VMSize; - lpBuffer->ullAvailVirtual = lpBuffer->ullAvailPhys; - - LOGEXIT("GlobalMemoryStatusEx returns %d\n", fRetVal); - PERF_EXIT(GlobalMemoryStatusEx); - - return fRetVal; -} - -bool -PAL_ReadMemoryValueFromFile(const char* filename, uint64_t* val) -{ - bool result = false; - char *line = nullptr; - size_t lineLen = 0; - char* endptr = nullptr; - uint64_t num = 0, multiplier; - - if (val == nullptr) - return false; - - FILE* file = fopen(filename, "r"); - if (file == nullptr) - goto done; - - if (getline(&line, &lineLen, file) == -1) - goto done; - - errno = 0; - num = strtoull(line, &endptr, 0); - if (errno != 0) - goto done; - - multiplier = GetMemorySizeMultiplier(*endptr); - *val = num * multiplier; - result = true; - if (*val/multiplier != num) - result = false; -done: - if (file) - fclose(file); - free(line); - return result; -} - -#define UPDATE_CACHE_SIZE_AND_LEVEL(NEW_CACHE_SIZE, NEW_CACHE_LEVEL) if (NEW_CACHE_SIZE > cacheSize) { cacheSize = NEW_CACHE_SIZE; cacheLevel = NEW_CACHE_LEVEL; } - -size_t -PALAPI -PAL_GetLogicalProcessorCacheSizeFromOS() -{ - size_t cacheLevel = 0; - size_t cacheSize = 0; - size_t size; - -#ifdef _SC_LEVEL1_DCACHE_SIZE - size = ( size_t) sysconf(_SC_LEVEL1_DCACHE_SIZE); - UPDATE_CACHE_SIZE_AND_LEVEL(size, 1) -#endif -#ifdef _SC_LEVEL2_CACHE_SIZE - size = ( size_t) sysconf(_SC_LEVEL2_CACHE_SIZE); - UPDATE_CACHE_SIZE_AND_LEVEL(size, 2) -#endif -#ifdef _SC_LEVEL3_CACHE_SIZE - size = ( size_t) sysconf(_SC_LEVEL3_CACHE_SIZE); - UPDATE_CACHE_SIZE_AND_LEVEL(size, 3) -#endif -#ifdef _SC_LEVEL4_CACHE_SIZE - size = ( size_t) sysconf(_SC_LEVEL4_CACHE_SIZE); - UPDATE_CACHE_SIZE_AND_LEVEL(size, 4) -#endif - -#if defined(TARGET_LINUX) && !defined(HOST_ARM) && !defined(HOST_X86) - if (cacheSize == 0) - { - // - // Fallback to retrieve cachesize via /sys/.. if sysconf was not available - // for the platform. Currently musl and arm64 should be only cases to use - // this method to determine cache size. - // - size_t level; - char path_to_size_file[] = "/sys/devices/system/cpu/cpu0/cache/index-/size"; - char path_to_level_file[] = "/sys/devices/system/cpu/cpu0/cache/index-/level"; - int index = 40; - _ASSERTE(path_to_size_file[index] == '-'); - _ASSERTE(path_to_level_file[index] == '-'); - - for (int i = 0; i < 5; i++) - { - path_to_size_file[index] = (char)(48 + i); - - if (PAL_ReadMemoryValueFromFile(path_to_size_file, &size)) - { - path_to_level_file[index] = (char)(48 + i); - - if (PAL_ReadMemoryValueFromFile(path_to_level_file, &level)) - { - UPDATE_CACHE_SIZE_AND_LEVEL(size, level) - } - else - { - cacheSize = std::max(cacheSize, size); - } - } - } - } -#endif - -#if (defined(HOST_ARM64) || defined(HOST_LOONGARCH64)) && !defined(TARGET_APPLE) - if (cacheSize == 0) - { - // We expect to get the L3 cache size for Arm64 but currently expected to be missing that info - // from most of the machines with an exceptions on some machines. - // - // _SC_LEVEL*_*CACHE_SIZE is not yet present. Work is in progress to enable this for arm64 - // - // /sys/devices/system/cpu/cpu*/cache/index*/ is also not yet present in most systems. - // Arm64 patch is in Linux kernel tip. - // - // midr_el1 is available in "/sys/devices/system/cpu/cpu0/regs/identification/midr_el1", - // but without an exhaustive list of ARM64 processors any decode of midr_el1 - // Would likely be incomplete - - // Published information on ARM64 architectures is limited. - // If we use recent high core count chips as a guide for state of the art, we find - // total L3 cache to be 1-2MB/core. As always, there are exceptions. - - // Estimate cache size based on CPU count - // Assume lower core count are lighter weight parts which are likely to have smaller caches - // Assume L3$/CPU grows linearly from 256K to 1.5M/CPU as logicalCPUs grows from 2 to 12 CPUs - DWORD logicalCPUs = PAL_GetLogicalCpuCountFromOS(); - - cacheSize = logicalCPUs*std::min(1536, std::max(256, (int)logicalCPUs*128))*1024; - } -#endif - -#if HAVE_SYSCTLBYNAME - if (cacheSize == 0) - { - int64_t cacheSizeFromSysctl = 0; - size_t sz = sizeof(cacheSizeFromSysctl); - const bool success = false - // macOS: Since macOS 12.0, Apple added ".perflevelX." to determinate cache sizes for efficiency - // and performance cores separately. "perflevel0" stands for "performance" - || sysctlbyname("hw.perflevel0.l3cachesize", &cacheSizeFromSysctl, &sz, nullptr, 0) == 0 - || sysctlbyname("hw.perflevel0.l2cachesize", &cacheSizeFromSysctl, &sz, nullptr, 0) == 0 - // macOS: these report cache sizes for efficiency cores only: - || sysctlbyname("hw.l3cachesize", &cacheSizeFromSysctl, &sz, nullptr, 0) == 0 - || sysctlbyname("hw.l2cachesize", &cacheSizeFromSysctl, &sz, nullptr, 0) == 0 - || sysctlbyname("hw.l1dcachesize", &cacheSizeFromSysctl, &sz, nullptr, 0) == 0; - if (success) - { - _ASSERTE(cacheSizeFromSysctl > 0); - cacheSize = ( size_t) cacheSizeFromSysctl; - } - } -#endif - -#if (defined(HOST_ARM64) || defined(HOST_LOONGARCH64)) && !defined(TARGET_APPLE) - if (cacheLevel != 3) - { - // We expect to get the L3 cache size for Arm64 but currently expected to be missing that info - // from most of the machines. - // Hence, just use the following heuristics at best depending on the CPU count - // 1 ~ 4 : 4 MB - // 5 ~ 16 : 8 MB - // 17 ~ 64 : 16 MB - // 65+ : 32 MB - DWORD logicalCPUs = PAL_GetLogicalCpuCountFromOS(); - if (logicalCPUs < 5) - { - cacheSize = 4; - } - else if (logicalCPUs < 17) - { - cacheSize = 8; - } - else if (logicalCPUs < 65) - { - cacheSize = 16; - } - else - { - cacheSize = 32; - } - - cacheSize *= (1024 * 1024); - } -#endif - - return cacheSize; -} From bb0cde35c24d725e4c252537a1bdf7926bf30021 Mon Sep 17 00:00:00 2001 From: Filip Navara Date: Wed, 7 Feb 2024 22:59:11 +0100 Subject: [PATCH 23/24] Fix tvOS build --- src/coreclr/pal/src/configure.cmake | 6 +++++- src/coreclr/pal/src/exception/seh.cpp | 4 ++-- src/coreclr/pal/src/exception/signal.cpp | 12 ++++++++++-- src/coreclr/pal/src/include/pal/thread.hpp | 2 +- src/coreclr/pal/src/init/sxs.cpp | 2 +- src/coreclr/pal/src/misc/sysinfo.cpp | 4 ++-- src/coreclr/pal/src/thread/process.cpp | 8 ++++++++ src/coreclr/pal/src/thread/thread.cpp | 6 +++--- src/native/corehost/apphost/static/CMakeLists.txt | 5 ++++- .../corehost/hostpolicy/hostpolicy_context.cpp | 2 ++ 10 files changed, 38 insertions(+), 13 deletions(-) diff --git a/src/coreclr/pal/src/configure.cmake b/src/coreclr/pal/src/configure.cmake index 624ed59d8e765e..669357c68d0895 100644 --- a/src/coreclr/pal/src/configure.cmake +++ b/src/coreclr/pal/src/configure.cmake @@ -134,7 +134,11 @@ check_function_exists(statvfs HAVE_STATVFS) check_function_exists(thread_self HAVE_THREAD_SELF) check_function_exists(_lwp_self HAVE__LWP_SELF) check_function_exists(pthread_mach_thread_np HAVE_MACH_THREADS) -check_function_exists(thread_set_exception_ports HAVE_MACH_EXCEPTIONS) +if(CLR_CMAKE_TARGET_TVOS) + set(HAVE_MACH_EXCEPTIONS 0) +else() + check_function_exists(thread_set_exception_ports HAVE_MACH_EXCEPTIONS) +endif() check_function_exists(vm_allocate HAVE_VM_ALLOCATE) check_function_exists(vm_read HAVE_VM_READ) check_function_exists(directio HAVE_DIRECTIO) diff --git a/src/coreclr/pal/src/exception/seh.cpp b/src/coreclr/pal/src/exception/seh.cpp index 2045084a5683e8..32129349105def 100644 --- a/src/coreclr/pal/src/exception/seh.cpp +++ b/src/coreclr/pal/src/exception/seh.cpp @@ -305,7 +305,7 @@ PAL_ERROR SEHEnable(CPalThread *pthrCurrent) { #if HAVE_MACH_EXCEPTIONS return pthrCurrent->EnableMachExceptions(); -#elif defined(__linux__) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(__sun) +#elif defined(__linux__) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(__sun) || defined(TARGET_TVOS) return NO_ERROR; #else// HAVE_MACH_EXCEPTIONS #error not yet implemented @@ -330,7 +330,7 @@ PAL_ERROR SEHDisable(CPalThread *pthrCurrent) { #if HAVE_MACH_EXCEPTIONS return pthrCurrent->DisableMachExceptions(); -#elif defined(__linux__) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(__sun) +#elif defined(__linux__) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(__sun) || defined(TARGET_TVOS) return NO_ERROR; #else // HAVE_MACH_EXCEPTIONS #error not yet implemented diff --git a/src/coreclr/pal/src/exception/signal.cpp b/src/coreclr/pal/src/exception/signal.cpp index c9112432f8d930..2b4e3b0f3a567d 100644 --- a/src/coreclr/pal/src/exception/signal.cpp +++ b/src/coreclr/pal/src/exception/signal.cpp @@ -182,7 +182,7 @@ BOOL SEHInitializeSignals(CorUnix::CPalThread *pthrCurrent, DWORD flags) handle_signal(SIGINT, sigint_handler, &g_previous_sigint, 0 /* additionalFlags */, true /* skipIgnored */); handle_signal(SIGQUIT, sigquit_handler, &g_previous_sigquit, 0 /* additionalFlags */, true /* skipIgnored */); -#if HAVE_MACH_EXCEPTIONS +#if HAVE_MACH_EXCEPTIONS || defined(TARGET_TVOS) handle_signal(SIGSEGV, sigsegv_handler, &g_previous_sigsegv); #else handle_signal(SIGTRAP, sigtrap_handler, &g_previous_sigtrap); @@ -471,7 +471,15 @@ static void sigfpe_handler(int code, siginfo_t *siginfo, void *context) invoke_previous_action(&g_previous_sigfpe, code, siginfo, context); } -#if !HAVE_MACH_EXCEPTIONS +#if defined(TARGET_TVOS) + +static bool SwitchStackAndExecuteHandler(int code, siginfo_t *siginfo, void *context, size_t sp) +{ + // Not yet implemented + _exit(0xdead); +} + +#elif !HAVE_MACH_EXCEPTIONS /*++ Function : diff --git a/src/coreclr/pal/src/include/pal/thread.hpp b/src/coreclr/pal/src/include/pal/thread.hpp index 411040f39b21a5..f0788a3f18e6d4 100644 --- a/src/coreclr/pal/src/include/pal/thread.hpp +++ b/src/coreclr/pal/src/include/pal/thread.hpp @@ -588,7 +588,7 @@ namespace CorUnix m_pNext = pNext; }; -#if !HAVE_MACH_EXCEPTIONS +#if !HAVE_MACH_EXCEPTIONS && !defined(TARGET_TVOS) BOOL EnsureSignalAlternateStack( void diff --git a/src/coreclr/pal/src/init/sxs.cpp b/src/coreclr/pal/src/init/sxs.cpp index ddb084098efa51..6b6d726f54818d 100644 --- a/src/coreclr/pal/src/init/sxs.cpp +++ b/src/coreclr/pal/src/init/sxs.cpp @@ -64,7 +64,7 @@ AllocatePalThread(CPalThread **ppThread) goto exit; } -#if !HAVE_MACH_EXCEPTIONS +#if !HAVE_MACH_EXCEPTIONS && !defined(TARGET_TVOS) // Ensure alternate stack for SIGSEGV handling. Our SIGSEGV handler is set to // run on an alternate stack and the stack needs to be allocated per thread. if (!pThread->EnsureSignalAlternateStack()) diff --git a/src/coreclr/pal/src/misc/sysinfo.cpp b/src/coreclr/pal/src/misc/sysinfo.cpp index 1239b962766f2e..450eed08ada19f 100644 --- a/src/coreclr/pal/src/misc/sysinfo.cpp +++ b/src/coreclr/pal/src/misc/sysinfo.cpp @@ -65,7 +65,7 @@ Revision History: #include #endif // defined(TARGET_APPLE) -#if !defined(TARGET_IOS) +#if !defined(TARGET_IOS) && !defined(TARGET_TVOS) // On some platforms sys/user.h ends up defining _DEBUG; if so // remove the definition before including the header and put // back our definition afterwards @@ -79,7 +79,7 @@ Revision History: #define _DEBUG OLD_DEBUG #undef OLD_DEBUG #endif -#endif // !TARGET_IOS +#endif // !TARGET_IOS && !TARGET_TVOS #include "pal/dbgmsg.h" #include "pal/process.h" diff --git a/src/coreclr/pal/src/thread/process.cpp b/src/coreclr/pal/src/thread/process.cpp index 88f34411133606..c919b08cdc8f2b 100644 --- a/src/coreclr/pal/src/thread/process.cpp +++ b/src/coreclr/pal/src/thread/process.cpp @@ -545,6 +545,9 @@ CorUnix::InternalCreateProcess( LPPROCESS_INFORMATION lpProcessInformation ) { +#ifdef TARGET_TVOS + return ERROR_NOT_SUPPORTED; +#else PAL_ERROR palError = NO_ERROR; IPalObject *pobjProcess = NULL; IPalObject *pobjProcessRegistered = NULL; @@ -1081,6 +1084,7 @@ CorUnix::InternalCreateProcess( } return palError; +#endif // !TARGET_TVOS } @@ -2195,6 +2199,9 @@ PROCCreateCrashDump( INT cbErrorMessageBuffer, bool serialize) { +#if defined(TARGET_IOS) || defined(TARGET_TVOS) + return FALSE; +#else _ASSERTE(argv.size() > 0); _ASSERTE(errorMessageBuffer == nullptr || cbErrorMessageBuffer > 0); @@ -2319,6 +2326,7 @@ PROCCreateCrashDump( } } return true; +#endif // !TARGET_IOS && !TARGET_TVOS } /*++ diff --git a/src/coreclr/pal/src/thread/thread.cpp b/src/coreclr/pal/src/thread/thread.cpp index 8c66057a3b5d40..be76691c36a6b5 100644 --- a/src/coreclr/pal/src/thread/thread.cpp +++ b/src/coreclr/pal/src/thread/thread.cpp @@ -173,7 +173,7 @@ static void InternalEndCurrentThreadWrapper(void *arg) will lock its own critical section */ LOADCallDllMain(DLL_THREAD_DETACH, NULL); -#if !HAVE_MACH_EXCEPTIONS +#if !HAVE_MACH_EXCEPTIONS && !defined(TARGET_TVOS) pThread->FreeSignalAlternateStack(); #endif // !HAVE_MACH_EXCEPTIONS @@ -1683,7 +1683,7 @@ CPalThread::ThreadEntry( } #endif // HAVE_SCHED_GETAFFINITY && HAVE_SCHED_SETAFFINITY -#if !HAVE_MACH_EXCEPTIONS +#if !HAVE_MACH_EXCEPTIONS && !defined(TARGET_TVOS) if (!pThread->EnsureSignalAlternateStack()) { ASSERT("Cannot allocate alternate stack for SIGSEGV!\n"); @@ -2406,7 +2406,7 @@ CPalThread::WaitForStartStatus( return m_fStartStatus; } -#if !HAVE_MACH_EXCEPTIONS +#if !HAVE_MACH_EXCEPTIONS && !defined(TARGET_TVOS) /*++ Function : EnsureSignalAlternateStack diff --git a/src/native/corehost/apphost/static/CMakeLists.txt b/src/native/corehost/apphost/static/CMakeLists.txt index abfc3d3b6d17c4..161e14baa75edf 100644 --- a/src/native/corehost/apphost/static/CMakeLists.txt +++ b/src/native/corehost/apphost/static/CMakeLists.txt @@ -144,7 +144,6 @@ else() System.Globalization.Native-Static System.IO.Compression.Native-Static - System.Net.Security.Native-Static System.Native-Static palrt @@ -155,6 +154,10 @@ else() nativeresourcestring ) + if(NOT CLR_CMAKE_TARGET_TVOS) + LIST(APPEND NATIVE_LIBS System.Net.Security.Native-Static) + endif() + if(NOT CLR_CMAKE_TARGET_MACCATALYST AND NOT CLR_CMAKE_TARGET_IOS AND NOT CLR_CMAKE_TARGET_TVOS AND NOT CLR_CMAKE_HOST_ANDROID) LIST(APPEND NATIVE_LIBS System.Security.Cryptography.Native.OpenSsl-Static) endif() diff --git a/src/native/corehost/hostpolicy/hostpolicy_context.cpp b/src/native/corehost/hostpolicy/hostpolicy_context.cpp index a51e4677605987..0001d2fd63ed79 100644 --- a/src/native/corehost/hostpolicy/hostpolicy_context.cpp +++ b/src/native/corehost/hostpolicy/hostpolicy_context.cpp @@ -60,10 +60,12 @@ namespace // This function is only called with the library name specified for a p/invoke, not any variations. // It must handle exact matches to the names specified. See Interop.Libraries.cs for each platform. #if !defined(_WIN32) +#if !defined(TARGET_TVOS) if (strcmp(library_name, LIB_NAME("System.Net.Security.Native")) == 0) { return SecurityResolveDllImport(entry_point_name); } +#endif if (strcmp(library_name, LIB_NAME("System.Native")) == 0) { From 8d293e15054070bf5474dd1cd9c0321a08962001 Mon Sep 17 00:00:00 2001 From: Filip Navara Date: Fri, 5 Apr 2024 10:30:15 +0200 Subject: [PATCH 24/24] Drop CoreCLR tvOS Simulator support for now --- docs/workflow/building/coreclr/ios.md | 4 +- src/coreclr/CMakeLists.txt | 49 +++++++++++-------- .../dlls/mscoree/coreclr/CMakeLists.txt | 2 +- src/coreclr/pal/src/configure.cmake | 6 +-- src/coreclr/pal/src/exception/seh.cpp | 4 +- src/coreclr/pal/src/exception/signal.cpp | 12 +---- src/coreclr/pal/src/include/pal/thread.hpp | 2 +- src/coreclr/pal/src/init/sxs.cpp | 2 +- src/coreclr/pal/src/thread/process.cpp | 8 +-- src/coreclr/pal/src/thread/thread.cpp | 6 +-- 10 files changed, 44 insertions(+), 51 deletions(-) diff --git a/docs/workflow/building/coreclr/ios.md b/docs/workflow/building/coreclr/ios.md index 1b62ed751464b2..9061fd17da438c 100644 --- a/docs/workflow/building/coreclr/ios.md +++ b/docs/workflow/building/coreclr/ios.md @@ -1,4 +1,4 @@ -# Cross Compilation for iOS/tvOS Simulator on macOS +# Cross Compilation for iOS Simulator on macOS ## Requirements @@ -9,7 +9,7 @@ Build requirements are the same as for building native CoreCLR on macOS. iPhone Build the runtime pack and tools with ``` -./build.sh clr+clr.runtime+libs+packs -os [iossimulator/tvossimulator/maccatalyst] -arch [x64/arm64] -cross -c Release +./build.sh clr+clr.runtime+libs+packs -os [iossimulator/maccatalyst] -arch [x64/arm64] -cross -c Release ``` ## Running the sample iOS app diff --git a/src/coreclr/CMakeLists.txt b/src/coreclr/CMakeLists.txt index 27a71f84997550..0b358c8ca08a4b 100644 --- a/src/coreclr/CMakeLists.txt +++ b/src/coreclr/CMakeLists.txt @@ -29,6 +29,10 @@ if(CORECLR_SET_RPATH) set(MACOSX_RPATH ON) endif(CORECLR_SET_RPATH) +if(CLR_CMAKE_HOST_TVOS) + set(FEATURE_STANDALONE_GC 0) +endif() + OPTION(CLR_CMAKE_ENABLE_CODE_COVERAGE "Enable code coverage" OFF) #---------------------------------------------------- @@ -97,8 +101,9 @@ if(CLR_CMAKE_HOST_UNIX) add_linker_flag(-Wl,-z,notext) endif() - add_subdirectory(pal) - + if(NOT CLR_CMAKE_HOST_TVOS) + add_subdirectory(pal) + endif() if(NOT CLR_CMAKE_HOST_MACCATALYST AND NOT CLR_CMAKE_HOST_IOS AND NOT CLR_CMAKE_HOST_TVOS) add_subdirectory(hosts) endif() @@ -122,7 +127,9 @@ add_subdirectory(pal/prebuilt/inc) set(EP_GENERATED_HEADER_PATH "${GENERATED_INCLUDE_DIR}") include (${CLR_SRC_NATIVE_DIR}/eventpipe/configure.cmake) -add_subdirectory(debug/debug-pal) +if(NOT CLR_CMAKE_HOST_TVOS) + add_subdirectory(debug/debug-pal) +endif() add_subdirectory(minipal) @@ -237,25 +244,27 @@ if(CLR_CMAKE_HOST_UNIX) add_subdirectory(nativeresources) endif(CLR_CMAKE_HOST_UNIX) -add_subdirectory(utilcode) -add_subdirectory(inc) +if(NOT CLR_CMAKE_HOST_TVOS) + add_subdirectory(utilcode) + add_subdirectory(inc) -if(CLR_CMAKE_HOST_UNIX) + if(CLR_CMAKE_HOST_UNIX) add_subdirectory(palrt) -endif(CLR_CMAKE_HOST_UNIX) - -add_subdirectory(ilasm) -add_subdirectory(ildasm) -add_subdirectory(gcinfo) -add_subdirectory(jit) -add_subdirectory(vm) -add_subdirectory(md) -add_subdirectory(debug) -add_subdirectory(binder) -add_subdirectory(classlibnative) -add_subdirectory(dlls) -add_subdirectory(unwinder) -add_subdirectory(interop) + endif(CLR_CMAKE_HOST_UNIX) + + add_subdirectory(ilasm) + add_subdirectory(ildasm) + add_subdirectory(gcinfo) + add_subdirectory(jit) + add_subdirectory(vm) + add_subdirectory(md) + add_subdirectory(debug) + add_subdirectory(binder) + add_subdirectory(classlibnative) + add_subdirectory(dlls) + add_subdirectory(unwinder) + add_subdirectory(interop) +endif() if(NOT CLR_CMAKE_HOST_MACCATALYST AND NOT CLR_CMAKE_HOST_IOS AND NOT CLR_CMAKE_HOST_TVOS) add_subdirectory(tools) diff --git a/src/coreclr/dlls/mscoree/coreclr/CMakeLists.txt b/src/coreclr/dlls/mscoree/coreclr/CMakeLists.txt index c85dd3ebc91ead..3c481c1cc03181 100644 --- a/src/coreclr/dlls/mscoree/coreclr/CMakeLists.txt +++ b/src/coreclr/dlls/mscoree/coreclr/CMakeLists.txt @@ -221,7 +221,7 @@ endif(CLR_CMAKE_TARGET_WIN32) # add the install targets install_clr(TARGETS coreclr DESTINATIONS . sharedFramework COMPONENT runtime) -if(CLR_CMAKE_HOST_MACCATALYST OR CLR_CMAKE_HOST_IOS OR CLR_CMAKE_HOST_TVOS) +if(CLR_CMAKE_HOST_MACCATALYST OR CLR_CMAKE_HOST_IOS) install_clr(TARGETS coreclr_static DESTINATIONS . sharedFramework COMPONENT runtime) endif() diff --git a/src/coreclr/pal/src/configure.cmake b/src/coreclr/pal/src/configure.cmake index 669357c68d0895..624ed59d8e765e 100644 --- a/src/coreclr/pal/src/configure.cmake +++ b/src/coreclr/pal/src/configure.cmake @@ -134,11 +134,7 @@ check_function_exists(statvfs HAVE_STATVFS) check_function_exists(thread_self HAVE_THREAD_SELF) check_function_exists(_lwp_self HAVE__LWP_SELF) check_function_exists(pthread_mach_thread_np HAVE_MACH_THREADS) -if(CLR_CMAKE_TARGET_TVOS) - set(HAVE_MACH_EXCEPTIONS 0) -else() - check_function_exists(thread_set_exception_ports HAVE_MACH_EXCEPTIONS) -endif() +check_function_exists(thread_set_exception_ports HAVE_MACH_EXCEPTIONS) check_function_exists(vm_allocate HAVE_VM_ALLOCATE) check_function_exists(vm_read HAVE_VM_READ) check_function_exists(directio HAVE_DIRECTIO) diff --git a/src/coreclr/pal/src/exception/seh.cpp b/src/coreclr/pal/src/exception/seh.cpp index 32129349105def..2045084a5683e8 100644 --- a/src/coreclr/pal/src/exception/seh.cpp +++ b/src/coreclr/pal/src/exception/seh.cpp @@ -305,7 +305,7 @@ PAL_ERROR SEHEnable(CPalThread *pthrCurrent) { #if HAVE_MACH_EXCEPTIONS return pthrCurrent->EnableMachExceptions(); -#elif defined(__linux__) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(__sun) || defined(TARGET_TVOS) +#elif defined(__linux__) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(__sun) return NO_ERROR; #else// HAVE_MACH_EXCEPTIONS #error not yet implemented @@ -330,7 +330,7 @@ PAL_ERROR SEHDisable(CPalThread *pthrCurrent) { #if HAVE_MACH_EXCEPTIONS return pthrCurrent->DisableMachExceptions(); -#elif defined(__linux__) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(__sun) || defined(TARGET_TVOS) +#elif defined(__linux__) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(__sun) return NO_ERROR; #else // HAVE_MACH_EXCEPTIONS #error not yet implemented diff --git a/src/coreclr/pal/src/exception/signal.cpp b/src/coreclr/pal/src/exception/signal.cpp index 2b4e3b0f3a567d..c9112432f8d930 100644 --- a/src/coreclr/pal/src/exception/signal.cpp +++ b/src/coreclr/pal/src/exception/signal.cpp @@ -182,7 +182,7 @@ BOOL SEHInitializeSignals(CorUnix::CPalThread *pthrCurrent, DWORD flags) handle_signal(SIGINT, sigint_handler, &g_previous_sigint, 0 /* additionalFlags */, true /* skipIgnored */); handle_signal(SIGQUIT, sigquit_handler, &g_previous_sigquit, 0 /* additionalFlags */, true /* skipIgnored */); -#if HAVE_MACH_EXCEPTIONS || defined(TARGET_TVOS) +#if HAVE_MACH_EXCEPTIONS handle_signal(SIGSEGV, sigsegv_handler, &g_previous_sigsegv); #else handle_signal(SIGTRAP, sigtrap_handler, &g_previous_sigtrap); @@ -471,15 +471,7 @@ static void sigfpe_handler(int code, siginfo_t *siginfo, void *context) invoke_previous_action(&g_previous_sigfpe, code, siginfo, context); } -#if defined(TARGET_TVOS) - -static bool SwitchStackAndExecuteHandler(int code, siginfo_t *siginfo, void *context, size_t sp) -{ - // Not yet implemented - _exit(0xdead); -} - -#elif !HAVE_MACH_EXCEPTIONS +#if !HAVE_MACH_EXCEPTIONS /*++ Function : diff --git a/src/coreclr/pal/src/include/pal/thread.hpp b/src/coreclr/pal/src/include/pal/thread.hpp index f0788a3f18e6d4..411040f39b21a5 100644 --- a/src/coreclr/pal/src/include/pal/thread.hpp +++ b/src/coreclr/pal/src/include/pal/thread.hpp @@ -588,7 +588,7 @@ namespace CorUnix m_pNext = pNext; }; -#if !HAVE_MACH_EXCEPTIONS && !defined(TARGET_TVOS) +#if !HAVE_MACH_EXCEPTIONS BOOL EnsureSignalAlternateStack( void diff --git a/src/coreclr/pal/src/init/sxs.cpp b/src/coreclr/pal/src/init/sxs.cpp index 6b6d726f54818d..ddb084098efa51 100644 --- a/src/coreclr/pal/src/init/sxs.cpp +++ b/src/coreclr/pal/src/init/sxs.cpp @@ -64,7 +64,7 @@ AllocatePalThread(CPalThread **ppThread) goto exit; } -#if !HAVE_MACH_EXCEPTIONS && !defined(TARGET_TVOS) +#if !HAVE_MACH_EXCEPTIONS // Ensure alternate stack for SIGSEGV handling. Our SIGSEGV handler is set to // run on an alternate stack and the stack needs to be allocated per thread. if (!pThread->EnsureSignalAlternateStack()) diff --git a/src/coreclr/pal/src/thread/process.cpp b/src/coreclr/pal/src/thread/process.cpp index c919b08cdc8f2b..c61dce490f2121 100644 --- a/src/coreclr/pal/src/thread/process.cpp +++ b/src/coreclr/pal/src/thread/process.cpp @@ -545,9 +545,6 @@ CorUnix::InternalCreateProcess( LPPROCESS_INFORMATION lpProcessInformation ) { -#ifdef TARGET_TVOS - return ERROR_NOT_SUPPORTED; -#else PAL_ERROR palError = NO_ERROR; IPalObject *pobjProcess = NULL; IPalObject *pobjProcessRegistered = NULL; @@ -1084,7 +1081,6 @@ CorUnix::InternalCreateProcess( } return palError; -#endif // !TARGET_TVOS } @@ -2199,7 +2195,7 @@ PROCCreateCrashDump( INT cbErrorMessageBuffer, bool serialize) { -#if defined(TARGET_IOS) || defined(TARGET_TVOS) +#if defined(TARGET_IOS) return FALSE; #else _ASSERTE(argv.size() > 0); @@ -2326,7 +2322,7 @@ PROCCreateCrashDump( } } return true; -#endif // !TARGET_IOS && !TARGET_TVOS +#endif // !TARGET_IOS } /*++ diff --git a/src/coreclr/pal/src/thread/thread.cpp b/src/coreclr/pal/src/thread/thread.cpp index be76691c36a6b5..8c66057a3b5d40 100644 --- a/src/coreclr/pal/src/thread/thread.cpp +++ b/src/coreclr/pal/src/thread/thread.cpp @@ -173,7 +173,7 @@ static void InternalEndCurrentThreadWrapper(void *arg) will lock its own critical section */ LOADCallDllMain(DLL_THREAD_DETACH, NULL); -#if !HAVE_MACH_EXCEPTIONS && !defined(TARGET_TVOS) +#if !HAVE_MACH_EXCEPTIONS pThread->FreeSignalAlternateStack(); #endif // !HAVE_MACH_EXCEPTIONS @@ -1683,7 +1683,7 @@ CPalThread::ThreadEntry( } #endif // HAVE_SCHED_GETAFFINITY && HAVE_SCHED_SETAFFINITY -#if !HAVE_MACH_EXCEPTIONS && !defined(TARGET_TVOS) +#if !HAVE_MACH_EXCEPTIONS if (!pThread->EnsureSignalAlternateStack()) { ASSERT("Cannot allocate alternate stack for SIGSEGV!\n"); @@ -2406,7 +2406,7 @@ CPalThread::WaitForStartStatus( return m_fStartStatus; } -#if !HAVE_MACH_EXCEPTIONS && !defined(TARGET_TVOS) +#if !HAVE_MACH_EXCEPTIONS /*++ Function : EnsureSignalAlternateStack