Skip to content

Commit 82161e7

Browse files
committed
cmake install; cmake fixes
1 parent d2dccb2 commit 82161e7

23 files changed

+207
-196
lines changed

CMakeLists.txt

+31-13
Original file line numberDiff line numberDiff line change
@@ -18,31 +18,49 @@ option(BUILD_EXAMPLES "Build example program" ${PROJECT_IS_TOP_LEVEL})
1818
option(BUILD_TESTING "Build test program" ${PROJECT_IS_TOP_LEVEL})
1919

2020
# Components
21-
option(HWINFO_OS "Enable OS detection" ON)
22-
option(HWINFO_MAINBOARD "Enable mainboard detection" ON)
23-
option(HWINFO_CPU "Enable CPU detection" ON)
24-
option(HWINFO_DISK "Enable disk detection" ON)
25-
option(HWINFO_RAM "Enable RAM detection" ON)
26-
option(HWINFO_GPU "Enable GPU detection" ON)
27-
option(HWINFO_GPU_OPENCL "Enable usage of OpenCL in GPU information" OFF)
28-
option(HWINFO_BATTERY "Enable battery detection" ON)
21+
option(HWINFO_OS "Enable OS information" ON)
22+
option(HWINFO_MAINBOARD "Enable mainboard information" ON)
23+
option(HWINFO_CPU "Enable CPU information" ON)
24+
option(HWINFO_DISK "Enable disk information" ON)
25+
option(HWINFO_RAM "Enable RAM information" ON)
26+
option(HWINFO_GPU "Enable GPU information" ON)
27+
option(HWINFO_GPU_OPENCL "Enable OpenCL for more GPU information" OFF)
28+
option(HWINFO_BATTERY "Enable battery information" ON)
2929

3030
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
3131
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
3232

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

36-
if (NOT PROJECT_IS_TOP_LEVEL)
37-
return()
38-
endif()
36+
include(CMakePackageConfigHelpers)
37+
write_basic_package_version_file(
38+
"${CMAKE_CURRENT_BINARY_DIR}/hwinfoConfigVersion.cmake"
39+
VERSION ${PROJECT_VERSION}
40+
COMPATIBILITY AnyNewerVersion
41+
)
42+
configure_file(cmake/hwinfoConfig.cmake.in
43+
"${CMAKE_CURRENT_BINARY_DIR}/hwinfoConfig.cmake"
44+
@ONLY
45+
)
46+
install(FILES
47+
"${CMAKE_CURRENT_BINARY_DIR}/hwinfoConfig.cmake"
48+
"${CMAKE_CURRENT_BINARY_DIR}/hwinfoConfigVersion.cmake"
49+
DESTINATION lib/cmake/hwinfo
50+
)
51+
install(EXPORT hwinfoTargets
52+
FILE hwinfoTargets.cmake
53+
NAMESPACE hwinfo::
54+
DESTINATION lib/cmake/hwinfo
55+
)
3956

40-
if(BUILD_EXAMPLES)
57+
if(BUILD_EXAMPLES OR BUILD_TESTING)
4158
add_subdirectory(examples)
4259
endif()
4360

4461
if(BUILD_TESTING)
4562
include(CTest)
4663
add_subdirectory(test)
47-
add_test(system_infoMain system_infoMain)
64+
add_test(system_info system_info)
65+
add_test(system_info_stand_alone system_info_stand_alone)
4866
endif()

README.md

+40-69
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@
1212
hwinfo provides an easy-to-use and modern C++ API for retrieving hardware information of your systems components such as
1313
CPU, RAM, GPU, Disks, Mainboard, ...
1414

15-
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`).
16-
1715
> **Note**
1816
>
1917
> If you face any issues, find bugs or if your platform is not supported yet, do not hesitate
@@ -22,17 +20,16 @@ hwinfo automatically uses the latest C++ standard supported by your compiler. C+
2220
## Content
2321

2422
* [Supported Components](#supported-components)
25-
* [API](#API)
2623
* [Build hwinfo](#build-hwinfo)
2724
* [Example](#example)
28-
* [Include hwinfo to cmake project](#include-hwinfo-in-your-cmake-project)
25+
* [Include hwinfo to cmake project](#include-hwinfo-to-cmake-project)
2926

3027
## Supported Components
3128

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

107-
## API
108-
109-
This section describes, how you can get information about the supported components of your computer.
110-
111-
### CPU
112-
113-
hwinfo supports reading CPU information on boards with multiple sockets and CPUs installed.
114-
`getAllSockets()` returns a `std::vector<Socket>`. A `Socket` object represents a physical socket and holds information
115-
about the installed CPU. You can access these information via `Socket::CPU()` which retuns a `CPU` instance.
116-
117-
> Why not just retrieving a `std::vector<CPU>`?
118-
> The reason for this lies within how linux handles CPUs. For linux systems, the cores of a multi core CPU are
119-
> considered as different physical CPUs.
120-
> Thus, I added the `Socket` layer to make clear, that multiple elements in the yielded `std::vector<Socket>` vector
121-
> mean that there are two CPUs on two different sockets installed.
122-
123-
The following methods are available for `CPU`:
124-
125-
- `const std::string& CPU::vendor() const` "GenuineIntel"
126-
- `const std::string& CPU::modelName() const` "Intel(R) Core(TM) i7-10700K CPU @ 3.80GHz"
127-
- `int64_t CPU::cacheSize_Bytes() const` 16384000
128-
- `int CPU::numPhysicalCores() const` 8
129-
- `int CPU::numLogicalCores() const` 16
130-
- `int64_t CPU::maxClockSpeed_MHz() const` 5100000
131-
- `int64_t CPU::regularClockSpeed_MHz() const` 3800000
132-
- `int64_t CPU::minClockSpeed_MHz() const` 1800000
133-
- `int64_t CPU::currentClockSpeed_MHz() const` 4700189
134-
- `const std::vector<std::string>& CPU::flags() cosnt` {"SSE", "AVX", ...}
135-
136-
### GPU
137-
138-
You can also get information about all installed GPUs using hwinfo.
139-
`getAllGPUs()` returns a `std::vector<GPU>`. A `GPU` object represents a physical GPU.
140-
141-
The following methods are available for `GPU`:
142-
143-
- `const std::string& GPU::vendor() const` "NVIDIA"
144-
- `const std::string& GPU::name() const` "NVIDIA GeForce RTX 3070 Ti"
145-
- `const std::string& GPU::driverVersion() const` "31.0.15.2698" (empty for linux)
146-
- `int64_t GPU::memory_Bytes() const` 8190000000
147-
- `int64_t GPU::min_frequency_MHz() const`
148-
- `int64_t GPU::current_frequency_MHz() const`
149-
- `int64_t GPU::max_frequency_MHz() const`
150-
- `int GPU::id() const` 0
151-
152-
### RAM
153-
154-
TODO
155-
156-
### OS
157-
158-
TODO
159-
160-
### Baseboard
161-
162-
TODO
163-
164-
### Disk
165-
166-
TODO
167-
168104
## Build `hwinfo`
169105

170106
> Requirements: git, cmake, c++ compiler (gcc, clang, MSVC)
@@ -176,9 +112,10 @@ TODO
176112
2. Build using cmake:
177113
```bash
178114
mkdir build
179-
cmake -B build -DCMAKE_BUILD_TYPE=Release # -DNO_OCL=ON (for C++11)
115+
cmake -B build -DCMAKE_BUILD_TYPE=Release
180116
cmake --build build
181117
```
118+
This builds static and dynamic libraries. Static library cmake targets are named `<target>_static` (e.g. `hwinfo_static`)
182119
183120
## Example
184121
@@ -261,7 +198,41 @@ Disk 4:
261198

262199
```
263200
264-
## Directly including `hwinfo` in your cmake project
201+
## include hwinfo to cmake project
202+
203+
### Include installed version
204+
205+
1. Install hwinfo
206+
```
207+
git clone https://github.com/lfreist/hwinfo && cd hwinfo
208+
mkdir build
209+
cmake -B build && cmake --build build
210+
cmake --install build
211+
```
212+
2. Simply add the following to your `CMakeLists.txt` file:
213+
```cmake
214+
# file: CMakeLists.txt
215+
216+
find_package(hwinfo REQUIRED)
217+
```
218+
3. Include `hwinfo` into your `.cpp/.h` files:
219+
```c++
220+
// file: your_executable.cpp
221+
222+
#include <hwinfo/hwinfo.h>
223+
224+
int main(int argc, char** argv) {
225+
// Your code
226+
}
227+
```
228+
4. Link it in cmake
229+
```cmake
230+
add_executable(your_executable your_executable.cpp)
231+
target_link_libraries(your_executable PUBLIC hwinfo::hwinfo)
232+
```
233+
234+
235+
### As git submodule
265236
266237
1. Download `hwinfo` into your project (e.g. in `<project-root>/third_party/hwinfo`)
267238
```

cmake/hwinfoConfig.cmake.in

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
@PACKAGE_INIT@
2+
3+
include("${CMAKE_CURRENT_LIST_DIR}/hwinfoTargets.cmake")

examples/CMakeLists.txt

+16-5
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,21 @@
1-
include(FetchContent)
2-
FetchContent_Declare(
1+
find_package(fmt QUIET)
2+
3+
if (NOT fmt_FOUND)
4+
message(" -> libfmt not found. Fetching from github...")
5+
include(FetchContent)
6+
FetchContent_Declare(
37
fmt
48
GIT_REPOSITORY https://github.com/fmtlib/fmt.git
59
GIT_TAG 11.0.1)
6-
FetchContent_MakeAvailable(fmt)
10+
FetchContent_MakeAvailable(fmt)
11+
else ()
12+
message(" -> using installed libfmt")
13+
endif ()
14+
15+
add_executable(system_info system_infoMain.cpp)
16+
add_executable(system_info_stand_alone system_infoMain.cpp)
717

8-
add_executable(system_infoMain system_infoMain.cpp)
18+
target_link_libraries(system_info PRIVATE hwinfo::hwinfo fmt::fmt)
19+
target_link_libraries(system_info_stand_alone PRIVATE hwinfo::hwinfo_static fmt::fmt)
920

10-
target_link_libraries(system_infoMain PRIVATE hwinfo::hwinfo fmt::fmt)
21+
install(TARGETS system_info system_info_stand_alone)

include/hwinfo/battery.h

-4
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33

44
#pragma once
55

6-
#ifdef HWINFO_BATTERY
7-
86
#include <hwinfo/platform.h>
97

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

5250
} // namespace hwinfo
53-
54-
#endif // HWINFO_BATTERY

include/hwinfo/cpu.h

-4
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33

44
#pragma once
55

6-
#ifdef HWINFO_CPU
7-
86
#include <hwinfo/platform.h>
97
#include <hwinfo/utils/wmi_wrapper.h>
108

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

8078
} // namespace hwinfo
81-
82-
#endif // HWINFO_CPU

include/hwinfo/cpuid.h

+1-5
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@
33

44
#pragma once
55

6-
#ifdef HWINFO_CPU
7-
8-
#include "hwinfo/platform.h"
6+
#include <hwinfo/platform.h>
97

108
#if defined(HWINFO_X86)
119

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

5654
#endif // HWINFO_X86
57-
58-
#endif // HWINFO_CPU

include/hwinfo/disk.h

-4
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33

44
#pragma once
55

6-
#ifdef HWINFO_DISK
7-
86
#include <hwinfo/platform.h>
97

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

4341
} // namespace hwinfo
44-
45-
#endif // HWINFO_DISK

include/hwinfo/gpu.h

-4
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33

44
#pragma once
55

6-
#ifdef HWINFO_GPU
7-
86
#include <hwinfo/platform.h>
97

108
#include <cstdint>
@@ -45,5 +43,3 @@ class HWINFO_API GPU {
4543

4644
std::vector<GPU> getAllGPUs();
4745
} // namespace hwinfo
48-
49-
#endif // HWINFO_GPU

include/hwinfo/mainboard.h

+4-8
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33

44
#pragma once
55

6-
#ifdef HWINFO_MAINBOARD
7-
86
#include <hwinfo/platform.h>
97

108
#include <string>
@@ -18,10 +16,10 @@ class HWINFO_API MainBoard {
1816
MainBoard();
1917
~MainBoard() = default;
2018

21-
const std::string& vendor() const;
22-
const std::string& name() const;
23-
const std::string& version() const;
24-
const std::string& serialNumber() const;
19+
HWI_NODISCARD const std::string& vendor() const;
20+
HWI_NODISCARD const std::string& name() const;
21+
HWI_NODISCARD const std::string& version() const;
22+
HWI_NODISCARD const std::string& serialNumber() const;
2523

2624
private:
2725
std::string _vendor;
@@ -31,5 +29,3 @@ class HWINFO_API MainBoard {
3129
};
3230

3331
} // namespace hwinfo
34-
35-
#endif // HWINFO_MAINBOARD

include/hwinfo/os.h

-4
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33

44
#pragma once
55

6-
#ifdef HWINFO_OS
7-
86
#include <hwinfo/platform.h>
97

108
#include <string>
@@ -35,5 +33,3 @@ class HWINFO_API OS {
3533
};
3634

3735
} // namespace hwinfo
38-
39-
#endif // HWINFO_OS

include/hwinfo/ram.h

-4
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33

44
#pragma once
55

6-
#ifdef HWINFO_RAM
7-
86
#include <hwinfo/platform.h>
97

108
#include <cstdint>
@@ -39,5 +37,3 @@ class HWINFO_API Memory {
3937
};
4038

4139
} // namespace hwinfo
42-
43-
#endif // HWINFO_RAM

0 commit comments

Comments
 (0)