From c3eb5e6a6292b90cfe35ee20a311b049c1cc4dfa Mon Sep 17 00:00:00 2001 From: Derek Christ Date: Mon, 18 Nov 2024 17:22:03 +0100 Subject: [PATCH] Hide the use of FetchContent behind a flag FetchContent is now disabled by default, when the project is included as an subproject by another top-level project. Also, every usage of FetchContent is behind a separate flag to enable and disable the usage with granular control. --- CMakeLists.txt | 94 ++++++++++++++---------- CMakePresets.json | 4 +- lib/CMakeLists.txt | 1 - lib/sqlite3/CMakeLists.txt | 11 +-- src/traceAnalyzer/CMakeLists.txt | 30 +++++--- tests/CMakeLists.txt | 20 +++-- tests/tests_configuration/CMakeLists.txt | 1 + tests/tests_dramsys/CMakeLists.txt | 1 + tests/tests_simulator/CMakeLists.txt | 1 + 9 files changed, 100 insertions(+), 63 deletions(-) delete mode 100644 lib/CMakeLists.txt diff --git a/CMakeLists.txt b/CMakeLists.txt index d9013ed1..7104bccc 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -47,9 +47,14 @@ if(POLICY CMP0135) endif() list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/cmake") -include(FetchContent) include(enable_extensions) +if (PROJECT_IS_TOP_LEVEL) + set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin) + set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib) + set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib) +endif() + if(DRAMSYS_ENABLE_COVERAGE) include(coverage) endif() @@ -57,9 +62,18 @@ endif() ### Project settings ### option(DRAMSYS_BUILD_TESTS "Build DRAMSys unit tests" OFF) option(DRAMSYS_BUILD_BENCHMARKS "Build DRAMSys benchmarks" OFF) -option(DRAMSYS_BUILD_CLI "Build DRAMSys Command Line Tool" ON) +option(DRAMSYS_BUILD_CLI "Build DRAMSys Command Line Tool" ${PROJECT_IS_TOP_LEVEL}) option(DRAMSYS_BUILD_TRACE_ANALYZER "Build DRAMSys Trace Analyzer" OFF) -option(DRAMSYS_WITH_DRAMPOWER "Build with DRAMPower support enabled." ON) +option(DRAMSYS_WITH_DRAMPOWER "Build with DRAMPower support enabled." OFF) + +# Use sane defaults for FetchContent: +# In case we are the top-level project, get everything by default +# In case we are included in another project, the user might want to provide their own system dependencies +option(DRAMSYS_USE_FETCH_CONTENT "Enable the FetchContent module" ${PROJECT_IS_TOP_LEVEL}) +option(DRAMSYS_USE_FETCH_CONTENT_INTERNAL "Enable FetchContent to provide internal dependencies" ${DRAMSYS_USE_FETCH_CONTENT}) +option(DRAMSYS_USE_FETCH_CONTENT_SYSTEMC "Enable FetchContent to provide SystemC" ${DRAMSYS_USE_FETCH_CONTENT}) +option(DRAMSYS_USE_FETCH_CONTENT_SQLITE3 "Enable FetchContent to provide SQLite3" ${DRAMSYS_USE_FETCH_CONTENT}) +option(DRAMSYS_USE_FETCH_CONTENT_NLOHMANN_JSON "Enable FetchContent to provide nlohmann_json" ${DRAMSYS_USE_FETCH_CONTENT}) # TODO: Remove as soon as DRAMPower is updated set(DRAMSYS_SHARED_LIBS OFF) @@ -68,12 +82,6 @@ if(DEFINED DRAMSYS_SHARED_LIBS) set(BUILD_SHARED_LIBS ${DRAMSYS_SHARED_LIBS}) endif() -if (PROJECT_IS_TOP_LEVEL) - set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin) - set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib) - set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib) -endif() - ### DRAMSys directories ### set(DRAMSYS_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/src") set(DRAMSYS_LIBRARY_DIR "${CMAKE_CURRENT_SOURCE_DIR}/lib") @@ -85,42 +93,52 @@ set(DRAMSYS_EXTENSIONS_DIR "${CMAKE_CURRENT_SOURCE_DIR}/extensions") ### Library Settings ### ############################################### -### nlohmann_json ### -add_subdirectory(${DRAMSYS_LIBRARY_DIR}) +if (DRAMSYS_USE_FETCH_CONTENT) + include(FetchContent) -if (NOT TARGET nlohmann_json::nlohmann_json) - FetchContent_Declare(nlohmann_json URL https://github.com/nlohmann/json/releases/download/v3.11.3/json.tar.xz) - FetchContent_MakeAvailable(nlohmann_json) -endif () + if (DRAMSYS_USE_FETCH_CONTENT_NLOHMANN_JSON) + FetchContent_Declare(nlohmann_json + URL https://github.com/nlohmann/json/releases/download/v3.11.3/json.tar.xz + OVERRIDE_FIND_PACKAGE + ) -### SystemC ### -if (NOT TARGET SystemC::systemc) # TODO ist das wirklich so korrekt? - set(DISABLE_COPYRIGHT_MESSAGE True) + FetchContent_MakeAvailable(nlohmann_json) + endif() - FetchContent_Declare( - systemc - GIT_REPOSITORY https://github.com/accellera-official/systemc.git - GIT_TAG 2.3.4) + if (DRAMSYS_USE_FETCH_CONTENT_SQLITE3) + add_subdirectory(${DRAMSYS_LIBRARY_DIR}/sqlite3) + endif() - FetchContent_MakeAvailable(systemc) + if (DRAMSYS_USE_FETCH_CONTENT_SYSTEMC) + set(DISABLE_COPYRIGHT_MESSAGE TRUE) - # Set include directories to SYSTEM to suppress warnings - set_target_properties(systemc PROPERTIES SYSTEM TRUE) -else() - find_package(systemc NAMES SystemCLanguage) + FetchContent_Declare( + SystemCLanguage + GIT_REPOSITORY https://github.com/accellera-official/systemc.git + GIT_TAG 2.3.4 + OVERRIDE_FIND_PACKAGE + ) + + FetchContent_MakeAvailable(SystemCLanguage) + + # Set include directories to SYSTEM to suppress warnings + set_target_properties(systemc PROPERTIES SYSTEM TRUE) + endif() + + if (DRAMSYS_WITH_DRAMPOWER AND DRAMSYS_USE_FETCH_CONTENT_INTERNAL) + FetchContent_Declare( + DRAMPower + GIT_REPOSITORY https://github.com/tukl-msd/DRAMPower + GIT_TAG 9e64a1b + ) + + FetchContent_MakeAvailable(DRAMPower) + endif() endif() - -### DRAMPower ### -if (DRAMSYS_WITH_DRAMPOWER) - FetchContent_Declare( - DRAMPower - GIT_REPOSITORY https://github.com/tukl-msd/DRAMPower - GIT_TAG 9e64a1b) - - FetchContent_MakeAvailable(DRAMPower) - set_target_properties(DRAMPower PROPERTIES FOLDER lib) -endif () +find_package(SystemCLanguage REQUIRED) +find_package(SQLite3 REQUIRED) +find_package(nlohmann_json REQUIRED) ############################################### ### Source Directory ### diff --git a/CMakePresets.json b/CMakePresets.json index 827e0500..0f947767 100644 --- a/CMakePresets.json +++ b/CMakePresets.json @@ -22,8 +22,8 @@ "inherits": "cmake-pedantic", "cacheVariables": { "DRAMSYS_BUILD_TESTS": "ON", - "DRAMSYS_WITH_DRAMPOWER": "ON", - "DRAMSYS_BUILD_TRACE_ANALYZER": "ON" + "DRAMSYS_BUILD_TRACE_ANALYZER": "ON", + "DRAMSYS_WITH_DRAMPOWER": "ON" } }, { diff --git a/lib/CMakeLists.txt b/lib/CMakeLists.txt deleted file mode 100644 index bbdf1add..00000000 --- a/lib/CMakeLists.txt +++ /dev/null @@ -1 +0,0 @@ -add_subdirectory(sqlite3) diff --git a/lib/sqlite3/CMakeLists.txt b/lib/sqlite3/CMakeLists.txt index 642b1c8b..1b760026 100644 --- a/lib/sqlite3/CMakeLists.txt +++ b/lib/sqlite3/CMakeLists.txt @@ -2,8 +2,12 @@ ### sqlite3 ### ######################################## -FetchContent_Declare(sqlite3 URL "https://www.sqlite.org/2022/sqlite-amalgamation-3400100.zip") -FetchContent_MakeAvailable(sqlite3) +FetchContent_Declare(SQLite3 + URL "https://www.sqlite.org/2022/sqlite-amalgamation-3400100.zip" + OVERRIDE_FIND_PACKAGE +) + +FetchContent_MakeAvailable(SQLite3) add_library(sqlite3) target_sources(sqlite3 PRIVATE @@ -14,11 +18,8 @@ target_sources(sqlite3 PRIVATE target_include_directories(sqlite3 PUBLIC ${sqlite3_SOURCE_DIR}) -set_target_properties(sqlite3 PROPERTIES FOLDER lib) - ### Compile options ### # refer to https://www.sqlite.org/compile.html - target_compile_definitions(sqlite3 PUBLIC SQLITE_ENABLE_RTREE SQLITE_OMIT_LOAD_EXTENSION) # Use the same name as the FindSQLite3 script of CMake uses diff --git a/src/traceAnalyzer/CMakeLists.txt b/src/traceAnalyzer/CMakeLists.txt index 3ddaccf5..093ce3d0 100644 --- a/src/traceAnalyzer/CMakeLists.txt +++ b/src/traceAnalyzer/CMakeLists.txt @@ -38,29 +38,35 @@ ### TraceAnalyzer ### ######################################## -# Add Python3 Dependency: -find_package(Python3 COMPONENTS Development Interpreter) +option(DRAMSYS_USE_FETCH_CONTENT_PYBIND "Enable FetchContent to provide pybind11" ${DRAMSYS_USE_FETCH_CONTENT}) -FetchContent_Declare( - pybind11 - URL https://github.com/pybind/pybind11/archive/refs/tags/v2.13.1.zip -) +if (DRAMSYS_USE_FETCH_CONTENT) + if (DRAMSYS_USE_FETCH_CONTENT_PYBIND) + FetchContent_Declare( + pybind11 + URL https://github.com/pybind/pybind11/archive/refs/tags/v2.13.1.zip + OVERRIDE_FIND_PACKAGE + ) + + FetchContent_MakeAvailable(pybind11) + endif() +endif() -FetchContent_MakeAvailable(pybind11) - -# Add QT Library: if (APPLE) set(Qt5_DIR "/opt/homebrew/opt/qt@5/lib/cmake/Qt5") endif(APPLE) + find_package(Qt5 COMPONENTS Core Gui Widgets Sql REQUIRED) +find_package(Qwt REQUIRED) + +find_package(Python3 COMPONENTS Development Interpreter REQUIRED) +find_package(pybind11 REQUIRED) + set(CMAKE_AUTOMOC ON) set(CMAKE_AUTOUIC ON) set(CMAKE_AUTORCC ON) set(CMAKE_INCLUDE_CURRENT_DIR ON) -# Add QWT Dependency: -find_package(Qwt REQUIRED) - add_executable(TraceAnalyzer businessObjects/commentmodel.cpp businessObjects/configmodels.cpp diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index cfb4e5cd..8d262fd1 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -2,12 +2,22 @@ include(GoogleTest) set(DRAMSYS_TEST_RESOURCES_DIR "${CMAKE_CURRENT_SOURCE_DIR}/resources") -FetchContent_Declare( - googletest - GIT_REPOSITORY https://github.com/google/googletest - GIT_TAG release-1.12.1) +option(DRAMSYS_USE_FETCH_CONTENT_GTEST "Enable FetchContent to provide gtest" ${DRAMSYS_USE_FETCH_CONTENT}) -FetchContent_MakeAvailable(googletest) +if (DRAMSYS_USE_FETCH_CONTENT) + if (DRAMSYS_USE_FETCH_CONTENT_GTEST) + FetchContent_Declare( + GTest + GIT_REPOSITORY https://github.com/google/googletest + GIT_TAG release-1.12.1 + OVERRIDE_FIND_PACKAGE + ) + + FetchContent_MakeAvailable(GTest) + endif() +endif() + +find_package(GTest REQUIRED) add_subdirectory(tests_configuration) add_subdirectory(tests_dramsys) diff --git a/tests/tests_configuration/CMakeLists.txt b/tests/tests_configuration/CMakeLists.txt index 68276e2a..b7732f6a 100644 --- a/tests/tests_configuration/CMakeLists.txt +++ b/tests/tests_configuration/CMakeLists.txt @@ -11,6 +11,7 @@ set_target_properties(tests_configuration PROPERTIES FOLDER tests/configuration) target_link_libraries(tests_configuration PRIVATE DRAMSys::config + gtest gtest_main ) diff --git a/tests/tests_dramsys/CMakeLists.txt b/tests/tests_dramsys/CMakeLists.txt index 7acacb38..dba79007 100644 --- a/tests/tests_dramsys/CMakeLists.txt +++ b/tests/tests_dramsys/CMakeLists.txt @@ -14,6 +14,7 @@ target_include_directories(tests_dramsys PUBLIC ${PROJECT_SOURCE_DIR}) set_target_properties(tests_dramsys PROPERTIES FOLDER tests) target_link_libraries(tests_dramsys DRAMSys::libdramsys + gtest gtest_main ) diff --git a/tests/tests_simulator/CMakeLists.txt b/tests/tests_simulator/CMakeLists.txt index bd08a30b..ded10b12 100644 --- a/tests/tests_simulator/CMakeLists.txt +++ b/tests/tests_simulator/CMakeLists.txt @@ -13,6 +13,7 @@ set_target_properties(tests_simulator PROPERTIES FOLDER tests/simulator) target_link_libraries(tests_simulator PRIVATE simulator + gtest gtest_main )