| 1 | #!/usr/bin/env bash
 | 
| 2 | #
 | 
| 3 | # Can we use SWIG to wrap pure C++ in Python extension modules?
 | 
| 4 | #
 | 
| 5 | # This might be hard for our garbage collected types.
 | 
| 6 | #
 | 
| 7 | # Usage:
 | 
| 8 | #   demo/swig.sh <function name>
 | 
| 9 | 
 | 
| 10 | set -o nounset
 | 
| 11 | set -o pipefail
 | 
| 12 | set -o errexit
 | 
| 13 | 
 | 
| 14 | install() {
 | 
| 15 |   sudo apt-get install swig
 | 
| 16 | }
 | 
| 17 | 
 | 
| 18 | # https://www.swig.org/Doc3.0/Python.html#Python
 | 
| 19 | 
 | 
| 20 | example() {
 | 
| 21 |   ### Translate, compile, and run
 | 
| 22 | 
 | 
| 23 |   swig -python -c++ demo/swig/example.i
 | 
| 24 | 
 | 
| 25 |   # Two Output Files
 | 
| 26 |   wc -l demo/swig/{example.py,example_wrap.*}
 | 
| 27 |   ls -l demo/swig/
 | 
| 28 | 
 | 
| 29 |   set -x
 | 
| 30 |   python2 demo/swig/setup.py build_ext --inplace
 | 
| 31 | 
 | 
| 32 |   python2 -c '
 | 
| 33 | import _example
 | 
| 34 | print(dir(_example))
 | 
| 35 | print("fact(5) = %d" % _example.fact(5))
 | 
| 36 | print("add(3, 4) = %d" % _example.add(3, 4))
 | 
| 37 | print(_example.send)
 | 
| 38 | 
 | 
| 39 | # TODO: convert Str* to Python
 | 
| 40 | # print(_example.send(42, "my payload"))
 | 
| 41 | 
 | 
| 42 | # Generates TypeError for you
 | 
| 43 | # print("add(x, 4) = %d" % _example.add("3", 4))
 | 
| 44 | '
 | 
| 45 | 
 | 
| 46 | }
 | 
| 47 | 
 | 
| 48 | clean-temp() {
 | 
| 49 |   # Don't check in generated files
 | 
| 50 |   rm -v demo/swig/{example.py,example_wrap.cxx}
 | 
| 51 | }
 | 
| 52 | 
 | 
| 53 | # Hm doesn't work?
 | 
| 54 | qsn() {
 | 
| 55 |   #swig -o _tmp/qsn -python cpp/qsn.i
 | 
| 56 |   swig -python -c++ cpp/qsn.i
 | 
| 57 |   ls -l cpp/qsn*
 | 
| 58 |   #ls -l _tmp/qsn
 | 
| 59 | }
 | 
| 60 | 
 | 
| 61 | "$@"
 |