tauri-apps / tauri

Build smaller, faster, and more secure desktop applications with a web frontend.
https://tauri.app
Apache License 2.0
79.5k stars 2.36k forks source link

[bug] Cannot build on MacOS apple silicon (Monterey) #6612

Open Magellol opened 1 year ago

Magellol commented 1 year ago

Describe the bug

Hello. I'm trying to build a basic app on macos. I was reading this might be due to newer Macosx SDKs not including the Carbon header file although I'm pretty new to native apps so I'm not sure what to do with that information.

I've tried to download an older SDK here, set the SDKROOT env variable to try and tell cargo to use the older SDK but that didn't seem to work. Possible I didn't do this right, like I said I'm pretty new to building native apps. I'm currently using macOS 12.3 SDK as default.

I've tried building the app on windows (native, not cross compiling) and it builds fine. Let me know if you have any ideas, thank you!

FYI, cargo build fails with the following error:

The following warnings were emitted during compilation:

warning: src/platform_impl/macos/carbon_hotkey/carbon_hotkey_binding.c:7:10: fatal error: 'Carbon/Carbon.h' file not found
warning: #include <Carbon/Carbon.h>
warning:          ^~~~~~~~~~~~~~~~~
warning: 1 error generated.

error: failed to run custom build command for `tao v0.15.8`

Caused by:
  process didn't exit successfully: `/Users/thomas/Work/hardware-monitor/app/target/debug/build/tao-16e14c691292959c/build-script-build` (exit status: 1)
  --- stdout
  cargo:rustc-link-lib=framework=Carbon
  TARGET = Some("aarch64-apple-darwin")
  OPT_LEVEL = Some("0")
  HOST = Some("aarch64-apple-darwin")
  cargo:rerun-if-env-changed=CC_aarch64-apple-darwin
  CC_aarch64-apple-darwin = None
  cargo:rerun-if-env-changed=CC_aarch64_apple_darwin
  CC_aarch64_apple_darwin = None
  cargo:rerun-if-env-changed=HOST_CC
  HOST_CC = None
  cargo:rerun-if-env-changed=CC
  CC = Some("clang")
  cargo:rerun-if-env-changed=CFLAGS_aarch64-apple-darwin
  CFLAGS_aarch64-apple-darwin = None
  cargo:rerun-if-env-changed=CFLAGS_aarch64_apple_darwin
  CFLAGS_aarch64_apple_darwin = None
  cargo:rerun-if-env-changed=HOST_CFLAGS
  HOST_CFLAGS = None
  cargo:rerun-if-env-changed=CFLAGS
  CFLAGS = None
  cargo:rerun-if-env-changed=CRATE_CC_NO_DEFAULTS
  CRATE_CC_NO_DEFAULTS = None
  DEBUG = Some("true")
  running: "clang" "-O0" "-ffunction-sections" "-fdata-sections" "-fPIC" "-gdwarf-2" "-fno-omit-frame-pointer" "--target=arm64-apple-darwin" "-Wall" "-Wextra" "-o" "/Users/thomas/Work/hardware-monitor/app/target/debug/build/tao-f9ddb4621241b175/out/src/platform_impl/macos/carbon_hotkey/carbon_hotkey_binding.o" "-c" "src/platform_impl/macos/carbon_hotkey/carbon_hotkey_binding.c"
  cargo:warning=src/platform_impl/macos/carbon_hotkey/carbon_hotkey_binding.c:7:10: fatal error: 'Carbon/Carbon.h' file not found
  cargo:warning=#include <Carbon/Carbon.h>
  cargo:warning=         ^~~~~~~~~~~~~~~~~
  cargo:warning=1 error generated.
  exit status: 1

  --- stderr

  error occurred: Command "clang" "-O0" "-ffunction-sections" "-fdata-sections" "-fPIC" "-gdwarf-2" "-fno-omit-frame-pointer" "--target=arm64-apple-darwin" "-Wall" "-Wextra" "-o" "/Users/thomas/Work/hardware-monitor/app/target/debug/build/tao-f9ddb4621241b175/out/src/platform_impl/macos/carbon_hotkey/carbon_hotkey_binding.o" "-c" "src/platform_impl/macos/carbon_hotkey/carbon_hotkey_binding.c" with args "clang" did not execute successfully (status code exit status: 1).

Reproduction

No response

Expected behavior

No response

Platform and versions

Environment
  › OS: Mac OS 12.3.1 X64
  › Node.js: 18.14.2
  › npm: 9.5.0
  › pnpm: Not installed!
  › yarn: 1.22.19
  › rustup: 1.25.2
  › rustc: 1.68.2
  › cargo: 1.68.2
  › Rust toolchain: stable-aarch64-apple-darwin

Packages
  › @tauri-apps/cli [NPM]: 1.2.3
  › @tauri-apps/api [NPM]: 1.2.0
  › tauri [RUST]: 1.2.4,
  › tauri-build [RUST]: 1.2.1,
  › tao [RUST]: 0.15.8,
  › wry [RUST]: 0.23.4,

App
  › build-type: bundle
  › CSP: unset
  › distDir: ../dist
  › devPath: http://localhost:3000/
  › framework: React
  › bundler: Webpack

App directory structure
  ├─ app
  ├─ dist
  ├─ node_modules
  ├─ shared
  ├─ webpack
  ├─ .git
  ├─ client


### Stack trace

_No response_

### Additional context

_No response_
datner commented 1 year ago

This is the wall I smashed into too :(

datner commented 1 year ago

this is probably the same issue as outlined here: https://github.com/tauri-apps/tao/issues/383 But it was solved an merged last year.. It shouldn't be a problem still. Quite upsetting

EDIT: Have tried to use nix to sidestep this issue, but then webkitgtk fails to build on my mac. This is frustrating..

Magellol commented 1 year ago

@datner How would nix help solving this problem? I'm a too running nix but I'm only using it to have the rustup dependency here. I feel this is due to newer macos sdks having missing dependencies to build but I can't figure out if it's possible to tell cargo to use an older one? 🤔

datner commented 1 year ago

@Magellol after many many many hours of bashing my head against the wall, it crumbled. I'm using this flake and everything works exactly as I wish it to. Big thanks to @yusefnapora for figuring it out in the first place.

{
  inputs = {
    nixpkgs.url = "github:nixos/nixpkgs/nixpkgs-unstable";
    rust-overlay.url = "github:oxalica/rust-overlay";
  };

  outputs = {
    nixpkgs,
    rust-overlay,
    ...
  }: let
    overlays = [(import rust-overlay)];

    # Helper generating outputs for each desired system
    forAllSystems = nixpkgs.lib.genAttrs [
      "x86_64-darwin"
      "x86_64-linux"
      "aarch64-darwin"
      "aarch64-linux"
    ];

    # Import nixpkgs' package set for each system.
    nixpkgsFor = forAllSystems(system:
      import nixpkgs {
        inherit system overlays;
      });
  in {
    formatter = forAllSystems (system:  nixpkgsFor.${system}.alejandra);
    devShells = forAllSystems (system: let
      pkgs = import nixpkgs {
        inherit system overlays;
      };

      inherit (pkgs.stdenv) isLinux;

      rust-toolchain = pkgs.rust-bin.stable.latest.default.override {
        extensions = ["rust-analyzer" "rust-src" "rust-std"];
        targets = ["wasm32-unknown-unknown"];
      };

      packages-linux = with pkgs; [
        rust-toolchain
        nodejs-18_x
        nodePackages.pnpm
        pkg-config
        gtk3
        webkitgtk
        libayatana-appindicator.dev
        alsa-lib.dev
      ];

      packages-darwin = with pkgs; [
        rust-toolchain
        nodejs-18_x
        nodePackages.pnpm
        curl
        wget
        pkg-config
        libiconv
        darwin.apple_sdk.frameworks.Security
        darwin.apple_sdk.frameworks.CoreServices
        darwin.apple_sdk.frameworks.CoreFoundation
        darwin.apple_sdk.frameworks.Foundation
        darwin.apple_sdk.frameworks.AppKit
        darwin.apple_sdk.frameworks.WebKit
        darwin.apple_sdk.frameworks.Cocoa
      ];

      packages =
        if isLinux
        then packages-linux
        else packages-darwin;
    in {
      default = pkgs.mkShell {
        name = "@bubble/client";

        buildInputs = packages;

      };
    });
  };
}

It's not optimized or anything, can be made more elegant. But it's plenty good enough for me

Magellol commented 1 year ago

@datner I was able to make this work as well having a somewhat similar flake file as yours. Thank you! For reference, this is what I ended up using

{
  inputs = {
    nixpkgs.url = "github:nixos/nixpkgs/nixpkgs-unstable";
    rust-overlay.url = "github:oxalica/rust-overlay";
  };

  outputs =
    { nixpkgs
    , rust-overlay
    , ...
    }:
    let
      overlays = [ (import rust-overlay) ];

      # Helper generating outputs for each desired system
      forAllSystems = nixpkgs.lib.genAttrs [
        "aarch64-darwin"
      ];

      # Import nixpkgs' package set for each system.
      nixpkgsFor = forAllSystems (system:
        import nixpkgs {
          inherit system overlays;
        });
    in
    {
      devShells = forAllSystems (system:
        let
          pkgs = import nixpkgs {
            inherit system overlays;
          };

          packages-darwin = with pkgs; [
            rustup
            libiconv
            darwin.apple_sdk.frameworks.AppKit
            darwin.apple_sdk.frameworks.WebKit
          ];

          packages = packages-darwin;
        in
        {
          default = pkgs.mkShell {
            buildInputs = packages;
          };
        });
    };
}

And this works perfectly building on mac os apple silicon. I was curious if you knew what was rust-overlay, it's the only bit I don't fully understand here.

datner commented 1 year ago

And this works perfectly building on mac os apple silicon. I was curious if you knew what was rust-overlay, it's the only bit I don't fully understand here.

@Magellol Yeah ofc, you can also read about it from their repo. But basically instead of installing from rustup in fragments, it's a pre-built binaries you can consume. Like everything in nix, it's more complicated than that, but in short - it's faster and more deterministic than installing the rust toolchain manually. I saw you removed most of the 'fat' from the apple_sdk, I should probably do that as well since I ended up not using them.

Eveeifyeve commented 2 months ago

I have made an unoffical flake that should work for this: https://github.com/eveeifyeve/nix-tauri

Note: The devenv flake works not the deafult one so just use nix flake init github:eveeifyeve/nix-tauri#devenv for now.