Changeset 6deab5a in mainline


Ignore:
Timestamp:
2024-01-06T19:32:57Z (6 months ago)
Author:
Vojtech Horky <vojtech.horky@…>
Branches:
master, topic/simplify-dev-export
Children:
25e1490
Parents:
88e188b2
git-author:
Vojtech Horky <vojtech.horky@…> (2024-01-06 19:32:56)
git-committer:
Vojtech Horky <vojtech.horky@…> (2024-01-06 19:32:57)
Message:

Improved and Pythonized helenos-pkg-config

Now it is able to get list of dependencies automatically and
does not need manual updates when new library is added.

We already use Python in other places so this should not break
any other use of this tool.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • tools/xcw/bin/helenos-pkg-config

    r88e188b2 r6deab5a  
    1 #!/bin/bash
     1#!/usr/bin/env python3
     2
    23#
    3 # Copyright (c) 2020 Jiri Svoboda
     4# Copyright (c) 2024 Vojtech Horky
    45# All rights reserved.
    56#
     
    3233#
    3334
    34 XCW="$(dirname "$0")"
    35 BUILD_ROOT="$(dirname "$(dirname "$(dirname "$XCW")")")"
    36 if [ -z "$EXPORT_DIR" ]; then
    37         EXPORT_DIR="$BUILD_ROOT/export"
    38 fi
    3935
    40 INCLUDE_DIR="$EXPORT_DIR/include"
    41 LIB_DIR="$EXPORT_DIR/lib"
     36import argparse
     37import os
     38import sys
    4239
    43 libmath_cflags="-I$INCLUDE_DIR/libmath"
    44 libmath_libs="$LIB_DIR/libmath.a"
     40# Package dependencies: always list only immediate dependencies so that
     41# we can get proper ordering for static libraries
     42CONFIG_DEPENDENCIES = {
     43    'libui': ['libdisplay', 'libcongfx', 'libgfxfont', 'libriff', 'libmemgfx', 'libconsole'],
     44    'libdisplay': ['libgfx', 'libipcgfx', 'libinput'],
     45    'libconsole': ['libinput', 'liboutput'],
     46    'libipcfgfx': ['libgfx'],
     47    'libgfximage': ['libpixconv'],
     48}
    4549
    46 libui_cflags="-I$INCLUDE_DIR/libui -I$INCLUDE_DIR/libdisplay -I$INCLUDE_DIR/libgfx -I$INCLUDE_DIR/libipcgfx -I$INCLUDE_DIR/libcongfx"
    47 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"
     50CONFIG_CFLAGS = {
     51}
    4852
    49 libgfximage_cflags="-I$INCLUDE_DIR/libgfximage"
    50 libgfximage_libs="$LIB_DIR/libgfximage.a $LIB_DIR/libpixconv.a"
     53CONFIG_LIBS = {
     54}
    5155
    52 libhound_cflags="-I$INCLUDE_DIR/libhound"
    53 libhound_libs="$LIB_DIR/libhound.a"
     56def get_with_dependencies(package, dependencies_first=True):
     57    deps = CONFIG_DEPENDENCIES.get(package, [])
     58    all_deps = [
     59        i
     60        for d in deps
     61        for i in get_with_dependencies(d, dependencies_first)
     62    ]
     63    result = (all_deps if dependencies_first else []) + [package] + ([] if dependencies_first else all_deps)
     64    seen = set()
     65    return [
     66        i
     67        for i in result
     68        if not (i in seen or seen.add(i))
     69    ]
    5470
    55 libpcm_cflags="-I$INCLUDE_DIR/libpcm"
    56 libpcm_libs="$LIB_DIR/libpcm.a"
     71def get_build_root():
     72    dn = lambda x : os.path.dirname(x)
     73    return dn(dn(dn(dn(os.path.abspath(sys.argv[0])))))
    5774
    58 libgfx_cflags="-I$INCLUDE_DIR/libgfx"
    59 libgfx_libs="$LIB_DIR/libgfx.a"
     75def main():
     76    args = argparse.ArgumentParser(description='pkg-config-like tool for HelenOS')
     77    args.add_argument(
     78        '--cflags',
     79        dest='cflags',
     80        action='store_true',
     81        default=False,
     82        help='Print required C compiler flags to stdout.'
     83    )
     84    args.add_argument(
     85        '--libs',
     86        dest='libs',
     87        action='store_true',
     88        default=False,
     89        help='Print required linker flags to stdout.'
     90    )
     91    args.add_argument('packages', metavar='PACKAGE', nargs='+')
    6092
    61 libipcgfx_cflags="-I$INCLUDE_DIR/libipcgfx"
    62 libipcgfx_libs="$LIB_DIR/libipcgfx.a"
     93    config = args.parse_args()
    6394
    64 libdisplay_cflags="-I$INCLUDE_DIR/libdisplay"
    65 libdisplay_libs="$LIB_DIR/libdisplay.a"
     95    export_dir = os.getenv('EXPORT_DIR', get_build_root())
    6696
    67 action=none
    68 pkg=
     97    result = []
     98    for package in config.packages:
     99        if config.cflags:
     100            for i in get_with_dependencies(package, False):
     101                if i in CONFIG_CFLAGS:
     102                    result.extend(CONFIG_CFLAGS[i])
     103                else:
     104                    result.append(f'-I{export_dir}/include/{i}')
     105        if config.libs:
     106            for i in get_with_dependencies(package, False):
     107                if i in CONFIG_LIBS:
     108                    result.extend(CONFIG_LIBS[i])
     109                else:
     110                    result.append(f'{export_dir}/lib/{i}.a')
    69111
    70 while [ ".$1" != . ] ; do
    71         case ".$1" in
    72         (.--cflags) action=cflags;;
    73         (.--libs) action=libs;;
    74         (.-*) echo "Uknwown option $1" >&2; exit 1;;
    75         (.*)
    76             case "$1" in
    77             (libui) ;;
    78             (libgfximage) ;;
    79             (libmath) ;;
    80             (libhound) ;;
    81             (libpcm) ;;
    82             (libgfx) ;;
    83             (libipcgfx) ;;
    84             (libdisplay) ;;
    85             (*) echo "Unknown package $1" >&2; exit 1;;
    86             esac
     112    print(' '.join(result))
    87113
    88             echo "$pkg" | grep -w "$1" >/dev/null 2>&1
    89             if [ $? -ne 0 ] ; then
    90                     pkg="$pkg $1"
    91             fi;;
    92         esac
    93         shift 1
    94 done
    95114
    96 if [ ."$pkg" = . ]; then
    97         echo "Package name(s) required." >&2
    98         exit 1
    99 fi
    100 
    101 for p in $pkg ; do
    102         case "$action" in
    103         (cflags) eval "printf ' %s' \$${p}_cflags";;
    104         (libs) eval "printf ' %s' \$${p}_libs";;
    105         esac
    106 done
    107 
    108 echo
     115if __name__ == '__main__':
     116    main()
Note: See TracChangeset for help on using the changeset viewer.