|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +package org.apache.datafusion; |
| 21 | + |
| 22 | +import java.util.Locale; |
| 23 | + |
| 24 | +/** |
| 25 | + * Identifies a target OS/arch pair and the path at which the bundled native library is published |
| 26 | + * inside the JAR. |
| 27 | + * |
| 28 | + * <p>Resource layout follows the convention from the {@code datafusion-java} packaging design: |
| 29 | + * |
| 30 | + * <pre> |
| 31 | + * org/apache/datafusion/linux/amd64/libdatafusion_jni.so |
| 32 | + * org/apache/datafusion/linux/aarch64/libdatafusion_jni.so |
| 33 | + * org/apache/datafusion/darwin/x86_64/libdatafusion_jni.dylib |
| 34 | + * org/apache/datafusion/darwin/aarch64/libdatafusion_jni.dylib |
| 35 | + * org/apache/datafusion/windows/amd64/datafusion_jni.dll |
| 36 | + * </pre> |
| 37 | + * |
| 38 | + * <p>Package-private; consumed only by {@link NativeLibraryLoader}. |
| 39 | + */ |
| 40 | +final class Platform { |
| 41 | + |
| 42 | + static final String LIBRARY_NAME = "datafusion_jni"; |
| 43 | + static final String RESOURCE_PREFIX = "org/apache/datafusion"; |
| 44 | + |
| 45 | + enum Os { |
| 46 | + LINUX("linux", "lib", "so"), |
| 47 | + DARWIN("darwin", "lib", "dylib"), |
| 48 | + WINDOWS("windows", "", "dll"); |
| 49 | + |
| 50 | + final String dirName; |
| 51 | + final String libPrefix; |
| 52 | + final String libSuffix; |
| 53 | + |
| 54 | + Os(String dirName, String libPrefix, String libSuffix) { |
| 55 | + this.dirName = dirName; |
| 56 | + this.libPrefix = libPrefix; |
| 57 | + this.libSuffix = libSuffix; |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + final Os os; |
| 62 | + final String arch; |
| 63 | + |
| 64 | + private Platform(Os os, String arch) { |
| 65 | + this.os = os; |
| 66 | + this.arch = arch; |
| 67 | + } |
| 68 | + |
| 69 | + static Platform current() { |
| 70 | + return of(System.getProperty("os.name"), System.getProperty("os.arch")); |
| 71 | + } |
| 72 | + |
| 73 | + static Platform of(String osName, String osArch) { |
| 74 | + Os os = detectOs(osName); |
| 75 | + String arch = detectArch(os, osArch); |
| 76 | + return new Platform(os, arch); |
| 77 | + } |
| 78 | + |
| 79 | + static Os detectOs(String osName) { |
| 80 | + if (osName == null) { |
| 81 | + throw new UnsupportedOperationException("os.name is not set"); |
| 82 | + } |
| 83 | + String n = osName.toLowerCase(Locale.ROOT); |
| 84 | + if (n.startsWith("linux")) { |
| 85 | + return Os.LINUX; |
| 86 | + } |
| 87 | + if (n.startsWith("mac") || n.contains("darwin")) { |
| 88 | + return Os.DARWIN; |
| 89 | + } |
| 90 | + if (n.startsWith("windows")) { |
| 91 | + return Os.WINDOWS; |
| 92 | + } |
| 93 | + throw new UnsupportedOperationException("Unsupported OS for datafusion_jni: " + osName); |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * Returns the architecture segment used in the resource path for {@code os}. |
| 98 | + * |
| 99 | + * <p>Linux and Windows use {@code amd64} (Java's preferred name for x86_64), while macOS uses |
| 100 | + * {@code x86_64}; all use {@code aarch64} for ARM64. |
| 101 | + */ |
| 102 | + static String detectArch(Os os, String osArch) { |
| 103 | + if (osArch == null) { |
| 104 | + throw new UnsupportedOperationException("os.arch is not set"); |
| 105 | + } |
| 106 | + String n = osArch.toLowerCase(Locale.ROOT); |
| 107 | + boolean isX64 = n.equals("amd64") || n.equals("x86_64") || n.equals("x64"); |
| 108 | + boolean isArm64 = n.equals("aarch64") || n.equals("arm64"); |
| 109 | + if (isX64) { |
| 110 | + return os == Os.DARWIN ? "x86_64" : "amd64"; |
| 111 | + } |
| 112 | + if (isArm64) { |
| 113 | + return "aarch64"; |
| 114 | + } |
| 115 | + throw new UnsupportedOperationException( |
| 116 | + "Unsupported CPU architecture for datafusion_jni: " + osArch); |
| 117 | + } |
| 118 | + |
| 119 | + String libFileName() { |
| 120 | + return os.libPrefix + LIBRARY_NAME + "." + os.libSuffix; |
| 121 | + } |
| 122 | + |
| 123 | + /** |
| 124 | + * Absolute classpath resource path (with leading slash) of the bundled native library for this |
| 125 | + * platform. |
| 126 | + */ |
| 127 | + String resourcePath() { |
| 128 | + return "/" + RESOURCE_PREFIX + "/" + os.dirName + "/" + arch + "/" + libFileName(); |
| 129 | + } |
| 130 | + |
| 131 | + @Override |
| 132 | + public String toString() { |
| 133 | + return os.dirName + "/" + arch; |
| 134 | + } |
| 135 | +} |
0 commit comments