Skip to content

Commit 3b29ba6

Browse files
authored
[FEATURE]: create python binding (#128)
* Use scikit build * Feat: support plugin in python * Clean up * Apply the suggestion from copilot * Remove create_cache * Remove redundant header * Complete binding for trace type * Clean up python code * Update README.md * Prepare for PyPI
1 parent 31fc1a9 commit 3b29ba6

32 files changed

+3800
-17
lines changed

.github/workflows/python.yml

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
name: Python
2+
3+
on: [push, pull_request]
4+
5+
jobs:
6+
build:
7+
runs-on: ubuntu-latest
8+
steps:
9+
- uses: actions/checkout@v4
10+
11+
- name: Set up Python
12+
uses: actions/setup-python@v4
13+
with:
14+
python-version: "3.10"
15+
16+
- name: Prepare
17+
run: bash scripts/install_dependency.sh
18+
19+
- name: Build main libCacheSim project
20+
run: |
21+
cmake -G Ninja -B build
22+
ninja -C build
23+
24+
- name: Install Python dependencies
25+
run: |
26+
pip install --upgrade pip
27+
pip install -r requirements.txt
28+
pip install pytest
29+
30+
- name: Build libCacheSim-python
31+
run: |
32+
cd libCacheSim-python
33+
pip install -e .
34+
35+
- name: Run tests
36+
run: |
37+
cd libCacheSim-python
38+
pytest tests/

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,5 @@ sftp-config.json
2020
# Clangd cache
2121
*.cache/
2222
.lint-logs/
23+
# Python wheels
24+
*.whl

CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,8 @@ else()
247247
message(STATUS "Building without test")
248248
endif()
249249

250+
# Export variables for scikit-build -> build/export_vars.cmake
251+
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/libCacheSim-python/export)
250252

251253
# libCacheSim unified library compilation and installation
252254
# Create a single library that combines all modular libraries

libCacheSim-python/.gitignore

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# Automatically generated by `hgimportsvn`
2+
.svn
3+
.hgsvn
4+
5+
# Ignore local virtualenvs
6+
lib/
7+
bin/
8+
include/
9+
.Python/
10+
11+
# These lines are suggested according to the svn:ignore property
12+
# Feel free to enable them by uncommenting them
13+
*.pyc
14+
*.pyo
15+
*.swp
16+
*.class
17+
*.orig
18+
*~
19+
.hypothesis/
20+
21+
# autogenerated
22+
src/_pytest/_version.py
23+
# setuptools
24+
.eggs/
25+
26+
doc/*/_build
27+
doc/*/.doctrees
28+
build/
29+
dist/
30+
*.egg-info
31+
htmlcov/
32+
issue/
33+
env/
34+
.env/
35+
.venv/
36+
/pythonenv*/
37+
3rdparty/
38+
.tox
39+
.cache
40+
.pytest_cache
41+
.mypy_cache
42+
.coverage
43+
.coverage.*
44+
coverage.xml
45+
.ropeproject
46+
.idea
47+
.hypothesis
48+
.pydevproject
49+
.project
50+
.settings
51+
.vscode
52+
__pycache__/
53+
.python-version
54+
55+
# generated by pip
56+
pip-wheel-metadata/
57+
58+
# pytest debug logs generated via --debug
59+
pytestdebug.log

libCacheSim-python/CMakeLists.txt

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
cmake_minimum_required(VERSION 3.15...3.27)
2+
3+
# Include exported variables from cache
4+
if(DEFINED LIBCB_BUILD_DIR)
5+
set(PARENT_BUILD_DIR "${LIBCB_BUILD_DIR}")
6+
message(STATUS "Using provided LIBCB_BUILD_DIR: ${LIBCB_BUILD_DIR}")
7+
else()
8+
set(PARENT_BUILD_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../build")
9+
endif()
10+
set(EXPORT_FILE "${PARENT_BUILD_DIR}/export_vars.cmake")
11+
12+
if(EXISTS "${EXPORT_FILE}")
13+
include("${EXPORT_FILE}")
14+
message(STATUS "Loaded variables from export_vars.cmake")
15+
else()
16+
message(FATAL_ERROR "export_vars.cmake not found at ${EXPORT_FILE}. Please build the main project first (e.g. cd .. && cmake -G Ninja -B build)")
17+
endif()
18+
19+
# Force enable -fPIC
20+
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
21+
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fPIC")
22+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIC")
23+
24+
project(libCacheSim-python VERSION "${LIBCACHESIM_VERSION}")
25+
26+
if(LOG_LEVEL_LOWER STREQUAL "default")
27+
if(CMAKE_BUILD_TYPE_LOWER MATCHES "debug")
28+
add_compile_definitions(LOGLEVEL=6)
29+
else()
30+
add_compile_definitions(LOGLEVEL=7)
31+
endif()
32+
elseif(LOG_LEVEL_LOWER STREQUAL "verbose")
33+
add_compile_definitions(LOGLEVEL=5)
34+
elseif(LOG_LEVEL_LOWER STREQUAL "debug")
35+
add_compile_definitions(LOGLEVEL=6)
36+
elseif(LOG_LEVEL_LOWER STREQUAL "info")
37+
add_compile_definitions(LOGLEVEL=7)
38+
elseif(LOG_LEVEL_LOWER STREQUAL "warn")
39+
add_compile_definitions(LOGLEVEL=8)
40+
elseif(LOG_LEVEL_LOWER STREQUAL "error")
41+
add_compile_definitions(LOGLEVEL=9)
42+
else()
43+
add_compile_definitions(LOGLEVEL=7)
44+
endif()
45+
46+
# Find python and pybind11
47+
find_package(Python REQUIRED COMPONENTS Interpreter Development.Module)
48+
find_package(pybind11 CONFIG REQUIRED)
49+
50+
# Include directories for dependencies
51+
include_directories(${GLib_INCLUDE_DIRS})
52+
include_directories(${GLib_CONFIG_INCLUDE_DIR})
53+
include_directories(${XGBOOST_INCLUDE_DIR})
54+
include_directories(${LIGHTGBM_PATH})
55+
include_directories(${ZSTD_INCLUDE_DIR})
56+
include_directories(${MAIN_PROJECT_SOURCE_DIR}/libCacheSim/bin)
57+
58+
# Find the main libCacheSim library
59+
set(MAIN_PROJECT_BUILD_DIR "${PARENT_BUILD_DIR}")
60+
set(MAIN_PROJECT_LIB_PATH "${MAIN_PROJECT_BUILD_DIR}/liblibCacheSim.a")
61+
62+
if(EXISTS "${MAIN_PROJECT_LIB_PATH}")
63+
message(STATUS "Found pre-built libCacheSim library at ${MAIN_PROJECT_LIB_PATH}")
64+
65+
# Import the main library as an imported target
66+
add_library(libCacheSim_main STATIC IMPORTED)
67+
set_target_properties(libCacheSim_main PROPERTIES
68+
IMPORTED_LOCATION "${MAIN_PROJECT_LIB_PATH}"
69+
INTERFACE_INCLUDE_DIRECTORIES "${MAIN_PROJECT_SOURCE_DIR}/libCacheSim/include;${MAIN_PROJECT_SOURCE_DIR}/libCacheSim/utils/include;${MAIN_PROJECT_SOURCE_DIR}/libCacheSim"
70+
)
71+
72+
# Link dependencies that the main library needs
73+
target_link_libraries(libCacheSim_main INTERFACE ${dependency_libs})
74+
set(LIBCACHESIM_TARGET libCacheSim_main)
75+
76+
else()
77+
message(FATAL_ERROR "Pre-built libCacheSim library not found. Please build the main project first: cd .. && cmake -G Ninja -B build && ninja -C build")
78+
endif()
79+
80+
python_add_library(_libcachesim MODULE
81+
src/pylibcachesim.cpp
82+
${MAIN_PROJECT_SOURCE_DIR}/libCacheSim/bin/cli_reader_utils.c
83+
WITH_SOABI
84+
)
85+
86+
set_target_properties(_libcachesim PROPERTIES
87+
POSITION_INDEPENDENT_CODE ON
88+
INSTALL_RPATH_USE_LINK_PATH TRUE
89+
BUILD_WITH_INSTALL_RPATH TRUE
90+
INSTALL_RPATH "$ORIGIN"
91+
)
92+
93+
target_compile_definitions(_libcachesim PRIVATE VERSION_INFO=${PROJECT_VERSION})
94+
95+
target_link_libraries(_libcachesim PRIVATE
96+
${LIBCACHESIM_TARGET}
97+
pybind11::headers
98+
pybind11::module
99+
-Wl,--no-as-needed -ldl
100+
)
101+
102+
# install to wheel directory
103+
install(TARGETS _libcachesim LIBRARY DESTINATION libcachesim)

libCacheSim-python/MAINFEST.in

Whitespace-only changes.

0 commit comments

Comments
 (0)