11const opencvBuild = require ( `@nut-tree/opencv-build-${ process . platform } ` )
22const { resolvePath } = require ( '../lib/commons' )
33const fs = require ( 'fs' )
4+ const { basename } = require ( "path" ) ;
45
56const libDir = resolvePath ( opencvBuild . opencvLibDir ) ;
67
@@ -23,12 +24,24 @@ const inc = [
2324 resolvePath ( opencvBuild . opencv4Include )
2425] ;
2526
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+ }
2639const libs = opencvBuild . isWin ( )
2740 ? 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 )
3245
3346module . exports = {
3447 OPENCV4NODEJS_LIBRARIES : ( ) => libs . join ( "\n" ) ,
0 commit comments