forked from tensorflow/tfjs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.js
More file actions
213 lines (193 loc) · 6.75 KB
/
install.js
File metadata and controls
213 lines (193 loc) · 6.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/**
* @license
* Copyright 2019 Google Inc. All Rights Reserved.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* =============================================================================
*/
const fs = require('fs');
let path = require('path');
const rimraf = require('rimraf');
const util = require('util');
const cp = require('child_process');
const os = require('os');
const {
depsPath,
depsLibPath,
depsLibTensorFlowPath,
getLibTensorFlowMajorDotMinorVersion,
LIBTENSORFLOW_VERSION,
modulePath
} = require('./deps-constants.js');
const resources = require('./resources');
const {addonName} = require('./get-addon-name.js');
const exists = util.promisify(fs.exists);
const mkdir = util.promisify(fs.mkdir);
const rename = util.promisify(fs.rename);
const rimrafPromise = util.promisify(rimraf);
const BASE_URI =
'https://storage.googleapis.com/tensorflow/libtensorflow/libtensorflow-';
const CPU_DARWIN = `cpu-darwin-x86_64-${LIBTENSORFLOW_VERSION}.tar.gz`;
const CPU_LINUX = `cpu-linux-x86_64-${LIBTENSORFLOW_VERSION}.tar.gz`;
const GPU_LINUX = `gpu-linux-x86_64-${LIBTENSORFLOW_VERSION}.tar.gz`;
const CPU_WINDOWS = `cpu-windows-x86_64-${LIBTENSORFLOW_VERSION}.zip`;
const GPU_WINDOWS = `gpu-windows-x86_64-${LIBTENSORFLOW_VERSION}.zip`;
// TODO(kreeger): Update to TensorFlow 1.13:
// https://github.com/tensorflow/tfjs/issues/1369
const TF_WIN_HEADERS_URI =
`https://storage.googleapis.com/tf-builds/tensorflow-headers-` +
`${getLibTensorFlowMajorDotMinorVersion()}.zip`;
const platform = os.platform();
let libType = process.argv[2] === undefined ? 'cpu' : process.argv[2];
let forceDownload = process.argv[3] === undefined ? undefined : process.argv[3];
let packageJsonFile;
async function setPackageJsonFile() {
packageJsonFile =
JSON.parse(fs.readFileSync(`${__dirname}/../package.json`).toString());
}
async function updateAddonName() {
packageJsonFile['binary']['package_name'] = addonName;
const stringFile = JSON.stringify(packageJsonFile, null, 2);
fs.writeFileSync((`${__dirname}/../package.json`), stringFile);
}
async function revertAddonName() {
delete packageJsonFile['binary']['package_name'];
const stringFile = JSON.stringify(packageJsonFile, null, 2).concat('\n');
fs.writeFileSync((`${__dirname}/../package.json`), stringFile);
}
/**
* Returns the libtensorflow hosted path of the current platform.
*/
function getPlatformLibtensorflowUri() {
let targetUri = BASE_URI;
if (platform === 'linux') {
if (os.arch() === 'arm') {
// TODO(kreeger): Handle arm64 as well:
targetUri =
'https://storage.googleapis.com/tf-builds/libtensorflow_r1_14_linux_arm.tar.gz';
} else {
if (libType === 'gpu') {
targetUri += GPU_LINUX;
} else {
targetUri += CPU_LINUX;
}
}
} else if (platform === 'darwin') {
targetUri += CPU_DARWIN;
} else if (platform === 'win32') {
// Use windows path
path = path.win32;
if (libType === 'gpu') {
targetUri += GPU_WINDOWS;
} else {
targetUri += CPU_WINDOWS;
}
} else {
throw new Error(`Unsupported platform: ${platform}`);
}
return targetUri;
}
/**
* Ensures a directory exists, creates as needed.
*/
async function ensureDir(dirPath) {
if (!await exists(dirPath)) {
await mkdir(dirPath);
}
}
/**
* Deletes the deps directory if it exists, and creates a fresh deps folder.
*/
async function cleanDeps() {
if (await exists(depsPath)) {
await rimrafPromise(depsPath);
}
await mkdir(depsPath);
}
/**
* Downloads libtensorflow and notifies via a callback when unpacked.
*/
async function downloadLibtensorflow(callback) {
// Ensure dependencies staged directory is available:
await ensureDir(depsPath);
console.warn('* Downloading libtensorflow');
resources.downloadAndUnpackResource(
getPlatformLibtensorflowUri(), depsPath, async () => {
if (platform === 'win32') {
// Some windows libtensorflow zip files are missing structure and the
// eager headers. Check, restructure, and download resources as
// needed.
const depsIncludePath = path.join(depsPath, 'include');
if (!await exists(depsLibTensorFlowPath)) {
// Verify that tensorflow.dll exists
const libtensorflowDll = path.join(depsPath, 'tensorflow.dll');
if (!await exists(libtensorflowDll)) {
throw new Error('Could not find libtensorflow.dll');
}
await ensureDir(depsLibPath);
await rename(libtensorflowDll, depsLibTensorFlowPath);
}
// The shipped headers for Windows libtensorflow are old - remove and
// download the latest:
if (await exists(depsIncludePath)) {
await rimrafPromise(depsIncludePath);
}
// Download the C headers only and unpack:
resources.downloadAndUnpackResource(
TF_WIN_HEADERS_URI, depsPath, () => {
if (callback !== undefined) {
callback();
}
});
} else {
// No other work is required on other platforms.
if (callback !== undefined) {
callback();
}
}
});
}
/**
* Calls node-gyp for Node.js Tensorflow binding after lib is downloaded.
*/
async function build() {
console.error('* Building TensorFlow Node.js bindings');
cp.exec('node-pre-gyp install --fallback-to-build', (err) => {
if (err) {
console.log('node-pre-gyp install failed with error: ' + err);
}
if (platform === 'win32') {
// Move libtensorflow to module path, where tfjs_binding.node locates.
cp.exec('node scripts/deps-stage.js symlink ' + modulePath);
}
revertAddonName();
});
}
/**
* Ensures libtensorflow requirements are met for building the binding.
*/
async function run() {
// Load package.json file
setPackageJsonFile();
// Update addon name in package.json file
await updateAddonName();
// First check if deps library exists:
if (forceDownload !== 'download' && await exists(depsLibTensorFlowPath)) {
// Library has already been downloaded, then compile and simlink:
await build();
} else {
// Library has not been downloaded, download, then compile and symlink:
await cleanDeps();
await downloadLibtensorflow(build);
}
}
run();