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()

31 32 33 34 35
# Write out lite_build_config.h It defines macros needed by lite
configure_file(src/lite_build_config.h.in
               ${CMAKE_CURRENT_BINARY_DIR}/genfiles/lite_build_config.h)
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/genfiles/lite_build_config.h
        DESTINATION ${CMAKE_INSTALL_PREFIX}/lite/include)
36 37

# begin config lite
38 39 40 41 42
if(LITE_BUILD_WITH_MGE
   AND LITE_WITH_CUDA
   AND NOT WIN32)
  # FXIME third_party cpp redis do not support build with clang-cl
  list(APPEND SOURCES_LITE ${CPP_REDIS_SRCS})
43
endif()
44 45 46 47 48
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)
49 50 51
  target_link_libraries(lite_static PRIVATE megbrain megdnn ${MGE_CUDA_LIBS})
  add_compile_definitions(LITE_BUILD_WITH_MGE=1)
  message(STATUS "build lite with MegEngine.")
52
else()
53
  target_link_libraries(lite_static PUBLIC flatbuffers)
54 55 56
endif()

include_directories(
57 58 59 60 61 62 63 64 65 66 67 68
  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>)
69 70 71 72 73
# end config lite

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

82 83 84
# define a shared lib for whl
add_library(lite_shared_whl SHARED $<TARGET_OBJECTS:lite_static>)
if(LITE_BUILD_WITH_MGE)
85 86 87 88 89
  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()
90 91
endif()
if(ANDROID)
92
  target_link_libraries(lite_shared_whl PRIVATE log)
93
endif()
94

95 96 97 98
# 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
99
# MegEngine/megbrain ROOT_DIR/CMakeLists.txt, just depends lite_static or lite_shared
100
# TODO: need refine lite_static_all_in_one depend objects, but now cmake do not support
101
# define a add_library which OBJECTS args is a set or list or string
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
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()
122
else()
123 124
  message(
    WARNING "do not support lite_static_all_in_one on Windows, please use lite_shared")
125
endif()
126 127

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

131 132 133 134 135 136 137 138
if(LITE_BUILD_WITH_MGE
   AND LITE_WITH_CUDA
   AND NOT WIN32)
  # 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})
139
endif()
140 141 142
set(LITE_VERSION_SCRIPT
    ${PROJECT_SOURCE_DIR}/lite/src/version_lite.ld
    CACHE INTERNAL "Path to linker version script")
143 144
add_custom_target(_lite_version_ld SOURCES ${LITE_VERSION_SCRIPT})
if(NOT MSVC AND NOT WIN32)
145 146
  set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fvisibility=hidden")
  set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fvisibility=hidden")
147
endif()
148 149 150 151 152 153 154
# 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})
155 156 157
endif()

# config install
158 159 160 161 162 163 164 165
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
166
  RUNTIME DESTINATION lite/lib/${MGE_ARCH}
167 168 169 170
  LIBRARY DESTINATION lite/lib/${MGE_ARCH}
  FRAMEWORK DESTINATION lite/lib/${MGE_ARCH}
  ARCHIVE DESTINATION lite/lib/${MGE_ARCH})

171 172 173 174 175 176 177 178
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()

179
install(FILES ${PROJECT_SOURCE_DIR}/lite/include/lite/common_enum_c.h
180
        DESTINATION ${CMAKE_INSTALL_PREFIX}/lite/include/lite-c)
181

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

188 189 190 191 192
install(
  DIRECTORY ${PROJECT_SOURCE_DIR}/lite/lite-c/include
  DESTINATION ${CMAKE_INSTALL_PREFIX}/lite
  FILES_MATCHING
  PATTERN "*.h")
193 194 195

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

199
# load_and_run
200 201
add_subdirectory(load_and_run)

202 203 204 205 206
# 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)
207 208
  # FIXME: hip obj can not find cpp obj only through lite_static
  target_link_libraries(rc4_encryptor megdnn)
209
endif()
210 211 212 213 214 215
target_include_directories(rc4_encryptor
                           PRIVATE {PROJECT_SOURCE_DIR}/lite/src/decryption)
install(
  TARGETS rc4_encryptor
  EXPORT ${LITE_EXPORT_TARGETS}
  RUNTIME DESTINATION lite/tools)