Skip to content

Commit 3b611dd

Browse files
authored
Merge pull request #560 from Blosc/restructure_install
Implement PEP recommendations
2 parents d1e8394 + 771521a commit 3b611dd

2 files changed

Lines changed: 95 additions & 11 deletions

File tree

CMakeLists.txt

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,8 @@ set(MINIEXPR_BUILD_BENCH OFF CACHE BOOL "Build miniexpr benchmarks" FORCE)
5858

5959
FetchContent_Declare(miniexpr
6060
GIT_REPOSITORY https://github.com/Blosc/miniexpr.git
61-
# GIT_TAG 979573da618e0443c3984bad8db3ed5d9ce72f75 # latest commit in main
62-
GIT_TAG main
63-
GIT_SHALLOW TRUE # fetch only the latest commit (only works with a branch in GIT_TAG)
64-
# In case you want to use a local copy of miniexpr for development, uncomment the line below
65-
# SOURCE_DIR "/Users/faltet/blosc/miniexpr"
61+
GIT_TAG main # latest commit in main
62+
GIT_SHALLOW True
6663
)
6764
FetchContent_MakeAvailable(miniexpr)
6865

@@ -111,7 +108,11 @@ else()
111108
set(DEACTIVATE_OPENZL ON CACHE BOOL "Do not include support for the OpenZL library.")
112109
endif()
113110
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
114-
# we want the binaries of the C-Blosc2 library to go into the wheels
111+
set(CMAKE_INSTALL_INCLUDEDIR ${SKBUILD_PLATLIB_DIR}/blosc2/include) # directory for include files
112+
set(CMAKE_INSTALL_LIBDIR ${SKBUILD_PLATLIB_DIR}/blosc2/lib) # directory for libblosc2 and pkgconfig
113+
set(Blosc2_INSTALL_CMAKEDIR ${CMAKE_INSTALL_LIBDIR}/cmake/blosc2) # directory for cmake files
114+
set(CMAKE_INSTALL_BINDIR ${SKBUILD_PLATLIB_DIR}/blosc2/lib) # directory for libblosc2.dll on windows
115+
# we will put the binaries of the C-Blosc2 library into the wheels according to PEP
115116
set(BLOSC_INSTALL ON)
116117
include(FetchContent)
117118
FetchContent_Declare(blosc2
@@ -126,9 +127,25 @@ else()
126127
target_link_libraries(blosc2_ext PRIVATE blosc2_static)
127128
endif()
128129

129-
add_custom_command(
130-
TARGET blosc2_ext POST_BUILD
131-
COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:blosc2_ext> ${CMAKE_SOURCE_DIR}/blosc2
132-
)
130+
# TODO
131+
# CHECK THIS
132+
if(UNIX)
133+
set_target_properties(blosc2_ext PROPERTIES
134+
BUILD_WITH_INSTALL_RPATH TRUE
135+
INSTALL_RPATH "$<IF:$<PLATFORM_ID:Darwin>,@loader_path/lib,\$ORIGIN/lib>"
136+
)
137+
endif()
133138

134-
install(TARGETS blosc2_ext LIBRARY DESTINATION blosc2)
139+
if(WIN32)
140+
add_custom_command(TARGET blosc2_ext POST_BUILD
141+
COMMAND ${CMAKE_COMMAND} -E copy_if_different
142+
$<TARGET_FILE:blosc2_shared>
143+
$<TARGET_FILE_DIR:blosc2_ext>
144+
)
145+
endif()
146+
147+
# Python extension -> site-packages/blosc2
148+
install(
149+
TARGETS blosc2_ext
150+
LIBRARY DESTINATION ${SKBUILD_PLATLIB_DIR}/blosc2
151+
)

README_DEVELOPERS.md

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,3 +115,70 @@ and run pytest from the `array-api-tests` source dir like this:
115115
``` bash
116116
ARRAY_API_TESTS_MODULE=blosc2 pytest array_api_tests --xfails-file ${BLOSC2_DIR}/tests/array-api-xfails.txt -xs
117117
```
118+
119+
# Using the C-library
120+
Since C-blosc2 is shipped as a compiled binary with python-blosc2, one can compile and run C code using C-blosc2 functions. As of python-blosc2 version 4.0, one can find the location of the ``include`` files and binaries as follows. Run the following command in the terminal, which will give as output the path to the ``__init__.py`` file within the blosc2 folder.
121+
```bash
122+
python -c "import blosc2; print(blosc2.__file__)"
123+
path/to/blosc2/__init__.py
124+
```
125+
## Using CMake
126+
One may then access the include files via ``path/to/blosc2/include`` and the binaries via ``path/to/blosc2/lib``. Thus one may link a C-app via a ``CMakelists.txt`` file with the following snippet
127+
```
128+
# Add directory to search list for find_package
129+
set(CMAKE_PREFIX_PATH "$(python - <<EOF
130+
import blosc2, pathlib
131+
print(pathlib.Path(blosc2.__file__).parent)
132+
EOF)")
133+
134+
find_package(Blosc2 CONFIG REQUIRED)
135+
136+
target_link_libraries(myapp PRIVATE Blosc2::blosc2_shared)
137+
```
138+
139+
## Using pkg-config
140+
If one prefers to avoid using CMake, one can also use the pkg-config that is shipped with the wheel by running the following sequence of commands
141+
142+
First
143+
```
144+
BLOSC2_PREFIX=$(python - <<'EOF'
145+
import blosc2, pathlib
146+
print(pathlib.Path(blosc2.__file__).parent)
147+
EOF
148+
)\
149+
export PKG_CONFIG_PATH="$BLOSC2_PREFIX/lib/pkgconfig"
150+
```
151+
152+
We can check that the .pc file has the required info and has been found via
153+
``bash
154+
pkg-config --modversion blosc2
155+
``
156+
157+
Then define a test program
158+
```bash
159+
cat > test.c <<'EOF'
160+
#include <stdio.h>
161+
#include <blosc2.h>
162+
163+
int main(void) {
164+
printf(blosc2_get_version_string());
165+
return 0;
166+
}
167+
EOF
168+
```
169+
and compile it to an executable
170+
```bash
171+
gcc test.c \
172+
$(pkg-config --cflags --libs blosc2) \
173+
-Wl,--enable-new-dtags \
174+
-Wl,-rpath,"\$ORIGIN" \
175+
-o test_blosc2
176+
```
177+
The executable has to have access to the C library, so we copy the shared library to the executable directory
178+
```bash
179+
cp "$BLOSC2_PREFIX/lib/"libblosc2.so .
180+
```
181+
and run the executable
182+
```bash
183+
./test_blosc2
184+
```

0 commit comments

Comments
 (0)