#!/usr/bin/env python
# encoding: utf-8
# Tom Wambold tom5760 gmail
# Thomas Nagy, 2010 (ita)

"""
if libgmp is present, try building with 'waf --exe'
"""

top = '.'
out = 'build'

def options(opt):
	opt.add_option('--exe', action='store_true', default=False, help='Execute the program after it is compiled')

def configure(ctx):
	ctx.load('go')

	try:
		ctx.load('gcc')
		ctx.check_cc(fragment='#include <gmp.h>\nint main() {return 0;}\n', uselib_store='GMP', lib='gmp')
	except ctx.errors.ConfigurationError:
		ctx.env.TRY_CGO = False
	else:
		ctx.env.TRY_CGO = True

def build(ctx):

	ctx(
		features = 'go gopackage',
		target   = 'other',
		source   = [
			'other/a.go',
			'other/b.go', # gopack sux
		],
	)

	ctx(
		features = 'go goprogram',
		target   = 'test',
		use      = 'other',
		source   = 'main.go',
		includes = '.',
	)

	# NOTE: if you use ant_glob, use it like this:     bld.path.ant_glob('*.go', excl='*_test.go')

	if ctx.env.TRY_CGO:
		# see http://code.google.com/p/go/issues/detail?id=1203
		# so we still have to move the files, grrrrr
		ctx(name='cgo', rule='${CGO} ${SRC} && mv .._${TGT[0].name} ${TGT[0].abspath()} && mv .._${TGT[1].name} ${TGT[1].abspath()}',
			target='gmp.cgo1.go gmp.cgo2.c   gmp.cgo2.c _cgo_gotypes.go _cgo_defun.c',
			source='gmp.go',
			shell=True)
		ctx(features='c cshlib', source='gmp.cgo2.c', target=ctx.path.find_or_declare('cgo_gmp.so'), use='GMP')

		# go files can only use go packages, so pi.go cannot compile
		#ctx.add_group()
		#ctx(features='go goprogram uselib', source='pi.go', target='pi', gocflags=['-I.', '-I..'])

	from waflib import Options, Utils
	if ctx.env.TRY_CGO and Options.options.exe:
		def exe(bld):
			p = Utils.subprocess.Popen('LD_LIBRARY_PATH=build ./build/pi', shell=True)
			p.wait()
		ctx.add_post_fun(exe)

