From 3b717b918f566e34759ccc819ae97d8e93cb6c94 Mon Sep 17 00:00:00 2001 From: Fabio Niephaus Date: Fri, 22 Jul 2022 17:48:06 +0200 Subject: [PATCH] Backport macOS fix and re-enable library launchers --- .github/workflows/ci.yml | 19 ++- .../liblauncher_macos_backport.patch | 112 ++++++++++++++++++ mx.trufflesqueak/mx_trufflesqueak.py | 4 +- mx.trufflesqueak/utils.sh | 2 + 4 files changed, 130 insertions(+), 7 deletions(-) create mode 100644 mx.trufflesqueak/liblauncher_macos_backport.patch diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index df29bfb6c..974b742d9 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -20,15 +20,16 @@ jobs: style_and_build_config: name: Code Style + Build Config timeout-minutes: 15 - runs-on: ubuntu-18.04 - env: - MX_PYTHON_VERSION: 3 + runs-on: ubuntu-20.04 steps: - name: Clone TruffleSqueak repository uses: actions/checkout@v2 + - name: Set up Python + uses: actions/setup-python@v2 + with: + python-version: '3.8' - name: Set up dependencies run: | - sudo pip install --quiet astroid==1.1.0 pylint==1.1.0 # Download Eclipse SDK eclipse_tar_path="${RUNNER_TEMP}/eclipse.tar.gz" wget --no-verbose https://archive.eclipse.org/eclipse/downloads/drops4/R-4.14-201912100610/eclipse-SDK-4.14-linux-gtk-x86_64.tar.gz -O ${eclipse_tar_path} @@ -37,6 +38,11 @@ jobs: echo "JDT=builtin" >> "${GITHUB_ENV}" # required by mx # Set up mx, oracle/graal, and LabsJDK11 mx.trufflesqueak/utils.sh "set-up-mx && shallow-clone-graal && set-up-labsjdk labsjdk-ce-11 ~/" + # Set up style dependencies + sudo apt update && sudo apt install python3-pip python-setuptools + cat ../graal/common.json | + jq -r '.deps.common.packages | to_entries[] | select(.key | startswith("pip:")) | (.key | split(":")[1]) + .value' | + xargs sudo pip install - name: Check style and perform full build run: mx gate --strict-mode --tags style,fullbuild @@ -52,7 +58,6 @@ jobs: TRUFFLESQUEAK_EXEC: "${{ matrix.os == 'windows-2019' && 'trufflesqueak.cmd' || 'trufflesqueak' }}" TS_INFIX: "${{ matrix.java == 11 && '_SVM' || '' }}" MX_ENV: "${{ matrix.java == 11 && 'trufflesqueak-svm' || 'trufflesqueak-jvm' }}" - MX_PYTHON_VERSION: 3 VERBOSE_GRAALVM_LAUNCHERS: true name: ${{ matrix.os }} + JDK${{ matrix.java }} timeout-minutes: 60 @@ -62,6 +67,10 @@ jobs: uses: actions/checkout@v2 with: submodules: true + - name: Set up Python + uses: actions/setup-python@v2 + with: + python-version: '3.8' - name: Set up dependencies shell: bash run: mx.trufflesqueak/utils.sh set-up-dependencies java${{ matrix.java }} diff --git a/mx.trufflesqueak/liblauncher_macos_backport.patch b/mx.trufflesqueak/liblauncher_macos_backport.patch new file mode 100644 index 000000000..915e7394d --- /dev/null +++ b/mx.trufflesqueak/liblauncher_macos_backport.patch @@ -0,0 +1,112 @@ +diff --git a/sdk/mx.sdk/mx_sdk_vm_impl.py b/sdk/mx.sdk/mx_sdk_vm_impl.py +index 7fd5bdbdc8e..3e525e11613 100644 +--- a/sdk/mx.sdk/mx_sdk_vm_impl.py ++++ b/sdk/mx.sdk/mx_sdk_vm_impl.py +@@ -2742,6 +2742,8 @@ class NativeLibraryLauncherProject(mx_native.DefaultNativeProject): + ] + if not mx.is_windows(): + _dynamic_cflags += ['-pthread'] ++ if mx.is_darwin(): ++ _dynamic_cflags += ['-ObjC++'] + + _graalvm_home = _get_graalvm_archive_path("") + +@@ -2807,6 +2809,8 @@ class NativeLibraryLauncherProject(mx_native.DefaultNativeProject): + _dynamic_ldlibs = [] + if not mx.is_windows(): + _dynamic_ldlibs += ['-ldl'] ++ if mx.is_darwin(): ++ _dynamic_ldlibs += ['-framework', 'Foundation'] + return super(NativeLibraryLauncherProject, self).ldlibs + _dynamic_ldlibs + + def default_language_home_relative_libpath(self): +diff --git a/sdk/src/org.graalvm.launcher.native/src/launcher.cc b/sdk/src/org.graalvm.launcher.native/src/launcher.cc +index 4f9c7450682..94d05906aa9 100644 +--- a/sdk/src/org.graalvm.launcher.native/src/launcher.cc ++++ b/sdk/src/org.graalvm.launcher.native/src/launcher.cc +@@ -115,6 +115,11 @@ + #endif + #define LIBJLI_RELPATH_STR STR(LIBJLI_RELPATH) + ++ /* Support Cocoa event loop on the main thread */ ++ #include ++ #include ++ #include ++ + #elif defined (_WIN32) + #include + #include +@@ -376,6 +381,41 @@ void parse_vm_options(int argc, char **argv, std::string exeDir, JavaVMInitArgs + } + } + ++static int jvm_main_thread(int argc, char *argv[], std::string exeDir, char *jvmModeEnv, bool jvmMode, std::string libPath); ++ ++#if defined (__APPLE__) ++static void dummyTimer(CFRunLoopTimerRef timer, void *info) {} ++ ++static void ParkEventLoop() { ++ // RunLoop needs at least one source, and 1e20 is pretty far into the future ++ CFRunLoopTimerRef t = CFRunLoopTimerCreate(kCFAllocatorDefault, 1.0e20, 0.0, 0, 0, dummyTimer, NULL); ++ CFRunLoopAddTimer(CFRunLoopGetCurrent(), t, kCFRunLoopDefaultMode); ++ CFRelease(t); ++ ++ // Park this thread in the main run loop. ++ int32_t result; ++ do { ++ result = CFRunLoopRunInMode(kCFRunLoopDefaultMode, 1.0e20, false); ++ } while (result != kCFRunLoopRunFinished); ++} ++ ++struct MainThreadArgs { ++ int argc; ++ char **argv; ++ std::string exeDir; ++ char *jvmModeEnv; ++ bool jvmMode; ++ std::string libPath; ++}; ++ ++static void *apple_main (void *arg) ++{ ++ struct MainThreadArgs *args = (struct MainThreadArgs *) arg; ++ int ret = jvm_main_thread(args->argc, args->argv, args->exeDir, args->jvmModeEnv, args->jvmMode, args->libPath); ++ exit(ret); ++} ++#endif /* __APPLE__ */ ++ + int main(int argc, char *argv[]) { + debug = (getenv("VERBOSE_GRAALVM_LAUNCHERS") != NULL); + std::string exeDir = exe_directory(); +@@ -400,8 +440,31 @@ int main(int argc, char *argv[]) { + return -1; + } + } ++ ++ struct MainThreadArgs args = { argc, argv, exeDir, jvmModeEnv, jvmMode, libPath}; ++ ++ /* Create dedicated "main" thread for the JVM. The actual main thread ++ * must run the UI event loop on macOS. Inspired by this OpenJDK code: ++ * https://github.com/openjdk/jdk/blob/011958d30b275f0f6a2de097938ceeb34beb314d/src/java.base/macosx/native/libjli/java_md_macosx.m#L328-L358 ++ */ ++ pthread_t main_thr; ++ if (pthread_create(&main_thr, NULL, &apple_main, &args) != 0) { ++ std::cerr << "Could not create main thread: " << strerror(errno) << std::endl; ++ return -1; ++ } ++ if (pthread_detach(main_thr)) { ++ std::cerr << "pthread_detach() failed: " << strerror(errno) << std::endl; ++ return -1; ++ } ++ ++ ParkEventLoop(); ++ return 0; ++#else ++ return jvm_main_thread(argc, argv, exeDir, jvmModeEnv, jvmMode, libPath); + #endif ++} + ++static int jvm_main_thread(int argc, char *argv[], std::string exeDir, char *jvmModeEnv, bool jvmMode, std::string libPath) { + /* parse VM args */ + JavaVM *vm; + JNIEnv *env; diff --git a/mx.trufflesqueak/mx_trufflesqueak.py b/mx.trufflesqueak/mx_trufflesqueak.py index e3baa1750..8fd477414 100644 --- a/mx.trufflesqueak/mx_trufflesqueak.py +++ b/mx.trufflesqueak/mx_trufflesqueak.py @@ -182,7 +182,7 @@ def patched_init(self, *args, **kw_args): mx_sdk_vm.register_vm_config('trufflesqueak', ['nfi', 'nfi-libffi', 'sdk', 'st', 'tfl'], _SUITE, env_file='trufflesqueak-jvm') -mx_sdk_vm.register_vm_config('trufflesqueak-svm', ['cmp', 'nfi', 'nfi-libffi', 'sdk', 'st', 'svm', 'svmnfi', 'tfl', 'tflm'], +mx_sdk_vm.register_vm_config('trufflesqueak-svm', ['cmp', 'nfi', 'nfi-libffi', 'sdk', 'st', 'svm', 'svmnfi', 'svmsl', 'tfl', 'tflm'], _SUITE, env_file='trufflesqueak-svm') SVM_BUILD_ARGS = [ @@ -191,7 +191,7 @@ def patched_init(self, *args, **kw_args): '-H:+DetectUserDirectoriesInImageHeap', '-H:+TruffleCheckBlockListMethods', ] -USE_LIBRARY_LAUNCHERS = False +USE_LIBRARY_LAUNCHERS = True mx_sdk.register_graalvm_component(mx_sdk.GraalVmLanguage( suite=_SUITE, diff --git a/mx.trufflesqueak/utils.sh b/mx.trufflesqueak/utils.sh index 409613550..d42c2f6f7 100755 --- a/mx.trufflesqueak/utils.sh +++ b/mx.trufflesqueak/utils.sh @@ -280,6 +280,8 @@ shallow-clone-graalvm-project() { shallow-clone-graal() { shallow-clone-graalvm-project https://github.com/oracle/graal.git "${GRAAL_VERSION}" echo "[graal repo (${GRAAL_VERSION}) cloned successfully]" + $(cd ${BASE_DIRECTORY}/../graal && git apply "${SCRIPT_DIRECTORY}/liblauncher_macos_backport.patch") + echo "[graal repo patched successfully]" } shallow-clone-graaljs() {