cmake_minimum_required(VERSION 3.14)
project (GraphEngine[CXX])

set(GE_CODE_DIR ${CMAKE_CURRENT_LIST_DIR})
set(CMAKE_SKIP_INSTALL_ALL_DEPENDENCY TRUE)

if (NOT BUILD_PATH)
    set(BUILD_PATH "${CMAKE_SOURCE_DIR}/build")
endif()

if(DEFINED ENV{ASCEND_CUSTOM_PATH})
    set(ASCEND_DIR $ENV{ASCEND_CUSTOM_PATH})
else()
    set(ASCEND_DIR /usr/local/Ascend)
endif()

if(DEFINED ENV{D_PKG_SERVER})
    set(GE_PB_PKG $ENV{D_PKG_SERVER})
    message("Download packages from DPKG server")
elseif(DEFINED ENV{MSLIBS_SERVER})
    set(GE_PB_PKG "http://$ENV{MSLIBS_SERVER}:8081")
    message("Download packages from MSPKG server")
endif ()

set(ASCEND_DRIVER_DIR ${ASCEND_DIR}/driver/lib64)
set(ASCEND_DRIVER_COMMON_DIR ${ASCEND_DIR}/driver/lib64/common)
set(ASCEND_DRIVER_SHARE_DIR ${ASCEND_DIR}/driver/lib64/share)
set(ASCEND_RUNTIME_DIR ${ASCEND_DIR}/fwkacllib/lib64)
set(ASCEND_ATC_DIR ${ASCEND_DIR}/atc/lib64)
set(ASCEND_ACL_DIR ${ASCEND_DIR}/acllib/lib64)
set(STATIC_ACL_LIB ${ASCEND_ACL_DIR})

set(ASCEND_MS_RUNTIME_PATH ${ASCEND_RUNTIME_DIR} ${ASCEND_ACL_DIR} ${ASCEND_ATC_DIR})
set(ASCEND_MS_DRIVER_PATH ${ASCEND_DRIVER_DIR} ${ASCEND_DRIVER_COMMON_DIR})
set(ATLAS_RUNTIME_DIR ${ASCEND_DIR}/ascend-toolkit/latest/fwkacllib/lib64)
set(ATLAS_ACL_DIR ${ASCEND_DIR}/ascend-toolkit/latest/acllib/lib64)
set(ATLAS_ATC_DIR ${ASCEND_DIR}/ascend-toolkit/latest/atc/lib64)
set(ATLAS_MS_RUNTIME_PATH ${ATLAS_RUNTIME_DIR} ${ATLAS_ACL_DIR} ${ATLAS_ATC_DIR})

option(ENABLE_OPEN_SRC "Enable graphengine compile in opensource." FALSE)

if (ENABLE_OPEN_SRC)
    set(HI_PYTHON python3)

    include(cmake/external_libs/protobuf_shared.cmake)
    include(cmake/external_libs/protobuf_static.cmake)
    include(cmake/external_libs/protoc.cmake)
    include(cmake/external_libs/gflags.cmake)
    include(cmake/external_libs/gtest.cmake)
    include(cmake/external_libs/securec.cmake)
    include(cmake/external_libs/json.cmake)
    include(cmake/FindModule.cmake)
    include(cmake/intf_pub_linux.cmake)

    # if D_LINK_PATH is set in environment variables, search libraries in given path
    if(DEFINED ENV{D_LINK_PATH})
        # D_LINK_PATH is set
        set(GE_LIB_PATH $ENV{D_LINK_PATH})
        set(GE_SYS_ARCH "")
        if(CMAKE_HOST_SYSTEM_PROCESSOR MATCHES "x86_64")
            # x86 ubuntu
            set(GE_SYS_ARCH "x86_64")
        elseif(CMAKE_HOST_SYSTEM_PROCESSOR MATCHES "aarch64")
            # arm euleros
            set(GE_SYS_ARCH "aarch64")
        else()
            message(FATAL_ERROR "Running on a unsupported architecture: ${SYSTEM_TYPE}, build terminated")
        endif()
        set(GE_LIB_PATH ${GE_LIB_PATH}/${GE_SYS_ARCH})
        set(STATIC_ACL_LIB ${GE_LIB_PATH})
        find_module(slog libalog.so ${GE_LIB_PATH})
        find_module(static_mmpa libmmpa.a ${GE_LIB_PATH})
        find_module(msprofiler_ext libmsprofiler.a ${GE_LIB_PATH})
        find_module(hccl libhccl.so ${GE_LIB_PATH})
        find_module(adump_server libadump_server.a ${GE_LIB_PATH})
        find_module(runtime libruntime.so ${GE_LIB_PATH})
        find_module(runtime_compile libruntime_compile.so ${GE_LIB_PATH})
        find_module(resource libresource.so ${GE_LIB_PATH})
        find_module(ascend_hal_stub libascend_hal.so ${GE_LIB_PATH})
        find_module(msprofiler_fwk_ext libmsprofiler_fwk.a ${GE_LIB_PATH})
        #find_module(ascendcl_static libascendcl.a ${GE_LIB_PATH})
    elseif(ENABLE_GE_COV OR ENABLE_GE_UT)
	add_subdirectory(tests)
    else()
        find_module(slog libalog.so ${ASCEND_ATC_DIR})
        find_module(static_mmpa libmmpa.a ${ASCEND_ATC_DIR})
        if(PLATFORM STREQUAL "train")
            find_module(adump_server libadump_server.a ${ASCEND_RUNTIME_DIR})
            find_module(runtime libruntime.so ${ASCEND_RUNTIME_DIR})
            find_module(msprofiler_fwk_ext libmsprofiler_fwk.a ${ASCEND_RUNTIME_DIR})
            find_module(ascend_hal_stub libascend_hal.so ${ASCEND_DRIVER_DIR}/driver)
            if(PRODUCT STREQUAL "flr3")
                message(FATAL_ERROR "This platform is not supported in train mode, build terminated")
            endif()
        elseif(PLATFORM STREQUAL "inference")
            find_module(adump_server libadump_server.a ${ASCEND_ACL_DIR})
            find_module(runtime libruntime.so ${ASCEND_ACL_DIR})
	    find_module(runtime_compile libruntime_compile.so ${ASCEND_ATC_DIR})
            find_module(msprofiler_ext libmsprofiler.a ${ASCEND_ACL_DIR})
            if(PRODUCT STREQUAL "flr3")
            elseif(PRODUCT STREQUAL "flr1")
                find_module(ascend_hal_stub libascend_hal.so ${ASCEND_DRIVER_DIR}/driver)
            elseif(PRODUCT STREQUAL "flr2")
                # flr2 ascend_hal_stub limsprof ?
            else()
                find_module(ascend_hal_stub libascend_hal.so ${ASCEND_DRIVER_DIR})
            endif()
        elseif(PLATFORM STREQUAL "all")
	    find_module(adump_server libadump_server.a ${ASCEND_RUNTIME_DIR})
	    find_module(runtime libruntime.so ${ASCEND_RUNTIME_DIR})
            find_module(msprofiler_fwk_ext libmsprofiler_fwk.a ${ASCEND_RUNTIME_DIR})
	    find_module(ascend_hal_stub libascend_hal.so ${ASCEND_DRIVER_DIR})
	    find_module(runtime_compile libruntime_compile.so ${ASCEND_ATC_DIR})
	    find_module(msprofiler_ext libmsprofiler.a ${ASCEND_ACL_DIR})
        else()
            message(STATUS "PLATFORM param is invalid, should be train or inference, you choose nothing!")
        endif()
    endif()

    set(METADEF_DIR ${CMAKE_CURRENT_LIST_DIR}/metadef)
    set(PARSER_DIR ${CMAKE_CURRENT_LIST_DIR}/parser)
    set(GE_DEPEND_DIR ${CMAKE_CURRENT_LIST_DIR}/..)

    add_subdirectory(metadef)
    add_subdirectory(parser)
    #add_subdirectory(metadef/graph)
    #add_subdirectory(metadef/register)
elseif (ENABLE_D OR ENABLE_ACL)
    # compiling with MindSpore
    include(cmake/external_libs/protobuf_static.cmake)
    include(cmake/external_libs/protoc.cmake)
    include(cmake/external_libs/securec.cmake)
    include(cmake/external_libs/json.cmake)
    include(cmake/FindModule.cmake)
    include(cmake/intf_pub_linux.cmake)

    # common libraries
    find_module(slog libalog.so ${ASCEND_MS_RUNTIME_PATH} ${ATLAS_MS_RUNTIME_PATH})
    find_module(static_mmpa libmmpa.a ${ASCEND_MS_RUNTIME_PATH} ${ATLAS_MS_RUNTIME_PATH})

    if (ENABLE_D)
        # training
        find_module(runtime libruntime.so ${ASCEND_MS_RUNTIME_PATH} ${ATLAS_MS_RUNTIME_PATH})
        find_module(register libregister.so ${ASCEND_MS_RUNTIME_PATH} ${ATLAS_MS_RUNTIME_PATH})
    endif ()

    set(METADEF_DIR ${CMAKE_CURRENT_LIST_DIR}/metadef)
    add_subdirectory(metadef)
elseif(ENABLE_MS_TESTCASES)
    include(cmake/external_libs/protobuf_static.cmake)
    include(cmake/external_libs/protoc.cmake)
    include(cmake/external_libs/securec.cmake)
    include(cmake/FindModule.cmake)
    include(cmake/intf_pub_linux.cmake)

    # common libraries
    find_module(slog libalog.so ${ASCEND_MS_RUNTIME_PATH} ${ATLAS_MS_RUNTIME_PATH})
    find_module(static_mmpa libmmpa.a ${ASCEND_MS_RUNTIME_PATH} ${ATLAS_MS_RUNTIME_PATH})

    set(METADEF_DIR ${CMAKE_CURRENT_LIST_DIR}/metadef)
    add_subdirectory(metadef)
else()
    set(METADEF_DIR ${CMAKE_CURRENT_LIST_DIR}/../metadef)
    set(PARSER_DIR ${CMAKE_CURRENT_LIST_DIR}/../parser)
    set(GE_DEPEND_DIR ${CMAKE_CURRENT_LIST_DIR}/..)
endif()

add_subdirectory(ge)
