1
1
const opencvBuild = require ( `@nut-tree/opencv-build-${ process . platform } ` )
2
2
const { resolvePath } = require ( '../lib/commons' )
3
3
const fs = require ( 'fs' )
4
+ const { basename } = require ( "path" ) ;
4
5
5
6
const libDir = resolvePath ( opencvBuild . opencvLibDir ) ;
6
7
@@ -23,12 +24,24 @@ const inc = [
23
24
resolvePath ( opencvBuild . opencv4Include )
24
25
] ;
25
26
27
+ // linkLib produces linker flags for GNU ld and BSD ld
28
+ // It generates linker flags based on the libPath, which make dealing with version numbers in lib names easier
29
+ // On Linux, it passes the full path via -l:/path/to/lib which links against the given file
30
+ // On macOS it strips the *.dylib suffix and the lib prefix and passes the result to via -l
31
+ // This results in e.g. -lopencv_world.4.1
32
+ const linkLib = ( lib ) => {
33
+ if ( opencvBuild . isOSX ( ) ) {
34
+ return `-l${ basename ( lib . libPath , ".dylib" ) . replace ( "lib" , "" ) } ` ;
35
+ } else {
36
+ return `-l:${ basename ( lib . libPath ) } ` ;
37
+ }
38
+ }
26
39
const libs = opencvBuild . isWin ( )
27
40
? libsFoundInDir . map ( lib => resolvePath ( lib . libPath ) )
28
- : // dynamically link libs if not on windows
29
- [ "-L" + libDir ]
30
- . concat ( libsFoundInDir . map ( lib => "-lopencv_" + lib . opencvModule ) )
31
- . concat ( " -Wl,-rpath," + libDir ) ;
41
+ // dynamically link libs if not on windows
42
+ : [ '-L' + libDir ]
43
+ . concat ( libsFoundInDir . map ( lib => linkLib ( lib ) ) )
44
+ . concat ( ' -Wl,-rpath,' + libDir )
32
45
33
46
module . exports = {
34
47
OPENCV4NODEJS_LIBRARIES : ( ) => libs . join ( "\n" ) ,
0 commit comments