Package: gvsbuild
gvsbuildThis python script helps you build a fullGTKlibrary
stack for Windows using Visual Studio. Currently, GTK 3 and GTK 4 are supported.Install GTK OnlyIf you want to only run GTK on Windows and not build it yourself, you can download
a zip file from the last release and unzip it toC:\gtk.It comes with GTK4, Cairo, PyGObject, Pycairo, GtkSourceView5, adwaita-icon-theme, and
all of their dependencies.Note however that these binaries are provided “AS IS”, WITHOUT WARRANTY OF ANY KIND.
They just contain the output of our latest CI run. They are not tested, and we cannot
commit to timely updates even for security issues. We strongly recommend to build your
own binaries, especially if you plan to distribute them with your application or use them in
production.Environmental VariablesFinally, add GTK to your environmental variables with:$env:Path="C:\gtk\bin;"+$env:Path$env:LIB="C:\gtk\lib;"+$env:LIB$env:INCLUDE="C:\gtk\include;C:\gtk\include\cairo;C:\gtk\include\glib-2.0;C:\gtk\include\gobject-introspection-1.0;C:\gtk\lib\glib-2.0\include;"+$env:INCLUDEPyGObject and PyCairoIf you are going to use PyGObject and Pycairo, you also need to use the gvsbuild
generated wheels with yourPython virtualenvin order to work around thisPyGObject bug:pipinstall--force-reinstall(Resolve-PathC:\gtk\wheels\PyGObject*.whl)pipinstall--force-reinstall(Resolve-PathC:\gtk\wheels\pycairo*.whl)Build GTKThe script supports multiple versions of Visual Studio - at the moment we are
focusing on VS 2022, but we include projects for other versions, and we gladly
accept patches.The script focuses on GTK and the surrounding ecosystem (e.g. GStreamer).
However, we are open to adding more libraries as long as the contributor takes
on the responsibility for keeping it up to date. The supported projects are
modules in theprojectsdirectory.The script requires a working installation ofVisual Studio for Windows
Desktop,Python 3andmsys2. The script will download any additional tools
required to build the libraries and will use them from a local directory,
without any installation. As of today these tools include cmake, meson, ninja,
nuget and perl.The script fetches source tarballs for the projects from their original
locations, however in some cases it might be necessary to host a patched tarball
on GitHub. To ensure integrity of the downloaded files, the script checks the
SHA256 hash of each download. Downloads are done using TLS, using SSL
certificates provided by the system, but in case of error the download is tried
again ignoring certificate errors.Development EnvironmentChocoWe recommend usingChocolatelyas a package manager
in Windows.To install it, open PowerShell as an administrator, then execute:Set-ExecutionPolicyBypass-ScopeProcess-Force;iex((New-ObjectSystem.Net.WebClient).DownloadString('https://community.chocolatey.org/install.ps1'))To run local scripts in follow-on steps, also executeSet-ExecutionPolicy RemoteSigned. This allows for local PowerShell scripts
to run without signing, but still requires signing for remote scripts.GitTo setup a development environment in Windows installGitby executing as an administrator:chocoinstallgitMSYS2Both of the development environments in the next steps need MSYS2 installed.InstallMSYS2:Keep PowerShell open as administrator and execute:chocoinstallmsys2Building GTKFirst we will install the gvsbuild dependencies:Visual C++ build tools workload for Visual Studio 2022 Build ToolsPythonInstall Visual Studio 2022With your admin PowerShell terminal:chocoinstallvisualstudio2022-workload-vctoolsNote: Visual Studio versions 2013 (not for all projects), 2015, 2017, 2019, and 2022 are currently supported.Install the Latest PythonIn Windows, The official installer contains all the Python components and is the
best option for developers using Python for any kind of project.For more information on how to use the official installer, please see thefull installer instructions.
The default installation options should be fine for use with gvsbuild.Install the latest Python version using theofficial installer.Open a PowerShell terminal as a normal user and check the python version:py-3.12--versionInstall gvsbuildThe recommended way to install gvsbuild is with pipx. Open a new regular user
PowerShell terminal and execute:py-3.12-mpipinstall--userpipxpy-3.12-mpipxensurepathpipxinstallgvsbuildAlternatively, you can also use git to clone the repository and install it.
Open a new regular user PowerShell terminal and execute:mkdirC:\gtk-build\githubcdC:\gtk-build\githubgitclonehttps://github.com/wingtk/gvsbuild.gitcdC:\gtk-build\github\gvsbuildpython-mvenv.venv.\.venv\Scripts\activate.ps1pipinstall.Build GTKIn the same PowerShell terminal, execute:gvsbuildbuildgtk3Alternatively, if you want to build GTK 4, execute:gvsbuildbuildgtk4Grab a coffee, the build will take a few minutes to complete.Add GTK to Your Environmental VariablesFrom the Start menu, go to the Control Panel entry for “Edit environment variables for your account”.Double-click thePathrow in the top list of variables. Click “New” to add a new item to the list.Paste inC:\gtk-build\gtk\x64\release\binClick "OK" twice.Using GTK with Visual StudioOpen Visual Studio and "Create a new project" using the "Empty Project" templateOn the left, right click on "Source Files" and choose "Add", then "New Item..." and replace the name withmain.cPaste in the following contents, then save the file:#include <gtk/gtk.h>

static void activate_cb(GtkApplication *app) {
  GtkWidget *window = gtk_application_window_new(app);
  gtk_widget_set_visible(window, true);
}

int main(int argc, char **argv) {
  GtkApplication *app =
      gtk_application_new("org.app", G_APPLICATION_DEFAULT_FLAGS);
  g_signal_connect(app, "activate", G_CALLBACK(activate_cb), NULL);
  return g_application_run(G_APPLICATION(app), argc, argv);
}Go to your project's settings by right-clicking and choosing "Properties"On the left, open "C/C++", then choose "Command Line".Open "Powershell" and run the commandpkg-config --cflags gtk4 --msvc-syntaxPaste the result into the "Additional Options" field at the bottom of the Visual Studio Properties window.Still in the Visual Studio window, click on "Linker" and choose "Command Line". Do the same thing as the last step, except use the output ofpkg-config --libs gtk4 --msvc-syntaxClick "OK"In the top menu bar, click "Debug" and "Start Without Debugging"Using GTK with RustSeethe fantasticgtk-rsbook.
You can skip the "Install GTK 4" step, as the above steps ^ covered that.Use PyGObjectFirst, add GTK to your environment variables:$env:LIB="C:\gtk-build\gtk\x64\release\lib;"+$env:LIB$env:INCLUDE="C:\gtk-build\gtk\x64\release\include;C:\gtk-build\gtk\x64\release\include\cairo;C:\gtk-build\gtk\x64\release\include\glib-2.0;C:\gtk-build\gtk\x64\release\include\gobject-introspection-1.0;C:\gtk-build\gtk\x64\release\lib\glib-2.0\include;"+$env:INCLUDENext, add the--enable-giand--py-wheeloptions like:gvsbuildbuild--enable-gi--py-wheelgtk4pygobjectOnce that finishes, then you need to use the gvsbuild generated wheels with yourPython virtualenvin order to
work around thisPyGObject
bug:pipinstall--force-reinstall(Resolve-PathC:\gtk-build\build\x64\release\pygobject\dist\PyGObject*.whl)pipinstall--force-reinstall(Resolve-PathC:\gtk-build\build\x64\release\pycairo\dist\pycairo*.whl)Use Icons with GTKIf you are going to use SVG icons with a GTK app, you need to also need to buildlibrsvg. Normally you want
to build GTK withgvsbuild build gtk4 adwaita-icon-themewhich will include librsvg and hicolor-icon-theme.Other OptionsFor more information about the possible commands run:gvsbuild --helpTo get detailed help on the build command run:gvsbuild build --helpIt is possible to set some parameters from a file, e.g. vs2015-release.pro, putting
the @ character before the file name. The file contains the option, one per
line, separated by a carriage return:--vs-ver
14
--win-sdk
8.1
--configuration
releaseEven if the format is not the easier to write or read in this way we eliminate
the problem of escaping spaces is file names and directories. Then you can use
it:gvsbuild build @vs2015-release.pro gtk3-fullTroubleshootingIf a build fails, try rebuilding it with--clean, if that fails, try
rebuilding it with--from-scratchIf the download of a tarball fails a partial file will not pass the hash check,
delete the file and try again.OpenSSLIn addition to the setup instructions above, to build OpenSSL you also need the
Visual C++ 2013 Redistributable Package installed. To install it, open PowerShell
as administrator and execute:chocoinstallvcredist2013Similar to other packages, you can build OpenSSL by executing:gvsbuild build opensslDependency GraphTo see and analyze the dependency between the various projects, in text or in a
Graphviz format, use the script deps.py:gvsbuild deps --graph --gv-file test.gvWithout option a simple dependency of all the projects is printed, as usual with
--help a summary of the options/commands is printed.LicenseThis build script is licensed under the GPL2.0 license, see the COPYING file for
the full text.The binaries produced by the build script are licensed under the license terms
of the project that is built (ie glib is LGPL so you can use glib.dll built
with this script under the terms of LGPL).Patches included in the repository are licensed under the license terms of the
project they apply to.CreditsThis tool originated from a gtk-win32 PowerShell script created by theHexChatdevelopers for building it for Windows.
Although this project is now archived, you can explore the original project if you
are interested in the history athttps://github.com/hexchat/gtk-win32.Compiling the GTK stack on MSVC would not be possible without the incredible
work byFan Chun-wei. If you are interested in more
details of how this works, please seeCompiling the GTK+ (and Clutter) stack using
Visual C++ 2008 and
later.