33import cffi
44import invoke
55import pathlib
6+ import platform
7+
8+ # Flag if we are on windows as that will require a different command line
9+ # for the compilers
10+ WINDOWS = platform .system ().startswith ("Windows" )
611
712
813@invoke .task
@@ -21,8 +26,15 @@ def print_banner(msg):
2126def build_cmult (c ):
2227 """ Build the shared library for the sample C code """
2328 print_banner ("Building C Library" )
24- invoke .run ("gcc -c -Wall -Werror -fpic cmult.c -I /usr/include/python3.7" )
25- invoke .run ("gcc -shared -o libcmult.so cmult.o" )
29+ if WINDOWS :
30+ invoke .run (
31+ "cl.exe /LD cmult.c /OUT:libcmult.dll"
32+ )
33+ else :
34+ invoke .run (
35+ "gcc -Wall -Werror -fpic cmult.c -I /usr/include/python3.7 "
36+ "-shared -o libcmult.so"
37+ )
2638 print ("* Complete" )
2739
2840
@@ -44,6 +56,14 @@ def build_cffi(c):
4456 with open (h_file_name ) as h_file :
4557 ffi .cdef (h_file .read ())
4658
59+ # need to set the rpath so the new Python module lib can find the .so file
60+ # in linux
61+ if WINDOWS :
62+ extra_links = []
63+ else :
64+ extra_links = ["-Wl,-rpath,." ]
65+
66+
4767 ffi .set_source (
4868 "cffi_example" ,
4969 # Since we are calling a fully built library directly no custom source
@@ -55,7 +75,7 @@ def build_cffi(c):
5575 # libraries we are linking against:
5676 libraries = ["cmult" ],
5777 library_dirs = [this_dir .as_posix ()],
58- extra_link_args = [ "-Wl,-rpath,." ] ,
78+ extra_link_args = extra_links ,
5979 )
6080
6181 ffi .compile ()
@@ -73,22 +93,37 @@ def test_cffi(c):
7393def build_cppmult (c ):
7494 """ Build the shared library for the sample C++ code """
7595 print_banner ("Building C++ Library" )
76- invoke .run (
77- "g++ -O3 -Wall -Werror -shared -std=c++11 -fPIC cppmult.cpp "
78- "-o libcppmult.so "
79- )
96+ if WINDOWS :
97+ invoke .run (
98+ "cl.exe /LD cmult.c /OUT:libcmult.dll"
99+ )
100+ else :
101+ invoke .run (
102+ "g++ -O3 -Wall -Werror -shared -std=c++11 -fPIC cppmult.cpp "
103+ "-o libcppmult.so "
104+ )
80105 print ("* Complete" )
81106
82107
83108def compile_python_module (cpp_name , extension_name ):
84- invoke .run (
85- "g++ -O3 -Wall -Werror -shared -std=c++11 -fPIC "
86- "`python3 -m pybind11 --includes` "
87- "-I /usr/include/python3.7 -I . "
88- "{0} "
89- "-o {1}`python3.7-config --extension-suffix` "
90- "-L. -lcppmult -Wl,-rpath,." .format (cpp_name , extension_name )
91- )
109+ if WINDOWS :
110+ # This needs to get updated with a way to call the python3.7-config
111+ # tool
112+ # This is the command line - possbily we jsut want a different function
113+ # for windows. The 'spam.lib' portion is going to be the cppmult lib.
114+ # cl /LD /I/python/include ni.c spam.lib ../libs/pythonXY.lib
115+ invoke .run (
116+ "cl.exe /LD {0} /OUT:{1}" .format (cpp_name , extension_name )
117+ )
118+ else :
119+ invoke .run (
120+ "g++ -O3 -Wall -Werror -shared -std=c++11 -fPIC "
121+ "`python3 -m pybind11 --includes` "
122+ "-I /usr/include/python3.7 -I . "
123+ "{0} "
124+ "-o {1}`python3.7-config --extension-suffix` "
125+ "-L. -lcppmult -Wl,-rpath,." .format (cpp_name , extension_name )
126+ )
92127
93128
94129@invoke .task (build_cppmult )
0 commit comments