Index: tools/xcw/bin/helenos-pkg-config
===================================================================
--- tools/xcw/bin/helenos-pkg-config	(revision 56a3c29e88b204aab857b6e159ccf9e553cf0c09)
+++ tools/xcw/bin/helenos-pkg-config	(revision 8996582a5a68dd2f404f0d237aa5a427d746962c)
@@ -1,5 +1,6 @@
-#!/bin/bash
+#!/usr/bin/env python3
+
 #
-# Copyright (c) 2020 Jiri Svoboda
+# Copyright (c) 2024 Vojtech Horky
 # All rights reserved.
 #
@@ -32,77 +33,84 @@
 #
 
-XCW="$(dirname "$0")"
-BUILD_ROOT="$(dirname "$(dirname "$(dirname "$XCW")")")"
-if [ -z "$EXPORT_DIR" ]; then
-	EXPORT_DIR="$BUILD_ROOT/export"
-fi
 
-INCLUDE_DIR="$EXPORT_DIR/include"
-LIB_DIR="$EXPORT_DIR/lib"
+import argparse
+import os
+import sys
 
-libmath_cflags="-I$INCLUDE_DIR/libmath"
-libmath_libs="$LIB_DIR/libmath.a"
+# Package dependencies: always list only immediate dependencies so that
+# we can get proper ordering for static libraries
+CONFIG_DEPENDENCIES = {
+    'libui': ['libdisplay', 'libcongfx', 'libgfxfont', 'libriff', 'libmemgfx', 'libconsole'],
+    'libdisplay': ['libgfx', 'libipcgfx', 'libinput'],
+    'libconsole': ['libinput', 'liboutput'],
+    'libipcfgfx': ['libgfx'],
+    'libgfximage': ['libpixconv'],
+}
 
-libui_cflags="-I$INCLUDE_DIR/libui -I$INCLUDE_DIR/libdisplay -I$INCLUDE_DIR/libgfx -I$INCLUDE_DIR/libipcgfx -I$INCLUDE_DIR/libcongfx"
-libui_libs="$LIB_DIR/libui.a $LIB_DIR/libdisplay.a $LIB_DIR/libipcgfx.a $LIB_DIR/libcongfx.a $LIB_DIR/libgfx.a $LIB_DIR/libgfxfont.a $LIB_DIR/libriff.a $LIB_DIR/libmemgfx.a"
+CONFIG_CFLAGS = {
+}
 
-libgfximage_cflags="-I$INCLUDE_DIR/libgfximage"
-libgfximage_libs="$LIB_DIR/libgfximage.a $LIB_DIR/libpixconv.a"
+CONFIG_LIBS = {
+}
 
-libhound_cflags="-I$INCLUDE_DIR/libhound"
-libhound_libs="$LIB_DIR/libhound.a"
+def get_with_dependencies(package, dependencies_first=True):
+    deps = CONFIG_DEPENDENCIES.get(package, [])
+    all_deps = [
+        i
+        for d in deps
+        for i in get_with_dependencies(d, dependencies_first)
+    ]
+    result = (all_deps if dependencies_first else []) + [package] + ([] if dependencies_first else all_deps)
+    seen = set()
+    return [
+        i
+        for i in result
+        if not (i in seen or seen.add(i))
+    ]
 
-libpcm_cflags="-I$INCLUDE_DIR/libpcm"
-libpcm_libs="$LIB_DIR/libpcm.a"
+def get_build_root():
+    dn = lambda x : os.path.dirname(x)
+    return dn(dn(dn(dn(os.path.abspath(sys.argv[0])))))
 
-libgfx_cflags="-I$INCLUDE_DIR/libgfx"
-libgfx_libs="$LIB_DIR/libgfx.a"
+def main():
+    args = argparse.ArgumentParser(description='pkg-config-like tool for HelenOS')
+    args.add_argument(
+        '--cflags',
+        dest='cflags',
+        action='store_true',
+        default=False,
+        help='Print required C compiler flags to stdout.'
+    )
+    args.add_argument(
+        '--libs',
+        dest='libs',
+        action='store_true',
+        default=False,
+        help='Print required linker flags to stdout.'
+    )
+    args.add_argument('packages', metavar='PACKAGE', nargs='+')
 
-libipcgfx_cflags="-I$INCLUDE_DIR/libipcgfx"
-libipcgfx_libs="$LIB_DIR/libipcgfx.a"
+    config = args.parse_args()
 
-libdisplay_cflags="-I$INCLUDE_DIR/libdisplay"
-libdisplay_libs="$LIB_DIR/libdisplay.a"
+    export_dir = os.getenv('EXPORT_DIR', get_build_root())
 
-action=none
-pkg=
+    result = []
+    for package in config.packages:
+        if config.cflags:
+            for i in get_with_dependencies(package, False):
+                if i in CONFIG_CFLAGS:
+                    result.extend(CONFIG_CFLAGS[i])
+                else:
+                    result.append(f'-I{export_dir}/include/{i}')
+        if config.libs:
+            for i in get_with_dependencies(package, False):
+                if i in CONFIG_LIBS:
+                    result.extend(CONFIG_LIBS[i])
+                else:
+                    result.append(f'{export_dir}/lib/{i}.a')
 
-while [ ".$1" != . ] ; do
-	case ".$1" in
-	(.--cflags) action=cflags;;
-	(.--libs) action=libs;;
-	(.-*) echo "Uknwown option $1" >&2; exit 1;;
-	(.*)
-	    case "$1" in
-	    (libui) ;;
-	    (libgfximage) ;;
-	    (libmath) ;;
-	    (libhound) ;;
-	    (libpcm) ;;
-	    (libgfx) ;;
-	    (libipcgfx) ;;
-	    (libdisplay) ;;
-	    (*) echo "Unknown package $1" >&2; exit 1;;
-	    esac
+    print(' '.join(result))
 
-	    echo "$pkg" | grep -w "$1" >/dev/null 2>&1
-	    if [ $? -ne 0 ] ; then
-		    pkg="$pkg $1"
-	    fi;;
-	esac
-	shift 1
-done
 
-if [ ."$pkg" = . ]; then
-	echo "Package name(s) required." >&2
-	exit 1
-fi
-
-for p in $pkg ; do
-	case "$action" in
-	(cflags) eval "printf ' %s' \$${p}_cflags";;
-	(libs) eval "printf ' %s' \$${p}_libs";;
-	esac
-done
-
-echo
+if __name__ == '__main__':
+    main()
