|
| 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.14) |
| 19 | + |
| 20 | +project( |
| 21 | + tvm_ffi |
| 22 | + VERSION 1.0 |
| 23 | + DESCRIPTION "TVM's FFI system" |
| 24 | + LANGUAGES CXX C |
| 25 | +) |
| 26 | + |
| 27 | +option(TVM_FFI_BUILD_TESTS "Adding test targets." OFF) |
| 28 | + |
| 29 | +########## NOTE: all options below are related to dynamic registry ##### |
| 30 | +option(TVM_FFI_BUILD_REGISTRY |
| 31 | + "Support for objects with non-static type indices. When turned on, \ |
| 32 | + targets linked against `tvm_ffi` will allow objects that comes with non-pre-defined type indices, \ |
| 33 | + as well as getting full stacktrace during debugging. \ |
| 34 | + so that the object hierarchy could expand without limitation. \ |
| 35 | + This will require the downstream targets to link against target `tvm_ffi` to be effective." |
| 36 | + OFF |
| 37 | +) |
| 38 | +option(TVM_FFI_USE_LIBBACKTRACE "Enable libbacktrace" ON) |
| 39 | +option(TVM_FFI_BACKTRACE_ON_SEGFAULT "Set signal handler to print traceback on segfault" ON) |
| 40 | + |
| 41 | +include(cmake/Utils/CxxWarning.cmake) |
| 42 | +include(cmake/Utils/Sanitizer.cmake) |
| 43 | +include(cmake/Utils/Library.cmake) |
| 44 | +if (TVM_FFI_USE_LIBBACKTRACE) |
| 45 | + include(cmake/Utils/AddLibbacktrace.cmake) |
| 46 | +endif() |
| 47 | + |
| 48 | +########## Target: `dlpack_header` ########## |
| 49 | + |
| 50 | +add_library(dlpack_header INTERFACE) |
| 51 | +target_include_directories(dlpack_header INTERFACE "${CMAKE_CURRENT_SOURCE_DIR}/3rdparty/dlpack/include") |
| 52 | + |
| 53 | +########## Target: `tvm_ffi_header` ########## |
| 54 | + |
| 55 | +add_library(tvm_ffi_header INTERFACE) |
| 56 | +target_include_directories(tvm_ffi_header INTERFACE "${CMAKE_CURRENT_SOURCE_DIR}/include") |
| 57 | +target_link_libraries(tvm_ffi_header INTERFACE dlpack_header) |
| 58 | + |
| 59 | +########## Target: `tvm_ffi` ########## |
| 60 | +add_library(tvm_ffi_objs OBJECT |
| 61 | + "${CMAKE_CURRENT_SOURCE_DIR}/src/ffi/traceback.cc" |
| 62 | + "${CMAKE_CURRENT_SOURCE_DIR}/src/ffi/traceback_win.cc" |
| 63 | + "${CMAKE_CURRENT_SOURCE_DIR}/src/ffi/object.cc" |
| 64 | + "${CMAKE_CURRENT_SOURCE_DIR}/src/ffi/error.cc" |
| 65 | + "${CMAKE_CURRENT_SOURCE_DIR}/src/ffi/function.cc" |
| 66 | + "${CMAKE_CURRENT_SOURCE_DIR}/src/ffi/ndarray.cc" |
| 67 | + "${CMAKE_CURRENT_SOURCE_DIR}/src/ffi/dtype.cc" |
| 68 | + "${CMAKE_CURRENT_SOURCE_DIR}/src/ffi/testing.cc" |
| 69 | + "${CMAKE_CURRENT_SOURCE_DIR}/src/ffi/container.cc" |
| 70 | +) |
| 71 | +set_target_properties( |
| 72 | + tvm_ffi_objs PROPERTIES |
| 73 | + POSITION_INDEPENDENT_CODE ON |
| 74 | + CXX_STANDARD 17 |
| 75 | + CXX_EXTENSIONS OFF |
| 76 | + CXX_STANDARD_REQUIRED ON |
| 77 | + CXX_VISIBILITY_PRESET hidden |
| 78 | + VISIBILITY_INLINES_HIDDEN ON |
| 79 | + PREFIX "lib" |
| 80 | +) |
| 81 | +add_cxx_warning(tvm_ffi_objs) |
| 82 | +target_link_libraries(tvm_ffi_objs PRIVATE dlpack_header) |
| 83 | +target_include_directories(tvm_ffi_objs PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/include") |
| 84 | + |
| 85 | +if (TVM_FFI_USE_LIBBACKTRACE) |
| 86 | + message(STATUS "Setting C++ macro TVM_FFI_USE_LIBBACKTRACE - 1") |
| 87 | + target_compile_definitions(tvm_ffi_objs PRIVATE TVM_FFI_USE_LIBBACKTRACE=1) |
| 88 | +else() |
| 89 | + message(STATUS "Setting C++ macro TVM_FFI_USE_LIBBACKTRACE - 0") |
| 90 | + target_compile_definitions(tvm_ffi_objs PRIVATE TVM_FFI_USE_LIBBACKTRACE=0) |
| 91 | +endif() |
| 92 | + |
| 93 | +if (TVM_FFI_BACKTRACE_ON_SEGFAULT) |
| 94 | + message(STATUS "Setting C++ macro TVM_FFI_BACKTRACE_ON_SEGFAULT - 1") |
| 95 | + target_compile_definitions(tvm_ffi_objs PRIVATE TVM_FFI_BACKTRACE_ON_SEGFAULT=1) |
| 96 | +else() |
| 97 | + message(STATUS "Setting C++ macro TVM_FFI_BACKTRACE_ON_SEGFAULT - 0") |
| 98 | + target_compile_definitions(tvm_ffi_objs PRIVATE TVM_FFI_BACKTRACE_ON_SEGFAULT=0) |
| 99 | +endif() |
| 100 | + |
| 101 | +add_target_from_obj(tvm_ffi tvm_ffi_objs) |
| 102 | + |
| 103 | +if (TARGET libbacktrace) |
| 104 | + target_link_libraries(tvm_ffi_objs PRIVATE libbacktrace) |
| 105 | + target_link_libraries(tvm_ffi_shared PRIVATE libbacktrace) |
| 106 | + target_link_libraries(tvm_ffi_static PRIVATE libbacktrace) |
| 107 | +endif () |
| 108 | + |
| 109 | +if (MSVC) |
| 110 | + target_link_libraries(tvm_ffi_objs PRIVATE DbgHelp.lib) |
| 111 | + target_link_libraries(tvm_ffi_shared PRIVATE DbgHelp.lib) |
| 112 | + target_link_libraries(tvm_ffi_static PRIVATE DbgHelp.lib) |
| 113 | +endif () |
| 114 | + |
| 115 | +target_link_libraries(tvm_ffi_objs PUBLIC tvm_ffi_header) |
| 116 | +target_link_libraries(tvm_ffi_shared PUBLIC tvm_ffi_header) |
| 117 | +target_link_libraries(tvm_ffi_static PUBLIC tvm_ffi_header) |
| 118 | + |
| 119 | +install(TARGETS tvm_ffi_static DESTINATION lib${LIB_SUFFIX}) |
| 120 | +install(TARGETS tvm_ffi_shared DESTINATION lib${LIB_SUFFIX}) |
| 121 | + |
| 122 | +add_msvc_flags(tvm_ffi_objs) |
| 123 | + |
| 124 | +########## Adding tests ########## |
| 125 | + |
| 126 | +if (${PROJECT_NAME} STREQUAL ${CMAKE_PROJECT_NAME}) |
| 127 | + if (TVM_FFI_BUILD_TESTS) |
| 128 | + enable_testing() |
| 129 | + message(STATUS "Enable Testing") |
| 130 | + include(cmake/Utils/AddGoogleTest.cmake) |
| 131 | + add_subdirectory(tests/cpp/) |
| 132 | + endif() |
| 133 | +endif () |
0 commit comments