Skip to content

Commit

Permalink
Backport macOS fix and re-enable library launchers
Browse files Browse the repository at this point in the history
  • Loading branch information
fniephaus committed Jul 24, 2022
1 parent a8ad1fa commit 3b717b9
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 7 deletions.
19 changes: 14 additions & 5 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand All @@ -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

Expand All @@ -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
Expand All @@ -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 }}
Expand Down
112 changes: 112 additions & 0 deletions mx.trufflesqueak/liblauncher_macos_backport.patch
Original file line number Diff line number Diff line change
@@ -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 <Cocoa/Cocoa.h>
+ #include <objc/objc-runtime.h>
+ #include <objc/objc-auto.h>
+
#elif defined (_WIN32)
#include <windows.h>
#include <libloaderapi.h>
@@ -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;
4 changes: 2 additions & 2 deletions mx.trufflesqueak/mx_trufflesqueak.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand All @@ -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,
Expand Down
2 changes: 2 additions & 0 deletions mx.trufflesqueak/utils.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down

0 comments on commit 3b717b9

Please sign in to comment.