Port eizoctl build to CMake

The GNU Makefile approach has its limits.
This commit is contained in:
Přemysl Eric Janouch 2024-11-25 04:53:27 +01:00
parent 3219d87bc4
commit 0283189070
Signed by: p
GPG Key ID: A0420B94F92B9493
4 changed files with 47 additions and 50 deletions

7
.gitignore vendored
View File

@ -1,7 +0,0 @@
/compile_commands.json
/eizoctl
/eizoctl.exe
/eizoctltray.png
/eizoctltray.ico
/eizoctltray.o
/eizoctltray.exe

View File

@ -13,12 +13,13 @@ endif ()
# Dependencies
set (CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/liberty/cmake)
include (AddThreads)
find_package (PkgConfig REQUIRED)
pkg_check_modules (libusb libusb-1.0)
pkg_search_module (hidapi hidapi hidapi-hidraw)
option (WITH_LIBUSB "Compile with libusb utilities" ${libusb_FOUND})
option (WITH_HIDRAW "Compile with hidraw utilities" ${hidapi_FOUND})
# Generate a configuration file
configure_file (${PROJECT_SOURCE_DIR}/config.h.in
@ -48,6 +49,40 @@ if (WITH_LIBUSB)
target_link_libraries (razer-bw-te-ctl ${libusb_LIBRARIES})
endif ()
if (WITH_HIDRAW)
list (APPEND targets eizoctl)
add_executable (eizoctl eizoctl.c)
target_include_directories (eizoctl PUBLIC ${hidapi_INCLUDE_DIRS})
target_link_directories (eizoctl PUBLIC ${hidapi_LIBRARY_DIRS})
target_link_libraries (eizoctl ${hidapi_LIBRARIES})
endif ()
if (WITH_HIDRAW AND WIN32)
list (APPEND targets_gui eizoctltray)
include (IconUtils)
set (icon_png_list)
foreach (icon_size 16 32 48)
icon_to_png (eizoctltray ${PROJECT_SOURCE_DIR}/eizoctltray.svg
${icon_size} ${PROJECT_BINARY_DIR}/icons icon_png)
list (APPEND icon_png_list ${icon_png})
endforeach ()
icon_to_png (eizoctltray ${PROJECT_SOURCE_DIR}/eizoctltray.svg
256 ${PROJECT_BINARY_DIR}/icons icon_png)
set (icon_ico ${PROJECT_BINARY_DIR}/eizoctltray.ico)
icon_for_win32 (${icon_ico} "${icon_png_list}" "${icon_png}")
list (APPEND icon_ico_list )
set_property (SOURCE eizoctltray.rc
APPEND PROPERTY OBJECT_DEPENDS ${icon_ico})
add_executable (eizoctltray WIN32 eizoctl.c eizoctltray.rc)
target_compile_definitions (eizoctltray PUBLIC -DUNICODE -D_UNICODE -DTRAY)
target_link_options (eizoctltray PUBLIC -static -municode)
target_include_directories (eizoctltray PUBLIC ${hidapi_INCLUDE_DIRS})
target_link_directories (eizoctltray PUBLIC ${hidapi_LIBRARY_DIRS})
target_link_libraries (eizoctltray ${hidapi_LIBRARIES} PowrProf)
endif ()
# Generate documentation from help output
find_program (HELP2MAN_EXECUTABLE help2man)
if (NOT HELP2MAN_EXECUTABLE)
@ -77,6 +112,7 @@ install (TARGETS ${targets} DESTINATION ${CMAKE_INSTALL_BINDIR}
GROUP_READ GROUP_EXECUTE
WORLD_READ WORLD_EXECUTE
SETUID)
install (TARGETS ${targets_gui} DESTINATION ${CMAKE_INSTALL_BINDIR})
install (FILES LICENSE DESTINATION ${CMAKE_INSTALL_DOCDIR})
foreach (page ${project_MAN_PAGES})

View File

@ -1,38 +0,0 @@
UNAME_S := $(shell uname -s)
ifeq ($(UNAME_S),Linux)
HIDAPI = hidapi-hidraw
else
HIDAPI = hidapi
endif
CFLAGS += -Wall -Wextra -g -std=gnu99 $(shell pkg-config --cflags $(HIDAPI))
LDFLAGS += $(shell pkg-config --libs $(HIDAPI))
outputs = eizoctl compile_commands.json
ifeq ($(OS),Windows_NT)
outputs += eizoctltray.png eizoctltray.ico eizoctltray.o eizoctltray.exe
LDFLAGS += -static
endif
all: $(outputs)
compile_commands.json:
>$@ echo '[{'
>>$@ echo '"directory": "'"$$(pwd)"'",'
>>$@ echo '"command": "$(CC) $(CFLAGS) eizoctl.c",'
>>$@ echo '"file": "'"$$(pwd)"'/eizoctl.c"'
>>$@ echo '}]'
eizoctl: eizoctl.c
$(CC) $(CFLAGS) $(CPPFLAGS) -o $@ $^ $(LDFLAGS)
clean:
rm -f $(outputs)
ifeq ($(OS),Windows_NT)
eizoctltray.png: eizoctltray.svg
rsvg-convert --output=$@ -- $<
eizoctltray.ico: eizoctltray.png
icotool -c -o $@ -- $<
eizoctltray.o: eizoctltray.rc eizoctltray.ico
windres -o $@ $<
eizoctltray.exe: eizoctl.c eizoctltray.o
$(CC) $(CFLAGS) $(CPPFLAGS) -DUNICODE -D_UNICODE -DTRAY \
-o $@ $^ $(LDFLAGS) -mwindows -municode -lPowrProf
endif

View File

@ -33,7 +33,9 @@
#include <getopt.h>
#include <hidapi.h>
#define PROJECT_NAME "eizoctl"
#include "config.h"
#undef PROGRAM_NAME
#define PROGRAM_NAME "eizoctl"
#if defined __GNUC__
#define ATTRIBUTE_PRINTF(x, y) __attribute__((format(printf, x, y)))
@ -966,6 +968,7 @@ run(int argc, char *argv[], print_fn output, print_fn error, bool verbose)
{"restart", no_argument, NULL, 'r'},
{"events", no_argument, NULL, 'e'},
{"help", no_argument, NULL, 'h'},
{"version", no_argument, NULL, 'V'},
{}
};
@ -994,6 +997,9 @@ run(int argc, char *argv[], print_fn output, print_fn error, bool verbose)
case 'h':
output(usage, name);
return 0;
case 'V':
output(PROGRAM_NAME " " PROGRAM_VERSION "\n");
return 0;
default:
error("Unknown option\n");
error(usage, name);
@ -1397,14 +1403,14 @@ wWinMain(
.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(1)),
.hCursor = LoadCursor(NULL, IDC_ARROW),
.hbrBackground = GetSysColorBrush(COLOR_3DFACE),
.lpszClassName = TEXT(PROJECT_NAME),
.lpszClassName = TEXT(PROGRAM_NAME),
};
if (!RegisterClassEx(&wc))
return 1;
// We need a window, but it can stay hidden.
g.hwnd = CreateWindowEx(WS_EX_CONTROLPARENT,
wc.lpszClassName, TEXT(PROJECT_NAME), WS_OVERLAPPEDWINDOW,
wc.lpszClassName, TEXT(PROGRAM_NAME), WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, 600, 400, NULL, NULL, hInstance, NULL);
NOTIFYICONDATA nid = {
.cbSize = sizeof nid,
@ -1414,7 +1420,7 @@ wWinMain(
.uCallbackMessage = WM_APP + 0,
// TODO(p): LoadIconMetric is suggested for high-DPI displays.
.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(1)),
.szTip = TEXT(PROJECT_NAME),
.szTip = TEXT(PROGRAM_NAME),
};
if (!Shell_NotifyIcon(NIM_ADD, &nid)) {
message_error("Failed to add notification area icon.");