Skip to content

Commit 8fe4158

Browse files
committed
WIP.
1 parent 58e9cb8 commit 8fe4158

File tree

3 files changed

+368
-142
lines changed

3 files changed

+368
-142
lines changed

CMakeLists.txt

Lines changed: 195 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,195 @@
1+
cmake_minimum_required(VERSION 3.15)
2+
project(python_lzo)
3+
4+
# Set C/C++ standards
5+
set(CMAKE_C_STANDARD 11)
6+
set(CMAKE_CXX_STANDARD 14)
7+
set(CMAKE_CXX_STANDARD_REQUIRED ON)
8+
9+
# Option to use system liblzo instead of building from source
10+
option(USE_SYSTEM_LZO "Use system-installed liblzo instead of building from source" OFF)
11+
12+
# Platform-specific settings
13+
if(WIN32)
14+
set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON)
15+
endif()
16+
17+
# Find Python for the extension
18+
find_package(Python COMPONENTS Interpreter Development REQUIRED)
19+
find_package(pybind11 CONFIG REQUIRED)
20+
21+
if(USE_SYSTEM_LZO)
22+
# Use system-installed liblzo
23+
find_library(LZO_LIBRARY
24+
NAMES lzo2 lzo
25+
PATHS /usr/lib /usr/local/lib /opt/local/lib
26+
DOC "liblzo library"
27+
)
28+
29+
find_path(LZO_INCLUDE_DIR
30+
NAMES lzo/lzo1x.h
31+
PATHS /usr/include /usr/local/include /opt/local/include
32+
DOC "liblzo include directory"
33+
)
34+
35+
if(NOT LZO_LIBRARY OR NOT LZO_INCLUDE_DIR)
36+
message(FATAL_ERROR "System liblzo not found. Install liblzo-dev or disable USE_SYSTEM_LZO")
37+
endif()
38+
39+
message(STATUS "Using system liblzo: ${LZO_LIBRARY}")
40+
message(STATUS "liblzo headers: ${LZO_INCLUDE_DIR}")
41+
42+
# Create imported target for system liblzo
43+
add_library(lzo_lib SHARED IMPORTED)
44+
set_target_properties(lzo_lib PROPERTIES
45+
IMPORTED_LOCATION ${LZO_LIBRARY}
46+
INTERFACE_INCLUDE_DIRECTORIES ${LZO_INCLUDE_DIR}
47+
)
48+
49+
else()
50+
# Build liblzo from source in lzo-2.10 subdirectory
51+
set(LZO_PROJECT_DIR "${CMAKE_CURRENT_SOURCE_DIR}/lzo-2.10")
52+
set(LZO_BUILD_DIR "${CMAKE_CURRENT_BINARY_DIR}/lzo_build")
53+
54+
# Determine build command based on platform
55+
if(WIN32)
56+
# Windows: Use MSBuild
57+
set(LZO_BUILD_COMMAND
58+
msbuild lzo_static_lib.vcxproj
59+
-p:Configuration=Release$<SEMICOLON>Platform=x64$<SEMICOLON>OutDir=..\\
60+
)
61+
set(LZO_STATIC_LIB "${LZO_BUILD_DIR}/lzo2.lib")
62+
set(LZO_CONFIGURE_COMMAND
63+
${CMAKE_COMMAND} -S ${LZO_PROJECT_DIR} -B ${LZO_BUILD_DIR}
64+
-DCMAKE_BUILD_TYPE=Release
65+
-A x64
66+
)
67+
else()
68+
# Linux/macOS: Use make
69+
set(LZO_BUILD_COMMAND make -j${CMAKE_BUILD_PARALLEL_LEVEL})
70+
if(APPLE)
71+
set(LZO_STATIC_LIB "${LZO_BUILD_DIR}/src/.libs/liblzo2.a")
72+
else()
73+
set(LZO_STATIC_LIB "${LZO_BUILD_DIR}/src/.libs/liblzo2.a")
74+
endif()
75+
set(LZO_CONFIGURE_COMMAND
76+
${CMAKE_COMMAND} -S ${LZO_PROJECT_DIR} -B ${LZO_BUILD_DIR}
77+
-DCMAKE_BUILD_TYPE=Release
78+
-DCMAKE_C_COMPILER=${CMAKE_C_COMPILER}
79+
-DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER}
80+
)
81+
endif()
82+
83+
# Configure and build liblzo using ExternalProject
84+
include(ExternalProject)
85+
ExternalProject_Add(lzo_project
86+
SOURCE_DIR ${LZO_PROJECT_DIR}
87+
BINARY_DIR ${LZO_BUILD_DIR}
88+
CONFIGURE_COMMAND ${LZO_CONFIGURE_COMMAND}
89+
BUILD_COMMAND ${LZO_BUILD_COMMAND}
90+
INSTALL_COMMAND "" # No install step needed
91+
BUILD_BYPRODUCTS ${LZO_STATIC_LIB}
92+
BUILD_IN_SOURCE 0
93+
)
94+
95+
# Create an imported target for the built liblzo static library
96+
add_library(lzo_lib STATIC IMPORTED)
97+
set_target_properties(lzo_lib PROPERTIES
98+
IMPORTED_LOCATION ${LZO_STATIC_LIB}
99+
INTERFACE_INCLUDE_DIRECTORIES ${LZO_PROJECT_DIR}/include
100+
)
101+
102+
# Make sure liblzo is built before we try to use it
103+
add_dependencies(lzo_lib lzo_project)
104+
endif()
105+
106+
# Compile the lzomodule.c file into an object library
107+
add_library(lzomodule_objects OBJECT
108+
lzomodule.c
109+
)
110+
111+
# Set properties for the object library
112+
target_include_directories(lzomodule_objects PRIVATE
113+
${Python_INCLUDE_DIRS}
114+
)
115+
116+
if(USE_SYSTEM_LZO)
117+
target_include_directories(lzomodule_objects PRIVATE ${LZO_INCLUDE_DIR})
118+
else()
119+
target_include_directories(lzomodule_objects PRIVATE ${LZO_PROJECT_DIR}/include)
120+
endif()
121+
122+
# Create the final Python extension module
123+
pybind11_add_module(lzo_module
124+
$<TARGET_OBJECTS:lzomodule_objects>
125+
)
126+
127+
# Link the lzo library (either system or built from source)
128+
target_link_libraries(lzo_module PRIVATE
129+
lzo_lib
130+
${Python_LIBRARIES}
131+
)
132+
133+
# Set target properties
134+
set_target_properties(lzo_module PROPERTIES
135+
OUTPUT_NAME "lzo" # This will create lzo.so/.dll/.pyd
136+
CXX_VISIBILITY_PRESET "hidden"
137+
INTERPROCEDURAL_OPTIMIZATION TRUE
138+
)
139+
140+
# Include directories for the final module
141+
if(USE_SYSTEM_LZO)
142+
target_include_directories(lzo_module PRIVATE ${LZO_INCLUDE_DIR})
143+
else()
144+
target_include_directories(lzo_module PRIVATE ${LZO_PROJECT_DIR}/include)
145+
# Ensure liblzo is built before our module
146+
add_dependencies(lzo_module lzo_project)
147+
endif()
148+
149+
# Platform-specific linking options
150+
if(WIN32)
151+
target_compile_definitions(lzo_module PRIVATE _WIN32_WINNT=0x0601)
152+
# On Windows, you might need additional libraries
153+
target_link_libraries(lzo_module PRIVATE ws2_32)
154+
elseif(APPLE)
155+
# macOS specific settings
156+
set_target_properties(lzo_module PROPERTIES
157+
INSTALL_RPATH "@loader_path"
158+
)
159+
elseif(UNIX)
160+
# Linux specific settings
161+
set_target_properties(lzo_module PROPERTIES
162+
INSTALL_RPATH "$ORIGIN"
163+
)
164+
target_link_libraries(lzo_module PRIVATE pthread dl)
165+
endif()
166+
167+
# Compiler-specific optimizations
168+
if(CMAKE_BUILD_TYPE STREQUAL "Release")
169+
if(MSVC)
170+
target_compile_options(lzo_module PRIVATE /O2)
171+
target_compile_options(lzomodule_objects PRIVATE /O2)
172+
else()
173+
target_compile_options(lzo_module PRIVATE -O3 -march=native)
174+
target_compile_options(lzomodule_objects PRIVATE -O3 -march=native)
175+
endif()
176+
endif()
177+
178+
# Print configuration summary
179+
message(STATUS "")
180+
message(STATUS "python-lzo Configuration Summary:")
181+
message(STATUS " Use system liblzo: ${USE_SYSTEM_LZO}")
182+
if(USE_SYSTEM_LZO)
183+
message(STATUS " liblzo library: ${LZO_LIBRARY}")
184+
message(STATUS " liblzo headers: ${LZO_INCLUDE_DIR}")
185+
else()
186+
message(STATUS " liblzo source: ${LZO_PROJECT_DIR}")
187+
message(STATUS " liblzo build: ${LZO_BUILD_DIR}")
188+
message(STATUS " Platform: ${CMAKE_SYSTEM_NAME}")
189+
if(WIN32)
190+
message(STATUS " Build tool: MSBuild")
191+
else()
192+
message(STATUS " Build tool: make")
193+
endif()
194+
endif()
195+
message(STATUS "")

pyproject.toml

Lines changed: 71 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,90 @@
11
[build-system]
22
requires = [
3-
"setuptools>=42",
4-
"wheel"
3+
"setuptools>=45",
4+
"wheel",
5+
"pybind11>=2.6.0",
6+
"cmake>=3.15",
7+
"ninja; platform_system!='Windows'"
58
]
69
build-backend = "setuptools.build_meta"
710

811
[project]
912
name = "python-lzo"
10-
version = "1.16"
11-
description = "Python bindings for the LZO data compression library"
12-
readme = "README.md"
13-
requires-python = ">=3.9"
13+
version = "1.15"
14+
description = "Python bindings for the LZO compression library"
1415
authors = [
15-
{name = "Markus F.X.J. Oberhumer", email = "[email protected]"},
16-
]
17-
maintainers = [
18-
{name = "Joshua D. Boyd", email = "[email protected]"},
16+
{name = "Markus F.X.J. Oberhumer", email = "[email protected]"}
1917
]
20-
keywords = ["lzo", "compression"]
21-
license = {text = "GNU General Public License v2 (GPLv2)"}
18+
readme = "README.md"
19+
license = {text = "GPL-2.0"}
20+
requires-python = ">=3.8"
2221
classifiers = [
2322
"Development Status :: 5 - Production/Stable",
2423
"Intended Audience :: Developers",
2524
"License :: OSI Approved :: GNU General Public License v2 (GPLv2)",
26-
"Topic :: Software Development :: Libraries",
25+
"Programming Language :: Python :: 3",
26+
"Programming Language :: Python :: 3.8",
27+
"Programming Language :: Python :: 3.9",
28+
"Programming Language :: Python :: 3.10",
29+
"Programming Language :: Python :: 3.11",
30+
"Programming Language :: Python :: 3.12",
31+
"Programming Language :: C",
32+
"Topic :: Software Development :: Libraries :: Python Modules",
2733
"Topic :: System :: Archiving :: Compression",
2834
]
35+
keywords = ["lzo", "compression"]
2936

3037
[project.optional-dependencies]
31-
test = ["pytest"]
38+
dev = [
39+
"pytest>=6.0",
40+
"pytest-cov",
41+
"black",
42+
"flake8",
43+
"mypy",
44+
]
45+
test = [
46+
"pytest>=6.0",
47+
]
3248

3349
[project.urls]
34-
Homepage = "https://github.com/jd-boyd/python-lzo"
35-
"Bug Tracker" = "https://github.com/jd-boyd/python-lzo/issues"
36-
37-
[tool.cibuildwheel]
38-
archs = ["all"]
39-
build-verbosity = 3
40-
test-requires = "pytest"
41-
test-command = "pytest {package}/tests"
42-
test-skip = "*-win_arm64"
50+
Homepage = "https://github.com/yourusername/python-lzo"
51+
Repository = "https://github.com/yourusername/python-lzo"
52+
Issues = "https://github.com/yourusername/python-lzo/issues"
53+
Documentation = "https://python-lzo.readthedocs.io/"
54+
55+
[tool.setuptools]
56+
packages = [] # No Python packages, just C extension
57+
58+
[tool.setuptools.package-data]
59+
"*" = ["*.so", "*.pyd", "*.dll"]
60+
61+
# Black code formatting
62+
[tool.black]
63+
line-length = 88
64+
target-version = ['py38']
65+
66+
# Pytest configuration
67+
[tool.pytest.ini_options]
68+
testpaths = ["tests"]
69+
python_files = ["test_*.py"]
70+
addopts = "-v"
71+
72+
# MyPy type checking
73+
[tool.mypy]
74+
python_version = "3.8"
75+
warn_return_any = true
76+
warn_unused_configs = true
77+
disallow_untyped_defs = true
78+
79+
# Coverage configuration
80+
[tool.coverage.run]
81+
source = ["lzo"]
82+
omit = ["*/tests/*"]
83+
84+
[tool.coverage.report]
85+
exclude_lines = [
86+
"pragma: no cover",
87+
"def __repr__",
88+
"raise AssertionError",
89+
"raise NotImplementedError",
90+
]

0 commit comments

Comments
 (0)