Skip to content

Commit

Permalink
Avoid calling internal CoreFoundation methods on Darwin
Browse files Browse the repository at this point in the history
  • Loading branch information
loicottet committed Mar 12, 2020
1 parent 341f6cb commit 5c5417b
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 125 deletions.
18 changes: 18 additions & 0 deletions substratevm/mx.substratevm/suite.py
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,23 @@
},
},

"com.oracle.svm.native.darwin": {
"subDir": "src",
"native": "static_lib",
"os_arch": {
"darwin": {
"<others>": {
"cflags": ["-ObjC", "-fPIC", "-O1", "-D_LITTLE_ENDIAN", "-ffunction-sections", "-fdata-sections", "-fvisibility=hidden", "-D_FORTIFY_SOURCE=0"],
},
},
"<others>": {
"<others>": {
"ignore": "only needed on darwin",
},
},
},
},

"com.oracle.svm.native.jvm.posix": {
"subDir": "src",
"native": "static_lib",
Expand Down Expand Up @@ -951,6 +968,7 @@
"<os>-<arch>/": [
"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",
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -54,7 +55,7 @@ public class PosixDirectives implements CContext.Directives {
};

private static final String[] darwinLibs = new String[]{
"<CoreFoundation/CoreFoundation.h>",
"<Foundation/Foundation.h>",
"<mach/mach.h>",
"<mach/mach_time.h>",
"<mach-o/dyld.h>",
Expand Down Expand Up @@ -85,6 +86,14 @@ public List<String> getHeaderFiles() {
return result;
}

@Override
public List<String> getOptions() {
if (Platform.includedIn(Platform.DARWIN.class)) {
return Collections.singletonList("-ObjC");
}
return Collections.emptyList();
}

@Override
public List<String> getMacroDefinitions() {
return Arrays.asList("_GNU_SOURCE", "_LARGEFILE64_SOURCE");
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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);
}
30 changes: 30 additions & 0 deletions substratevm/src/com.oracle.svm.native.darwin/src/foundation.c
Original file line number Diff line number Diff line change
@@ -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 <Foundation/Foundation.h>

void operatingSystemVersion(NSOperatingSystemVersion* osv) {
*osv = [[NSProcessInfo processInfo] operatingSystemVersion];
}

0 comments on commit 5c5417b

Please sign in to comment.