Skip to content

Commit

Permalink
cli: universal support for CI template
Browse files Browse the repository at this point in the history
  • Loading branch information
skirsdeda authored and Brooooooklyn committed Dec 19, 2022
1 parent 4832e93 commit cf68288
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 14 deletions.
51 changes: 46 additions & 5 deletions cli/src/new/ci-template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
40 changes: 31 additions & 9 deletions cli/src/new/ci-yml.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -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
Expand Down Expand Up @@ -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, {
Expand Down
1 change: 1 addition & 0 deletions cli/src/new/package.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
},
}
Expand Down
7 changes: 7 additions & 0 deletions cli/src/parse-triple.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down

0 comments on commit cf68288

Please sign in to comment.