Skip to content

Commit 5c5417b

Browse files
committed
Avoid calling internal CoreFoundation methods on Darwin
1 parent 341f6cb commit 5c5417b

File tree

6 files changed

+83
-125
lines changed

6 files changed

+83
-125
lines changed

substratevm/mx.substratevm/suite.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -442,6 +442,23 @@
442442
},
443443
},
444444

445+
"com.oracle.svm.native.darwin": {
446+
"subDir": "src",
447+
"native": "static_lib",
448+
"os_arch": {
449+
"darwin": {
450+
"<others>": {
451+
"cflags": ["-ObjC", "-fPIC", "-O1", "-D_LITTLE_ENDIAN", "-ffunction-sections", "-fdata-sections", "-fvisibility=hidden", "-D_FORTIFY_SOURCE=0"],
452+
},
453+
},
454+
"<others>": {
455+
"<others>": {
456+
"ignore": "only needed on darwin",
457+
},
458+
},
459+
},
460+
},
461+
445462
"com.oracle.svm.native.jvm.posix": {
446463
"subDir": "src",
447464
"native": "static_lib",
@@ -951,6 +968,7 @@
951968
"<os>-<arch>/": [
952969
"dependency:com.oracle.svm.native.libchelper/*",
953970
"dependency:com.oracle.svm.native.strictmath/*",
971+
"dependency:com.oracle.svm.native.darwin/*",
954972
"dependency:com.oracle.svm.native.jvm.posix/*",
955973
"dependency:com.oracle.svm.native.jvm.windows/*",
956974
"extracted-dependency:truffle:LIBFFI_DIST",

substratevm/src/com.oracle.svm.core.posix/src/com/oracle/svm/core/posix/darwin/DarwinCoreFoundationUtils.java

Lines changed: 0 additions & 60 deletions
This file was deleted.

substratevm/src/com.oracle.svm.core.posix/src/com/oracle/svm/core/posix/darwin/DarwinSystemPropertiesSupport.java

Lines changed: 10 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,9 @@
2424
*/
2525
package com.oracle.svm.core.posix.darwin;
2626

27-
import static com.oracle.svm.core.posix.headers.darwin.CoreFoundation.CFRetain;
28-
2927
import org.graalvm.nativeimage.ImageSingletons;
3028
import org.graalvm.nativeimage.StackValue;
29+
import org.graalvm.nativeimage.c.function.CLibrary;
3130
import org.graalvm.nativeimage.c.type.CCharPointer;
3231
import org.graalvm.nativeimage.c.type.CTypeConversion;
3332
import org.graalvm.nativeimage.hosted.Feature;
@@ -39,9 +38,9 @@
3938
import com.oracle.svm.core.posix.PosixSystemPropertiesSupport;
4039
import com.oracle.svm.core.posix.headers.Limits;
4140
import com.oracle.svm.core.posix.headers.Unistd;
42-
import com.oracle.svm.core.posix.headers.darwin.CoreFoundation;
43-
import com.oracle.svm.core.posix.headers.darwin.CoreFoundation.CFStringRef;
41+
import com.oracle.svm.core.posix.headers.darwin.Foundation;
4442

43+
@CLibrary(value = "darwin", requireStatic = true)
4544
public class DarwinSystemPropertiesSupport extends PosixSystemPropertiesSupport {
4645

4746
@Override
@@ -69,30 +68,16 @@ protected String osVersionValue() {
6968
return osVersionValue;
7069
}
7170

72-
/* On OSX Java returns the ProductVersion instead of kernel release info. */
73-
CoreFoundation.CFDictionaryRef dict = CoreFoundation._CFCopyServerVersionDictionary();
74-
if (dict.isNull()) {
75-
dict = CoreFoundation._CFCopySystemVersionDictionary();
76-
}
77-
if (dict.isNull()) {
71+
Foundation.NSOperatingSystemVersion osVersion = StackValue.get(Foundation.NSOperatingSystemVersion.class);
72+
Foundation.operatingSystemVersion(osVersion);
73+
if (osVersion.isNull()) {
7874
return osVersionValue = "Unknown";
79-
}
80-
CoreFoundation.CFStringRef dictKeyRef = DarwinCoreFoundationUtils.toCFStringRef("MacOSXProductVersion");
81-
CoreFoundation.CFStringRef dictValue = CoreFoundation.CFDictionaryGetValue(dict, dictKeyRef);
82-
CoreFoundation.CFRelease(dictKeyRef);
83-
if (dictValue.isNull()) {
84-
dictKeyRef = DarwinCoreFoundationUtils.toCFStringRef("ProductVersion");
85-
dictValue = CoreFoundation.CFDictionaryGetValue(dict, dictKeyRef);
86-
CoreFoundation.CFRelease(dictKeyRef);
87-
}
88-
if (dictValue.isNonNull()) {
89-
dictValue = (CFStringRef) CFRetain(dictValue);
90-
osVersionValue = DarwinCoreFoundationUtils.fromCFStringRef(dictValue);
91-
CoreFoundation.CFRelease(dictValue);
9275
} else {
93-
osVersionValue = "Unknown";
76+
long major = osVersion.getMajorVersion();
77+
long minor = osVersion.getMinorVersion();
78+
long patch = osVersion.getPatchVersion();
79+
return osVersionValue = major + "." + minor + "." + patch;
9480
}
95-
return osVersionValue;
9681
}
9782
}
9883

substratevm/src/com.oracle.svm.core.posix/src/com/oracle/svm/core/posix/headers/PosixDirectives.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
import java.util.ArrayList;
2828
import java.util.Arrays;
29+
import java.util.Collections;
2930
import java.util.List;
3031

3132
import org.graalvm.nativeimage.Platform;
@@ -54,7 +55,7 @@ public class PosixDirectives implements CContext.Directives {
5455
};
5556

5657
private static final String[] darwinLibs = new String[]{
57-
"<CoreFoundation/CoreFoundation.h>",
58+
"<Foundation/Foundation.h>",
5859
"<mach/mach.h>",
5960
"<mach/mach_time.h>",
6061
"<mach-o/dyld.h>",
@@ -85,6 +86,14 @@ public List<String> getHeaderFiles() {
8586
return result;
8687
}
8788

89+
@Override
90+
public List<String> getOptions() {
91+
if (Platform.includedIn(Platform.DARWIN.class)) {
92+
return Collections.singletonList("-ObjC");
93+
}
94+
return Collections.emptyList();
95+
}
96+
8897
@Override
8998
public List<String> getMacroDefinitions() {
9099
return Arrays.asList("_GNU_SOURCE", "_LARGEFILE64_SOURCE");
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2013, 2019, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2019, 2020, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -27,57 +27,33 @@
2727
import org.graalvm.nativeimage.c.CContext;
2828
import org.graalvm.nativeimage.c.function.CFunction;
2929
import org.graalvm.nativeimage.c.function.CLibrary;
30+
import org.graalvm.nativeimage.c.struct.CField;
31+
import org.graalvm.nativeimage.c.struct.CStruct;
3032
import org.graalvm.word.PointerBase;
31-
import org.graalvm.word.SignedWord;
3233

3334
import com.oracle.svm.core.posix.headers.PosixDirectives;
3435

3536
// Checkstyle: stop
3637

3738
/**
38-
* Definitions manually translated from the C header file CoreFoundation/CoreFoundation.h.
39+
* Definitions manually translated from the C header file Foundation/Foundation.h.
3940
*/
4041
@CContext(PosixDirectives.class)
41-
@CLibrary("-framework CoreFoundation")
42-
public class CoreFoundation {
42+
@CLibrary("-framework Foundation")
43+
public class Foundation {
4344

44-
public interface CFStringRef extends PointerBase {
45-
}
45+
@CStruct
46+
public interface NSOperatingSystemVersion extends PointerBase {
47+
@CField("majorVersion")
48+
long getMajorVersion();
4649

47-
public interface CFMutableStringRef extends CFStringRef {
50+
@CField("minorVersion")
51+
long getMinorVersion();
4852

53+
@CField("patchVersion")
54+
long getPatchVersion();
4955
}
5056

5157
@CFunction
52-
public static native CFMutableStringRef CFStringCreateMutable(PointerBase alloc, SignedWord maxLength);
53-
54-
@CFunction
55-
public static native void CFStringAppendCharacters(CFMutableStringRef theString, PointerBase chars, SignedWord numChars);
56-
57-
@CFunction
58-
public static native void CFStringNormalize(CFMutableStringRef theString, SignedWord theForm);
59-
60-
@CFunction
61-
public static native long CFStringGetLength(CFStringRef theString);
62-
63-
@CFunction
64-
public static native void CFRelease(PointerBase cf);
65-
66-
@CFunction
67-
public static native PointerBase CFRetain(PointerBase cf);
68-
69-
public interface CFDictionaryRef extends PointerBase {
70-
}
71-
72-
@CFunction
73-
public static native CFDictionaryRef _CFCopyServerVersionDictionary();
74-
75-
@CFunction
76-
public static native CFDictionaryRef _CFCopySystemVersionDictionary();
77-
78-
@CFunction
79-
public static native CFStringRef CFDictionaryGetValue(CFDictionaryRef theDict, CFStringRef key);
80-
81-
@CFunction
82-
public static native char CFStringGetCharacterAtIndex(CFStringRef theString, long idx);
58+
public static native void operatingSystemVersion(PointerBase osVersionStruct);
8359
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* Copyright (c) 2019, 2020, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation. Oracle designates this
8+
* particular file as subject to the "Classpath" exception as provided
9+
* by Oracle in the LICENSE file that accompanied this code.
10+
*
11+
* This code is distributed in the hope that it will be useful, but WITHOUT
12+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14+
* version 2 for more details (a copy is included in the LICENSE file that
15+
* accompanied this code).
16+
*
17+
* You should have received a copy of the GNU General Public License version
18+
* 2 along with this work; if not, write to the Free Software Foundation,
19+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20+
*
21+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22+
* or visit www.oracle.com if you need additional information or have any
23+
* questions.
24+
*/
25+
26+
#include <Foundation/Foundation.h>
27+
28+
void operatingSystemVersion(NSOperatingSystemVersion* osv) {
29+
*osv = [[NSProcessInfo processInfo] operatingSystemVersion];
30+
}

0 commit comments

Comments
 (0)