Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable mono cross-build on SunOS-like OS #37560

Merged
merged 1 commit into from
Jun 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/mono/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
<!-- Set up common target properties that we use to conditionally include sources -->
<PropertyGroup>
<TargetsFreeBSD Condition="'$(TargetOS)' == 'FreeBSD'">true</TargetsFreeBSD>
<TargetsIllumos Condition="'$(TargetOS)' == 'illumos'">true</TargetsIllumos>
<TargetsSolaris Condition="'$(TargetOS)' == 'Solaris'">true</TargetsSolaris>
<TargetsLinux Condition="'$(TargetOS)' == 'Linux' or '$(TargetOS)' == 'Android'">true</TargetsLinux>
<TargetsNetBSD Condition="'$(TargetOS)' == 'NetBSD'">true</TargetsNetBSD>
<TargetsOSX Condition="'$(TargetOS)' == 'OSX'">true</TargetsOSX>
Expand All @@ -44,7 +46,7 @@
<TargetsAndroid Condition="'$(TargetOS)' == 'Android'">true</TargetsAndroid>
<TargetsBrowser Condition="'$(TargetOS)' == 'Browser'">true</TargetsBrowser>
<TargetsWindows Condition="'$(TargetOS)' == 'Windows_NT'">true</TargetsWindows>
<TargetsUnix Condition="'$(TargetsFreeBSD)' == 'true' or '$(TargetsLinux)' == 'true' or '$(TargetsNetBSD)' == 'true' or '$(TargetsOSX)' == 'true' or '$(TargetstvOS)' == 'true' or '$(TargetsiOS)' == 'true' or '$(TargetsAndroid)' == 'true' or '$(TargetsBrowser)' == 'true'">true</TargetsUnix>
<TargetsUnix Condition="'$(TargetsFreeBSD)' == 'true' or '$(TargetsIllumos)' == 'true' or '$(TargetsSolaris)' == 'true' or '$(TargetsLinux)' == 'true' or '$(TargetsNetBSD)' == 'true' or '$(TargetsOSX)' == 'true' or '$(TargetstvOS)' == 'true' or '$(TargetsiOS)' == 'true' or '$(TargetsAndroid)' == 'true' or '$(TargetsBrowser)' == 'true'">true</TargetsUnix>
</PropertyGroup>

<PropertyGroup>
Expand Down
34 changes: 25 additions & 9 deletions src/mono/configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ platform_ios=no
host_darwin=no
host_linux=no
build_darwin=no
host_sunos=no

case "$host" in
wasm32*)
Expand Down Expand Up @@ -397,13 +398,14 @@ case "$host" in
use_sigposix=yes
;;
*-*-solaris*)
CPPFLAGS="$CPPFLAGS -DGC_SOLARIS_THREADS -DGC_SOLARIS_PTHREADS -D_REENTRANT -D_POSIX_PTHREAD_SEMANTICS -DUSE_MMAP -DUSE_MUNMAP -DHOST_SOLARIS"
CPPFLAGS="$CPPFLAGS -DGC_SOLARIS_THREADS -DGC_SOLARIS_PTHREADS -D_REENTRANT -D_POSIX_PTHREAD_SEMANTICS -DUSE_MMAP -DUSE_MUNMAP -DHOST_SOLARIS -D__EXTENSIONS__ -D_XPG4_2"
need_link_unlink=yes
libmono_cflags="-D_REENTRANT"
libgc_threads=pthreads
has_dtrace=yes
use_sigposix=yes
enable_solaris_tar_check=yes
host_sunos=yes
;;
*-*-darwin*)
parallel_mark="Disabled_Currently_Hangs_On_MacOSX"
Expand Down Expand Up @@ -2274,14 +2276,13 @@ if test x$host_win32 = xno; then
AC_CHECK_FUNCS(getresuid)
AC_CHECK_FUNCS(setresuid)
AC_CHECK_FUNCS(kqueue)
# IBM provides a compatibility library for i offering this function.
# IBM provides a compatibility library for offering this function.
# BSDs and others, have execinfo in base or packages.
AC_SEARCH_LIBS(backtrace_symbols, execinfo util)
# Two-step so it sets it in config.h
AC_CHECK_FUNCS(backtrace_symbols)
AC_CHECK_FUNCS(mkstemp)
AC_CHECK_FUNCS(mmap)
AC_CHECK_FUNCS(madvise)
AC_CHECK_FUNCS(getrusage)
AC_CHECK_FUNCS(getpriority)
AC_CHECK_FUNCS(setpriority)
Expand Down Expand Up @@ -2747,7 +2748,7 @@ if test x$host_win32 = xno; then
dnl *** Checks for libxnet ***
dnl *****************************
case "${host}" in
*solaris* )
*solaris*)
AC_MSG_CHECKING(for Solaris XPG4 support)
if test -f /usr/lib/libxnet.so; then
CPPFLAGS="$CPPFLAGS -D_XOPEN_SOURCE=500"
Expand Down Expand Up @@ -3215,7 +3216,6 @@ if test x$host_win32 = xno; then
AC_CHECK_FUNCS(remap_file_pages)
AC_CHECK_FUNCS(posix_fadvise)
AC_CHECK_FUNCS(posix_fallocate)
AC_CHECK_FUNCS(posix_madvise)
AC_CHECK_FUNCS(vsnprintf)
AC_CHECK_FUNCS(sendfile)
AC_CHECK_FUNCS(gethostid sethostid)
Expand Down Expand Up @@ -3921,6 +3921,19 @@ if test x$host_win32 = xno; then

AC_CHECK_DECL(IN_EXCL_UNLINK, [AC_DEFINE(HAVE_IN_EXCL_UNLINK, 1, [IN_EXCL_UNLINK])], [], [[#include <sys/inotify.h>]])

if test x$host_sunos = xyes; then
# set -Werror=strict-prototypes, to match the flags used during the compilation.
ORIG_CFLAGS="$CFLAGS"
CFLAGS="$CFLAGS -Werror=strict-prototypes"
fi

AC_CHECK_FUNCS(madvise)
AC_CHECK_FUNCS(posix_madvise)
am11 marked this conversation as resolved.
Show resolved Hide resolved

if test x$host_sunos = xyes; then
CFLAGS="$ORIG_CFLAGS"
fi

# *** End of Mono.Native checks ***
else
AM_CONDITIONAL(ENABLE_GSS, false)
Expand Down Expand Up @@ -6836,9 +6849,9 @@ if test x$with_core = xonly; then
ICU_CFLAGS="$CPPFLAGS -DPALEXPORT="" -DOSX_ICU_LIBRARY_PATH=AS_ESCAPE(\"/usr/lib/libicucore.dylib\", '\"') -DTARGET_UNIX -DU_DISABLE_RENAMING -Wno-reserved-id-macro -Wno-documentation -Wno-documentation-unknown-command -Wno-switch-enum -Wno-covered-switch-default -Wno-covered-switch-default -Wno-extra-semi-stmt -Wno-unknown-warning-option -Wno-deprecated-declarations"
fi
CPPFLAGS=$ORIG_CPPFLAGS
elif test x$platform_android = xyes; then
elif test x$platform_android = xyes; then
ICU_CFLAGS="-DPALEXPORT="" -DHAVE_UDAT_STANDALONE_SHORTER_WEEKDAYS -DHAVE_SET_MAX_VARIABLE -DTARGET_UNIX -DTARGET_ANDROID -Wno-reserved-id-macro -Wno-documentation -Wno-documentation-unknown-command -Wno-switch-enum -Wno-covered-switch-default -Wno-covered-switch-default -Wno-extra-semi-stmt -Wno-unknown-warning-option"
have_sys_icu=yes
have_sys_icu=yes
elif test x$host_linux = xyes; then
AC_CHECK_LIB(icuuc, main, LIBS=$LIBS,
[AC_MSG_ERROR([Cannot find libicuuc, try installing libicu-dev (or the appropriate package for your platform).])])
Expand All @@ -6848,14 +6861,17 @@ if test x$with_core = xonly; then
if test x$have_sys_icu = xyes; then
ICU_CFLAGS="-DPALEXPORT="" -DTARGET_UNIX -Wno-reserved-id-macro -Wno-documentation -Wno-documentation-unknown-command -Wno-switch-enum -Wno-covered-switch-default -Wno-covered-switch-default -Wno-extra-semi-stmt -Wno-unknown-warning-option"
fi
elif test x$host_sunos = xyes; then
ICU_CFLAGS="-DPALEXPORT="" -DTARGET_UNIX -Wno-reserved-id-macro -Wno-documentation -Wno-documentation-unknown-command -Wno-switch-enum -Wno-covered-switch-default -Wno-extra-semi-stmt -Wno-unknown-warning-option"
have_sys_icu=yes
else
GLOBALIZATION_SHIM_DEFINES="-DNO_GLOBALIZATION_SHIM"
fi
AC_SUBST(ICU_CFLAGS)
else
else
GLOBALIZATION_SHIM_DEFINES="-DNO_GLOBALIZATION_SHIM"
fi
else
else
GLOBALIZATION_SHIM_DEFINES="-DNO_GLOBALIZATION_SHIM"
fi

Expand Down
35 changes: 34 additions & 1 deletion src/mono/mono.proj
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@
<BuildMonoAOTCrossCompiler Condition="'$(TargetsBrowser)' == 'true'">true</BuildMonoAOTCrossCompiler>
<CrossConfigH Condition="'$(BuildMonoAOTCrossCompiler)' == 'true'">$(MonoObjDir)cross\config.h</CrossConfigH>
<MonoBundleLLVMOptimizer Condition="'$(MonoEnableLLVM)' == 'true'">true</MonoBundleLLVMOptimizer>
<MonoCCompiler>$(Compiler)</MonoCCompiler>
<MonoCCompiler Condition="'$(MonoCCompiler)' == ''">clang</MonoCCompiler>
<!-- when we use runtime/build.sh -gcc, it is passed to eng/build.sh which sets the raw value to $(Compiler); strip the leading hyphens. -->
<MonoCCompiler Condition="$(MonoCCompiler.StartsWith('-'))">$(MonoCCompiler.TrimStart('-'))</MonoCCompiler>
am11 marked this conversation as resolved.
Show resolved Hide resolved
<MonoCxxCompiler Condition="'$(MonoCCompiler)' == 'clang'">clang++</MonoCxxCompiler>
<MonoCxxCompiler Condition="'$(MonoCCompiler)' == 'gcc'">g++</MonoCxxCompiler>
</PropertyGroup>

<!-- OSX/iOS/tvOS/Android/Linux Mono runtime build -->
Expand Down Expand Up @@ -580,6 +586,33 @@
<_MonoSTRIPOption>STRIP="$(_MonoTuple)-strip"</_MonoSTRIPOption>
</PropertyGroup>

<!-- x64 illumos cross build options -->
<PropertyGroup Condition="'$(TargetsIllumos)' == 'true' and '$(MonoCrossDir)' != ''">
<_MonoTuple>x86_64-sun-solaris2.10</_MonoTuple>
<_ToolPrefix>$(MonoCrossDir)/bin/x86_64-illumos</_ToolPrefix>

<_MonoRANLIBOption>RANLIB="$(_ToolPrefix)-ranlib"</_MonoRANLIBOption>
<_MonoAROption>AR="$(_ToolPrefix)-ar"</_MonoAROption>
<_MonoASOption>AS="$(_ToolPrefix)-as"</_MonoASOption>
<_MonoLDOption>LD="$(_ToolPrefix)-ld"</_MonoLDOption>
<_MonoSTRIPOption>STRIP="$(_ToolPrefix)-strip"</_MonoSTRIPOption>

<_MonoCCOption>CC="$(_ToolPrefix)-$(MonoCCompiler)"</_MonoCCOption>
<_MonoCXXOption>CXX="$(_ToolPrefix)-$(MonoCxxCompiler)"</_MonoCXXOption>
</PropertyGroup>

<ItemGroup Condition="'$(TargetsIllumos)' == 'true' and '$(MonoCrossDir)' != ''">
<_MonoConfigureParams Include="--host=$(_MonoTuple)" />
<_MonoConfigureParams Include="--target=$(_MonoTuple)" />

<_MonoCFLAGS Include="--sysroot=$(MonoCrossDir)" />
<_MonoCFLAGS Include="-I$(MonoCrossDir)/include" />
<_MonoCXXFLAGS Include="--sysroot=$(MonoCrossDir)" />
<_MonoLDFLAGS Include="--sysroot=$(MonoCrossDir)" />
<_MonoCCLDFLAGS Include="-XCClinker" />
<_MonoConfigureParams Include="--with-tls=pthread" />
</ItemGroup>

<Error Condition="'$(TargetsBrowser)' == 'true' and '$(EMSDK_PATH)' == ''" Text="The EMSDK_PATH environment variable should be set pointing to the emscripten SDK root dir."/>

<!-- WASM specific options -->
Expand Down Expand Up @@ -675,7 +708,7 @@
<_MonoCCLDFLAGSOption Condition="@(_MonoCCLDFLAGS->Count()) &gt; 0">CCLDFLAGS="@(_MonoCCLDFLAGS, ' ')"</_MonoCCLDFLAGSOption>

<_MonoConfigureOptions>@(_MonoConfigureParams, ' ') @(_MonoAC_VARS, ' ') $(_MonoCFLAGSOption) $(_MonoCXXFLAGSOption) $(_MonoCPPFLAGSOption) $(_MonoCXXCPPFLAGSOption) $(_MonoLDFLAGSOption) $(_MonoCCLDFLAGSOption) $(_MonoCCOption) $(_MonoCXXOption) $(_MonoAROption) $(_MonoASOption) $(_MonoCPPOption) $(_MonoCXXCPPOption) $(_MonoDLLTOOLOption) $(_MonoLDOption) $(_MonoOBJDUMPOption) $(_MonoRANLIBOption) $(_MonoCMAKEOption) $(_MonoSTRIPOption)</_MonoConfigureOptions>
<_MonoConfigureCommand Condition="'$(_MonoCCOption)' == '' and '$(_MonoCXXOption)' == ''">bash -c 'source $(RepositoryEngineeringDir)native/init-compiler.sh $(Platform) clang &amp;&amp; $(MonoProjectRoot)configure $(_MonoConfigureOptions)'</_MonoConfigureCommand>
<_MonoConfigureCommand Condition="'$(_MonoCCOption)' == '' and '$(_MonoCXXOption)' == ''">bash -c 'source $(RepositoryEngineeringDir)native/init-compiler.sh $(Platform) $(MonoCCompiler) &amp;&amp; $(MonoProjectRoot)configure $(_MonoConfigureOptions)'</_MonoConfigureCommand>
<_MonoConfigureCommand Condition="'$(_MonoCCOption)' != '' and '$(_MonoCXXOption)' != ''">$(MonoProjectRoot)configure $(_MonoConfigureOptions)</_MonoConfigureCommand>
<_MonoConfigureCommand Condition="'$(TargetsBrowser)' == 'true'">bash -c 'source $(EMSDK_PATH)/emsdk_env.sh &amp;&amp; emconfigure $(MonoProjectRoot)configure $(_MonoConfigureOptions)'</_MonoConfigureCommand>
</PropertyGroup>
Expand Down
9 changes: 9 additions & 0 deletions src/mono/mono/metadata/threads.c
Original file line number Diff line number Diff line change
Expand Up @@ -805,6 +805,15 @@ mono_thread_internal_set_priority (MonoInternalThread *internal, MonoThreadPrior
break;
#ifdef SCHED_BATCH
case SCHED_BATCH:
#endif
#ifdef SCHED_IA
case SCHED_IA:
#endif
#ifdef SCHED_FSS
case SCHED_FSS:
#endif
#ifdef SCHED_FX
case SCHED_FX:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are these all unique to illumos? Maybe leave a comment?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

illumos and Solaris at least. Is there a way to tell for sure which platform support which flag? The existing #ifdef SCHED_BATCH is also without a comment, I guess because it is self-explanatory feature detection.

#endif
case SCHED_OTHER:
param.sched_priority = 0;
Expand Down
2 changes: 1 addition & 1 deletion src/mono/mono/mini/mini-amd64.h
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,7 @@ typedef struct {

#endif /* !HOST_WIN32 */

#if !defined(__linux__)
#if !defined(__linux__) && !defined(__sun)
#define MONO_ARCH_NOMAP32BIT 1
#endif

Expand Down
1 change: 1 addition & 0 deletions src/mono/mono/utils/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ monoutils_sources = \
mono-threads-haiku.c \
mono-threads-aix.c \
mono-threads-wasm.c \
mono-threads-sunos.c \
mono-threads.h \
mono-threads-debug.h \
mono-threads-api.h \
Expand Down
54 changes: 54 additions & 0 deletions src/mono/mono/utils/mono-threads-sunos.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/**
* \file
*/

#include <config.h>

#if defined(__sun__)

#include <mono/utils/mono-threads.h>
#include <pthread.h>
#include <sys/syscall.h>

void
mono_threads_platform_get_stack_bounds (guint8 **staddr, size_t *stsize)
{
pthread_attr_t attr;
gint res;

*staddr = NULL;
*stsize = (size_t)-1;

res = pthread_attr_init (&attr);
if (G_UNLIKELY (res != 0))
g_error ("%s: pthread_attr_init failed with \"%s\" (%d)", __func__, g_strerror (res), res);

res = pthread_attr_get_np (pthread_self (), &attr);
if (G_UNLIKELY (res != 0))
g_error ("%s: pthread_getattr_np failed with \"%s\" (%d)", __func__, g_strerror (res), res);

res = pthread_attr_getstack (&attr, (void**)staddr, stsize);
if (G_UNLIKELY (res != 0))
g_error ("%s: pthread_attr_getstack failed with \"%s\" (%d)", __func__, g_strerror (res), res);

res = pthread_attr_destroy (&attr);
if (G_UNLIKELY (res != 0))
g_error ("%s: pthread_attr_destroy failed with \"%s\" (%d)", __func__, g_strerror (res), res);

}

guint64
mono_native_thread_os_id_get (void)
{
// TODO: investigate whether to use light weight process lwp ID of keep pthread_self()
// after the libraries and SDK ports are completed on illumos and/or Solaris platform.
return (guint64)pthread_self ();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function is intended to return the actual TID, a pid_t, not the results of pthread_self (). This needs to be the sunos gettid syscall equivalent.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup and this is what I have found as an equivalent. Also, saw the same thing happening in coreclr against gettid unavailability on SunOS:

#else
#define PlatformGetCurrentThreadId() (SIZE_T)pthread_self()
#endif

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah I see. And I guess the value from thr_self () is no better than the POSIX thread ID... What a mess. Can you leave a comment so it's clear this isn't just a mistake? Otherwise I suspect I'll see this code 6 months down the line and, having forgotten about this conversation, look into it all over again. 😄

Copy link
Member Author

@am11 am11 Jun 17, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@CoffeeFlux, I understand your position. and tbh, I am also not 100% sure whether to use light weight process (lwp) ID or pthread_self in this case as we are doing different things with this returned value at the call site (some places are using pthread library, others are making syscalls etc.). As far as I have read, POSIX does not provide strict specifications for process/thread related IDs and their relation with platform native threads, hence the disparity across all platforms and makes it difficult for devs to figure out the exact semantics (which we can see in all mono-threads-{platform}.c files). I only made sure that the hello world is working without violating any assertion i.e. changes in threads.c were the result of assertion violation on run time.

It is not exactly straight-forward to port dotnet/runtime to a new Unix platform, and test it all in one go. The eng side is quite a work to pull and lots of (ever changing) moving parts to learn. That's the reason why I am currently pushing changes to get this semi-ready state checked, to be able to get SDK to work, and then port libraries and run tests on the host Sun-like platforms.

Added a TODO comment to that effect. So far, this is just-working. We will get more information, when we will be able to execute related libraries tests to exercise this code thoroughly, and it might get changed to lwpId, something else or not.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me, and thanks for the detailed response. I'm happy with a just-working state on this PR; I just want to make sure we're attempting to capture that context somewhere in the code instead of buried in a Github issue. Appreciate your efforts to get this working!

As far as I have read, POSIX does not provide strict specifications for process/thread related IDs and their relation with platform native threads, hence the disparity across all platforms

Yep. 😢

}

#else

#include <mono/utils/mono-compiler.h>

MONO_EMPTY_SOURCE_FILE (mono_threads_sunos);

#endif