Skip to content

Commit

Permalink
Use scikit-build-core
Browse files Browse the repository at this point in the history
Signed-off-by: Pablo Galindo <[email protected]>
  • Loading branch information
pablogsal committed Sep 3, 2024
1 parent 6d2e143 commit a73f4f0
Show file tree
Hide file tree
Showing 4 changed files with 252 additions and 41 deletions.
145 changes: 145 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
cmake_minimum_required(VERSION 3.7)
project(memray)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

# Find Python
find_package(Python COMPONENTS Interpreter Development.Module Development.SABIModule REQUIRED)

# Find Cython
find_program(CYTHON_EXECUTABLE cython)
if(NOT CYTHON_EXECUTABLE)
message(FATAL_ERROR "Cython not found. Please install Cython.")
endif()

# Find required packages
find_package(PkgConfig REQUIRED)
pkg_check_modules(LZ4 REQUIRED liblz4)
if(NOT LZ4_FOUND)
message(FATAL_ERROR "LZ4 library not found. Please install liblz4-dev or equivalent.")
endif()

if(${CMAKE_SYSTEM_NAME} MATCHES "Linux")
pkg_check_modules(LIBUNWIND REQUIRED libunwind)
endif()

# Set compiler flags
set(COMPILER_FLAGS "-Wall")
if(NOT DEFINED ENV{NO_MEMRAY_FAST_TLS})
add_definitions(-DUSE_MEMRAY_TLS_MODEL=1)
endif()

if(DEFINED ENV{MEMRAY_MINIMIZE_INLINING})
set(COMPILER_FLAGS ${COMPILER_FLAGS} -Og)
else()
set(COMPILER_FLAGS ${COMPILER_FLAGS} -flto)
set(LINKER_FLAGS ${LINKER_FLAGS} -flto)
endif()

if(${CMAKE_SYSTEM_NAME} MATCHES "Linux")
set(BINARY_FORMAT "elf")
elseif(${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
set(BINARY_FORMAT "macho")
else()
message(FATAL_ERROR "Unsupported platform: ${CMAKE_SYSTEM_NAME}")
endif()

# Set up libbacktrace
set(LIBBACKTRACE_DIR ${CMAKE_SOURCE_DIR}/src/vendor/libbacktrace)
set(LIBBACKTRACE_INCLUDE_DIR ${LIBBACKTRACE_DIR}/install/include)
set(LIBBACKTRACE_LIB_DIR ${LIBBACKTRACE_DIR}/install/lib)

# Add custom command to build libbacktrace
add_custom_command(
OUTPUT ${LIBBACKTRACE_LIB_DIR}/libbacktrace.a
COMMAND ${CMAKE_COMMAND} -E make_directory ${CMAKE_CURRENT_BINARY_DIR}/libbacktrace_build
COMMAND ${CMAKE_COMMAND} -E make_directory ${LIBBACKTRACE_INSTALL_DIR}
COMMAND ${CMAKE_COMMAND} -E chdir ${CMAKE_CURRENT_BINARY_DIR}/libbacktrace_build
${LIBBACKTRACE_DIR}/configure
--with-pic
--prefix=${LIBBACKTRACE_INSTALL_DIR}
--includedir=${LIBBACKTRACE_INSTALL_DIR}/include/libbacktrace
COMMAND ${CMAKE_COMMAND} -E chdir ${CMAKE_CURRENT_BINARY_DIR}/libbacktrace_build make -j
COMMAND ${CMAKE_COMMAND} -E chdir ${CMAKE_CURRENT_BINARY_DIR}/libbacktrace_build make install
DEPENDS ${LIBBACKTRACE_DIR}/configure
)
add_custom_target(libbacktrace DEPENDS ${LIBBACKTRACE_LIB_DIR}/libbacktrace.a)

# _memray extension

add_custom_command(
OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/_memray.cpp
COMMAND Python::Interpreter -m cython
--cplus
-3
-I ${CMAKE_SOURCE_DIR}/src/memray/
${CMAKE_SOURCE_DIR}/src/memray/_memray.pyx
-o ${CMAKE_CURRENT_BINARY_DIR}/_memray.cpp
DEPENDS ${CMAKE_SOURCE_DIR}/src/memray/_memray.pyx
VERBATIM
)
set(MEMRAY_SOURCES
src/memray/_memray/compat.cpp
src/memray/_memray/hooks.cpp
src/memray/_memray/tracking_api.cpp
src/memray/_memray/${BINARY_FORMAT}_shenanigans.cpp
src/memray/_memray/logging.cpp
src/memray/_memray/python_helpers.cpp
src/memray/_memray/source.cpp
src/memray/_memray/sink.cpp
src/memray/_memray/records.cpp
src/memray/_memray/record_reader.cpp
src/memray/_memray/record_writer.cpp
src/memray/_memray/snapshot.cpp
src/memray/_memray/socket_reader_thread.cpp
src/memray/_memray/native_resolver.cpp
)
python_add_library(_memray MODULE WITH_SOABI ${MEMRAY_SOURCES} ${CMAKE_CURRENT_BINARY_DIR}/_memray.cpp)
target_include_directories(_memray PRIVATE
${CMAKE_SOURCE_DIR}/src/memray/_memray
${LIBBACKTRACE_INCLUDE_DIR}
${LZ4_INCLUDE_DIRS}
)
target_link_libraries(_memray PRIVATE ${LZ4_LIBRARIES} dl ${LIBUNWIND_LIBRARIES} ${LIBBACKTRACE_LIB_DIR}/libbacktrace.a)
target_link_options(_memray PRIVATE ${LZ4_LDFLAGS})
target_compile_options(_memray PRIVATE ${COMPILER_FLAGS})
target_link_options(_memray PRIVATE ${LINKER_FLAGS})
add_dependencies(_memray libbacktrace)

# _test_utils extension

add_custom_command(
OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/_memray_test_utils.cpp
COMMAND Python::Interpreter -m cython
--cplus
-3
-I ${CMAKE_SOURCE_DIR}/src/memray/
${CMAKE_SOURCE_DIR}/src/memray/_memray_test_utils.pyx
-o ${CMAKE_CURRENT_BINARY_DIR}/_memray_test_utils.cpp
--module-name memray._test_utils
DEPENDS ${CMAKE_SOURCE_DIR}/src/memray/_memray_test_utils.pyx
VERBATIM
)
python_add_library(_test_utils MODULE WITH_SOABI ${CMAKE_CURRENT_BINARY_DIR}/_memray_test_utils.cpp)
target_include_directories(_test_utils PRIVATE
${CMAKE_SOURCE_DIR}/src/memray/_memray
)

# _inject extension

set(INJECT_SOURCES
src/memray/_memray/inject.cpp
)
python_add_library(_inject MODULE WITH_SOABI USE_SABI 3.7 ${INJECT_SOURCES})
target_include_directories(_inject PRIVATE
${CMAKE_SOURCE_DIR}/src/memray
)
target_compile_options(_test_utils PRIVATE ${COMPILER_FLAGS})
target_link_options(_test_utils PRIVATE ${LINKER_FLAGS})
target_compile_options(_inject PRIVATE ${COMPILER_FLAGS})
target_link_options(_inject PRIVATE ${LINKER_FLAGS})


# Install targets
install(TARGETS _memray _test_utils _inject LIBRARY DESTINATION memray)
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ build: build-js build-ext ## (default) Build package extensions and assets in-p

.PHONY: build-ext
build-ext: ## Build package extensions in-place
$(PYTHON) setup.py build_ext --inplace
$(PYTHON) -m pip install --no-build-isolation --config-settings=editable.rebuild=true -Cbuild-dir=build -ve.

$(reporters_path)/templates/assets/%.js: $(reporters_path)/assets/%.js
$(NPM) install
Expand Down
136 changes: 101 additions & 35 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,13 +1,92 @@
[build-system]
[project]
name = "memray"
version = "1.11.0"
description = "A memory profiler for Python applications"
readme = "README.md"
requires-python = ">=3.7.0"
license = { text = "Apache 2.0" }
authors = [{ name = "Pablo Galindo Salgado" }]
classifiers = [
"Intended Audience :: Developers",
"License :: OSI Approved :: Apache Software License",
"Operating System :: POSIX :: Linux",
"Operating System :: MacOS",
"Programming Language :: Python :: 3.8",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
"Programming Language :: Python :: 3.12",
"Programming Language :: Python :: Implementation :: CPython",
"Topic :: Software Development :: Debuggers",
]
dependencies = [
"jinja2 >= 2.9",
"typing_extensions; python_version < '3.8.0'",
"rich >= 11.2.0",
"textual >= 0.41.0",
]

requires = [
"setuptools",
"wheel",
"pkgconfig",
"Cython>=0.29.31"
[project.optional-dependencies]
test = [
"Cython",
"greenlet; python_version < '3.12'",
"pytest",
"pytest-cov",
"ipython",
"setuptools; python_version >= '3.12'",
"pytest-textual-snapshot",
]
docs = [
"IPython",
"bump2version",
"sphinx",
"furo",
"sphinx-argparse",
"towncrier",
]
lint = ["black", "flake8", "isort", "mypy", "check-manifest"]
benchmark = ["asv"]
dev = [
"Cython",
"greenlet; python_version < '3.12'",
"pytest",
"pytest-cov",
"ipython",
"setuptools; python_version >= '3.12'",
"pytest-textual-snapshot",
"black",
"flake8",
"isort",
"mypy",
"check-manifest",
"IPython",
"bump2version",
"sphinx",
"furo",
"sphinx-argparse",
"towncrier",
"asv",
]

build-backend = 'setuptools.build_meta'
[project.urls]
Homepage = "https://github.com/bloomberg/memray"

[project.scripts]
memray = "memray.__main__:main"
"memray3.7" = "memray.__main__:main"
"memray3.8" = "memray.__main__:main"
"memray3.9" = "memray.__main__:main"
"memray3.10" = "memray.__main__:main"
"memray3.11" = "memray.__main__:main"
"memray3.12" = "memray.__main__:main"

[build-system]
requires = ["scikit-build-core", "cython"]
build-backend = "scikit_build_core.build"

[tool.scikit-build]
wheel.packages = ["src/memray"]


[tool.ruff]
line-length = 95
Expand All @@ -17,7 +96,7 @@ fix = true
[tool.ruff.isort]
force-single-line = true
known-first-party = ["memray"]
known-third-party=["rich", "elftools", "pytest"]
known-third-party = ["rich", "elftools", "pytest"]

[tool.ruff.per-file-ignores]
"benchmarks/*" = ["C4", "PERF"]
Expand All @@ -29,36 +108,32 @@ include_trailing_comma = true
force_grid_wrap = 0
use_parentheses = true
line_length = 88
known_first_party=["memray"]
known_third_party=["rich", "elftools", "pytest"]
known_first_party = ["memray"]
known_third_party = ["rich", "elftools", "pytest"]

[tool.towncrier]
package = "memray"
package_dir = "src"
filename = "NEWS.rst"
directory = "news"
type = [
{ name = "Features", directory = "feature", showcontent = true },
{ name = "Features", directory = "feature", showcontent = true },
{ name = "Deprecations and Removals", directory = "removal", showcontent = true },
{ name = "Bug Fixes", directory = "bugfix", showcontent = true },
{ name = "Improved Documentation", directory = "doc", showcontent = true },
{ name = "Miscellaneous", directory = "misc", showcontent = true },
{ name = "Bug Fixes", directory = "bugfix", showcontent = true },
{ name = "Improved Documentation", directory = "doc", showcontent = true },
{ name = "Miscellaneous", directory = "misc", showcontent = true },
]
underlines = "-~"

[tool.pytest.ini_options]
markers = [
"valgrind",
]
markers = ["valgrind"]
xfail_strict = true

[tool.check-manifest]
ignore = [
"src/memray/reporters/templates/assets/*.js",
]
ignore = ["src/memray/reporters/templates/assets/*.js"]

[tool.mypy]
exclude="tests/integration/(native_extension|multithreaded_extension)/"
exclude = "tests/integration/(native_extension|multithreaded_extension)/"

[tool.cibuildwheel]
build = ["cp38-*", "cp39-*", "cp310-*", "cp311-*"]
Expand All @@ -82,26 +157,19 @@ before-all = [
"git clone --depth 1 --branch v1.9.4 https://github.com/lz4/lz4 lz4",
"cd lz4",
"make",
"make install DESTDIR=/tmp/lz4_install"
"make install DESTDIR=/tmp/lz4_install",
]
before-test = [
"codesign --remove-signature /Library/Frameworks/Python.framework/Versions/*/bin/python3 || true",
"codesign --remove-signature /Library/Frameworks/Python.framework/Versions/*/Resources/Python.app/Contents/MacOS/Python || true",
]

[tool.coverage.run]
plugins = [
"Cython.Coverage",
]
source = [
"src/memray",
"tests/",
]
plugins = ["Cython.Coverage"]
source = ["src/memray", "tests/"]
branch = true
parallel = true
omit = [
"*__init__.py",
]
omit = ["*__init__.py"]

[tool.coverage.report]
skip_covered = true
Expand All @@ -111,6 +179,4 @@ show_missing = true
# Override the default linux before-all for musl linux
[[tool.cibuildwheel.overrides]]
select = "*-musllinux*"
before-all = [
"apk add --update libunwind-dev lz4-dev gdb lldb",
]
before-all = ["apk add --update libunwind-dev lz4-dev gdb lldb"]
10 changes: 5 additions & 5 deletions src/memray/_memray.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,11 @@ from libcpp.unordered_map cimport unordered_map
from libcpp.utility cimport move
from libcpp.vector cimport vector

from ._destination import Destination
from ._destination import FileDestination
from ._destination import SocketDestination
from ._metadata import Metadata
from ._stats import Stats
from memray._destination import Destination
from memray._destination import FileDestination
from memray._destination import SocketDestination
from memray._metadata import Metadata
from memray._stats import Stats


def set_log_level(int level):
Expand Down

0 comments on commit a73f4f0

Please sign in to comment.