From 1b580dbe5b4fd79843c3da91fc3911f75305808c Mon Sep 17 00:00:00 2001 From: Carlos Zoido Date: Wed, 18 Mar 2026 18:15:20 +0100 Subject: [PATCH 1/2] use conan-py-build for building --- CMakeLists.txt | 187 ------------------------------------------------- conanfile.py | 38 ++++++++++ pyproject.toml | 17 ++--- 3 files changed, 44 insertions(+), 198 deletions(-) delete mode 100644 CMakeLists.txt create mode 100644 conanfile.py diff --git a/CMakeLists.txt b/CMakeLists.txt deleted file mode 100644 index 4b06d98b32..0000000000 --- a/CMakeLists.txt +++ /dev/null @@ -1,187 +0,0 @@ -cmake_minimum_required(VERSION 3.21) - -project(llama_cpp) - -option(LLAMA_BUILD "Build llama.cpp shared library and install alongside python package" ON) -option(LLAVA_BUILD "Build llava shared library and install alongside python package" ON) - -function(llama_cpp_python_install_target target) - if(NOT TARGET ${target}) - return() - endif() - - install( - TARGETS ${target} - LIBRARY DESTINATION ${CMAKE_CURRENT_SOURCE_DIR}/llama_cpp/lib - RUNTIME DESTINATION ${CMAKE_CURRENT_SOURCE_DIR}/llama_cpp/lib - ARCHIVE DESTINATION ${CMAKE_CURRENT_SOURCE_DIR}/llama_cpp/lib - FRAMEWORK DESTINATION ${CMAKE_CURRENT_SOURCE_DIR}/llama_cpp/lib - RESOURCE DESTINATION ${CMAKE_CURRENT_SOURCE_DIR}/llama_cpp/lib - ) - install( - TARGETS ${target} - LIBRARY DESTINATION ${SKBUILD_PLATLIB_DIR}/llama_cpp/lib - RUNTIME DESTINATION ${SKBUILD_PLATLIB_DIR}/llama_cpp/lib - ARCHIVE DESTINATION ${SKBUILD_PLATLIB_DIR}/llama_cpp/lib - FRAMEWORK DESTINATION ${SKBUILD_PLATLIB_DIR}/llama_cpp/lib - RESOURCE DESTINATION ${SKBUILD_PLATLIB_DIR}/llama_cpp/lib - ) - set_target_properties(${target} PROPERTIES - INSTALL_RPATH "$ORIGIN" - BUILD_WITH_INSTALL_RPATH TRUE - ) - if(UNIX) - if(APPLE) - set_target_properties(${target} PROPERTIES - INSTALL_RPATH "@loader_path" - BUILD_WITH_INSTALL_RPATH TRUE - ) - else() - set_target_properties(${target} PROPERTIES - INSTALL_RPATH "$ORIGIN" - BUILD_WITH_INSTALL_RPATH TRUE - ) - endif() - endif() -endfunction() - -if (LLAMA_BUILD) - set(BUILD_SHARED_LIBS "On") - - set(CMAKE_SKIP_BUILD_RPATH FALSE) - - # When building, don't use the install RPATH already - # (but later on when installing) - set(CMAKE_BUILD_WITH_INSTALL_RPATH FALSE) - - # Add the automatically determined parts of the RPATH - # which point to directories outside the build tree to the install RPATH - set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE) - set(CMAKE_SKIP_RPATH FALSE) - - # Enable building of the common library - set(LLAMA_BUILD_COMMON ON CACHE BOOL "Build llama.cpp common library" FORCE) - - # Disable building curl support - set(LLAMA_CURL OFF CACHE BOOL "llama.cpp: enable curl" FORCE) - - # Architecture detection and settings for Apple platforms - if (APPLE) - # Get the target architecture - execute_process( - COMMAND uname -m - OUTPUT_VARIABLE HOST_ARCH - OUTPUT_STRIP_TRAILING_WHITESPACE - ) - - # If CMAKE_OSX_ARCHITECTURES is not set, use the host architecture - if(NOT CMAKE_OSX_ARCHITECTURES) - set(CMAKE_OSX_ARCHITECTURES ${HOST_ARCH} CACHE STRING "Build architecture for macOS" FORCE) - endif() - - message(STATUS "Host architecture: ${HOST_ARCH}") - message(STATUS "Target architecture: ${CMAKE_OSX_ARCHITECTURES}") - - # Configure based on target architecture - if(CMAKE_OSX_ARCHITECTURES STREQUAL "x86_64") - # Intel Mac settings - set(GGML_AVX "OFF" CACHE BOOL "ggml: enable AVX" FORCE) - set(GGML_AVX2 "OFF" CACHE BOOL "ggml: enable AVX2" FORCE) - set(GGML_FMA "OFF" CACHE BOOL "ggml: enable FMA" FORCE) - set(GGML_F16C "OFF" CACHE BOOL "ggml: enable F16C" FORCE) - endif() - - # Metal settings (enable for both architectures) - set(GGML_METAL "ON" CACHE BOOL "ggml: enable Metal" FORCE) - set(GGML_METAL_EMBED_LIBRARY "ON" CACHE BOOL "ggml: embed metal library" FORCE) - endif() - - - add_subdirectory(vendor/llama.cpp) - - if (WIN32) - if (TARGET llama) - set_target_properties(llama PROPERTIES WINDOWS_EXPORT_ALL_SYMBOLS ON) - endif() - endif() - - llama_cpp_python_install_target(llama) - llama_cpp_python_install_target(ggml) - - llama_cpp_python_install_target(ggml-base) - - llama_cpp_python_install_target(ggml-amx) - llama_cpp_python_install_target(ggml-blas) - llama_cpp_python_install_target(ggml-can) - llama_cpp_python_install_target(ggml-cpu) - llama_cpp_python_install_target(ggml-cuda) - llama_cpp_python_install_target(ggml-hip) - llama_cpp_python_install_target(ggml-kompute) - llama_cpp_python_install_target(ggml-metal) - llama_cpp_python_install_target(ggml-musa) - llama_cpp_python_install_target(ggml-rpc) - llama_cpp_python_install_target(ggml-sycl) - llama_cpp_python_install_target(ggml-vulkan) - - # Workaround for Windows + CUDA https://github.com/abetlen/llama-cpp-python/issues/563 - if (WIN32) - install( - FILES $ - DESTINATION ${CMAKE_CURRENT_SOURCE_DIR}/llama_cpp/lib - ) - install( - FILES $ - DESTINATION ${SKBUILD_PLATLIB_DIR}/llama_cpp/lib - ) - install( - FILES $ - DESTINATION ${CMAKE_CURRENT_SOURCE_DIR}/llama_cpp/lib - ) - install( - FILES $ - DESTINATION ${SKBUILD_PLATLIB_DIR}/llama_cpp/lib - ) - endif() - - if (LLAVA_BUILD) - if (LLAMA_CUBLAS OR LLAMA_CUDA) - add_compile_definitions(GGML_USE_CUBLAS) - add_compile_definitions(GGML_USE_CUDA) - endif() - - if (LLAMA_METAL) - add_compile_definitions(GGML_USE_METAL) - endif() - - # Building llava - add_subdirectory(vendor/llama.cpp/tools/mtmd) - - if (WIN32) - set_target_properties(mtmd PROPERTIES CUDA_ARCHITECTURES OFF) - endif() - llama_cpp_python_install_target(mtmd) - if (WIN32) - install( - FILES $ - DESTINATION ${CMAKE_CURRENT_SOURCE_DIR}/llama_cpp/lib - ) - install( - FILES $ - DESTINATION ${SKBUILD_PLATLIB_DIR}/llama_cpp/lib - ) - endif() - - # Fix for mtmd build: Add include directory for llama.h - # Move these commands after the add_subdirectory call - target_include_directories(mtmd PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/vendor/llama.cpp/include) - target_include_directories(mtmd PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/vendor/llama.cpp/ggml/include) - - if (BUILD_SHARED_LIBS) - target_include_directories(mtmd PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/vendor/llama.cpp/include) - target_include_directories(mtmd PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/vendor/llama.cpp/ggml/include) - endif() - - # target_include_directories(llama-llava-cli PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/vendor/llama.cpp/include) - # target_include_directories(llama-minicpmv-cli PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/vendor/llama.cpp/include) - endif() -endif() diff --git a/conanfile.py b/conanfile.py new file mode 100644 index 0000000000..5708e46696 --- /dev/null +++ b/conanfile.py @@ -0,0 +1,38 @@ +from pathlib import Path + +from conan import ConanFile +from conan.tools.files import copy + + +class LlamaCppPythonConan(ConanFile): + name = "llama_cpp_python" + version = "0.3.16" + settings = "os", "compiler", "build_type", "arch" + default_options = {"llama-cpp/*:shared": True} + + def requirements(self): + self.requires("llama-cpp/b6153") + + def layout(self): + self.folders.source = "." + self.folders.build = "build" + + def package(self): + dep = self.dependencies["llama-cpp"] + src = Path(dep.package_folder) + pkg = Path(self.package_folder) / "llama_cpp" + src_py = Path(self.source_folder) / "llama_cpp" + + # Python + copy(self, "*.py", src=src_py, dst=pkg) + copy(self, "py.typed", src=src_py, dst=pkg) + if (src_py / "server").exists(): + copy(self, "*.py", src=src_py / "server", dst=pkg / "server") + + # Shared libs + lib_dir = pkg / "lib" + lib_dir.mkdir(parents=True, exist_ok=True) + for pattern in ("*.so", "*.so.*", "*.dylib", "*.dll"): + copy(self, pattern, src=src / "lib", dst=lib_dir) + if self.settings.os == "Windows": + copy(self, "*.dll", src=src / "bin", dst=lib_dir) diff --git a/pyproject.toml b/pyproject.toml index f5ae7b59c7..25bd84c062 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [build-system] -requires = ["scikit-build-core[pyproject]>=0.9.2"] -build-backend = "scikit_build_core.build" +requires = ["conan-py-build"] +build-backend = "conan_py_build.build" [project] name = "llama_cpp_python" @@ -61,16 +61,11 @@ all = [ "llama_cpp_python[server,test,dev]", ] -[tool.scikit-build] -wheel.packages = ["llama_cpp"] -cmake.verbose = true -cmake.minimum-version = "3.21" -minimum-version = "0.5.1" -sdist.include = [".git", "vendor/llama.cpp/*"] +[tool.conan-py-build] +version-file = "llama_cpp/__init__.py" -[tool.scikit-build.metadata.version] -provider = "scikit_build_core.metadata.regex" -input = "llama_cpp/__init__.py" +[tool.conan-py-build.wheel] +packages = ["llama_cpp"] [project.urls] Homepage = "https://github.com/abetlen/llama-cpp-python" From 715590cddb9e908108d85f2a6dde9ad53184d86f Mon Sep 17 00:00:00 2001 From: Carlos Zoido Date: Fri, 20 Mar 2026 07:38:06 +0100 Subject: [PATCH 2/2] update --- pyproject.toml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 25bd84c062..cec6b9db74 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -61,8 +61,8 @@ all = [ "llama_cpp_python[server,test,dev]", ] -[tool.conan-py-build] -version-file = "llama_cpp/__init__.py" +[tool.conan-py-build.version] +file = "llama_cpp/__init__.py" [tool.conan-py-build.wheel] packages = ["llama_cpp"]