From 6c5f4f3c0b7678d0d808d6686f6e506b16c19ab5 Mon Sep 17 00:00:00 2001 From: "Gustavo J. A. M. Carneiro" Date: Mon, 28 Apr 2008 12:49:30 +0100 Subject: [PATCH] Add the 'a' example from Mathieu --- examples/a/a.cc | 18 ++++++++++++++++++ examples/a/a.h | 7 +++++++ examples/a/a_test.py | 9 +++++++++ examples/a/modulegen.py | 23 +++++++++++++++++++++++ examples/a/wscript | 30 ++++++++++++++++++++++++++++++ examples/wscript | 2 +- 6 files changed, 88 insertions(+), 1 deletion(-) create mode 100644 examples/a/a.cc create mode 100644 examples/a/a.h create mode 100644 examples/a/a_test.py create mode 100644 examples/a/modulegen.py create mode 100644 examples/a/wscript diff --git a/examples/a/a.cc b/examples/a/a.cc new file mode 100644 index 0000000..4a5ff18 --- /dev/null +++ b/examples/a/a.cc @@ -0,0 +1,18 @@ +#include "a.h" +#include + +void ADoA (void) +{ + std::cout << "ADoA" << std::endl; +} + +void ADoB (uint32_t b) +{ + std::cout << "ADoB=" << b << std::endl; +} + +uint32_t ADoC (void) +{ + std::cout << "ADoC" << std::endl; + return 1; +} diff --git a/examples/a/a.h b/examples/a/a.h new file mode 100644 index 0000000..1063df1 --- /dev/null +++ b/examples/a/a.h @@ -0,0 +1,7 @@ +#include + +void ADoA (void); + +void ADoB (uint32_t b); + +uint32_t ADoC (void); diff --git a/examples/a/a_test.py b/examples/a/a_test.py new file mode 100644 index 0000000..379ba7b --- /dev/null +++ b/examples/a/a_test.py @@ -0,0 +1,9 @@ +import sys +sys.path.insert(0, "../../build/default/examples/a") +from a import * + +ADoA () +ADoB (1) +a = ADoC () +print a + diff --git a/examples/a/modulegen.py b/examples/a/modulegen.py new file mode 100644 index 0000000..6e293e0 --- /dev/null +++ b/examples/a/modulegen.py @@ -0,0 +1,23 @@ +#! /usr/bin/env python + +import sys + +import pybindgen +from pybindgen import (ReturnValue, Parameter, Module, Function, FileCodeSink) +from pybindgen import (CppMethod, CppConstructor, CppClass, Enum) + + +def my_module_gen(out_file): + pybindgen.write_preamble(FileCodeSink(out_file)) + print >> out_file, "#include \"a.h\"" + + mod = Module('a') + + mod.add_function(Function(ReturnValue.new('void'), 'ADoA', [])) + mod.add_function(Function(ReturnValue.new('void'), 'ADoB', [Parameter.new('uint32_t', 'b')])) + mod.add_function(Function(ReturnValue.new('uint32_t'), 'ADoC', [])) + + mod.generate(FileCodeSink(out_file) ) + +if __name__ == '__main__': + my_module_gen(sys.stdout) diff --git a/examples/a/wscript b/examples/a/wscript new file mode 100644 index 0000000..1a09ded --- /dev/null +++ b/examples/a/wscript @@ -0,0 +1,30 @@ +## -*- python -*- +import Params + +def create_pyext_obj(bld, c_or_cpp_type): + waf_version = [int (s) for s in Params.g_version.split('.')] + if waf_version >= [1,4]: + return bld.create_obj(c_or_cpp_type, 'shlib', 'pyext') + else: + return bld.create_obj(c_or_cpp_type, 'plugin', 'pyext') + + +def build(bld): + ## manual code generation using simple pybindgen API calls + bindgen = bld.create_obj('command-output') + bindgen.stdout = 'amodule.cc' + bindgen.argv = [bindgen.input_file('modulegen.py')] + bindgen.command = bindgen.env['PYTHON'] + bindgen.command_is_external = True + bindgen.prio = 55 # must run after the 'py' objects in ../pybindgen + + obj = create_pyext_obj(bld, 'cpp') + obj.source = [ + 'a.cc', + 'amodule.cc' + ] + obj.target = 'a' + obj.inst_var = 0 # do not install + + ## Workaround a WAF bug (http://code.google.com/p/waf/issues/detail?id=37) + obj.env.append_value("CPPPATH", bld.m_curdirnode.srcpath(obj.env)) diff --git a/examples/wscript b/examples/wscript index ed70ce2..e2a1c7e 100644 --- a/examples/wscript +++ b/examples/wscript @@ -1,5 +1,5 @@ ## -*- python -*- def build(bld): - pass # TODO + bld.add_subdirs('a')