Skip to content

Building PYCC for MacOS issues

dimxy edited this page Sep 11, 2020 · 25 revisions

Several issues when building PYCC komodod and pycctx.so lib for MacOS

Used MacOS: Catalyna

Building komodod with Python Engine on MacOS

Note: To build komodod with pycc support you also need to pass --enable-pycc flag to configure.

Where the reference to the python lib is:
the ref to Python lib is defined in src/Makefile.am. Be careful it should be defined both major and minor version numbers (3.n) and you should install the exact version as default in the OS. In Makefile.am the version was initially set as -lpython3.6m, for mac I changed it to 3.7 as 3.6 installation is not directly supported on my MacOS.

To make headers like "Python.h" and lib libpython3.7m.dylib available for the compiler I added
-I /usr/local/opt/python@3.7/Frameworks/Python.framework/Headers to CPPFLAGS var and
-L /usr/local/opt/python@3.7/3.7.8_1/Frameworks/Python.framework/Versions/3.7/lib/ to LDFLAGS var in build-mac.sh

This is how the customized env vars in build-mac.sh look like:

CPPFLAGS="-I$PREFIX/include -I/usr/local/opt/python@3.7/Frameworks/Python.framework/Headers -arch x86_64" \
LDFLAGS="-L$PREFIX/lib -L/usr/local/opt/python@3.7/Frameworks/Python.framework/Versions/Current/lib -arch x86_64 -Wl,-no_pie" \
CXXFLAGS='-arch x86_64 -I/usr/local/Cellar/gcc\@8/8.3.0/include/c++/8.3.0/ -I$PREFIX/include -fwrapv -fno-strict-aliasing -Wno-builtin-declaration-mismatch -Werror -g -Wl,-undefined -Wl,dynamic_lookup -Wno-attributes' 
./configure --prefix="${PREFIX}" --with-gui=no "$HARDENING_ARG" "$LCOV_ARG" --enable-pycc

(Note: first I tried these params:
I added -L/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.7/headers to CPPFLAGS and yet I added -Wl,-rpath,/Library/Developer/CommandLineTools/Library/Frameworks to CXXFLAGS to resolve relative dependency to the python3 that was added: @rpath/Python3.framework/Versions/3.7/Python3. Then it was built okay but komodod began to print errors like base58.py is not installed. Installing base58 with pip3 did not resolve the issue so it looks like this python in /Library... folder is a different installation, and it did not have pip3 in itself)

Yet another issue with the build komodod with py support: when I tried to run the komodod build within the python virtual env, the build could not find python-config module (and this continued after pip install python-config cmd). It was resolved with setting PYTHON_CONFIG env var to the dir with python-config.py file: /Users/dimxy/repo/pycc/.env/lib/python3.7/site-packages

Building libpycctx.dylib on MacOS

When I built libpycctx.dylib (running make) make ended with a lot of errors like:

Undefined symbols for architecture x86_64:
            "_PyImport_ExecCodeModuleEx", referenced from:
                pyo3::types::module::PyModule::from_code::h5221d5256e3ab9a9 in libpyo3-a79a90d0681fac3f.rlib(pyo3-a79a90d0681fac3f.pyo3.dl3qelwn-cgu.9.rcgu.o)

The solution to fix those build errors for Mac was found here: https://github.com/PyO3/pyo3, it is to create a ~/.cargo/config with params (or add to an existing config):

[target.x86_64-apple-darwin]
rustflags = [
  "-C", "link-arg=-undefined",
  "-C", "link-arg=dynamic_lookup",
]

When libpycc.dylib is built successfully it is needed to make a link to it from the ./pycc directory

pycc % ln -s ./target/release/libpycctx.dylib pycctx.so

(note .so extension)

Clone this wiki locally