Skip to content

Commit

Permalink
fixup! Use scikit-build-core
Browse files Browse the repository at this point in the history
  • Loading branch information
godlygeek committed Sep 18, 2024
1 parent 4d55628 commit 8d36fd5
Show file tree
Hide file tree
Showing 13 changed files with 222 additions and 127 deletions.
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
102 changes: 65 additions & 37 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 3.7)
cmake_minimum_required(VERSION 3.24...3.26)
project(memray)

set(CMAKE_CXX_STANDARD 17)
Expand All @@ -21,21 +21,22 @@ if(NOT LZ4_FOUND)
endif()

if(${CMAKE_SYSTEM_NAME} MATCHES "Linux")
pkg_check_modules(LIBUNWIND REQUIRED libunwind)
pkg_check_modules(UNWIND REQUIRED libunwind)
pkg_check_modules(DEBUGINFOD libdebuginfod)
if(NOT DEBUGINFOD_LIBRARIES)
set(DEBUGINFOD_LIBRARIES "debuginfod")
endif()
endif()

# Set compiler flags
set(COMPILER_FLAGS "-Wall")
add_compile_options(-Wall)
if(NOT DEFINED ENV{NO_MEMRAY_FAST_TLS})
add_definitions(-DUSE_MEMRAY_TLS_MODEL=1)
add_compile_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()
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")
Expand All @@ -48,21 +49,23 @@ 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_DIR}/install/include)
set(LIBBACKTRACE_LIB_DIR ${LIBBACKTRACE_DIR}/install/lib)
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_INSTALL_DIR}/include/libbacktrace
COMMAND cd ${CMAKE_CURRENT_BINARY_DIR}/libbacktrace_build && make -j
COMMAND cd ${CMAKE_CURRENT_BINARY_DIR}/libbacktrace_build && make install
${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)
Expand All @@ -72,15 +75,22 @@ add_custom_target(libbacktrace DEPENDS ${LIBBACKTRACE_LIB_DIR}/libbacktrace.a)
add_custom_command(
OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/_memray.cpp
COMMAND Python::Interpreter -m cython
--cplus
-3
--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.pyx
${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
)
set(MEMRAY_SOURCES
python_add_library(_memray MODULE WITH_SOABI
src/memray/_memray/compat.cpp
src/memray/_memray/hooks.cpp
src/memray/_memray/tracking_api.cpp
Expand All @@ -95,45 +105,63 @@ set(MEMRAY_SOURCES
src/memray/_memray/snapshot.cpp
src/memray/_memray/socket_reader_thread.cpp
src/memray/_memray/native_resolver.cpp
${CMAKE_CURRENT_BINARY_DIR}/_memray.cpp
)
python_add_library(_memray MODULE WITH_SOABI ${MEMRAY_SOURCES} ${CMAKE_CURRENT_BINARY_DIR}/_memray.cpp)
target_include_directories(_memray PRIVATE

target_include_directories(_memray PRIVATE
${CMAKE_SOURCE_DIR}/src/memray/_memray
${LIBBACKTRACE_INCLUDE_DIR}
${LIBBACKTRACE_INCLUDE_DIR}
${LZ4_INCLUDE_DIRS}
${UNWIND_INCLUDE_DIRS}
${DEBUGINFOD_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})
target_link_libraries(_memray PRIVATE
${LIBBACKTRACE_LIB_DIR}/libbacktrace.a
${LZ4_LIBRARIES}
${UNWIND_LIBRARIES}
${DEBUGINFOD_LIBRARIES}
dl
)
set_target_properties(_memray PROPERTIES INSTALL_RPATH "${DEBUGINFOD_LIBRARY_DIRS}")

set(CMAKE_BUILD_RPATH "${LZ4_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
--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
${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
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
python_add_library(_inject MODULE WITH_SOABI USE_SABI 3.7
src/memray/_memray/inject.cpp
)
python_add_library(_inject MODULE WITH_SOABI USE_SABI 3.7 ${INJECT_SOURCES})
target_include_directories(_inject PRIVATE
target_include_directories(_inject PRIVATE
${CMAKE_SOURCE_DIR}/src/memray
)
target_compile_options(_test_utils PRIVATE ${COMPILER_FLAGS})
Expand All @@ -143,4 +171,4 @@ target_link_options(_inject PRIVATE ${LINKER_FLAGS})


# Install targets
install(TARGETS _memray _test_utils _inject LIBRARY DESTINATION memray)
install(TARGETS _memray _test_utils _inject LIBRARY DESTINATION memray)
41 changes: 0 additions & 41 deletions MANIFEST.in

This file was deleted.

6 changes: 5 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,11 @@ build: build-js build-ext ## (default) Build package extensions and assets in-p

.PHONY: build-ext
build-ext: ## Build package extensions in-place
$(PYTHON) -m pip install --no-build-isolation --config-settings=editable.rebuild=true -Cbuild-dir=build -ve.
$(PYTHON) -m pip install -ve . \
--no-build-isolation \
--config-settings=editable.verbose=false \
--config-settings=cmake.build-type=Debug \
-Cbuild-dir=build

$(reporters_path)/templates/assets/%.js: $(reporters_path)/assets/%.js
$(NPM) install
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
6 changes: 3 additions & 3 deletions docs/run.rst
Original file line number Diff line number Diff line change
Expand Up @@ -140,15 +140,15 @@ You can run a program in live mode using ``run --live``:

.. code:: shell
memray3.9 run --live application.py
memray run --live application.py
Immediately Memray will start your application in the background and will run a TUI in the foreground that you can use
to analyze your application's memory usage. If you don't want to run your program in the background, you can instead
use ``run --live-remote``:

.. code:: shell
memray3.9 run --live-remote application.py
memray run --live-remote application.py
In this mode, Memray will choose an unused port, bind to it, and display a message saying:

Expand All @@ -160,7 +160,7 @@ It will wait for you to run:

.. code:: shell
memray3.9 live <port>
memray live <port>
in another terminal window to attach to it. Regardless of whether you choose to use one terminal or two, the resulting
TUI is exactly the same. See :doc:`live` for details on how to interpret and control the TUI.
Expand Down
Loading

0 comments on commit 8d36fd5

Please sign in to comment.