From 79f0aa5864322b041c1660845fdc7f7fce2af69a Mon Sep 17 00:00:00 2001 From: pavel_a Date: Sat, 7 Sep 2024 00:08:01 +0300 Subject: [PATCH] CMakeLists.txt - add config option to exclude whole pci stuff --- CMakeLists.txt | 120 +++++++++++++++++++++++++---------------------- examples/pci.cpp | 4 ++ src/pci.cpp | 6 +++ 3 files changed, 73 insertions(+), 57 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 8a14a24..296fc62 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -17,6 +17,8 @@ option(INFOWARE_USE_OPENCL "Add public, transitive define to infoware to use Ope option(INFOWARE_USE_X11 "Add public, transitive define to infoware to use X11 for display detection." OFF) +option(INFOWARE_USE_PCIIDS "Want PCI IDs. If disabled, the PCI ID database will not be compiled, and the pci.hpp API will be unimplemented." ON) + option(INFOWARE_EXAMPLES "Add infoware examples to the build." OFF) option(INFOWARE_TESTS "Input tests for infoware." OFF) @@ -91,67 +93,71 @@ if(BUILD_SHARED_LIBS) # Controls add_library() w/o explicit mode target_compile_definitions(infoware PUBLIC INFOWARE_DLL=1) endif() -if(NOT Git_FOUND) # Could be pre-injected - find_package(Git) -endif() +if(INFOWARE_USE_PCIIDS) + target_compile_definitions(infoware PRIVATE INFOWARE_USE_PCIIDS) -set(INFOWARE_PCI_DATA_DIR infoware_generated CACHE PATH "Output directory for the PCI ids generator") -set(INFOWARE_PCI_DATA_HPP pci_data.hpp) -set(INFOWARE_PCI_DATA_GEN "${INFOWARE_PCI_DATA_DIR}/${INFOWARE_PCI_DATA_HPP}") -set(infoware_pci_ids_error "\ -The pci.ids file, downloadable from https://github.com/pciutils/pciids or http://pci-ids.ucw.cz, is required for building infoware, \ -and cloned automatically from that GitHub repository by default.\n\ -To use a local copy, set INFOWARE_PCI_IDS_PATH to its location.") -if(NOT CMAKE_CROSSCOMPILING) - if(INFOWARE_PCI_IDS_PATH) - if(NOT EXISTS "${INFOWARE_PCI_IDS_PATH}") - message(WARNING "The specified pci.ids file INFOWARE_PCI_IDS_PATH=${INFOWARE_PCI_IDS_PATH} doesn't seem to exist.") - endif() - set(infoware_pci_ids_file "${INFOWARE_PCI_IDS_PATH}") - elseif(NOT Git_FOUND) - message(SEND_ERROR "Couldn't find a usable git executable in the environment, and the CMake variable INFOWARE_PCI_IDS_PATH is empty.\n${infoware_pci_ids_error}") - else() - # Thanks, @griwes - set(infoware_pci_ids_file "${CMAKE_CURRENT_BINARY_DIR}/pciids/pci.ids") - if(EXISTS "${infoware_pci_ids_file}") - execute_process(COMMAND "${GIT_EXECUTABLE}" remote set-url origin "${INFOWARE_PCI_IDS_REPOSITORY}" - WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/pciids") - execute_process(COMMAND "${GIT_EXECUTABLE}" pull - WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/pciids" - RESULT_VARIABLE infoware_git_pciids_clone_err) + if(NOT Git_FOUND) # Could be pre-injected + find_package(Git) + endif() + + set(INFOWARE_PCI_DATA_DIR infoware_generated CACHE PATH "Output directory for the PCI ids generator") + set(INFOWARE_PCI_DATA_HPP pci_data.hpp) + set(INFOWARE_PCI_DATA_GEN "${INFOWARE_PCI_DATA_DIR}/${INFOWARE_PCI_DATA_HPP}") + set(infoware_pci_ids_error "\ + The pci.ids file, downloadable from https://github.com/pciutils/pciids or http://pci-ids.ucw.cz, is required for building infoware, \ + and cloned automatically from that GitHub repository by default.\n\ + To use a local copy, set INFOWARE_PCI_IDS_PATH to its location.") + if(NOT CMAKE_CROSSCOMPILING) + if(INFOWARE_PCI_IDS_PATH) + if(NOT EXISTS "${INFOWARE_PCI_IDS_PATH}") + message(WARNING "The specified pci.ids file INFOWARE_PCI_IDS_PATH=${INFOWARE_PCI_IDS_PATH} doesn't seem to exist.") + endif() + set(infoware_pci_ids_file "${INFOWARE_PCI_IDS_PATH}") + elseif(NOT Git_FOUND) + message(SEND_ERROR "Couldn't find a usable git executable in the environment, and the CMake variable INFOWARE_PCI_IDS_PATH is empty.\n${infoware_pci_ids_error}") else() - execute_process(COMMAND "${GIT_EXECUTABLE}" clone "${INFOWARE_PCI_IDS_REPOSITORY}" -- pciids - WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}" - RESULT_VARIABLE infoware_git_pciids_clone_err) + # Thanks, @griwes + set(infoware_pci_ids_file "${CMAKE_CURRENT_BINARY_DIR}/pciids/pci.ids") + if(EXISTS "${infoware_pci_ids_file}") + execute_process(COMMAND "${GIT_EXECUTABLE}" remote set-url origin "${INFOWARE_PCI_IDS_REPOSITORY}" + WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/pciids") + execute_process(COMMAND "${GIT_EXECUTABLE}" pull + WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/pciids" + RESULT_VARIABLE infoware_git_pciids_clone_err) + else() + execute_process(COMMAND "${GIT_EXECUTABLE}" clone "${INFOWARE_PCI_IDS_REPOSITORY}" -- pciids + WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}" + RESULT_VARIABLE infoware_git_pciids_clone_err) + endif() + if(infoware_git_pciids_clone_err) + message(SEND_ERROR "Cloning/pulling pciids repository from ${INFOWARE_PCI_IDS_REPOSITORY} failed with ${infoware_git_pciids_clone_err}.\n${infoware_pci_ids_error}") + endif() endif() - if(infoware_git_pciids_clone_err) - message(SEND_ERROR "Cloning/pulling pciids repository from ${INFOWARE_PCI_IDS_REPOSITORY} failed with ${infoware_git_pciids_clone_err}.\n${infoware_pci_ids_error}") - endif() - endif() - add_executable(infoware_pci_generator tools/pci_generator.cpp) - set_target_properties(infoware_pci_generator PROPERTIES CXX_STANDARD 14 - CXX_STANDARD_REQUIRED ON - CXX_EXTENSIONS OFF) - - add_custom_command(OUTPUT ${INFOWARE_PCI_DATA_GEN} - COMMAND ${CMAKE_COMMAND} -E make_directory "${INFOWARE_PCI_DATA_DIR}" - COMMAND $ "${infoware_pci_ids_file}" > "${INFOWARE_PCI_DATA_GEN}" - DEPENDS "${infoware_pci_ids_file}" - COMMENT "Generating ${INFOWARE_PCI_DATA_HPP}") - add_custom_target(infoware_generate_pcis DEPENDS "${INFOWARE_PCI_DATA_GEN}") -else() - include(ExternalProject) - ExternalProject_Add(infoware_generate_pcis - SOURCE_DIR ${CMAKE_SOURCE_DIR} - PREFIX ${CMAKE_BINARY_DIR}/pci_generator - BINARY_DIR ${CMAKE_BINARY_DIR}/pci_generator - BUILD_COMMAND ${CMAKE_COMMAND} --build --target infoware_generate_pcis - INSTALL_COMMAND "" - BUILD_ALWAYS ON - CMAKE_ARGS -DINFOWARE_PCI_DATA_DIR:PATH=${CMAKE_BINARY_DIR}/${INFOWARE_PCI_DATA_DIR}) -endif() -add_dependencies(infoware infoware_generate_pcis) + add_executable(infoware_pci_generator tools/pci_generator.cpp) + set_target_properties(infoware_pci_generator PROPERTIES CXX_STANDARD 14 + CXX_STANDARD_REQUIRED ON + CXX_EXTENSIONS OFF) + + add_custom_command(OUTPUT ${INFOWARE_PCI_DATA_GEN} + COMMAND ${CMAKE_COMMAND} -E make_directory "${INFOWARE_PCI_DATA_DIR}" + COMMAND $ "${infoware_pci_ids_file}" > "${INFOWARE_PCI_DATA_GEN}" + DEPENDS "${infoware_pci_ids_file}" + COMMENT "Generating ${INFOWARE_PCI_DATA_HPP}") + add_custom_target(infoware_generate_pcis DEPENDS "${INFOWARE_PCI_DATA_GEN}") + else() + include(ExternalProject) + ExternalProject_Add(infoware_generate_pcis + SOURCE_DIR ${CMAKE_SOURCE_DIR} + PREFIX ${CMAKE_BINARY_DIR}/pci_generator + BINARY_DIR ${CMAKE_BINARY_DIR}/pci_generator + BUILD_COMMAND ${CMAKE_COMMAND} --build --target infoware_generate_pcis + INSTALL_COMMAND "" + BUILD_ALWAYS ON + CMAKE_ARGS -DINFOWARE_PCI_DATA_DIR:PATH=${CMAKE_BINARY_DIR}/${INFOWARE_PCI_DATA_DIR}) + endif() + add_dependencies(infoware infoware_generate_pcis) +endif(INFOWARE_USE_PCIIDS) if(MSVC) diff --git a/examples/pci.cpp b/examples/pci.cpp index 58e385b..c19920c 100644 --- a/examples/pci.cpp +++ b/examples/pci.cpp @@ -28,6 +28,7 @@ static bool print_vendor(std::uint64_t vendor_id, const char* vendor_name); int main(int, const char** argv) { std::cout << "Infoware version " << iware::version << '\n'; +#ifdef INFOWARE_USE_PCIIDS if(argv[1] == nullptr) std::cout << "Usage: " << argv[0] << " [device_id]" << '\n'; else { @@ -51,6 +52,9 @@ int main(int, const char** argv) { return (vendor_ok ? 0 : 1) | (device.device_name ? 0 : 2); } } +#else + std::cout << "Compiled without PCI ID support\n"; +#endif } diff --git a/src/pci.cpp b/src/pci.cpp index 027f785..64ab9c9 100644 --- a/src/pci.cpp +++ b/src/pci.cpp @@ -2,6 +2,9 @@ // infoware - C++ System information Library +#ifdef INFOWARE_USE_PCIIDS + + #include "infoware/pci.hpp" #include "infoware_generated/pci_data.hpp" #include @@ -62,3 +65,6 @@ const char* iware::pci::identify_vendor(std::uint64_t pci_id) noexcept { else return nullptr; } + + +#endif