jnr / jnr-ffi

Java Abstracted Foreign Function Layer
Other
1.25k stars 155 forks source link

os.arch may be i686 #104

Closed lygstate closed 7 years ago

lygstate commented 7 years ago
    private static CPU determineCPU() {
        String archString = System.getProperty("os.arch");
        if (equalsIgnoreCase("x86", archString) || equalsIgnoreCase("i386", archString) || equalsIgnoreCase("i86pc", archString)) {
            return CPU.I386;
headius commented 7 years ago

You are correct sir!

lygstate commented 7 years ago

The situation are more complicated

headius commented 7 years ago

Can you elaborate on that?

lygstate commented 7 years ago

Sorry for the delay to reply, I am on the vacation recent days. To be specific, I add a new file named CpuDetect.java

/*
 * Copyright (C) 2007, 2008, 2009 Wayne Meissner
 *
 * This file is part of jffi.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * 
 * Alternatively, you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * 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 Lesser General Public License
 * for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this work.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.kenai.jffi;
import static com.kenai.jffi.Util.equalsIgnoreCase;

public class CpuDetect {
    private static final java.util.Locale LOCALE = java.util.Locale.ENGLISH;
    /**
     * The common names of cpu architectures.
     *
     * <b>Note</b> The names of the enum values are used in other parts of the
     * code to determine where to find the native stub library.  Do not rename.
     */
    public enum CPU {
        /** Intel ia32 */
        I386(32),
        /** AMD 64 bit (aka EM64T/X64) */
        X86_64(64),
        /** Power PC 32 bit */
        PPC(32),
        /** Power PC 64 bit */
        PPC64(64),
        /** Power PC 64 bit little endian */
        PPC64LE(64),
        /** Sun sparc 32 bit */
        SPARC(32),
        /** Sun sparc 64 bit */
        SPARCV9(64),
        /** IBM zSeries S/390 64 bit */
        S390X(64),
        /** ARM */
        ARM(32),
        /** AARCH64 */
        AARCH64(64),
        /** Unknown CPU */
        UNKNOWN(64);

        CPU(int dataModel) {
            this.dataModel = dataModel;
            this.addressMask = dataModel == 32 ? 0xffffffffL : 0xffffffffffffffffL;
        }

        public final int dataModel;
        public final long addressMask;
        @Override
        public String toString() { return name().toLowerCase(LOCALE); }
    }

    /**
     * Determines the CPU architecture the JVM is running on.
     *
     * This normalizes all the variations that are equivalent (e.g. i386, x86, i86pc)
     * into a common cpu type.
     *
     * @return A member of the <tt>CPU</tt> enum.
     */
    public static CPU determineCPU() {
        String archString = null;
        try {
            archString = Foreign.getInstance().getArch();
        } catch (UnsatisfiedLinkError ex) {}

        if (archString == null || "unknown".equals(archString)) {
            archString = System.getProperty("os.arch", "unknown");
        }

        if (Util.equalsIgnoreCase("x86", archString, LOCALE)
                || Util.equalsIgnoreCase("i386", archString, LOCALE)
                || Util.equalsIgnoreCase("i586", archString, LOCALE)
                || Util.equalsIgnoreCase("i686", archString, LOCALE)
                || Util.equalsIgnoreCase("i86pc", archString, LOCALE)) {
            return CPU.I386;
        } else if (Util.equalsIgnoreCase("x86_64", archString, LOCALE) || Util.equalsIgnoreCase("amd64", archString, LOCALE)) {
            return CPU.X86_64;
        } else if (Util.equalsIgnoreCase("ppc", archString, LOCALE) || Util.equalsIgnoreCase("powerpc", archString, LOCALE)) {
            return CPU.PPC;
        } else if (Util.equalsIgnoreCase("ppc64", archString, LOCALE) || Util.equalsIgnoreCase("powerpc64", archString, LOCALE)) {
            return CPU.PPC64;
        } else if (Util.equalsIgnoreCase("ppc64le", archString, LOCALE) || Util.equalsIgnoreCase("powerpc64le", archString, LOCALE)) {
            return CPU.PPC64LE;
        } else if (Util.equalsIgnoreCase("s390", archString, LOCALE) || Util.equalsIgnoreCase("s390x", archString, LOCALE)) {
            return CPU.S390X;
        } else if (Util.equalsIgnoreCase("arm", archString, LOCALE)
                || Util.equalsIgnoreCase("armv7l", archString, LOCALE)) {
            return CPU.ARM;
        } else if (Util.equalsIgnoreCase("aarch64", archString, LOCALE)) {
            return CPU.AARCH64;
        }

        // Try to find by lookup up in the CPU list
        for (CPU cpu : CPU.values()) {
            if (cpu.name().equalsIgnoreCase(archString)) {
                return cpu;
            }
        }

        return CPU.UNKNOWN;
    }
}

So that jffi & jnr.fii sharing the same CPU detecting code.