From 187a3e7614a6adba4ced2e14b36c2de1f491fa63 Mon Sep 17 00:00:00 2001 From: Austin Adams Date: Sun, 2 Jun 2024 17:26:19 -0400 Subject: [PATCH 1/2] Build gtest only if we are building tests --- tpls/CMakeLists.txt | 62 +++++++++++++++++++++++---------------------- 1 file changed, 32 insertions(+), 30 deletions(-) diff --git a/tpls/CMakeLists.txt b/tpls/CMakeLists.txt index ebed3400b..02db034d1 100644 --- a/tpls/CMakeLists.txt +++ b/tpls/CMakeLists.txt @@ -49,41 +49,43 @@ if (NOT XACC_USE_EXTERNAL_CPR) add_subdirectory(cpr) endif() -# Googletest -set(BUILD_SHARED_LIBS TRUE) -include(FetchContent) -FetchContent_Declare( - googletest - GIT_REPOSITORY "https://github.com/google/googletest" - GIT_TAG release-1.12.1 -) -set(gtest_force_shared_crt ON CACHE BOOL "" FORCE) -FetchContent_MakeAvailable(googletest) +if(XACC_BUILD_TESTS) + # Googletest + set(BUILD_SHARED_LIBS TRUE) + include(FetchContent) + FetchContent_Declare( + googletest + GIT_REPOSITORY "https://github.com/google/googletest" + GIT_TAG release-1.12.1 + ) + set(gtest_force_shared_crt ON CACHE BOOL "" FORCE) + FetchContent_MakeAvailable(googletest) -if(APPLE) - set_target_properties(gtest PROPERTIES INSTALL_RPATH "@loader_path") - set_target_properties(gtest_main PROPERTIES INSTALL_RPATH "@loader_path") -else() - set_target_properties(gtest PROPERTIES INSTALL_RPATH "$ORIGIN") - set_target_properties(gtest_main PROPERTIES INSTALL_RPATH "$ORIGIN") -endif() + if(APPLE) + set_target_properties(gtest PROPERTIES INSTALL_RPATH "@loader_path") + set_target_properties(gtest_main PROPERTIES INSTALL_RPATH "@loader_path") + else() + set_target_properties(gtest PROPERTIES INSTALL_RPATH "$ORIGIN") + set_target_properties(gtest_main PROPERTIES INSTALL_RPATH "$ORIGIN") + endif() -set(GTEST_FOUND TRUE) -set(GTEST_LIBRARIES gtest) -set(GTEST_MAIN_LIBRARIES gtest_main) -set(GTEST_BOTH_LIBRARIES gtest gtest_main) -set(GTEST_INCLUDE_DIRS ${googletest_SOURCE_DIR}/googletest/include) + set(GTEST_FOUND TRUE) + set(GTEST_LIBRARIES gtest) + set(GTEST_MAIN_LIBRARIES gtest_main) + set(GTEST_BOTH_LIBRARIES gtest gtest_main) + set(GTEST_INCLUDE_DIRS ${googletest_SOURCE_DIR}/googletest/include) -set_property(TARGET gtest PROPERTY FOLDER "tests/gtest") -set_property(TARGET gtest_main PROPERTY FOLDER "tests/gtest") + set_property(TARGET gtest PROPERTY FOLDER "tests/gtest") + set_property(TARGET gtest_main PROPERTY FOLDER "tests/gtest") -set_cache_variable(GTEST_FOUND "Set if libgtest was found or built") -set_cache_variable(GTEST_LIBRARIES "Location of libgtest") -set_cache_variable(GTEST_MAIN_LIBRARIES "Location of libgtest-main") -set_cache_variable(GTEST_BOTH_LIBRARIES "Location of both gtest libraries") -set_cache_variable(GTEST_INCLUDE_DIRS "Location of gtest include files") + set_cache_variable(GTEST_FOUND "Set if libgtest was found or built") + set_cache_variable(GTEST_LIBRARIES "Location of libgtest") + set_cache_variable(GTEST_MAIN_LIBRARIES "Location of libgtest-main") + set_cache_variable(GTEST_BOTH_LIBRARIES "Location of both gtest libraries") + set_cache_variable(GTEST_INCLUDE_DIRS "Location of gtest include files") -install(DIRECTORY "${googletest_SOURCE_DIR}/googletest/include/gtest" DESTINATION include/gtest ) + install(DIRECTORY "${googletest_SOURCE_DIR}/googletest/include/gtest" DESTINATION include/gtest ) +endif() set(BUILD_SHARED_LIBS FALSE) set(BOOST_LIBS_OPTIONAL graph CACHE STRING "" FORCE) From 5329e7894b49fe33a9e7706144070ef3c903b03d Mon Sep 17 00:00:00 2001 From: Austin Adams Date: Sun, 2 Jun 2024 17:27:04 -0400 Subject: [PATCH 2/2] Allow saying find_package(XACC 1.0.0) This creates a version file so that you can specify a version for find_package(XACC), like find_package(XACC 1.0.0). For more info, see: https://cmake.org/cmake/help/latest/command/find_package.html#config-mode-version-selection --- CMakeLists.txt | 2 ++ cmake/xacc-config-version.cmake.in | 8 ++++++++ cmake/xacc-config.cmake.in | 4 ---- 3 files changed, 10 insertions(+), 4 deletions(-) create mode 100644 cmake/xacc-config-version.cmake.in diff --git a/CMakeLists.txt b/CMakeLists.txt index 0431ea841..bd242ee00 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -244,7 +244,9 @@ set(PATCH_VERSION 0) # their CMake builds, and pass -DXACC_DIR=/path/to/install/xacc # and CMake loads include paths, libs, etc configure_file("${CMAKE_SOURCE_DIR}/cmake/xacc-config.cmake.in" "${CMAKE_BINARY_DIR}/xacc-config.cmake" @ONLY) +configure_file("${CMAKE_SOURCE_DIR}/cmake/xacc-config-version.cmake.in" "${CMAKE_BINARY_DIR}/xacc-config-version.cmake" @ONLY) install(FILES "${CMAKE_BINARY_DIR}/xacc-config.cmake" DESTINATION .) +install(FILES "${CMAKE_BINARY_DIR}/xacc-config-version.cmake" DESTINATION .) install(FILES "${CMAKE_SOURCE_DIR}/cmake/Modules/format.cmake" DESTINATION share/xacc/) install(FILES "${CMAKE_SOURCE_DIR}/tpls/mpark-variant/variant.hpp" DESTINATION include/xacc/) install(FILES "${CMAKE_SOURCE_DIR}/tpls/taocpp/operators.hpp" DESTINATION include/xacc/) diff --git a/cmake/xacc-config-version.cmake.in b/cmake/xacc-config-version.cmake.in new file mode 100644 index 000000000..f5e0851f8 --- /dev/null +++ b/cmake/xacc-config-version.cmake.in @@ -0,0 +1,8 @@ +set(PACKAGE_VERSION "@MAJOR_VERSION@.@MINOR_VERSION@.@PATCH_VERSION@") +# The quotes for the right operand are intentional here: if CMake executes this +# file without ${PACKAGE_FIND_VERSION} set, then if() will still get three +# operands. Without the quotes, if() will only get two operands in that case, +# throwing an error. +if(${PACKAGE_VERSION} VERSION_GREATER_EQUAL "${PACKAGE_FIND_VERSION}") + set(PACKAGE_VERSION_COMPATIBLE 1) +endif() diff --git a/cmake/xacc-config.cmake.in b/cmake/xacc-config.cmake.in index d09ed19d1..7bd065752 100644 --- a/cmake/xacc-config.cmake.in +++ b/cmake/xacc-config.cmake.in @@ -20,11 +20,7 @@ if(NOT TARGET format) endif() # Set version info -set(XACC_VERSION_MAJOR @MAJOR_VERSION@) -set(XACC_VERSION_MINOR @MINOR_VERSION@) -set(XACC_VERSION_PATCH @PATCH_VERSION@) set(XACC_VERSION_SUFFIX @XACC_BUILD_VERSION@) -set(XACC_VERSION "@MAJOR_VERSION@.@MINOR_VERSION@.@PATCH_VERSION@-@XACC_BUILD_VERSION@") set(XACC_LIBRARY_DIR "${XACC_ROOT}/lib")