diff --git a/substratevm/mx.substratevm/suite.py b/substratevm/mx.substratevm/suite.py index bb368d19a360..c31ab9f91752 100644 --- a/substratevm/mx.substratevm/suite.py +++ b/substratevm/mx.substratevm/suite.py @@ -442,6 +442,23 @@ }, }, + "com.oracle.svm.native.darwin": { + "subDir": "src", + "native": "static_lib", + "os_arch": { + "darwin": { + "": { + "cflags": ["-ObjC", "-fPIC", "-O1", "-D_LITTLE_ENDIAN", "-ffunction-sections", "-fdata-sections", "-fvisibility=hidden", "-D_FORTIFY_SOURCE=0"], + }, + }, + "": { + "": { + "ignore": "only needed on darwin", + }, + }, + }, + }, + "com.oracle.svm.native.jvm.posix": { "subDir": "src", "native": "static_lib", @@ -951,6 +968,7 @@ "-/": [ "dependency:com.oracle.svm.native.libchelper/*", "dependency:com.oracle.svm.native.strictmath/*", + "dependency:com.oracle.svm.native.darwin/*", "dependency:com.oracle.svm.native.jvm.posix/*", "dependency:com.oracle.svm.native.jvm.windows/*", "extracted-dependency:truffle:LIBFFI_DIST", diff --git a/substratevm/src/com.oracle.svm.core.posix/src/com/oracle/svm/core/posix/darwin/DarwinCoreFoundationUtils.java b/substratevm/src/com.oracle.svm.core.posix/src/com/oracle/svm/core/posix/darwin/DarwinCoreFoundationUtils.java deleted file mode 100644 index c14916944195..000000000000 --- a/substratevm/src/com.oracle.svm.core.posix/src/com/oracle/svm/core/posix/darwin/DarwinCoreFoundationUtils.java +++ /dev/null @@ -1,60 +0,0 @@ -/* - * Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved. - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. - * - * This code is free software; you can redistribute it and/or modify it - * under the terms of the GNU General Public License version 2 only, as - * published by the Free Software Foundation. Oracle designates this - * particular file as subject to the "Classpath" exception as provided - * by Oracle in the LICENSE file that accompanied this code. - * - * This code is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License - * version 2 for more details (a copy is included in the LICENSE file that - * accompanied this code). - * - * You should have received a copy of the GNU General Public License version - * 2 along with this work; if not, write to the Free Software Foundation, - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. - * - * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA - * or visit www.oracle.com if you need additional information or have any - * questions. - */ -package com.oracle.svm.core.posix.darwin; - -import org.graalvm.nativeimage.PinnedObject; -import org.graalvm.word.PointerBase; -import org.graalvm.word.WordFactory; - -import com.oracle.svm.core.posix.headers.darwin.CoreFoundation; - -public final class DarwinCoreFoundationUtils { - - private DarwinCoreFoundationUtils() { - } - - public static CoreFoundation.CFMutableStringRef toCFStringRef(String str) { - CoreFoundation.CFMutableStringRef stringRef = CoreFoundation.CFStringCreateMutable(WordFactory.nullPointer(), WordFactory.zero()); - if (stringRef.isNull()) { - throw new OutOfMemoryError("native heap"); - } - char[] charArray = str.toCharArray(); - try (PinnedObject pathPin = PinnedObject.create(charArray)) { - PointerBase addressOfCharArray = pathPin.addressOfArrayElement(0); - CoreFoundation.CFStringAppendCharacters(stringRef, addressOfCharArray, WordFactory.signed(charArray.length)); - } - return stringRef; - } - - public static String fromCFStringRef(CoreFoundation.CFStringRef cfstr) { - int length = (int) CoreFoundation.CFStringGetLength(cfstr); - char[] chars = new char[length]; - for (int i = 0; i < length; ++i) { - chars[i] = CoreFoundation.CFStringGetCharacterAtIndex(cfstr, i); - } - return String.valueOf(chars); - } - -} diff --git a/substratevm/src/com.oracle.svm.core.posix/src/com/oracle/svm/core/posix/darwin/DarwinSystemPropertiesSupport.java b/substratevm/src/com.oracle.svm.core.posix/src/com/oracle/svm/core/posix/darwin/DarwinSystemPropertiesSupport.java index 77c0ec2bab83..92db80348581 100644 --- a/substratevm/src/com.oracle.svm.core.posix/src/com/oracle/svm/core/posix/darwin/DarwinSystemPropertiesSupport.java +++ b/substratevm/src/com.oracle.svm.core.posix/src/com/oracle/svm/core/posix/darwin/DarwinSystemPropertiesSupport.java @@ -24,10 +24,9 @@ */ package com.oracle.svm.core.posix.darwin; -import static com.oracle.svm.core.posix.headers.darwin.CoreFoundation.CFRetain; - import org.graalvm.nativeimage.ImageSingletons; import org.graalvm.nativeimage.StackValue; +import org.graalvm.nativeimage.c.function.CLibrary; import org.graalvm.nativeimage.c.type.CCharPointer; import org.graalvm.nativeimage.c.type.CTypeConversion; import org.graalvm.nativeimage.hosted.Feature; @@ -39,9 +38,9 @@ import com.oracle.svm.core.posix.PosixSystemPropertiesSupport; import com.oracle.svm.core.posix.headers.Limits; import com.oracle.svm.core.posix.headers.Unistd; -import com.oracle.svm.core.posix.headers.darwin.CoreFoundation; -import com.oracle.svm.core.posix.headers.darwin.CoreFoundation.CFStringRef; +import com.oracle.svm.core.posix.headers.darwin.Foundation; +@CLibrary(value = "darwin", requireStatic = true) public class DarwinSystemPropertiesSupport extends PosixSystemPropertiesSupport { @Override @@ -69,30 +68,16 @@ protected String osVersionValue() { return osVersionValue; } - /* On OSX Java returns the ProductVersion instead of kernel release info. */ - CoreFoundation.CFDictionaryRef dict = CoreFoundation._CFCopyServerVersionDictionary(); - if (dict.isNull()) { - dict = CoreFoundation._CFCopySystemVersionDictionary(); - } - if (dict.isNull()) { + Foundation.NSOperatingSystemVersion osVersion = StackValue.get(Foundation.NSOperatingSystemVersion.class); + Foundation.operatingSystemVersion(osVersion); + if (osVersion.isNull()) { return osVersionValue = "Unknown"; - } - CoreFoundation.CFStringRef dictKeyRef = DarwinCoreFoundationUtils.toCFStringRef("MacOSXProductVersion"); - CoreFoundation.CFStringRef dictValue = CoreFoundation.CFDictionaryGetValue(dict, dictKeyRef); - CoreFoundation.CFRelease(dictKeyRef); - if (dictValue.isNull()) { - dictKeyRef = DarwinCoreFoundationUtils.toCFStringRef("ProductVersion"); - dictValue = CoreFoundation.CFDictionaryGetValue(dict, dictKeyRef); - CoreFoundation.CFRelease(dictKeyRef); - } - if (dictValue.isNonNull()) { - dictValue = (CFStringRef) CFRetain(dictValue); - osVersionValue = DarwinCoreFoundationUtils.fromCFStringRef(dictValue); - CoreFoundation.CFRelease(dictValue); } else { - osVersionValue = "Unknown"; + long major = osVersion.getMajorVersion(); + long minor = osVersion.getMinorVersion(); + long patch = osVersion.getPatchVersion(); + return osVersionValue = major + "." + minor + "." + patch; } - return osVersionValue; } } diff --git a/substratevm/src/com.oracle.svm.core.posix/src/com/oracle/svm/core/posix/headers/PosixDirectives.java b/substratevm/src/com.oracle.svm.core.posix/src/com/oracle/svm/core/posix/headers/PosixDirectives.java index fe891e0bc633..da50426faf28 100644 --- a/substratevm/src/com.oracle.svm.core.posix/src/com/oracle/svm/core/posix/headers/PosixDirectives.java +++ b/substratevm/src/com.oracle.svm.core.posix/src/com/oracle/svm/core/posix/headers/PosixDirectives.java @@ -26,6 +26,7 @@ import java.util.ArrayList; import java.util.Arrays; +import java.util.Collections; import java.util.List; import org.graalvm.nativeimage.Platform; @@ -54,7 +55,7 @@ public class PosixDirectives implements CContext.Directives { }; private static final String[] darwinLibs = new String[]{ - "", + "", "", "", "", @@ -85,6 +86,14 @@ public List getHeaderFiles() { return result; } + @Override + public List getOptions() { + if (Platform.includedIn(Platform.DARWIN.class)) { + return Collections.singletonList("-ObjC"); + } + return Collections.emptyList(); + } + @Override public List getMacroDefinitions() { return Arrays.asList("_GNU_SOURCE", "_LARGEFILE64_SOURCE"); diff --git a/substratevm/src/com.oracle.svm.core.posix/src/com/oracle/svm/core/posix/headers/darwin/CoreFoundation.java b/substratevm/src/com.oracle.svm.core.posix/src/com/oracle/svm/core/posix/headers/darwin/Foundation.java similarity index 51% rename from substratevm/src/com.oracle.svm.core.posix/src/com/oracle/svm/core/posix/headers/darwin/CoreFoundation.java rename to substratevm/src/com.oracle.svm.core.posix/src/com/oracle/svm/core/posix/headers/darwin/Foundation.java index 1c0612ed62fd..0abd0523b638 100644 --- a/substratevm/src/com.oracle.svm.core.posix/src/com/oracle/svm/core/posix/headers/darwin/CoreFoundation.java +++ b/substratevm/src/com.oracle.svm.core.posix/src/com/oracle/svm/core/posix/headers/darwin/Foundation.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2013, 2019, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2019, 2020, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -27,57 +27,33 @@ import org.graalvm.nativeimage.c.CContext; import org.graalvm.nativeimage.c.function.CFunction; import org.graalvm.nativeimage.c.function.CLibrary; +import org.graalvm.nativeimage.c.struct.CField; +import org.graalvm.nativeimage.c.struct.CStruct; import org.graalvm.word.PointerBase; -import org.graalvm.word.SignedWord; import com.oracle.svm.core.posix.headers.PosixDirectives; // Checkstyle: stop /** - * Definitions manually translated from the C header file CoreFoundation/CoreFoundation.h. + * Definitions manually translated from the C header file Foundation/Foundation.h. */ @CContext(PosixDirectives.class) -@CLibrary("-framework CoreFoundation") -public class CoreFoundation { +@CLibrary("-framework Foundation") +public class Foundation { - public interface CFStringRef extends PointerBase { - } + @CStruct + public interface NSOperatingSystemVersion extends PointerBase { + @CField("majorVersion") + long getMajorVersion(); - public interface CFMutableStringRef extends CFStringRef { + @CField("minorVersion") + long getMinorVersion(); + @CField("patchVersion") + long getPatchVersion(); } @CFunction - public static native CFMutableStringRef CFStringCreateMutable(PointerBase alloc, SignedWord maxLength); - - @CFunction - public static native void CFStringAppendCharacters(CFMutableStringRef theString, PointerBase chars, SignedWord numChars); - - @CFunction - public static native void CFStringNormalize(CFMutableStringRef theString, SignedWord theForm); - - @CFunction - public static native long CFStringGetLength(CFStringRef theString); - - @CFunction - public static native void CFRelease(PointerBase cf); - - @CFunction - public static native PointerBase CFRetain(PointerBase cf); - - public interface CFDictionaryRef extends PointerBase { - } - - @CFunction - public static native CFDictionaryRef _CFCopyServerVersionDictionary(); - - @CFunction - public static native CFDictionaryRef _CFCopySystemVersionDictionary(); - - @CFunction - public static native CFStringRef CFDictionaryGetValue(CFDictionaryRef theDict, CFStringRef key); - - @CFunction - public static native char CFStringGetCharacterAtIndex(CFStringRef theString, long idx); + public static native void operatingSystemVersion(PointerBase osVersionStruct); } diff --git a/substratevm/src/com.oracle.svm.native.darwin/src/foundation.c b/substratevm/src/com.oracle.svm.native.darwin/src/foundation.c new file mode 100644 index 000000000000..7f251c068c2c --- /dev/null +++ b/substratevm/src/com.oracle.svm.native.darwin/src/foundation.c @@ -0,0 +1,30 @@ +/* + * Copyright (c) 2019, 2020, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the LICENSE file that accompanied this code. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +#include + +void operatingSystemVersion(NSOperatingSystemVersion* osv) { + *osv = [[NSProcessInfo processInfo] operatingSystemVersion]; +}