1#!/usr/bin/env python
2# encoding: utf-8
3# John O'Meara, 2006
4# Thomas Nagy, 2006-2018 (ita)
5
6"""
7The **flex** program is a code generator which creates C or C++ files.
8The generated files are compiled into object files.
9"""
10
11import os, re
12from waflib import Task, TaskGen
13from waflib.Tools import ccroot
14
15def decide_ext(self, node):
16	if 'cxx' in self.features:
17		return ['.lex.cc']
18	return ['.lex.c']
19
20def flexfun(tsk):
21	env = tsk.env
22	bld = tsk.generator.bld
23	wd = bld.variant_dir
24	def to_list(xx):
25		if isinstance(xx, str):
26			return [xx]
27		return xx
28	tsk.last_cmd = lst = []
29	lst.extend(to_list(env.FLEX))
30	lst.extend(to_list(env.FLEXFLAGS))
31	inputs = [a.path_from(tsk.get_cwd()) for a in tsk.inputs]
32	if env.FLEX_MSYS:
33		inputs = [x.replace(os.sep, '/') for x in inputs]
34	lst.extend(inputs)
35	lst = [x for x in lst if x]
36	txt = bld.cmd_and_log(lst, cwd=wd, env=env.env or None, quiet=0)
37	tsk.outputs[0].write(txt.replace('\r\n', '\n').replace('\r', '\n')) # issue #1207
38
39TaskGen.declare_chain(
40	name = 'flex',
41	rule = flexfun, # issue #854
42	ext_in = '.l',
43	decider = decide_ext,
44)
45
46# To support the following:
47# bld(features='c', flexflags='-P/foo')
48Task.classes['flex'].vars = ['FLEXFLAGS', 'FLEX']
49ccroot.USELIB_VARS['c'].add('FLEXFLAGS')
50ccroot.USELIB_VARS['cxx'].add('FLEXFLAGS')
51
52def configure(conf):
53	"""
54	Detect the *flex* program
55	"""
56	conf.find_program('flex', var='FLEX')
57	conf.env.FLEXFLAGS = ['-t']
58
59	if re.search (r"\\msys\\[0-9.]+\\bin\\flex.exe$", conf.env.FLEX[0]):
60		# this is the flex shipped with MSYS
61		conf.env.FLEX_MSYS = True
62
63