Blame msvc/pc_base.py

Packit 0af36a
#!/usr/bin/python
Packit 0af36a
#
Packit 0af36a
# Simple utility script to generate the basic info
Packit 0af36a
# needed in a .pc (pkg-config) file, used especially
Packit 0af36a
# for introspection purposes
Packit 0af36a
Packit 0af36a
# This can be used in various projects where
Packit 0af36a
# there is the need to generate .pc files,
Packit 0af36a
# and is copied from GLib's $(srcroot)/build/win32
Packit 0af36a
Packit 0af36a
# Author: Fan, Chun-wei
Packit 0af36a
# Date: March 10, 2016
Packit 0af36a
Packit 0af36a
import os
Packit 0af36a
import sys
Packit 0af36a
import argparse
Packit 0af36a
Packit 0af36a
class BasePCItems:
Packit 0af36a
    def __init__(self):
Packit 0af36a
        self.base_replace_items = {}
Packit 0af36a
        self.exec_prefix = ''
Packit 0af36a
        self.includedir = ''
Packit 0af36a
        self.libdir = ''
Packit 0af36a
        self.prefix = ''
Packit 0af36a
        self.srcdir = os.path.dirname(__file__)
Packit 0af36a
        self.top_srcdir = self.srcdir + '\\..'
Packit 0af36a
        self.version = ''
Packit 0af36a
Packit 0af36a
    def setup(self, argv, parser=None):
Packit 0af36a
        if parser is None:
Packit 0af36a
            parser = argparse.ArgumentParser(description='Setup basic .pc file info')
Packit 0af36a
        parser.add_argument('--prefix', help='prefix of the installed library',
Packit 0af36a
                            required=True)
Packit 0af36a
        parser.add_argument('--exec-prefix',
Packit 0af36a
                            help='prefix of the installed programs, \
Packit 0af36a
                                  if different from the prefix')
Packit 0af36a
        parser.add_argument('--includedir',
Packit 0af36a
                            help='includedir of the installed library, \
Packit 0af36a
                                  if different from ${prefix}/include')
Packit 0af36a
        parser.add_argument('--libdir',
Packit 0af36a
                            help='libdir of the installed library, \
Packit 0af36a
                                  if different from ${prefix}/lib')
Packit 0af36a
        parser.add_argument('--version', help='Version of the package',
Packit 0af36a
                            required=True)
Packit 0af36a
        args = parser.parse_args()
Packit 0af36a
Packit 0af36a
        self.version = args.version
Packit 0af36a
Packit 0af36a
        # check whether the prefix and exec_prefix are valid
Packit 0af36a
        if not os.path.exists(args.prefix):
Packit 0af36a
            raise SystemExit('Specified prefix \'%s\' is invalid' % args.prefix)
Packit 0af36a
Packit 0af36a
        # use absolute paths for prefix
Packit 0af36a
        self.prefix = os.path.abspath(args.prefix).replace('\\','/')
Packit 0af36a
Packit 0af36a
        # check and setup the exec_prefix
Packit 0af36a
        if getattr(args, 'exec_prefix', None) is None:
Packit 0af36a
            exec_prefix_use_shorthand = True
Packit 0af36a
            self.exec_prefix = '${prefix}'
Packit 0af36a
        else:
Packit 0af36a
            if args.exec_prefix.startswith('${prefix}'):
Packit 0af36a
                exec_prefix_use_shorthand = True
Packit 0af36a
                input_exec_prefix = args.prefix + args.exec_prefix[len('${prefix}'):]
Packit 0af36a
            else:
Packit 0af36a
                exec_prefix_use_shorthand = False
Packit 0af36a
                input_exec_prefix = args.exec_prefix
Packit 0af36a
            if not os.path.exists(input_exec_prefix):
Packit 0af36a
                raise SystemExit('Specified exec_prefix \'%s\' is invalid' %
Packit 0af36a
                                  args.exec_prefix)
Packit 0af36a
            if exec_prefix_use_shorthand is True:
Packit 0af36a
                self.exec_prefix = args.exec_prefix.replace('\\','/')
Packit 0af36a
            else:
Packit 0af36a
                self.exec_prefix = os.path.abspath(input_exec_prefix).replace('\\','/')
Packit 0af36a
Packit 0af36a
        # check and setup the includedir
Packit 0af36a
        if getattr(args, 'includedir', None) is None:
Packit 0af36a
            self.includedir = '${prefix}/include'
Packit 0af36a
        else:
Packit 0af36a
            if args.includedir.startswith('${prefix}'):
Packit 0af36a
                includedir_use_shorthand = True
Packit 0af36a
                input_includedir = args.prefix + args.includedir[len('${prefix}'):]
Packit 0af36a
            else:
Packit 0af36a
                if args.includedir.startswith('${exec_prefix}'):
Packit 0af36a
                    includedir_use_shorthand = True
Packit 0af36a
                    input_includedir = input_exec_prefix + args.includedir[len('${exec_prefix}'):]
Packit 0af36a
                else:
Packit 0af36a
                    includedir_use_shorthand = False
Packit 0af36a
                    input_includedir = args.includedir
Packit 0af36a
            if not os.path.exists(input_includedir):
Packit 0af36a
                raise SystemExit('Specified includedir \'%s\' is invalid' %
Packit 0af36a
                                  args.includedir)
Packit 0af36a
            if includedir_use_shorthand is True:
Packit 0af36a
                self.includedir = args.includedir.replace('\\','/')
Packit 0af36a
            else:
Packit 0af36a
                self.includedir = os.path.abspath(input_includedir).replace('\\','/')
Packit 0af36a
Packit 0af36a
        # check and setup the libdir
Packit 0af36a
        if getattr(args, 'libdir', None) is None:
Packit 0af36a
            self.libdir = '${prefix}/lib'
Packit 0af36a
        else:
Packit 0af36a
            if args.libdir.startswith('${prefix}'):
Packit 0af36a
                libdir_use_shorthand = True
Packit 0af36a
                input_libdir = args.prefix + args.libdir[len('${prefix}'):]
Packit 0af36a
            else:
Packit 0af36a
                if args.libdir.startswith('${exec_prefix}'):
Packit 0af36a
                    libdir_use_shorthand = True
Packit 0af36a
                    input_libdir = input_exec_prefix + args.libdir[len('${exec_prefix}'):]
Packit 0af36a
                else:
Packit 0af36a
                    libdir_use_shorthand = False
Packit 0af36a
                    input_libdir = args.libdir
Packit 0af36a
            if not os.path.exists(input_libdir):
Packit 0af36a
                raise SystemExit('Specified libdir \'%s\' is invalid' %
Packit 0af36a
			                      args.libdir)
Packit 0af36a
            if libdir_use_shorthand is True:
Packit 0af36a
                self.libdir = args.libdir.replace('\\','/')
Packit 0af36a
            else:
Packit 0af36a
                self.libdir = os.path.abspath(input_libdir).replace('\\','/')
Packit 0af36a
Packit 0af36a
        # setup dictionary for replacing items in *.pc.in
Packit 0af36a
        self.base_replace_items.update({'@VERSION@': self.version})
Packit 0af36a
        self.base_replace_items.update({'@prefix@': self.prefix})
Packit 0af36a
        self.base_replace_items.update({'@exec_prefix@': self.exec_prefix})
Packit 0af36a
        self.base_replace_items.update({'@libdir@': self.libdir})
Packit 0af36a
        self.base_replace_items.update({'@includedir@': self.includedir})