GTK3-cross-compile

Cross-compile a GTK3 app from Linux to Windows
git clone git://git.wrycode.com/wrycode/GTK3-cross-compile.git
Log | Files | Refs | README

commit 6f92dd9d47708ebcd2463adca8df20748083eddb
Author: Nick Econopouly <wry@mm.st>
Date:   Tue, 10 Dec 2019 22:19:59 -0500

Initial commit

Diffstat:
Aicon.rc | 1+
Amain.c | 59+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asettings.ini | 2++
Awinbuild.sh | 79+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
4 files changed, 141 insertions(+), 0 deletions(-)

diff --git a/icon.rc b/icon.rc @@ -0,0 +1 @@ +id ICON "icon.ico" diff --git a/main.c b/main.c @@ -0,0 +1,59 @@ +#include <gtk/gtk.h> +#include <stdlib.h> + +static void print_hello (GtkWidget *widget, gpointer data) { + g_print ("Hello World\n"); +} + +GdkPixbuf *create_pixbuf(const gchar * filename) { + + GdkPixbuf *pixbuf; + GError *error = NULL; + pixbuf = gdk_pixbuf_new_from_file(filename, &error); + + if (!pixbuf) { + + fprintf(stderr, "%s\n", error->message); + g_error_free(error); + } + + return pixbuf; +} + +static void activate (GtkApplication *app, gpointer user_data) { + GtkWidget *window; + GtkWidget *button; + GtkWidget *button_box; + + window = gtk_application_window_new (app); + gtk_window_set_title (GTK_WINDOW (window), "Window"); + gtk_window_set_default_size (GTK_WINDOW (window), 200, 200); + + button_box = gtk_button_box_new (GTK_ORIENTATION_HORIZONTAL); + gtk_container_add (GTK_CONTAINER (window), button_box); + + button = gtk_button_new_with_label ("Hello World"); + g_signal_connect (button, "clicked", G_CALLBACK (print_hello), NULL); + g_signal_connect_swapped (button, "clicked", G_CALLBACK (gtk_widget_destroy), window); + gtk_container_add (GTK_CONTAINER (button_box), button); + + GdkPixbuf *icon; + icon = create_pixbuf("./icon.ico"); + gtk_window_set_icon(GTK_WINDOW(window), icon); + + gtk_widget_show_all (window); +} + +int main (int argc, char **argv) { + putenv("GTK_CSD=0"); + + GtkApplication *app; + int status; + + app = gtk_application_new ("org.gtk.example", G_APPLICATION_FLAGS_NONE); + g_signal_connect (app, "activate", G_CALLBACK (activate), NULL); + status = g_application_run (G_APPLICATION (app), argc, argv); + g_object_unref (app); + + return status; +} diff --git a/settings.ini b/settings.ini @@ -0,0 +1,2 @@ +[Settings] +gtk-theme-name=Windows10 diff --git a/winbuild.sh b/winbuild.sh @@ -0,0 +1,79 @@ +#!/bin/bash +setup () { + # Create a fedora container + container=$(buildah from fedora) + + # Install dependencies + buildah run $container dnf -y install mingw64-gtk3 mingw32-binutils mingw32-nsiswrapper + + # Fix typo in mingw library + buildah run $container bash -c "sed -i -e 's/-Wl,-luuid/-luuid/g' /usr/x86_64-w64-mingw32/sys-root/mingw/lib/pkgconfig/gdk-3.0.pc" + + # Cache image to avoid re-downloading dependencies every time + buildah commit $container my-tutorial-app + + # Clean up + buildah rm $container +} + +build () { + # Create a new container from the base one we created + container=$(buildah from localhost/my-tutorial-app) + + # Folder to hold everything needed for the windows installer: + output_folder=windows + yes | rm -r $output_folder + mkdir $output_folder + + # Directory of your package in the container + folder=/root/app + + # Copy program into container + buildah copy $container . $folder + + # Name for the executable file on Windows + executable_name="Demo.exe" + + # make some folders we'll need + buildah run $container bash -c "cd $folder && mkdir -p $output_folder/share/themes $output_folder/etc/gtk-3.0" + + # generate a RC file with the windres utility to embed an icon into the .exe later on + buildah run $container bash -c "cd $folder && x86_64-w64-mingw32-windres icon.rc -O coff -o icon.res" + + # copy some project resources + buildah run $container bash -c "cd $folder && cp -r Windows10 $output_folder/share/themes && \ + cp settings.ini $output_folder/etc/gtk-3.0/ &&\ + cp icon.ico $output_folder" + + # Compile program + buildah run $container bash -c "cd $folder && x86_64-w64-mingw32-gcc -mwindows -o $executable_name main.c icon.res \`mingw64-pkg-config --cflags gtk+-3.0 --libs gtk+-3.0\`" + + # Copy executable into installation folder + buildah run $container bash -c "cd $folder && cp $executable_name $output_folder" + + # Copy mingw dlls into installation folder + # This part may need to be personalized + buildah run $container bash -c "yes | cp -r /usr/x86_64-w64-mingw32/sys-root/mingw/{bin/*.dll,share} $folder/$output_folder/" + + # Generate an installer + buildah run $container bash -ic "cd $folder/$output_folder && nsiswrapper --run $executable_name ./*" + + # Copy the output from the container + cp -ru $(buildah unshare buildah mount $container)$folder/$output_folder . + + # Clean up + buildah rm $container +} + +# This just checks whether the container already exists on your drive +buildah inspect localhost/my-tutorial-app &>/dev/null +return_value=$? + +if [ $return_value -eq 1 ] +then + echo "Initial container setup" + setup +fi + +# Build project +build