Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add macOS support #123

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -164,3 +164,36 @@ jobs:
- name: Build example for iOS
run: |
yarn turbo run build:ios --cache-dir="${{ env.TURBO_CACHE_DIR }}"

build-macos:
runs-on: macos-latest
env:
TURBO_CACHE_DIR: .turbo/macos
steps:
- name: Checkout
uses: actions/checkout@v3

- name: Setup
uses: ./.github/actions/setup
with:
github_token: ${{ secrets.GITHUB_TOKEN }}

- name: Cache turborepo for macOS
uses: actions/cache@v3
with:
path: ${{ env.TURBO_CACHE_DIR }}
key: ${{ runner.os }}-turborepo-macos-${{ hashFiles('yarn.lock') }}
restore-keys: |
${{ runner.os }}-turborepo-macos-

- name: Check turborepo cache for macOS
run: |
TURBO_CACHE_STATUS=$(node -p "($(yarn turbo run build:macos --cache-dir="${{ env.TURBO_CACHE_DIR }}" --dry=json)).tasks.find(t => t.task === 'build:macos').cache.status")

if [[ $TURBO_CACHE_STATUS == "HIT" ]]; then
echo "turbo_cache_hit=1" >> $GITHUB_ENV
fi

- name: Build example for macOS
run: |
yarn turbo run build:macos --cache-dir="${{ env.TURBO_CACHE_DIR }}"
4 changes: 3 additions & 1 deletion apps/example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@
"android": "react-native run-android",
"ios": "react-native run-ios",
"start": "react-native start",
"pod:install": "pod install --project-directory=ios",
"pod:install:ios": "pod install --project-directory=ios",
"pod:install:macos": "pod install --project-directory=macos",
"build:android": "cd android && ./gradlew assembleDebug --warning-mode all",
"build:ios": "react-native build-ios --scheme Example --mode Debug --extra-params \"-sdk iphonesimulator CC=clang CPLUSPLUS=clang++ LD=clang LDPLUSPLUS=clang++ GCC_OPTIMIZATION_LEVEL=0 GCC_PRECOMPILE_PREFIX_HEADER=YES ASSETCATALOG_COMPILER_OPTIMIZATION=time DEBUG_INFORMATION_FORMAT=dwarf COMPILER_INDEX_STORE_ENABLE=NO\"",
"build:macos": "react-native build-macos --scheme Example --mode Debug --extra-params \"-sdk iphonesimulator CC=clang CPLUSPLUS=clang++ LD=clang LDPLUSPLUS=clang++ GCC_OPTIMIZATION_LEVEL=0 GCC_PRECOMPILE_PREFIX_HEADER=YES ASSETCATALOG_COMPILER_OPTIMIZATION=time DEBUG_INFORMATION_FORMAT=dwarf COMPILER_INDEX_STORE_ENABLE=NO\"",
"mkdist": "node -e \"require('node:fs').mkdirSync('dist', { recursive: true, mode: 0o755 })\""
},
"dependencies": {
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"lint": "turbo run lint",
"tsc": "turbo run tsc",
"build:ios": "turbo run build:ios",
"build:macos": "turbo run build:macos",
"build:android": "turbo run build:android",
"build": "turbo run build",
"pod:install": "turbo run pod:install"
Expand Down
8 changes: 8 additions & 0 deletions packages/webgpu/apple/ApplePlatformContext.mm
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,20 @@
throw std::runtime_error("Couldn't retrive blob data");
}

#if !TARGET_OS_OSX
UIImage *image = [UIImage imageWithData:blobData];
#else
NSImage *image = [[NSImage alloc] initWithData:blobData];
#endif
if (!image) {
throw std::runtime_error("Couldn't decode image");
}

#if !TARGET_OS_OSX
CGImageRef cgImage = image.CGImage;
#else
CGImageRef cgImage = [image CGImageForProposedRect:NULL context:NULL hints:NULL];
#endif
size_t width = CGImageGetWidth(cgImage);
size_t height = CGImageGetHeight(cgImage);
size_t bitsPerComponent = 8;
Expand Down
4 changes: 2 additions & 2 deletions packages/webgpu/apple/MetalView.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#pragma once

#import "WebGPUModule.h"
#import <UIKit/UIKit.h>
#import "RNWGUIKit.h"

@interface MetalView : UIView
@interface MetalView : RNWGPlatformView

@property NSNumber *contextId;

Expand Down
12 changes: 12 additions & 0 deletions packages/webgpu/apple/MetalView.mm
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,21 @@ @implementation MetalView {
BOOL _isConfigured;
}

#if !TARGET_OS_OSX
+ (Class)layerClass {
return [CAMetalLayer class];
}
#else // !TARGET_OS_OSX
- (instancetype)init
{
self = [super init];
if (self) {
self.wantsLayer = true;
self.layer = [CAMetalLayer layer];
}
return self;
}
#endif // !TARGET_OS_OSX

- (void)configure
{
Expand Down
11 changes: 11 additions & 0 deletions packages/webgpu/apple/RNWGUIKit.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#if !TARGET_OS_OSX
#import <UIKit/UIKit.h>
#else
#import <Appkit/Appkit.h>
#endif

#if !TARGET_OS_OSX
typedef UIView RNWGPlatformView;
#else
typedef NSView RNWGPlatformView;
#endif
5 changes: 3 additions & 2 deletions packages/webgpu/apple/WebGPUViewManager.mm
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#import <React/RCTViewManager.h>
#import "MetalView.h"
#import "RCTBridge.h"
#import "RNWGUIKit.h"
#import "WebGPUModule.h"

@interface WebGPUViewManager : RCTViewManager
Expand All @@ -11,11 +12,11 @@ @implementation WebGPUViewManager

RCT_EXPORT_MODULE(WebGPUView)

- (UIView *)view {
- (RNWGPlatformView *)view {
return [MetalView new];
}

RCT_CUSTOM_VIEW_PROPERTY(contextId, NSNumber, UIView) {
RCT_CUSTOM_VIEW_PROPERTY(contextId, NSNumber, RNWGPlatformView) {
NSNumber *contextId = [RCTConvert NSNumber:json];
[(MetalView *)view setContextId:contextId];
}
Expand Down
2 changes: 1 addition & 1 deletion packages/webgpu/react-native-wgpu.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Pod::Spec.new do |s|
s.license = package["license"]
s.authors = package["author"]

s.platforms = { :ios => min_ios_version_supported, :visionos => "1.0" }
s.platforms = { :ios => min_ios_version_supported, :osx => "10.15", :visionos => "1.0" }
s.source = { :git => "https://github.com/wcandillon/react-native-webgpu.git", :tag => "#{s.version}" }

s.source_files = [
Expand Down
28 changes: 25 additions & 3 deletions turbo.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
]
},
"build:ios": {
"dependsOn": ["pod:install"],
"dependsOn": ["pod:install:ios"],
"outputs": [
"apps/*/ios/build",
"apps/*/ios/Pods"
Expand All @@ -34,14 +34,36 @@
"**/package.json",
"**/*.podspec",
"packages/webgpu/cpp/**",
"packages/webgpu/ios/**",
"packages/webgpu/apple/**",
"apps/*/package.json",
"apps/*/ios",
"!apps/*/ios/build",
"!apps/*/ios/Pods"
]
},
"pod:install": {
"build:macos": {
"dependsOn": ["pod:install:macos"],
"outputs": [
"apps/*/macos/build",
"apps/*/macos/Pods"
],
"inputs": [
"**/package.json",
"**/*.podspec",
"packages/webgpu/cpp/**",
"packages/webgpu/apple/**",
"apps/*/package.json",
"apps/*/macos",
"!apps/*/macos/build",
"!apps/*/macos/Pods"
]
},
"pod:install:ios": {
"cache": false,
"inputs": ["**/ios/Podfile", "**/ios/Podfile.lock"],
"outputs": ["**/ios/Pods/**"]
},
"pod:install:macos": {
"cache": false,
"inputs": ["**/ios/Podfile", "**/ios/Podfile.lock"],
"outputs": ["**/ios/Pods/**"]
Expand Down
6 changes: 3 additions & 3 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -10474,8 +10474,8 @@ __metadata:
linkType: hard

"react-native-macos@npm:^0.74.0":
version: 0.74.5
resolution: "react-native-macos@npm:0.74.5"
version: 0.74.10
resolution: "react-native-macos@npm:0.74.10"
dependencies:
"@jest/create-cache-key-function": ^29.6.3
"@react-native-community/cli": 13.6.9
Expand Down Expand Up @@ -10522,7 +10522,7 @@ __metadata:
optional: true
bin:
react-native-macos: cli.js
checksum: 732f09c21be670188ef7b7d658bd849fe4c0ef78157cb31edf88a46912eb83dbbce93decde809a6bfffdfda42caf04faa62638475279c16390a4fdb8315d7794
checksum: d94a78599cd5442e51f5c9cf5fc8cd94586e5c05c11ce6f09a725deb5b28722744f5c6b5a5912eddbf71718f999b845db24b1a408e306252563e4d2fd5528e9a
languageName: node
linkType: hard

Expand Down
Loading