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