CMakeLists.txt 11.3 KB
Newer Older
1 2
# force define a SHARED target for whl, caused by when build for APPLE we will force set
# BUILD_SHARED_LIBS=OFF for xcode needed
3
set(MGE_SHARED_LIB megengine_shared)
4 5 6
set(MGE_SHARED_LIB
    ${MGE_SHARED_LIB}
    PARENT_SCOPE)
7

M
Megvii Engine Team 已提交
8
if(MGE_WITH_JIT_MLIR)
9
  add_subdirectory(jit/include/megbrain/jit/mlir/ir)
M
Megvii Engine Team 已提交
10 11
endif()

12 13 14 15 16 17 18 19 20
file(
  GLOB_RECURSE
  SOURCES
  core/impl/*.cpp
  gopt/impl/*.cpp
  opr/impl/*.cpp
  opr/impl/nvof/*.cpp
  plugin/impl/*.cpp
  serialization/impl/*.cpp
21
  rdnn/impl/*.cpp
22 23 24 25 26
  core/impl/*.inl
  gopt/impl/*.inl
  opr/impl/*.inl
  plugin/impl/*.inl
  serialization/impl/*.inl)
27 28

if(MGE_WITH_JIT)
29 30 31 32 33 34
  file(GLOB_RECURSE SOURCES_ jit/impl/*.cpp jit/impl/*.inl)
  if(MGE_WITH_JIT_MLIR)
    file(GLOB_RECURSE MLIR_SOURCES_ jit/impl/mlir/ir/*.cpp jit/impl/mlir/*.cpp)
    list(APPEND SOURCES_ ${MLIR_SOURCES_})
  endif()
  list(APPEND SOURCES ${SOURCES_})
35 36 37
endif()

if(MGE_WITH_DISTRIBUTED)
38 39 40 41 42 43 44 45 46 47 48
  file(GLOB_RECURSE SOURCES_ opr-mm/impl/*.cpp opr-mm/impl/*.inl)
  list(APPEND SOURCES ${SOURCES_})
  file(
    GLOB_RECURSE PROTO_FILES
    RELATIVE ${CMAKE_CURRENT_SOURCE_DIR}
    "../src/opr-mm/proto/*.proto")
  protobuf_generate_cpp_with_root(GRPC_SRCS GRPC_HDRS ${CMAKE_CURRENT_SOURCE_DIR}
                                  ${PROTO_FILES})
  add_custom_target(mgb_proto_target DEPENDS ${GRPC_SRCS} ${GRPC_HDRS}
                                             ${PROTOBUF_PROTOC_EXECUTABLE})
  list(APPEND SOURCES ${GRPC_SRCS})
49 50
endif()

51 52 53 54 55 56
set(MGB_INC
    ${PROJECT_BINARY_DIR}/genfiles
    ${CMAKE_CURRENT_LIST_DIR}/core/include
    ${CMAKE_CURRENT_LIST_DIR}/gopt/include
    ${CMAKE_CURRENT_LIST_DIR}/opr/include
    ${CMAKE_CURRENT_LIST_DIR}/plugin/include
57 58
    ${CMAKE_CURRENT_LIST_DIR}/serialization/include
    ${CMAKE_CURRENT_LIST_DIR}/rdnn/include)
59 60

if(MGE_WITH_JIT)
61 62 63 64
  list(APPEND MGB_INC ${CMAKE_CURRENT_LIST_DIR}/jit/include)
  if(MGE_WITH_CUDA)
    list(APPEND MGB_INC ${CMAKE_CURRENT_LIST_DIR}/jit/impl/cuda)
  endif()
65 66 67
endif()

if(MGE_WITH_DISTRIBUTED)
68
  list(APPEND MGB_INC ${CMAKE_CURRENT_LIST_DIR}/opr-mm/include)
69 70 71
endif()

if(MGE_WITH_CUDA AND MGE_WITH_TRT)
72 73 74
  list(APPEND MGB_INC ${CMAKE_CURRENT_LIST_DIR}/tensorrt/include)
  file(GLOB_RECURSE SOURCES_ tensorrt/impl/*.cpp tensorrt/impl/*.inl)
  list(APPEND SOURCES ${SOURCES_})
75
endif()
76
if(MGE_WITH_CAMBRICON)
77 78 79
  list(APPEND MGB_INC ${CMAKE_CURRENT_LIST_DIR}/cambricon/include)
  file(GLOB_RECURSE SOURCES_ cambricon/impl/*.cpp cambricon/impl/*.inl)
  list(APPEND SOURCES ${SOURCES_})
80 81 82 83
endif()
set(MGB_CAMBRICON ${MGE_WITH_CAMBRICON})

set(MGB_ATLAS ${MGE_WITH_ATLAS})
84

85
if(MGE_WITH_CUDA)
86 87
  file(GLOB_RECURSE SOURCES_ opr/impl/standalone/*.cu)
  list(APPEND SOURCES ${SOURCES_})
88 89
endif()

90
if(MGE_WITH_CUSTOM_OP)
91 92
  list(APPEND MGB_INC ${CMAKE_CURRENT_LIST_DIR}/custom/include)
  file(GLOB_RECURSE SOURCES_ custom/impl/*.cpp)
93 94 95 96 97 98 99 100 101

  set(EXCLUDE_PLATFORM_DIR "custom/impl/platform")
  foreach(CUSOURCE ${SOURCES_})
    string(FIND ${CUSOURCE} ${EXCLUDE_PLATFORM_DIR} EXCLUDE_DIR_FOUND)
    if(NOT ${EXCLUDE_DIR_FOUND} EQUAL -1)
      list(REMOVE_ITEM SOURCES_ ${CUSOURCE})
    endif()
  endforeach(CUSOURCE)

102
  list(APPEND SOURCES ${SOURCES_})
103 104
endif()

M
Megvii Engine Team 已提交
105
add_library(megbrain OBJECT ${SOURCES})
106
target_link_libraries(megbrain PUBLIC mgb_opr_param_defs)
107
if(MGE_WITH_CUDA)
108 109 110 111 112 113 114 115 116 117 118 119
  target_include_directories(megbrain PUBLIC ${TRT_INCLUDE_DIR})
  target_include_directories(megbrain PRIVATE ${CUDNN_INCLUDE_DIR})
  find_path(
    NVTX3_INCLUDE
    NAMES nvToolsExtCudaRt.h
    HINTS $ENV{CUDA_ROOT_DIR} $ENV{CUDA_PATH} $ENV{CUDA_BIN_PATH}
    PATH_SUFFIXES include/nvtx3
    DOC "NVTX3_INCLUDE")
  if(NVTX3_INCLUDE STREQUAL "NVTX3_INCLUDE-NOTFOUND")
    message(
      FATAL_ERROR
        "Can not find NVTX3 INCLUDE, please export cuda sdk path to CUDA_ROOT_DIR or CUDA_PATH or CUDA_BIN_PATH"
120
    )
121 122 123 124 125 126 127 128 129 130
  endif()
  target_include_directories(megbrain PRIVATE ${NVTX3_INCLUDE})
endif()
target_include_directories(
  megbrain
  PUBLIC $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
  PRIVATE ${PROJECT_SOURCE_DIR}/third_party/midout/src)

foreach(INCPATH IN LISTS MGB_INC)
  target_include_directories(megbrain PUBLIC $<BUILD_INTERFACE:${INCPATH}>)
131
endforeach()
132 133

if(MGE_WITH_CUDA)
134 135 136 137 138
  if(NOT WIN32 AND NOT MSVC)
    target_compile_options(
      megbrain PRIVATE "$<$<COMPILE_LANGUAGE:CUDA>:-Xcompiler=-Wno-unused-parameter>"
                       "$<$<NOT:$<COMPILE_LANGUAGE:CUDA>>:-Wno-unused-parameter>")
  endif()
139
else()
140
  target_compile_options(megbrain PRIVATE "-Wno-unused-parameter")
141 142
endif()
if(CXX_SUPPORT_WCLASS_MEMACCESS)
143 144 145 146 147 148 149
  if(MGE_WITH_CUDA)
    target_compile_options(
      megbrain PRIVATE "$<$<COMPILE_LANGUAGE:CUDA>:-Xcompiler=-Wno-class-memaccess>"
                       "$<$<NOT:$<COMPILE_LANGUAGE:CUDA>>:-Wno-class-memaccess>")
  else()
    target_compile_options(megbrain PRIVATE "-Wno-class-memaccess")
  endif()
150
endif()
151
target_link_libraries(megbrain PUBLIC megdnn)
152
if(MGE_WITH_DISTRIBUTED)
153 154 155 156 157 158
  add_dependencies(megbrain mgb_proto_target)
  target_link_libraries(megbrain PRIVATE libprotobuf libzmq)
  set(CPPZMQ_INC ${PROJECT_SOURCE_DIR}/third_party/cppzmq)
  # FIXME: add CMAKE_CURRENT_BINARY_DIR for including mm_handler.pb.h
  target_include_directories(megbrain PRIVATE ${CPPZMQ_INC} ${CMAKE_CURRENT_BINARY_DIR})
  target_link_libraries(megbrain PRIVATE megray)
159
endif()
160 161
target_link_libraries(megbrain PUBLIC ${MGE_CAMBRICON_LIBS})
target_link_libraries(megbrain PUBLIC ${MGE_ATLAS_LIBS})
162
if(MGE_WITH_JIT AND MGE_WITH_HALIDE)
163 164
  target_link_libraries(megbrain PRIVATE libhalide)
  target_link_libraries(megbrain PRIVATE ${HALIDE_LLVM_LIBS})
165
endif()
M
Megvii Engine Team 已提交
166
if(MGE_WITH_JIT_MLIR)
167 168 169 170 171 172 173 174 175 176 177 178 179 180
  target_include_directories(megbrain PRIVATE ${MLIR_LLVM_INCLUDE_DIR})
  target_link_libraries(megbrain PRIVATE ${MLIR_LLVM_LIBS})
  add_dependencies(megbrain mgb_dialect)
  target_include_directories(megbrain PRIVATE ${CMAKE_CURRENT_BINARY_DIR}/jit/include)
endif()
if(MGB_WITH_FLATBUFFERS)
  set(GEN_FLATBUFFERS_SCHEMA_PY
      ${PROJECT_SOURCE_DIR}/dnn/scripts/gen_flatbuffers_schema.py)
  set(OPR_PARAM_DEFS_PY ${PROJECT_SOURCE_DIR}/dnn/scripts/opr_param_defs.py)
  set(MGB_PARAM_DEFS_PY ${PROJECT_SOURCE_DIR}/tools/param_defs/mgb_opr_param_defs.py)
  file(MAKE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/serialization/impl)
  add_custom_command(
    OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/serialization/impl/opr_param_defs.fbs
    COMMAND ${PYTHON_EXECUTABLE} ${GEN_FLATBUFFERS_SCHEMA_PY} ${OPR_PARAM_DEFS_PY}
181
            ${CMAKE_CURRENT_BINARY_DIR}/serialization/impl/opr_param_defs.fbs
182 183 184 185 186
    DEPENDS ${GEN_FLATBUFFERS_SCHEMA_PY} ${OPR_PARAM_DEFS_PY}
    VERBATIM)
  add_custom_command(
    OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/serialization/impl/mgb_opr_param_defs.fbs
    COMMAND ${PYTHON_EXECUTABLE} ${GEN_FLATBUFFERS_SCHEMA_PY} ${MGB_PARAM_DEFS_PY}
187
            ${CMAKE_CURRENT_BINARY_DIR}/serialization/impl/mgb_opr_param_defs.fbs
188 189 190 191 192 193 194 195 196
    DEPENDS ${GEN_FLATBUFFERS_SCHEMA_PY} ${MGB_PARAM_DEFS_PY}
    VERBATIM)
  list(
    APPEND
    FLATBUFFERS_SCHEMA_FILES
    ${CMAKE_CURRENT_SOURCE_DIR}/serialization/impl/dtype.fbs
    ${CMAKE_CURRENT_BINARY_DIR}/serialization/impl/opr_param_defs.fbs
    ${CMAKE_CURRENT_BINARY_DIR}/serialization/impl/mgb_opr_param_defs.fbs
    ${CMAKE_CURRENT_SOURCE_DIR}/opr/impl/mgb_cpp_opr.fbs
197 198
    ${CMAKE_CURRENT_SOURCE_DIR}/serialization/impl/schema.fbs
    ${CMAKE_CURRENT_SOURCE_DIR}/serialization/impl/schema_v2.fbs)
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
  list(
    APPEND FLATBUFFERS_SCHEMA_INCLUDE_DIR
    ${CMAKE_CURRENT_SOURCE_DIR}/serialization/impl
    ${CMAKE_CURRENT_BINARY_DIR}/serialization/impl ${CMAKE_CURRENT_SOURCE_DIR}/opr/impl)
  build_flatbuffers(
    "${FLATBUFFERS_SCHEMA_FILES}"
    "${FLATBUFFERS_SCHEMA_INCLUDE_DIR}"
    mgb_serialization_schema_fbs
    "${FLATBUFFERS_SCHEMA_FILES}"
    "${CMAKE_CURRENT_BINARY_DIR}/serialization/include/megbrain/serialization/internal"
    ""
    "")
  add_dependencies(megbrain mgb_serialization_schema_fbs)
  target_include_directories(megbrain
                             PRIVATE ${CMAKE_CURRENT_BINARY_DIR}/serialization/include)
  target_compile_definitions(megbrain PUBLIC MGB_ENABLE_FBS_SERIALIZATION=1)
  target_link_libraries(megbrain PUBLIC flatbuffers)
  set(GENERATED_FLATBUFFERS_CONVERTER_PATH ${CMAKE_CURRENT_BINARY_DIR}/genfiles)
  set(GEN_FLATBUFFERS_CONVERTER_PY
      ${PROJECT_SOURCE_DIR}/dnn/scripts/gen_flatbuffers_converter.py)
  file(MAKE_DIRECTORY ${GENERATED_FLATBUFFERS_CONVERTER_PATH})
  add_custom_command(
    OUTPUT ${GENERATED_FLATBUFFERS_CONVERTER_PATH}/opr_param_defs_converter.inl
    COMMAND ${PYTHON_EXECUTABLE} ${GEN_FLATBUFFERS_CONVERTER_PY} ${OPR_PARAM_DEFS_PY}
223
            ${GENERATED_FLATBUFFERS_CONVERTER_PATH}/opr_param_defs_converter.inl
224 225 226 227 228
    DEPENDS ${GEN_FLATBUFFERS_CONVERTER_PY} ${OPR_PARAM_DEFS_PY}
    VERBATIM)
  add_custom_command(
    OUTPUT ${GENERATED_FLATBUFFERS_CONVERTER_PATH}/mgb_opr_param_defs_converter.inl
    COMMAND ${PYTHON_EXECUTABLE} ${GEN_FLATBUFFERS_CONVERTER_PY} ${MGB_PARAM_DEFS_PY}
229
            ${GENERATED_FLATBUFFERS_CONVERTER_PATH}/mgb_opr_param_defs_converter.inl
230 231 232 233 234 235 236 237 238
    DEPENDS ${GEN_FLATBUFFERS_CONVERTER_PY} ${MGB_PARAM_DEFS_PY}
    VERBATIM)
  target_sources(
    megbrain
    PRIVATE ${GENERATED_FLATBUFFERS_CONVERTER_PATH}/opr_param_defs_converter.inl)
  target_sources(
    megbrain
    PRIVATE ${GENERATED_FLATBUFFERS_CONVERTER_PATH}/mgb_opr_param_defs_converter.inl)
  target_include_directories(megbrain PRIVATE ${GENERATED_FLATBUFFERS_CONVERTER_PATH})
239
endif()
240 241 242 243
if(UNIX
   AND NOT ANDROID
   AND NOT APPLE)
  target_link_libraries(megbrain PUBLIC dl rt atomic)
244 245
endif()

246
if(ANDROID)
247
  target_link_libraries(megbrain PUBLIC log)
248 249
endif()

250
set(_VER_FILE ${PROJECT_SOURCE_DIR}/src/version.ld)
251

252 253
# Build as SHARED or STATIC depending on BUILD_SHARED_LIBS=ON/OFF
add_library(megengine)
254
add_library(${MGE_SHARED_LIB} SHARED)
255 256
target_link_libraries(megengine PRIVATE ${MGE_CUDA_LIBS})
target_link_libraries(megengine PUBLIC megbrain megdnn)
257 258
target_link_libraries(${MGE_SHARED_LIB} PUBLIC megbrain megdnn)
target_link_libraries(${MGE_SHARED_LIB} PRIVATE ${MGE_CUDA_LIBS})
259 260 261 262 263 264 265
if(UNIX AND NOT APPLE)
  target_link_options(megengine PRIVATE -Wl,--no-undefined
                      -Wl,--version-script=${_VER_FILE})
  set_target_properties(megengine PROPERTIES LINK_DEPENDS ${_VER_FILE})
  target_link_options(${MGE_SHARED_LIB} PRIVATE -Wl,--no-undefined
                      -Wl,--version-script=${_VER_FILE})
  set_target_properties(${MGE_SHARED_LIB} PROPERTIES LINK_DEPENDS ${_VER_FILE})
266 267
endif()
if(WIN32 OR MSVC)
268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290
  target_compile_definitions(megbrain PRIVATE MGE_DLL_EXPORT)
  target_compile_definitions(megdnn PRIVATE MGE_DLL_EXPORT)
  target_compile_definitions(megengine PRIVATE MGE_DLL_EXPORT)
  target_compile_definitions(${MGE_SHARED_LIB} PRIVATE MGE_DLL_EXPORT)
  # please do not use WINDOWS_EXPORT_ALL_SYMBOLS, as symbols max than 65535 when build
  # with CUDA set_target_properties(megengine PROPERTIES WINDOWS_EXPORT_ALL_SYMBOLS
  # TRUE) set_target_properties(${MGE_SHARED_LIB} PROPERTIES WINDOWS_EXPORT_ALL_SYMBOLS
  # TRUE)
endif()
if(MGE_WITH_DISTRIBUTED)
  message(VERBOSE "megengine configured to link megray")
  target_link_libraries(megengine PUBLIC megray)
  target_link_libraries(${MGE_SHARED_LIB} PUBLIC megray)
endif()
# Do not export targets if MGE_WITH_DISTRIBUTED is on. MegRay is not ready for this.
install(
  TARGETS megengine
  EXPORT ${MGE_EXPORT_TARGETS}
  LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
  ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR})

if(NOT MGE_WITH_DISTRIBUTED)
  install(TARGETS megbrain EXPORT ${MGE_EXPORT_TARGETS})
291
endif()
292

293
foreach(_PATH ${MGB_INC})
294 295 296 297 298
  install(
    DIRECTORY ${_PATH}/megbrain
    DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
    FILES_MATCHING
    PATTERN "*.h")
299
endforeach()