1#!/usr/bin/python
2#
3# Simple utility script to generate the basic info
4# needed in a .pc (pkg-config) file, used especially
5# for introspection purposes
6
7# This can be used in various projects where
8# there is the need to generate .pc files,
9# and is copied from GLib's $(srcroot)/win32
10
11# Author: Fan, Chun-wei
12# Date: March 10, 2016
13
14import os
15import sys
16import argparse
17
18class BasePCItems:
19    def __init__(self):
20        self.base_replace_items = {}
21        self.exec_prefix = ''
22        self.includedir = ''
23        self.libdir = ''
24        self.prefix = ''
25        self.srcdir = os.path.dirname(__file__)
26        self.top_srcdir = self.srcdir + '\\..'
27        self.version = ''
28
29    def setup(self, argv, parser=None):
30        if parser is None:
31            parser = argparse.ArgumentParser(description='Setup basic .pc file info')
32        parser.add_argument('--prefix', help='prefix of the installed library',
33                            required=True)
34        parser.add_argument('--exec-prefix',
35                            help='prefix of the installed programs, \
36                                  if different from the prefix')
37        parser.add_argument('--includedir',
38                            help='includedir of the installed library, \
39                                  if different from ${prefix}/include')
40        parser.add_argument('--libdir',
41                            help='libdir of the installed library, \
42                                  if different from ${prefix}/lib')
43        parser.add_argument('--version', help='Version of the package',
44                            required=True)
45        args = parser.parse_args()
46
47        self.version = args.version
48
49        # check whether the prefix and exec_prefix are valid
50        if not os.path.exists(args.prefix):
51            raise SystemExit('Specified prefix \'%s\' is invalid' % args.prefix)
52
53        # use absolute paths for prefix
54        self.prefix = os.path.abspath(args.prefix).replace('\\','/')
55
56        # check and setup the exec_prefix
57        if getattr(args, 'exec_prefix', None) is None:
58            exec_prefix_use_shorthand = True
59            self.exec_prefix = '${prefix}'
60        else:
61            if args.exec_prefix.startswith('${prefix}'):
62                exec_prefix_use_shorthand = True
63                input_exec_prefix = args.prefix + args.exec_prefix[len('${prefix}'):]
64            else:
65                exec_prefix_use_shorthand = False
66                input_exec_prefix = args.exec_prefix
67            if not os.path.exists(input_exec_prefix):
68                raise SystemExit('Specified exec_prefix \'%s\' is invalid' %
69                                  args.exec_prefix)
70            if exec_prefix_use_shorthand is True:
71                self.exec_prefix = args.exec_prefix.replace('\\','/')
72            else:
73                self.exec_prefix = os.path.abspath(input_exec_prefix).replace('\\','/')
74
75        # check and setup the includedir
76        if getattr(args, 'includedir', None) is None:
77            self.includedir = '${prefix}/include'
78        else:
79            if args.includedir.startswith('${prefix}'):
80                includedir_use_shorthand = True
81                input_includedir = args.prefix + args.includedir[len('${prefix}'):]
82            else:
83                if args.includedir.startswith('${exec_prefix}'):
84                    includedir_use_shorthand = True
85                    input_includedir = input_exec_prefix + args.includedir[len('${exec_prefix}'):]
86                else:
87                    includedir_use_shorthand = False
88                    input_includedir = args.includedir
89            if not os.path.exists(input_includedir):
90                raise SystemExit('Specified includedir \'%s\' is invalid' %
91                                  args.includedir)
92            if includedir_use_shorthand is True:
93                self.includedir = args.includedir.replace('\\','/')
94            else:
95                self.includedir = os.path.abspath(input_includedir).replace('\\','/')
96
97        # check and setup the libdir
98        if getattr(args, 'libdir', None) is None:
99            self.libdir = '${prefix}/lib'
100        else:
101            if args.libdir.startswith('${prefix}'):
102                libdir_use_shorthand = True
103                input_libdir = args.prefix + args.libdir[len('${prefix}'):]
104            else:
105                if args.libdir.startswith('${exec_prefix}'):
106                    libdir_use_shorthand = True
107                    input_libdir = input_exec_prefix + args.libdir[len('${exec_prefix}'):]
108                else:
109                    libdir_use_shorthand = False
110                    input_libdir = args.libdir
111            if not os.path.exists(input_libdir):
112                raise SystemExit('Specified libdir \'%s\' is invalid' %
113			                      args.libdir)
114            if libdir_use_shorthand is True:
115                self.libdir = args.libdir.replace('\\','/')
116            else:
117                self.libdir = os.path.abspath(input_libdir).replace('\\','/')
118
119        # setup dictionary for replacing items in *.pc.in
120        self.base_replace_items.update({'@VERSION@': self.version})
121        self.base_replace_items.update({'@prefix@': self.prefix})
122        self.base_replace_items.update({'@exec_prefix@': self.exec_prefix})
123        self.base_replace_items.update({'@libdir@': self.libdir})
124        self.base_replace_items.update({'@includedir@': self.includedir})
125