Skip to content

Commit

Permalink
CMakeLists.txt - add config option to exclude whole pci stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
pavel_a authored and nabijaczleweli committed Sep 6, 2024
1 parent c8da4c2 commit 79f0aa5
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 57 deletions.
120 changes: 63 additions & 57 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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 $<TARGET_FILE:infoware_pci_generator> "${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 <BINARY_DIR> --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 $<TARGET_FILE:infoware_pci_generator> "${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 <BINARY_DIR> --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)
Expand Down
4 changes: 4 additions & 0 deletions examples/pci.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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] << " <vendor_id> [device_id]" << '\n';
else {
Expand All @@ -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
}


Expand Down
6 changes: 6 additions & 0 deletions src/pci.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
// infoware - C++ System information Library


#ifdef INFOWARE_USE_PCIIDS


#include "infoware/pci.hpp"
#include "infoware_generated/pci_data.hpp"
#include <algorithm>
Expand Down Expand Up @@ -62,3 +65,6 @@ const char* iware::pci::identify_vendor(std::uint64_t pci_id) noexcept {
else
return nullptr;
}


#endif

0 comments on commit 79f0aa5

Please sign in to comment.