Skip to content

Commit

Permalink
♻️ switch to FetchContent for mqt-core
Browse files Browse the repository at this point in the history
Signed-off-by: burgholzer <[email protected]>
  • Loading branch information
burgholzer committed Jan 29, 2024
1 parent df42459 commit 3a2cdb2
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 87 deletions.
13 changes: 0 additions & 13 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,6 @@ project(
LANGUAGES CXX
DESCRIPTION "MQT QCEC - A tool for Quantum Circuit Equivalence Checking")

# check whether `modulename` is correctly cloned in the `extern` directory.
macro(CHECK_SUBMODULE_PRESENT modulename)
if(NOT EXISTS "${PROJECT_SOURCE_DIR}/extern/${modulename}/CMakeLists.txt")
message(
FATAL_ERROR
"${modulename} submodule not cloned properly. \
Please run `git submodule update --init --recursive` \
from the main project directory")
endif()
endmacro()

check_submodule_present(mqt-core)

option(BUILD_MQT_QCEC_TESTS "Also build tests for the MQT QCEC project" ON)
option(BUILD_MQT_QCEC_BINDINGS "Build the MQT QCEC Python bindings" OFF)

Expand Down
142 changes: 75 additions & 67 deletions cmake/ExternalDependencies.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -3,80 +3,88 @@
include(FetchContent)
set(FETCH_PACKAGES "")

# A macro to declare a dependency that takes into account the different CMake
# versions and the features that they make available. In particular: - CMake
# 3.24 introduced the `FIND_PACKAGE_ARGS` option to `FetchContent` which allows
# to combine `FetchContent_Declare` and `find_package` in a single call. - CMake
# 3.25 introduced the `SYSTEM` option to `FetchContent_Declare` which marks the
# dependency as a system dependency. This is useful to avoid compiler warnings
# from external header only libraries. - CMake 3.28 introduced the
# `EXCLUDE_FROM_ALL` option to `FetchContent_Declare` which allows to exclude
# all targets from the dependency from the `all` target.
macro(DECLARE_DEPENDENCY)
cmake_parse_arguments(DEPENDENCY "SYSTEM;EXCLUDE_FROM_ALL"
"NAME;URL;MD5;MIN_VERSION;ALT_NAME" "" ${ARGN})
set(ADDITIONAL_OPTIONS "")
if(DEPENDENCY_SYSTEM AND CMAKE_VERSION VERSION_GREATER_EQUAL 3.25)
list(APPEND ADDITIONAL_OPTIONS SYSTEM)
endif()
if(DEPENDENCY_EXCLUDE_FROM_ALL AND CMAKE_VERSION VERSION_GREATER_EQUAL 3.28)
list(APPEND ADDITIONAL_OPTIONS EXCLUDE_FROM_ALL)
if(BUILD_MQT_QCEC_BINDINGS)
if(NOT SKBUILD)
# Manually detect the installed pybind11 package.
execute_process(
COMMAND "${Python_EXECUTABLE}" -m pybind11 --cmakedir
OUTPUT_STRIP_TRAILING_WHITESPACE
OUTPUT_VARIABLE pybind11_DIR)

# Add the detected directory to the CMake prefix path.
list(APPEND CMAKE_PREFIX_PATH "${pybind11_DIR}")
endif()
if(CMAKE_VERSION VERSION_GREATER_EQUAL 3.28)
FetchContent_Declare(
${DEPENDENCY_NAME}
URL ${DEPENDENCY_URL}
URL_MD5 ${DEPENDENCY_MD5}
${ADDITIONAL_OPTIONS} FIND_PACKAGE_ARGS ${DEPENDENCY_MIN_VERSION} NAMES
${DEPENDENCY_ALT_NAME})
list(APPEND FETCH_PACKAGES ${DEPENDENCY_NAME})
elseif(CMAKE_VERSION VERSION_GREATER_EQUAL 3.25)
FetchContent_Declare(
${DEPENDENCY_NAME}
URL ${DEPENDENCY_URL}
URL_MD5 ${DEPENDENCY_MD5}
${ADDITIONAL_OPTIONS} FIND_PACKAGE_ARGS ${DEPENDENCY_MIN_VERSION} NAMES
${DEPENDENCY_ALT_NAME})
list(APPEND FETCH_PACKAGES ${DEPENDENCY_NAME})
elseif(CMAKE_VERSION VERSION_GREATER_EQUAL 3.24)

# add pybind11 library
find_package(pybind11 CONFIG REQUIRED)
endif()

set(FETCHCONTENT_SOURCE_DIR_MQT-CORE
${PROJECT_SOURCE_DIR}/extern/mqt-core
CACHE
PATH
"Path to the source directory of the mqt-core library. This variable is used by FetchContent to download the library if it is not already available."
)
set(MQT_CORE_VERSION
2.2.2
CACHE STRING "MQT Core version")
if(CMAKE_VERSION VERSION_GREATER_EQUAL 3.24)
FetchContent_Declare(
mqt-core
GIT_REPOSITORY https://github.com/cda-tum/mqt-core.git
GIT_TAG v${MQT_CORE_VERSION}
FIND_PACKAGE_ARGS ${MQT_CORE_VERSION})
list(APPEND FETCH_PACKAGES mqt-core)
else()
find_package(mqt-core ${MQT_CORE_VERSION} QUIET)
if(NOT mqt-core_FOUND)
FetchContent_Declare(
${DEPENDENCY_NAME}
URL ${DEPENDENCY_URL}
URL_MD5 ${DEPENDENCY_MD5}
${ADDITIONAL_OPTIONS} FIND_PACKAGE_ARGS ${DEPENDENCY_MIN_VERSION} NAMES
${DEPENDENCY_ALT_NAME})
list(APPEND FETCH_PACKAGES ${DEPENDENCY_NAME})
else()
# try to get the system installed version
find_package(${DEPENDENCY_NAME} ${DEPENDENCY_MIN_VERSION} QUIET NAMES
${DEPENDENCY_ALT_NAME})
if(NOT ${DEPENDENCY_NAME}_FOUND)
FetchContent_Declare(
${DEPENDENCY_NAME}
URL ${DEPENDENCY_URL}
URL_MD5 ${DEPENDENCY_MD5})
list(APPEND FETCH_PACKAGES ${DEPENDENCY_NAME})
endif()
mqt-core
GIT_REPOSITORY https://github.com/cda-tum/mqt-core.git
GIT_TAG v${MQT_CORE_VERSION})
list(APPEND FETCH_PACKAGES mqt-core)
endif()
endmacro()
endif()

if(BUILD_MQT_QCEC_TESTS)
set(gtest_force_shared_crt # cmake-lint: disable=C0103
set(gtest_force_shared_crt
ON
CACHE BOOL "" FORCE)
declare_dependency(
NAME
googletest
URL
https://github.com/google/googletest/archive/refs/tags/v1.14.0.tar.gz
MD5
c8340a482851ef6a3fe618a082304cfc
MIN_VERSION
1.14.0
ALT_NAME
GTest
SYSTEM
EXCLUDE_FROM_ALL)
set(GTEST_VERSION
1.14.0
CACHE STRING "Google Test version")
set(GTEST_URL
https://github.com/google/googletest/archive/refs/tags/v${GTEST_VERSION}.tar.gz
)
if(CMAKE_VERSION VERSION_GREATER_EQUAL 3.24)
FetchContent_Declare(googletest URL ${GTEST_URL} FIND_PACKAGE_ARGS
${GTEST_VERSION} NAMES GTest)
list(APPEND FETCH_PACKAGES googletest)
else()
find_package(googletest ${GTEST_VERSION} QUIET NAMES GTest)
if(NOT googletest_FOUND)
FetchContent_Declare(googletest URL ${GTEST_URL})
list(APPEND FETCH_PACKAGES googletest)
endif()
endif()
endif()

if(BUILD_MQT_QCEC_BINDINGS)
# add pybind11_json library
if(CMAKE_VERSION VERSION_GREATER_EQUAL 3.24)
FetchContent_Declare(
pybind11_json
GIT_REPOSITORY https://github.com/pybind/pybind11_json
FIND_PACKAGE_ARGS)
list(APPEND FETCH_PACKAGES pybind11_json)
else()
find_package(pybind11_json QUIET)
if(NOT pybind11_json_FOUND)
FetchContent_Declare(
pybind11_json GIT_REPOSITORY https://github.com/pybind/pybind11_json)
list(APPEND FETCH_PACKAGES pybind11_json)
endif()
endif()
endif()

# Make all declared dependencies available.
Expand Down
7 changes: 0 additions & 7 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,10 +1,3 @@
# add MQT::Core target
set(BUILD_MQT_CORE_TESTS
OFF
CACHE BOOL "Build MQT Core tests")
add_subdirectory("${PROJECT_SOURCE_DIR}/extern/mqt-core" "extern/mqt-core"
EXCLUDE_FROM_ALL)

add_library(
${PROJECT_NAME}
${PROJECT_SOURCE_DIR}/include/checker
Expand Down

0 comments on commit 3a2cdb2

Please sign in to comment.