Skip to content

Commit 7c8be60

Browse files
committed
Refactor platforms
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.
1 parent bd9b12a commit 7c8be60

File tree

15 files changed

+459
-146
lines changed

15 files changed

+459
-146
lines changed

README.md

Lines changed: 33 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -12,23 +12,25 @@ For now, this is just a CLI app with no GUI.
1212
## Supported platforms
1313
This currently supports
1414

15-
- Windows (32- and 64-bit)
15+
- Windows (32- and 64-bit x86)
1616
- Mac OS X 10.4 or higher
17-
- Linux (32- and 64-bit)
18-
19-
ARM is not currently supported.
17+
- Linux (32- and 64-bit x86, and ARM soft- and hard-float)
2018

2119
### Windows notes
2220

23-
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.
21+
Artifacts are installed by default in `C:\Users\<user>\OpenCV\`. You will need to add the install locations to the `PATH` environment variable.
2422

2523
### Linux notes
2624

2725
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
2826

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

31-
Make sure that `/usr/local/lib` is on `LD_LIBRARY_PATH` or the JNI bindings won't be loaded by the JVM.
29+
Make sure that `/usr/local/lib` is on `LD_LIBRARY_PATH` or the JNI bindings won't be loaded by the JVM.
30+
31+
### OS X notes
32+
33+
Similar to Linux, but you probably won't need to use `sudo`.
3234

3335

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

48-
#### Options for `platform`
50+
### Options for `platform`
4951
```
50-
win32
51-
win64
52-
osx
53-
linux32
54-
linux64
52+
windows-x86
53+
windows-x86_64
54+
osx-x86_64
55+
linux-x86
56+
linux-x86_64
57+
linux-arm
58+
linux-armhf
5559
```
5660

57-
## Usage
61+
### Usage
5862
```
59-
java -jar opencv-installer --version <version> --platform <platform> --java --jni --headers --natives --overwrite
63+
java -jar opencv-installer --version <version> --platform <platform> --java <location> --jni <location> --headers <location> --natives <location> --overwrite
6064
```
6165

6266
## Using the installer in Gradle build scripts
@@ -73,16 +77,23 @@ buildscript {
7377
}
7478
}
7579
80+
import edu.wpi.first.wpilib.opencv.installer.Installer
7681
import edu.wpi.first.wpilib.opencv.installer.PlatformDetector
7782
7883
def openCvPlatform = PlatformDetector.getPlatform()
79-
def openCvVersion = "3.1.0"
84+
def openCvVersion = '3.1.0'
8085
8186
dependencies {
8287
compile group: 'org.opencv', name: 'opencv-java', version: openCvVersion
83-
runtime group: 'org.opencv', name: 'opencv-jni', version: "${openCvPlatform}-${openCvVersion}"
8488
...
8589
}
90+
91+
task installOpenCvJni(type: Copy) {
92+
Installer.setVersion(openCvVersion)
93+
Installer.installJni(openCvPlatform.defaultJniLocation())
94+
// Or manually set the install location
95+
Installer.installJni('/usr/local/lib')
96+
}
8697
```
8798

8899
## Using the installer as Java library
@@ -113,11 +124,11 @@ class Main {
113124

114125
static {
115126
Installer.setOpenCvVersion(Core.VERSION);
116-
Installer.installJni(PlatformDetector.getPlatform().getDefaultJniInstallLocation());
127+
Installer.installJni(PlatformDetector.getPlatform().defaultJniLocation());
117128
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
118129
}
119130

120131
}
121132
```
122133

123-
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)
134+
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()`

src/main/java/edu/wpi/first/wpilib/opencv/installer/Installer.java

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
11
package edu.wpi.first.wpilib.opencv.installer;
22

3+
import edu.wpi.first.wpilib.opencv.installer.platform.Platform;
34
import lombok.experimental.UtilityClass;
4-
import org.apache.commons.cli.*;
5+
import org.apache.commons.cli.CommandLine;
6+
import org.apache.commons.cli.CommandLineParser;
7+
import org.apache.commons.cli.DefaultParser;
8+
import org.apache.commons.cli.HelpFormatter;
9+
import org.apache.commons.cli.MissingOptionException;
10+
import org.apache.commons.cli.Option;
11+
import org.apache.commons.cli.Options;
12+
import org.apache.commons.cli.ParseException;
513

614
import java.io.File;
715
import java.io.FileInputStream;
@@ -113,28 +121,28 @@ public static void main(String[] args) throws ParseException {
113121
System.out.println("Installing specified OpenCV components");
114122
if (parsedArgs.hasOption("java") || parsedArgs.hasOption("all")) {
115123
try {
116-
installJava(parsedArgs.getOptionValue("java", platform.getDefaultJavaInstallLocation()));
124+
installJava(parsedArgs.getOptionValue("java", platform.defaultJavaLocation()));
117125
} catch (IOException e) {
118126
e.printStackTrace();
119127
}
120128
}
121129
if (parsedArgs.hasOption("jni") || parsedArgs.hasOption("all")) {
122130
try {
123-
installJni(parsedArgs.getOptionValue("jni", platform.getDefaultJniInstallLocation()));
131+
installJni(parsedArgs.getOptionValue("jni", platform.defaultJniLocation()));
124132
} catch (IOException e) {
125133
e.printStackTrace();
126134
}
127135
}
128136
if (parsedArgs.hasOption("headers") || parsedArgs.hasOption("all")) {
129137
try {
130-
installHeaders(parsedArgs.getOptionValue("headers", platform.getDefaultHeadersInstallLocation()));
138+
installHeaders(parsedArgs.getOptionValue("headers", platform.defaultHeadersLocation()));
131139
} catch (IOException e) {
132140
e.printStackTrace();
133141
}
134142
}
135143
if (parsedArgs.hasOption("natives") || parsedArgs.hasOption("all")) {
136144
try {
137-
installNatives(parsedArgs.getOptionValue("natives", platform.getDefaultNativesInstallLocation()));
145+
installNatives(parsedArgs.getOptionValue("natives", platform.defaultNativesLocation()));
138146
} catch (IOException e) {
139147
e.printStackTrace();
140148
}

src/main/java/edu/wpi/first/wpilib/opencv/installer/Platform.java

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

src/main/java/edu/wpi/first/wpilib/opencv/installer/PlatformDetector.java

Lines changed: 17 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package edu.wpi.first.wpilib.opencv.installer;
22

3+
import edu.wpi.first.wpilib.opencv.installer.platform.Platform;
34
import lombok.experimental.UtilityClass;
45

56
@UtilityClass
@@ -26,7 +27,7 @@ public static String getOs() throws UnsupportedOperatingSystemError {
2627
}
2728
String osName = System.getProperty("os.name").toLowerCase();
2829
if (osName.contains("windows")) {
29-
os = "win";
30+
os = "windows";
3031
} else if (osName.contains("macos") || osName.contains("mac os")) {
3132
os = "osx";
3233
} else if (osName.contains("linux")) {
@@ -40,8 +41,10 @@ public static String getOs() throws UnsupportedOperatingSystemError {
4041
/**
4142
* Gets the operating system architecture as one of:
4243
* <ul>
43-
* <li>"32"</li>
44-
* <li>"64"</li>
44+
* <li>"x86"</li>
45+
* <li>"x86_64"</li>
46+
* <li>"arm"</li>
47+
* <li>"armhf"</li>
4548
* </ul>
4649
*
4750
* @return the operating system architecture
@@ -53,9 +56,13 @@ public static String getArch() throws UnsupportedOperatingSystemError {
5356
}
5457
String archName = System.getProperty("os.arch");
5558
if (archName.matches("^(i386|x86)$")) {
56-
arch = "32";
59+
arch = "x86";
5760
} else if (archName.matches("$(x86_64|amd64)$")) {
58-
arch = "64";
61+
arch = "x86_64";
62+
} else if (archName.matches("$(arm)^")) {
63+
arch = "arm";
64+
} else if (archName.matches("$(armhf)^")) {
65+
arch = "armhf";
5966
} else {
6067
throw new UnsupportedOperatingSystemError("Unsupported architecture: " + archName);
6168
}
@@ -71,33 +78,11 @@ public static Platform getPlatform() {
7178
if (platform != null) {
7279
return platform;
7380
}
74-
getOs();
75-
getArch();
76-
switch (os) {
77-
case "win":
78-
switch (arch) {
79-
case "32":
80-
platform = Platform.win32;
81-
break;
82-
case "64":
83-
platform = Platform.win64;
84-
break;
85-
}
86-
case "osx":
87-
platform = Platform.osx;
88-
break;
89-
case "linux":
90-
switch (arch) {
91-
case "32":
92-
platform = Platform.linux32;
93-
break;
94-
case "64":
95-
platform = Platform.linux64;
96-
break;
97-
}
98-
}
99-
if (platform == null) {
100-
throw new UnsupportedOperatingSystemError(os + arch);
81+
final String platName = getOs() + "-" + getArch();
82+
try {
83+
platform = Platform.valueOf(platName);
84+
} catch (IllegalArgumentException unsupported) {
85+
throw new UnsupportedOperatingSystemError(unsupported.getMessage());
10186
}
10287
return platform;
10388
}

0 commit comments

Comments
 (0)