Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Tencent Ncnn Setup Py

From Leeroopedia
Revision as of 16:50, 16 February 2026 by Admin (talk | contribs) (Auto-imported from implementations/Tencent_Ncnn_Setup_Py.md)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)


Knowledge Sources
Domains Build, Python Bindings
Last Updated 2026-02-09 19:00 GMT

Overview

Concrete tool for building and installing the ncnn Python bindings package using a CMake-driven setuptools build system.

Description

This is the Python package build and installation script for the ncnn Python bindings. It implements a CMake-driven setuptools build through three custom classes: find_version() extracts the version from CMakeLists.txt as MAJOR.MINOR.YYYYMMDD, CMakeExtension defines a source-directory-based extension, and CMakeBuild invokes cmake configure and build with flags for NCNN_PYTHON=ON, NCNN_VULKAN=ON, and disabled benchmarks/examples/tools. The build supports MSVC multi-config generators, automatic parallel build based on CPU count, and extensive configuration via environment variables for Vulkan, toolchain, OpenMP, and iOS settings. Package metadata includes Python 3.6-3.14 classifiers, BSD-3 license, and installs from the python/ directory with numpy, tqdm, requests, portalocker, and opencv-python as dependencies.

Usage

Use this script to install ncnn from source via pip install . or to build wheel packages for PyPI distribution. It is the entry point for making ncnn accessible to the Python ecosystem with a standard setuptools interface while handling the complexity of building a C++ CMake project underneath.

Code Reference

Source Location

Key Classes and Functions

def find_version():
    """Extract version from CMakeLists.txt as MAJOR.MINOR.YYYYMMDD"""

class InstallCommand(install):
    """Custom install command with --vulkan option"""

class CMakeExtension(Extension):
    """Extension using sourcedir instead of file list"""

class CMakeBuild(build_ext):
    """Invokes cmake configure and build for the ncnn native extension"""

Import

from setuptools import setup, find_packages, Extension
from setuptools.command.build_ext import build_ext
from setuptools.command.install import install

I/O Contract

Inputs (Environment Variables)

Name Type Required Description
Vulkan_LIBRARY str No Path to Vulkan library
CMAKE_TOOLCHAIN_FILE str No Path to CMake toolchain file for cross-compilation
PLATFORM str No Target platform (e.g., for iOS builds)
ARCHS str No Target architectures
DEPLOYMENT_TARGET str No Minimum deployment target version
OpenMP_C_FLAGS str No C compiler flags for OpenMP
OpenMP_CXX_FLAGS str No C++ compiler flags for OpenMP
OpenMP_C_LIB_NAMES str No OpenMP C library names
OpenMP_CXX_LIB_NAMES str No OpenMP C++ library names
OpenMP_libomp_LIBRARY str No Path to libomp library
ENABLE_BITCODE str No iOS bitcode setting
ENABLE_ARC str No iOS ARC setting
ENABLE_VISIBILITY str No Symbol visibility setting
EXTRA_CMAKE_ARGS str No Additional CMake arguments (space-separated)
CMAKE_GENERATOR str No CMake generator override (e.g., Ninja, NMake)

Outputs

Name Type Description
ncnn Python package Shared library Compiled ncnn native extension installed into the Python environment

CMake Configuration

The following CMake flags are set by default during the build:

cmake_args = [
    "-DCMAKE_LIBRARY_OUTPUT_DIRECTORY={}".format(extdir),
    "-DPYTHON_EXECUTABLE={}".format(sys.executable),
    "-DCMAKE_BUILD_TYPE={}".format(cfg),
    "-DNCNN_PYTHON=ON",
    "-DNCNN_VULKAN=ON",
    "-DNCNN_DISABLE_RTTI=OFF",
    "-DNCNN_DISABLE_EXCEPTION=OFF",
    "-DNCNN_BUILD_BENCHMARK=OFF",
    "-DNCNN_BUILD_EXAMPLES=OFF",
    "-DNCNN_BUILD_TOOLS=OFF",
]

Dependencies

Runtime Dependencies

Package Purpose
numpy Array operations
tqdm Progress bars
requests HTTP client
portalocker File locking
opencv-python Image processing

Build Dependencies

Package Condition
cmake >= 3.12 Installed if cmake is not found on PATH
ninja Installed on non-Windows if ninja is not found on PATH

Usage Examples

Install from Source

pip install .

Install with Vulkan Support

pip install . --install-option="--vulkan=ON"

Cross-compilation with Toolchain

CMAKE_TOOLCHAIN_FILE=/path/to/toolchain.cmake pip install .

Build with Extra CMake Arguments

EXTRA_CMAKE_ARGS="-DNCNN_VULKAN=OFF" pip install .

Version Scheme

The version is extracted from CMakeLists.txt and formatted as:

{NCNN_VERSION_MAJOR}.{NCNN_VERSION_MINOR}.{YYYYMMDD}

Where YYYYMMDD is the build date, ensuring unique versions for each build.

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment