add makefile, fix annoying errors

This commit is contained in:
2026-08-13 17:39:36 +02:00
parent 73a9b4303d
commit e4e7c4bbc2
9 changed files with 129 additions and 31 deletions

100
Makefile Normal file
View File

@@ -0,0 +1,100 @@
MAKEFLAGS := -j $(shell nproc)
CC := cc
PKG_CONFIG := pkg-config
TARGET := build/linuxSmartie
BITCHY_ERRORS := \
-Wconversion \
-Wsign-conversion \
-Wshadow \
-Wformat=2 \
-Wundef \
-Wstrict-prototypes \
-Wmissing-prototypes \
-Wold-style-definition \
-Wwrite-strings \
-Wvla \
-Wcast-qual \
-Wcast-align \
-Wpointer-arith \
-Wswitch-enum \
CFLAGS := \
-std=c23 \
-Iinclude \
-Ibuild \
-D_GNU_SOURCE \
-D_POSIX_C_SOURCE=200809L \
-Wall \
-Wextra \
-Wpedantic \
-Werror \
-pedantic-errors \
-MMD -MP \
-O2 \
$(BITCHY_ERRORS)
# -Wno-c++-compat \
# -Wno-format-truncation \
# -Wno-conversion \
# -Wno-cast-align \
# -Wno-padded \
# -Wno-bad-function-cast \
LDLIBS := \
$(shell $(PKG_CONFIG) --libs libusb-1.0) \
-lm
SOURCES := $(wildcard src/*.c)
OBJECTS := $(patsubst src/%.c,build/src/%.o,$(SOURCES))
DEPS := $(OBJECTS:.o=.d)
VERSION_H := build/version.h
# glowny target, linkuje linuxSmartie, potrzebuje ${OBJECTS}, jest na samej górze wiec jest domyslny
$(TARGET): $(OBJECTS) $(VERSION_H)
$(CC) $(OBJECTS) -o $@ $(LDLIBS)
# Compile source files
build/src/%.o: src/%.c $(VERSION_H)
@mkdir -p $(dir $@)
$(CC) $(CFLAGS) -c $< -o $@
# Generate version.h from Git
$(VERSION_H): include/version.h.in
@mkdir -p $(dir $@)
@version=$$(git describe --always --long --dirty 2>/dev/null || echo unknown); \
sed "s/@VCS_TAG@/$$version/" $< > $@.tmp; \
if ! cmp -s $@.tmp $@ 2>/dev/null; then \
mv $@.tmp $@; \
else \
rm -f $@.tmp; \
fi
# Include automatically generated header dependencies
-include $(DEPS)
.PHONY: all clean install uninstall debug
all: $(TARGET)
debug: CFLAGS += -O0 -g
debug: $(TARGET)
clean:
rm -rf build
install: $(TARGET)
install -Dm755 $(TARGET) $(DESTDIR)$(HOME)/.local/bin/linuxSmartie
install_service: install
mkdir -p ~/.config/systemd/user
cp linuxSmartie.service ~/.config/systemd/user/linuxSmartie.service
systemctl --user daemon-reload
systemctl --user enable linuxSmartie
systemctl --user start linuxSmartie
uninstall:
rm -f $(DESTDIR)$(HOME)/.local/bin/linuxSmartie

View File

@@ -1,8 +1,7 @@
#ifndef LINUXSMARTIE_UTILS_H
#define LINUXSMARTIE_UTILS_H
#include <stddef.h>
#include <time.h>
#include <sys/types.h>
#if __STDC_VERSION__ >= 202000L
#define C_STD "C23"
@@ -22,11 +21,11 @@
#define P_BYTES(bytes, start) pretty_bytes((bytes), (start), (char[16]){0}, 16)
#define P_NUM(num) pretty_num((num), (char[16]){0}, 16)
int get_instance_lock(void);
int get_instance_lock();
void timespec_add_ms(struct timespec *ts, uint16_t ms);
uint64_t timespec_diff_us(const struct timespec *a, const struct timespec *b);
int64_t timespec_diff_us(const struct timespec *a, const struct timespec *b);
char *pretty_num(double num, char *buf, size_t bufsz);
@@ -34,7 +33,7 @@ char *pretty_bytes(uint64_t bytes, int8_t start_suffix, char *buf, size_t bufsz)
int get_hwmon_dir(char *path, size_t bufsz);
size_t read_file(const char *dir, const char *file, char *buffer, size_t n);
ssize_t read_file(const char *dir, const char *file, char *buffer, size_t n);
void fast_read_num_u64(const char *dir, const char *file, char *buf, uint64_t *result, uint32_t divider);

View File

@@ -49,10 +49,8 @@ executable(
'-D_POSIX_C_SOURCE=200809L',
'-Wno-c++-compat', # dont care about c++
'-Wno-format-truncation', # hates pretty_bytes/pretty_num
'-Wno-conversion', # just annoying
'-Wno-cast-align', # casting char array to gpu_metrics
'-Wno-padded', # mostly for gpu_metrics.h
'-Wno-bad-function-cast' # cpu power bug
],
install : true,
)

View File

@@ -80,7 +80,7 @@ static float aio_get_water_temp(libusb_device_handle *dev) {
static uint16_t aio_get_pump_speed(libusb_device_handle *dev) {
POST_AND_GET_RESP(0x31, 5);
return resp[3] << 8 | resp[4];
return (uint8_t)(resp[3] << 8 | resp[4]);
}
static enum aio_pump_mode_t aio_get_pump_mode(libusb_device_handle *dev) {
@@ -91,7 +91,7 @@ static enum aio_pump_mode_t aio_get_pump_mode(libusb_device_handle *dev) {
static void aio_get_fan_speed(libusb_device_handle *dev, uint16_t *speeds) {
for (uint8_t i = 0; i < 2; i++) {
POST_AND_GET_RESP2(0x41, i, 6);
speeds[i] = resp[4] << 8 | resp[5];
speeds[i] = (uint8_t)(resp[4] << 8 | resp[5]);
}
}
@@ -122,7 +122,7 @@ static void aio_set_pump_mode(libusb_device_handle *dev, const enum aio_pump_mod
if (mode == last_mode)
return;
applog(LOG_DBG, "setting pump to %u", mode);
POST_AND_GET_RESP2(0x32, mode, 6);
POST_AND_GET_RESP2(0x32, (uint8_t)mode, 6);
last_mode = mode;
}
@@ -182,7 +182,7 @@ static void aio_set_fan_curve(libusb_device_handle *dev, const struct curve_poin
req[9 + i] = padded[i].duty;
// set curve for both fans
for (int i = 0; i < 2; i++) {
for (uint8_t i = 0; i < 2; i++) {
req[1] = i;
aio_post(dev, req, sizeof(req), resp, sizeof(resp));
}
@@ -230,7 +230,7 @@ void aio_update(struct aio_t *aio) {
fan_duty = clamp(fan_duty, params.fans.min_duty, params.fans.max_duty);
applog(LOG_DBG, "Fan duty %.3f, error %.4f, slope %.4f", (double)fan_duty, (double)error * (double)params.fans.ctrl.Ki, (double)slope * (double)params.fans.ctrl.Kd);
SET_FANS_DUTY(aio->handle, fan_duty);
SET_FANS_DUTY(aio->handle, (uint8_t)fan_duty);
/* pump logic */
static int16_t counter = 0;

View File

@@ -10,7 +10,7 @@ static uint64_t max_energy_range_uj = 0;
void cpu_detect(struct cpu_sensors_t *t, const char *pci_dev) {
t->module = unknown;
t->num_cores = sysconf(_SC_NPROCESSORS_ONLN);
t->num_cores = (uint8_t)sysconf(_SC_NPROCESSORS_ONLN);
snprintf(t->hwmon, sizeof(t->hwmon), "/sys/bus/pci/devices/%s/hwmon", pci_dev);
const int ret = get_hwmon_dir(t->hwmon, sizeof(t->hwmon));
@@ -61,7 +61,7 @@ void cpu_update(struct cpu_sensors_t *c) {
uint64_t t = 0;
sprintf(p, "/sys/devices/system/cpu/cpu%d/cpufreq/", i);
fast_read_num_u64(p, "cpuinfo_avg_freq", buf, &t, 1000);
c->avg_freq += t;
c->avg_freq += (uint32_t)t;
}
c->avg_freq /= (c->num_cores > 1) ? c->num_cores : 1;

View File

@@ -67,17 +67,17 @@ void update_from_metrics(struct gpu_sensors_t *g) {
/* rx5700xt, rx7800xt, rx9070xt */
case 13: {
const struct gpu_metrics_v1_3 *metrics = (struct gpu_metrics_v1_3 *)raw_metrics;
g->usage_core = metrics->average_gfx_activity;
g->usage_umc = metrics->average_umc_activity;
g->usage_media = metrics->average_mm_activity;
g->usage_core = (uint8_t)metrics->average_gfx_activity;
g->usage_umc = (uint8_t)metrics->average_umc_activity;
g->usage_media = (uint8_t)metrics->average_mm_activity;
g->temp_edge = metrics->temperature_edge;
g->temp_hotspot = metrics->temperature_hotspot;
g->temp_mem = metrics->temperature_mem;
g->temp_edge = (uint8_t)metrics->temperature_edge;
g->temp_hotspot = (uint8_t)metrics->temperature_hotspot;
g->temp_mem = (uint8_t)metrics->temperature_mem;
g->temp_vrm_gfx = metrics->temperature_vrgfx;
g->temp_vrm_soc = metrics->temperature_vrsoc;
g->temp_vrm_mem = metrics->temperature_vrmem;
g->temp_vrm_gfx = (uint8_t)metrics->temperature_vrgfx;
g->temp_vrm_soc = (uint8_t)metrics->temperature_vrsoc;
g->temp_vrm_mem = (uint8_t)metrics->temperature_vrmem;
// g->power_avg = metrics->average_socket_power; // jumpy on 9070xt
g->fan_speed = metrics->current_fan_speed;
@@ -93,7 +93,7 @@ void update_from_metrics(struct gpu_sensors_t *g) {
/* ryzen 5 5600g */
case 22: {
const struct gpu_metrics_v2_2 *metrics = (struct gpu_metrics_v2_2 *)raw_metrics;
g->temp_edge = metrics->temperature_gfx / 100;
g->temp_edge = (uint8_t)(metrics->temperature_gfx / 100);
break;
}

View File

@@ -131,7 +131,7 @@ static void *display_thread(void *arg) {
{
// prepare some tricky sensors
constexpr char modes[] = "QBE";
const uint16_t avg_fan_speed = (sensors->aio.fan_speed[0] + sensors->aio.fan_speed[1]) / 2;
const uint16_t avg_fan_speed = (uint16_t)((sensors->aio.fan_speed[0] + sensors->aio.fan_speed[1]) / 2);
char gpudelta[5];
const uint8_t delta = sensors->gpu.temp_hotspot - sensors->gpu.temp_edge;
@@ -180,7 +180,7 @@ static void *display_thread(void *arg) {
return nullptr;
}
int main(void) {
int main() {
applog(LOG_START, "linuxSmartie %s starting", GIT_HASH);
applog(LOG_DBG, "Compiled with %s", C_STD);

View File

@@ -68,7 +68,7 @@ void sys_get_network(struct network_t *network) {
clock_gettime(CLOCK_MONOTONIC, &current.ts);
fast_read_num_u64(path, "rx_bytes", buf, &current.rx, 1);
fast_read_num_u64(path, "tx_bytes", buf, &current.tx, 1);
const uint64_t diff_us = timespec_diff_us(&current.ts, &prev.ts);
const int64_t diff_us = timespec_diff_us(&current.ts, &prev.ts);
const uint64_t rx_delta = current.rx >= prev.rx ? current.rx - prev.rx : 0;
const uint64_t tx_delta = current.tx >= prev.tx ? current.tx - prev.tx : 0;
const double diff_s = (double)diff_us / 1000000.0;

View File

@@ -6,6 +6,7 @@
#include <stdlib.h>
#include <string.h>
#include <sys/file.h>
#include <sys/types.h>
#include <time.h>
#include <unistd.h>
@@ -42,7 +43,7 @@ inline void timespec_add_ms(struct timespec *ts, const uint16_t ms) {
}
}
inline uint64_t timespec_diff_us(const struct timespec *a, const struct timespec *b) {
inline int64_t timespec_diff_us(const struct timespec *a, const struct timespec *b) {
const int64_t sec = a->tv_sec - b->tv_sec;
const int64_t nsec = a->tv_nsec - b->tv_nsec;
return sec * 1000000LL + nsec / 1000LL;
@@ -84,7 +85,7 @@ int get_hwmon_dir(char *path, const size_t bufsz) {
/* read n bytes from path into buffer */
// file descriptors shouldnt change once opened, so we could open once, cache them, just call read() in the loop, then close all of them on program exit?
inline size_t read_file(const char *dir, const char *file, char *buffer, const size_t n) {
inline ssize_t read_file(const char *dir, const char *file, char *buffer, const size_t n) {
char path[160]; // bug alert
char *nt = stpcpy(path, dir); // nt is a pointer to the null terminator of the copied string
strcpy(nt, file); // copy to the null terminator of prev copy, append basically
@@ -108,12 +109,12 @@ inline void fast_read_num_u64(const char *dir, const char *file, char *buf, uint
inline void fast_read_num_u16(const char *dir, const char *file, char *buf, uint16_t *result, const uint32_t divider) {
read_file(dir, file, buf, 32);
*result = strtol(buf, nullptr, 10) / divider;
*result = (uint16_t)(strtol(buf, nullptr, 10) / divider);
}
inline void fast_read_num_u8(const char *dir, const char *file, char *buf, uint8_t *result, const uint32_t divider) {
read_file(dir, file, buf, 32);
*result = strtol(buf, nullptr, 10) / divider;
*result = (uint8_t)(strtol(buf, nullptr, 10) / divider);
}
inline void fast_read_num_i16(const char *dir, const char *file, char *buf, int16_t *result, const uint32_t divider) {