from __future__ import division, print_function, unicode_literals

import os

Import ('env')

# compose paths
def radbin(name): return os.path.join(env['RAD_BUILDBIN'], name)
def radlib(name): return os.path.join(env['RAD_BUILDLIB'], name)

# extra flag
MDIRFLAGS = env.get('CPPFLAGS', []) + [
	'-DMDIR=\\"%s\\"' % os.path.join(env['RAD_RLIBDIR'], 'meta')]
bgraph = env.Object(source='bgraph.c', CPPFLAGS=MDIRFLAGS)
igraph = env.Object(source='igraph.c', CPPFLAGS=MDIRFLAGS)
dgraph = env.Object(source='dgraph.c', CPPFLAGS=MDIRFLAGS)
gcomp = env.Object(source='gcomp.c', CPPFLAGS=MDIRFLAGS)
syscalls = env.Object(source='syscalls.c', CPPFLAGS=MDIRFLAGS)
# used by several targets
mfio = env.Object(source='mfio.c')
misc = env.Object(source='misc.c')
primout = env.Object(source='primout.c')
plot = env.Object(source='plot.c')
rplot = env.Object(source='rplot.c')
palloc = env.Object(source='palloc.c')
plotin = env.Object(source='plotin.c')
mgvars = env.Object(source='mgvars.c')
mgraph = env.Object(source='mgraph.c')
cgraph = env.Object(source='cgraph.c')
gcalc = env.Object(source='gcalc.c')
# multiple output from same source

common = [mfio, misc, syscalls]

libmeta = env.StaticLibrary(radlib('meta'),
	Split ('metacalls.c progname.c') +[primout] + common)

# standard targets
PROGS = (
('meta2bmp', Split('meta2bmp.c') +[rplot, palloc, plot] + common, ['rtrad']),
('meta2tga', Split('meta2tga.c') +[rplot, palloc, plot] + common, ['rtrad']),
('pexpand',  Split('pexpand.c expand.c segment.c') +[palloc]+ common,['rtrad']),
('psort',    Split('psort.c sort.c') +[palloc]+ common, ['rtrad']),
('cv',       Split('cv.c cvhfio.c') + common, ['rtrad']),
('psmeta',   Split('psmeta.c psplot.c') + common, ['rtrad']),
('plotin',   [plotin, primout] + common, ['rtrad']),
('bgraph',   [bgraph, mgraph, mgvars], ['rtrad', 'meta']),
('igraph',   [igraph, gcalc, cgraph, mgraph, mgvars], ['rtrad', 'meta']),
('dgraph',   [dgraph, cgraph, mgvars], ['rtrad']),
('gcomp',    [gcomp, gcalc, mgvars], ['rtrad']),
('plot4',    Split('plot4.c') +[primout]+ common, ['rtrad']),
)
for p in PROGS:
	prog = env.Program(target=radbin(p[0]), source=p[1],
			LIBS=p[2]+env['RAD_MLIB'], )
	Default(prog)
	env.Append(RAD_BININSTALL=[env.Install(env['RAD_BINDIR'], prog)])

# X11 targets
if env.has_key('X11LIB'):
	x11meta = env.Program(radbin('x11meta'), [plotin, primout]+ common,
		CPPPATH=env.get('CPPPATH',[])+[env['X11INCLUDE']],
		LIBPATH=env.get('LIBPATH',[])+[env['X11LIB']],
		LIBS=['rtrad', 'X11'] + env['RAD_MLIB'])
	Default(x11meta)
	env.Append(RAD_BININSTALL=[env.Install(env['RAD_BINDIR'], x11meta)])


# vim: set syntax=python:
# vi: set ts=4 sw=4 :
