Skip to main content Skip to navigation

Developing Krita in Visual Studio Code, part 3

Categories:
Everyone uses deevad's pics for Krita's building instructions 😄 Credits: David Revoy, CC-BY-4.0

Hi again, everyone! This is the final post for this series, in which I’ll show you how to set Krita up in Visual Studio Code. For this post, we’ll cover the Windows operating system. I’ll show you how to take advantage of the same build scripts maintainers use for building the Windows releases.


In the spirit of David’s post and Krita’s docs, this post has the following sections:

So, without further ado, let’s begin!

A warning: this is a distilled version of the official, updated documentation, ported to Windows. It’s optimized to set you up under Visual Studio Code.

Preparing your development environment

Credits: David Revoy, CC-BY-4.0

We’ll set up a main folder called krita in one of our drives, for instance C:\.

mkdir C:\krita

Getting the Source Code

Credits: David Revoy, CC-BY-4.0

We’ll grab a copy of the source code from KDE’s GitLab instance. Install the Windows version of Git from git-scm.com. Then, let’s go to our krita folder,

cd C:\krita

and run:

git clone https://invent.kde.org/kde/krita.git src

If you already have a copy, skip the above and let’s check it’s properly updated:

cd src
git pull

Getting the Compiler and Python

Credits: David Revoy, CC-BY-4.0

Compiling in Windows is special, because Krita doesn’t use Microsoft’s compiler. We use instead a port of GCC called mingw-w64. We’ll grab version 7.3 (by mingw-builds) from the KDE FTP site. Uncompress this file to a subdirectory named toolchain inside our krita folder.

Next, we’ll set up Python. As of the writing of this post, we use version 3.8.1 exactly. Go to its download page, download the installer that matches the architecture you’ll compile for (which is almost always 64-bit), and install it.

Finally, the most difficult of our prerequisites is… Windows SDK. You can get it through the Visual Studio Installer, or by going to the Windows Dev Center. After installation, make sure the following environment variables are set:

  • WindowsSdkDir: typically set to C:\Program Files (x86)\Windows Kits\10;
  • WindowsSdkVerBinPath: typically set to C:\Program Files (x86)\Windows Kits\10\bin\10.0.18362.0, where 10.0.18362.0 is the version of the SDK that you installed.

Configuring the Build

Credits: David Revoy, CC-BY-4.0

This was the most difficult part in our Linux journey. However, in Windows, we can skip a lot of the hard work by setting up Build tasks! You must install first the CMake build system.

Do not use the CMake Tools extension here; it will really complicate things.

Now, open our krita folder under Visual Studio Code,

code ~/krita

open the Command Palette (Ctrl+Shift+P), select Tasks: Configure Tasks, then Create tasks.json file from template.

A JSON file will open. Replace its contents with the following:

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "label": "Krita",
            "type": "shell",
            "args": [
                "--no-interactive",
                "--skip-deps",
                "--jobs",
                "12",
            ],
            "options": {
                "cwd": "${workspaceFolder}",
                "shell": {
                    "executable": "${env:SYSTEMROOT}\\system32\\cmd.exe",
                    "args": [
                        "/C"
                    ]
                },
                "env": {
                    "PATH": "${workspaceFolder}\\toolchain\\bin;${env:PATH}"
                },
            },
            "command": "${workspaceFolder}\\src\\build-tools\\windows\\build.cmd",
            "detail": "Compile Krita with the official script",
            "group": "build"
        },
        {
            "label": "Dependencies",
            "type": "shell",
            "args": [
                "--no-interactive",
                "--skip-krita",
                "--jobs",
                "12",
            ],
            "options": {
                "cwd": "${workspaceFolder}",
                "shell": {
                    "executable": "${env:SYSTEMROOT}\\system32\\cmd.exe",
                    "args": [
                        "/C"
                    ]
                },
                "env": {
                    "PATH": "${workspaceFolder}\\toolchain\\bin;${env:PATH}"
                },
            },
            "command": "${workspaceFolder}\\src\\build-tools\\windows\\build.cmd",
            "detail": "Compile dependencies with the official script",
            "group": "build"
        }
    ]
}

We have added two tasks called “Dependencies” and “Krita”, that use the script, build.cmd.

  • We have added our MinGW installation to the PATH by setting it in the task’s options.env value.
  • We have set the working directory (cwd) to ${workspaceFolder}, because build.cmd expects to be run from the krita` folder.
  • The script has the following arguments:
    • --no-interactive uses default paths and goes straight to the build process.
    • --skip-krita and --skip-deps skips building Krita and dependencies, respectively.
    • --jobs 12 tells CMake to build dependencies using 12 threads. You can adjust it to your tastes, or remove it to let the script autodetect the number of available threads.
Credits: David Revoy, CC-BY-4.0

That’s it! Open the Command Palette again, select Tasks: Run Build Task (or press Ctrl+Shift+B), and run:

  1. First, the Dependencies task.
  2. Then, the Krita task. And off you go!

Running Krita

Credits: David Revoy, CC-BY-4.0

You’ll probably want to run Krita inside a debugger. Good news is that you can do it from the comfort of Visual Studio Code, through a Launch task!

Open the Command Palette again, and select Debug: Open launch.json. My configuration adds a target called (gdb) Start:

{
    // https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "(gdb) Start",
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceFolder}\\i\\bin\\krita.exe",
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [
                {
                    "name": "PATH",
                    "value": "${workspaceFolder}\\i_deps\\bin\\;${workspaceFolder}\\toolchain\\bin;${env:PATH}"
                }
            ],
            "MIMode": "gdb",
            "miDebuggerPath": "${workspaceFolder}\\toolchain\\bin\\gdb.exe",
            "setupCommands": [
                {
                    "description": "Enable pretty printing for GDB",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ]
        }
    ]
}

The most important bits are:

  • program: it points to the krita executable, installed inside our install subdirectory;
  • environment: we have to show our debugger the right path to Krita’s libraries, through the PATH environment variable.
  • miDebuggerPath: we have to use the gdb executable that comes bundled with our copy of MinGW.

After this, you can launch Krita by just pressing F5! Or also by opening the Command Palette, and selecting Debug: Start Debugging.

Credits: David Revoy, CC-BY-4.0

Updating

Credits: David Revoy, CC-BY-4.0

Thanks to Git, this is really easy. Go to our src folder and pull the last changes:

cd ~/krita/src
git pull

If you have a branch, check it out first:

cd ~/krita/src
git checkout MY_BRANCH
git pull

Then build and install Krita. Open the Command Palette, select CMake: Build Target, install.

Troubleshooting

Credits: David Revoy, CC-BY-4.0
  • Building dependencies through the scripts need a working Internet connection. Please check you have one before trying!
  • Sometimes, you’ll be unable to set Build tasks up. If this option doesn’t appear, it’s usually because your Visual Studio Code has User tasks already configured. Try opening the suggested “echo” task, delete it, and try again.
  • To set environment variables, open the Start menu, then select Run, and enter rundll32 sysdm.cpl,EditEnvironmentVariables .
  • Make sure that the version of Python we’re using comes first in your PATH, for instance, by setting it up in the Build task. Do not set the PYTHONHOME or PYTHONPATH environment variables.
  • Make sure all the variables are properly set! If not, the dependencies build process WILL fail with the most strange errors.

That is all for this process! Remember, if you experience any issues, please do ping me, amyspark @ #krita on Freenode.

Cheers,

~amyspark