Skip to content

Instantly share code, notes, and snippets.

@Cold06
Last active September 23, 2024 00:16
Show Gist options
  • Save Cold06/a0f0b4fb9b852db37fb66d4d624ae7b5 to your computer and use it in GitHub Desktop.
Save Cold06/a0f0b4fb9b852db37fb66d4d624ae7b5 to your computer and use it in GitHub Desktop.
Yall don't want contributors or something?

How to build Krita on MacOS (as of Sep 2024)

If you follow the official guide you will quickly find that it doesn't work. And even if you manage to build it once, you will also find that you won't get any autocomplete or intelisense on VSCode or CLion without some extra non-obvious configuration.

We could give up, and have a better use of our time, or we could waste our weekend trying to make it work, let's do the latter.

Prerequisites

Make sure you have the prerequisites as lited on the official guide, I will paste them here for convenience.

I'm not sure if Qt Creator is actually required, since I got a full build without even opening it? You be the judge.

Preparation

The guide tells you to just run this:

export BUILDROOT=$HOME/dev

mkdir -p $BUILDROOT
cd $BUILDROOT

But, this is the time for you to customize where this will be located, maybe you have a special folder on your device for this sort of project, for me its under ~/w, but there is also the detail that this process will create more folders outside the Krita build folder, so you might not want a venv folder and a bunch of other folder names starting with underscore on your "projects" folder, so maybe put the Krita clone one extra folder layer deep, lets call it krita-build, that way when we are done we can just nuke ~/w/krita-build to delete Krita and all its adjacent folders.

For me the command will be as follow, but you customzie the path to something else.

export BUILDROOT=$HOME/w/krita-build #change this to your liking

mkdir -p $BUILDROOT
cd $BUILDROOT

pwd

Then the guide tells us to download a bunch of stuff, you might want to add a --depth=1 to the Krita clone as well, you will download 700M instead of 1.4GB if thats important for you. I won't here since it's not one or two gigabytes that will save me from the problems of non-upgradeable storage.

cd $BUILDROOT
git clone https://invent.kde.org/graphics/krita.git
cd krita
git clone https://invent.kde.org/dkazakov/krita-deps-management.git krita-deps-management --depth=1
git clone https://invent.kde.org/dkazakov/ci-utilities.git krita-deps-management/ci-utilities --depth=1
python3 -m venv $BUILDROOT/venv --upgrade-deps
source $BUILDROOT/venv/bin/activate
pip install -r krita-deps-management/requirements.txt

And this is why we put the Krita clone one extra folder layer deep into our projects folder, it already created one extra directory, more to come:

(venv) ➜  krita git:(master) ✗ ls ..
krita venv

Anyways, next command block, some problems there, you can try running this, but won't do anything...

cd $BUILDROOT
python3 $BUILDROOT/krita/build-tools/ci-scripts/download-macos-tools.py
source $BUILDROOT/_krita-tools/activate

For me it dies with:

/opt/homebrew/Cellar/[email protected]/3.12.5/Frameworks/Python.framework/Versions/3.12/Resources/Python.app/Contents/MacOS/Python: can't open file '/Users/cold/w/krita-build/krita/build-tools/ci-scripts/download-macos-tools.py': [Errno 2] No such file or directory
source: no such file or directory: /Users/cold/w/krita-build/_krita-tools/activate

Right... a quick look with fzf (shoutout to fzf) reveals that the file is actually under krita-deps-management not build-tools. Fine, we will fix the command and try again, that should fix the second command as well:

Actually don't run this just yet... it won't work again:

cd $BUILDROOT
python3 $BUILDROOT/krita/krita-deps-management/tools/download-macos-tools.py # correct path
source $BUILDROOT/_krita-tools/activate
Traceback (most recent call last):
  File "/Users/cold/w/krita-build/krita/krita-deps-management/tools/download-macos-tools.py", line 17, in <module>
    downloadsDir = os.environ.pop('EXTERNALS_DOWNLOAD_DIR')
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<frozen _collections_abc>", line 945, in pop
  File "<frozen os>", line 714, in __getitem__
KeyError: 'EXTERNALS_DOWNLOAD_DIR'
source: no such file or directory: /Users/cold/w/krita-build/_krita-tools/activate

Fine, the script wants a EXTERNALS_DOWNLOAD_DIR, the docs said nothing about one?

To fix this you actually need to set this variable to "somewhere", it preferably inside the container folder, so:

cd $BUILDROOT
export EXTERNALS_DOWNLOAD_DIR="$BUILDROOT/_download"
python3 $BUILDROOT/krita/krita-deps-management/tools/download-macos-tools.py # correct path
source $BUILDROOT/_krita-tools/activate

Where did I get the /_download path from? I made it up! Let's see if it works!

Im a bit confused because on the prerequisites they ask us to download CMake, but then download cmake, ccache and ninja, on this script, in a optional step? Im not sure if this step is really optional because ninja and ccache were not listed as requirements.

Anyways, this script downloads those tools and add them to our current path as seen on the script:

export PATH=/Users/cold/w/krita-build/_krita-tools/ninja:$PATH
export PATH=/Users/cold/w/krita-build/_krita-tools/cmake-3.29.3-macos-universal/CMake.app/Contents/bin:$PATH
export PATH=/Users/cold/w/krita-build/_krita-tools/ccache-4.9.1-darwin:$PATH

Useful to know, because maybe you are using different versions of ninja,cmake and ccache, you can use those to configure your IDE to use the proper tools.

But yeah, two more folders for the krita build:

(venv) ➜  krita-build ls
_download    _krita-tools krita        venv

Fetching prebuilt dependencies

The guide then asks us to do this:

cd $BUILDROOT/krita
source $BUILDROOT/venv/bin/activate
source $BUILDROOT/_krita-tools/activate # if you used CI build tools

python krita-deps-management/tools/setup-env.py --full-krita-env -v $BUILDROOT/venv -p $BUILDROOT/$KDECI_CRAFT_PLATFORM/dev-utils/bin/

I do not have a $KDECI_CRAFT_PLATFORM set, but looking at the script its just to add that folder to the path? probably looking for some extra build tools, as this guide was copy-pasted straight from their CI config, so don't worry about that.

And I quote the doc here:

"

The script will generate the following environment for you:

  • $BUILDROOT/krita/_install — the install prefix for Krita with all the deps preinstalled
  • $BUILDROOT/krita/_build — the build folder for Krita
  • $BUILDROOT/krita/env — a script for build environment activation
  • $BUILDROOT/krita/env_deactivate — a script for build environment de-activation

"

Which is good! as it says, next time you reopen the terminal you can just source $BUILDROOT/krita/env to get the right settings. AND source $BUILDROOT/_krita-tools/activate but it is told later.

Alas, the command that will actually build Krita:

cd $BUILDROOT/krita
source $BUILDROOT/krita/env
source $BUILDROOT/_krita-tools/activate # if you used CI build tools
mkdir -p _build
cd _build
cmake -DCMAKE_BUILD_TYPE=RelWithDebInfo \
      -DHIDE_SAFE_ASSERTS=OFF \
      -DBUILD_TESTING=ON \
      -DCMAKE_INSTALL_PREFIX=$BUILDROOT/krita/_install \
      -DCMAKE_TOOLCHAIN_FILE=$BUILDROOT/krita/krita-deps-management/tools/macos-toolchain-krita.cmake \
      $BUILDROOT/krita
ninja -j8 install

Except that it won't but ok.

Does a bunch of stuff, then fails with ninja: error: loading 'build.ninja': No such file or directory.

Maybe thats again, extremily outdated documentation, because there is not a single file nammed build.ninja in the entirety of krita-build sub-directories...

Anyway, make install -j8 will do the same thing, so after having ran the previous cmake command, run:

cd $BUILDROOT/krita/_build
make install -j8

Then go grab a coffe, do your guitar pratice routine or something, it takes a little while to build.

Anyways after a couple of hours we should get:

[ 93%] Linking CXX static library libkritamypaintop_static.a
fatal error: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ranlib: can't write to output file (No space left on device)

Fuckin non-upgradeable storage...

Well, let's remove some node modules... But yeah, don't worry if the build suddenly dies in the middle, it should be cached if you restart it, just make isntall -j8 again.

A few more minutes later:

-- Up-to-date: /Users/cold/w/krita-build/krita/_install/share/locale/tg/LC_MESSAGES
-- Installing: /Users/cold/w/krita-build/krita/_install/share/locale/tg/LC_MESSAGES/krita.mo
(krita) ➜  _build git:(master) ✗

Good, lets see if it runs:

$BUILDROOT/krita/_install/bin/krita.app/Contents/MacOS/krita

If it doesn't explodes immediately after pressing enter, then it probably worked, just wait a bit.

You should be seeing Krita UI after maybe a minute or two, if you can't, don't ask me whats up, idk.

But yeah, running once is good and all, how about changing something.

Let's say you are trying to debug the extremily annoying bug where when you rotate the canvas with the trackpad on a macbook, the canvas starts walking arround ever so slightly, lets go to libs/ui/canvas/kis_coordinates_converter.cpp , over to the function void KisCoordinatesConverter::beginRotation and put a qInfo() << "Begin Rotation"; anywhere:

void KisCoordinatesConverter::beginRotation()
{
     KIS_SAFE_ASSERT_RECOVER_NOOP(!m_d->isRotating);

+    qInfo() << "Begin Rotation";

Ok, change made, now you need to recompile.

As to not get lost again, try closing all your terminals, and going over the Krita folder again, you will see that there is no (krita) prefix on the command line agian, thats because you didn't enabled the virtual enviroment, or whatever that is.

To fix that, just run those two commands again (inside the krita-build folder, not the krita folder)

source ./krita/env
source ./_krita-tools/activate

Then, go over to the _build folder again, and run make install

cd ./krita/_build
make install -j8

That should try to recompile the changed file, if we run ./krita/_install/bin/krita.app/Contents/MacOS/krita again, we should see our (begin rotation) message being logged on the console once we do a rotation with the trackpad, I sure did!

# if you are inside the build folder, but yeah the _install folder is inside the krita folder
../_install/bin/krita.app/Contents/MacOS/krita

What about pykrita extensions?

Yeah, maybe you are missing some of your plugins, if you go over "Settings" > "Manage Resources" > "Open Resource Folder", you will see the dev build uses the standard resource folder at '~/Library/Application Support/krita', so you just need to the Krita settings and make sure the plugin is enabled.

Making intelisense work in VSCode

First, install the C++ Extension pack from the store, and open the krita folder. Lets see what happens:

CMake extensions will ask which compiler to use, select unspecified, we won't be compiling the code via the extension anyways, we only care about the intelisense and debugging.

CMake Error at /Users/cold/w/krita-build/_krita-tools/cmake-3.29.3-macos-universal/CMake.app/Contents/share/cmake-3.29/Modules/FindPackageHandleStandardArgs.cmake:230 (message):
[cmake]   Could NOT find LibExiv2: Found unsuitable version "..", but required is at
[cmake]   least "0.16" (found LibExiv2_LIBRARIES-NOTFOUND)
[cmake] Call Stack (most recent call first):
[cmake]   /Users/cold/w/krita-build/_krita-tools/cmake-3.29.3-macos-universal/CMake.app/Contents/share/cmake-3.29/Modules/FindPackageHandleStandardArgs.cmake:598 (_FPHSA_FAILURE_MESSAGE)
[cmake]   cmake/modules/FindLibExiv2.cmake:78 (find_package_handle_standard_args)
[cmake]   CMakeLists.txt:1100 (find_package)
[cmake] 
[cmake] 
[cmake] -- Configuring incomplete, errors occurred!

Yeah, its not happening, if you scroll all the way to the top we can see this message:

[cmake] CMake Warning at cmake/modules/FindPythonLibrary.cmake:68 (message):
[cmake]   CMAKE_PREFIX_PATH variable is not set, we might NOT be able to detect SIP

If you search the project by CMAKE_PREFIX_PATH you will see that in the env file it was set to point to the _install folder, this is what we are doing when we run source ./krita/env and source ./_krita-tools/activate before a build, we are just updating our enviroment variables and setting which tools to use.

So, the CMake that VSCode runs, needs this and all other variables from this env file.

Again, since we are not compiling from VSCode, we are going to re-run the make script, we only care about VSCode being able to configure the project properly.

So, click on the CMake button on the sidebar, click the gear icon on the section "Project Status", scroll over to Cmake: Configure Enviroment.

It has a "enviroment variable" editor, a very TRASH one, because it takes multiple clicks to set one, and we need to set several, yeah no thank you. Add a X variable with value Y (literally), press ok to save, then scroll to Cmake: Configure Settings then click Edit in settings.json, in the JSON it opens you can remove the cmake.configureSettings, focus on the cmake.configureEnvironment, there our X and Y there, remove that too:

    ...
    "cmake.configureEnvironment": {
        
    }
    ...

Alright, now we just need to figure out which variables we need to set, if you look at the env file on the krita folder, you will notice it has all the enviroment variables from your sistem as usual, plus some others like QT_DATA_DIRS pointing to the krita folder.

We could copy everything and put there, or you can write a script to go over all the variables on that file one by one, and just grabs the one that are not set on your regular shell, make sure to not run that script on a (krita) shell.

For me those were the env vars, maybe they are the same for you:

KDECI_ENV_ACTIVATION_SCRIPT
KDECI_ENV_DEACTIVATION_SCRIPT
KDECI_CC_CACHE
EXTERNALS_DOWNLOAD_DIR
KDECI_COMPRESS_PACKAGES_ON_DOWNLOAD
KDECI_REPO_METADATA_PATH
VIRTUAL_ENV
QMAKE_MACOSX_DEPLOYMENT_TARGET
KDECI_GLOBAL_CONFIG_OVERRIDE_PATH
MACOSX_DEPLOYMENT_TARGET
KDECI_BUILD_TYPE
VIRTUAL_ENV_PROMPT
CMAKE_PREFIX_PATH
IGNORE_THIS_VARIABLE
PKG_CONFIG_PATH
XDG_DATA_DIRS
QMAKEFEATURES
QT_DATA_DIRS
QT_SELECT
XDG_CURRENT_DESKTOP
ASAN_OPTIONS
KDECI_BUILD
CCACHE_DIR
CMAKE_C_COMPILER_LAUNCHER
CMAKE_CXX_COMPILER_LAUNCHER
PYTHONDONTWRITEBYTECODE

Your config should look like this:

    "cmake.configureEnvironment": {
        "KDECI_ENV_ACTIVATION_SCRIPT": "<YOUR_USER_FOLDER>krita-build/krita/env",
        "KDECI_ENV_DEACTIVATION_SCRIPT": "<YOUR_USER_FOLDER>krita-build/krita/env_deactivate",
        "KDECI_CC_CACHE": "<YOUR_USER_FOLDER>krita-build/krita/ccache",
        "EXTERNALS_DOWNLOAD_DIR": "<YOUR_USER_FOLDER>krita-build/krita/cache/downloads",
        "KDECI_COMPRESS_PACKAGES_ON_DOWNLOAD": "1",
        "KDECI_REPO_METADATA_PATH": "<YOUR_USER_FOLDER>krita-build/krita/krita-deps-management/repo-metadata",
        "VIRTUAL_ENV": "<YOUR_USER_FOLDER>krita-build/venv",
        "QMAKE_MACOSX_DEPLOYMENT_TARGET": "10.14",
        "KDECI_GLOBAL_CONFIG_OVERRIDE_PATH": "<YOUR_USER_FOLDER>krita-build/krita/krita-deps-management/global-config.yml",
        "MACOSX_DEPLOYMENT_TARGET": "10.14",
        "KDECI_BUILD_TYPE": "Release",
        "VIRTUAL_ENV_PROMPT": "(krita) ",
        "CMAKE_PREFIX_PATH": "<YOUR_USER_FOLDER>krita-build/krita/_install",
        "IGNORE_THIS_VARIABLE": "<YOUR_USER_FOLDER>krita-build/krita/_install/lib",
        "PKG_CONFIG_PATH": "<YOUR_USER_FOLDER>krita-build/krita/_install/lib/pkgconfig:<YOUR_USER_FOLDER>krita-build/krita/_install/share/pkgconfig",
        "XDG_DATA_DIRS": "<YOUR_USER_FOLDER>krita-build/krita/_install/share",
        "QMAKEFEATURES": "<YOUR_USER_FOLDER>krita-build/krita/_install/mkspecs/features/mac",
        "QT_DATA_DIRS": "<YOUR_USER_FOLDER>krita-build/krita/_install/share",
        "QT_SELECT": "qt5",
        "XDG_CURRENT_DESKTOP": "KDE",
        "ASAN_OPTIONS": "detect_leaks=0:new_delete_type_mismatch=0:detect_odr_violation=0:stack-use-after-scope=0:alloc_dealloc_mismatch=0",
        "KDECI_BUILD": "TRUE",
        "CCACHE_DIR": "<YOUR_USER_FOLDER>krita-build/krita/ccache/krita",
        "CMAKE_C_COMPILER_LAUNCHER": "ccache",
        "CMAKE_CXX_COMPILER_LAUNCHER": "ccache",
        "PYTHONDONTWRITEBYTECODE": "1",
    }

Just replace <YOUR_USER_FOLDER> with your user home folder.

Save the file, reload the window, and you should see that in the CMake/Build output view, it now correctly is able to configure the project.

If you try opening libs/ui/canvas/kis_coordinates_converter.cpp now, you should be able to go to definition of everything and use auto-complete for object fields.

The autocomplete is relatively fast, but the go-to defintion can take like one minute????

Making intelisense work in CLion

Open CLion on the krita folder.

It will prompt you to configure CMake project, select RelWithDebInfo as the build type.

Set the build directory to __build (we wont be using that).

And then on the Enviroment Variables, click on the paper icon, remember the script you made to select the correct enviroment variables on the previous section? yeah rewrite it to output the env-vars in the format of X=Y, for example this is mine:

KDECI_ENV_ACTIVATION_SCRIPT=<YOUR_BUILD_FOLDER>/krita/env
KDECI_ENV_DEACTIVATION_SCRIPT=<YOUR_BUILD_FOLDER>/krita/env_deactivate
KDECI_CC_CACHE=<YOUR_BUILD_FOLDER>/krita/ccache
EXTERNALS_DOWNLOAD_DIR=<YOUR_BUILD_FOLDER>/krita/cache/downloads
KDECI_COMPRESS_PACKAGES_ON_DOWNLOAD=1
KDECI_REPO_METADATA_PATH=<YOUR_BUILD_FOLDER>/krita/krita-deps-management/repo-metadata
VIRTUAL_ENV=<YOUR_BUILD_FOLDER>/venv
QMAKE_MACOSX_DEPLOYMENT_TARGET=10.14
KDECI_GLOBAL_CONFIG_OVERRIDE_PATH=<YOUR_BUILD_FOLDER>/krita/krita-deps-management/global-config.yml
MACOSX_DEPLOYMENT_TARGET=10.14
KDECI_BUILD_TYPE=Release
VIRTUAL_ENV_PROMPT=(krita) 
CMAKE_PREFIX_PATH=<YOUR_BUILD_FOLDER>/krita/_install
IGNORE_THIS_VARIABLE=<YOUR_BUILD_FOLDER>/krita/_install/lib
PKG_CONFIG_PATH=<YOUR_BUILD_FOLDER>/krita/_install/lib/pkgconfig:<YOUR_BUILD_FOLDER>/krita/_install/share/pkgconfig
XDG_DATA_DIRS=<YOUR_BUILD_FOLDER>/krita/_install/share
QMAKEFEATURES=<YOUR_BUILD_FOLDER>/krita/_install/mkspecs/features/mac
QT_DATA_DIRS=<YOUR_BUILD_FOLDER>/krita/_install/share
QT_SELECT=qt5
XDG_CURRENT_DESKTOP=KDE
ASAN_OPTIONS=detect_leaks=0:new_delete_type_mismatch=0:detect_odr_violation=0:stack-use-after-scope=0:alloc_dealloc_mismatch=0
KDECI_BUILD=TRUE
CCACHE_DIR=<YOUR_BUILD_FOLDER>/krita/ccache/krita
CMAKE_C_COMPILER_LAUNCHER=ccache
CMAKE_CXX_COMPILER_LAUNCHER=ccache
PYTHONDONTWRITEBYTECODE=1

Or just copy this and replace <YOUR_BUILD_FOLDER> with your build folder.

Then copy this and just press the paste button on the env-var editor, and press ok, it will reload the project, and after it finishes processing the files you should be able to again, open Canvas.cpp and check that the autocomplete works.

Debugging Krita with CLion

I could waste the rest of my sunday night trying to make the build work through CLion, but we aready know how to run the script that does the build...

So we are just going to make a dummy launch target just so CLion can run our binary and attach the debugger to it:

Ok, so on the side of the "Debug" button there should be 3 dots, click on it, Configuration > Edit, on this screen you will create another Run configuration.

Click the + icon on the top left, select Native Application, next we need to trick CLion into building nothing, so on the Target input, click the gear icon, create a new build target. Call it "Dummy" or whatever, on Build click the 3 dots, + on the top left. ANOTHER window opens, name it "Dummy Build", use echo "Cold is the greatest" as the build command, it has to be that string otherwise it wont work, uncheck everything from advanced options, click ok, click ok, click ok, back to the Run/Debug Configurarions screen, select Dummy (at the very bootom of the list of targetz) as your build target. Then the executable, set it to your krita install location, which should be under krita/_install/bin/krita.app/Contents/MacOS/krita, dont copy and paste this string, just use the file picker on the editor to locate your folder. Press Ok.

Sidenote: if you really wants to rebuild by pressing a button, you could create a script like:

source ./env
cd ./_build
make install -j8

call it rebuild.sh, put it on the root of the krita folder, and chmod +x it, then use it instead as the echo command in the dummy build, that works as well.

Alright, now we should be able to debug the thing, again, open libs/ui/canvas/kis_coordinates_converter.cpp, put a breakpoint under KisCoordinatesConverter::beginRotation, then press the insect icon to debug in the editor. It will build nothing, and open your krita build with the debugger attached, then open any file and rotate, you should have your breakpoint hit.

If you don't, yeah that sucks... like, shit man, idk what to tell you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment