快轉到主要內容

Darling幫幫我!Linux跑macOS的Xcode程式

Linux系統 虛擬機與容器技術 Darling Xcode MacOS Linux
🗓️ 民國113年 甲辰年
✍ 切換正體/簡體字
目錄

現在你看到的是在Linux系統執行的macOS程式。

Draling是一款自由軟體,作者描述該軟體為A Darwin/macOS emulation layer for Linux,即Linux系統適用的macOS程式模擬層。

跟Wine轉譯Windows指令碼的機制類似,Darling可以把macOS指令碼轉譯成Linux指令碼,速度比虛擬機要快很多。

Darling支援dmg、pkg格式打包的macOS程式,連Homebrew也支援。

但是,Darling僅支援轉譯命令行macOS程式,例如Xcode Command Line Tools。

圖形macOS程式?規劃中…已經規劃10年了,作者到目前為止僅實現Cocoa API。

你可能會說:只能跑命令行程式意義在哪裡?這些程式Linux不是幾乎都有。是呀,看未來Darling能不能進一步完整支援Xcode,讓我們能在Linux編譯iPhone的APP。

現階段欲開發iOS APP, 跑macOS KVM虛擬機之類的黑蘋果可能還是比較實在。

1. 安裝Darling相容層
#

由於該專案還在積極開發中,請參照 作者Github的指示編譯,不要裝deb套件。

Darling是否支援ARM架構?應該不行,這裡使用x86架構的Linux比較保險。

  1. Ubuntu 22.04安裝依賴套件
sudo apt install cmake automake clang-15 bison flex libfuse-dev libudev-dev pkg-config libc6-dev-i386 \
gcc-multilib libcairo2-dev libgl1-mesa-dev curl libglu1-mesa-dev libtiff5-dev \
libfreetype6-dev git git-lfs libelf-dev libxml2-dev libegl1-mesa-dev libfontconfig1-dev \
libbsd-dev libxrandr-dev libxcursor-dev libgif-dev libavutil-dev libpulse-dev \
libavformat-dev libavcodec-dev libswresample-dev libdbus-1-dev libxkbfile-dev \
libssl-dev libstdc++-12-dev
  1. 複製儲存庫,開始編譯。Darling專案檔案很大,大概會吃掉16GB磁碟空間。
git clone --recursive https://github.com/darlinghq/darling.git

cd darling

# 如果有的話,移除舊版本Darling
sudo tools/uninstall

mkdir build && cd build

cmake ..

make -j8
  1. 安裝Darling
sudo make install

2. 基本操作
#

  1. 進入Darling的shell,模擬macOS環境的輕量容器
darling shell
  1. Darling支援類似WinePrefix的環境變數,可用於指定容器路徑
export DPREFIX=~/darling_custom

darling shell
  1. 進入終端機後,如果你需要zsh就打指令切換
chsh -s /bin/zsh
  1. Darling執行sw_vers指令回傳的macOS版本是為11.7.4。macOS容器的資料放在~/.darling,下面的目錄結構跟macOS很像。

  2. Darling只能透過純文字模式安裝軟體,以pkg為例子,你得先透過Linux下載pkg檔案,將Darling關機,把檔案放進容器後,再重開機安裝。

# 這是Linux的shell
darling shutdown

wget "pkg網址"

mv app.pkg ~/.darling/Users/user

# 這是Darling的shell
darling shell

cd ~/.darling/Users/user

installer -pkg app.pkg -target /
  1. 若為dmg格式的軟體,將其用hdiutil掛載,再以installer安裝
# 這是Linux的shell
darling shutdown

wget "dmg網址"

mv app.dmg ~/.darling/Users/user

# 這是Darling的shell
darling shell

cd /Users/user

hdiutil attach app.dmg

installer -pkg "/Volumes/app/app.pkg" -target /

3. 透過Darling安裝Xcode
#

Apple開發的Xcode附贈命令列工具「Xcode Command Line Tools」,搞到這款工具就足夠「編譯」macOS應用程式了。

裝完Xcode Command Line Tools後才能裝 Homebrew

至於iOS、tvOS的應用程式嘛…我自己測試還是不行,「Xcode Command Line Tools」正常,但「Xcode」跑xcodebuild會碰到一堆Error loading required libraries.的錯誤。因此要開發iOS、tvOS應用程式請還是請乖乖開黑蘋果吧,例如 跑macOS KVM虛擬機

本節我們要使用指令安裝Xcode commandline tools。

  1. Apple Developers,登入Apple帳號,按Additional Downloads,下載Command Line Tools for Xcode 13.2(對應macOS 11.7)

  2. 將dmg檔放到Darling目錄,安裝dmg檔

darling shutdown

mv ~/Downloads/Command_Line_Tools_for_Xcode_13.2.dmg ~/.darling/Users/user

darling shell

cd /Users/user

hdiutil attach Command_Line_Tools_for_Xcode_13.2.dmg

installer -pkg "/Volumes/Command_Line_Tools_for_Xcode_13.2/Command Line Tools.pkg" -target /
  1. 如果你想一併安裝Xcode,到Apple Developers下載Xcode 13.2.1(對應macOS 11.7),下載xip檔。

  2. 將Darling關機

darling shutdown
  1. 把xip檔案放到Darling的磁碟,再安裝
mv ~/Downloads/Xcode_13.2.1.xip ~/.darling/Applications

darling shell

unxip Applications/Xcode_13.2.1.xip

如果解不開,改用Linux的 apple-sdk-tools工具來解xip

exit

darling shutdown

cd ~/.darling/Applications

wget https://raw.githubusercontent.com/bitcoin-core/apple-sdk-tools/master/extract_xcode.py

python3 extract_xcode.py -f Xcode_13.2.1.xip  | cpio -d -i
  1. 最後設定Xcode的路徑
# Xcode
xcode-select --switch /Applications/Xcode.app

# 如果設定上述路徑出現錯誤,改回預設的Xcode commandline tools
xcode-select --switch /Library/Developer/CommandLineTools
  1. 根據Darling作者給的例子,在安裝Xcode工具之後,應該就可以叫Clang出來編譯程式
export SDKROOT=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk

echo '#include <stdio.h> void main() { puts("Hello world"); }' > helloworld.c

/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang helloworld.c -o helloworld

./helloworld

參考資料
#

相關文章

免Mac電腦,OSX-KVM讓你在Linux系統安裝macOS的QEMU虛擬機
Linux系統 虛擬機與容器技術 MacOS QEMU Libvirt Xcode
Czkawka軟體用法,Linux以圖搜圖、搜尋重複檔案的小工具
Linux系統 Linux好用軟體 Disk Cleanup Linux Windows MacOS
Rustdesk使用教學:開源免費遠端桌面,可自架
Linux系統 Self-hosting自架 RustDesk Remote Desktop Linux Windows MacOS

留言板

此處提供二種留言板。點選按鈕,選擇您覺得方便的留言板。要討論程式碼請用Giscus,匿名討論請用Disqus。

這是Giscus留言板,需要Github帳號才能留言。支援markdown語法,若要上傳圖片請貼Imgur連結。您的留言會在Github Discussions向所有人公開。

這是Disqus留言板,您可能會看到Disqus強制投放的廣告。有時留言可能會被系統判定需審核,導致延遲顯示,請見諒。