Skip to content

Commit

Permalink
feat(macos): build universal kubo binary
Browse files Browse the repository at this point in the history
  • Loading branch information
lidel committed May 16, 2024
1 parent b2edb4d commit 348258f
Show file tree
Hide file tree
Showing 4 changed files with 135 additions and 57 deletions.
79 changes: 24 additions & 55 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@
"lint:fix": "ts-standard --fix && standard --fix",
"test": "cross-env NODE_ENV=test playwright test 'test/unit/.*.spec.js'",
"test:e2e": "xvfb-maybe cross-env NODE_ENV=test playwright test -c test/e2e/playwright.config.js",
"postinstall": "run-s install-app-deps patch-deps",
"postinstall": "run-s install-app-deps patch-deps macos-universal-kubo",
"install-app-deps": "electron-builder install-app-deps",
"macos-universal-kubo": "node pkgs/macos/build-universal-kubo-binary.js",
"patch-deps": "patch-package",
"clean": "shx rm -rf node_modules/kubo/bin",
"force-webui-download": "shx rm -rf assets/webui && run-s build:webui",
Expand Down Expand Up @@ -61,7 +62,7 @@
"got": "^12.0.3",
"ipfs-or-gateway": "^4.1.0",
"npm-run-all": "^4.1.5",
"patch-package": "^6.5.0",
"patch-package": "^6.5.1",
"playwright": "^1.35.1",
"pre-commit": "^1.2.2",
"proxyquire": "^2.1.3",
Expand Down
57 changes: 57 additions & 0 deletions patches/kubo+0.28.0.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
diff --git a/node_modules/kubo/src/download.js b/node_modules/kubo/src/download.js
index 0c04e8d..1c90cfe 100644
--- a/node_modules/kubo/src/download.js
+++ b/node_modules/kubo/src/download.js
@@ -236,13 +236,23 @@ async function link ({ depBin, version }) {
return localBin
}

+/**
+ * @param {object} options
+ * @param {string} options.version
+ * @param {string} options.platform
+ * @param {string} options.arch
+ * @param {string} options.installPath
+ * @param {string} options.distUrl
+ */
+module.exports.download = download
+
/**
* @param {string} [version]
* @param {string} [platform]
* @param {string} [arch]
* @param {string} [installPath]
*/
-module.exports = async (version, platform, arch, installPath) => {
+module.exports.downloadAndUpdateBin = async (version, platform, arch, installPath) => {
const args = cleanArguments(version, platform, arch, installPath)

return link({
diff --git a/node_modules/kubo/src/index.js b/node_modules/kubo/src/index.js
index 1ec6f6a..3cece54 100644
--- a/node_modules/kubo/src/index.js
+++ b/node_modules/kubo/src/index.js
@@ -3,6 +3,10 @@
const fs = require('fs')
const path = require('path')

+const { download } = require('./download')
+
+module.exports.download = download
+
module.exports.path = function () {
if (process.env.KUBO_BINARY) {
return process.env.KUBO_BINARY
diff --git a/node_modules/kubo/src/post-install.js b/node_modules/kubo/src/post-install.js
index 0a86c03..acd8ead 100644
--- a/node_modules/kubo/src/post-install.js
+++ b/node_modules/kubo/src/post-install.js
@@ -2,7 +2,7 @@

const download = require('./download')

-download()
+downloadAndUpdateBin()
.catch(err => {
console.error(err)
process.exit(1)
51 changes: 51 additions & 0 deletions pkgs/macos/build-universal-kubo-binary.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
const { download } = require('kubo')
const path = require('path')
const tmp = require('tmp')
const fs = require('fs')
const os = require('os')
const { execSync } = require('child_process')

;(async () => {
if (os.platform() !== 'darwin') {
// skip when we are not running on macos
return
}

// Remove single-platform binary
const ipfsBinaryPath = path.join('node_modules', 'kubo', 'bin', 'ipfs')
if (fs.existsSync(ipfsBinaryPath)) {
fs.unlinkSync(ipfsBinaryPath)
console.log(`Removed ${ipfsBinaryPath}`)
}

const packageJsonPath = require.resolve(path.join('kubo', 'package.json'))
const kuboVersion = `v${require(packageJsonPath).version}`

const intelDir = tmp.dirSync({ prefix: 'kubo-amd64' }).name
console.log(`→ Fetchng amd64 version of Kubo ${kuboVersion} to ${intelDir}`)
await download({
version: kuboVersion,
platform: 'darwin',
arch: 'amd64',
distUrl: 'https://dist.ipfs.tech',
installPath: intelDir
})

const armDir = tmp.dirSync({ prefix: 'kubo-arm64' }).name
console.log(`→ Fetchng arm64 version of Kubo ${kuboVersion} to ${armDir}`)
await download({
version: kuboVersion,
platform: 'darwin',
arch: 'arm64',
distUrl: 'https://dist.ipfs.tech',
installPath: armDir
})

// merge both and save result where the binary included in DMG lives
const intelBinary = `${intelDir}/kubo/ipfs`
const armBinary = `${armDir}/kubo/ipfs`
const lipoCommand = `lipo -create "${intelBinary}" "${armBinary}" -output "${ipfsBinaryPath}"`

execSync(lipoCommand)
console.log(`→ macOS lipo command executed successfully, universal Kubo binary saved at ${ipfsBinaryPath}`)
})()

0 comments on commit 348258f

Please sign in to comment.