Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use scikit-build #673

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ jobs:
echo 0 | sudo tee /proc/sys/kernel/yama/ptrace_scope
- name: Run coverage
env:
CYTHON_TEST_MACROS: 1
PYTHON: ./venv/bin/python
run: |
make dev-install pycoverage
Expand Down Expand Up @@ -104,7 +103,6 @@ jobs:
/venv/bin/python -m pip install --upgrade pip
- name: Run coverage
env:
CYTHON_TEST_MACROS: 1
PYTHON: /venv/bin/python
GENHTMLOPTS: "--ignore-errors inconsistent"
run: |
Expand Down Expand Up @@ -161,8 +159,6 @@ jobs:
python3 -m pip install --upgrade pip
python3 -m pip install -r requirements-test.txt
python3 -m pip install -e .
env:
MEMRAY_MINIMIZE_INLINING: 1
- name: Run Valgrind
run: make valgrind
- name: Run Helgrind
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/coverage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ jobs:
python3.10-dbg
- name: Install Python dependencies
run: |
python3 -m pip install --upgrade pip cython pkgconfig
python3 -m pip install --upgrade pip scikit-build-core cython pkgconfig
make test-install
- name: Disable ptrace security restrictions
run: |
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ CMakeFiles
CMakeScripts
Testing
Makefile
!/Makefile
cmake_install.cmake
install_manifest.txt
compile_commands.json
Expand Down
193 changes: 193 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,193 @@
cmake_minimum_required(VERSION 3.24...3.26)
project(memray)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)

# 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 liblz4)
if(NOT LZ4_FOUND)
message(FATAL_ERROR "liblz4 not found. Please install liblz4-dev or equivalent.")
endif()

if(${CMAKE_SYSTEM_NAME} MATCHES "Linux")
pkg_check_modules(UNWIND libunwind)
if(NOT UNWIND_FOUND)
message(FATAL_ERROR "libunwind not found. Please install libunwind-dev or equivalent.")
endif()

pkg_check_modules(DEBUGINFOD libdebuginfod)
if(NOT DEBUGINFOD_FOUND)
# Some systems don't have a libdebuginfod.pc file.
# See if there's a libdebuginfod.so wherever liblz4 or libunwind are.
include(CheckLibraryExists)
check_library_exists("libdebuginfod.so" "debuginfod_find_debuginfo" "${LZ4_LIBRARY_DIRS};${UNWIND_LIBRARY_DIRS}" DEBUGINFOD)
if(DEBUGINFOD)
set(DEBUGINFOD_LIBRARIES "debuginfod")
else()
message(FATAL_ERROR "libdebuginfod not found. Please install libdebuginfod-dev or equivalent.")
endif()
endif()
endif()

# Set compiler flags
add_compile_options(-Wall)
if(NOT DEFINED ENV{NO_MEMRAY_FAST_TLS})
add_compile_definitions(-DUSE_MEMRAY_TLS_MODEL=1)
endif()

set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -flto")
set(CMAKE_CXX_FLAGS_RELWITHDEBINFO "${CMAKE_CXX_FLAGS_RELWITHDEBINFO} -flto")
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -Og")

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_INSTALL_DIR ${LIBBACKTRACE_DIR}/install)
set(LIBBACKTRACE_INCLUDE_DIR ${LIBBACKTRACE_INSTALL_DIR}/include)
set(LIBBACKTRACE_LIB_DIR ${LIBBACKTRACE_INSTALL_DIR}/lib)

# Add custom command to build libbacktrace
add_custom_command(
OUTPUT ${LIBBACKTRACE_LIB_DIR}/libbacktrace.a
OUTPUT ${LIBBACKTRACE_INCLUDE_DIR}/libbacktrace/backtrace.h
OUTPUT ${LIBBACKTRACE_INCLUDE_DIR}/libbacktrace/internal.h
COMMAND mkdir -p ${LIBBACKTRACE_INSTALL_DIR}
COMMAND mkdir -p ${CMAKE_CURRENT_BINARY_DIR}/libbacktrace_build
COMMAND cd ${CMAKE_CURRENT_BINARY_DIR}/libbacktrace_build &&
${LIBBACKTRACE_DIR}/configure
--with-pic
--prefix=${LIBBACKTRACE_INSTALL_DIR}
--includedir=${LIBBACKTRACE_INCLUDE_DIR}/libbacktrace
COMMAND make -C ${CMAKE_CURRENT_BINARY_DIR}/libbacktrace_build -j
COMMAND make -C ${CMAKE_CURRENT_BINARY_DIR}/libbacktrace_build 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
-X embedsignature=True
-X boundscheck=$<IF:$<CONFIG:Debug>,True,False>
-X wraparound=$<IF:$<CONFIG:Debug>,True,False>
-X overflowcheck=$<IF:$<CONFIG:Debug>,True,False>
-X cdivision=$<IF:$<CONFIG:Debug>,False,True>
-X c_string_type=unicode
-X c_string_encoding=utf8
-I ${CMAKE_SOURCE_DIR}/src/memray/
${CMAKE_SOURCE_DIR}/src/memray/_memray.pyx
-o ${CMAKE_CURRENT_BINARY_DIR}/_memray.cpp
--module-name memray._memray
DEPENDS ${CMAKE_SOURCE_DIR}/src/memray/_memray.pyx
VERBATIM
)
python_add_library(_memray MODULE WITH_SOABI
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
${CMAKE_CURRENT_BINARY_DIR}/_memray.cpp
)

target_include_directories(_memray PRIVATE
${CMAKE_SOURCE_DIR}/src/memray/_memray
${LIBBACKTRACE_INCLUDE_DIR}
${LZ4_INCLUDE_DIRS}
${UNWIND_INCLUDE_DIRS}
${DEBUGINFOD_INCLUDE_DIRS}
)
target_link_libraries(_memray PRIVATE
${LIBBACKTRACE_LIB_DIR}/libbacktrace.a
${LZ4_LIBRARIES}
${UNWIND_LIBRARIES}
${DEBUGINFOD_LIBRARIES}
dl
)
target_link_directories(_memray PRIVATE
${LZ4_LIBRARY_DIRS}
${UNWIND_LIBRARY_DIRS}
${DEBUGINFOD_LIBRARY_DIRS}
)
target_link_options(_memray PRIVATE
${LZ4_LDFLAGS}
${UNWIND_LDFLAGS}
${DEBUGINFOD_LDFLAGS}
)
target_compile_options(_memray PRIVATE
${LZ4_CFLAGS}
${UNWIND_CFLAGS}
${DEBUGINFOD_CFLAGS}
)
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
-X embedsignature=True
-X boundscheck=False
-X wraparound=False
-X cdivision=True
-X c_string_type=unicode
-X c_string_encoding=utf8
-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

python_add_library(_inject MODULE WITH_SOABI USE_SABI 3.7
src/memray/_memray/inject.cpp
)
target_include_directories(_inject PRIVATE
${CMAKE_SOURCE_DIR}/src/memray
)

# Install targets
install(TARGETS _memray _test_utils _inject LIBRARY DESTINATION memray)
3 changes: 1 addition & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@ ENV VIRTUAL_ENV=/venv \
RUN python3 -m venv "$VIRTUAL_ENV"

ENV PATH="${VIRTUAL_ENV}/bin:/usr/lib/ccache:${PATH}" \
PYTHON="${VIRTUAL_ENV}/bin/python" \
MEMRAY_MINIMIZE_INLINING="1"
PYTHON="${VIRTUAL_ENV}/bin/python"

COPY requirements-test.txt requirements-extra.txt requirements-docs.txt /tmp/

Expand Down
41 changes: 0 additions & 41 deletions MANIFEST.in

This file was deleted.

10 changes: 5 additions & 5 deletions 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 -ve .

$(reporters_path)/templates/assets/%.js: $(reporters_path)/assets/%.js
$(NPM) install
Expand All @@ -41,15 +41,15 @@ dist: ## Generate Python distribution files

.PHONY: install-sdist
install-sdist: dist ## Install from source distribution
$(ENV) $(PIP_INSTALL) $(wildcard dist/*.tar.gz)
$(PIP_INSTALL) $(wildcard dist/*.tar.gz)

.PHONY: test-install
test-install: build-js ## Install with test dependencies
$(ENV) CYTHON_TEST_MACROS=1 $(PIP_INSTALL) -e .[test]
$(PIP_INSTALL) -e .[test]

.PHONY: dev-install
dev-install: build-js ## Install with dev dependencies
$(ENV) CYTHON_TEST_MACROS=1 $(PIP_INSTALL) -e .[dev]
$(PIP_INSTALL) -e .[dev]

.PHONY: check
check: check-python check-js ## Run all the tests
Expand Down Expand Up @@ -102,7 +102,7 @@ helgrind: ## Run helgrind, with the correct configuration
.PHONY: ccoverage
ccoverage: ## Run the test suite, with C++ code coverage
$(MAKE) clean
CFLAGS="$(CFLAGS) -O0 -pg --coverage" $(MAKE) build
CFLAGS="$(CFLAGS) -O0 -pg --coverage" CXXFLAGS="$(CXXFLAGS) -O0 -pg --coverage" $(MAKE) build
$(MAKE) check
gcov -i build/*/src/memray/_memray -i -d
lcov --capture --directory . --output-file cppcoverage.lcov
Expand Down
6 changes: 3 additions & 3 deletions docs/examples/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ Make sure you install the required dependencies by running
directory. The examples below use the project in the ``mandelbrot`` folder, but
you can use the same instructions to launch the other examples as well.

To track memory allocations, invoke ``memray3.9 run``:
To track memory allocations, invoke ``memray run``:

.. code:: shell

memray3.9 run mandelbrot/mandelbrot.py
memray run mandelbrot/mandelbrot.py

Memray will print a message displaying the output file it creates.

Expand All @@ -28,7 +28,7 @@ graph, use the following command:

.. code:: shell

memray3.9 flamegraph mandelbrot/memray-mandelbrot.py.187967.bin
memray flamegraph mandelbrot/memray-mandelbrot.py.187967.bin

The HTML file for the flame graph will be generated under
``mandelbrot/memray-flamegraph-mandelbrot.py.187967.html``. The flame graph
Expand Down
14 changes: 4 additions & 10 deletions docs/getting_started.rst
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,13 @@ You can invoke Memray the following way:

python3.9 -m memray

Or alternatively through the version-qualified ``memrayX.Y`` script:

.. code:: shell

memray3.9

You can also invoke Memray without version-qualifying it:
Or alternatively through the ``memray`` script:

.. code:: shell

memray

The downside to the unqualified ``memray`` script is that it's not immediately
The downside to using the ``memray`` script is that it's not immediately
clear what Python interpreter will be used to execute Memray. If you're using
a virtual environment that's not a problem because you know exactly what interpreter is
in use, but otherwise you need to be careful to ensure that ``memray`` is
Expand All @@ -56,7 +50,7 @@ To run memray on the ``example.py`` script, use :doc:`the run subcommand <run>`.

.. code:: shell

memray3.9 run example.py
memray run example.py

This will execute the script and track its memory allocations, displaying the name of the file where results are being recorded with a message like:

Expand All @@ -72,7 +66,7 @@ the results file:

.. code:: shell

memray3.9 flamegraph memray-example.py.4131.bin
memray flamegraph memray-example.py.4131.bin

This will generate the ``memray-flamegraph-example.py.4131.html`` file in the current directory. See the :doc:`flamegraph`
documentation which explains how to interpret flame graphs.
Expand Down
Loading
Loading