Closed Sparkenstein closed 3 years ago
Unfortunately, we will not be able to support cross-compilation anytime soon, due to platform-specific requirements. The target
arg is designed for specifying which installers you want to build for your current platform. We recommend using tauri-action to create installers for all platforms.
I needed to do cross compile from Mac for Windows binary for tauri application. I believe should be same for linux host as well. As I had my docker running, was able to follow the instruction mentioned on docker website to build my image and then compile my executable. Just putting here for reference if anybody needed till this is getting added to tauri and if they don't want to use github actions.
Put below dockerfile at the root of your project -
FROM rust:latest
RUN apt update && apt upgrade -y
RUN apt install -y g++-mingw-w64-x86-64
RUN rustup target add x86_64-pc-windows-gnu
RUN rustup toolchain install stable-x86_64-pc-windows-gnu
WORKDIR /app/src-tauri
CMD ["cargo", "build", "--target", "x86_64-pc-windows-gnu"]
Build image
docker build . -t tauri/windows
Run the image with your application
docker run --rm -v ${pwd}:/app tauri/windows
If you get any exception for permission (which I got for one of my mac) just instead of above docker run command use below commands
Reference - https://www.docker.com/blog/cross-compiling-rust-code-for-multiple-architectures/
For anyone copy/pasting the solution from @Jay1305
Run the image with your application
docker run --rm -v ${pwd}:/app tauri/windows
the command should be: docker run --rm -v $(pwd):/app tauri/windows
note the braces around pwd
For anyone stumbling upon this issue. Keep in mind that the generated .exe file is not really standalone. As of today the caveats (that i can remember right now) are:
I managed to do this using vagga, a user space container. The content of my vagga.yaml are:
containers:
tauri:
auto-clean: true
setup:
- !Ubuntu jammy
- !Sh |
mkdir -p .vagga/home
mkdir -p .vagga/run/user/root
- !Sh |
# install required packages
apt install -y software-properties-common gpg gpg-agent gnupg-agent
add-apt-repository ppa:apt-fast/stable
apt-add-repository -y universe
apt-add-repository -y multiverse
# install base packages
DEBIAN_FRONTEND=noninteractive apt install -y apt-fast axel aria2
apt-fast install -y curl ca-certificates build-essential libssl-dev pkg-config cmake make tar
curl --fail https://apt.llvm.org/llvm-snapshot.gpg.key | gpg --dearmor > $KEYRINGS/llvm.gpg;
echo "deb [signed-by=$KEYRINGS/llvm.gpg] http://apt.llvm.org/jammy/ llvm-toolchain-jammy-15 main" > /etc/apt/sources.list.d/llvm.list;
apt update
# install llvm
apt-fast install -y llvm-15 lld-15 clang-15 libobjc-11-dev
# ensure that clang/clang++ are callable directly
ln -s clang-15 /usr/bin/clang && ln -s clang /usr/bin/clang++ && ln -s lld-15 /usr/bin/ld.lld
# We also need to setup symlinks ourselves for the MSVC shims because they aren't in the debian packages
ln -s clang-15 /usr/bin/clang-cl && ln -s llvm-ar-15 /usr/bin/llvm-lib && ln -s lld-link-15 /usr/bin/lld-link
# Use clang instead of gcc when compiling binaries targeting the host (eg proc macros, build files)
update-alternatives --install /usr/bin/cc cc /usr/bin/clang 100
update-alternatives --install /usr/bin/c++ c++ /usr/bin/clang++ 100
# delete resolv.conf file; causes build time dns errors
rm -rf /etc/resolv.conf
environ: &envs
HOME: /work/.vagga/home
USER: root
XDG_RUNTIME_DIR: /work/.vagga/run/user/root
XDG_CACHE_HOME: /work/.vagga/home/.cache
PATH: /bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:/work/.vagga/home/.local/bin:/work/.vagga/home/.cargo/bin
CARGO_HOME: /work/.vagga/home/.cargo
RUSTFLAGS: '-C target-feature=+crt-static'
volumes: &vols
/tmp: !Tmpfs
size: 1G
tools:
auto-clean: true
setup:
- !Container tauri
- !Env PATH: /bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:/work/.vagga/home/.local/bin:/work/.vagga/home/.cargo/bin
- !Env CARGO_HOME: /work/.vagga/home/.cargo
- !Sh |
# rustup
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
environ: *envs
volumes: *vols
commands:
init: !CapsuleCommand
run: vagga _capsule build tauri
description: initialize tauri container
tools: !CapsuleCommand
run: vagga _capsule build tools
description: install and setup tools needed for cross compilation
dist: !Command
container: tools
run: |
# set rustup to stable version
rustup default stable
# add target arch
rustup target add x86_64-pc-windows-msvc
# install cargo build packages
cargo install cargo-xwin
# change directory; /work is the root to where vagga.yaml resides; tauri-app is in the same folder as vagga.yaml
cd /work/tauri-app/src-tauri
cargo xwin build --release --target x86_64-pc-windows-msvc
description: cross compile for windows
sh: !Command
container: tauri
run: bash
description: execute bash
install vagga:
cd ~/Downloads
rm -rf vagga*
wget https://files.zerogw.com/vagga/vagga-0.8.1-86-g505b3ea.tar.xz
tar -xJf vagga-0.8.1-86-g505b3ea.tar.xz
cd vagga
chmod +x install.sh
sudo ./install.sh
project directory tree:
├── tauri-app
│ ├── src
│ ├── src-tauri
│ ├── .gitignore
│ ├── README.md
├── vagga.yaml
to build, cd to where vagga.yaml is, and run vagga dist
other commands:
vagga init
- initialize base container
vagga tools
- install or update build tools
vagga sh
- open shell inside the container
@Jay1305 To solve permissions issues with running docker, instead of:
Put below dockerfile at the root of your project -
FROM rust:latest
RUN apt update && apt upgrade -y
RUN apt install -y g++-mingw-w64-x86-64
RUN rustup target add x86_64-pc-windows-gnu
RUN rustup toolchain install stable-x86_64-pc-windows-gnu
WORKDIR /app/src-tauri
CMD ["cargo", "build", "--target", "x86_64-pc-windows-gnu"]
Build image
docker build . -t tauri/windows
Run the image with your application
docker run --rm -v ${pwd}:/app tauri/windows
If you get any exception for permission (which I got for one of my mac) just instead of above docker run command use below commands
Use this:
FROM rust:latest
ARG HOST_USER_NAME="exadra37"
ARG HOST_UID="1000"
ARG HOST_GID="1000"
ENV HOST_USER_NAME=${HOST_USER_NAME} \
HOST_HOME=/home/${HOST_USER_NAME} \
HOST_UID=${HOST_UID} \
HOST_GID=${HOST_GID}
ENV WORKSPACE_PATH=${HOST_HOME}/app/src-tauri
RUN groupadd -g "${HOST_GID}" "${HOST_USER_NAME}" && \
useradd --create-home --uid "${HOST_UID}" --gid "${HOST_GID}" "${HOST_USER_NAME}"
RUN apt update && apt upgrade -y
RUN apt install -y g++-mingw-w64-x86-64
USER "${HOST_USER_NAME}"
WORKDIR "${WORKSPACE_PATH}"
RUN rustup target add x86_64-pc-windows-gnu
RUN rustup toolchain install stable-x86_64-pc-windows-gnu
# BUILD WITH:
# docker build --build-arg "HOST_USER_NAME=$(id -un)" --build-arg "HOST_UID=$(id -u)" --build-arg "HOST_GID=$(id -g)" . -t tauri/windows
# RUN WIDTH:
# docker run --rm -v $(pwd):/home/$(id -un)/app/src-tauri
CMD ["cargo", "build", "--target", "x86_64-pc-windows-gnu"]
This image will build a container matching the user in the host, thus solving any permissions issues.
Build:
docker build --build-arg "HOST_USER_NAME=$(id -un)" --build-arg "HOST_UID=$(id -u)" --build-arg "HOST_GID=$(id -g)" . -t tauri/windows
Run:
docker run --rm -v $(pwd):/home/$(id -un)/app/src-tauri tauri/windows
CC @nicolaiunrein
Describe the bug I am trying to cross compile tauri app from my ubuntu linux to other platoforms, namely Windows and Mac OS at least. according to
tauri build --help
I just need to pass "Comma-separated list of target triples". but when I pass anything other than linux toolchain, it fails with different error.is cross compilation even possible? if yes how I can do that?
Platform and Versions (please complete the following information):
OS: Ubuntu 20.04
Stack Trace ex apple: