gx
chenyc
2025-02-12 ea42ff3ebee1eeb3fb29423aa848a249441db81c
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
/**
 * @license
 * Copyright 2018 Google LLC. 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 os = require('os');
const fs = require('fs');
const join = require('path').join;
const module_path_napi = require('../package.json').binary.module_path;
const modulePath =
    module_path_napi.replace('{napi_build_version}', process.versions.napi);
 
/** Version of the libtensorflow shared library to depend on. */
const LIBTENSORFLOW_VERSION = '2.9.1';
 
/** Map the os.arch() to arch string in a file name */
const ARCH_MAPPING = {
  'x64': 'x86_64',
  'arm64': 'arm64'
};
/** Map the os.platform() to the platform value in a file name */
const PLATFORM_MAPPING = {
  'darwin': 'darwin',
  'linux': 'linux',
  'win32': 'windows'
};
/** The extension of a compressed file */
const PLATFORM_EXTENSION = os.platform() === 'win32' ? 'zip' : 'tar.gz';
/**
 * Current supported type, platform and architecture combinations
 * `tf-lib` represents tensorflow shared libraries and `binding` represents
 * node binding.
 */
const ALL_SUPPORTED_COMBINATION = [
  'cpu-darwin-x86_64', 'gpu-linux-x86_64', 'cpu-linux-x86_64',
  'cpu-windows-x86_64', 'gpu-windows-x86_64'
];
 
/** Get the MAJOR.MINOR-only version of libtensorflow. */
function getLibTensorFlowMajorDotMinorVersion() {
  const items = LIBTENSORFLOW_VERSION.split('.');
  if (items.length < 3) {
    throw new Error(
        `Invalid version string for libtensorflow: ${LIBTENSORFLOW_VERSION}`);
  }
  return `${items[0]}.${items[1]}`;
}
 
// Determine constants for deps folder names and destination (build) path names.
let depsLibTensorFlowName = 'libtensorflow';
let depsLibTensorFlowFrameworkName = 'libtensorflow_framework';
 
let destLibTensorFlowName = depsLibTensorFlowName;
let destLibTensorFlowFrameworkName = depsLibTensorFlowFrameworkName;
 
if (os.platform() === 'win32') {
  depsLibTensorFlowName = 'tensorflow.dll';
  depsLibTensorFlowFrameworkName = '';  // Not supported on Windows
 
  destLibTensorFlowName = depsLibTensorFlowName;
  destLibTensorFlowFrameworkName = '';  // Not supported on Windows
} else if (os.platform() === 'darwin') {
  depsLibTensorFlowName += '.dylib';
  depsLibTensorFlowFrameworkName += '.dylib';
 
  destLibTensorFlowName = depsLibTensorFlowName;
  destLibTensorFlowFrameworkName = depsLibTensorFlowFrameworkName;
} else if (os.platform() === 'linux') {
  // Linux has a hard-coded version number, make the destination name simpler:
  depsLibTensorFlowName += `.so.${LIBTENSORFLOW_VERSION}`;
  depsLibTensorFlowFrameworkName += `.so.${LIBTENSORFLOW_VERSION}`;
  destLibTensorFlowName += '.so';
  destLibTensorFlowFrameworkName += '.so';
} else {
  throw new Error('Unsupported platform: ' + os.platform());
}
 
const depsPath = join(__dirname, '..', 'deps');
const depsLibPath = join(depsPath, 'lib');
 
const depsLibTensorFlowPath = join(depsLibPath, depsLibTensorFlowName);
const depsLibTensorFlowFrameworkPath =
    join(depsLibPath, depsLibTensorFlowFrameworkName);
 
// Get information for custom binary
const CUSTOM_BINARY_FILENAME = 'custom-binary.json';
function loadCustomBinary() {
  const cfg = join(__dirname, CUSTOM_BINARY_FILENAME);
  return fs.existsSync(cfg) ? require(cfg) : {};
}
const customBinaries = loadCustomBinary();
 
module.exports = {
  depsPath,
  depsLibPath,
  depsLibTensorFlowFrameworkName,
  depsLibTensorFlowFrameworkPath,
  depsLibTensorFlowName,
  depsLibTensorFlowPath,
  destLibTensorFlowFrameworkName,
  destLibTensorFlowName,
  getLibTensorFlowMajorDotMinorVersion,
  modulePath,
  LIBTENSORFLOW_VERSION,
  ARCH_MAPPING,
  PLATFORM_MAPPING,
  PLATFORM_EXTENSION,
  ALL_SUPPORTED_COMBINATION,
  customTFLibUri : customBinaries['tf-lib'],
  customAddon : customBinaries['addon']
};