from __future__ import division, print_function, unicode_literals

import os
from build_utils import vstamp

Import('env') # inherit from parent

mlib = env['RAD_MLIB']

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

# custom compile flags
dielectric = env.Object(source='dielectric.c',
		CCFLAGS=env.get('CCFLAGS',[]) + ['-DDISPERSE'])

duphead = env.Object(source="duphead.c")
persist = env.Object(source="persist.c")
ambio = env.Object(source="ambio.c")
rayfifo = env.Object(source="rayfifo.c")
raypwin = env.Object(source="raypwin.c")
raycalls = env.Object(source="raycalls.c")

CCFLAGS = env.get('CCFLAGS',[])
if env.get('PMAP_OOC', 0) not in (0,'0','','n','no','false',False,None):
	PMD = Split('ooccache.c oocnn.c oocmorton.c oococt.c oocsort.c oocbuild.c')
	OOC_CCFLAGS = env.get('CCFLAGS',[]) + ['-DPMAP_OOC']
	for src in Split('''pmapdata.c pmapio.c pmapopt.c '''):
		obj = env.Object(source=src, CCFLAGS=OOC_CCFLAGS)
		PMD.append(obj)
	pmaptype = env.Object(source="pmaptype.c", CCFLAGS=OOC_CCFLAGS)
	pmapparm = env.Object(source="pmapparm.c", CCFLAGS=OOC_CCFLAGS)
else:
	PMD = Split('''pmapdata.c pmapio.c pmapopt.c ''')
	OOC_CCFLAGS = env.get('CCFLAGS',[])
	pmaptype = env.Object(source="pmaptype.c")
	pmapparm = env.Object(source="pmapparm.c")

# source and object dependencies
RAY = Split('''ambcomp.c ambient.c freeobjmem.c initotypes.c preload.c
		raytrace.c renderopts.c''') + [ambio]
PMAP = Split('''pmap.c pmapsrc.c pmapmat.c pmaprand.c
        pmapbias.c pmapcontrib.c pmapamb.c pmapray.c pmapdiag.c
		pmcontrib2.c pmutil.c
		''') + [pmaptype, pmapparm]
SURF = Split('''srcsamp.c srcsupp.c srcobstr.c source.c virtuals.c sphere.c
		o_face.c o_cone.c o_instance.c o_mesh.c''')
MAT = Split('''aniso.c normal.c m_clip.c glass.c m_brdf.c m_mirror.c
		m_direct.c m_mist.c fprism.c m_alias.c m_bsdf.c ashikhmin.c '''
		) + [dielectric]
MOD = Split('p_func.c t_func.c p_data.c t_data.c text.c mx_func.c mx_data.c')
SUP = Split('func.c noise3.c data.c')

# build libraries
rsrc = RAY + PMAP + PMD + SURF + MAT + MOD + SUP
env.Command('Version.c', 'VERSION', vstamp.build_version_c)
env.Depends('Version.c', rsrc)
Version = env.Object(source=['Version.c'])
env.version = Version # make it remotely accessible
tracelib = env.Library(target=radlib('rttrace'), source=rsrc + [Version])

rclib = env.Library(target=radlib('raycalls'),
		source=Split('raycalls.c $RAD_PCALLS'))
Default(rclib)

rtmain = env.Object(source=['rtmain.c'],
		CCFLAGS=env.get('CCFLAGS',[]) + ['-DNICE=4'])

# build executables
PROGS = [
('mkpmap',   ['mkpmap.c', Version], CCFLAGS,
	 ['rttrace', 'rtrad', '$RAD_SOCKETLIB', mlib]),
('pmapdump', ['pmapdump.c', pmaptype, pmapparm, Version], OOC_CCFLAGS,
	 ['rtrad', '$RAD_SOCKETLIB', mlib]),
('lookamb',  ['lookamb.c', ambio], CCFLAGS, ['rtrad', mlib]),
('rcontrib', Split('rcmain.c rcontrib.c rc2.c rc3.c') + [Version], CCFLAGS,
	 ['rttrace', 'rtrad', '$RAD_SOCKETLIB', mlib]),
('rtrace',   ['rtrace.c', duphead, persist, rtmain, rayfifo, raypwin, raycalls],
	 CCFLAGS, ['rttrace', 'rtrad'] +  mlib),
('rpict',    Split('rpmain.c rpict.c srcdraw.c') + [duphead, persist], CCFLAGS,
	 ['rttrace', 'rtrad', '$RAD_SOCKETLIB']+mlib),
]
for p in PROGS:
	prog = env.Program(target=radbin(p[0]), source = p[1],
			CCFLAGS = p[2], LIBS = p[3]),
	Default(prog)
	env.Append(RAD_BININSTALL=[env.Install('$RAD_BINDIR', prog)])

# we need RVOBJS for any derived viewers, such as winrview
if os.name == 'nt': RV_DEF = ['WIN_RVIEW']
else: RV_DEF = []
rvmain = env.Object(source='rvmain.c',
		CCFLAGS=env['CCFLAGS'] + ['-DNICE=6'])
rvobjs = [rvmain]
for src in  Split('''rview.c rv2.c rv3.c
	devtable.c devcomm.c colortab.c editline.c'''):
	rvobjs.append(env.Object(source=src,
			CPPDEFINES=env.get('CPPDEFINES', []) + RV_DEF))
env['RVOBJS'] = rvobjs

if env.has_key('X11LIB'): # lots of special requirements for rvu
	x11incls = [env['X11INCLUDE']] + env['CPPPATH']
	x11 = env.Object(source='x11.c', CPPPATH=x11incls)
	x11twind = env.Object(source='x11twind.c', CPPPATH=x11incls)
	rvxobjs = [x11, x11twind]
	rvsrc =  rvobjs + rvxobjs
	rvu = env.Program(target=radbin('rvu'), source=rvsrc,
		LIBPATH=env['LIBPATH'] + [env['X11LIB']],
		LIBS = ([rclib, 'rttrace', 'rtrad', 'X11'] + mlib)
		)
	Default(rvu)
	env.Append(RAD_BININSTALL=[env.Install('$RAD_BINDIR', rvu)])

LIBFILES = Split('rayinit.cal')
env.Append(RAD_RLIBINSTALL=[env.Install('$RAD_RLIBDIR', LIBFILES)])

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