I'm trying to figure out how to build a python extension module using waf but I've got stuck. Consider a simple tree such as:
foo.h
#pragma once
class Foo {
public:
Foo();
const int &a() const;
int &a();
private:
int m_a;
};
inline const int &Foo::a() const { return m_a; }
inline int &Foo::a() { return m_a; }
foo.cpp
#include "foo.h"
Foo::Foo() : m_a(0) {}
foo.i
%module foo
%{
#include "foo.h"
%}
%include "foo.h"
main.cpp
#include <iostream>
#include "foo.h"
using namespace std;
int main() {
Foo foo;
cout << foo.a() << endl;
foo.a() = 10;
cout << foo.a() << endl;
}
main.py
import sys
sys.path.append('build')
from foo import Foo
foo = Foo()
print foo.a
foo.a = 1
print foo.a
wscript
import subprocess
from pathlib import Path
def configure(conf):
conf.env.MSVC_VERSIONS = ["msvc 15.0"]
conf.env.MSVC_TARGETS = ["x86"]
conf.load("msvc python swig")
conf.check_python_version((3, 6, 8))
# This method is not working properly on windows/virtualenv
# conf.check_python_headers()
# Maybe something like this could help?
# from distutils import sysconfig
# print(sysconfig.get_python_inc())
conf.check_swig_version()
def build(bld):
bld.program(source=["main.cpp", "foo.cpp"], cxxflags=["/EHsc"], target="test")
bld.shlib(
features="cxx",
source="foo.cpp",
target="foo",
includes=".",
export_includes=".",
name="FOO",
)
# bld.program(
# source = 'main.cpp',
# target = 'main',
# use = ['FOO'],
# rpath = ['$ORIGIN'],
# )
# bld(
# features = 'cxx cxxshlib pyext',
# source = 'foo.i',
# target = '_foo',
# swig_flags = '-c++ -python -Wall',
# includes = '.',
# use = 'FOO',
# rpath = ['$ORIGIN', ],
# )
def run(bld):
root_path = Path(bld.path.abspath())
subprocess.run(str(root_path / "build/test.exe"))
The first issue I'm already facing here is when I do waf distclean configure build
, I'll get this error:
(py382_64) D:\swigtest>waf distclean configure build
'distclean' finished successfully (0.006s)
Setting top to : D:\swigtest
Setting out to : D:\swigtest\build
Checking for program 'CL' : C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.26.28801\bin\HostX86\x86\CL.exe
Checking for program 'CL' : C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.26.28801\bin\HostX86\x86\CL.exe
Checking for program 'LINK' : C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.26.28801\bin\HostX86\x86\LINK.exe
Checking for program 'LIB' : C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.26.28801\bin\HostX86\x86\LIB.exe
Checking for program 'MT' : C:\Program Files (x86)\Windows Kits\10\bin\10.0.18362.0\x86\MT.exe
Checking for program 'RC' : C:\Program Files (x86)\Windows Kits\10\bin\10.0.18362.0\x86\RC.exe
Checking for program 'python' : d:\virtual_envs\py368_32\scripts\python.exe
Checking for program 'swig' : D:\software\swig\swig.exe
Checking for python version >= 3.6.8 : 3.6.8
Checking for swig version : 4.0.0
'configure' finished successfully (1.538s)
Waf: Entering directory `D:\swigtest\build'
[1/5] Compiling foo.cpp
[2/5] Compiling main.cpp
[3/5] Compiling foo.cpp
foo.cpp
foo.cpp
[4/5] Linking build\foo.dll
main.cpp
[5/5] Linking build\test.exe
Waf: Leaving directory `D:\swigtest\build'
Build failed
-> missing file: 'D:\\swigtest\\build\\foo.lib'
QUESTION: How can I fix that little issue? In any case, my main question here would be what's the proper way to generate a python extension module with swig and waf on windows?