整理 Python-Solvespace 端口的編譯流程
Python 位置提供
利用 Python 的 sysconfig 模組,可以取得 Python 相關路徑的位置,進而回傳至 Makefile 以供編譯參數使用。
$python3 -m sysconfig >python -m sysconfig
以 Windows 平台的 Python 3.5 為例,得到數值如下:
Platform: "win-amd64"
Python version: "3.5"
Current installation scheme: "nt"
Paths:
data = "C:\Users\...\Python35"
include = "C:\Users\...\Python35\Include"
platinclude = "C:\Users\...\Python35\Include"
platlib = "C:\Users\...\Python35\Lib\site-packages"
platstdlib = "C:\Users\...\Python35\Lib"
purelib = "C:\Users\...\Python35\Lib\site-packages"
scripts = "C:\Users\...\Python35\Scripts"
stdlib = "C:\Users\...\Python35\Lib"
Variables:
BINDIR = "C:\Users\...\Python35"
BINLIBDEST = "C:\Users\...\Python35\Lib"
...
其中不同平台得到的路徑會不太一樣,如 Ubuntu 得到的變數種類更多。
這些變數可以透過下列 Python 程式得到。
from distutils import sysconfig
print(sysconfig.get_config_var('BINDIR'))
#C:\Users\...\Python35
因此,在 Makefile 的指令中,可以以一行式得到路徑參數:
g++ -shared -o _slvs.pyd $^ -L. -l:libslvs.so \
-L$(shell python -c "from distutils import sysconfig;print(sysconfig.get_config_var('BINDIR'))")\libs \
-lPython$(shell python -c "from distutils import sysconfig;print(sysconfig.get_config_var('VERSION'))") \
-Wl,--output-def,libslvs.def,--out-implib,libslvs.lib
#同於:
g++ -shared -o _slvs.pyd $^ -L. -l:libslvs.so \
-LC:\Users\...\Python35\libs \
-lPython35 \
-Wl,--output-def,libslvs.def,--out-implib,libslvs.lib
藉此,可免去使用 Python 腳本產生 Makefile 不便的過程。
Windows 平台的 Python 設定
由於之前換過 Python 版本,導致 Windows 平台的原始碼和編譯器設定必須一起更動。
-
libpython3x.a沒有提供。這個問題在最近的 Python 3.5、3.6 中都已提供,不須要手動製作。
-
原始碼與編譯器
必須修改
include\pyconfig.h和Lib\distutils\cygwinccompiler.py,否則會導致 SWIG 製作的端口會有錯誤。 -
math.h與pyconfig.h衝突修改
pyconfig.h的#define hypot _hypot,只要改 GCC Compiler 的部分即可。
上述問題的解決方案都寫在 exposed 資料夾的 Readme.md 中,若是使用 Windows 平台新安裝的 Python,就必須注意調整。
而 Ubuntu 平台則無需調整。
Comments
comments powered by Disqus