sconsでpythonモジュールをビルド→試行錯誤中

[id:shive:20090901:1251820396]
環境によってはsconsがvc9ビルドしてくれない件、詳しく調べてみたらeasy_install版とwindowsインストーラ版が両方入っていて、有効になってる方のバージョンだとvc8までしかサポートできていないことがわかった。しかもダメな子はeasy_installの方でwindowsインストーラの最新版(latest)をダウンロードしてインストールしたらちゃんとvc9ビルドされるようになりました。
…が、こんどはswigサポートが無くなってしまってる。なんでやねん…。

お?

TOOLSにswigがデフォルトでは入らなくなってる。これか?

出来た

// hoge.h
#include <stdio.h>
class Base {
protected:
    Base() {
        printf("Base::ctor\n");
    }
public:
    virtual ~Base() {}
    virtual const char* _what() = 0;
    void say() {
        printf("%s\n", this->_what());
    }
};
class Impl : public Base {
public:
    Impl() {
        printf("Impl::ctor\n");
    }
    virtual const char* _what() {
        return "I'm in the c++!!";
    }
};
// hoge.cpp
#include "hoge.h"
// hoge.i
%module hoge
%{
#include "hoge.h"
%}
%include "hoge.h"
## SConstruct
import distutils.sysconfig
env = Environment(
    TOOLS     = [ 'default',
                  'msvc',
                  'swig' ],
    SWIG      = r'c:\cygwin\bin\swig.exe',
    SWIGFLAGS = ['-c++', '-python'],
    CPPPATH   = [distutils.sysconfig.get_python_inc()],
    CPPFLAGS  = ['/O2', '/MD', '/EHsc'],
    LIBPATH   = [distutils.sysconfig.get_python_lib(standard_lib=True) + 's'],
    SHLIBSUFFIX = '.pyd'
    )
env.SharedLibrary(
    '_hoge',
    ['hoge.cpp', 'hoge.i'],
)

これを作った状態で、sconsとやると_hoge.pydとhoge.pyができる。

>>> from hoge import *
>>> x=Impl()
Base::ctor()
Impl::ctor()
>>> x.say()
I'm in the c++!!

こうなる。満足。
ちなみにこれでImplを派生して_whatをオーバーライドしてもvirtualは反映されず、sayの内容も変化しない。
そこでdirectorの登場です。そっちはまたそのうち。

環境memo

以下の環境で確認できました。

  • VisualStudio2008 Express(vc9)
  • python2.6.2
  • scons-1.2.0-d20090919
  • swig-1.3.36 (cygwin)