1#!/usr/local/bin/python3.8
2import subprocess
3import os
4import sys
5import time;
6from optparse import OptionParser
7from scripts import version
8from scripts import configfile
9import re
10
11from scripts.run import run
12
13TARGETS = ['libbirdfont',
14           'libbirdgems',
15           'birdfont',
16           'birdfont-autotrace',
17           'birdfont-export',
18           'birdfont-import',
19           'birdfont-test']
20
21VERSION = version.VERSION
22
23HEADER = '\033[95m'
24OKBLUE = '\033[94m'
25OKGREEN = '\033[92m'
26WARNING = '\033[93m'
27FAIL = '\033[91m'
28ENDC = '\033[0m'
29
30gee = '';
31
32def test_program_version (program, a, b, c):
33	print ('Checking for %s version >= %s.%s.%s' % (program, a, b, c))
34	process = subprocess.Popen (program + ' --version', shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
35	v = process.stdout.readline().decode('utf-8')
36	process.communicate()[0]
37	if not process.returncode == 0:
38		print (FAIL + 'Not found' + ENDC)
39		exit (1)
40	print ('Found ' + v)
41
42	o = v.split (' ');
43	for s in o:
44		if re.search( r'[0-9]*\.', s):
45			v = s
46			break
47
48	v = re.sub(r'[a-zA-Z\-].*', '0', v)
49	version = [int(n) for n in v.split ('.')]
50	return [a,b,c] <= version
51
52def test_library_version(pkg_config, lib, required=True, version=None):
53	print ('Looking for library: ' + lib + '\t\t')
54	process = subprocess.Popen(pkg_config + ' --modversion ' + lib, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
55	v = process.stdout.readline().decode('utf-8')
56	process.communicate()[0]
57	return process.returncode == 0
58
59def has_posixvala ():
60	posixvala = test_library_version ('posixvala')
61	if not posixvala:
62		print (OKGREEN + 'Glib will be used instead of Posix (libc).' + ENDC)
63		return 'False'
64	else:
65		print (OKGREEN + 'Using posix profile.' + ENDC)
66	return 'True'
67
68def configure(libbgee, valac, pkg_config):
69	global gee
70
71	if not test_program_version(valac, 0, 16, 0):
72		print (FAIL + 'valac is too old.' + ENDC)
73		exit (1)
74
75	libs = [
76			'cairo',
77			'gdk-pixbuf-2.0',
78			'gio-2.0',
79			'glib-2.0',
80			'gtk+-3.0',
81			'webkit2gtk-4.0',
82			'libsoup-2.4',
83			'libnotify',
84			'sqlite3',
85			'xmlbird'
86			]
87
88	test_library_version(pkg_config, 'xmlbird', True, '1.2.0')
89
90	for lib in libs:
91		test_library_version(pkg_config, lib)
92
93	if libbgee == 'Any':
94		if test_library_version(pkg_config, 'gee-0.8', False):
95			gee = 'gee-0.8'
96		elif test_library_version(pkg_config, 'gee-1.0', False):
97			gee = 'gee-1.0'
98		else:
99			print (FAIL + 'Can not find libgee (version 0.8 or version 1.0).' + ENDC)
100			exit (1)
101	else:
102		if not test_library_version(pkg_config, libbgee):
103			print (FAIL + 'Can not find lib gee.' + ENDC)
104			exit (1)
105		gee = libbgee;
106
107	run ('mkdir -p build')
108	run ('touch build/configured')
109
110	print ('');
111	print (OKGREEN + 'Done' + ENDC);
112
113
114parser = OptionParser()
115parser.add_option('-p', '--prefix', dest='prefix', help='Install prefix', metavar='PREFIX')
116parser.add_option('-d', '--dest', dest='dest', help='Install to this directory', metavar='DEST')
117parser.add_option('-c', '--cc', dest='cc', help='C compiler', metavar='CC')
118parser.add_option('-e', '--gee', dest='gee', help='Version of libgee', metavar='GEE')
119parser.add_option('-v', '--valac', dest='valac', help='Vala compiler', metavar='VALAC')
120parser.add_option('-P', '--pkg-config', dest='pkg_config', help='Package config tool', metavar='PKG_CONFIG')
121parser.add_option('-n', '--nonnull', dest='nonnull', action="store_true", help='Enable compiletime checks for null pointers', metavar='NONNULL')
122
123parser.add_option('', '--valac-flags', dest='valac_flags', help='Vala compiler flags for all targets', metavar='VALAC_FLAGS', default='')
124for target in TARGETS:
125	parser.add_option('', '--valac-flags-' + target, dest='valac_flags_' + target, help='Vala compiler flags for ' + target, metavar='VALAC_FLAGS', default='')
126
127parser.add_option('', '--cflags', dest='cflags', help='C compiler flags for all targets', metavar='CFLAGS', default='')
128for target in TARGETS:
129	parser.add_option('', '--cflags-' + target, dest='cflags_' + target, help='C compiler flags for ' + target, metavar='CFLAGS', default='')
130
131parser.add_option('', '--ldflags', dest='ldflags', help='Linker flags for all targets', metavar='LDFLAGS', default='')
132for target in TARGETS:
133	parser.add_option('', '--ldflags-' + target, dest='ldflags_' + target, help='Linker flags for ' + target, metavar='LDFLAGS', default='')
134
135(options, args) = parser.parse_args()
136option_dict = vars(options)
137
138valacflags = dict()
139cflags = dict()
140ldflags = dict()
141
142for target in TARGETS:
143	cflags[target] = options.cflags
144	cflags[target] = cflags[target] + ' ' + option_dict.get('cflags_' + target, "")
145	cflags[target] = cflags[target].strip()
146
147	ldflags[target] = options.ldflags
148	ldflags[target] = ldflags[target] + ' ' + option_dict.get('ldflags_' + target, "")
149	ldflags[target] = ldflags[target].strip()
150
151	valacflags[target] = options.valac_flags
152	valacflags[target] = valacflags[target] + ' ' + option_dict.get('valac_flags_' + target, "")
153	valacflags[target] = valacflags[target].strip()
154
155if not options.prefix:
156	if 'bsd' in sys.platform or 'dragonfly' in sys.platform:
157		options.prefix = '${DESTDIR}${PREFIX}'
158	else:
159		options.prefix = '/usr'
160
161if not options.dest:
162	options.dest = ''
163if not options.cc:
164	options.cc = 'gcc'
165if not options.gee:
166	options.gee = 'Any'
167if not options.valac:
168	options.valac = 'valac'
169if not options.pkg_config:
170	options.pkg_config = 'pkg-config'
171if not options.nonnull:
172	options.nonnull = False
173else:
174	options.nonnull = True
175
176configure(options.gee, options.valac, options.pkg_config)
177
178configfile.write_config(options.prefix)
179configfile.write_compile_parameters(options.prefix,
180									options.dest,
181									options.cc,
182									gee,
183									options.valac,
184									options.pkg_config,
185									options.nonnull,
186									valacflags,
187									cflags,
188									ldflags)
189