Difference between revisions of "How to build VCMI (macOS)"

From VCMI Project Wiki
Jump to: navigation, search
(Building VCMI with Qt Creator)
(update for 1.1)
 
(11 intermediate revisions by 4 users not shown)
Line 1: Line 1:
= Installing dependencies =
+
= Requirements =
===== Installing Xcode =====
+
# C++ toolchain, either of:
App Store -> Xcode
+
#* Xcode Command Line Tools (aka CLT): <code>sudo xcode-select --install</code>
Launch it after the installation to agree with it's license terms.
+
#* Xcode IDE: https://developer.apple.com/xcode/
 +
#* (not tested) other C++ compilers, e.g. gcc/clang from [https://brew.sh/ Homebrew]
 +
# CMake: <code>brew install --cask cmake</code> or get from https://cmake.org/download/
 +
# (optional) Ninja: <code>brew install ninja</code> or get from https://github.com/ninja-build/ninja/releases
  
Alternatively Xcode can be downloaded from Apple Developers website.
+
= Obtaining source code =
Registration is easier and every possible version is available: https://developer.apple.com/download/more/
+
Clone https://github.com/vcmi/vcmi with submodules. Example for command line:
 +
<nowiki>git clone --recurse-submodules https://github.com/vcmi/vcmi.git</nowiki>
  
===== Setting Xcode version Xcode =====
+
= Obtaining dependencies =
In case you have some weird CMake errors during generation attempts such as
+
There're 2 ways to get dependencies automatically.
<pre>
+
 
CMake Error: Xcode 1.5 not supported.
+
== Conan package manager ==
</pre>
+
Please find detailed instructions in [https://github.com/vcmi/vcmi/tree/develop/docs/conan.md VCMI repository]. Note that the link points to the cutting-edge state in <code>develop</code> branch, for the latest release check the same document in the [https://github.com/vcmi/vcmi/tree/master/docs/conan.md master branch].
You might need to specify installed version of Xcode:
+
 
<pre>
+
On the step where you need to replace '''PROFILE''', choose:
sudo /usr/bin/xcode-select --switch /Users/admin/Downloads/Xcode.app
+
* if you're on an Intel Mac: <code>macos-intel</code>
</pre>
+
* if you're on an Apple Silicon Mac: <code>macos-arm</code>
After that Cmake will be able to generate projects properly!
+
 
 +
Note: if you wish to build 1.0 release in non-<code>Release</code> configuration, you should define <code>USE_CONAN_WITH_ALL_CONFIGS=1</code> environment variable when executing <code>conan install</code>.
 +
 
 +
== Homebrew ==
 +
# [https://brew.sh/ Install Homebrew]
 +
# Install dependencies: <code>brew install boost minizip sdl2 sdl2_image sdl2_mixer sdl2_ttf tbb</code>
 +
# If you want to watch in-game videos, also install FFmpeg: <code>brew install ffmpeg@4</code>
 +
# Install Qt dependency in either of the ways (note that you can skip this if you're not going to build Launcher):
 +
#* <code>brew install qt@5</code> for Qt 5 or <code>brew install qt</code> for Qt 6
 +
#* using [https://www.qt.io/download Qt Online Installer] - choose '''Go open source'''
  
===== Installing [https://brew.sh/ Homebrew] =====
+
= Preparing build environment =
<pre>
+
This applies only to Xcode-based toolchain. If <code>xcrun -f clang</code> prints errors, then use either of the following ways:
ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
+
* select an Xcode instance from Xcode application - Preferences - Locations - Command Line Tools
</pre>
+
* use <code>xcode-select</code> utility to set Xcode or Xcode Command Line Tools path: for example, <code>sudo xcode-select -s /Library/Developer/CommandLineTools</code>
 +
* set <code>DEVELOPER_DIR</code> environment variable pointing to Xcode or Xcode Command Line Tools path: for example, <code>export DEVELOPER_DIR=/Applications/Xcode.app</code>
  
===== Installing dependencies using Homebrew =====
+
= Configuring project for building =
<pre>
+
Note that if you wish to use Qt Creator IDE, you should skip this step and configure respective variables inside the IDE.
brew install git cmake sdl2 sdl2_ttf sdl2_image boost ffmpeg minizip
 
brew install sdl2_mixer --with-smpeg2
 
# Now install Qt5 for launcher
 
brew install qt5
 
# Make sure to read brew output on Qt carefully
 
# It's will tell you how to add Qt to your $PATH environment variable
 
#
 
# Currently it's can be done like that
 
# echo 'export PATH="/usr/local/opt/qt/bin:$PATH"' >> ~/.bash_profile
 
</pre>
 
  
= Building VCMI in command line with Makefiles =
+
# In Terminal <code>cd</code> to the source code directory
===== Getting and configuring VCMI =====
+
# Start assembling CMake invocation: type <code>cmake -S . -B BUILD_DIR</code> where ''BUILD_DIR'' can be any path, '''don't press Return'''
<pre>
+
# Decide which CMake generator you want to use:
# You can choose any directory
+
#* Makefiles: no extra option needed or pass <code>-G 'Unix Makefiles'</code>
mkdir ~/DEV && cd ~/DEV
+
#* Ninja (if you have installed it): pass <code>-G Ninja</code>
# Clone VCMI with all submodules
+
#* Xcode IDE (if you have installed it): pass <code>-G Xcode</code>
git clone -b develop --depth 1 --recursive  https://github.com/vcmi/vcmi.git
+
# If you picked Makefiles or Ninja, pick desired ''build type'' - either of Debug / RelWithDebInfo / Release / MinSizeRel - and pass it in <code>CMAKE_BUILD_TYPE</code> option, for example: <code>-D CMAKE_BUILD_TYPE=Release</code>. If you don't pass this option, <code>RelWithDebInfo</code> will be used.
# Creating directory for building
+
# If you don't want to build Launcher, pass <code>-D ENABLE_LAUNCHER=OFF</code>
mkdir cmake && cd cmake
+
# You can also pass <code>-Wno-dev</code> if you're not interested in CMake developer warnings
cmake ../vcmi -G "Unix Makefiles" -DCMAKE_BUILD_TYPE=Release -Wno-devc
+
# Next step depends on the dependency manager you have picked:
</pre>
+
#* Conan: pass <code>-D CMAKE_TOOLCHAIN_FILE=conan-generated/conan_toolchain.cmake</code> where '''conan-generated''' must be replaced with your directory choice
You can as well build VCMI without Qt5 or launcher by disabling it:
+
#* Homebrew: if you installed FFmpeg or Qt 5, you need to pass <code>-D "CMAKE_PREFIX_PATH="</code> variable. See below what you can insert after <code>=</code> (but '''before the closing quote'''), multiple values must be separated with <code>;</code> (semicolon):
<pre>
+
#** if you installed FFmpeg, insert <code>$(brew --prefix ffmpeg@4)</code>
# But please don't do that unless you know what you're doing
+
#** if you installed Qt 5 from Homebrew, insert: <code>$(brew --prefix qt@5)</code>
cmake ../vcmi -G "Unix Makefiles" -DCMAKE_BUILD_TYPE=Release -DENABLE_LAUNCHER=0 -Wno-devc
+
#** if you installed Qt from Online Installer, insert your path to Qt directory, for example: <code>/Users/kambala/dev/Qt-libs/5.15.2/Clang64</code>
</pre>
+
#** example for FFmpeg + Qt 5: <code>-D "CMAKE_PREFIX_PATH=$(brew --prefix ffmpeg@4);$(brew --prefix qt@5)"</code>
 +
# now press Return
  
===== Building and packaging VCMI =====
+
= Building project =
<pre>
 
cd ~/DEV/cmake
 
# Start build using cmake
 
cmake --build . -- -j 4
 
# Or you can as well use simple
 
# make -j 4
 
  
# When build is completed you can trigger packaging into DMG
+
You must also install game files to be able to run the built version, see [[Installation on macOS]].
cpack
 
# After file named such as "vcmi-0.99.dmg" will be created
 
</pre>
 
  
= Building VCMI with Xcode =
+
== From Xcode IDE ==
===== Getting and configuring VCMI =====
+
Open <code>VCMI.xcodeproj</code> from the build directory, select <code>vcmiclient</code> scheme and hit Run (Cmd+R). To build Launcher, select <code>vcmilauncher</code> scheme instead.
<pre>
 
# You can choose any directory
 
mkdir ~/DEV && cd ~/DEV
 
# Clone VCMI with all submodules
 
git clone -b develop --depth 1 --recursive  https://github.com/vcmi/vcmi.git
 
# Creating directory for building
 
mkdir cmake && cd cmake
 
# Xcode is multi-configuration project so specifying CMAKE_BUILD_TYPE makes no sense
 
cmake ../vcmi -G "Xcode"
 
  
# You can as well open project in Xcode using:
+
== From command line ==
open VCMI.xcodeproj
+
<code>cmake --build <path to build directory></code>
</pre>
 
  
===== Building and packaging VCMI =====
+
* If using Makefiles generator, you'd want to utilize all your CPU cores by appending <code>-- -j$(sysctl -n hw.ncpu)</code> to the above
<pre>
+
* If using Xcode generator, you can also choose which configuration to build by appending <code>--config <configuration name></code> to the above, for example: <code>--config Debug</code>
cd ~/DEV/xcode
 
# Start build. Xcode built using multiple processes by default so you don't need to specify it
 
# Since it's multi-configuration project you need to specify if you want Debug build since Release is default
 
cmake --build . --config Debug
 
  
# You have to specify build configuration for CPack. Otherwise it's will expect Release as default
+
= Packaging project into DMG file =
cpack -C Debug
+
After building, run <code>cpack</code> from the build directory. If using Xcode generator, also pass <code>-C <configuration name></code> with the same configuration that you used to build the project.
</pre>
 
  
= Building VCMI with Qt Creator =
+
If you use Conan, it's expected that you use '''conan-generated''' directory at step 4 of [[#Conan package manager]].
When opening project with Qt Creator installed using online installer you might have a problem with CMake unable to find Qt even if you added it into the $PATH environment variable. In that case try to open it from command line:
 
<pre>
 
open ~/Qt/Qt\ Creator.app
 
</pre>
 
  
 
= Running VCMI =
 
= Running VCMI =
Line 104: Line 81:
 
Alternatively you can run binaries directly from "bin" directory:
 
Alternatively you can run binaries directly from "bin" directory:
 
<pre>
 
<pre>
~/DEV/cmake/bin/vcmilauncher
+
BUILD_DIR/bin/vcmilauncher
~/DEV/cmake/bin/vcmiclient
+
BUILD_DIR/bin/vcmiclient
~/DEV/cmake/bin/vcmiserver
+
BUILD_DIR/bin/vcmiserver
 
</pre>
 
</pre>
 
CMake include commands to copy all needed assets from source directory into "bin" on each build. They'll work when you build from Xcode too.
 
CMake include commands to copy all needed assets from source directory into "bin" on each build. They'll work when you build from Xcode too.
  
'''Be aware you still need directories "Data", "Mp3" and "Maps" from Heroes 3 game assets in VCMI app data directory:'''
+
= Some useful debugging tips =
<pre>
 
~/Library/Application\ Support/vcmi/
 
</pre>
 
 
 
===== Some useful debugging tips =====
 
 
Anyone who might want to debug builds, but new to macOS could find following commands useful:
 
Anyone who might want to debug builds, but new to macOS could find following commands useful:
 
<pre>
 
<pre>
 
# To attach DMG file from command line use
 
# To attach DMG file from command line use
hdiutil attach vcmi-0.99.dmg
+
hdiutil attach vcmi-1.0.dmg
 
# Detach volume:
 
# Detach volume:
hdiutil detach /Volumes/vcmi-0.99
+
hdiutil detach /Volumes/vcmi-1.0
 
# To view dependency paths
 
# To view dependency paths
otool -L /Volumes/vcmi-0.99/VCMI.app/Contents/MacOS/vcmiclient
+
otool -L /Volumes/vcmi-1.0/VCMI.app/Contents/MacOS/vcmiclient
 
# To display load commands such as LC_RPATH  
 
# To display load commands such as LC_RPATH  
otool -l /Volumes/vcmi-0.99/VCMI.app/Contents/MacOS/vcmiclient
+
otool -l /Volumes/vcmi-1.0/VCMI.app/Contents/MacOS/vcmiclient
 
</pre>
 
</pre>
 +
 +
= Troubleshooting =
 +
In case of troubles you can always consult our CI build scripts or contact the dev team via slack

Latest revision as of 08:25, 23 December 2022

Requirements

  1. C++ toolchain, either of:
  2. CMake: brew install --cask cmake or get from https://cmake.org/download/
  3. (optional) Ninja: brew install ninja or get from https://github.com/ninja-build/ninja/releases

Obtaining source code

Clone https://github.com/vcmi/vcmi with submodules. Example for command line:

git clone --recurse-submodules https://github.com/vcmi/vcmi.git

Obtaining dependencies

There're 2 ways to get dependencies automatically.

Conan package manager

Please find detailed instructions in VCMI repository. Note that the link points to the cutting-edge state in develop branch, for the latest release check the same document in the master branch.

On the step where you need to replace PROFILE, choose:

  • if you're on an Intel Mac: macos-intel
  • if you're on an Apple Silicon Mac: macos-arm

Note: if you wish to build 1.0 release in non-Release configuration, you should define USE_CONAN_WITH_ALL_CONFIGS=1 environment variable when executing conan install.

Homebrew

  1. Install Homebrew
  2. Install dependencies: brew install boost minizip sdl2 sdl2_image sdl2_mixer sdl2_ttf tbb
  3. If you want to watch in-game videos, also install FFmpeg: brew install ffmpeg@4
  4. Install Qt dependency in either of the ways (note that you can skip this if you're not going to build Launcher):
    • brew install qt@5 for Qt 5 or brew install qt for Qt 6
    • using Qt Online Installer - choose Go open source

Preparing build environment

This applies only to Xcode-based toolchain. If xcrun -f clang prints errors, then use either of the following ways:

  • select an Xcode instance from Xcode application - Preferences - Locations - Command Line Tools
  • use xcode-select utility to set Xcode or Xcode Command Line Tools path: for example, sudo xcode-select -s /Library/Developer/CommandLineTools
  • set DEVELOPER_DIR environment variable pointing to Xcode or Xcode Command Line Tools path: for example, export DEVELOPER_DIR=/Applications/Xcode.app

Configuring project for building

Note that if you wish to use Qt Creator IDE, you should skip this step and configure respective variables inside the IDE.

  1. In Terminal cd to the source code directory
  2. Start assembling CMake invocation: type cmake -S . -B BUILD_DIR where BUILD_DIR can be any path, don't press Return
  3. Decide which CMake generator you want to use:
    • Makefiles: no extra option needed or pass -G 'Unix Makefiles'
    • Ninja (if you have installed it): pass -G Ninja
    • Xcode IDE (if you have installed it): pass -G Xcode
  4. If you picked Makefiles or Ninja, pick desired build type - either of Debug / RelWithDebInfo / Release / MinSizeRel - and pass it in CMAKE_BUILD_TYPE option, for example: -D CMAKE_BUILD_TYPE=Release. If you don't pass this option, RelWithDebInfo will be used.
  5. If you don't want to build Launcher, pass -D ENABLE_LAUNCHER=OFF
  6. You can also pass -Wno-dev if you're not interested in CMake developer warnings
  7. Next step depends on the dependency manager you have picked:
    • Conan: pass -D CMAKE_TOOLCHAIN_FILE=conan-generated/conan_toolchain.cmake where conan-generated must be replaced with your directory choice
    • Homebrew: if you installed FFmpeg or Qt 5, you need to pass -D "CMAKE_PREFIX_PATH=" variable. See below what you can insert after = (but before the closing quote), multiple values must be separated with ; (semicolon):
      • if you installed FFmpeg, insert $(brew --prefix ffmpeg@4)
      • if you installed Qt 5 from Homebrew, insert: $(brew --prefix qt@5)
      • if you installed Qt from Online Installer, insert your path to Qt directory, for example: /Users/kambala/dev/Qt-libs/5.15.2/Clang64
      • example for FFmpeg + Qt 5: -D "CMAKE_PREFIX_PATH=$(brew --prefix ffmpeg@4);$(brew --prefix qt@5)"
  8. now press Return

Building project

You must also install game files to be able to run the built version, see Installation on macOS.

From Xcode IDE

Open VCMI.xcodeproj from the build directory, select vcmiclient scheme and hit Run (Cmd+R). To build Launcher, select vcmilauncher scheme instead.

From command line

cmake --build <path to build directory>

  • If using Makefiles generator, you'd want to utilize all your CPU cores by appending -- -j$(sysctl -n hw.ncpu) to the above
  • If using Xcode generator, you can also choose which configuration to build by appending --config <configuration name> to the above, for example: --config Debug

Packaging project into DMG file

After building, run cpack from the build directory. If using Xcode generator, also pass -C <configuration name> with the same configuration that you used to build the project.

If you use Conan, it's expected that you use conan-generated directory at step 4 of #Conan package manager.

Running VCMI

You can run VCMI from DMG, but it's will also work from your IDE be it Xcode or Qt Creator.

Alternatively you can run binaries directly from "bin" directory:

BUILD_DIR/bin/vcmilauncher
BUILD_DIR/bin/vcmiclient
BUILD_DIR/bin/vcmiserver

CMake include commands to copy all needed assets from source directory into "bin" on each build. They'll work when you build from Xcode too.

Some useful debugging tips

Anyone who might want to debug builds, but new to macOS could find following commands useful:

# To attach DMG file from command line use
hdiutil attach vcmi-1.0.dmg
# Detach volume:
hdiutil detach /Volumes/vcmi-1.0
# To view dependency paths
otool -L /Volumes/vcmi-1.0/VCMI.app/Contents/MacOS/vcmiclient
# To display load commands such as LC_RPATH 
otool -l /Volumes/vcmi-1.0/VCMI.app/Contents/MacOS/vcmiclient

Troubleshooting

In case of troubles you can always consult our CI build scripts or contact the dev team via slack