Skip to content

Commit

Permalink
cmake install; cmake fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
lfreist committed Aug 2, 2024
1 parent d2dccb2 commit 82161e7
Show file tree
Hide file tree
Showing 23 changed files with 207 additions and 196 deletions.
44 changes: 31 additions & 13 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,31 +18,49 @@ option(BUILD_EXAMPLES "Build example program" ${PROJECT_IS_TOP_LEVEL})
option(BUILD_TESTING "Build test program" ${PROJECT_IS_TOP_LEVEL})

# Components
option(HWINFO_OS "Enable OS detection" ON)
option(HWINFO_MAINBOARD "Enable mainboard detection" ON)
option(HWINFO_CPU "Enable CPU detection" ON)
option(HWINFO_DISK "Enable disk detection" ON)
option(HWINFO_RAM "Enable RAM detection" ON)
option(HWINFO_GPU "Enable GPU detection" ON)
option(HWINFO_GPU_OPENCL "Enable usage of OpenCL in GPU information" OFF)
option(HWINFO_BATTERY "Enable battery detection" ON)
option(HWINFO_OS "Enable OS information" ON)
option(HWINFO_MAINBOARD "Enable mainboard information" ON)
option(HWINFO_CPU "Enable CPU information" ON)
option(HWINFO_DISK "Enable disk information" ON)
option(HWINFO_RAM "Enable RAM information" ON)
option(HWINFO_GPU "Enable GPU information" ON)
option(HWINFO_GPU_OPENCL "Enable OpenCL for more GPU information" OFF)
option(HWINFO_BATTERY "Enable battery information" ON)

set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)

set(HWINFO_INCLUDE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/include")
add_subdirectory(src)

if (NOT PROJECT_IS_TOP_LEVEL)
return()
endif()
include(CMakePackageConfigHelpers)
write_basic_package_version_file(
"${CMAKE_CURRENT_BINARY_DIR}/hwinfoConfigVersion.cmake"
VERSION ${PROJECT_VERSION}
COMPATIBILITY AnyNewerVersion
)
configure_file(cmake/hwinfoConfig.cmake.in
"${CMAKE_CURRENT_BINARY_DIR}/hwinfoConfig.cmake"
@ONLY
)
install(FILES
"${CMAKE_CURRENT_BINARY_DIR}/hwinfoConfig.cmake"
"${CMAKE_CURRENT_BINARY_DIR}/hwinfoConfigVersion.cmake"
DESTINATION lib/cmake/hwinfo
)
install(EXPORT hwinfoTargets
FILE hwinfoTargets.cmake
NAMESPACE hwinfo::
DESTINATION lib/cmake/hwinfo
)

if(BUILD_EXAMPLES)
if(BUILD_EXAMPLES OR BUILD_TESTING)
add_subdirectory(examples)
endif()

if(BUILD_TESTING)
include(CTest)
add_subdirectory(test)
add_test(system_infoMain system_infoMain)
add_test(system_info system_info)
add_test(system_info_stand_alone system_info_stand_alone)
endif()
109 changes: 40 additions & 69 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
hwinfo provides an easy-to-use and modern C++ API for retrieving hardware information of your systems components such as
CPU, RAM, GPU, Disks, Mainboard, ...

hwinfo automatically uses the latest C++ standard supported by your compiler. C++20 is required for GPU OpenCL support if enabled (configurable with `HWINFO_GPU_OPENCL`).

> **Note**
>
> If you face any issues, find bugs or if your platform is not supported yet, do not hesitate
Expand All @@ -22,17 +20,16 @@ hwinfo automatically uses the latest C++ standard supported by your compiler. C+
## Content

* [Supported Components](#supported-components)
* [API](#API)
* [Build hwinfo](#build-hwinfo)
* [Example](#example)
* [Include hwinfo to cmake project](#include-hwinfo-in-your-cmake-project)
* [Include hwinfo to cmake project](#include-hwinfo-to-cmake-project)

## Supported Components

> **Note**
>
> The listed components that are not yet implemented (indicated with ❌) are in development and will be supported in
> later releases. **You are welcome to start contributing and help improving this library!**
> future releases. **You are welcome to start contributing and help improving this library!**
| Component | Info | Linux | Apple | Windows |
|------------------|:-------------------|:-----:|:------:|:-------:|
Expand All @@ -50,7 +47,7 @@ hwinfo automatically uses the latest C++ standard supported by your compiler. C+
| | Name ||| ✔️ |
| | Serial Number ||| ✔️ |
| | Total Memory Size | ✔️ | ✔️ | ✔️ |
| | Free Memory Size | ✔️ || |
| | Free Memory Size | ✔️ || ✔️ |
| Mainboard | Vendor | ✔️ || ✔️ |
| | Model | ✔️ || ✔️ |
| | Version | ✔️ || ✔️ |
Expand Down Expand Up @@ -104,67 +101,6 @@ The CMake options control which components will be built and available in the li
- `HWINFO_GPU_OPENCL` "Enable usage of OpenCL in GPU information" (default to `OFF`)
- `HWINFO_BATTERY` "Enable battery detection" (default to `ON`)

## API

This section describes, how you can get information about the supported components of your computer.

### CPU

hwinfo supports reading CPU information on boards with multiple sockets and CPUs installed.
`getAllSockets()` returns a `std::vector<Socket>`. A `Socket` object represents a physical socket and holds information
about the installed CPU. You can access these information via `Socket::CPU()` which retuns a `CPU` instance.

> Why not just retrieving a `std::vector<CPU>`?
> The reason for this lies within how linux handles CPUs. For linux systems, the cores of a multi core CPU are
> considered as different physical CPUs.
> Thus, I added the `Socket` layer to make clear, that multiple elements in the yielded `std::vector<Socket>` vector
> mean that there are two CPUs on two different sockets installed.
The following methods are available for `CPU`:

- `const std::string& CPU::vendor() const` "GenuineIntel"
- `const std::string& CPU::modelName() const` "Intel(R) Core(TM) i7-10700K CPU @ 3.80GHz"
- `int64_t CPU::cacheSize_Bytes() const` 16384000
- `int CPU::numPhysicalCores() const` 8
- `int CPU::numLogicalCores() const` 16
- `int64_t CPU::maxClockSpeed_MHz() const` 5100000
- `int64_t CPU::regularClockSpeed_MHz() const` 3800000
- `int64_t CPU::minClockSpeed_MHz() const` 1800000
- `int64_t CPU::currentClockSpeed_MHz() const` 4700189
- `const std::vector<std::string>& CPU::flags() cosnt` {"SSE", "AVX", ...}

### GPU

You can also get information about all installed GPUs using hwinfo.
`getAllGPUs()` returns a `std::vector<GPU>`. A `GPU` object represents a physical GPU.

The following methods are available for `GPU`:

- `const std::string& GPU::vendor() const` "NVIDIA"
- `const std::string& GPU::name() const` "NVIDIA GeForce RTX 3070 Ti"
- `const std::string& GPU::driverVersion() const` "31.0.15.2698" (empty for linux)
- `int64_t GPU::memory_Bytes() const` 8190000000
- `int64_t GPU::min_frequency_MHz() const`
- `int64_t GPU::current_frequency_MHz() const`
- `int64_t GPU::max_frequency_MHz() const`
- `int GPU::id() const` 0

### RAM

TODO

### OS

TODO

### Baseboard

TODO

### Disk

TODO

## Build `hwinfo`

> Requirements: git, cmake, c++ compiler (gcc, clang, MSVC)
Expand All @@ -176,9 +112,10 @@ TODO
2. Build using cmake:
```bash
mkdir build
cmake -B build -DCMAKE_BUILD_TYPE=Release # -DNO_OCL=ON (for C++11)
cmake -B build -DCMAKE_BUILD_TYPE=Release
cmake --build build
```
This builds static and dynamic libraries. Static library cmake targets are named `<target>_static` (e.g. `hwinfo_static`)
## Example
Expand Down Expand Up @@ -261,7 +198,41 @@ Disk 4:

```
## Directly including `hwinfo` in your cmake project
## include hwinfo to cmake project
### Include installed version
1. Install hwinfo
```
git clone https://github.com/lfreist/hwinfo && cd hwinfo
mkdir build
cmake -B build && cmake --build build
cmake --install build
```
2. Simply add the following to your `CMakeLists.txt` file:
```cmake
# file: CMakeLists.txt
find_package(hwinfo REQUIRED)
```
3. Include `hwinfo` into your `.cpp/.h` files:
```c++
// file: your_executable.cpp
#include <hwinfo/hwinfo.h>
int main(int argc, char** argv) {
// Your code
}
```
4. Link it in cmake
```cmake
add_executable(your_executable your_executable.cpp)
target_link_libraries(your_executable PUBLIC hwinfo::hwinfo)
```
### As git submodule
1. Download `hwinfo` into your project (e.g. in `<project-root>/third_party/hwinfo`)
```
Expand Down
3 changes: 3 additions & 0 deletions cmake/hwinfoConfig.cmake.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
@PACKAGE_INIT@

include("${CMAKE_CURRENT_LIST_DIR}/hwinfoTargets.cmake")
21 changes: 16 additions & 5 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,10 +1,21 @@
include(FetchContent)
FetchContent_Declare(
find_package(fmt QUIET)

if (NOT fmt_FOUND)
message(" -> libfmt not found. Fetching from github...")
include(FetchContent)
FetchContent_Declare(
fmt
GIT_REPOSITORY https://github.com/fmtlib/fmt.git
GIT_TAG 11.0.1)
FetchContent_MakeAvailable(fmt)
FetchContent_MakeAvailable(fmt)
else ()
message(" -> using installed libfmt")
endif ()

add_executable(system_info system_infoMain.cpp)
add_executable(system_info_stand_alone system_infoMain.cpp)

add_executable(system_infoMain system_infoMain.cpp)
target_link_libraries(system_info PRIVATE hwinfo::hwinfo fmt::fmt)
target_link_libraries(system_info_stand_alone PRIVATE hwinfo::hwinfo_static fmt::fmt)

target_link_libraries(system_infoMain PRIVATE hwinfo::hwinfo fmt::fmt)
install(TARGETS system_info system_info_stand_alone)
4 changes: 0 additions & 4 deletions include/hwinfo/battery.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@

#pragma once

#ifdef HWINFO_BATTERY

#include <hwinfo/platform.h>

#include <cstdint>
Expand Down Expand Up @@ -50,5 +48,3 @@ class HWINFO_API Battery {
std::vector<Battery> getAllBatteries();

} // namespace hwinfo

#endif // HWINFO_BATTERY
4 changes: 0 additions & 4 deletions include/hwinfo/cpu.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@

#pragma once

#ifdef HWINFO_CPU

#include <hwinfo/platform.h>
#include <hwinfo/utils/wmi_wrapper.h>

Expand Down Expand Up @@ -78,5 +76,3 @@ class HWINFO_API CPU {
std::vector<CPU> getAllCPUs();

} // namespace hwinfo

#endif // HWINFO_CPU
6 changes: 1 addition & 5 deletions include/hwinfo/cpuid.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@

#pragma once

#ifdef HWINFO_CPU

#include "hwinfo/platform.h"
#include <hwinfo/platform.h>

#if defined(HWINFO_X86)

Expand Down Expand Up @@ -54,5 +52,3 @@ inline void cpuid(uint32_t func_id, uint32_t sub_func_id, uint32_t regs[4]) {
} // namespace hwinfo

#endif // HWINFO_X86

#endif // HWINFO_CPU
4 changes: 0 additions & 4 deletions include/hwinfo/disk.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@

#pragma once

#ifdef HWINFO_DISK

#include <hwinfo/platform.h>

#include <cstdint>
Expand Down Expand Up @@ -41,5 +39,3 @@ class HWINFO_API Disk {
std::vector<Disk> getAllDisks();

} // namespace hwinfo

#endif // HWINFO_DISK
4 changes: 0 additions & 4 deletions include/hwinfo/gpu.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@

#pragma once

#ifdef HWINFO_GPU

#include <hwinfo/platform.h>

#include <cstdint>
Expand Down Expand Up @@ -45,5 +43,3 @@ class HWINFO_API GPU {

std::vector<GPU> getAllGPUs();
} // namespace hwinfo

#endif // HWINFO_GPU
12 changes: 4 additions & 8 deletions include/hwinfo/mainboard.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@

#pragma once

#ifdef HWINFO_MAINBOARD

#include <hwinfo/platform.h>

#include <string>
Expand All @@ -18,10 +16,10 @@ class HWINFO_API MainBoard {
MainBoard();
~MainBoard() = default;

const std::string& vendor() const;
const std::string& name() const;
const std::string& version() const;
const std::string& serialNumber() const;
HWI_NODISCARD const std::string& vendor() const;
HWI_NODISCARD const std::string& name() const;
HWI_NODISCARD const std::string& version() const;
HWI_NODISCARD const std::string& serialNumber() const;

private:
std::string _vendor;
Expand All @@ -31,5 +29,3 @@ class HWINFO_API MainBoard {
};

} // namespace hwinfo

#endif // HWINFO_MAINBOARD
4 changes: 0 additions & 4 deletions include/hwinfo/os.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@

#pragma once

#ifdef HWINFO_OS

#include <hwinfo/platform.h>

#include <string>
Expand Down Expand Up @@ -35,5 +33,3 @@ class HWINFO_API OS {
};

} // namespace hwinfo

#endif // HWINFO_OS
4 changes: 0 additions & 4 deletions include/hwinfo/ram.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@

#pragma once

#ifdef HWINFO_RAM

#include <hwinfo/platform.h>

#include <cstdint>
Expand Down Expand Up @@ -39,5 +37,3 @@ class HWINFO_API Memory {
};

} // namespace hwinfo

#endif // HWINFO_RAM
Loading

0 comments on commit 82161e7

Please sign in to comment.