Skip to content

Commit

Permalink
8333795: [mobile] allow building jdk native libs for ios/android
Browse files Browse the repository at this point in the history
Reviewed-by: ihse
  • Loading branch information
Johan Vos committed Jun 15, 2024
1 parent 51fb5b5 commit deb4256
Show file tree
Hide file tree
Showing 32 changed files with 137 additions and 22 deletions.
6 changes: 6 additions & 0 deletions make/autoconf/flags-cflags.m4
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,12 @@ AC_DEFUN([FLAGS_SETUP_CFLAGS_HELPER],
CFLAGS_OS_DEF_JDK="-D_ALLBSD_SOURCE"
elif test "x$OPENJDK_TARGET_OS" = xwindows; then
CFLAGS_OS_DEF_JVM="-D_WINDOWS -DWIN32 -D_JNI_IMPLEMENTATION_"
elif test "x$OPENJDK_TARGET_OS" = xandroid; then
CFLAGS_OS_DEF_JVM="-target aarch64-linux-android -DLINUX -D_ALLBSD_SOURCE -DANDROID"
CFLAGS_OS_DEF_JDK="-target aarch64-linux-android -DLINUX -D__USE_BSD"
elif test "x$OPENJDK_TARGET_OS" = xios; then
CFLAGS_OS_DEF_JVM="-DLINUX -D__IOS__ "
CFLAGS_OS_DEF_JDK="-D_ALLBSD_SOURCE -D__IOS__"
fi
CFLAGS_OS_DEF_JDK="$CFLAGS_OS_DEF_JDK -D$OPENJDK_TARGET_OS_UPPERCASE"
Expand Down
3 changes: 3 additions & 0 deletions make/autoconf/flags-other.m4
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,9 @@ AC_DEFUN([FLAGS_SETUP_ASFLAGS],
-DMAC_OS_X_VERSION_MAX_ALLOWED=$MACOSX_VERSION_MAX_NODOTS"
fi
fi
if test "x$OPENJDK_TARGET_OS" = xandroid; then
JVM_BASIC_ASFLAGS="-target aarch64-linux-android"
fi
])

################################################################################
Expand Down
2 changes: 2 additions & 0 deletions make/autoconf/lib-freetype.m4
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ AC_DEFUN_ONCE([LIB_SETUP_FREETYPE],
FREETYPE_TO_USE=bundled
if test "x$OPENJDK_TARGET_OS" != "xwindows" && \
test "x$OPENJDK_TARGET_OS" != "xmacosx" && \
test "x$OPENJDK_TARGET_OS" != "xandroid" && \
test "x$OPENJDK_TARGET_OS" != "xios" && \
test "x$OPENJDK_TARGET_OS" != "xaix"; then
FREETYPE_TO_USE=system
fi
Expand Down
4 changes: 2 additions & 2 deletions make/autoconf/libraries.m4
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ m4_include([lib-tests.m4])
AC_DEFUN_ONCE([LIB_DETERMINE_DEPENDENCIES],
[
# Check if X11 is needed
if test "x$OPENJDK_TARGET_OS" = xwindows || test "x$OPENJDK_TARGET_OS" = xmacosx; then
if test "x$OPENJDK_TARGET_OS" = xwindows || test "x$OPENJDK_TARGET_OS" = xmacosx || test "x$OPENJDK_TARGET_OS" = xandroid || test "x$OPENJDK_TARGET_OS" = xios; then
# No X11 support on windows or macosx
NEEDS_LIB_X11=false
else
Expand All @@ -52,7 +52,7 @@ AC_DEFUN_ONCE([LIB_DETERMINE_DEPENDENCIES],
fi
# Check if fontconfig is needed
if test "x$OPENJDK_TARGET_OS" = xwindows || test "x$OPENJDK_TARGET_OS" = xmacosx; then
if test "x$OPENJDK_TARGET_OS" = xwindows || test "x$OPENJDK_TARGET_OS" = xmacosx || test "x$OPENJDK_TARGET_OS" = xios; then
# No fontconfig support on windows or macosx
NEEDS_LIB_FONTCONFIG=false
else
Expand Down
8 changes: 8 additions & 0 deletions make/autoconf/platform.m4
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,14 @@ AC_DEFUN([PLATFORM_EXTRACT_VARS_FROM_CPU],
AC_DEFUN([PLATFORM_EXTRACT_VARS_FROM_OS],
[
case "$1" in
*android*)
VAR_OS=android
VAR_OS_TYPE=unix
;;
*ios*)
VAR_OS=ios
VAR_OS_TYPE=unix
;;
*linux*)
VAR_OS=linux
VAR_OS_TYPE=unix
Expand Down
12 changes: 7 additions & 5 deletions make/autoconf/toolchain.m4
Original file line number Diff line number Diff line change
Expand Up @@ -865,11 +865,13 @@ AC_DEFUN_ONCE([TOOLCHAIN_SETUP_BUILD_COMPILERS],
PATH="$OLDPATH"
TOOLCHAIN_EXTRACT_COMPILER_VERSION(BUILD_CC, [BuildC])
TOOLCHAIN_EXTRACT_COMPILER_VERSION(BUILD_CXX, [BuildC++])
TOOLCHAIN_PREPARE_FOR_VERSION_COMPARISONS([BUILD_], [OPENJDK_BUILD_], [build ])
TOOLCHAIN_EXTRACT_LD_VERSION(BUILD_LD, [build linker])
TOOLCHAIN_PREPARE_FOR_LD_VERSION_COMPARISONS([BUILD_], [OPENJDK_BUILD_])
if test "x$OPENJDK_TARGET_OS" != "xandroid"; then
TOOLCHAIN_EXTRACT_COMPILER_VERSION(BUILD_CC, [BuildC])
TOOLCHAIN_EXTRACT_COMPILER_VERSION(BUILD_CXX, [BuildC++])
TOOLCHAIN_PREPARE_FOR_VERSION_COMPARISONS([BUILD_], [OPENJDK_BUILD_], [build ])
TOOLCHAIN_EXTRACT_LD_VERSION(BUILD_LD, [build linker])
TOOLCHAIN_PREPARE_FOR_LD_VERSION_COMPARISONS([BUILD_], [OPENJDK_BUILD_])
fi
else
# If we are not cross compiling, use the normal target compilers for
# building the build platform executables.
Expand Down
12 changes: 12 additions & 0 deletions make/common/JdkNativeCompilation.gmk
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,18 @@ FindSrcDirsForComponent += \
$(TOPDIR)/src/$(strip $1)/$(OPENJDK_TARGET_OS_TYPE)/native/$(strip $2) \
$(TOPDIR)/src/$(strip $1)/share/native/$(strip $2)))

ifeq ($(OPENJDK_TARGET_OS), android)
FindSrcDirsForComponent += \
$(call uniq, $(wildcard \
$(TOPDIR)/src/$(strip $1)/linux/native/$(strip $2)))
endif

ifeq ($(OPENJDK_TARGET_OS), ios)
FindSrcDirsForComponent += \
$(call uniq, $(wildcard \
$(TOPDIR)/src/$(strip $1)/macosx/native/$(strip $2)))
endif

# Find a library. Used for declaring dependencies on libraries in different
# modules.
# Param 1 - module name
Expand Down
7 changes: 7 additions & 0 deletions make/common/Modules.gmk
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,13 @@ SRC_SUBDIRS += $(OPENJDK_TARGET_OS)/classes
ifneq ($(OPENJDK_TARGET_OS), $(OPENJDK_TARGET_OS_TYPE))
SRC_SUBDIRS += $(OPENJDK_TARGET_OS_TYPE)/classes
endif
ifeq ($(OPENJDK_TARGET_OS), android)
SRC_SUBDIRS += linux/classes
endif
ifeq ($(OPENJDK_TARGET_OS), ios)
SRC_SUBDIRS += macosx/classes
endif

SRC_SUBDIRS += share/classes

SPEC_SUBDIRS += share/specs
Expand Down
2 changes: 1 addition & 1 deletion make/common/native/Paths.gmk
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ define SetupObjectFileList

# If we are building static library, 'AR' on macosx/aix may not support @-file.
ifeq ($$($1_TYPE), STATIC_LIBRARY)
ifeq ($(call isTargetOs, macosx aix), true)
ifeq ($(call isTargetOs, macosx ios aix), true)
$1_LD_OBJ_ARG := `cat $$($1_OBJ_FILE_LIST)`
endif
endif
Expand Down
8 changes: 8 additions & 0 deletions make/hotspot/lib/JvmFlags.gmk
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,14 @@ JVM_CFLAGS_INCLUDES += \
-I$(TOPDIR)/src/java.base/share/native/libjimage \
#

ifeq ($(call isTargetOs, android ios), true)
JVM_CFLAGS_INCLUDES += \
-I$(TOPDIR)/src/hotspot/os_cpu/linux_aarch64 \
-I$(TOPDIR)/src/hotspot/os/linux \
-I$(TOPDIR)/src/hotspot/os/bsd \
#
endif

# INCLUDE_SUFFIX_* is only meant for including the proper
# platform files. Don't use it to guard code. Use the value of
# HOTSPOT_TARGET_CPU_DEFINE etc. instead.
Expand Down
2 changes: 1 addition & 1 deletion make/modules/java.desktop/Gensrc.gmk
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ ifeq ($(call isTargetOs, windows), false)
include gensrc/GensrcIcons.gmk
endif

ifeq ($(call isTargetOs, linux aix), true)
ifeq ($(call isTargetOs, linux aix android), true)
include gensrc/GensrcX11Wrappers.gmk
endif

Expand Down
2 changes: 1 addition & 1 deletion make/modules/java.desktop/Java.gmk
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ EXCLUDE_FILES += \
.template \
#

ifeq ($(call isTargetOs, macosx), true)
ifeq ($(call isTargetOs, macosx android ios), true)
# exclude all X11 on Mac.
EXCLUDES += \
sun/awt/screencast \
Expand Down
8 changes: 5 additions & 3 deletions make/modules/java.desktop/Lib.gmk
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,12 @@ $(call FillFindCache, $(wildcard $(TOPDIR)/src/java.desktop/*/native))
################################################################################
# Create the AWT/2D libraries

include lib/AwtLibraries.gmk
include lib/ClientLibraries.gmk
ifeq ($(call isTargetOs, android ios), false)
include lib/AwtLibraries.gmk
include lib/ClientLibraries.gmk
endif

ifeq ($(call isTargetOs, aix), false)
ifeq ($(call isTargetOs, aix android ios), false)
##############################################################################
# Build libjsound
##############################################################################
Expand Down
2 changes: 1 addition & 1 deletion src/hotspot/os/posix/os_posix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1233,7 +1233,7 @@ static bool _use_clock_monotonic_condattr = false;
// Determine what POSIX API's are present and do appropriate
// configuration.
void os::Posix::init(void) {
#if defined(_ALLBSD_SOURCE)
#if defined(_ALLBSD_SOURCE) && !defined(ANDROID)
clock_tics_per_sec = CLK_TCK;
#else
clock_tics_per_sec = checked_cast<int>(sysconf(_SC_CLK_TCK));
Expand Down
2 changes: 1 addition & 1 deletion src/hotspot/os/posix/signals_posix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ typedef struct sigaction *(*get_signal_t)(int);
static get_signal_t get_signal_action = nullptr;

// suspend/resume support
#if defined(__APPLE__)
#if defined(__APPLE__) && !defined(__IOS__)
static OSXSemaphore sr_semaphore;
#else
static PosixSemaphore sr_semaphore;
Expand Down
2 changes: 2 additions & 0 deletions src/hotspot/share/prims/whitebox.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2424,12 +2424,14 @@ WB_END
WB_ENTRY(jboolean, WB_CheckLibSpecifiesNoexecstack(JNIEnv* env, jobject o, jstring libfile))
jboolean ret = false;
#ifdef LINUX
#ifndef __IOS__
// Can't be in VM when we call JNI.
ThreadToNativeFromVM ttnfv(thread);
const char* lf = env->GetStringUTFChars(libfile, nullptr);
CHECK_JNI_EXCEPTION_(env, 0);
ret = (jboolean) ElfFile::specifies_noexecstack(lf);
env->ReleaseStringUTFChars(libfile, lf);
#endif // IOS
#endif
return ret;
WB_END
Expand Down
2 changes: 2 additions & 0 deletions src/hotspot/share/runtime/os.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1005,6 +1005,8 @@ class os: AllStatic {
// like ::LinuxUtils
#if defined(AIX)
class Aix;
#elif defined(__IOS__)
class Linux;
#elif defined(BSD)
class Bsd;
#elif defined(LINUX)
Expand Down
2 changes: 1 addition & 1 deletion src/hotspot/share/utilities/decoder_elf.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
#ifndef SHARE_UTILITIES_DECODER_ELF_HPP
#define SHARE_UTILITIES_DECODER_ELF_HPP

#if !defined(_WINDOWS) && !defined(__APPLE__)
#if !defined(_WINDOWS) && (!defined(__APPLE__) || defined(__IOS__))

#include "utilities/decoder.hpp"
#include "utilities/elfFile.hpp"
Expand Down
2 changes: 1 addition & 1 deletion src/hotspot/share/utilities/elfFile.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
#ifndef SHARE_UTILITIES_ELFFILE_HPP
#define SHARE_UTILITIES_ELFFILE_HPP

#if !defined(_WINDOWS) && !defined(__APPLE__) && !defined(_AIX)
#if !defined(_WINDOWS) && (!defined(__APPLE__) || defined(__IOS__)) && !defined(_AIX)

#if defined(__OpenBSD__)
#include <sys/exec_elf.h>
Expand Down
2 changes: 1 addition & 1 deletion src/hotspot/share/utilities/globalDefinitions_gcc.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@

#if defined(LINUX) || defined(_ALLBSD_SOURCE) || defined(_AIX)
#include <signal.h>
#ifndef __OpenBSD__
#if !defined(__OpenBSD__) && !defined(__IOS__)
#include <ucontext.h>
#endif
#ifdef __APPLE__
Expand Down
13 changes: 13 additions & 0 deletions src/java.base/macosx/native/libjava/java_props_macosx.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
* questions.
*/

#ifndef __IOS__
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
Expand All @@ -31,6 +32,16 @@
#include <CoreFoundation/CoreFoundation.h>
#include <SystemConfiguration/SystemConfiguration.h>
#include <Foundation/Foundation.h>
#else
#include <stdlib.h>
#include <string.h>
#include <sys/param.h>
#include <objc/runtime.h>
#include <objc/objc.h>
#include <objc/message.h>
#include <CoreFoundation/CoreFoundation.h>
#include <CoreFoundation/CFlocale.h>
#endif

#include "java_props_macosx.h"

Expand Down Expand Up @@ -222,6 +233,7 @@ char *setupMacOSXLocale(int cat) {
}
}

#ifndef __IOS__
// 10.9 SDK does not include the NSOperatingSystemVersion struct.
// For now, create our own
typedef struct {
Expand Down Expand Up @@ -492,3 +504,4 @@ void setProxyProperties(java_props_t *sProps) {

CFRelease(dict);
}
#endif
18 changes: 18 additions & 0 deletions src/java.base/macosx/native/libjli/java_md_macosx.m
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,12 @@

#include "manifest_info.h"

#ifndef __IOS__
/* Support Cocoa event loop on the main thread */
#include <Cocoa/Cocoa.h>
#include <objc/objc-runtime.h>
#include <objc/objc-auto.h>
#endif

#include <errno.h>
#include <spawn.h>
Expand Down Expand Up @@ -273,15 +275,23 @@
sPreferredJVMType = strdup(prefJVM);
}

#ifdef __IOS__
static jboolean awtLoaded = 0;
#else
static BOOL awtLoaded = NO;
#endif
static pthread_mutex_t awtLoaded_mutex = PTHREAD_MUTEX_INITIALIZER;
static pthread_cond_t awtLoaded_cv = PTHREAD_COND_INITIALIZER;

JNIEXPORT void JNICALL
JLI_NotifyAWTLoaded()
{
pthread_mutex_lock(&awtLoaded_mutex);
#ifdef __IOS__
awtLoaded = 1;
#else
awtLoaded = YES;
#endif
pthread_cond_signal(&awtLoaded_cv);
pthread_mutex_unlock(&awtLoaded_mutex);
}
Expand Down Expand Up @@ -310,6 +320,7 @@
exit(main_fptr(args->argc, args->argv));
}

#ifndef __IOS__
static void dummyTimer(CFRunLoopTimerRef timer, void *info) {}

static void ParkEventLoop() {
Expand All @@ -324,6 +335,7 @@ static void ParkEventLoop() {
result = CFRunLoopRunInMode(kCFRunLoopDefaultMode, 1.0e20, false);
} while (result != kCFRunLoopRunFinished);
}
#endif

/*
* Mac OS X mandates that the GUI event loop run on very first thread of
Expand Down Expand Up @@ -354,7 +366,9 @@ static void MacOSXStartup(int argc, char *argv[]) {
exit(1);
}

#ifndef __IOS__
ParkEventLoop();
#endif
}

void
Expand Down Expand Up @@ -937,6 +951,7 @@ static size_t adjustStackSize(size_t stack_size) {
JVMInit(InvocationFunctions* ifn, jlong threadStackSize,
int argc, char **argv,
int mode, char *what, int ret) {
#ifndef __IOS__
if (sameThread) {
JLI_TraceLauncher("In same thread\n");
// need to block this thread against the main thread
Expand Down Expand Up @@ -967,6 +982,9 @@ static size_t adjustStackSize(size_t stack_size) {
} else {
return ContinueInNewThread(ifn, threadStackSize, argc, argv, mode, what, ret);
}
#else
return ContinueInNewThread(ifn, threadStackSize, argc, argv, mode, what, ret);
#endif
}

/*
Expand Down
2 changes: 2 additions & 0 deletions src/java.base/macosx/native/libnio/fs/UTIFileTypeDetector.c
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ Java_sun_nio_fs_UTIFileTypeDetector_probe0(JNIEnv* env, jobject ftd,

CFStringRef extension = toCFString(env, ext);
if (extension != NULL) {
#ifndef __IOS__ // deprecated on ios
CFStringRef uti =
UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension,
extension, NULL);
Expand All @@ -121,6 +122,7 @@ Java_sun_nio_fs_UTIFileTypeDetector_probe0(JNIEnv* env, jobject ftd,
CFRelease(mimeType);
}
}
#endif
}

return result;
Expand Down
2 changes: 1 addition & 1 deletion src/java.base/unix/native/libjava/TimeZone_md.c
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ static const char popularZones[][4] = {"UTC", "GMT"};
static const char *ETC_ENVIRONMENT_FILE = "/etc/environment";
#endif

#if defined(__linux__) || defined(MACOSX)
#if defined(__linux__) || defined(MACOSX) || defined(__IOS__)

/*
* remove repeated path separators ('/') in the given 'path'.
Expand Down
Loading

0 comments on commit deb4256

Please sign in to comment.