diff --git a/CMakeLists.txt b/CMakeLists.txt index ab980ebeb3..a5b0153e71 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -95,6 +95,15 @@ option(FBGEMM_BUILD_TESTS "Build fbgemm unit tests" ON) option(FBGEMM_BUILD_BENCHMARKS "Build fbgemm benchmarks" ON) option(FBGEMM_BUILD_DOCS "Build fbgemm documentation" OFF) option(FBGEMM_BUILD_FBGEMM_GPU "Build fbgemm_gpu library" OFF) +option(USE_SYSTEM_LIBS "Use system libraries. Does not affect fbgemm_gpu" OFF) +option(USE_SYSTEM_ASMJIT "Use system asmjit. Does not affect fbgemm_gpu" ${USE_SYSTEM_LIBS}) +option(USE_SYSTEM_CPUINFO "Use system cpuinfo. Does not affect fbgemm_gpu" ${USE_SYSTEM_LIBS}) +option(USE_SYSTEM_GOOGLETEST "Use system Google Test. Does not affect fbgemm_gpu" ${USE_SYSTEM_LIBS}) + +# If any of the USE_SYSTEM_* options are set, FBGEMM_BUILD_FBGEMM_GPU must be false. +if((USE_SYSTEM_LIBS OR USE_SYSTEM_ASMJIT OR USE_SYSTEM_CPUINFO OR USE_SYSTEM_GOOGLETEST) AND FBGEMM_BUILD_FBGEMM_GPU) + message(FATAL_ERROR "USE_SYSTEM_* options are not supported for fbgemm_gpu") +endif() if(FBGEMM_BUILD_TESTS) enable_testing() @@ -227,9 +236,15 @@ message(WARNING "==========") message(WARNING "CMAKE_BUILD_TYPE = ${CMAKE_BUILD_TYPE}") message(WARNING "CMAKE_CXX_FLAGS_DEBUG is ${CMAKE_CXX_FLAGS_DEBUG}") message(WARNING "CMAKE_CXX_FLAGS_RELEASE is ${CMAKE_CXX_FLAGS_RELEASE}") +message(WARNING "USE_SYSTEM_LIBS is ${USE_SYSTEM_LIBS}") +message(WARNING "USE_SYSTEM_ASMJIT is ${USE_SYSTEM_ASMJIT}") +message(WARNING "USE_SYSTEM_CPUINFO is ${USE_SYSTEM_CPUINFO}") +message(WARNING "USE_SYSTEM_GOOGLETEST is ${USE_SYSTEM_GOOGLETEST}") message(WARNING "==========") -if(NOT TARGET asmjit) +if(USE_SYSTEM_ASMJIT) + find_package(asmjit REQUIRED) +elseif(NOT TARGET asmjit) #Download asmjit from github if ASMJIT_SRC_DIR is not specified. if(NOT DEFINED ASMJIT_SRC_DIR) set(ASMJIT_SRC_DIR "${FBGEMM_SOURCE_DIR}/third_party/asmjit" @@ -258,7 +273,9 @@ if(NOT TARGET asmjit) endif() endif() -if(NOT TARGET cpuinfo) +if(USE_SYSTEM_CPUINFO) + find_package(cpuinfo REQUIRED) +elseif(NOT TARGET cpuinfo) #Download cpuinfo from github if CPUINFO_SOURCE_DIR is not specified. if(NOT DEFINED CPUINFO_SOURCE_DIR) set(CPUINFO_SOURCE_DIR "${FBGEMM_SOURCE_DIR}/third_party/cpuinfo" @@ -276,23 +293,30 @@ if(NOT TARGET cpuinfo) set_property(TARGET cpuinfo PROPERTY POSITION_INDEPENDENT_CODE ON) endif() -target_include_directories(fbgemm_generic BEFORE - PUBLIC $ - PUBLIC $ - PRIVATE "${ASMJIT_SRC_DIR}/src" - PRIVATE "${CPUINFO_SOURCE_DIR}/include") - -target_include_directories(fbgemm_avx2 BEFORE - PUBLIC $ - PUBLIC $ - PRIVATE "${ASMJIT_SRC_DIR}/src" - PRIVATE "${CPUINFO_SOURCE_DIR}/include") +target_include_directories(fbgemm_generic + PUBLIC $) +target_include_directories(fbgemm_avx2 + PUBLIC $) +target_include_directories(fbgemm_avx512 + PUBLIC $) + +if(NOT USE_SYSTEM_ASMJIT) + target_include_directories(fbgemm_generic BEFORE + PRIVATE "${ASMJIT_SRC_DIR}/src") + target_include_directories(fbgemm_avx2 BEFORE + PRIVATE "${ASMJIT_SRC_DIR}/src") + target_include_directories(fbgemm_avx512 BEFORE + PRIVATE "${ASMJIT_SRC_DIR}/src") +endif() -target_include_directories(fbgemm_avx512 BEFORE - PUBLIC $ - PUBLIC $ - PRIVATE "${ASMJIT_SRC_DIR}/src" - PRIVATE "${CPUINFO_SOURCE_DIR}/include") +if(NOT USE_SYSTEM_CPUINFO) + target_include_directories(fbgemm_generic BEFORE + PRIVATE "${CPUINFO_SOURCE_DIR}/include") + target_include_directories(fbgemm_avx2 BEFORE + PRIVATE "${CPUINFO_SOURCE_DIR}/include") + target_include_directories(fbgemm_avx512 BEFORE + PRIVATE "${CPUINFO_SOURCE_DIR}/include") +endif() if(FBGEMM_LIBRARY_TYPE STREQUAL "default") add_library(fbgemm @@ -322,31 +346,35 @@ else() message(FATAL_ERROR "Unsupported library type ${FBGEMM_LIBRARY_TYPE}") endif() +target_include_directories(fbgemm + PUBLIC $) +set_target_properties(fbgemm PROPERTIES PUBLIC_HEADER "${FBGEMM_PUBLIC_HEADERS}") + if(USE_SANITIZER) target_link_options(fbgemm PRIVATE "-fsanitize=${USE_SANITIZER}" "-fno-omit-frame-pointer") endif() -target_include_directories(fbgemm BEFORE - PUBLIC $ - PUBLIC $) - -target_link_libraries(fbgemm - $ - $) -add_dependencies(fbgemm - asmjit - cpuinfo) +target_link_libraries(fbgemm PUBLIC asmjit cpuinfo) if(OpenMP_FOUND) - target_link_libraries(fbgemm OpenMP::OpenMP_CXX) + target_link_libraries(fbgemm PUBLIC OpenMP::OpenMP_CXX) endif() +include(CMakePackageConfigHelpers) +configure_package_config_file( + ${CMAKE_CURRENT_SOURCE_DIR}/cmake/${CMAKE_PROJECT_NAME}-config.cmake.in + ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_PROJECT_NAME}-config.cmake + INSTALL_DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/${CMAKE_PROJECT_NAME} + PATH_VARS OpenMP_FOUND +) + install( - TARGETS fbgemm - EXPORT fbgemmLibraryConfig + TARGETS ${CMAKE_PROJECT_NAME} + EXPORT ${CMAKE_PROJECT_NAME}Targets ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} + PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/${CMAKE_PROJECT_NAME} RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}) #For windows install( @@ -354,9 +382,9 @@ install( DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/fbgemm") install( - EXPORT fbgemmLibraryConfig - DESTINATION share/cmake/fbgemm - FILE fbgemmLibraryConfig.cmake) + EXPORT ${CMAKE_PROJECT_NAME}Targets + FILE ${CMAKE_PROJECT_NAME}-config.cmake + DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/${CMAKE_PROJECT_NAME}) if(MSVC) if(FBGEMM_LIBRARY_TYPE STREQUAL "shared") @@ -368,9 +396,6 @@ if(MSVC) install(TARGETS asmjit DESTINATION ${CMAKE_INSTALL_LIBDIR}) endif() -#Make project importable from the build directory -#export(TARGETS fbgemm asmjit FILE fbgemmLibraryConfig.cmake) - if(FBGEMM_BUILD_TESTS) add_subdirectory(test) endif() diff --git a/bench/CMakeLists.txt b/bench/CMakeLists.txt index 8b07789428..9e7a1f9e54 100644 --- a/bench/CMakeLists.txt +++ b/bench/CMakeLists.txt @@ -41,6 +41,8 @@ macro(add_benchmark BENCHNAME) BenchUtils.cc ../test/QuantizationHelpers.cc ../test/EmbeddingSpMDMTestUtils.cc) + # Ensure that we can include header files from the root directory. + target_include_directories(${BENCHNAME} PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/..) target_compile_options(${BENCHNAME} PRIVATE "-m64" "-mavx2" "-mfma" "-masm=intel") target_link_libraries(${BENCHNAME} fbgemm) diff --git a/cmake/fbgemm-config.cmake.in b/cmake/fbgemm-config.cmake.in new file mode 100644 index 0000000000..5d012726d7 --- /dev/null +++ b/cmake/fbgemm-config.cmake.in @@ -0,0 +1,9 @@ +@PACKAGE_INIT@ +include(CMakeFindDependencyMacro) +find_dependency(asmjit REQUIRED) +find_dependency(cpuinfo REQUIRED) +if(@OpenMP_FOUND@) + find_dependency(OpenMP REQUIRED) +endif() +include("${CMAKE_CURRENT_LIST_DIR}/fbgemmTargets.cmake") +check_required_components(fbgemm) diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 2cffddf608..a6a1a3e0ff 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -14,27 +14,31 @@ set(CMAKE_C_STANDARD 11) set(CMAKE_C_EXTENSIONS OFF) set(CMAKE_C_STANDARD_REQUIRED ON) -if(FBGEMM_BUILD_TESTS AND NOT TARGET gtest) - #Download Googletest framework from github if - #GOOGLETEST_SOURCE_DIR is not specified. - if(NOT DEFINED GOOGLETEST_SOURCE_DIR) - set(GOOGLETEST_SOURCE_DIR "${FBGEMM_SOURCE_DIR}/third_party/googletest" - CACHE STRING "googletest source directory from submodules") - endif() +if(USE_SYSTEM_GOOGLETEST) + find_package(GTest REQUIRED) +else() + if(FBGEMM_BUILD_TESTS AND NOT TARGET gtest) + #Download Googletest framework from github if + #GOOGLETEST_SOURCE_DIR is not specified. + if(NOT DEFINED GOOGLETEST_SOURCE_DIR) + set(GOOGLETEST_SOURCE_DIR "${FBGEMM_SOURCE_DIR}/third_party/googletest" + CACHE STRING "googletest source directory from submodules") + endif() - #build Googletest framework - #MSVC needs gtest_for_shared_crt to select right runtime lib - if (MSVC AND FBGEMM_LIBRARY_TYPE STREQUAL "shared") - message(WARNING "gtest_force_shared_crt is ON") - set(gtest_force_shared_crt ON CACHE BOOL "" FORCE) - else() - message(WARNING "gtest_force_shared_crt is OFF") - endif() - add_subdirectory("${GOOGLETEST_SOURCE_DIR}" "${FBGEMM_BINARY_DIR}/googletest") - # add flags required for mac build - if(NOT MSVC) - target_compile_options(gtest PRIVATE "-Wno-deprecated-copy") - target_compile_options(gmock PRIVATE "-Wno-deprecated-copy") + #build Googletest framework + #MSVC needs gtest_for_shared_crt to select right runtime lib + if (MSVC AND FBGEMM_LIBRARY_TYPE STREQUAL "shared") + message(WARNING "gtest_force_shared_crt is ON") + set(gtest_force_shared_crt ON CACHE BOOL "" FORCE) + else() + message(WARNING "gtest_force_shared_crt is OFF") + endif() + add_subdirectory("${GOOGLETEST_SOURCE_DIR}" "${FBGEMM_BINARY_DIR}/googletest") + # add flags required for mac build + if(NOT MSVC) + target_compile_options(gtest PRIVATE "-Wno-deprecated-copy") + target_compile_options(gmock PRIVATE "-Wno-deprecated-copy") + endif() endif() endif() @@ -52,6 +56,8 @@ macro(add_gtest TESTNAME) EmbeddingSpMDMTestUtils.cc QuantizationHelpers.cc TestUtils.cc) + # Ensure that we can include header files from the root directory. + target_include_directories(${TESTNAME} PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/..) # To compile test files with AVX2 turned on # For static build, defining FBGEMM_STATIC to avoid generating # functions with _dllimport attributes. @@ -78,7 +84,10 @@ macro(add_gtest TESTNAME) target_link_libraries(${TESTNAME} ${OpenMP_CXX_LIBRARIES}) endif() - add_dependencies(${TESTNAME} gtest fbgemm) + if(NOT USE_SYSTEM_GOOGLETEST) + add_dependencies(${TESTNAME} gtest gmock) + endif() + add_dependencies(${TESTNAME} fbgemm) add_test(${TESTNAME} ${TESTNAME}) set_target_properties(${TESTNAME} PROPERTIES FOLDER test) endmacro()