diff --git a/CMakeLists.txt b/CMakeLists.txt index 91b7df8d..aee59ae2 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -7,7 +7,7 @@ set(CMAKE_CXX_EXTENSIONS OFF) set(CMAKE_POSITION_INDEPENDENT_CODE ON) set(CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake/Modules) -if(NOT MSVC AND NOT APPLE) +if(NOT MSVC AND NOT APPLE AND NOT WIN32) set(CMAKE_CXX_ARCHIVE_CREATE " Dqc ") set(CMAKE_CXX_ARCHIVE_APPEND " Dq ") set(CMAKE_CXX_ARCHIVE_FINISH " -D ") @@ -72,7 +72,7 @@ if(CMAKE_TOOLCHAIN_FILE) endif() if(${MGE_ARCH} STREQUAL "AUTO") - if(${CMAKE_SYSTEM_PROCESSOR} STREQUAL "x86_64") + if(${CMAKE_SYSTEM_PROCESSOR} STREQUAL "x86_64" OR ${CMAKE_SYSTEM_PROCESSOR} STREQUAL "AMD64") set(MGE_ARCH "x86_64") elseif(${CMAKE_SYSTEM_PROCESSOR} STREQUAL "i386" OR ${CMAKE_SYSTEM_PROCESSOR} STREQUAL "i686") set(MGE_ARCH "i386") @@ -85,8 +85,42 @@ if(${MGE_ARCH} STREQUAL "AUTO") endif() endif() +if(MSVC OR WIN32) + add_compile_definitions(NOMINMAX=1 _USE_MATH_DEFINES=1 WIN32=1) + message("-- into windows build...") + message(" -- CMAKE_C_COMPILER_ID: ${CMAKE_C_COMPILER_ID}") + if (${CMAKE_C_COMPILER_ID} STREQUAL "Clang-cl") + message(FATAL_ERROR "only support clang-cl for windows build, pls check detail: scripts/cmake-build/BUILD_README.md") + endif() + # add flags for sse/avx for X86 + set(WIN_FLAGS "-msse4.2 -mavx -mavx2") + # if u CPU is cascadelake series, u can enable for performance + # set(WIN_FLAGS "{WIN_FLAGS} -march=cascadelake -mtune=cascadelake") + # set(WIN_FLAGS "{WIN_FLAGS} -mavx512cd -mavx512vl -mavx512dq -mavx512bw -mavx512vbmi -mavx512vnni") + + # for windows build + set(WIN_FLAGS "${WIN_FLAGS} -Wno-error=implicit-int-conversion -Wno-error=double-promotion") + set(WIN_FLAGS "${WIN_FLAGS} -Wno-error=zero-as-null-pointer-constant -Wno-error=implicit-int-conversion") + set(WIN_FLAGS "${WIN_FLAGS} -Wno-error=float-conversion -Wno-error=shadow-field -Wno-error=covered-switch-default") + set(WIN_FLAGS "${WIN_FLAGS} -Wno-error=deprecated -Wno-error=documentation -Wno-error=unreachable-code-break") + set(WIN_FLAGS "${WIN_FLAGS} /DWIN32 -Wno-macro-redefined /D_WIN32_WINNT=0x0601") + set(WIN_FLAGS "${WIN_FLAGS} /D_CRT_SECURE_NO_DEPRECATE /D_CRT_SECURE_NO_WARNINGS /DNOGDI /D_USE_MATH_DEFINES /bigobj") + set(WIN_FLAGS "${WIN_FLAGS} /Zm500 /EHs /wd4351 /wd4291 /wd4250 /wd4996 /wd4819 -Wno-inconsistent-dllimport") + + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${WIN_FLAGS}") + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${WIN_FLAGS}") +else() + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra") + set(CMAKE_CXX_FLAGS_DEBUG "-O0 -g") + if(ANDROID) + set(CMAKE_CXX_FLAGS_RELEASE "-Ofast -DNDEBUG") + else() + set(CMAKE_CXX_FLAGS_RELEASE "-O3 -DNDEBUG") + endif() +endif() + CHECK_CXX_COMPILER_FLAG(-fuse-ld=gold CXX_SUPPORT_GOLD) -if(CXX_SUPPORT_GOLD AND NOT ANDROID AND NOT APPLE) +if(CXX_SUPPORT_GOLD AND NOT ANDROID AND NOT APPLE AND NOT MSVC AND NOT WIN32) message("-- Using GNU gold linker.") set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fuse-ld=gold") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fuse-ld=gold") @@ -188,18 +222,6 @@ if(MGB_WITH_FLATBUFFERS) include(cmake/flatbuffers.cmake) endif() -if(MSVC) - add_compile_definitions(NOMINMAX=1 _USE_MATH_DEFINES=1 WIN32=1) -else() - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra") - set(CMAKE_CXX_FLAGS_DEBUG "-O0 -g") - if(ANDROID) - set(CMAKE_CXX_FLAGS_RELEASE "-Ofast -DNDEBUG") - else() - set(CMAKE_CXX_FLAGS_RELEASE "-O3 -DNDEBUG") - endif() -endif() - if(MGE_WITH_CUDA) include_directories(${CMAKE_CUDA_TOOLKIT_INCLUDE_DIRECTORIES}) foreach(path ${CMAKE_CUDA_HOST_IMPLICIT_LINK_DIRECTORIES}) @@ -393,6 +415,12 @@ else() set(MGB_ENABLE_DEBUG_UTIL 0) endif() +# FIXME: remove this after imp DEBUG UTIL for windows +if(MSVC OR WIN32) + set(MGB_ENABLE_DEBUG_UTIL 0) + message(" -- disable MGB_ENABLE_DEBUG_UTIL in windows build") +endif() + # TensorRT set(MGB_ENABLE_TENSOR_RT ${MGE_WITH_TRT}) diff --git a/cmake/MKL_DNN.cmake b/cmake/MKL_DNN.cmake index a564f303..7bf370ed 100644 --- a/cmake/MKL_DNN.cmake +++ b/cmake/MKL_DNN.cmake @@ -1,31 +1,25 @@ -include(ExternalProject) -include(GNUInstallDirs) - -set(MKLDNN_DIR "${PROJECT_SOURCE_DIR}/third_party/intel-mkl-dnn" CACHE STRING "mkldnn directory") -set(MKLDNN_BUILD_DIR ${PROJECT_BINARY_DIR}/third_party/intel-mkl-dnn) -set(MKLDNN_LIB ${MKLDNN_BUILD_DIR}/${CMAKE_INSTALL_LIBDIR}/libdnnl.a) - +option(DNNL_BUILD_TESTS "" OFF) +option(DNNL_BUILD_EXAMPLES "" OFF) +set(DNNL_LIBRARY_TYPE STATIC CACHE STRING "config dnnl to STATIC") +# we do not want to use OMP now, so config to CPU mode +# if set to OMP, some dnnl algo will be more fast +set(DNNL_CPU_RUNTIME DNNL_RUNTIME_SEQ CACHE STRING "config dnnl to DNNL_RUNTIME_SEQ") if(MGE_BLAS STREQUAL "MKL") - list(APPEND MKLDNN_BUILD_ARGS -D_DNNL_USE_MKL=ON -DMKLROOT=${MKL_ROOT_DIR}) + option(_DNNL_USE_MKL "" ON) + set(MKLROOT ${MKL_ROOT_DIR} CACHE STRING "MKL ROOT FOR DNNL") + if (WIN32) + set(MKLLIB "mkl_core" CACHE STRING "MKLLIB NAME FOR DNNL") + else() + set(MKLLIB "libmkl_core.a" CACHE STRING "MKLLIB NAME FOR DNNL") + endif() + # workround for DNNL link failed, we do not want to modify + # dnnl MKL.cmake of intel-mkl-dnn when include intel-mkl-dnn + # via add_subdirectory api + link_directories(${MKL_ROOT_DIR}/lib) else() - list(APPEND MKLDNN_BUILD_ARGS -D_DNNL_USE_MKL=OFF) + option(_DNNL_USE_MKL "" OFF) endif() -ExternalProject_add( - mkl_dnn - SOURCE_DIR ${MKLDNN_DIR} - PREFIX ${MKLDNN_BUILD_DIR} - CMAKE_ARGS -DCMAKE_C_COMPILER_LAUNCHER=${CMAKE_C_COMPILER_LAUNCHER} -DCMAKE_CXX_COMPILER_LAUNCHER=${CMAKE_CXX_COMPILER_LAUNCHER} -DDNNL_BUILD_TESTS=OFF -DDNNL_BUILD_EXAMPLES=OFF -DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE} -DCMAKE_INSTALL_PREFIX=${MKLDNN_BUILD_DIR} -DCMAKE_TOOLCHAIN_FILE=${CMAKE_TOOLCHAIN_FILE} -DDNNL_LIBRARY_TYPE=STATIC -DDNNL_CPU_RUNTIME=DNNL_RUNTIME_SEQ ${MKLDNN_BUILD_ARGS} - BUILD_BYPRODUCTS ${MKLDNN_LIB} -) - -set(MKLDNN_INC ${MKLDNN_BUILD_DIR}/include) -file(MAKE_DIRECTORY ${MKLDNN_INC}) - -add_library(libmkl_dnn STATIC IMPORTED GLOBAL) -add_dependencies(libmkl_dnn mkl_dnn) -set_target_properties( - libmkl_dnn PROPERTIES - IMPORTED_LOCATION ${MKLDNN_LIB} - INTERFACE_INCLUDE_DIRECTORIES ${MKLDNN_INC} -) +set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wno-unused-parameter -Wno-extra") +set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-unused-parameter -Wno-extra") +add_subdirectory(${PROJECT_SOURCE_DIR}/third_party/intel-mkl-dnn) diff --git a/cmake/flatbuffers.cmake b/cmake/flatbuffers.cmake index 47818998..b64e3948 100644 --- a/cmake/flatbuffers.cmake +++ b/cmake/flatbuffers.cmake @@ -2,8 +2,24 @@ if (MGE_USE_SYSTEM_LIB) find_package(FlatBuffers REQUIRED) return() endif() +if(MSVC OR WIN32) + message("-- add flags flatc for clang-cl build") + set(FLATC_FLAGS "") + set(FLATC_FLAGS "${FLATC_FLAGS} -Wno-error=unknown-argument -Wno-error=c++98-compat -Wno-error=reserved-id-macro") + set(FLATC_FLAGS "${FLATC_FLAGS} -Wno-error=sign-conversion -Wno-error=exceptions -Wno-error=argument-outside-range") + set(FLATC_FLAGS "${FLATC_FLAGS} -Wno-error=delete-non-virtual-dtor -Wno-error=ignored-attributes -Wno-error=format") + set(FLATC_FLAGS "${FLATC_FLAGS} -Wno-error=sign-compare -Wno-error=unused-private-field -Wno-error=braced-scalar-init") + set(FLATC_FLAGS "${FLATC_FLAGS} -Wno-error=return-type-c-linkage -Wno-error=invalid-noreturn -Wno-error=c++98-compat-pedantic") + set(FLATC_FLAGS "${FLATC_FLAGS} -Wno-error=extra-semi-stmt -Wno-error=missing-prototypes -Wno-error=documentation-unknown-command") + set(FLATC_FLAGS "${FLATC_FLAGS} -Wno-error=missing-variable-declarations -Wno-error=nonportable-system-include-path") + set(FLATC_FLAGS "${FLATC_FLAGS} -Wno-error=exit-time-destructors -Wno-error=unused-macros -Wno-error=global-constructors") + set(FLATC_FLAGS "${FLATC_FLAGS} -Wno-error=switch-enum -Wno-error=missing-noreturn -Wno-error=float-equal") + + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${FLATC_FLAGS}") + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${FLATC_FLAGS}") +endif() option(FLATBUFFERS_BUILD_TESTS "" OFF) add_subdirectory(${PROJECT_SOURCE_DIR}/third_party/flatbuffers ${CMAKE_CURRENT_BINARY_DIR}/flatbuffers - EXCLUDE_FROM_ALL) \ No newline at end of file + EXCLUDE_FROM_ALL) diff --git a/cmake/mkl.cmake b/cmake/mkl.cmake index d222b0a8..391c5693 100644 --- a/cmake/mkl.cmake +++ b/cmake/mkl.cmake @@ -2,6 +2,8 @@ find_path(MKL_ROOT_DIR include/mkl_cblas.h PATHS ${PROJECT_SOURCE_DIR}/third_party/mkl/${MGE_ARCH} + ${PROJECT_SOURCE_DIR}/third_party/mkl/${MGE_ARCH}/Library + ${PROJECT_SOURCE_DIR}/third_party/mkl/x86_32/Library $ENV{MKLDIR} /opt/intel/mkl/*/ /opt/intel/cmkl/*/ @@ -23,20 +25,20 @@ find_path(MKL_INCLUDE_DIR option(MGE_MKL_USE_STATIC "Build MegEngine with static MKL" ON) if(MGE_MKL_USE_STATIC) find_library(MKL_CORE_LIBRARY - NAMES libmkl_core.a libmkl_core.lib + NAMES libmkl_core.a mkl_core.lib PATHS ${MKL_ROOT_DIR}/lib/${MKL_ARCH_DIR} ${MKL_ROOT_DIR}/lib/) find_library(MKL_SEQUENTIAL_LIBRARY - NAMES libmkl_sequential.a libmkl_sequential.lib + NAMES libmkl_sequential.a mkl_sequential.lib PATHS ${MKL_ROOT_DIR}/lib/${MKL_ARCH_DIR} ${MKL_ROOT_DIR}/lib/) if(${MGE_ARCH} STREQUAL "x86_64") find_library(MKL_IPL_LIBRARY - NAMES libmkl_intel_ilp64.a libmkl_intel_ilp64.lib + NAMES libmkl_intel_ilp64.a mkl_intel_ilp64.lib PATHS ${MKL_ROOT_DIR}/lib/${MKL_ARCH_DIR} ${MKL_ROOT_DIR}/lib/) - elseif(${MGE_ARCH} STREQUAL "x86_32") + elseif(${MGE_ARCH} STREQUAL "i386") find_library(MKL_IPL_LIBRARY - NAMES libmkl_intel_32.a libmkl_intel_32.lib + NAMES libmkl_intel_32.a mkl_intel_32.lib mkl_intel_c.lib PATHS ${MKL_ROOT_DIR}/lib/${MKL_ARCH_DIR} ${MKL_ROOT_DIR}/lib/) endif() diff --git a/dnn/CMakeLists.txt b/dnn/CMakeLists.txt index ecbc7d59..0bc22e63 100644 --- a/dnn/CMakeLists.txt +++ b/dnn/CMakeLists.txt @@ -11,7 +11,7 @@ add_custom_command( COMMAND ${PYTHON_EXECUTABLE} ${OPR_PARAM_DEFS_SCRIPT} ${OPR_PARAM_DEFS_SRCS} ${OPR_PARAM_DEFS_OUT_DIR}/megdnn/opr_param_defs.h COMMAND ${PYTHON_EXECUTABLE} ${OPR_PARAM_DEFS_SCRIPT} ${OPR_PARAM_DEFS_SRCS} - /dev/null --write-cppjson ${OPR_PARAM_DEFS_OUT_DIR}/megdnn/opr_param_json.h + tmp_unuse.log --write-cppjson ${OPR_PARAM_DEFS_OUT_DIR}/megdnn/opr_param_json.h DEPENDS ${OPR_PARAM_DEFS_SRCS} ${OPR_PARAM_DEFS_SCRIPT} VERBATIM ) diff --git a/dnn/include/megdnn/dtype/half_common_prologue.h b/dnn/include/megdnn/dtype/half_common_prologue.h index 6de710eb..d96f7857 100644 --- a/dnn/include/megdnn/dtype/half_common_prologue.h +++ b/dnn/include/megdnn/dtype/half_common_prologue.h @@ -189,6 +189,8 @@ #include #include #include +#include +#include #if HALF_ENABLE_CPP11_TYPE_TRAITS #include #endif diff --git a/dnn/src/CMakeLists.txt b/dnn/src/CMakeLists.txt index 84f83db7..da359706 100644 --- a/dnn/src/CMakeLists.txt +++ b/dnn/src/CMakeLists.txt @@ -67,7 +67,7 @@ endif() target_compile_definitions(megdnn INTERFACE ${LIBMEGDNN_DEF}) if(MGE_WITH_MKLDNN AND ${MGE_ARCH} STREQUAL "x86_64") - target_link_libraries(megdnn libmkl_dnn) + target_link_libraries(megdnn dnnl) endif() target_link_libraries(megdnn ${MGE_CUDA_LIBS}) target_link_libraries(megdnn ${MGE_BLAS_LIBS}) diff --git a/dnn/src/common/cv/aligned_allocator.h b/dnn/src/common/cv/aligned_allocator.h index cb09411a..6fc42659 100644 --- a/dnn/src/common/cv/aligned_allocator.h +++ b/dnn/src/common/cv/aligned_allocator.h @@ -18,14 +18,15 @@ #include "megdnn/arch.h" -#ifdef _MSC_VER +#if defined(_MSC_VER) || defined(__MINGW32__) #include "malloc.h" #endif + #if defined(__ANDROID__) || defined(ANDROID) #include "malloc.h" #define HAS_MEMALIGN -#elif !defined(_MSC_VER) +#elif !defined(_MSC_VER) && !defined(__MINGW32__) #define HAS_POSIX_MEMALIGN #endif @@ -77,7 +78,7 @@ public: return result; #elif defined(HAS_MEMALIGN) return (_Tp*)memalign(_align, __n * sizeof(_Tp)); -#elif defined(_MSC_VER) +#elif defined(_MSC_VER) || defined(__MINGW32__) return (_Tp*)_aligned_malloc(__n * sizeof(_Tp), _align); #else #warning \ diff --git a/dnn/src/x86/add_update/opr_impl.cpp b/dnn/src/x86/add_update/opr_impl.cpp index 8e1e7199..437f12a0 100644 --- a/dnn/src/x86/add_update/opr_impl.cpp +++ b/dnn/src/x86/add_update/opr_impl.cpp @@ -17,7 +17,7 @@ #include "src/x86/utils.h" #include -#ifdef WIN32CMAKE +#ifdef WIN32 #include #include #endif diff --git a/dnn/src/x86/conv_bias/f32/strategy_2x3_8x8.cpp b/dnn/src/x86/conv_bias/f32/strategy_2x3_8x8.cpp index a018eb37..6b90829a 100644 --- a/dnn/src/x86/conv_bias/f32/strategy_2x3_8x8.cpp +++ b/dnn/src/x86/conv_bias/f32/strategy_2x3_8x8.cpp @@ -18,7 +18,7 @@ #include "src/x86/avx_helper.h" #include -#ifdef WIN32CMAKE +#ifdef WIN32 #include #include #include diff --git a/dnn/src/x86/conv_bias/f32/strategy_6x3_8x8.cpp b/dnn/src/x86/conv_bias/f32/strategy_6x3_8x8.cpp index 62032e4f..fb92fca6 100644 --- a/dnn/src/x86/conv_bias/f32/strategy_6x3_8x8.cpp +++ b/dnn/src/x86/conv_bias/f32/strategy_6x3_8x8.cpp @@ -19,7 +19,7 @@ #include "src/x86/avx_helper.h" #include -#ifdef WIN32CMAKE +#ifdef WIN32 #include #include #include diff --git a/dnn/src/x86/conv_bias/int8/avx2_chanwise_kern.cpp b/dnn/src/x86/conv_bias/int8/avx2_chanwise_kern.cpp index e00c848e..335fda4f 100644 --- a/dnn/src/x86/conv_bias/int8/avx2_chanwise_kern.cpp +++ b/dnn/src/x86/conv_bias/int8/avx2_chanwise_kern.cpp @@ -15,7 +15,7 @@ #include "src/common/unroll_macro.h" #include "src/x86/conv_bias/int8/common_helper.h" #include "src/x86/elemwise_op.h" -#ifdef WIN32CMAKE +#ifdef WIN32 #include #endif diff --git a/dnn/src/x86/conv_bias/int8/common_helper.h b/dnn/src/x86/conv_bias/int8/common_helper.h index 4fd875ee..b01d6a63 100644 --- a/dnn/src/x86/conv_bias/int8/common_helper.h +++ b/dnn/src/x86/conv_bias/int8/common_helper.h @@ -13,7 +13,7 @@ #include #include "src/common/unroll_macro.h" #include "megdnn/arch.h" -#ifdef WIN32CMAKE +#ifdef WIN32 #include #endif diff --git a/dnn/src/x86/elemwise/avx_util/avx_mathfun.h b/dnn/src/x86/elemwise/avx_util/avx_mathfun.h index 7c93a03e..423caf1b 100644 --- a/dnn/src/x86/elemwise/avx_util/avx_mathfun.h +++ b/dnn/src/x86/elemwise/avx_util/avx_mathfun.h @@ -12,7 +12,7 @@ #include "megdnn/arch.h" #include "megdnn/basic_types.h" #include -#ifdef WIN32CMAKE +#ifdef WIN32 #include #include #include diff --git a/dnn/src/x86/elemwise/fma_util/fma_util.cpp b/dnn/src/x86/elemwise/fma_util/fma_util.cpp index 53a0933c..bc3e60c3 100644 --- a/dnn/src/x86/elemwise/fma_util/fma_util.cpp +++ b/dnn/src/x86/elemwise/fma_util/fma_util.cpp @@ -9,7 +9,7 @@ * "AS IS" BASIS, WITHOUT ARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. */ #include -#ifdef WIN32CMAKE +#ifdef WIN32 #include #include #endif diff --git a/dnn/src/x86/elemwise_helper/kimpl/typecvt.h b/dnn/src/x86/elemwise_helper/kimpl/typecvt.h index e1885bb6..eb7d032f 100644 --- a/dnn/src/x86/elemwise_helper/kimpl/typecvt.h +++ b/dnn/src/x86/elemwise_helper/kimpl/typecvt.h @@ -11,7 +11,7 @@ #pragma once #include -#ifdef WIN32CMAKE +#ifdef WIN32 #include #include #include diff --git a/dnn/src/x86/matrix_mul/common/common.h b/dnn/src/x86/matrix_mul/common/common.h index 477c8450..45f11f94 100644 --- a/dnn/src/x86/matrix_mul/common/common.h +++ b/dnn/src/x86/matrix_mul/common/common.h @@ -11,7 +11,7 @@ #pragma once #include -#ifdef WIN32CMAKE +#ifdef WIN32 #include #include #include diff --git a/dnn/src/x86/quantized_converter.h b/dnn/src/x86/quantized_converter.h index 3f07b182..a0635b54 100644 --- a/dnn/src/x86/quantized_converter.h +++ b/dnn/src/x86/quantized_converter.h @@ -11,7 +11,7 @@ #pragma once #include -#ifdef WIN32CMAKE +#ifdef WIN32 #include #include #include diff --git a/scripts/cmake-build/BUILD_README.md b/scripts/cmake-build/BUILD_README.md new file mode 100644 index 00000000..d973b706 --- /dev/null +++ b/scripts/cmake-build/BUILD_README.md @@ -0,0 +1,106 @@ +# build support status +## host build +* windows build (ok) +* linux build (ok) +* macos build (ok) +## cross build +* windows cross build arm-android (ok) +* windows cross build arm-linux (ok) +* linux cross build arm-android (ok) +* linux cross build arm-linux (ok) +* macos cross build arm-android (ok) +* macos cross build arm-linux (ok but experimental) +* macos cross build ios (ok) + +# build env prepare +## package install +### windows host build + ``` + 1: installl Visual Studio (need support LLVM/clang-cl), eg 2019 + clang-cl 9 linker have crash issue, pls install 7/8/10 + 2: install extension of VS: python/cmake/LLVM + 3: CUDA env(if enable CUDA), version detail: project_root_dir/README.md + ``` +### linux host build + ``` + 1: cmake, which version > 3.14.4 + 2: gcc/g++, which version > 6 + 3: install build-essential git git-lfs gfortran libgfortran-6-dev autoconf gnupg flex bison gperf curl + 4: zlib1g-dev gcc-multilib g++-multilib lib32ncurses5-dev libxml2-utils xsltproc unzip libtool: + 5: librdmacm-dev rdmacm-utils python3-dev swig python3-numpy texinfo + 6: CUDA env(if enable CUDA), version detail: project_root_dir/README.md + ``` +### macos host build + ``` + 1: cmake, which version > 3.14.4 + 2: install brew: /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)" + 3: brew install python python3 swig coreutils + 4: install at least xcode command line tool: https://developer.apple.com/xcode/ + 5: about cuda: we do not support CUDA on macos + ``` +### cross build for arm-android + now we support windows/linux/macos cross build to arm-android + ``` + 1: install unix-like tools, eg MSYS if you are using windows(recommend) + we also support CMD.exe or powershell on windows + 1: download NDK from https://developer.android.google.cn/ndk/downloads/ + for diff OS platform package, suggested NDK20 or NDK21 + 2: export NDK_ROOT=NDK_DIR at bash-like env + 3: config NDK_ROOT to PATH env at windows control board if use CMD/powershell + ``` +### cross build for arm-linux + now we support arm-linux on linux and windows fully, also experimental on MACOS + ``` + 1: download toolchains on https://releases.linaro.org/components/toolchain/gcc-linaro/ + or https://developer.arm.com/tools-and-software/open-source-software/developer-tools/gnu-toolchain/gnu-a/downloads + if use windows or linux + 2: download https://github.com/thinkski/osx-arm-linux-toolchains if use MACOS + ``` +### cross build for ios + now we only support cross build to ios on MACOS + ``` + 1: install full xcode: https://developer.apple.com/xcode/ + ``` +## third-party code prepare +### with bash env(linux/macos/unix-like tools on windows, eg: msys etc) + ``` + ./third_party/prepare.sh + ./third_party/install-mkl.sh + ``` +### windows shell env(eg: cmd, powershell etc) + infact if you can use git command on windows, which means you always install + bash.exe at the same dir of git.exe, find it, then you can prepare third-party + code by command: + ``` + bash.exe ./third_party/prepare.sh + bash.exe ./third_party/install-mkl.sh + ``` +# how to build +## with bash env(linux/macos/unix-like tools on windows, eg: msys etc) + ``` + 1: host build just use scripts:scripts/cmake-build/host_build.sh + 2: cross build to arm-android: scripts/cmake-build/cross_build_android_arm_inference.sh + 3: cross build to arm-linux: scripts/cmake-build/cross_build_linux_arm_inference.sh + 4: cross build to ios: scripts/cmake-build/cross_build_ios_arm_inference.sh + ``` +## windows shell env(eg: cmd, powershell etc) + ``` + 1: we do not provide BAT for cmd/powershlel scripts, BUT u can refs for scripts/cmake-build/*.sh + ``` +## Visual Studio GUI(only for windows host) + ``` + 1: import megengine src to Visual Studio as a project + 2: right click CMakeLists.txt, choose config 'cmake config' + choose clang_cl_x86 or clang_cl_x64 + 3: config other CMAKE config, eg, CUDA ON OR OFF + ``` + + +# other arm-linux-like board support +it`s easy to support other customized arm-linux-like board, example: +1: HISI 3516/3519, infact u can just use toolchains from arm developer or linaro +then call scripts/cmake-build/cross_build_linux_arm_inference.sh to build a ELF +binary, or if you get HISI official toolschain, you just need modify CMAKE_CXX_COMPILER +and CMAKE_C_COMPILER in toolchains/arm-linux-gnueabi* to a real name + +2: about Raspberry, just use scripts/cmake-build/cross_build_linux_arm_inference.sh diff --git a/scripts/cmake-build/cross_build_android_arm_inference.sh b/scripts/cmake-build/cross_build_android_arm_inference.sh index ddb359be..d4b05287 100755 --- a/scripts/cmake-build/cross_build_android_arm_inference.sh +++ b/scripts/cmake-build/cross_build_android_arm_inference.sh @@ -76,13 +76,18 @@ echo "ARCH: $ARCH" echo "----------------------------------------------------" READLINK=readlink +MAKEFILE_TYPE="Unix" OS=$(uname -s) if [ $OS = "Darwin" ];then READLINK=greadlink +elif [[ $OS =~ "NT" ]]; then + echo "BUILD in NT ..." + MAKEFILE_TYPE="Unix" fi SRC_DIR=$($READLINK -f "`dirname $0`/../../") +source $SRC_DIR/scripts/cmake-build/utils/host_build_flatc.sh if [ -z $NDK_ROOT ];then echo "can not find NDK_ROOT env, pls export you NDK root dir to NDK_ROOT" @@ -99,6 +104,7 @@ function cmake_build() { echo "build type: $BUILD_TYPE" echo "build ABI: $BUILD_ABI" echo "build native level: $BUILD_NATIVE_LEVEL" + echo "BUILD MAKEFILE_TYPE: $MAKEFILE_TYPE" if [ -e $BUILD_DIR ];then echo "clean old dir: $BUILD_DIR" rm -rf $BUILD_DIR @@ -112,7 +118,8 @@ function cmake_build() { mkdir -p $BUILD_DIR mkdir -p $INSTALL_DIR cd $BUILD_DIR - cmake -DCMAKE_TOOLCHAIN_FILE="$NDK_ROOT/build/cmake/android.toolchain.cmake" \ + cmake -G "$MAKEFILE_TYPE Makefiles" \ + -DCMAKE_TOOLCHAIN_FILE="$NDK_ROOT/build/cmake/android.toolchain.cmake" \ -DANDROID_NDK="$NDK_ROOT" \ -DCMAKE_BUILD_TYPE=$BUILD_TYPE \ -DANDROID_ABI=$BUILD_ABI \ @@ -129,34 +136,7 @@ function cmake_build() { make install/strip } -function build_flatc() { - BUILD_DIR=$SRC_DIR/build_dir/host_flatc/build - INSTALL_DIR=$BUILD_DIR/../install - if [ -e $BUILD_DIR ];then - echo "clean old dir: $BUILD_DIR" - rm -rf $BUILD_DIR - fi - if [ -e $INSTALL_DIR ];then - echo "clean old dir: $INSTALL_DIR" - rm -rf $INSTALL_DIR - fi - - echo "create build dir" - mkdir -p $BUILD_DIR - mkdir -p $INSTALL_DIR - cd $BUILD_DIR - cmake -DCMAKE_BUILD_TYPE=Release \ - -DCMAKE_INSTALL_PREFIX=$INSTALL_DIR \ - -DFLATBUFFERS_BUILD_TESTS=OFF \ - -DFLATBUFFERS_BUILD_FLATHASH=OFF \ - -DFLATBUFFERS_BUILD_FLATLIB=OFF \ - -DFLATBUFFERS_LIBCXX_WITH_CLANG=OFF \ - $SRC_DIR/third_party/flatbuffers - - make -j$(nproc) - make install/strip -} -build_flatc +build_flatc $SRC_DIR api_level=16 abi="armeabi-v7a with NEON" diff --git a/scripts/cmake-build/cross_build_ios_arm_inference.sh b/scripts/cmake-build/cross_build_ios_arm_inference.sh index 53df15e2..2c0db6e5 100755 --- a/scripts/cmake-build/cross_build_ios_arm_inference.sh +++ b/scripts/cmake-build/cross_build_ios_arm_inference.sh @@ -86,6 +86,7 @@ else fi SRC_DIR=$($READLINK -f "`dirname $0`/../../") +source $SRC_DIR/scripts/cmake-build/utils/host_build_flatc.sh function cmake_build() { BUILD_DIR=$SRC_DIR/build_dir/apple/$3/$4/$1/$BUILD_TYPE/build @@ -133,34 +134,7 @@ function cmake_build() { make install } -function build_flatc() { - BUILD_DIR=$SRC_DIR/build_dir/host_flatc/build - INSTALL_DIR=$BUILD_DIR/../install - if [ -e $BUILD_DIR ];then - echo "clean old dir: $BUILD_DIR" - rm -rf $BUILD_DIR - fi - if [ -e $INSTALL_DIR ];then - echo "clean old dir: $INSTALL_DIR" - rm -rf $INSTALL_DIR - fi - - echo "create build dir" - mkdir -p $BUILD_DIR - mkdir -p $INSTALL_DIR - cd $BUILD_DIR - cmake -DCMAKE_BUILD_TYPE=Release \ - -DCMAKE_INSTALL_PREFIX=$INSTALL_DIR \ - -DFLATBUFFERS_BUILD_TESTS=OFF \ - -DFLATBUFFERS_BUILD_FLATHASH=OFF \ - -DFLATBUFFERS_BUILD_FLATLIB=OFF \ - -DFLATBUFFERS_LIBCXX_WITH_CLANG=OFF \ - $SRC_DIR/third_party/flatbuffers - - make -j$(nproc) - make install/strip -} -build_flatc +build_flatc $SRC_DIR # refs for ../../toolchains/ios.toolchain.cmake # to config this, if u want to build other, diff --git a/scripts/cmake-build/cross_build_linux_arm_inference.sh b/scripts/cmake-build/cross_build_linux_arm_inference.sh index 16cef54f..d7cfe0f3 100755 --- a/scripts/cmake-build/cross_build_linux_arm_inference.sh +++ b/scripts/cmake-build/cross_build_linux_arm_inference.sh @@ -76,13 +76,19 @@ echo "ARCH: $ARCH" echo "----------------------------------------------------" READLINK=readlink +MAKEFILE_TYPE="Unix" OS=$(uname -s) if [ $OS = "Darwin" ];then READLINK=greadlink +elif [[ $OS =~ "NT" ]]; then + echo "BUILD in NT ..." + MAKEFILE_TYPE="Unix" fi SRC_DIR=$($READLINK -f "`dirname $0`/../../") +source $SRC_DIR/scripts/cmake-build/utils/host_build_flatc.sh + function cmake_build() { BUILD_DIR=$SRC_DIR/build_dir/gnu-linux/$1/$BUILD_TYPE/build INSTALL_DIR=$BUILD_DIR/../install @@ -91,6 +97,7 @@ function cmake_build() { echo "install dir: $INSTALL_DIR" echo "build type: $BUILD_TYPE" echo "build toolchain: $TOOLCHAIN" + echo "BUILD MAKEFILE_TYPE: $MAKEFILE_TYPE" if [ -e $BUILD_DIR ];then echo "clean old dir: $BUILD_DIR" rm -rf $BUILD_DIR @@ -104,7 +111,8 @@ function cmake_build() { mkdir -p $BUILD_DIR mkdir -p $INSTALL_DIR cd $BUILD_DIR - cmake -DCMAKE_TOOLCHAIN_FILE=$TOOLCHAIN \ + cmake -G "$MAKEFILE_TYPE Makefiles" \ + -DCMAKE_TOOLCHAIN_FILE=$TOOLCHAIN \ -DCMAKE_BUILD_TYPE=$BUILD_TYPE \ -DMGE_INFERENCE_ONLY=ON \ -DMGE_WITH_CUDA=OFF \ @@ -118,34 +126,7 @@ function cmake_build() { make install/strip } -function build_flatc() { - BUILD_DIR=$SRC_DIR/build_dir/host_flatc/build - INSTALL_DIR=$BUILD_DIR/../install - if [ -e $BUILD_DIR ];then - echo "clean old dir: $BUILD_DIR" - rm -rf $BUILD_DIR - fi - if [ -e $INSTALL_DIR ];then - echo "clean old dir: $INSTALL_DIR" - rm -rf $INSTALL_DIR - fi - - echo "create build dir" - mkdir -p $BUILD_DIR - mkdir -p $INSTALL_DIR - cd $BUILD_DIR - cmake -DCMAKE_BUILD_TYPE=Release \ - -DCMAKE_INSTALL_PREFIX=$INSTALL_DIR \ - -DFLATBUFFERS_BUILD_TESTS=OFF \ - -DFLATBUFFERS_BUILD_FLATHASH=OFF \ - -DFLATBUFFERS_BUILD_FLATLIB=OFF \ - -DFLATBUFFERS_LIBCXX_WITH_CLANG=OFF \ - $SRC_DIR/third_party/flatbuffers - - make -j$(nproc) - make install/strip -} -build_flatc +build_flatc $SRC_DIR toolchain=null if [ "$ARCH" = "arm64-v8a" ]; then diff --git a/scripts/cmake-build/host_build.sh b/scripts/cmake-build/host_build.sh index 64c33dcc..74cc824e 100755 --- a/scripts/cmake-build/host_build.sh +++ b/scripts/cmake-build/host_build.sh @@ -7,6 +7,8 @@ function usage() { echo "-d : Build with Debug mode, defaule Release mode" echo "-c : Build with CUDA, default without CUDA" echo "-t : Build with training mode, default inference only" + echo "-m : Build with m32 mode(only for windows build), default m64" + echo "-h : show usage" echo "example: $0 -d" exit -1 } @@ -14,8 +16,11 @@ function usage() { BUILD_TYPE=Release MGE_WITH_CUDA=OFF MGE_INFERENCE_ONLY=ON +MGE_WINDOWS_BUILD_ARCH=x64 +MGE_WINDOWS_BUILD_MARCH=m64 +MGE_ARCH=x86_64 -while getopts "dct" arg +while getopts "hdctm" arg do case $arg in d) @@ -30,6 +35,16 @@ do echo "Build with training mode" MGE_INFERENCE_ONLY=OFF ;; + h) + echo "show usage" + usage + ;; + m) + echo "build for m32(only use for windows)" + MGE_WINDOWS_BUILD_ARCH=x86 + MGE_WINDOWS_BUILD_MARCH=m32 + MGE_ARCH=i386 + ;; ?) echo "unkonw argument" usage @@ -51,6 +66,8 @@ if [ $OS = "Darwin" ];then echo "MACOS DO NOT SUPPORT TensorRT, ABORT NOW!!" exit -1 fi +elif [[ $OS =~ "NT" ]]; then + echo "BUILD in NT ..." fi SRC_DIR=$($READLINK -f "`dirname $0`/../../") @@ -88,7 +105,111 @@ function cmake_build() { make -j$(nproc) make install/strip - } +} + +function windows_env_err() { + echo "check windows env failed!!" + echo "please install LLVM/clang-cl/cmake/python at Visual Studio Extensions" + exit -1 +} + +function prepare_env_for_windows_build() { + echo "check Visual Studio install path env..." + if [[ -z $VS_PATH ]];then + echo "can not find visual_studio_path env, pls export you Visual Studio install dir to VS_PATH" + echo "examle for export Visual Studio 2019 Enterprise default install dir" + echo "export VS_PATH=/c/Program\ Files\ \(x86\)/Microsoft\ Visual\ Studio/2019/Enterprise" + exit -1 + fi + echo $VS_PATH -cmake_build $MGE_WITH_CUDA $MGE_INFERENCE_ONLY $BUILD_TYPE + # only use cmake/clang-cl/Ninja install from Visual Studio, if not, may build failed + # some user env may install cmake/clang-cl/Ninja at MSYS env, so we put Visual Studio + # path at the head of PATH, and check the valid + echo "check cmake install..." + export PATH=$VS_PATH/Common7/IDE/CommonExtensions/Microsoft/CMake/CMake/bin/:$PATH + which cmake + cmake_loc=`which cmake` + if [[ $cmake_loc =~ "Visual" ]]; then + echo "cmake valid ..." + else + echo "cmake Invalid: ..." + windows_env_err + fi + echo "check clang-cl install..." + export PATH=$VS_PATH/VC/Tools/Llvm/bin/:$PATH + which clang-cl + clang_loc=`which clang-cl` + if [[ $clang_loc =~ "Visual" ]]; then + echo "clang-cl valid ..." + else + echo "clang-cl Invalid: ..." + windows_env_err + fi + + echo "check Ninja install..." + export PATH=$VS_PATH/Common7/IDE/CommonExtensions/Microsoft/CMake/Ninja/:$PATH + which Ninja + ninja_loc=`which Ninja` + if [[ $ninja_loc =~ "Visual" ]]; then + echo "Ninja valid ..." + else + echo "Ninja Invalid: ..." + windows_env_er + fi + + export PATH=$VS_PATH/VC/Auxiliary/Build:$PATH + echo "put vcvarsall.bat path to PATH env.." +} + +function cmake_build_windows() { + # windows do not support long path, so we cache the BUILD_DIR ASAP + prepare_env_for_windows_build + BUILD_DIR=$SRC_DIR/build_dir/host/build + INSTALL_DIR=$BUILD_DIR/../install + MGE_WITH_CUDA=$1 + MGE_INFERENCE_ONLY=$2 + BUILD_TYPE=$3 + echo "build dir: $BUILD_DIR" + echo "install dir: $INSTALL_DIR" + echo "build type: $BUILD_TYPE" + echo "MGE_WITH_CUDA: $MGE_WITH_CUDA" + echo "MGE_INFERENCE_ONLY: $MGE_INFERENCE_ONLY" + if [ -e $BUILD_DIR ];then + echo "clean old dir: $BUILD_DIR" + rm -rf $BUILD_DIR + fi + if [ -e $INSTALL_DIR ];then + echo "clean old dir: $INSTALL_DIR" + rm -rf $INSTALL_DIR + fi + + echo "create build dir" + mkdir -p $BUILD_DIR + mkdir -p $INSTALL_DIR + cd $BUILD_DIR + echo "now try build windows native with cmake/clang-ci/Ninja/Visual Studio ....." + export CFLAGS=-$MGE_WINDOWS_BUILD_MARCH + export CXXFLAGS=-$MGE_WINDOWS_BUILD_MARCH + cmd.exe /c " \ + vcvarsall.bat $MGE_WINDOWS_BUILD_ARCH && cmake -G "Ninja" \ + -DMGE_ARCH=$MGE_ARCH \ + -DMGE_INFERENCE_ONLY=$MGE_INFERENCE_ONLY \ + -DMGE_WITH_CUDA=$MGE_WITH_CUDA \ + -DCMAKE_BUILD_TYPE=$CMAKE_BUILD_TYPE \ + -DCMAKE_INSTALL_PREFIX:PATH=$INSTALL_DIR \ + -DCMAKE_C_COMPILER=clang-cl.exe \ + -DCMAKE_CXX_COMPILER=clang-cl.exe \ + -DCMAKE_MAKE_PROGRAM=ninja.exe \ + ../../.. && \ + echo \"start Ninja build log to build.log, may take serval min...\" && \ + Ninja load_and_run > build.log" +} + + +if [[ $OS =~ "NT" ]]; then + cmake_build_windows $MGE_WITH_CUDA $MGE_INFERENCE_ONLY $BUILD_TYPE +else + cmake_build $MGE_WITH_CUDA $MGE_INFERENCE_ONLY $BUILD_TYPE +fi diff --git a/scripts/cmake-build/utils/host_build_flatc.sh b/scripts/cmake-build/utils/host_build_flatc.sh new file mode 100755 index 00000000..3cc55be8 --- /dev/null +++ b/scripts/cmake-build/utils/host_build_flatc.sh @@ -0,0 +1,39 @@ +#!/usr/bin/env bash +set -e + +MAKEFILE_TYPE="Unix" +OS=$(uname -s) + +if [[ $OS =~ "NT" ]]; then + echo "BUILD in NT ..." + MAKEFILE_TYPE="Unix" +fi + +function build_flatc() { + BUILD_DIR=$1/build_dir/host_flatc/build + INSTALL_DIR=$BUILD_DIR/../install + if [ -e $BUILD_DIR ];then + echo "clean old dir: $BUILD_DIR" + rm -rf $BUILD_DIR + fi + if [ -e $INSTALL_DIR ];then + echo "clean old dir: $INSTALL_DIR" + rm -rf $INSTALL_DIR + fi + + echo "create build dir" + mkdir -p $BUILD_DIR + mkdir -p $INSTALL_DIR + cd $BUILD_DIR + cmake -G "$MAKEFILE_TYPE Makefiles" \ + -DCMAKE_BUILD_TYPE=Release \ + -DCMAKE_INSTALL_PREFIX=$INSTALL_DIR \ + -DFLATBUFFERS_BUILD_TESTS=OFF \ + -DFLATBUFFERS_BUILD_FLATHASH=OFF \ + -DFLATBUFFERS_BUILD_FLATLIB=OFF \ + -DFLATBUFFERS_LIBCXX_WITH_CLANG=OFF \ + $SRC_DIR/third_party/flatbuffers + + make -j$(nproc) + make install/strip +} diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 21e06fcc..52c402db 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -106,7 +106,7 @@ if (MGB_WITH_FLATBUFFERS) target_compile_definitions(megbrain PUBLIC MGB_ENABLE_FBS_SERIALIZATION=1) target_link_libraries(megbrain flatbuffers) - set (GENERATED_FLATBUFFERS_CONVERTER_PATH ${CMAKE_CURRENT_BINARY_DIR}/genfiles/this/should/be/added/to/sereg_caller/target/only/but/hey/yolo/there/are/no/fine/grained/targets/let/us/just/make/this/path/unreasonably/long/to/avoid/collision) + 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( diff --git a/test/src/include/megbrain/test/helper.h b/test/src/include/megbrain/test/helper.h index eaea3e2e..66dfa1e5 100644 --- a/test/src/include/megbrain/test/helper.h +++ b/test/src/include/megbrain/test/helper.h @@ -17,7 +17,7 @@ #include -#ifdef _WIN32 +#if defined(WIN32) static inline void unsetenv(std::string name) { name += "="; _putenv(name.c_str()); diff --git a/third_party/install-mkl.sh b/third_party/install-mkl.sh index 4b963aaf..666d0bd3 100755 --- a/third_party/install-mkl.sh +++ b/third_party/install-mkl.sh @@ -2,19 +2,50 @@ cd $(dirname $0) +#FIXME: anaconda just upload serval lastest version, so this version may lose efficacy +echo "this script only for linux/macos/windows-unix-like-env(MSYS etc) prepare MKL env" +echo "if you build windows for native at cmd.exe, powershell env or Visual Studio GUI," +echo "u need download MKL package and untar manually" +echo "refs: https://software.intel.com/content/www/us/en/develop/tools/math-kernel-library/choose-download/windows.html" MKL_VERSION=2019.5 MKL_PATCH=281 CONDA_BASE_URL=https://anaconda.org/intel +OS=$(uname -s) +FILE_PREFIX=null +TAR=tar + +if [ $OS = "Darwin" ];then + FILE_PREFIX=osx +elif [ $OS = "Linux" ];then + FILE_PREFIX=linux +elif [[ $OS =~ "NT" ]]; then + FILE_PREFIX=win + # NT use /c/Windows/system32/tar will stuck for big file + # so we back to GNU tar + TAR=/usr/bin/tar +else + echo "DO NOT SUPPORT OS NOW" + exit -1 +fi + +echo "config FILE_PREFIX to: $FILE_PREFIX" rm -rf mkl for platform in 32 64 do + if [ $OS = "Darwin" ]&&[ $platform = 32 ];then + echo "strip 32 bit file for Darwin" + continue + fi mkdir -p mkl/x86_${platform} for package in "mkl-include" "mkl-static" do - echo "Installing ${package} for x86_${platform}..." - URL=${CONDA_BASE_URL}/${package}/${MKL_VERSION}/download/linux-${platform}/${package}-${MKL_VERSION}-intel_${MKL_PATCH}.tar.bz2 - wget -q --show-progress "${URL}" -O - | tar xj -C mkl/x86_${platform} + DOWNLOAD_FILE=${package}-${MKL_VERSION}-intel_${MKL_PATCH}.tar.bz2 + echo "Installing ${DOWNLOAD_FILE} for x86_${platform}..." + URL=${CONDA_BASE_URL}/${package}/${MKL_VERSION}/download/$FILE_PREFIX-${platform}/${DOWNLOAD_FILE} + echo "try download mkl package from: ${URL}" + wget -q --show-progress "${URL}" -O mkl/x86_${platform}/${DOWNLOAD_FILE} + $TAR xvj -C mkl/x86_${platform} -f mkl/x86_${platform}/${DOWNLOAD_FILE} done done