Skip to content

Commit

Permalink
Refactor platforms
Browse files Browse the repository at this point in the history
Add platform detection for linux arm and armhf (no binaries exist yet for these)
Add default install paths for Windows platforms. Semi-fixes #3

Update readme.
  • Loading branch information
SamCarlberg committed Sep 13, 2016
1 parent bd9b12a commit 7c8be60
Show file tree
Hide file tree
Showing 15 changed files with 459 additions and 146 deletions.
55 changes: 33 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,25 @@ For now, this is just a CLI app with no GUI.
## Supported platforms
This currently supports

- Windows (32- and 64-bit)
- Windows (32- and 64-bit x86)
- Mac OS X 10.4 or higher
- Linux (32- and 64-bit)

ARM is not currently supported.
- Linux (32- and 64-bit x86, and ARM soft- and hard-float)

### Windows notes

This doesn't work on Windows. At all. JNI and native binaries need to be built and the install paths need to be figured out.
Artifacts are installed by default in `C:\Users\<user>\OpenCV\`. You will need to add the install locations to the `PATH` environment variable.

### Linux notes

JNI and native bindings will be installed in `/usr/local/lib` and the headers will be installed in `/usr/local/include`. If you don't have write access to these folders, you can run the installer with

`sudo java -Duser.home=$HOME -jar ...`

Make sure that `/usr/local/lib` is on `LD_LIBRARY_PATH` or the JNI bindings won't be loaded by the JVM.
Make sure that `/usr/local/lib` is on `LD_LIBRARY_PATH` or the JNI bindings won't be loaded by the JVM.

### OS X notes

Similar to Linux, but you probably won't need to use `sudo`.


## Command line arguments
Expand All @@ -37,26 +39,28 @@ Short name | Long name | Description | Argument
---|---|---|---
| | `help` | Prints the help text |
`v` | `version` | Sets the OpenCV version | The version in the format `x.x.x` e.g. `3.1.0`
`j` | `java` | Flags the Java API for install. This does _not_ install JNI bindings
`jni` | `jni` | Flags the JNI bindings for install
`h` | `headers` | Flags the C++ headers for install
`n` | `natives` | Flags the C++ native libraries for install
`j` | `java` | Flags the Java API for install. This does _not_ install JNI bindings | Install location (optional)
`jni` | `jni` | Flags the JNI bindings for install | Install location (optional)
`h` | `headers` | Flags the C++ headers for install | Install location (optional)
`n` | `natives` | Flags the C++ native libraries for install | Install location (optional)
| | `all` | Installs all OpenCV artifacts
| `o` | `overwrite` | Overwrite already installed files
| | `platform` | Download artifacts for a specific platform. They will be located in `./install` | The platform to download artifacts for

#### Options for `platform`
### Options for `platform`
```
win32
win64
osx
linux32
linux64
windows-x86
windows-x86_64
osx-x86_64
linux-x86
linux-x86_64
linux-arm
linux-armhf
```

## Usage
### Usage
```
java -jar opencv-installer --version <version> --platform <platform> --java --jni --headers --natives --overwrite
java -jar opencv-installer --version <version> --platform <platform> --java <location> --jni <location> --headers <location> --natives <location> --overwrite
```

## Using the installer in Gradle build scripts
Expand All @@ -73,16 +77,23 @@ buildscript {
}
}
import edu.wpi.first.wpilib.opencv.installer.Installer
import edu.wpi.first.wpilib.opencv.installer.PlatformDetector
def openCvPlatform = PlatformDetector.getPlatform()
def openCvVersion = "3.1.0"
def openCvVersion = '3.1.0'
dependencies {
compile group: 'org.opencv', name: 'opencv-java', version: openCvVersion
runtime group: 'org.opencv', name: 'opencv-jni', version: "${openCvPlatform}-${openCvVersion}"
...
}
task installOpenCvJni(type: Copy) {
Installer.setVersion(openCvVersion)
Installer.installJni(openCvPlatform.defaultJniLocation())
// Or manually set the install location
Installer.installJni('/usr/local/lib')
}
```

## Using the installer as Java library
Expand Down Expand Up @@ -113,11 +124,11 @@ class Main {

static {
Installer.setOpenCvVersion(Core.VERSION);
Installer.installJni(PlatformDetector.getPlatform().getDefaultJniInstallLocation());
Installer.installJni(PlatformDetector.getPlatform().defaultJniLocation());
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
}

}
```

This will install OpenCV on the current system if the JNI bindings are available for it. If there aren't any JNI bindings, an `IOException` will be thrown by the call to `Installer.installJni()` (you may wrap this in a `try-catch` block if you want to do something with this exception, such as logging it)
This will install OpenCV on the current system if the JNI bindings are available for it. If there aren't any JNI bindings, an `IOException` will be thrown by the call to `Installer.installJni()`
18 changes: 13 additions & 5 deletions src/main/java/edu/wpi/first/wpilib/opencv/installer/Installer.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
package edu.wpi.first.wpilib.opencv.installer;

import edu.wpi.first.wpilib.opencv.installer.platform.Platform;
import lombok.experimental.UtilityClass;
import org.apache.commons.cli.*;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.DefaultParser;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.MissingOptionException;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;

import java.io.File;
import java.io.FileInputStream;
Expand Down Expand Up @@ -113,28 +121,28 @@ public static void main(String[] args) throws ParseException {
System.out.println("Installing specified OpenCV components");
if (parsedArgs.hasOption("java") || parsedArgs.hasOption("all")) {
try {
installJava(parsedArgs.getOptionValue("java", platform.getDefaultJavaInstallLocation()));
installJava(parsedArgs.getOptionValue("java", platform.defaultJavaLocation()));
} catch (IOException e) {
e.printStackTrace();
}
}
if (parsedArgs.hasOption("jni") || parsedArgs.hasOption("all")) {
try {
installJni(parsedArgs.getOptionValue("jni", platform.getDefaultJniInstallLocation()));
installJni(parsedArgs.getOptionValue("jni", platform.defaultJniLocation()));
} catch (IOException e) {
e.printStackTrace();
}
}
if (parsedArgs.hasOption("headers") || parsedArgs.hasOption("all")) {
try {
installHeaders(parsedArgs.getOptionValue("headers", platform.getDefaultHeadersInstallLocation()));
installHeaders(parsedArgs.getOptionValue("headers", platform.defaultHeadersLocation()));
} catch (IOException e) {
e.printStackTrace();
}
}
if (parsedArgs.hasOption("natives") || parsedArgs.hasOption("all")) {
try {
installNatives(parsedArgs.getOptionValue("natives", platform.getDefaultNativesInstallLocation()));
installNatives(parsedArgs.getOptionValue("natives", platform.defaultNativesLocation()));
} catch (IOException e) {
e.printStackTrace();
}
Expand Down
87 changes: 0 additions & 87 deletions src/main/java/edu/wpi/first/wpilib/opencv/installer/Platform.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package edu.wpi.first.wpilib.opencv.installer;

import edu.wpi.first.wpilib.opencv.installer.platform.Platform;
import lombok.experimental.UtilityClass;

@UtilityClass
Expand All @@ -26,7 +27,7 @@ public static String getOs() throws UnsupportedOperatingSystemError {
}
String osName = System.getProperty("os.name").toLowerCase();
if (osName.contains("windows")) {
os = "win";
os = "windows";
} else if (osName.contains("macos") || osName.contains("mac os")) {
os = "osx";
} else if (osName.contains("linux")) {
Expand All @@ -40,8 +41,10 @@ public static String getOs() throws UnsupportedOperatingSystemError {
/**
* Gets the operating system architecture as one of:
* <ul>
* <li>"32"</li>
* <li>"64"</li>
* <li>"x86"</li>
* <li>"x86_64"</li>
* <li>"arm"</li>
* <li>"armhf"</li>
* </ul>
*
* @return the operating system architecture
Expand All @@ -53,9 +56,13 @@ public static String getArch() throws UnsupportedOperatingSystemError {
}
String archName = System.getProperty("os.arch");
if (archName.matches("^(i386|x86)$")) {
arch = "32";
arch = "x86";
} else if (archName.matches("$(x86_64|amd64)$")) {
arch = "64";
arch = "x86_64";
} else if (archName.matches("$(arm)^")) {
arch = "arm";
} else if (archName.matches("$(armhf)^")) {
arch = "armhf";
} else {
throw new UnsupportedOperatingSystemError("Unsupported architecture: " + archName);
}
Expand All @@ -71,33 +78,11 @@ public static Platform getPlatform() {
if (platform != null) {
return platform;
}
getOs();
getArch();
switch (os) {
case "win":
switch (arch) {
case "32":
platform = Platform.win32;
break;
case "64":
platform = Platform.win64;
break;
}
case "osx":
platform = Platform.osx;
break;
case "linux":
switch (arch) {
case "32":
platform = Platform.linux32;
break;
case "64":
platform = Platform.linux64;
break;
}
}
if (platform == null) {
throw new UnsupportedOperatingSystemError(os + arch);
final String platName = getOs() + "-" + getArch();
try {
platform = Platform.valueOf(platName);
} catch (IllegalArgumentException unsupported) {
throw new UnsupportedOperatingSystemError(unsupported.getMessage());
}
return platform;
}
Expand Down
Loading

0 comments on commit 7c8be60

Please sign in to comment.