From aaaf2d9f22d577510f145508886a50ac2992a336 Mon Sep 17 00:00:00 2001 From: Kelvie Wong Date: Sat, 14 Jan 2023 22:14:06 -0800 Subject: [PATCH 1/5] Add cross-compile script That will simply generate the win64 artifacts for those of us that run DF under Proton on Steam for Linux. --- build/.gitignore | 1 + build/build-win64-from-linux.sh | 39 +++++++++++++++++++++ docs/dev/compile/Compile.rst | 60 +++++++++++++++++++++++++++++++-- 3 files changed, 98 insertions(+), 2 deletions(-) create mode 100755 build/build-win64-from-linux.sh diff --git a/build/.gitignore b/build/.gitignore index 3c85ab4df..f25d10ca7 100644 --- a/build/.gitignore +++ b/build/.gitignore @@ -6,3 +6,4 @@ DF_PATH.txt _CPack_Packages *.tar.* .cmake +win64-cross diff --git a/build/build-win64-from-linux.sh b/build/build-win64-from-linux.sh new file mode 100755 index 000000000..74c307c89 --- /dev/null +++ b/build/build-win64-from-linux.sh @@ -0,0 +1,39 @@ +#!/usr/bin/env sh + +# Number of jobs == core count +jobs=$(grep -c ^processor /proc/cpuinfo) + +# Calculate absolute paths for docker to do mounts +srcdir=$(realpath "$(dirname "$(readlink -f "$0")")"/..) + +cd "$srcdir"/build + +mkdir -p win64-cross +mkdir -p win64-cross/output + +# Assumes you built a container image called dfhack-build-msvc from +# https://github.com/BenLubar/build-env/tree/master/msvc, see +# docs/dev/compile/Compile.rst. +# +# NOTE: win64-cross is mounted in /src/build due to the hardcoded `cmake ..` in +# the Dockerfile +# +# TODO: make this work for rootless docker, i.e. remove the sudo for those that +# don't normally need to use sudo to run docker. +if ! sudo docker run --rm -it -v "$srcdir":/src -v "$srcdir/build/win64-cross/":/src/build \ + --user buildmaster \ + --name dfhack-win \ + dfhack-build-msvc bash -c "cd /src/build && dfhack-configure windows 64 Release -DCMAKE_INSTALL_PREFIX=/src/build/output && dfhack-make -j$jobs install" \ + ; then + echo + echo "Build failed" + exit 1 +else + echo + echo "Windows artifacts are at win64-cross/output. Copy or symlink them to" + echo "your steam DF directory to install dfhack (and optionally delete the" + echo "hack/ directory already present)" + echo + echo "Typically this can be done like this:" + echo " cp -r win64-cross/output/* \"$HOME/.local/share/Steam/steamapps/common/Dwarf Fortress\"" +fi diff --git a/docs/dev/compile/Compile.rst b/docs/dev/compile/Compile.rst index 032eb7524..8e38139fa 100644 --- a/docs/dev/compile/Compile.rst +++ b/docs/dev/compile/Compile.rst @@ -284,8 +284,8 @@ addition to the normal ``CC`` and ``CXX`` flags above:: export PATH=/usr/local/bin:$PATH -Windows cross compiling from Linux -================================== +Windows cross compiling from Linux (running DF inside docker) +============================================================= .. highlight:: bash @@ -368,6 +368,62 @@ host when you want to reattach:: If you edit code and need to rebuild, run ``dfhack-make`` and then ``ninja install``. That will handle all the wineserver management for you. +Cross-compiling windows files for running DF in Steam for Linux +=============================================================== + +.. highlight:: bash + +If you wish, you can use Docker to build just the Windows files to copy to your +existing Steam installation on Linux. + +.. contents:: + :local: + :depth: 1 + +Step 1: Build the MSVC builder image +------------------------------------ + +It'll be called ``dfhack-build-msvc:latest`` after it's done building:: + + git clone https://github.com/BenLubar/build-env.git + cd build-env/msvc + docker build --build-arg BUILDER_UID=$(id -u) -t dfhack-build-msvc . + +The ``BUILDER_UID`` argument is used to make sure the build user can access your +source code directory inside the container. + +The docker build takes a while, but only needs to be done once, unless the build +environment changes. + +Step 2: Get dfhack, and run the build script +-------------------------------------------- + +Check out ``dfhack`` into another directory, and run the build script:: + + git clone https://github.com/DFHack/dfhack.git + cd dfhack + git submodule update --init --recursive + cd build + ./build-win64-from-linux.sh + +The last step may ask you for your ``sudo`` password -- if you are able to run +``docker`` without ``sudo`` normally, you can remove the ``sudo`` from the build +script. + +The script will mount your host's ``dfhack`` directory to docker, use it to +build the artifacts in ``build/win64-cross``, and put all the files needed to +install in ``build/win64-cross/output``. + +Step 3: install dfhack to your Steam DF install +----------------------------------------------- +As the script will tell you, you can then copy the files into your DF folder:: + + # Optional -- remove the old hack directory in case we leave files behind + rm ~/.local/share/Steam/steamapps/common/"Dwarf Fortress"/hack + cp -r win64-cross/output/* ~/.local/share/Steam/steamapps/common/"Dwarf Fortress"/ + +Afterward, just run DF as normal. + .. _note-offline-builds: Building DFHack Offline From 5a20f775bf13baac435a739defd99d4601638d01 Mon Sep 17 00:00:00 2001 From: Kelvie Wong Date: Sun, 15 Jan 2023 20:34:52 -0800 Subject: [PATCH 2/5] Set UIDs properly on run; fix sudo use This depends on a new entrypoint put inside the docker image, that will read BUILDER_UID and fix up the users and files. This also removes "sudo" and allows the entire script to be run with sudo without messing with the build file permissions. --- build/build-win64-from-linux.sh | 29 +++++++++++++++++++++++------ docs/dev/compile/Compile.rst | 14 ++++++-------- 2 files changed, 29 insertions(+), 14 deletions(-) diff --git a/build/build-win64-from-linux.sh b/build/build-win64-from-linux.sh index 74c307c89..7f23d78c3 100755 --- a/build/build-win64-from-linux.sh +++ b/build/build-win64-from-linux.sh @@ -1,5 +1,6 @@ -#!/usr/bin/env sh +#!/bin/bash +set -e # Number of jobs == core count jobs=$(grep -c ^processor /proc/cpuinfo) @@ -8,20 +9,36 @@ srcdir=$(realpath "$(dirname "$(readlink -f "$0")")"/..) cd "$srcdir"/build +builder_uid=$(id -u) + mkdir -p win64-cross mkdir -p win64-cross/output +# Check for sudo; we want to use the real user +if [[ $(id -u) -eq 0 ]]; then + if [[ -z "$SUDO_UID" || "$SUDO_UID" -eq 0 ]]; then + echo "Please don't run this script directly as root, use sudo instead:" + echo + echo " sudo $0" + # This is because we can't change the buildmaster UID in the container to 0 -- + # that's already taken by root. + exit 1 + fi + + # If this was run using sudo, let's make sure the directories are owned by the + # real user (and set the BUILDER_UID to it) + builder_uid=$SUDO_UID + chown $builder_uid win64-cross win64-cross/output +fi + # Assumes you built a container image called dfhack-build-msvc from # https://github.com/BenLubar/build-env/tree/master/msvc, see # docs/dev/compile/Compile.rst. # # NOTE: win64-cross is mounted in /src/build due to the hardcoded `cmake ..` in # the Dockerfile -# -# TODO: make this work for rootless docker, i.e. remove the sudo for those that -# don't normally need to use sudo to run docker. -if ! sudo docker run --rm -it -v "$srcdir":/src -v "$srcdir/build/win64-cross/":/src/build \ - --user buildmaster \ +if ! docker run --rm -it -v "$srcdir":/src -v "$srcdir/build/win64-cross/":/src/build \ + -e BUILDER_UID=$builder_uid \ --name dfhack-win \ dfhack-build-msvc bash -c "cd /src/build && dfhack-configure windows 64 Release -DCMAKE_INSTALL_PREFIX=/src/build/output && dfhack-make -j$jobs install" \ ; then diff --git a/docs/dev/compile/Compile.rst b/docs/dev/compile/Compile.rst index 8e38139fa..f91c43c07 100644 --- a/docs/dev/compile/Compile.rst +++ b/docs/dev/compile/Compile.rst @@ -387,10 +387,7 @@ It'll be called ``dfhack-build-msvc:latest`` after it's done building:: git clone https://github.com/BenLubar/build-env.git cd build-env/msvc - docker build --build-arg BUILDER_UID=$(id -u) -t dfhack-build-msvc . - -The ``BUILDER_UID`` argument is used to make sure the build user can access your -source code directory inside the container. + docker build -t dfhack-build-msvc . The docker build takes a while, but only needs to be done once, unless the build environment changes. @@ -406,14 +403,15 @@ Check out ``dfhack`` into another directory, and run the build script:: cd build ./build-win64-from-linux.sh -The last step may ask you for your ``sudo`` password -- if you are able to run -``docker`` without ``sudo`` normally, you can remove the ``sudo`` from the build -script. - The script will mount your host's ``dfhack`` directory to docker, use it to build the artifacts in ``build/win64-cross``, and put all the files needed to install in ``build/win64-cross/output``. +If you need to run ``docker`` using ``sudo``, run the script using ``sudo`` +rather than directly:: + + sudo ./build-win64-from-linux.sh + Step 3: install dfhack to your Steam DF install ----------------------------------------------- As the script will tell you, you can then copy the files into your DF folder:: From 839636a56339d449d6a8ed0a80ccafbef1207397 Mon Sep 17 00:00:00 2001 From: Kelvie Wong Date: Fri, 20 Jan 2023 17:21:33 -0800 Subject: [PATCH 3/5] Change $HOME to ~ As $HOME shows /root under sudo --- build/build-win64-from-linux.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/build/build-win64-from-linux.sh b/build/build-win64-from-linux.sh index 7f23d78c3..713ae90e4 100755 --- a/build/build-win64-from-linux.sh +++ b/build/build-win64-from-linux.sh @@ -52,5 +52,5 @@ else echo "hack/ directory already present)" echo echo "Typically this can be done like this:" - echo " cp -r win64-cross/output/* \"$HOME/.local/share/Steam/steamapps/common/Dwarf Fortress\"" + echo " cp -r win64-cross/output/* ~/.local/share/Steam/steamapps/common/\"Dwarf Fortress\"" fi From ce12e2d90633af18166a31543447884df9ebfd36 Mon Sep 17 00:00:00 2001 From: Kelvie Wong Date: Sat, 21 Jan 2023 09:38:46 -0800 Subject: [PATCH 4/5] Also build docs. The in-game docs aren't built otherwise, so you won't get help for the various commands if this isn't set. --- build/build-win64-from-linux.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/build/build-win64-from-linux.sh b/build/build-win64-from-linux.sh index 713ae90e4..08ea88793 100755 --- a/build/build-win64-from-linux.sh +++ b/build/build-win64-from-linux.sh @@ -40,7 +40,7 @@ fi if ! docker run --rm -it -v "$srcdir":/src -v "$srcdir/build/win64-cross/":/src/build \ -e BUILDER_UID=$builder_uid \ --name dfhack-win \ - dfhack-build-msvc bash -c "cd /src/build && dfhack-configure windows 64 Release -DCMAKE_INSTALL_PREFIX=/src/build/output && dfhack-make -j$jobs install" \ + dfhack-build-msvc bash -c "cd /src/build && dfhack-configure windows 64 Release -DCMAKE_INSTALL_PREFIX=/src/build/output cmake .. -DBUILD_DOCS=1 && dfhack-make -j$jobs install" \ ; then echo echo "Build failed" From fb1ce3fed7062ce6d9d0637f5dc2d461e6b72345 Mon Sep 17 00:00:00 2001 From: Kelvie Wong Date: Sun, 22 Jan 2023 11:44:35 -0800 Subject: [PATCH 5/5] Add changelog. --- docs/changelog.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/changelog.txt b/docs/changelog.txt index 94b7d61c9..8d5665c85 100644 --- a/docs/changelog.txt +++ b/docs/changelog.txt @@ -38,6 +38,7 @@ changelog.txt uses a syntax similar to RST, with a few special sequences: ## Fixes ## Misc Improvements +- A new cross-compile build script was added for building the Windows files from a Linux Docker builder (see the Compile instructions in the docs) ## Documentation