Skip to content

Commit

Permalink
dub: Use arm64 binaries on arm64 macos when available
Browse files Browse the repository at this point in the history
Since version 1.38.1 dub started publishing arm64 releases for macos
in addition to their x86_64 releases.

Signed-off-by: Andrei Horodniceanu <[email protected]>
  • Loading branch information
the-horo committed Jul 14, 2024
1 parent ab33f49 commit 73502c9
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 8 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -258,3 +258,6 @@ It should work even if it's a x86_64 binary thanks to the Rosetta compatibility

Updated a lot of dependencies, changed the output target to es2022 from es2017.
This means that one no longer needs to set `NODE_OPTIONS=--openssl-legacy-provider` when building.

Use arm64 binaries for dub on macos, when applicable.
Dub started publishing macos binaries since version [1.38.1](https://github.com/dlang/dub/releases/tag/v1.38.1)
19 changes: 19 additions & 0 deletions __tests__/d-dub.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ function init (version: string) { return Dub.initialize(version, '') }


test('Test dub URLs for exact versions', async () => {
Object.defineProperty(process, 'arch', { value: 'x64' })

Object.defineProperty(process, 'platform', { value: 'linux' })
await expect(init('v1.38.1')).resolves.toHaveProperty(
'url', 'https://github.com/dlang/dub/releases/download/v1.38.1/dub-v1.38.1-linux-x86_64.tar.gz')
Expand All @@ -24,6 +26,7 @@ test('Test dub URLs for exact versions', async () => {
})

test('Test that dub accepts both v<version> and <version>', async () => {
Object.defineProperty(process, 'arch', { value: 'x64' })
Object.defineProperty(process, 'platform', { value: 'linux' })
await expect(init('v1.37.0')).resolves.toHaveProperty(
'url', 'https://github.com/dlang/dub/releases/download/v1.37.0/dub-v1.37.0-linux-x86_64.tar.gz')
Expand Down Expand Up @@ -86,3 +89,19 @@ test('Test that dub adds itself to PATH', async () => {

process.env = origEnv
})

test('Test that dub uses arm64 binaries on arm64 macos when available', async () => {
Object.defineProperty(process, 'platform', { value: 'darwin' })

Object.defineProperty(process, 'arch', { value: 'arm64' })
await expect(init('v1.38.1')).resolves.toHaveProperty(
'url', 'https://github.com/dlang/dub/releases/download/v1.38.1/dub-v1.38.1-osx-arm64.tar.gz')
await expect(init('v1.38.0')).resolves.toHaveProperty(
'url', 'https://github.com/dlang/dub/releases/download/v1.38.0/dub-v1.38.0-osx-x86_64.tar.gz')

Object.defineProperty(process, 'arch', { value: 'x64' })
await expect(init('v1.38.1')).resolves.toHaveProperty(
'url', 'https://github.com/dlang/dub/releases/download/v1.38.1/dub-v1.38.1-osx-x86_64.tar.gz')
await expect(init('v1.38.0')).resolves.toHaveProperty(
'url', 'https://github.com/dlang/dub/releases/download/v1.38.0/dub-v1.38.0-osx-x86_64.tar.gz')
})
8 changes: 4 additions & 4 deletions dist/index.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

23 changes: 20 additions & 3 deletions src/d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -705,16 +705,18 @@ export class Dub implements ITool {
if (matches[2])
throw new Error("only release versions of DUB are supported, not: " + version)
version = "v" + matches[1];

const archSuffix = Dub.getUrlArchSuffix(version)
let url: string
switch (process.platform) {
case "win32":
url = `https://github.com/dlang/dub/releases/download/${version}/dub-${version}-windows-x86_64.zip`
url = `https://github.com/dlang/dub/releases/download/${version}/dub-${version}-windows-${archSuffix}.zip`
break
case "linux":
url = `https://github.com/dlang/dub/releases/download/${version}/dub-${version}-linux-x86_64.tar.gz`
url = `https://github.com/dlang/dub/releases/download/${version}/dub-${version}-linux-${archSuffix}.tar.gz`
break
case "darwin":
url = `https://github.com/dlang/dub/releases/download/${version}/dub-${version}-osx-x86_64.tar.gz`
url = `https://github.com/dlang/dub/releases/download/${version}/dub-${version}-osx-${archSuffix}.tar.gz`
break
default:
throw new Error("unsupported platform: " + process.platform);
Expand All @@ -723,6 +725,21 @@ export class Dub implements ITool {
return new Dub(url, version)
}

static getUrlArchSuffix (version: string) {
if (process.arch == 'x64')
return 'x86_64'

if (process.arch == 'arm64' && process.platform == 'darwin') {
const sv = semver.parseSimpleSemver(version)
const hasArm64Binaries: semver.SemVer = [1, 38, 1, []]
if (semver.cmpSemver(sv, hasArm64Binaries) >= 0)
return 'arm64'
return 'x86_64'
}

throw new Error(`Unsupported platform-arch triple (${process.platform}-${process.arch}) for dub releases ${version}`)
}

/** Return the path to where the url archive was extracted, after caching it */
private async getCached(): Promise<string> {
let cached = await tc.find(this.name, this.version)
Expand Down

0 comments on commit 73502c9

Please sign in to comment.