Skip to content

Commit

Permalink
add Platfrm class
Browse files Browse the repository at this point in the history
  • Loading branch information
UrielCh committed Apr 22, 2024
1 parent f0b0f83 commit 914f8d0
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 44 deletions.
23 changes: 5 additions & 18 deletions src/OpenCVBuildEnv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {
import { ALL_OPENCV_MODULES } from "./misc";
import pc from "picocolors";
import * as detector from "./helper/detect";
import { getEnv, setEnv } from "./env";
import { getEnv, Platfrm, setEnv } from "./env";
import Log from "./Log";
import StaticTools from "./StaticTools";

Expand Down Expand Up @@ -99,7 +99,7 @@ export default class OpenCVBuildEnv
public buildRoot: string;
// Path to find package.json legacy option
public packageRoot: string;
protected _platform: NodeJS.Platform;
// protected _platform: NodeJS.Platform;
private no_autobuild: string;

private getExpectedVersion(defaultVersion?: string): string {
Expand Down Expand Up @@ -146,7 +146,6 @@ export default class OpenCVBuildEnv

constructor(private opts = {} as OpenCVBuildEnvParams) {
this.prebuild = opts.prebuild;
this._platform = process.platform;
this.packageRoot = opts.rootcwd || getEnv("INIT_CWD") || process.cwd();
this.buildRoot = StaticTools.getBuildDir(opts);
// get project Root path to looks for package.json for opencv4nodejs section
Expand Down Expand Up @@ -685,18 +684,6 @@ export default class OpenCVBuildEnv
return optArgs;
}

public get platform(): NodeJS.Platform {
return this._platform;
}

public setPlatform(p: NodeJS.Platform): void {
this._platform = p;
}

public get isWin(): boolean {
return this.platform === "win32";
}

public get rootDir(): string {
return this.buildRoot;
}
Expand Down Expand Up @@ -744,23 +731,23 @@ export default class OpenCVBuildEnv
this.getReady();
const candidat = getEnv("OPENCV_LIB_DIR");
if (candidat) return candidat;
return this.isWin
return Platfrm.isWindows
? path.join(this.opencvBuild, "lib/Release")
: path.join(this.opencvBuild, "lib");
}
public get opencvBinDir(): string {
this.getReady();
const candidat = getEnv("OPENCV_BIN_DIR");
if (candidat) return candidat;
return this.isWin
return Platfrm.isWindows
? path.join(this.opencvBuild, "bin/Release")
: path.join(this.opencvBuild, "bin");
}
public get autoBuildFile(): string {
return path.join(this.opencvRoot, "auto-build.json");
}
public get autoBuildLog(): string {
if (this.isWin) {
if (Platfrm.isWindows) {
return path.join(this.opencvRoot, "build-cmd.bat");
} else {
return path.join(this.opencvRoot, "build-cmd.sh");
Expand Down
19 changes: 19 additions & 0 deletions src/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,22 @@ export function getDirname(): string {
// @ts-ignore ESM code
return new URL(".", import.meta.url).pathname;
}


export class Platfrm {
public static theOS: string = process.platform;//Deno.build.os;

public static changeOS(os: "windows" | "linux" | "darwin") {
Platfrm.theOS = os;
}
public static get isWindows() {
return Platfrm.theOS.startsWith("win"); // === 'windows';
}
public static get isLinux() {
return Platfrm.theOS === "linux";
}
public static get isMac() {
return Platfrm.theOS === "darwin";
}
}

12 changes: 5 additions & 7 deletions src/getLibsFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import path from "node:path";
import OpenCVBuilder from "./OpenCVBuilder";
import type { OpencvModule } from "./types";
import { OpencvModulesType } from "./misc";
import { Platfrm } from "./env";

export class getLibsFactory {
libFiles: string[] = [];
Expand All @@ -29,21 +30,18 @@ export class getLibsFactory {
* @returns current OS prefix
*/
get getLibPrefix(): string {
return this.builder.env.isWin ? "opencv_" : "libopencv_";
return Platfrm.isWindows ? "opencv_" : "libopencv_";
}

/**
* @returns lib extention based on current OS
*/
get getLibSuffix(): "lib" | "dylib" | "so" {
switch (this.builder.env.platform) {
case "win32":
if (Platfrm.isWindows)
return "lib";
case "darwin":
if (Platfrm.isMac)
return "dylib";
default:
return "so";
}
return "so";
}

/**
Expand Down
23 changes: 10 additions & 13 deletions src/helper/detect.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import fs from "node:fs";
import { globSync } from "glob";
import { highlight } from "../utils";
import { setEnv } from "../env";
import { Platfrm, setEnv } from "../env";

export const summery = new Set<string>();

Expand All @@ -28,9 +28,8 @@ export function detect(): {
// detect_OPENCV_INCLUDE_DIR

export function detectBinDir(): string {
const os = process.platform;
// chocolatey
if (os === "win32") {
if (Platfrm.isWindows) {
const lookup = "c:/tools/opencv/build/x64/vc*/bin";
// const candidates = ["c:\\tools\\opencv\\build\\x64\\vc14\\bin", "c:\\tools\\opencv\\build\\x64\\vc16\\bin"];
const candidates = globSync(lookup);
Expand All @@ -49,15 +48,15 @@ export function detectBinDir(): string {
}`,
);
}
} else if (os === "linux") {
} else if (Platfrm.isLinux) {
const candidate = "/usr/bin/";
if (fs.existsSync(candidate)) {
summery.add("OPENCV_BIN_DIR resolved");
return candidate;
} else {
summery.add(`failed to resolve OPENCV_BIN_DIR from ${candidate}`);
}
} else if (os === "darwin") {
} else if (Platfrm.isMac) {
const lookups = [
"/opt/homebrew/Cellar/opencv/*/bin",
"/usr/local/Cellar/opencv/*/bin",
Expand All @@ -79,8 +78,7 @@ export function detectBinDir(): string {
}

export function detectLibDir(): string {
const os = process.platform;
if (os === "win32") {
if (Platfrm.isWindows) {
// chocolatey
const lookup = "c:/tools/opencv/build/x64/vc*/lib";
// const candidates = ["c:\\tools\\opencv\\build\\x64\\vc14\\lib", "c:\\tools\\opencv\\build\\x64\\vc16\\lib"]
Expand All @@ -100,7 +98,7 @@ export function detectLibDir(): string {
}`,
);
}
} else if (os === "linux") {
} else if (Platfrm.isLinux) {
const lookup = "/usr/lib/*-linux-gnu";
// tiny-blob need to be fix bypassing th issue
//const [candidate] = fs.readdirSync('/usr/lib/').filter((a: string) => a.endsWith('-linux-gnu')).map(a => `/usr/lib/${a}`);
Expand All @@ -111,7 +109,7 @@ export function detectLibDir(): string {
} else {
summery.add(`failed to resolve OPENCV_LIB_DIR from ${lookup}`);
}
} else if (os === "darwin") {
} else if (Platfrm.isMac) {
const lookups = [
"/opt/homebrew/Cellar/opencv/*/lib",
"/usr/local/Cellar/opencv/*/lib",
Expand All @@ -137,8 +135,7 @@ export function detectLibDir(): string {
* detect OPENCV_INCLUDE_DIR
*/
export function detectIncludeDir(): string {
const os = process.platform;
if (os === "win32") {
if (Platfrm.isWindows) {
// chocolatey
const candidate = "c:\\tools\\opencv\\build\\include";
if (fs.existsSync(candidate)) {
Expand All @@ -147,15 +144,15 @@ export function detectIncludeDir(): string {
} else {
summery.add(`failed to resolve OPENCV_INCLUDE_DIR from ${candidate}`);
}
} else if (os === "linux") {
} else if (Platfrm.isLinux) {
const candidate = "/usr/include/opencv4/";
if (fs.existsSync(candidate)) {
summery.add(`OPENCV_INCLUDE_DIR resolved as ${candidate}`);
return candidate;
} else {
summery.add(`failed to resolve OPENCV_INCLUDE_DIR from ${candidate}`);
}
} else if (os === "darwin") {
} else if (Platfrm.isMac) {
const lookups = [
"/opt/homebrew/Cellar/opencv/*/include",
"/usr/local/Cellar/opencv/*/include",
Expand Down
8 changes: 4 additions & 4 deletions src/setupOpencv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import log from "npmlog";
import { rimraf } from "rimraf";
import { OPENCV_PATHS_ENV } from "./misc";
import path from "path";
import { getEnv } from "./env";
import { getEnv, Platfrm } from "./env";

export class SetupOpencv {
constructor(private readonly builder: OpenCVBuilder) {}
Expand Down Expand Up @@ -104,7 +104,7 @@ export class SetupOpencv {
}

private async getMsbuildIfWin(): Promise<PathVersion | undefined> {
if (this.builder.env.isWin) {
if (Platfrm.isWindows) {
const msbuilds = await findMSBuild();
if (msbuilds.length > 1) {
log.warn(
Expand Down Expand Up @@ -203,7 +203,7 @@ export class SetupOpencv {
let cMakeFlags: string[] = [];
let msbuildPath = "";
// Get cmake flags here to check for CUDA early on instead of the start of the building process
if (env.isWin) {
if (Platfrm.isWindows) {
if (!msbuild) {
throw Error("Error getting Ms Build info");
}
Expand Down Expand Up @@ -235,7 +235,7 @@ export class SetupOpencv {
for (const k of OPENCV_PATHS_ENV) {
const v = getEnv(k);
if (v) {
const setEnv = (process.platform === "win32") ? "$Env:" : "export ";
const setEnv = Platfrm.isWindows ? "$Env:" : "export ";
this.execLog.push(`${setEnv}${k}=${protect(v)}`);
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { EOL } from "node:os";
import path from "node:path";
import log from "npmlog";
import pc from "picocolors";
import { getEnv } from "./env";
import { getEnv, Platfrm } from "./env";

/**
* excape spaces for shell execution
Expand Down Expand Up @@ -215,7 +215,7 @@ export function isCudaAvailable(): boolean {
return cached_cuda;
}
log.info("install", "Check if CUDA is available & what version...");
if (process.platform == "win32") {
if (Platfrm.isWindows) {
try {
requireCmdSync("nvcc --version", "CUDA availability check");
// return true;
Expand Down

0 comments on commit 914f8d0

Please sign in to comment.