CMakeLists.txt 5.2 KB
Newer Older
1 2 3
find_package(NumPy REQUIRED)

set(PACKAGE_NAME megengine)
4 5 6
set(PACKAGE_NAME
    ${PACKAGE_NAME}
    PARENT_SCOPE)
7
set(MODULE_NAME _imperative_rt)
8 9 10
set(MODULE_NAME
    ${MODULE_NAME}
    PARENT_SCOPE)
11
file(GLOB_RECURSE SRCS src/impl/*.cpp src/include/*.h python/src/*.cpp python/src/*.h)
12
set(SRCS ${SRCS} ${CPP_REDIS_SRCS})
13 14 15 16

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DMGB_WITH_IMPERATIVE=1")

file(GLOB_RECURSE PYTHON_SRCS python/${PACKAGE_NAME}/*.py)
17 18 19 20 21 22 23 24 25
file(
  GLOB_RECURSE
  ALL_HEADERS
  src/cpp/megbrain_pubapi.h
  ${PROJECT_SOURCE_DIR}/src/core/include/*
  ${PROJECT_SOURCE_DIR}/src/opr/include/*
  ${PROJECT_SOURCE_DIR}/src/serialization/include/*
  ${PROJECT_SOURCE_DIR}/src/plugin/include/*
  ${PROJECT_SOURCE_DIR}/dnn/include/*)
26 27 28

set(MEGENGINE_DIR ${CMAKE_CURRENT_BINARY_DIR}/python/)

29
add_subdirectory(tablegen)
30

31
add_custom_target(_version_ld SOURCES ${MGE_VERSION_SCRIPT})
32

33 34
add_subdirectory(${PROJECT_SOURCE_DIR}/third_party/pybind11
                 ${PROJECT_BINARY_DIR}/third_party/pybind11)
35
pybind11_add_module(${MODULE_NAME} NO_EXTRAS ${SRCS})
36 37 38 39 40 41 42
if(APPLE)
  target_link_libraries(${MODULE_NAME} PRIVATE megengine_shared)
elseif(MSVC OR WIN32)
  target_link_libraries(${MODULE_NAME} PRIVATE megengine_shared)
  message(STATUS "CMAKE_MSVC_RUNTIME_LIBRARY: ${CMAKE_MSVC_RUNTIME_LIBRARY}")
  set_target_properties(${MODULE_NAME} PROPERTIES MSVC_RUNTIME_LIBRARY
                                                  "${CMAKE_MSVC_RUNTIME_LIBRARY}")
43
else()
44 45 46 47
  # use to fix runtime crash when build both mgb(MGE_WITH_PYTHON_MODULE) and
  # imperative(MGE_BUILD_IMPERATIVE_RT)
  target_link_libraries(
    ${MODULE_NAME} PRIVATE megengine_shared -Wl,--version-script=${MGE_VERSION_SCRIPT})
48
endif()
49

50 51
add_subdirectory(${PROJECT_SOURCE_DIR}/third_party/range-v3
                 ${PROJECT_BINARY_DIR}/third_party/range-v3)
52 53
target_link_libraries(${MODULE_NAME} PRIVATE range-v3)

54 55
add_subdirectory(${PROJECT_SOURCE_DIR}/third_party/Json
                 ${PROJECT_BINARY_DIR}/third_party/Json)
56 57
target_link_libraries(${MODULE_NAME} PRIVATE nlohmann_json::nlohmann_json)

58 59 60 61 62
target_include_directories(
  ${MODULE_NAME}
  PUBLIC src/include
  PRIVATE ${PYTHON_INCLUDE_DIRS} ${NUMPY_INCLUDE_DIR} ${MGB_OPDEF_OUT_DIR}
          ${CPP_REDIS_INCLUDES})
63 64 65
target_compile_definitions(${MODULE_NAME} PRIVATE MODULE_NAME=${MODULE_NAME})
target_compile_options(${MODULE_NAME} PRIVATE -Wno-unused-parameter)
if(CXX_SUPPORT_WCLASS_MEMACCESS)
66
  target_compile_options(${MODULE_NAME} PRIVATE "-Wno-class-memaccess")
67
endif()
68 69 70 71 72 73 74 75 76 77 78
set_target_properties(
  ${MODULE_NAME}
  PROPERTIES SUFFIX ${CMAKE_SHARED_LIBRARY_SUFFIX}
             LIBRARY_OUTPUT_DIRECTORY ${MEGENGINE_DIR}/${PACKAGE_NAME}/core)
if(APPLE
   OR MSVC
   OR WIN32)
  message(
    VERBOSE
    "overwriting SUFFIX at macos and windows before config by set_target_properties")
  pybind11_extension(${MODULE_NAME})
79
endif()
80
add_dependencies(${MODULE_NAME} mgb_opdef _version_ld)
81 82

if(MGE_WITH_TEST AND MGE_ENABLE_RTTI)
83
  add_subdirectory(test)
84 85 86
endif()

add_custom_command(
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
  TARGET ${MODULE_NAME}
  POST_BUILD
  COMMAND ${CMAKE_COMMAND} -E copy ${PROJECT_SOURCE_DIR}/LICENSE
          ${PROJECT_SOURCE_DIR}/ACKNOWLEDGMENTS ${PROJECT_BINARY_DIR}
  COMMAND
    ${CMAKE_COMMAND} -E remove -f
    ${CMAKE_CURRENT_SOURCE_DIR}/python/megengine/core/$<TARGET_FILE_NAME:${MODULE_NAME}> # clean
                                                                                         # develop
  COMMAND ${CMAKE_COMMAND} -E remove -f
          ${CMAKE_CURRENT_SOURCE_DIR}/python/megengine/version.py # clean develop
  COMMAND ${CMAKE_COMMAND} -E remove -f
          ${CMAKE_CURRENT_SOURCE_DIR}/python/megengine/core/include # clean develop
  COMMAND ${CMAKE_COMMAND} -E remove -f
          ${CMAKE_CURRENT_SOURCE_DIR}/python/megengine/core/lib # clean develop
  COMMAND
    ${CMAKE_COMMAND} -E copy_directory ${CMAKE_CURRENT_SOURCE_DIR}/python/megengine
    ${CMAKE_CURRENT_BINARY_DIR}/python/megengine
  COMMAND ${CMAKE_COMMAND} -E copy_directory ${CMAKE_CURRENT_SOURCE_DIR}/python/test
          ${CMAKE_CURRENT_BINARY_DIR}/python/test
  COMMAND ${CMAKE_COMMAND} -E copy_directory ${PROJECT_SOURCE_DIR}/src/custom/include
          ${CMAKE_CURRENT_BINARY_DIR}/python/megengine/core/include
  COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_CURRENT_SOURCE_DIR}/python/setup.py
          ${CMAKE_CURRENT_BINARY_DIR}/python/setup.py
  COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_CURRENT_SOURCE_DIR}/python/requires.txt
          ${CMAKE_CURRENT_BINARY_DIR}/python/requires.txt
  COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_CURRENT_SOURCE_DIR}/python/requires-style.txt
          ${CMAKE_CURRENT_BINARY_DIR}/python/requires-style.txt
  COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_CURRENT_SOURCE_DIR}/python/requires-test.txt
          ${CMAKE_CURRENT_BINARY_DIR}/python/requires-test.txt)
116

117
if(DEFINED MGB_VER_MAJOR)
118
  set(IS_INTERNAL "--internal")
119
else()
120
  set(IS_INTERNAL "")
M
Megvii Engine Team 已提交
121 122 123
endif(DEFINED MGB_VER_MAJOR)

if(DEFINED MGE_EXTRA_NAME)
124
  set(RC_NAME "--rc=${MGE_EXTRA_NAME}")
M
Megvii Engine Team 已提交
125
else()
126
  set(RC_NAME "")
M
Megvii Engine Team 已提交
127
endif(DEFINED MGE_EXTRA_NAME)
128

129
add_custom_command(
130 131 132 133 134 135
  TARGET ${MODULE_NAME}
  POST_BUILD
  COMMAND
    "${PYTHON_EXECUTABLE}" ${CMAKE_CURRENT_SOURCE_DIR}/python/gen_version.py --output
    ${CMAKE_CURRENT_BINARY_DIR}/python/megengine/version.py --major ${MGE_VER_MAJOR}
    --minor ${MGE_VER_MINOR} --patch ${MGE_VER_PATCH} ${RC_NAME} ${IS_INTERNAL})