Skip to content

Commit

Permalink
Leverage pkg-config in setup.py
Browse files Browse the repository at this point in the history
Using pkg-config we can get automatically the build flags in MacOS
and Linux and this doesn't require the user to manually modify CFLAGS
just to build the extension.

Signed-off-by: Pablo Galindo <[email protected]>
  • Loading branch information
pablogsal committed Nov 17, 2023
1 parent 986a17e commit c4b35f3
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 8 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/coverage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ jobs:
run: |
sudo apt-get update
sudo apt-get install -qy \
pkg-config \
libunwind-dev \
liblz4-dev \
gdb \
Expand All @@ -46,7 +47,7 @@ jobs:
python3.10-dbg
- name: Install Python dependencies
run: |
python3 -m pip install --upgrade pip cython
python3 -m pip install --upgrade pip cython pkgconfig
make test-install
- name: Disable ptrace security restrictions
run: |
Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
requires = [
"setuptools",
"wheel",
"pkgconfig",
"Cython>=0.29.31"
]

Expand Down
1 change: 1 addition & 0 deletions requirements-test.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ pytest
pytest-cov
ipython
setuptools; python_version >= '3.12'
pkgconfig
pytest-textual-snapshot
27 changes: 20 additions & 7 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from sys import platform
from sys import version_info

import pkgconfig
from Cython.Build import cythonize
from setuptools import Extension
from setuptools import find_packages
Expand Down Expand Up @@ -212,6 +213,22 @@ def build_js_files(self):
BINARY_FORMATS = {"darwin": "macho", "linux": "elf"}
BINARY_FORMAT = BINARY_FORMATS.get(sys.platform, "elf")

library_flags = {"libraries": ["lz4"]}
if IS_LINUX:
library_flags["libraries"].append("unwind")

try:
if IS_LINUX:
library_flags = pkgconfig.parse("liblz4 libunwind")
else:
library_flags = pkgconfig.parse("liblz4")
except EnvironmentError as e:
print("pkg-config not found.", e)
print("Falling back to static flags.")
except pkgconfig.PackageNotFoundError as e:
print("Package Not Found", e)
print("Falling back to static flags.")

MEMRAY_EXTENSION = Extension(
name="memray._memray",
sources=[
Expand All @@ -231,20 +248,16 @@ def build_js_files(self):
"src/memray/_memray/socket_reader_thread.cpp",
"src/memray/_memray/native_resolver.cpp",
],
libraries=[
"lz4",
],
library_dirs=[str(LIBBACKTRACE_LIBDIR)],
include_dirs=["src", str(LIBBACKTRACE_INCLUDEDIRS)],
language="c++",
extra_compile_args=["-std=c++17", "-Wall", *EXTRA_COMPILE_ARGS],
extra_link_args=["-std=c++17", "-lbacktrace", *EXTRA_LINK_ARGS],
define_macros=DEFINE_MACROS,
undef_macros=UNDEF_MACROS,
**library_flags,
)

if IS_LINUX:
MEMRAY_EXTENSION.libraries.append("unwind")
MEMRAY_EXTENSION.library_dirs.extend([str(LIBBACKTRACE_LIBDIR)])
MEMRAY_EXTENSION.include_dirs.extend(["src", str(LIBBACKTRACE_INCLUDEDIRS)])
MEMRAY_EXTENSION.libraries.append("dl")


Expand Down

0 comments on commit c4b35f3

Please sign in to comment.