CMakeLists.txt 7.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
option(LITE_BUILD_WITH_MGE "Build lite with MegEngine." ON)

# config lite_build_config.h.in
set(LITE_WITH_OPENCL ${MGE_WITH_OPENCL})
set(LITE_WITH_CUDA ${MGE_WITH_CUDA})
set(LITE_ENABLE_LOGGING ${MGE_ENABLE_LOGGING})
set(LITE_ENABLE_EXCEPTION ${MGE_ENABLE_EXCEPTIONS})
set(LITE_ASSERT_LOC ${MGB_ASSERT_LOC})

if(NOT MGB_WITH_FLATBUFFERS)
11
  include(../cmake/flatbuffers.cmake)
12 13 14 15
endif()

file(GLOB_RECURSE SRC_FBS src/**/*.fbs)
build_flatbuffers(
16 17 18 19 20 21 22
  "${SRC_FBS}"
  ""
  lite_fbs_generate
  ""
  "${CMAKE_CURRENT_BINARY_DIR}"
  ""
  "")
23 24 25 26

file(GLOB_RECURSE SOURCES_LITE src/*.cpp src/*.cc lite-c/*.cpp)

if(MGE_WITH_MINIMUM_SIZE)
27 28
  set(LITE_ENABLE_LOGGING OFF)
  set(LITE_ENABLE_EXCEPTION OFF)
29 30
endif()

M
Megvii Engine Team 已提交
31 32 33 34
if(LITE_BUILD_WITH_MGE)
  set(LITE_BUILD_WITH_MGE ON)
endif()

35
# Write out lite_build_config.h It defines macros needed by lite
M
Megvii Engine Team 已提交
36
configure_file(build_config/lite_build_config.h.in
37
               ${CMAKE_CURRENT_BINARY_DIR}/genfiles/lite_build_config.h)
38 39

# begin config lite
40
if(LITE_BUILD_WITH_MGE
41 42
   AND NOT WIN32
   AND NOT APPLE)
43 44
  # FXIME third_party cpp redis do not support build with clang-cl
  list(APPEND SOURCES_LITE ${CPP_REDIS_SRCS})
45
endif()
46 47 48 49 50
add_library(lite_static STATIC ${SOURCES_LITE})
add_dependencies(lite_static lite_fbs_generate)
include_directories($<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}/genfiles>)

if(LITE_BUILD_WITH_MGE)
51 52
  target_link_libraries(lite_static PRIVATE megbrain megdnn ${MGE_CUDA_LIBS})
  message(STATUS "build lite with MegEngine.")
53
else()
54
  target_link_libraries(lite_static PUBLIC flatbuffers)
55 56 57
endif()

include_directories(
58 59 60 61 62 63 64 65 66 67 68 69
  PUBLIC
  $<INSTALL_INTERFACE:${CMAKE_INSTALL_PREFIX}/lite/include>
  PUBLIC
  $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/lite/include>
  PUBLIC
  $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/lite/include/lite>
  PUBLIC
  $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/lite/lite-c/include>
  PUBLIC
  $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/lite/src>
  PUBLIC
  $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/third_party/Json/include>)
70 71 72 73 74
# end config lite

# define a shared lib
add_library(lite_shared SHARED $<TARGET_OBJECTS:lite_static>)
if(LITE_BUILD_WITH_MGE)
75
  target_link_libraries(lite_shared PRIVATE megbrain megdnn ${MGE_CUDA_LIBS})
76 77
endif()
if(ANDROID)
78 79 80
  link_libraries(log)
  target_link_libraries(lite_static PRIVATE log)
  target_link_libraries(lite_shared PRIVATE log)
81 82
endif()

83 84 85
# define a shared lib for whl
add_library(lite_shared_whl SHARED $<TARGET_OBJECTS:lite_static>)
if(LITE_BUILD_WITH_MGE)
86 87 88 89 90
  if(IOS)
    target_link_libraries(lite_shared_whl PRIVATE megbrain megdnn ${MGE_CUDA_LIBS})
  else()
    target_link_libraries(lite_shared_whl PRIVATE megengine_shared)
  endif()
91 92
endif()
if(ANDROID)
93
  target_link_libraries(lite_shared_whl PRIVATE log)
94
endif()
95

96 97 98 99
# add lite_static_all_in_one same name build by BUILD please do not change
# flatbuffers/cpuinfo/clog/lite_static order, if change!, cmake can not gen
# flatbuffers/cpuinfo/clog OBJs to lite_static_all_in_one, this may cmake issue NOTICE:
# this target always use to separate build with lite, if build lite via include
100
# MegEngine/megbrain ROOT_DIR/CMakeLists.txt, just depends lite_static or lite_shared
101
# TODO: need refine lite_static_all_in_one depend objects, but now cmake do not support
102
# define a add_library which OBJECTS args is a set or list or string
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
if(NOT WIN32)
  if(MGE_ENABLE_CPUINFO AND MGE_WITH_OPENCL)
    add_library(
      lite_static_all_in_one STATIC
      $<TARGET_OBJECTS:flatbuffers> $<TARGET_OBJECTS:cpuinfo> $<TARGET_OBJECTS:clog>
      $<TARGET_OBJECTS:OpenCL> $<TARGET_OBJECTS:lite_static>)
  elseif(MGE_ENABLE_CPUINFO AND NOT MGE_WITH_OPENCL)
    add_library(
      lite_static_all_in_one STATIC
      $<TARGET_OBJECTS:flatbuffers> $<TARGET_OBJECTS:cpuinfo> $<TARGET_OBJECTS:clog>
      $<TARGET_OBJECTS:lite_static>)
  elseif(NOT MGE_ENABLE_CPUINFO AND MGE_WITH_OPENCL)
    add_library(
      lite_static_all_in_one STATIC
      $<TARGET_OBJECTS:flatbuffers> $<TARGET_OBJECTS:OpenCL>
      $<TARGET_OBJECTS:lite_static>)
  else()
    add_library(lite_static_all_in_one STATIC $<TARGET_OBJECTS:flatbuffers>
                                              $<TARGET_OBJECTS:lite_static>)
  endif()
123
else()
124 125
  message(
    WARNING "do not support lite_static_all_in_one on Windows, please use lite_shared")
126
endif()
127 128

if(LITE_BUILD_WITH_MGE AND NOT WIN32)
129
  target_link_libraries(lite_static_all_in_one PRIVATE megbrain megdnn ${MGE_CUDA_LIBS})
130
endif()
131

132
if(LITE_BUILD_WITH_MGE
133 134
   AND NOT WIN32
   AND NOT APPLE)
135 136 137 138 139
  # FXIME third_party cpp redis do not support build with clang-cl
  target_include_directories(lite_static PRIVATE ${CPP_REDIS_INCLUDES})
  target_include_directories(lite_shared PRIVATE ${CPP_REDIS_INCLUDES})
  target_include_directories(lite_shared_whl PRIVATE ${CPP_REDIS_INCLUDES})
  target_include_directories(lite_static_all_in_one PRIVATE ${CPP_REDIS_INCLUDES})
140
endif()
141 142 143
set(LITE_VERSION_SCRIPT
    ${PROJECT_SOURCE_DIR}/lite/src/version_lite.ld
    CACHE INTERNAL "Path to linker version script")
144 145
add_custom_target(_lite_version_ld SOURCES ${LITE_VERSION_SCRIPT})
if(NOT MSVC AND NOT WIN32)
146 147
  set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fvisibility=hidden")
  set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fvisibility=hidden")
148
endif()
149 150 151 152 153 154 155
# TODO: implemente version script for other OS
if(UNIX AND NOT APPLE)
  target_link_options(lite_shared PRIVATE -Wl,--version-script=${LITE_VERSION_SCRIPT})
  set_target_properties(lite_shared PROPERTIES LINK_DEPENDS ${LITE_VERSION_SCRIPT})
  target_link_options(lite_shared_whl PRIVATE
                      -Wl,--version-script=${LITE_VERSION_SCRIPT})
  set_target_properties(lite_shared_whl PROPERTIES LINK_DEPENDS ${LITE_VERSION_SCRIPT})
156 157
endif()

M
Megvii Engine Team 已提交
158 159 160 161
# config install, please keep all install file with lite/BUILD:hdrs_public
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/genfiles/lite_build_config.h
        DESTINATION ${CMAKE_INSTALL_PREFIX}/lite/build_config)

162 163 164 165 166 167 168 169
install(
  TARGETS lite_static
  LIBRARY DESTINATION lite/lib/${MGE_ARCH}
  FRAMEWORK DESTINATION lite/lib/${MGE_ARCH}
  ARCHIVE DESTINATION lite/lib/${MGE_ARCH})

install(
  TARGETS lite_shared
170
  RUNTIME DESTINATION lite/lib/${MGE_ARCH}
171 172 173 174
  LIBRARY DESTINATION lite/lib/${MGE_ARCH}
  FRAMEWORK DESTINATION lite/lib/${MGE_ARCH}
  ARCHIVE DESTINATION lite/lib/${MGE_ARCH})

175 176 177 178 179 180 181 182
if(NOT WIN32)
  install(
    TARGETS lite_static_all_in_one
    LIBRARY DESTINATION lite/lib/${MGE_ARCH}
    FRAMEWORK DESTINATION lite/lib/${MGE_ARCH}
    ARCHIVE DESTINATION lite/lib/${MGE_ARCH})
endif()

183 184 185 186 187
install(
  DIRECTORY ${PROJECT_SOURCE_DIR}/lite/include
  DESTINATION ${CMAKE_INSTALL_PREFIX}/lite
  FILES_MATCHING
  PATTERN "*.h")
188

189 190
install(
  DIRECTORY ${PROJECT_SOURCE_DIR}/lite/lite-c/include
M
Megvii Engine Team 已提交
191
  DESTINATION ${CMAKE_INSTALL_PREFIX}/lite/lite-c
192 193
  FILES_MATCHING
  PATTERN "*.h")
194 195 196

add_subdirectory(example)
if(MGE_WITH_TEST)
197
  add_subdirectory(test)
198 199
endif()

200
# load_and_run
201 202
add_subdirectory(load_and_run)

203 204 205 206 207
# tools and example
add_executable(rc4_encryptor tools/rc4_encrypt.cpp)

target_link_libraries(rc4_encryptor lite_static)
if(LITE_BUILD_WITH_MGE AND MGE_WITH_ROCM)
208 209
  # FIXME: hip obj can not find cpp obj only through lite_static
  target_link_libraries(rc4_encryptor megdnn)
210
endif()
211 212 213 214 215 216
target_include_directories(rc4_encryptor
                           PRIVATE {PROJECT_SOURCE_DIR}/lite/src/decryption)
install(
  TARGETS rc4_encryptor
  EXPORT ${LITE_EXPORT_TARGETS}
  RUNTIME DESTINATION lite/tools)