提交 786afef4 编写于 作者: M Megvii Engine Team 提交者: Xu Xinran

feat(build): install CMake config module and pkg-config descriptor

Also, upgrade to CMake 3.13.

The commit also contains significant refactors, as otherwise it is not
possible to properly export target `megengine` to
MegEngine-targets.cmake:

1. Optionally use system provided Flatbuffers.
2. Optionally use system provided MKL-DNN (Tested with Debian).
3. Refactor megbrain and megdnn targets into object libraries.
4. Set different path in BUILD_INTERFACE and INSTALL_INTERFACE of
   various target_include_directories.
5. Specify PUBLIC/PRIVATE on various target_link_libraries.

GitOrigin-RevId: df118a879e12acdc42b56bdd7c7c71b34b0effc3
上级 4d35397b
cmake_minimum_required(VERSION 3.9.0)
project(MegEngine)
cmake_minimum_required(VERSION 3.13.0)
include (cmake/FetchMegBrainVersion.cmake)
project(MegEngine LANGUAGES C CXX VERSION ${MGB_VER_STRING})
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
set(CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake/Modules)
set(CMAKE_POLICY_DEFAULT_CMP0048 NEW)
if(NOT MSVC AND NOT APPLE AND NOT WIN32)
set(CMAKE_CXX_ARCHIVE_CREATE "<CMAKE_AR> Dqc <TARGET> <LINK_FLAGS> <OBJECTS>")
......@@ -13,6 +16,7 @@ if(NOT MSVC AND NOT APPLE AND NOT WIN32)
set(CMAKE_CXX_ARCHIVE_FINISH "<CMAKE_RANLIB> -D <TARGET>")
endif()
include(GNUInstallDirs)
include(CheckCXXCompilerFlag)
CHECK_CXX_COMPILER_FLAG(-Wclass-memaccess CXX_SUPPORT_WCLASS_MEMACCESS)
......@@ -22,6 +26,7 @@ set_property(CACHE MGE_ARCH PROPERTY STRINGS AUTO
armv7 aarch64
naive fallback
)
set (MGE_EXPORT_TARGETS MegEngine-targets)
option(MGE_WITH_JIT "Build MegEngine with JIT." ON)
option(MGE_WITH_HALIDE "Build MegEngine with Halide JIT" ON)
......@@ -33,6 +38,15 @@ option(MGE_CUDA_USE_STATIC "Enable MegEngine CUDA static linking." ON)
option(MGE_WITH_TRT "Build MegEngine with TensorRT." ON)
option(MGE_USE_SYSTEM_LIB "Build MegEngine with system libraries." OFF)
option(MGB_WITH_FLATBUFFERS "Build MegBrain with FlatBuffers serialization support." ON)
option(BUILD_SHARED_LIBS "Build shared libraries" ON)
if (MGE_USE_SYSTEM_LIB)
set (MGE_CUDA_USE_STATIC OFF)
endif()
if (MGB_WITH_FLATBUFFERS)
set(MGB_ENABLE_FBS_SERIALIZATION ON)
endif()
if(CMAKE_TOOLCHAIN_FILE)
message("We are cross compiling.")
......@@ -509,7 +523,7 @@ set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${MARCH}")
# Write out megbrain_build_config.h
# It defines macros needed by both megbrain and dnn
configure_file(src/megbrain_build_config.h.in ${CMAKE_CURRENT_BINARY_DIR}/genfiles/megbrain_build_config.h)
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/genfiles/megbrain_build_config.h DESTINATION include)
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/genfiles/megbrain_build_config.h DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
add_subdirectory(dnn)
......@@ -531,13 +545,18 @@ list(APPEND MGB_OPR_PARAM_DEFS_OUTS
${MGB_OPR_PARAM_DEFS_OUT_DIR}/megbrain/opr/param_defs.h
)
install(FILES ${MGB_OPR_PARAM_DEFS_OUTS} DESTINATION include/megbrain/opr/)
install(FILES ${MGB_OPR_PARAM_DEFS_OUTS} DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/megbrain/opr/)
list(APPEND MGB_OPR_PARAM_DEFS_INC ${MGB_OPR_PARAM_DEFS_OUT_DIR})
add_custom_target(_mgb_opr_param_defs DEPENDS ${MGB_OPR_PARAM_DEFS_OUTS})
add_library(mgb_opr_param_defs INTERFACE)
target_include_directories(mgb_opr_param_defs INTERFACE ${MGB_OPR_PARAM_DEFS_INC})
target_include_directories(mgb_opr_param_defs
INTERFACE
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
$<BUILD_INTERFACE:${MGB_OPR_PARAM_DEFS_INC}>
)
add_dependencies(mgb_opr_param_defs _mgb_opr_param_defs)
install(TARGETS mgb_opr_param_defs EXPORT ${MGE_EXPORT_TARGETS})
if(MGE_WITH_DISTRIBUTED)
add_subdirectory(${PROJECT_SOURCE_DIR}/third_party/MegRay)
......@@ -554,12 +573,12 @@ if(MGE_WITH_TEST AND MGE_ENABLE_RTTI)
add_subdirectory(test)
endif()
if(TARGET _mgb)
if(TARGET mgb)
add_custom_target(
develop
COMMAND ${CMAKE_COMMAND} -E create_symlink
${CMAKE_CURRENT_BINARY_DIR}/python_module/megengine/_internal/$<TARGET_FILE_NAME:_mgb>
${CMAKE_CURRENT_SOURCE_DIR}/python_module/megengine/_internal/$<TARGET_FILE_NAME:_mgb>
${CMAKE_CURRENT_BINARY_DIR}/python_module/megengine/_internal/$<TARGET_FILE_NAME:mgb>
${CMAKE_CURRENT_SOURCE_DIR}/python_module/megengine/_internal/$<TARGET_FILE_NAME:mgb>
COMMAND ${CMAKE_COMMAND} -E create_symlink
${CMAKE_CURRENT_BINARY_DIR}/python_module/megengine/_internal/mgb.py
${CMAKE_CURRENT_SOURCE_DIR}/python_module/megengine/_internal/mgb.py
......@@ -572,7 +591,7 @@ if(TARGET _mgb)
COMMAND ${CMAKE_COMMAND} -E create_symlink
${CMAKE_CURRENT_BINARY_DIR}/python_module/megengine/_internal/include
${CMAKE_CURRENT_SOURCE_DIR}/python_module/megengine/_internal/include
DEPENDS _mgb
DEPENDS mgb
VERBATIM
)
endif()
......@@ -584,3 +603,38 @@ IF(APPLE)
set(CMAKE_USE_PTHREADS_INIT 1)
set(THREADS_PREFER_PTHREAD_FLAG ON)
ENDIF()
# Configure and install pkg-config.
# Note that unlike the Config.cmake modules, this is not relocatable (and not
# really portable) because we have two dependencies without pkg-config
# descriptions: FlatBuffers and MKL-DNN
if (MGE_USE_SYSTEM_MKLDNN)
set (MGE_PKGCONFIG_LIBS_PRIVATE "-ldnnl")
endif()
if (MGE_USE_SYSTEM_OPENBLAS)
set (MGE_PKGCONFIG_LIBS_PRIVATE "${MGE_PKGCONFIG_LIBS_PRIVATE} -lopenblas")
endif()
configure_file(cmake/megengine.pc.in
${CMAKE_CURRENT_BINARY_DIR}/megengine.pc
@ONLY)
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/megengine.pc
DESTINATION ${CMAKE_INSTALL_LIBDIR}/pkgconfig)
# Do not export targets if MGE_WITH_DISTRIBUTED is on. MegRay is not ready.
if (NOT MGE_WITH_DISTRIBUTED)
include(CMakePackageConfigHelpers)
set (MGE_INSTALL_CMAKEDIR ${CMAKE_INSTALL_LIBDIR}/cmake/MegEngine)
configure_package_config_file(cmake/MegEngineConfig.cmake.in
${CMAKE_CURRENT_BINARY_DIR}/MegEngineConfig.cmake
INSTALL_DESTINATION ${MGE_INSTALL_CMAKEDIR}
)
write_basic_package_version_file(
${CMAKE_CURRENT_BINARY_DIR}/MegEngineConfigVersion.cmake
VERSION ${MGB_VER_MAJOR}.${MGB_VER_MINOR}.${MGB_VER_PATCH}
COMPATIBILITY SameMajorVersion)
install(EXPORT ${MGE_EXPORT_TARGETS} DESTINATION ${MGE_INSTALL_CMAKEDIR})
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/MegEngineConfig.cmake
${CMAKE_CURRENT_BINARY_DIR}/MegEngineConfigVersion.cmake
DESTINATION ${MGE_INSTALL_CMAKEDIR})
endif()
# Copyright 2015 Google Inc. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# General function to create FlatBuffer build rules for the given list of
# schemas.
#
# flatbuffers_schemas: A list of flatbuffer schema files to process.
#
# schema_include_dirs: A list of schema file include directories, which will be
# passed to flatc via the -I parameter.
#
# custom_target_name: The generated files will be added as dependencies for a
# new custom target with this name. You should add that target as a dependency
# for your main target to ensure these files are built. You can also retrieve
# various properties from this target, such as GENERATED_INCLUDES_DIR,
# BINARY_SCHEMAS_DIR, and COPY_TEXT_SCHEMAS_DIR.
#
# additional_dependencies: A list of additional dependencies that you'd like
# all generated files to depend on. Pass in a blank string if you have none.
#
# generated_includes_dir: Where to generate the C++ header files for these
# schemas. The generated includes directory will automatically be added to
# CMake's include directories, and will be where generated header files are
# placed. This parameter is optional; pass in empty string if you don't want to
# generate include files for these schemas.
#
# binary_schemas_dir: If you specify an optional binary schema directory, binary
# schemas will be generated for these schemas as well, and placed into the given
# directory.
#
# copy_text_schemas_dir: If you want all text schemas (including schemas from
# all schema include directories) copied into a directory (for example, if you
# need them within your project to build JSON files), you can specify that
# folder here. All text schemas will be copied to that folder.
#
# IMPORTANT: Make sure you quote all list arguments you pass to this function!
# Otherwise CMake will only pass in the first element.
# Example: build_flatbuffers("${fb_files}" "${include_dirs}" target_name ...)
function(build_flatbuffers flatbuffers_schemas
schema_include_dirs
custom_target_name
additional_dependencies
generated_includes_dir
binary_schemas_dir
copy_text_schemas_dir)
# Test if including from FindFlatBuffers
if(FLATBUFFERS_FLATC_EXECUTABLE)
set(FLATC_TARGET "")
set(FLATC ${FLATBUFFERS_FLATC_EXECUTABLE})
else()
set(FLATC_TARGET flatbuffers::flatc)
set(FLATC flatc)
endif()
set(FLATC_SCHEMA_ARGS --gen-mutable)
if(FLATBUFFERS_FLATC_SCHEMA_EXTRA_ARGS)
set(FLATC_SCHEMA_ARGS
${FLATBUFFERS_FLATC_SCHEMA_EXTRA_ARGS}
${FLATC_SCHEMA_ARGS}
)
endif()
set(working_dir "${CMAKE_CURRENT_SOURCE_DIR}")
set(schema_glob "*.fbs")
# Generate the include files parameters.
set(include_params "")
set(all_generated_files "")
foreach (include_dir ${schema_include_dirs})
set(include_params -I ${include_dir} ${include_params})
if (NOT ${copy_text_schemas_dir} STREQUAL "")
# Copy text schemas from dependent folders.
file(GLOB_RECURSE dependent_schemas ${include_dir}/${schema_glob})
foreach (dependent_schema ${dependent_schemas})
file(COPY ${dependent_schema} DESTINATION ${copy_text_schemas_dir})
endforeach()
endif()
endforeach()
foreach(schema ${flatbuffers_schemas})
get_filename_component(filename ${schema} NAME_WE)
# For each schema, do the things we requested.
if (NOT ${generated_includes_dir} STREQUAL "")
set(generated_include ${generated_includes_dir}/${filename}_generated.h)
add_custom_command(
OUTPUT ${generated_include}
COMMAND ${FLATC} ${FLATC_SCHEMA_ARGS}
-o ${generated_includes_dir}
${include_params}
-c ${schema}
DEPENDS ${FLATC_TARGET} ${schema} ${additional_dependencies}
WORKING_DIRECTORY "${working_dir}")
list(APPEND all_generated_files ${generated_include})
endif()
if (NOT ${binary_schemas_dir} STREQUAL "")
set(binary_schema ${binary_schemas_dir}/${filename}.bfbs)
add_custom_command(
OUTPUT ${binary_schema}
COMMAND ${FLATC} -b --schema
-o ${binary_schemas_dir}
${include_params}
${schema}
DEPENDS ${FLATC_TARGET} ${schema} ${additional_dependencies}
WORKING_DIRECTORY "${working_dir}")
list(APPEND all_generated_files ${binary_schema})
endif()
if (NOT ${copy_text_schemas_dir} STREQUAL "")
file(COPY ${schema} DESTINATION ${copy_text_schemas_dir})
endif()
endforeach()
# Create a custom target that depends on all the generated files.
# This is the target that you can depend on to trigger all these
# to be built.
add_custom_target(${custom_target_name}
DEPENDS ${all_generated_files} ${additional_dependencies})
# Register the include directory we are using.
if (NOT ${generated_includes_dir} STREQUAL "")
include_directories(${generated_includes_dir})
set_property(TARGET ${custom_target_name}
PROPERTY GENERATED_INCLUDES_DIR
${generated_includes_dir})
endif()
# Register the binary schemas dir we are using.
if (NOT ${binary_schemas_dir} STREQUAL "")
set_property(TARGET ${custom_target_name}
PROPERTY BINARY_SCHEMAS_DIR
${binary_schemas_dir})
endif()
# Register the text schema copy dir we are using.
if (NOT ${copy_text_schemas_dir} STREQUAL "")
set_property(TARGET ${custom_target_name}
PROPERTY COPY_TEXT_SCHEMAS_DIR
${copy_text_schemas_dir})
endif()
endfunction()
# Parses the version set in src/core/include/megbrain/version.h
# Exports the following variables:
# MGB_VER_MAJOR: Major version
# MGB_VER_MINOR: Minor version
# MGB_VER_PATCH: Patch version
# MGB_IS_DEV: Is development version
# MGB_VER_STRING: Version string
option(MGB_FORCE_DEV_VERSION "Force -dev tag in version stamp" OFF)
file (READ "${CMAKE_SOURCE_DIR}/src/core/include/megbrain/version.h" content)
string (REGEX MATCH "MGB_MAJOR +([0-9]+)" _ ${content})
set (MGB_VER_MAJOR ${CMAKE_MATCH_1})
string (REGEX MATCH "MGB_MINOR +([0-9]+)" _ ${content})
set (MGB_VER_MINOR ${CMAKE_MATCH_1})
string (REGEX MATCH "MGB_PATCH *([0-9]+)" _ ${content})
set (MGB_VER_PATCH ${CMAKE_MATCH_1})
if (MGB_FORCE_DEV_VERSION)
set (MGB_IS_DEV 1)
else()
string (REGEX MATCH "MGB_IS_DEV +([01])" _ ${content})
set (MGB_IS_DEV ${CMAKE_MATCH_1})
endif()
set (MGB_VER_STRING "${MGB_VER_MAJOR}.${MGB_VER_MINOR}.${MGB_VER_PATCH}")
if (MGB_IS_DEV)
set (MGB_VER_STRING "${MGB_VER_STRING}-dev")
endif()
message("-- Building MegBrain ${MGB_VER_STRING}")
if (MGE_USE_SYSTEM_LIB)
find_package(dnnl)
if (dnnl_FOUND)
message("Using system provided MKL-DNN.")
set (MGE_USE_SYSTEM_MKLDNN ON)
return()
endif()
endif()
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")
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)
set(MKLLIB libmkl)
else()
option(_DNNL_USE_MKL "" OFF)
endif()
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wno-unused-parameter -Wno-extra")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-unused-parameter -Wno-extra")
set(DNNL_LIBRARY_TYPE STATIC CACHE STRING "config dnnl to STATIC")
add_subdirectory(${PROJECT_SOURCE_DIR}/third_party/intel-mkl-dnn)
@PACKAGE_INIT@
find_package(Flatbuffers REQUIRED)
if (@MGE_WITH_MKLDNN@)
find_package(dnnl REQUIRED)
endif()
list (APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_LIST_DIR})
if (NOT TARGET megengine)
include ("${CMAKE_CURRENT_LIST_DIR}/@MGE_EXPORT_TARGETS@.cmake")
check_required_components("megengine")
endif ()
if (MGE_USE_SYSTEM_LIB)
find_package(OpenBLAS)
set (MGE_USE_SYSTEM_OPENBLAS ON)
message("Using system provided OpenBLAS ${OpenBLAS_VERSION}")
add_library(libopenblas IMPORTED GLOBAL)
set_target_properties(
libopenblas PROPERTIES
IMPORTED_LOCATION ${OpenBLAS_LIBRARIES}
INTERFACE_INCLUDE_DIRECTORIES ${OpenBLAS_INCLUDE_DIRS}
)
return()
endif()
include(ExternalProject)
include(GNUInstallDirs)
......
if (MGE_USE_SYSTEM_LIB)
find_package(FlatBuffers REQUIRED)
find_package(Flatbuffers REQUIRED)
message("Using system provided Flatbuffers ${Flatbuffers_VERSION}")
include(cmake/BuildFlatBuffers.cmake)
return()
endif()
if(MSVC OR WIN32)
......@@ -21,5 +23,5 @@ endif()
option(FLATBUFFERS_BUILD_TESTS "" OFF)
add_subdirectory(${PROJECT_SOURCE_DIR}/third_party/flatbuffers
${CMAKE_CURRENT_BINARY_DIR}/flatbuffers
EXCLUDE_FROM_ALL)
${CMAKE_CURRENT_BINARY_DIR}/flatbuffers)
add_library(flatbuffers::flatbuffers ALIAS flatbuffers)
prefix=@CMAKE_INSTALL_PREFIX@
libdir=@CMAKE_INSTALL_PREFIX@/@CMAKE_INSTALL_LIBDIR@
includedir=@CMAKE_INSTALL_PREFIX@/@CMAKE_INSTALL_INCLUDEDIR@
Name: MegEngine
Description: A deep learning framework
Version: @MGB_VER_STRING@
URL: https://github.com/MegEngine/MegEngine
Cflags: -I${includedir}
Libs: -L${libdir} -lmegengine -pthread
Libs.private: @MGE_PKGCONFIG_LIBS_PRIVATE@
......@@ -42,11 +42,11 @@ if(MGE_MKL_USE_STATIC)
PATHS ${MKL_ROOT_DIR}/lib/${MKL_ARCH_DIR} ${MKL_ROOT_DIR}/lib/)
endif()
add_library(libmkl INTERFACE)
if(APPLE)
target_link_libraries(libmkl INTERFACE ${MKL_CORE_LIBRARY} ${MKL_SEQUENTIAL_LIBRARY} ${MKL_IPL_LIBRARY})
else()
add_library(libmkl INTERFACE IMPORTED)
if(UNIX AND NOT APPLE)
target_link_libraries(libmkl INTERFACE -Wl,--start-group ${MKL_CORE_LIBRARY} ${MKL_SEQUENTIAL_LIBRARY} ${MKL_IPL_LIBRARY} -Wl,--end-group)
else()
target_link_libraries(libmkl INTERFACE ${MKL_CORE_LIBRARY} ${MKL_SEQUENTIAL_LIBRARY} ${MKL_IPL_LIBRARY})
endif()
target_include_directories(libmkl INTERFACE ${MKL_INCLUDE_DIR})
else()
......
list(APPEND OPR_PARAM_DEFS_SRCS ${CMAKE_CURRENT_SOURCE_DIR}/scripts/opr_param_defs.py)
set(OPR_PARAM_DEFS_SCRIPT ${CMAKE_CURRENT_SOURCE_DIR}/scripts/gen_param_defs.py)
......@@ -40,14 +39,22 @@ list(APPEND OPR_PARAM_DEFS_OUTS
list(APPEND OPR_PARAM_DEFS_INC ${OPR_PARAM_DEFS_OUT_DIR})
install(DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/include/megdnn DESTINATION include FILES_MATCHING PATTERN "*.h")
install(DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/include/megdnn DESTINATION ${CMAKE_INSTALL_INCLUDEDIR} FILES_MATCHING PATTERN "*.h")
add_custom_target(_opr_param_defs DEPENDS ${OPR_PARAM_DEFS_OUTS})
add_library(opr_param_defs INTERFACE)
target_include_directories(opr_param_defs INTERFACE ${OPR_PARAM_DEFS_INC})
add_dependencies(opr_param_defs _opr_param_defs)
target_include_directories(opr_param_defs
INTERFACE
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
)
foreach (INCPATH IN LISTS OPR_PARAM_DEFS_INC)
target_include_directories(opr_param_defs
INTERFACE $<BUILD_INTERFACE:${INCPATH}>
)
endforeach()
add_dependencies(opr_param_defs _opr_param_defs)
install(TARGETS opr_param_defs EXPORT ${MGE_EXPORT_TARGETS})
......@@ -56,3 +63,5 @@ if(MGE_WITH_TEST)
endif()
add_subdirectory(src)
# install(TARGETS megdnn opr_param_defs libmkl EXPORT ${MGE_EXPORT_TARGETS})
......@@ -4,3 +4,4 @@ add_library (cuda-stub SHARED ${SOURCES})
set_target_properties(cuda-stub PROPERTIES OUTPUT_NAME cuda)
target_compile_definitions(cuda-stub PRIVATE __CUDA_API_VERSION_INTERNAL)
target_link_libraries(cuda-stub PRIVATE dl -Wl,--no-undefined)
install (TARGETS cuda-stub EXPORT ${MGE_EXPORT_TARGETS})
......@@ -48,11 +48,18 @@ endif()
add_definitions(${LIBMEGDNN_DEF})
add_library(megdnn EXCLUDE_FROM_ALL STATIC ${SOURCES})
add_library(megdnn EXCLUDE_FROM_ALL OBJECT ${SOURCES})
target_link_libraries(megdnn opr_param_defs)
target_include_directories(megdnn PUBLIC ${PROJECT_BINARY_DIR}/genfiles ${PROJECT_SOURCE_DIR}/dnn/include)
target_include_directories(megdnn PRIVATE ${PROJECT_SOURCE_DIR}/dnn ${PROJECT_SOURCE_DIR}/third_party/midout/src)
target_link_libraries(megdnn PUBLIC opr_param_defs)
target_include_directories(megdnn
PUBLIC
$<BUILD_INTERFACE:${PROJECT_BINARY_DIR}/genfiles>
$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/dnn/include>
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
PRIVATE
${PROJECT_SOURCE_DIR}/dnn
${PROJECT_SOURCE_DIR}/third_party/midout/src
)
install(DIRECTORY ${PROJECT_SOURCE_DIR}/dnn/include DESTINATION . FILES_MATCHING PATTERN "*.h*")
......@@ -67,10 +74,20 @@ endif()
target_compile_definitions(megdnn INTERFACE ${LIBMEGDNN_DEF})
if(MGE_WITH_MKLDNN AND ${MGE_ARCH} STREQUAL "x86_64")
target_link_libraries(megdnn dnnl)
if (BUILD_SHARED_LIBS)
target_link_libraries(megdnn PRIVATE $<BUILD_INTERFACE:dnnl>)
else()
target_link_libraries(megdnn PRIVATE dnnl)
endif()
endif()
target_link_libraries(megdnn PRIVATE ${MGE_CUDA_LIBS})
if (BUILD_SHARED_LIBS)
target_link_libraries(megdnn PRIVATE $<BUILD_INTERFACE:${MGE_BLAS_LIBS}>)
else()
target_link_libraries(megdnn PRIVATE ${MGE_BLAS_LIBS})
endif()
target_link_libraries(megdnn ${MGE_CUDA_LIBS})
target_link_libraries(megdnn ${MGE_BLAS_LIBS})
if(CMAKE_THREAD_LIBS_INIT)
target_link_libraries(megdnn Threads::Threads)
target_link_libraries(megdnn PRIVATE Threads::Threads)
endif()
install(TARGETS megdnn EXPORT ${MGE_EXPORT_TARGETS})
......@@ -29,7 +29,7 @@ endif()
add_executable(megdnn_test ${SOURCES})
target_link_libraries(megdnn_test gtest)
target_link_libraries(megdnn_test megdnn)
target_link_libraries(megdnn_test megdnn ${MGE_BLAS_LIBS})
if(UNIX)
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -static-libgcc -static-libstdc++")
......@@ -42,5 +42,3 @@ endif()
if(UNIX)
target_link_libraries(megdnn_test dl rt)
endif()
install(TARGETS megdnn_test RUNTIME DESTINATION test)
cmake_policy(SET CMP0086 NEW)
find_package(PythonLibs ${PYTHON_VERSION_STRING} EXACT REQUIRED)
find_package(Git)
......@@ -5,7 +7,7 @@ if(GIT_FOUND)
message("git found: ${GIT_EXECUTABLE}")
endif()
find_package(Numpy REQUIRED)
find_package(NumPy REQUIRED)
find_package(SWIG REQUIRED)
set(SWIG_SRC src/swig/mgb.i)
......@@ -63,15 +65,19 @@ swig_add_library(mgb LANGUAGE python SOURCES ${SWIG_SRC} ${SRCS})
set(VERSION_SCRIPT ${CMAKE_CURRENT_SOURCE_DIR}/src/version.ld)
add_custom_target(version_ld SOURCES ${VERSION_SCRIPT})
set_target_properties(_mgb PROPERTIES LIBRARY_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/megengine/_internal)
target_link_libraries(_mgb megbrain -Wl,--version-script=${VERSION_SCRIPT})
target_include_directories(_mgb PRIVATE ${PYTHON_INCLUDE_DIRS} src/cpp ${CMAKE_CURRENT_BINARY_DIR} ${NUMPY_INCLUDE_DIR})
target_link_libraries(_mgb ${PYTHON_LIBRARIES})
set_target_properties(mgb PROPERTIES LIBRARY_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/megengine/_internal)
target_link_libraries(mgb megbrain megdnn -Wl,--version-script=${VERSION_SCRIPT})
target_include_directories(mgb PRIVATE ${PYTHON_INCLUDE_DIRS} src/cpp ${CMAKE_CURRENT_BINARY_DIR} ${NUMPY_INCLUDE_DIR})
target_link_libraries(mgb ${PYTHON_LIBRARIES})
if (MGE_WITH_DISTRIBUTED)
target_link_libraries(mgb megray)
endif()
add_dependencies(_mgb mgb_opr_py version_ld)
add_dependencies(mgb mgb_opr_py version_ld)
add_custom_command(
TARGET _mgb POST_BUILD
TARGET mgb POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy ${PROJECT_SOURCE_DIR}/LICENSE ${PROJECT_SOURCE_DIR}/ACKNOWLEDGMENTS ${PROJECT_BINARY_DIR}
COMMAND ${CMAKE_COMMAND} -E copy_directory ${CMAKE_CURRENT_SOURCE_DIR}/megengine ${CMAKE_CURRENT_BINARY_DIR}/megengine
COMMAND ${CMAKE_COMMAND} -E copy_directory ${CMAKE_CURRENT_SOURCE_DIR}/test ${CMAKE_CURRENT_BINARY_DIR}/test
......
......@@ -8,7 +8,9 @@ global:
*mgb::*;
*megdnn::*;
*megcore::*;
megcore*;
};
megcore*;
local:
*;
......
......@@ -2,10 +2,15 @@ include_directories(src)
file (GLOB_RECURSE SOURCES src/*.cpp main.cpp)
add_executable (load_and_run ${SOURCES})
target_link_libraries (load_and_run megbrain)
install (TARGETS load_and_run RUNTIME DESTINATION bin)
if (WIN32)
# Windows does not support implicitly importing data members from DLL.
target_link_libraries(load_and_run megbrain megdnn)
else()
target_link_libraries (load_and_run megengine)
endif()
install (TARGETS load_and_run EXPORT ${MGE_EXPORT_TARGETS} RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})
if(MGE_WITH_TEST)
add_executable(json_loader_test test/json_loader_test.cpp src/json_loader.h src/json_loader.cpp)
target_link_libraries (json_loader_test megbrain)
target_link_libraries (json_loader_test megengine)
endif()
file(GLOB_RECURSE SOURCES core/impl/*.cpp gopt/impl/*.cpp opr/impl/*.cpp plugin/impl/*.cpp serialization/impl/*.cpp core/impl/*.inl gopt/impl/*.inl opr/impl/*.inl plugin/impl/*.inl serialization/impl/*.inl)
if(MGE_WITH_JIT)
......@@ -16,29 +14,36 @@ if(MGE_WITH_DISTRIBUTED)
list(APPEND SOURCES ${GRPC_SRCS})
endif()
set(MGB_INC ${PROJECT_BINARY_DIR}/genfiles core/include gopt/include opr/include plugin/include serialization/include)
set(MGB_INC ${PROJECT_BINARY_DIR}/genfiles ${CMAKE_CURRENT_LIST_DIR}/core/include ${CMAKE_CURRENT_LIST_DIR}/gopt/include ${CMAKE_CURRENT_LIST_DIR}/opr/include ${CMAKE_CURRENT_LIST_DIR}/plugin/include ${CMAKE_CURRENT_LIST_DIR}/serialization/include)
if(MGE_WITH_JIT)
list(APPEND MGB_INC jit/include)
list(APPEND MGB_INC ${CMAKE_CURRENT_LIST_DIR}/jit/include)
if(MGE_WITH_CUDA)
list(APPEND MGB_INC jit/impl/cuda)
list(APPEND MGB_INC ${CMAKE_CURRENT_LIST_DIR}/jit/impl/cuda)
endif()
endif()
if(MGE_WITH_DISTRIBUTED)
list(APPEND MGB_INC opr-mm/include)
list(APPEND MGB_INC ${CMAKE_CURRENT_LIST_DIR}/opr-mm/include)
endif()
if(MGE_WITH_CUDA AND MGE_WITH_TRT)
list(APPEND MGB_INC tensorrt/include)
list(APPEND MGB_INC ${CMAKE_CURRENT_LIST_DIR}/tensorrt/include)
file(GLOB_RECURSE SOURCES_ tensorrt/impl/*.cpp tensorrt/impl/*.inl)
list(APPEND SOURCES ${SOURCES_})
endif()
add_library(megbrain STATIC EXCLUDE_FROM_ALL ${SOURCES})
target_link_libraries(megbrain mgb_opr_param_defs)
target_include_directories(megbrain PUBLIC ${MGB_INC})
add_library(megbrain OBJECT EXCLUDE_FROM_ALL ${SOURCES})
target_link_libraries(megbrain PUBLIC mgb_opr_param_defs)
target_include_directories(megbrain
PUBLIC $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
)
foreach (INCPATH IN LISTS MGB_INC)
target_include_directories(megbrain
PUBLIC $<BUILD_INTERFACE:${INCPATH}>
)
endforeach()
if(MGE_WITH_CUDA)
target_compile_options(megbrain PRIVATE "$<$<COMPILE_LANGUAGE:CUDA>:-Xcompiler=-Wno-unused-parameter>"
......@@ -54,19 +59,19 @@ if(CXX_SUPPORT_WCLASS_MEMACCESS)
target_compile_options(megbrain PRIVATE "-Wno-class-memaccess")
endif()
endif()
target_link_libraries(megbrain megdnn)
target_link_libraries(megbrain PUBLIC megdnn)
if(MGE_WITH_DISTRIBUTED)
add_dependencies(megbrain mgb_proto_target)
target_link_libraries (megbrain libprotobuf libzmq)
target_link_libraries (megbrain PRIVATE libprotobuf libzmq)
set(CPPZMQ_INC ${PROJECT_SOURCE_DIR}/third_party/cppzmq)
# FIXME: add CMAKE_CURRENT_BINARY_DIR for including mm_handler.pb.h
target_include_directories(megbrain PRIVATE ${CPPZMQ_INC} ${CMAKE_CURRENT_BINARY_DIR})
target_link_libraries (megbrain megray)
target_link_libraries (megbrain PRIVATE megray)
endif()
target_link_libraries(megbrain ${MGE_CUDA_LIBS})
target_link_libraries(megbrain PRIVATE ${MGE_CUDA_LIBS})
if(MGE_WITH_JIT AND MGE_WITH_HALIDE)
target_link_libraries(megbrain libhalide)
target_link_libraries(megbrain ${HALIDE_LLVM_LIBS})
target_link_libraries(megbrain PRIVATE libhalide)
target_link_libraries(megbrain PRIVATE ${HALIDE_LLVM_LIBS})
endif()
if (MGB_WITH_FLATBUFFERS)
set (GEN_FLATBUFFERS_SCHEMA_PY ${PROJECT_SOURCE_DIR}/dnn/scripts/gen_flatbuffers_schema.py)
......@@ -113,7 +118,7 @@ if (MGB_WITH_FLATBUFFERS)
add_dependencies(megbrain mgb_serialization_schema_fbs)
target_include_directories(megbrain PRIVATE ${CMAKE_CURRENT_BINARY_DIR}/serialization/include)
target_compile_definitions(megbrain PUBLIC MGB_ENABLE_FBS_SERIALIZATION=1)
target_link_libraries(megbrain flatbuffers)
target_link_libraries(megbrain PUBLIC flatbuffers)
set (GENERATED_FLATBUFFERS_CONVERTER_PATH ${CMAKE_CURRENT_BINARY_DIR}/genfiles)
set (GEN_FLATBUFFERS_CONVERTER_PY ${PROJECT_SOURCE_DIR}/dnn/scripts/gen_flatbuffers_converter.py)
......@@ -139,27 +144,34 @@ if (MGB_WITH_FLATBUFFERS)
target_include_directories(megbrain PRIVATE ${GENERATED_FLATBUFFERS_CONVERTER_PATH})
endif()
if(UNIX AND NOT ANDROID AND NOT APPLE)
target_link_libraries(megbrain dl rt atomic)
target_link_libraries(megbrain PUBLIC dl rt atomic)
endif()
if(ANDROID)
target_link_libraries(megbrain log)
target_link_libraries(megbrain PUBLIC log)
endif()
add_library(megengine SHARED $<TARGET_PROPERTY:megbrain,SOURCES>)
if(MGB_WITH_FLATBUFFERS)
add_dependencies(megengine mgb_serialization_schema_fbs)
# Build as SHARED or STATIC depending on BUILD_SHARED_LIBS=ON/OFF
add_library(megengine)
target_link_libraries(megengine PUBLIC megbrain megdnn)
if (UNIX AND NOT APPLE)
# TODO: Use target_link_options after upgrading to CMake 3.13
target_link_options(megengine PRIVATE -Wl,--no-undefined -Wl,--version-script=${PROJECT_SOURCE_DIR}/python_module/src/version.ld)
endif()
target_include_directories(megengine PRIVATE $<TARGET_PROPERTY:megbrain,INCLUDE_DIRECTORIES>)
target_compile_definitions(megengine PUBLIC $<TARGET_PROPERTY:megbrain,COMPILE_DEFINITIONS>)
target_compile_options(megengine PUBLIC $<TARGET_PROPERTY:megbrain,COMPILE_OPTIONS>)
if(APPLE)
target_link_libraries(megengine PRIVATE $<TARGET_PROPERTY:megbrain,LINK_LIBRARIES>)
set_target_properties(megengine PROPERTIES CXX_VISIBILITY_PRESET default)
set_target_properties(megengine PROPERTIES WINDOWS_EXPORT_ALL_SYMBOLS TRUE)
if (MGE_WITH_DISTRIBUTED)
# Do not export targets if MGE_WITH_DISTRIBUTED is on. MegRay is not ready
# for this.
install(TARGETS megengine
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR})
else()
target_link_libraries(megengine PRIVATE $<TARGET_PROPERTY:megbrain,LINK_LIBRARIES> -Wl,--no-undefined -Wl,--version-script=${PROJECT_SOURCE_DIR}/python_module/src/version.ld)
install(TARGETS megengine megbrain
EXPORT ${MGE_EXPORT_TARGETS}
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR})
endif()
set_target_properties(megengine PROPERTIES CXX_VISIBILITY_PRESET default)
install(TARGETS megengine LIBRARY DESTINATION lib64)
foreach(_PATH ${MGB_INC})
install(DIRECTORY ${_PATH}/megbrain DESTINATION include FILES_MATCHING PATTERN "*.h")
install(DIRECTORY ${_PATH}/megbrain DESTINATION ${CMAKE_INSTALL_INCLUDEDIR} FILES_MATCHING PATTERN "*.h")
endforeach()
......@@ -15,7 +15,9 @@
#define MGB_MINOR 4
#define MGB_PATCH 1
//! whether it is development version
#ifndef MGB_IS_DEV
#define MGB_IS_DEV 0
#endif // MGB_IS_DEV
namespace mgb {
struct Version {
......
......@@ -26,7 +26,8 @@
#cmakedefine01 MGB_ENABLE_JSON
#cmakedefine01 MGB_HAVE_THREAD
#cmakedefine01 MGB_ENABLE_OPR_MM
#cmakedefine01 MEGDNN_ENABLE_MANGLING
#cmakedefine01 MGB_ENABLE_FBS_SERIALIZATION
#cmakedefine01 MGB_IS_DEV
// DNN related flags
// Platform macro's
......@@ -41,7 +42,7 @@
#cmakedefine01 MEGDNN_X86_WITH_MKL_DNN
#cmakedefine01 MEGDNN_ENABLE_RTTI
#cmakedefine01 MEGDNN_ENABLE_LOGGING
#cmakedefine01 MEGDNN_ENABLE_LOGGING
#cmakedefine01 MEGDNN_ENABLE_MANGLING
#cmakedefine01 MEGDNN_ENABLE_EXCEPTIONS
#cmakedefine01 MEGDNN_NAIVE
#cmakedefine01 MEGDNN_X86
......
......@@ -17,7 +17,7 @@ endif()
add_executable(megbrain_test ${SOURCES})
target_link_libraries(megbrain_test gtest)
target_link_libraries(megbrain_test megbrain)
target_link_libraries(megbrain_test megengine)
if(CXX_SUPPORT_WCLASS_MEMACCESS)
if(MGE_WITH_CUDA)
target_compile_options(megbrain_test PRIVATE "$<$<COMPILE_LANGUAGE:CUDA>:-Xcompiler=-Wno-class-memaccess>"
......@@ -31,4 +31,6 @@ if(UNIX)
target_link_libraries(megbrain_test dl rt)
endif()
install(TARGETS megbrain_test RUNTIME DESTINATION test)
if (MGE_WITH_DISTRIBUTED)
target_link_libraries(megbrain_test megray)
endif()
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册