1# Copyright (c) Meta Platforms, Inc. and affiliates. 2# All rights reserved. 3# 4# This source code is licensed under the BSD-style license found in the 5# LICENSE file in the root directory of this source tree. 6 7# Config defining how CMake should find ExecuTorch package. CMake will search 8# for this file and find ExecuTorch package if it is installed. Typical usage 9# is: 10# 11# find_package(executorch REQUIRED) 12# ------- 13# 14# Finds the ExecuTorch library 15# 16# This will define the following variables: 17# 18# EXECUTORCH_FOUND -- True if the system has the ExecuTorch library 19# EXECUTORCH_INCLUDE_DIRS -- The include directories for ExecuTorch 20# EXECUTORCH_LIBRARIES -- Libraries to link against 21# 22cmake_minimum_required(VERSION 3.19) 23 24# Find prebuilt _portable_lib.<EXT_SUFFIX>.so. This file should be installed under 25# <site-packages>/executorch/share/cmake 26 27# Find python 28if(DEFINED ENV{CONDA_DEFAULT_ENV} AND NOT $ENV{CONDA_DEFAULT_ENV} STREQUAL "base") 29 set(PYTHON_EXECUTABLE 30 python 31 ) 32else() 33 set(PYTHON_EXECUTABLE 34 python3 35 ) 36endif() 37 38# Get the Python version and platform information 39execute_process( 40 COMMAND ${PYTHON_EXECUTABLE} -c "import sysconfig; print(sysconfig.get_config_var('EXT_SUFFIX'))" 41 OUTPUT_VARIABLE EXT_SUFFIX 42 RESULT_VARIABLE SYSCONFIG_RESULT 43 ERROR_VARIABLE SYSCONFIG_ERROR 44 OUTPUT_STRIP_TRAILING_WHITESPACE 45) 46 47if(SYSCONFIG_RESULT EQUAL 0) 48 message(STATUS "Sysconfig extension suffix: ${EXT_SUFFIX}") 49else() 50 message(FATAL_ERROR "Failed to retrieve sysconfig config var EXT_SUFFIX: ${SYSCONFIG_ERROR}") 51endif() 52 53find_library( 54 _portable_lib_LIBRARY 55 NAMES _portable_lib${EXT_SUFFIX} 56 PATHS "${CMAKE_CURRENT_LIST_DIR}/../../extension/pybindings/" 57) 58 59set(EXECUTORCH_LIBRARIES) 60set(EXECUTORCH_FOUND OFF) 61if(_portable_lib_LIBRARY) 62 set(EXECUTORCH_FOUND ON) 63 message(STATUS "ExecuTorch portable library is found at ${_portable_lib_LIBRARY}") 64 list(APPEND EXECUTORCH_LIBRARIES _portable_lib) 65 add_library(_portable_lib STATIC IMPORTED) 66 set(EXECUTORCH_INCLUDE_DIRS ${CMAKE_CURRENT_LIST_DIR}/../../include) 67 set_target_properties(_portable_lib PROPERTIES 68 IMPORTED_LOCATION "${_portable_lib_LIBRARY}" 69 INTERFACE_INCLUDE_DIRECTORIES "${EXECUTORCH_INCLUDE_DIRS}" 70 CXX_STANDARD 17 71 ) 72endif() 73