cmake_minimum_required(VERSION 3.15.2)
message(STATUS "CMAKE_GENERATOR: ${CMAKE_GENERATOR}")
if(NOT ${CMAKE_GENERATOR} STREQUAL "Ninja")
message(WARNING "CMAKE_GENERATOR NOT EQUAL Ninja, which we do not recommend")
endif()
include(cmake/FetchMegBrainVersion.cmake)
project(
MegEngine
LANGUAGES C CXX
VERSION ${MGB_VER_STRING})
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
set(CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake/Modules)
set(CMAKE_POLICY_DEFAULT_CMP0048 NEW)
if(NOT MSVC
AND NOT APPLE
AND NOT WIN32)
set(CMAKE_CXX_ARCHIVE_CREATE "<CMAKE_AR> Dqc <TARGET> <LINK_FLAGS> <OBJECTS>")
set(CMAKE_CXX_ARCHIVE_APPEND "<CMAKE_AR> Dq <TARGET> <LINK_FLAGS> <OBJECTS>")
set(CMAKE_CXX_ARCHIVE_FINISH "<CMAKE_RANLIB> -D <TARGET>")
endif()
include(GNUInstallDirs)
include(CheckCXXCompilerFlag)
include(CheckIPOSupported)
include(CMakeDependentOption)
check_cxx_compiler_flag(-Wclass-memaccess CXX_SUPPORT_WCLASS_MEMACCESS)
set(MGE_ARCH
AUTO
CACHE STRING "Architecture on which MegEngine to be built.")
set_property(
CACHE MGE_ARCH
PROPERTY STRINGS
AUTO
x86_64
i386
armv7
aarch64
naive
fallback)
set(MGE_EXPORT_TARGETS MegEngine-targets)
if(NOT "$ENV{LD_LIBRARY_PATH}" STREQUAL "")
string(REPLACE ":" ";" ALTER_LD_LIBRARY_PATHS $ENV{LD_LIBRARY_PATH})
else()
set(ALTER_LD_LIBRARY_PATHS "")
endif()
if(NOT "$ENV{LIBRARY_PATH}" STREQUAL "")
string(REPLACE ":" ";" ALTER_LIBRARY_PATHS $ENV{LIBRARY_PATH})
else()
set(ALTER_LIBRARY_PATHS "")
endif()
option(MGE_WITH_JIT "Build MegEngine with JIT." ON)
option(MGE_WITH_JIT_MLIR "Build MegEngine with MLIR JIT." OFF)
option(MGE_WITH_HALIDE "Build MegEngine with Halide JIT" OFF)
option(MGE_WITH_MIDOUT_PROFILE "Build MegEngine with Midout profile." OFF)
option(
MGE_WITH_MINIMUM_SIZE
"Swith off MGE_ENABLE_RTTI、MGE_ENABLE_EXCEPTIONS、MGE_ENABLE_LOGGING and switch on MGE_INFERENCE_ONLY so that compile minimum load_and_run."
OFF)
option(MGE_ARMV8_2_FEATURE_FP16 "Enable armv8.2-a+fp16 support" OFF)
option(MGE_DISABLE_FLOAT16 "Disable MegEngine float16 support." OFF)
option(MGE_WITH_CUDA "Enable MegEngine CUDA support." ON)
option(MGE_CUDA_USE_STATIC "Enable MegEngine CUDA static linking." ON)
option(MGE_WITH_LITE "Build MGE with lite" ON)
option(MGE_WITH_TRT "Build MegEngine with TensorRT." ON)
option(MGE_WITH_CUDA_STUB "Build MegEngine with CUDA stub." ON)
option(MGE_WITH_NVRTC_STUB "Build MegEngine with NVRTC stub." OFF)
option(MGE_WITH_CUDNN_SHARED "Build MegEngine with CUDNN shared." ON)
option(MGE_WITH_CUBLAS_SHARED "Build MegEngine with CUBLAS shared." OFF)
option(MGE_USE_SYSTEM_LIB "Build MegEngine with system libraries." OFF)
option(MGB_WITH_FLATBUFFERS "Build MegBrain with FlatBuffers serialization support." ON)
option(MGE_WITH_CAMBRICON "Build MegEngine with Cambricon support" OFF)
option(BUILD_SHARED_LIBS "Build shared libraries" ON)
option(MGE_WITH_ATLAS "Build MegEngine with Atlas support" OFF)
option(MGE_ENABLE_RTTI "Build with RTTI" ON)
option(MGE_ENABLE_LOGGING "Build with logging" ON)
option(MGE_DEBUG_UTIL "Enable debug utility" ON)
option(MGE_ENABLE_EXCEPTIONS "Build with exceptions" ON)
option(MGE_WITH_TEST "Enable test for MegEngine." OFF)
option(MGE_WITH_BENCHMARK "Enable DNN BENCHMARK" OFF)
option(MGE_WITH_DISTRIBUTED "Build with distributed support" ON)
option(MGE_BUILD_IMPERATIVE_RT "Build _imperative_rt Python Module " ON)
option(MGE_INFERENCE_ONLY "Build inference only library." OFF)
option(MGE_WITH_MKLDNN "Enable Intel MKL_DNN support," ON)
option(MGE_WITH_ROCM "Enable ROCM support" OFF)
option(MGE_WITH_LARGE_ARCHIVE "Enable big archive link support" OFF)
option(MGE_BUILD_WITH_ASAN "Enable build with ASAN, need compiler support" OFF)
option(MGE_WITH_CUSTOM_OP "Build with Custom op" OFF)
option(MGE_SYNC_THIRD_PARTY "help sync third_party submodule" OFF)
option(MGE_PROFILE_COMPILE_TIME "help profile compile time per file" OFF)
if(MGE_PROFILE_COMPILE_TIME)
set_property(GLOBAL PROPERTY RULE_LAUNCH_COMPILE "cmake -E time")
endif()
# TODO: add windows support
cmake_dependent_option(MGE_WITH_CUPTI "Build with CUPTI" OFF
"MGE_WITH_CUDA;MGE_BUILD_IMPERATIVE_RT;NOT MSVC;NOT WIN32" OFF)
set(MGB_CUPTI ${MGE_WITH_CUPTI})
if(MSVC OR WIN32)
# FIXME: static link Windows vc runtime with some version from Visual Studio have some
# runtime issue at some call PATH, for example: _imperative_rt.pyd -->
# megengine_shared.dll for example c api flush can not find the fd args, I have no
# idea about this issue as a Workround, dynamic link vc runtime, but at some case, we
# will static link vcrt when
# MGE_DEPLOY_INFERENCE_ON_WINDOWS_XP/MGE_DEPLOY_INFERENCE_ON_WINDOWS_XP_SP2, so please
# use lite_static_all_in_one(lite/CMakeLists.txt) in Windows XP env as possible How to
# install VC runtime if you env do not install, refer to:
# https://docs.microsoft.com/en-us/cpp/windows/latest-supported-vc-redist?view=msvc-160
option(MGE_STATIC_LINK_WITH_VC_RUNTIME
"Enable mge static link with Windows vc runtime" OFF)
option(MGE_DEPLOY_INFERENCE_ON_WINDOWS_XP "Enable deploy inference on Windows xp" OFF)
# special MGE_DEPLOY_INFERENCE_ON_WINDOWS_XP_SP2 for Windows XP sp2(32bit) internal
# behavior: 1: will force define MGB_HAVE_THREAD=0, which means only support single
# thread 2: some Feature will be disable, eg: MGB_ENABLE_JSON and var sanity check, do
# not too many care this!!, if you want to use this Feature to 'DEBUG', you can run
# same model at NON-XP-SP2 env, eg Win7 or XP-SP3(build without
# MGE_DEPLOY_INFERENCE_ON_WINDOWS_XP_SP2) 3: we only support MegEngine(load_and_run)
# and MegEngineLite API work on XP SP2 some debug utils, eg, megbrain_test/megdnn_test
# not support run, most caused by gtest src code sdk caller: 1: as we remove mutex,
# when you use MSVC self API eg CreateThread to start several MegEngine instances in
# the same progress, please call MegEngine API(init/run) as serial as possible, also
# please do not use std::thread std::mutex/std::this_thread_id at SDK caller side!!!
# check dll/exe can deploy on Windows XP sp2 or not: please checkout
# scripts/misc/check_windows_xp_sp2_deploy.py
option(MGE_DEPLOY_INFERENCE_ON_WINDOWS_XP_SP2
"Enable deploy inference on Windows xp sp2" OFF)
# PE file linked by LLVM lld can not run at Windows XP env, so we force use link.exe
# which always locate in Microsoft Visual Studio/*/*/VC/Tools/MSVC/*/bin/*/*/link.exe
set(CMAKE_LINKER "link.exe")
if(MGE_DEPLOY_INFERENCE_ON_WINDOWS_XP OR MGE_DEPLOY_INFERENCE_ON_WINDOWS_XP_SP2)
set(MGE_STATIC_LINK_WITH_VC_RUNTIME ON)
message(
STATUS "Force set MGE_STATIC_LINK_WITH_VC_RUNTIME ON when build for Windows XP")
if(NOT ${MGE_ARCH} STREQUAL "i386")
message(FATAL_ERROR "only support 32bit when build for Windows xp")
endif()
if(NOT MGE_INFERENCE_ONLY)
message(FATAL_ERROR "only support inference when build for Windows xp")
endif()
if(MGE_WITH_CUDA)
message(FATAL_ERROR "do not support CUDA when build for Windows xp")
endif()
# Windows XP sp3 have thread issue, Workround for it
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /D_WIN32_WINNT=0x0501 /Zc:threadSafeInit-")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /D_WIN32_WINNT=0x0501 /Zc:threadSafeInit-")
# for Windows XP type
add_link_options("/SUBSYSTEM:CONSOLE,5.01")
# some old lib(for example mkl for xp) use legacy stdio, so we force link
# legacy_stdio_definitions
add_link_options("/DEFAULTLIB:legacy_stdio_definitions.lib")
if(MGE_DEPLOY_INFERENCE_ON_WINDOWS_XP_SP2)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -D__DEPLOY_ON_XP_SP2__=1")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -D__DEPLOY_ON_XP_SP2__=1")
endif()
else()
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /D_