@@ -30,23 +30,32 @@ const {
3030 modulePath
3131} = require ( './deps-constants.js' ) ;
3232const resources = require ( './resources' ) ;
33- const { addonName} = require ( './get-addon-name.js' ) ;
33+ const {
34+ addonName,
35+ customTFLibUri,
36+ customAddon,
37+ PLATFORM_MAPPING ,
38+ ARCH_MAPPING ,
39+ PLATFORM_EXTENSION ,
40+ ALL_SUPPORTED_COMBINATION ,
41+ } = require ( './get-addon-name.js' ) ;
3442
3543const exists = util . promisify ( fs . exists ) ;
3644const mkdir = util . promisify ( fs . mkdir ) ;
3745const rename = util . promisify ( fs . rename ) ;
3846const rimrafPromise = util . promisify ( rimraf ) ;
3947
4048const BASE_URI =
41- 'https://storage.googleapis.com/tensorflow/libtensorflow/libtensorflow-' ;
42- const CPU_DARWIN = `cpu-darwin-x86_64-${ LIBTENSORFLOW_VERSION } .tar.gz` ;
43- const CPU_LINUX = `cpu-linux-x86_64-${ LIBTENSORFLOW_VERSION } .tar.gz` ;
44- const GPU_LINUX = `gpu-linux-x86_64-${ LIBTENSORFLOW_VERSION } .tar.gz` ;
45- const CPU_WINDOWS = `cpu-windows-x86_64-${ LIBTENSORFLOW_VERSION } .zip` ;
46- const GPU_WINDOWS = `gpu-windows-x86_64-${ LIBTENSORFLOW_VERSION } .zip` ;
49+ 'https://storage.googleapis.com/tensorflow/libtensorflow/libtensorflow-' ;
4750
4851const platform = os . platform ( ) ;
52+ // Use windows path
53+ if ( platform === 'win32' ) {
54+ path = path . win32 ;
55+ }
4956let libType = process . argv [ 2 ] === undefined ? 'cpu' : process . argv [ 2 ] ;
57+ const system = `${ libType } -${ PLATFORM_MAPPING [ platform ] } -` +
58+ `${ ARCH_MAPPING [ os . arch ( ) ] } ` ;
5059let forceDownload = process . argv [ 3 ] === undefined ? undefined : process . argv [ 3 ] ;
5160
5261let packageJsonFile ;
@@ -56,14 +65,20 @@ async function setPackageJsonFile() {
5665 JSON . parse ( fs . readFileSync ( `${ __dirname } /../package.json` ) . toString ( ) ) ;
5766}
5867
59- async function updateAddonName ( ) {
60- packageJsonFile [ 'binary' ] [ 'package_name' ] = addonName ;
68+ function updateAddonName ( ) {
69+ const origBinary = JSON . parse ( JSON . stringify ( packageJsonFile [ 'binary' ] ) ) ;
70+ if ( customAddon !== undefined ) {
71+ Object . assign ( packageJsonFile [ 'binary' ] , customAddon ) ;
72+ } else {
73+ packageJsonFile [ 'binary' ] [ 'package_name' ] = addonName ;
74+ }
6175 const stringFile = JSON . stringify ( packageJsonFile , null , 2 ) ;
6276 fs . writeFileSync ( ( `${ __dirname } /../package.json` ) , stringFile ) ;
77+ return origBinary ;
6378}
6479
65- async function revertAddonName ( ) {
66- delete packageJsonFile [ 'binary' ] [ 'package_name' ] ;
80+ function revertAddonName ( orig ) {
81+ packageJsonFile [ 'binary' ] = orig ;
6782 const stringFile = JSON . stringify ( packageJsonFile , null , 2 ) . concat ( '\n' ) ;
6883 fs . writeFileSync ( ( `${ __dirname } /../package.json` ) , stringFile ) ;
6984}
@@ -72,33 +87,16 @@ async function revertAddonName() {
7287 * Returns the libtensorflow hosted path of the current platform.
7388 */
7489function getPlatformLibtensorflowUri ( ) {
75- let targetUri = BASE_URI ;
76- if ( platform === 'linux' ) {
77- if ( os . arch ( ) === 'arm' ) {
78- // TODO(kreeger): Handle arm64 as well:
79- targetUri =
80- 'https://storage.googleapis.com/tf-builds/libtensorflow_r1_14_linux_arm.tar.gz' ;
81- } else {
82- if ( libType === 'gpu' ) {
83- targetUri += GPU_LINUX ;
84- } else {
85- targetUri += CPU_LINUX ;
86- }
87- }
88- } else if ( platform === 'darwin' ) {
89- targetUri += CPU_DARWIN ;
90- } else if ( platform === 'win32' ) {
91- // Use windows path
92- path = path . win32 ;
93- if ( libType === 'gpu' ) {
94- targetUri += GPU_WINDOWS ;
95- } else {
96- targetUri += CPU_WINDOWS ;
97- }
98- } else {
99- throw new Error ( `Unsupported platform: ${ platform } ` ) ;
90+
91+ if ( customTFLibUri !== undefined ) {
92+ return customTFLibUri ;
93+ }
94+
95+ if ( ALL_SUPPORTED_COMBINATION . indexOf ( system ) === - 1 ) {
96+ throw new Error ( `Unsupported system: ${ libType } -${ platform } -${ os . arch ( ) } ` ) ;
10097 }
101- return targetUri ;
98+
99+ return `${ BASE_URI } ${ system } -${ LIBTENSORFLOW_VERSION } .${ PLATFORM_EXTENSION } ` ;
102100}
103101
104102/**
@@ -157,15 +155,19 @@ async function downloadLibtensorflow(callback) {
157155 */
158156async function build ( ) {
159157 console . error ( '* Building TensorFlow Node.js bindings' ) ;
160- cp . exec ( 'node-pre-gyp install --fallback-to-build' , ( err ) => {
158+ let buildOption = '--fallback-to-build' ;
159+ if ( customTFLibUri !== undefined && customAddon === undefined ) {
160+ // Has custom tensorflow shared libs but no addon. Then build it from source
161+ buildOption = '--build-from-source' ;
162+ }
163+ cp . exec ( `node-pre-gyp install ${ buildOption } ` , ( err ) => {
161164 if ( err ) {
162165 console . log ( 'node-pre-gyp install failed with error: ' + err ) ;
163166 }
164167 if ( platform === 'win32' ) {
165168 // Move libtensorflow to module path, where tfjs_binding.node locates.
166169 cp . exec ( 'node scripts/deps-stage.js symlink ' + modulePath ) ;
167170 }
168- revertAddonName ( ) ;
169171 } ) ;
170172}
171173
@@ -176,7 +178,7 @@ async function run() {
176178 // Load package.json file
177179 setPackageJsonFile ( ) ;
178180 // Update addon name in package.json file
179- await updateAddonName ( ) ;
181+ const origBinary = updateAddonName ( ) ;
180182 // First check if deps library exists:
181183 if ( forceDownload !== 'download' && await exists ( depsLibTensorFlowPath ) ) {
182184 // Library has already been downloaded, then compile and simlink:
@@ -186,6 +188,7 @@ async function run() {
186188 await cleanDeps ( ) ;
187189 await downloadLibtensorflow ( build ) ;
188190 }
191+ revertAddonName ( origBinary ) ;
189192}
190193
191194run ( ) ;
0 commit comments