2020-02-13 23:52:16 +00:00
|
|
|
/*
|
|
|
|
Copyright 2020 The Matrix.org Foundation C.I.C.
|
|
|
|
|
|
|
|
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 path = require('path');
|
2020-02-17 14:52:24 +00:00
|
|
|
const childProcess = require('child_process');
|
2020-02-13 23:52:16 +00:00
|
|
|
|
2020-02-15 16:52:41 +00:00
|
|
|
const fs = require('fs');
|
2020-02-13 23:52:16 +00:00
|
|
|
const fsProm = require('fs').promises;
|
|
|
|
const needle = require('needle');
|
|
|
|
const tar = require('tar');
|
|
|
|
|
|
|
|
module.exports = async function(hakEnv, moduleInfo) {
|
2020-02-17 20:10:58 +00:00
|
|
|
if (!hakEnv.isLinux()) {
|
|
|
|
await getSqlCipher(hakEnv, moduleInfo);
|
|
|
|
}
|
2020-02-13 23:52:16 +00:00
|
|
|
|
|
|
|
if (hakEnv.isWin()) {
|
2020-03-01 14:19:34 +00:00
|
|
|
await getOpenSsl(hakEnv, moduleInfo);
|
2020-02-13 23:52:16 +00:00
|
|
|
}
|
2020-02-17 14:52:24 +00:00
|
|
|
};
|
2020-02-13 23:52:16 +00:00
|
|
|
|
|
|
|
async function getSqlCipher(hakEnv, moduleInfo) {
|
2020-04-03 23:21:52 +00:00
|
|
|
const version = moduleInfo.cfg.dependencies.sqlcipher;
|
2020-04-03 22:20:56 +00:00
|
|
|
const sqlCipherDir = path.join(moduleInfo.moduleDotHakDir, `sqlcipher-${version}`);
|
2020-02-13 23:52:16 +00:00
|
|
|
|
|
|
|
let haveSqlcipher;
|
|
|
|
try {
|
|
|
|
await fsProm.stat(sqlCipherDir);
|
|
|
|
haveSqlcipher = true;
|
|
|
|
} catch (e) {
|
|
|
|
haveSqlcipher = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (haveSqlcipher) return;
|
|
|
|
|
2020-04-03 22:20:56 +00:00
|
|
|
const sqlCipherTarball = path.join(moduleInfo.moduleDotHakDir, `sqlcipher-${version}.tar.gz`);
|
2020-02-13 23:52:16 +00:00
|
|
|
let haveSqlcipherTar;
|
|
|
|
try {
|
|
|
|
await fsProm.stat(sqlCipherTarball);
|
|
|
|
haveSqlcipherTar = true;
|
|
|
|
} catch (e) {
|
|
|
|
haveSqlcipherTar = false;
|
|
|
|
}
|
|
|
|
if (!haveSqlcipherTar) {
|
2020-04-03 22:20:56 +00:00
|
|
|
const bob = needle('get', `https://github.com/sqlcipher/sqlcipher/archive/v${version}.tar.gz`, {
|
2020-02-13 23:52:16 +00:00
|
|
|
follow: 10,
|
|
|
|
output: sqlCipherTarball,
|
|
|
|
});
|
|
|
|
await bob;
|
|
|
|
}
|
|
|
|
|
2021-07-06 22:52:19 +00:00
|
|
|
// Extract the tarball to per-target directories, then we avoid cross-contaiminating archs
|
2020-02-13 23:52:16 +00:00
|
|
|
await tar.x({
|
|
|
|
file: sqlCipherTarball,
|
2021-07-06 22:52:19 +00:00
|
|
|
cwd: moduleInfo.moduleTargetDotHakDir,
|
2020-02-13 23:52:16 +00:00
|
|
|
});
|
2020-02-15 16:52:41 +00:00
|
|
|
|
|
|
|
if (hakEnv.isWin()) {
|
|
|
|
// On Windows, we need to patch the makefile because it forces TEMP_STORE to
|
|
|
|
// default to files (1) but the README specifically says you '*must*' set it
|
|
|
|
// set it to 2 (default to memory).
|
2020-04-03 22:20:56 +00:00
|
|
|
const patchFile = path.join(moduleInfo.moduleHakDir, `sqlcipher-${version}-win.patch`);
|
2020-02-15 16:52:41 +00:00
|
|
|
|
|
|
|
await new Promise((resolve, reject) => {
|
2020-02-17 14:52:24 +00:00
|
|
|
const readStream = fs.createReadStream(patchFile);
|
2020-02-15 16:52:41 +00:00
|
|
|
|
2020-02-17 14:52:24 +00:00
|
|
|
const proc = childProcess.spawn(
|
2020-02-15 16:52:41 +00:00
|
|
|
'patch',
|
|
|
|
['-p1'],
|
|
|
|
{
|
|
|
|
cwd: sqlCipherDir,
|
|
|
|
stdio: ['pipe', 'inherit', 'inherit'],
|
|
|
|
},
|
|
|
|
);
|
|
|
|
proc.on('exit', (code) => {
|
|
|
|
code ? reject(code) : resolve();
|
|
|
|
});
|
2020-02-17 14:52:24 +00:00
|
|
|
readStream.pipe(proc.stdin);
|
2020-02-15 16:52:41 +00:00
|
|
|
});
|
|
|
|
}
|
2020-02-13 23:52:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async function getOpenSsl(hakEnv, moduleInfo) {
|
2020-04-03 23:21:52 +00:00
|
|
|
const version = moduleInfo.cfg.dependencies.openssl;
|
2020-04-03 22:20:56 +00:00
|
|
|
const openSslDir = path.join(moduleInfo.moduleDotHakDir, `openssl-${version}`);
|
2020-02-13 23:52:16 +00:00
|
|
|
|
|
|
|
let haveOpenSsl;
|
|
|
|
try {
|
|
|
|
await fsProm.stat(openSslDir);
|
|
|
|
haveOpenSsl = true;
|
|
|
|
} catch (e) {
|
|
|
|
haveOpenSsl = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (haveOpenSsl) return;
|
|
|
|
|
2020-04-03 22:20:56 +00:00
|
|
|
const openSslTarball = path.join(moduleInfo.moduleDotHakDir, `openssl-${version}.tar.gz`);
|
2020-02-13 23:52:16 +00:00
|
|
|
let haveOpenSslTar;
|
|
|
|
try {
|
|
|
|
await fsProm.stat(openSslTarball);
|
|
|
|
haveOpenSslTar = true;
|
|
|
|
} catch (e) {
|
|
|
|
haveOpenSslTar = false;
|
|
|
|
}
|
|
|
|
if (!haveOpenSslTar) {
|
2020-04-03 22:20:56 +00:00
|
|
|
await needle('get', `https://www.openssl.org/source/openssl-${version}.tar.gz`, {
|
2020-02-13 23:52:16 +00:00
|
|
|
follow: 10,
|
|
|
|
output: openSslTarball,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-02-17 14:52:24 +00:00
|
|
|
console.log("extracting " + openSslTarball + " in " + moduleInfo.moduleDotHakDir);
|
2020-02-13 23:52:16 +00:00
|
|
|
await tar.x({
|
|
|
|
file: openSslTarball,
|
2021-07-06 22:52:19 +00:00
|
|
|
cwd: moduleInfo.moduleTargetDotHakDir,
|
2020-02-13 23:52:16 +00:00
|
|
|
});
|
|
|
|
}
|