From cf682882328ead49166f25e26243465bce8836f9 Mon Sep 17 00:00:00 2001 From: Tadas Dailyda Date: Thu, 15 Dec 2022 17:10:04 +0200 Subject: [PATCH] cli: universal support for CI template --- cli/src/new/ci-template.ts | 51 ++++++++++++++++++++++++++++++++++---- cli/src/new/ci-yml.ts | 40 +++++++++++++++++++++++------- cli/src/new/package.ts | 1 + cli/src/parse-triple.ts | 7 ++++++ 4 files changed, 85 insertions(+), 14 deletions(-) diff --git a/cli/src/new/ci-template.ts b/cli/src/new/ci-template.ts index 591e1581..d240ff8b 100644 --- a/cli/src/new/ci-template.ts +++ b/cli/src/new/ci-template.ts @@ -57,11 +57,6 @@ jobs: - host: macos-latest target: 'aarch64-apple-darwin' build: | - sudo rm -Rf /Library/Developer/CommandLineTools/SDKs/*; - export CC=$(xcrun -f clang); - export CXX=$(xcrun -f clang++); - SYSROOT=$(xcrun --sdk macosx --show-sdk-path); - export CFLAGS="-isysroot $SYSROOT -isystem $SYSROOT"; yarn build --target aarch64-apple-darwin strip -x *.node - host: ubuntu-latest @@ -512,6 +507,52 @@ jobs: yarn test ls -la + universal-macOS: + name: Build universal macOS binary + needs: + - build + runs-on: macos-latest + + steps: + - uses: actions/checkout@v3 + + - name: Setup node + uses: actions/setup-node@v3 + with: + node-version: 16 + check-latest: true + cache: yarn + + - name: Cache NPM dependencies + uses: actions/cache@v3 + with: + path: .yarn/cache + key: npm-cache-test-x86_64-apple-darwin-16-\${{ hashFiles('yarn.lock') }} + + - name: 'Install dependencies' + run: yarn install + + - name: Download macOS x64 artifact + uses: actions/download-artifact@v3 + with: + name: bindings-x86_64-apple-darwin + path: artifacts + - name: Download macOS arm64 artifact + uses: actions/download-artifact@v3 + with: + name: bindings-aarch64-apple-darwin + path: artifacts + + - name: Combine binaries + run: yarn universal + + - name: Upload artifact + uses: actions/upload-artifact@v3 + with: + name: bindings-universal-apple-darwin + path: \${{ env.APP_NAME }}.*.node + if-no-files-found: error + publish: name: Publish runs-on: ubuntu-latest diff --git a/cli/src/new/ci-yml.ts b/cli/src/new/ci-yml.ts index 963b965c..1f956dd3 100644 --- a/cli/src/new/ci-yml.ts +++ b/cli/src/new/ci-yml.ts @@ -1,5 +1,7 @@ import { load, dump } from 'js-yaml' +import { NodeArchToCpu, UniArchsByPlatform, parseTriple } from '../parse-triple' + import { YAML } from './ci-template' const BUILD_FREEBSD = 'build-freebsd' @@ -9,25 +11,39 @@ const TEST_LINUX_X64_MUSL = 'test-linux-x64-musl-binding' const TEST_LINUX_AARCH64_GNU = 'test-linux-aarch64-gnu-binding' const TEST_LINUX_AARCH64_MUSL = 'test-linux-aarch64-musl-binding' const TEST_LINUX_ARM_GNUEABIHF = 'test-linux-arm-gnueabihf-binding' +const UNIVERSAL_MACOS = 'universal-macOS' export const createGithubActionsCIYml = ( binaryName: string, targets: string[], ) => { + const allTargets = new Set( + targets.flatMap((t) => { + const platform = parseTriple(t) + if (platform.arch === 'universal') { + const srcTriples = UniArchsByPlatform[platform.platform]?.map((arch) => + t.replace('universal', NodeArchToCpu[arch]), + ) + return [t, ...(srcTriples ?? [])] + } + return [t] + }), + ) const fullTemplate = load(YAML(binaryName)) as any const requiredSteps = [] - const enableWindowsX86 = targets.includes('x86_64-pc-windows-msvc') - const enableMacOSX86 = targets.includes('x86_64-apple-darwin') - const enableLinuxX86Gnu = targets.includes('x86_64-unknown-linux-gnu') - const enableLinuxX86Musl = targets.includes('x86_64-unknown-linux-musl') - const enableLinuxArm8Gnu = targets.includes('aarch64-unknown-linux-gnu') - const enableLinuxArm8Musl = targets.includes('aarch64-unknown-linux-musl') - const enableLinuxArm7 = targets.includes('armv7-unknown-linux-gnueabihf') - const enableFreeBSD = targets.includes('x86_64-unknown-freebsd') + const enableWindowsX86 = allTargets.has('x86_64-pc-windows-msvc') + const enableMacOSX86 = allTargets.has('x86_64-apple-darwin') + const enableLinuxX86Gnu = allTargets.has('x86_64-unknown-linux-gnu') + const enableLinuxX86Musl = allTargets.has('x86_64-unknown-linux-musl') + const enableLinuxArm8Gnu = allTargets.has('aarch64-unknown-linux-gnu') + const enableLinuxArm8Musl = allTargets.has('aarch64-unknown-linux-musl') + const enableLinuxArm7 = allTargets.has('armv7-unknown-linux-gnueabihf') + const enableFreeBSD = allTargets.has('x86_64-unknown-freebsd') + const enableMacOSUni = allTargets.has('universal-apple-darwin') fullTemplate.env.APP_NAME = binaryName fullTemplate.jobs.build.strategy.matrix.settings = fullTemplate.jobs.build.strategy.matrix.settings.filter( - ({ target }: { target: string }) => targets.includes(target), + ({ target }: { target: string }) => allTargets.has(target), ) if (!fullTemplate.jobs.build.strategy.matrix.settings.length) { delete fullTemplate.jobs.build.strategy.matrix @@ -81,6 +97,12 @@ export const createGithubActionsCIYml = ( requiredSteps.push(TEST_LINUX_ARM_GNUEABIHF) } + if (!enableMacOSUni) { + delete fullTemplate.jobs[UNIVERSAL_MACOS] + } else { + requiredSteps.push(UNIVERSAL_MACOS) + } + fullTemplate.jobs.publish.needs = requiredSteps return dump(fullTemplate, { diff --git a/cli/src/new/package.ts b/cli/src/new/package.ts index 84678e12..dfa555ee 100644 --- a/cli/src/new/package.ts +++ b/cli/src/new/package.ts @@ -31,6 +31,7 @@ export const createPackageJson = ( 'build:debug': 'napi build --platform', prepublishOnly: 'napi prepublish -t npm', test: 'ava', + universal: 'napi universal', version: 'napi version', }, } diff --git a/cli/src/parse-triple.ts b/cli/src/parse-triple.ts index 6d42620c..a5da6662 100644 --- a/cli/src/parse-triple.ts +++ b/cli/src/parse-triple.ts @@ -22,6 +22,13 @@ const CpuToNodeArch: { [index: string]: NodeJSArch } = { armv7: 'arm', } +export const NodeArchToCpu: { [index: string]: string } = { + x64: 'x86_64', + arm64: 'aarch64', + ia32: 'i686', + arm: 'armv7', +} + const SysToNodePlatform: { [index: string]: NodeJS.Platform } = { linux: 'linux', freebsd: 'freebsd',