|
| 1 | +# Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +# or more contributor license agreements. See the NOTICE file |
| 3 | +# distributed with this work for additional information |
| 4 | +# regarding copyright ownership. The ASF licenses this file |
| 5 | +# to you under the Apache License, Version 2.0 (the |
| 6 | +# "License"); you may not use this file except in compliance |
| 7 | +# with the License. You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, |
| 12 | +# software distributed under the License is distributed on an |
| 13 | +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +# KIND, either express or implied. See the License for the |
| 15 | +# specific language governing permissions and limitations |
| 16 | +# under the License. |
| 17 | + |
| 18 | +cmake_minimum_required(VERSION 3.18) |
| 19 | +project(tvm_ffi_extension) |
| 20 | + |
| 21 | +option(TVM_FFI_EXT_FROM_SOURCE "Build tvm_ffi from source, useful for cross compilation." ON) |
| 22 | +option(TVM_FFI_EXT_SHIP_DEBUG_SYMBOLS "Ship debug symbols" ON) |
| 23 | + |
| 24 | +# There are two ways to include tvm_ffi |
| 25 | +# |
| 26 | +# 1. Build tvm_ffi from source, which is reasonably cheap since tvm ffi is small |
| 27 | +# 2. Use the pre-built tvm_ffi shipped from the pip |
| 28 | +# |
| 29 | +# This example shows both options, you only need to pick a specific one. |
| 30 | +# |
| 31 | +# - For common build cases, using pre-built and link tvm_ffi_shared is sufficient. |
| 32 | +# - For cases where you may want to cross-compile or bundle part of tvm_ffi_objects directly |
| 33 | +# into your project, opt for building tvm_ffi from source path. |
| 34 | +# Note that it is always safe to build from source and extra cost of building tvm_ffi is small. |
| 35 | +# So when in doubt, you can always choose to the building tvm_ffi from source route. |
| 36 | +# |
| 37 | +# In python or other cases when we dynamically load libtvm_ffi_shared. Even when you build |
| 38 | +# from source, you do not need to ship libtvm_ffi_shared.so built here as they are only |
| 39 | +# used to supply the linking information. |
| 40 | +# first find python related components |
| 41 | +find_package(Python COMPONENTS Interpreter REQUIRED) |
| 42 | +if (TVM_FFI_BUILD_FROM_SOURCE) |
| 43 | + execute_process( |
| 44 | + COMMAND "${Python_EXECUTABLE}" -m tvm_ffi.config --sourcedir |
| 45 | + OUTPUT_STRIP_TRAILING_WHITESPACE OUTPUT_VARIABLE tvm_ffi_ROOT) |
| 46 | + message(STATUS "Building tvm_ffi from source: ${tvm_ffi_ROOT}") |
| 47 | + add_subdirectory(${tvm_ffi_ROOT} tvm_ffi) |
| 48 | +else() |
| 49 | + # call tvm_ffi.config to get the cmake directory and set it to tvm_ffi_ROOT |
| 50 | + execute_process( |
| 51 | + COMMAND "${Python_EXECUTABLE}" -m tvm_ffi.config --cmakedir |
| 52 | + OUTPUT_STRIP_TRAILING_WHITESPACE OUTPUT_VARIABLE tvm_ffi_ROOT) |
| 53 | + find_package(tvm_ffi CONFIG REQUIRED) |
| 54 | +endif() |
| 55 | + |
| 56 | +# use the projects as usual |
| 57 | +add_library(tvm_ffi_extension SHARED src/extension.cc) |
| 58 | +target_link_libraries(tvm_ffi_extension tvm_ffi_header) |
| 59 | +target_link_libraries(tvm_ffi_extension tvm_ffi_shared) |
| 60 | + |
| 61 | +# show as tvm_ffi_extension.so |
| 62 | +set_target_properties( |
| 63 | + tvm_ffi_extension PROPERTIES PREFIX "" |
| 64 | +) |
| 65 | + |
| 66 | +if (TVM_FFI_EXT_SHIP_DEBUG_SYMBOLS) |
| 67 | + # ship debugging symbols for backtrace on macos |
| 68 | + tvm_ffi_add_prefix_map(tvm_ffi_extension ${CMAKE_CURRENT_SOURCE_DIR}) |
| 69 | + tvm_ffi_add_apple_dsymutil(tvm_ffi_extension) |
| 70 | + install(DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/ DESTINATION . FILES_MATCHING PATTERN "*.dSYM") |
| 71 | +endif() |
| 72 | + |
| 73 | +install(TARGETS tvm_ffi_extension DESTINATION .) |
0 commit comments