001/*----------------------------------------------------------------------------*/
002/* Copyright (c) 2017-2018 FIRST. All Rights Reserved.                        */
003/* Open Source Software - may be modified and shared by FRC teams. The code   */
004/* must be accompanied by the FIRST BSD license file in the root directory of */
005/* the project.                                                               */
006/*----------------------------------------------------------------------------*/
007
008package edu.wpi.first.wpiutil;
009
010import java.io.File;
011import java.io.IOException;
012import java.io.BufferedReader;
013import java.io.InputStreamReader;
014
015public class RuntimeDetector {
016  private static String filePrefix;
017  private static String fileExtension;
018  private static String filePath;
019
020  private static synchronized void computePlatform() {
021    if (fileExtension != null && filePath != null && filePrefix != null) {
022      return;
023    }
024
025
026    boolean intel32 = is32BitIntel();
027    boolean intel64 = is64BitIntel();
028
029    if (isWindows()) {
030      filePrefix = "";
031      fileExtension = ".dll";
032      if (intel32) {
033        filePath = "/windows/x86/";
034      } else {
035        filePath = "/windows/x86-64/";
036      }
037    } else if (isMac()) {
038      filePrefix = "lib";
039      fileExtension = ".dylib";
040      if (intel32) {
041        filePath = "/osx/x86";
042      } else {
043        filePath = "/osx/x86-64/";
044      }
045    } else if (isLinux()) {
046      filePrefix = "lib";
047      fileExtension = ".so";
048      if (intel32) {
049        filePath = "/linux/x86/";
050      } else if (intel64) {
051        filePath = "/linux/x86-64/";
052      } else if (isAthena()) {
053        filePath = "/linux/athena/";
054      } else {
055        filePath = "/linux/nativearm/";
056      }
057    } else {
058      throw new RuntimeException("Failed to determine OS");
059    }
060  }
061
062  public static synchronized String getFilePrefix() {
063    computePlatform();
064
065    return filePrefix;
066  }
067
068  public static synchronized String getFileExtension() {
069    computePlatform();
070
071    return fileExtension;
072  }
073
074  public static synchronized String getPlatformPath() {
075    computePlatform();
076
077    return filePath;
078  }
079
080  public static synchronized String getLibraryResource(String libName) {
081    computePlatform();
082
083    return filePath + filePrefix + libName + fileExtension;
084  }
085
086  public static boolean isAthena() {
087    File runRobotFile = new File("/usr/local/frc/bin/frcRunRobot.sh");
088    return runRobotFile.exists();
089  }
090
091  public static boolean isLinux() {
092    return System.getProperty("os.name").startsWith("Linux");
093  }
094
095  public static boolean isWindows() {
096    return System.getProperty("os.name").startsWith("Windows");
097  }
098
099  public static boolean isMac() {
100    return System.getProperty("os.name").startsWith("Mac");
101  }
102
103  public static boolean is32BitIntel() {
104    String arch = System.getProperty("os.arch");
105    return arch.equals("x86") || arch.equals("i386");
106  }
107
108  public static boolean is64BitIntel() {
109    String arch = System.getProperty("os.arch");
110    return arch.equals("amd64") || arch.equals("x86_64");
111  }
112}