Skip to content

Commit

Permalink
cmake: introduce USE_SYSTEM_* flags and support find_package(fbgemm)
Browse files Browse the repository at this point in the history
  • Loading branch information
Connor Baker committed Aug 22, 2023
1 parent d43717a commit 87b615c
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 58 deletions.
99 changes: 62 additions & 37 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand All @@ -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 $<BUILD_INTERFACE:${FBGEMM_SOURCE_DIR}>
PUBLIC $<BUILD_INTERFACE:${FBGEMM_SOURCE_DIR}/include>
PRIVATE "${ASMJIT_SRC_DIR}/src"
PRIVATE "${CPUINFO_SOURCE_DIR}/include")

target_include_directories(fbgemm_avx2 BEFORE
PUBLIC $<BUILD_INTERFACE:${FBGEMM_SOURCE_DIR}>
PUBLIC $<BUILD_INTERFACE:${FBGEMM_SOURCE_DIR}/include>
PRIVATE "${ASMJIT_SRC_DIR}/src"
PRIVATE "${CPUINFO_SOURCE_DIR}/include")
target_include_directories(fbgemm_generic
PUBLIC $<BUILD_INTERFACE:${FBGEMM_SOURCE_DIR}/include>)
target_include_directories(fbgemm_avx2
PUBLIC $<BUILD_INTERFACE:${FBGEMM_SOURCE_DIR}/include>)
target_include_directories(fbgemm_avx512
PUBLIC $<BUILD_INTERFACE:${FBGEMM_SOURCE_DIR}/include>)

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 $<BUILD_INTERFACE:${FBGEMM_SOURCE_DIR}>
PUBLIC $<BUILD_INTERFACE:${FBGEMM_SOURCE_DIR}/include>
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
Expand Down Expand Up @@ -322,41 +346,45 @@ else()
message(FATAL_ERROR "Unsupported library type ${FBGEMM_LIBRARY_TYPE}")
endif()

target_include_directories(fbgemm
PUBLIC $<BUILD_INTERFACE:${FBGEMM_SOURCE_DIR}/include>)
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 $<BUILD_INTERFACE:${FBGEMM_SOURCE_DIR}>
PUBLIC $<BUILD_INTERFACE:${FBGEMM_SOURCE_DIR}/include>)

target_link_libraries(fbgemm
$<BUILD_INTERFACE:asmjit>
$<BUILD_INTERFACE:cpuinfo>)
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(
FILES ${FBGEMM_PUBLIC_HEADERS}
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")
Expand All @@ -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()
Expand Down
4 changes: 4 additions & 0 deletions bench/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ macro(add_benchmark BENCHNAME)
BenchUtils.cc
../test/QuantizationHelpers.cc
../test/EmbeddingSpMDMTestUtils.cc)
# Ensure that we can include header files from the src and test directory.
target_include_directories(${BENCHNAME}
PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/../src
PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/../test)
target_compile_options(${BENCHNAME} PRIVATE
"-m64" "-mavx2" "-mfma" "-masm=intel")
target_link_libraries(${BENCHNAME} fbgemm)
Expand Down
9 changes: 9 additions & 0 deletions cmake/fbgemm-config.cmake.in
Original file line number Diff line number Diff line change
@@ -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)
53 changes: 32 additions & 21 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand All @@ -52,6 +56,10 @@ macro(add_gtest TESTNAME)
EmbeddingSpMDMTestUtils.cc
QuantizationHelpers.cc
TestUtils.cc)
# Ensure that we can include header files from the bench and src directory.
target_include_directories(${TESTNAME}
PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/../bench
PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/../src)
# To compile test files with AVX2 turned on
# For static build, defining FBGEMM_STATIC to avoid generating
# functions with _dllimport attributes.
Expand All @@ -78,7 +86,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()
Expand Down

0 comments on commit 87b615c

Please sign in to comment.