File: FindPythonPackage.cmake

package info (click to toggle)
openexr 3.1.13-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 37,616 kB
  • sloc: cpp: 186,653; ansic: 24,266; sh: 173; python: 68; makefile: 23
file content (79 lines) | stat: -rw-r--r-- 2,173 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# SPDX-License-Identifier: BSD-3-Clause
# Copyright Contributors to the OpenColorIO Project.
#
# Locate a Python package
#
# Variables defined by this module:
#   <package>_FOUND
#
# Usage:
#   find_python_package(Jinja2 2.10.1)
#
# If the package is not installed in a standard path, add it to the PYTHONPATH 
# environment variable before running CMake.
#

find_package(Python QUIET COMPONENTS Interpreter)

macro(find_python_package package)
    # Some Python packages have "-" in the PyPi name, but are replaced with "_" 
    # in the actual package name.
    string(REPLACE "-" "_" _PKG_IMPORT ${package})

    # Parse options
    foreach(_ARG ${ARGN})
        if(_ARG STREQUAL "REQUIRED")
            set(_PKG_REQUIRED TRUE)
        endif()
        if(_PREV_ARG STREQUAL "REQUIRES")
            set(_PKG_REQUIRES ${_ARG})
        endif()
        set(_PREV_ARG ${_ARG})
    endforeach()
    
    if(NOT TARGET ${package})
        add_custom_target(${package})
        if(_PKG_REQUIRES)
            add_dependencies(${package} ${_PKG_REQUIRES})
            unset(_PKG_REQUIRES)
        endif()
        set(_${package}_TARGET_CREATE TRUE)
    endif()

    set(_PKG_INSTALL TRUE)

    # Try importing Python package
    execute_process(
        COMMAND
            "${Python_EXECUTABLE}" -c "import ${_PKG_IMPORT}"
        RESULT_VARIABLE
            _PKG_IMPORT_RESULT
        OUTPUT_QUIET ERROR_QUIET
    )

    if(_PKG_IMPORT_RESULT EQUAL 0)
        set(${package}_FOUND TRUE)

        # Get the package's location
        execute_process(
            COMMAND
                "${Python_EXECUTABLE}" -c 
                "import ${_PKG_IMPORT}, os; print(os.path.dirname(${_PKG_IMPORT}.__file__))"
            OUTPUT_VARIABLE
                _PKG_DIR
            ERROR_QUIET
        )
        string(STRIP "${_PKG_DIR}" _PKG_DIR)
        message(STATUS "Found ${package}: ${_PKG_DIR}")

    else()
        set(${package}_FOUND FALSE)
        set(_FIND_ERR "Could NOT find ${package} (import ${_PKG_IMPORT} failed)")

        if(_PKG_REQUIRED)
            message(FATAL_ERROR "${_FIND_ERR}")
        endif()
        message(STATUS "${_FIND_ERR}")
    endif()

endmacro()