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 "" )
0 commit comments