From 53987c4150ddad289fd7bb96eee7695d0b3b7be4 Mon Sep 17 00:00:00 2001 From: Dominic Radermacher Date: Wed, 5 Oct 2022 18:08:06 +0200 Subject: [PATCH] cosmetic changes (some are overdue since many years) --- CMakeLists.txt | 70 ++++++++++++++++++++---------------------- cmake/gitversion.cmake | 13 +++++--- src/libptouch.c | 10 +++--- src/ptouch-print.c | 28 ++++++++--------- 4 files changed, 61 insertions(+), 60 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 94a9a13..949010d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,10 +1,11 @@ cmake_minimum_required(VERSION 3.15) + project(ptouch-print C) -# Configure CMake -set(CMAKE_C_STANDARD 11) list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/cmake") +set(CMAKE_C_STANDARD 11) + # Configure required dependencies find_package(Gettext REQUIRED) find_package(GD REQUIRED) @@ -15,18 +16,41 @@ find_package(Intl REQUIRED) pkg_check_modules(LIBUSB REQUIRED libusb-1.0) # Configure project executable -add_executable(ptouch-print) +add_executable(${PROJECT_NAME}) -target_sources(ptouch-print PUBLIC - version.h +target_include_directories(${PROJECT_NAME} PUBLIC + ${CMAKE_BINARY_DIR} # HB9HEI - location of generated version.h + ${CMAKE_SOURCE_DIR}/include + ${GD_INCLUDE_DIR} + ${LIBUSB_INCLUDE_DIRS} + ${Intl_INCLUDE_DIRS} +) + +target_link_libraries(${PROJECT_NAME} PRIVATE + ${GD_LIBRARIES} + ${LIBUSB_LIBRARIES} + ${LIBUSB_LINK_LIBRARIES} + ${Intl_LIBRARY} +) + +target_sources(${PROJECT_NAME} PRIVATE include/ptouch.h include/gettext.h src/libptouch.c src/ptouch-print.c ) -# Configure compiler -target_compile_options(ptouch-print PUBLIC +add_dependencies(${PROJECT_NAME} + git-version +) + +target_compile_definitions(${PROJECT_NAME} PUBLIC + LOCALEDIR="${CMAKE_INSTALL_LOCALEDIR}" + USING_CMAKE=1 + PACKAGE="ptouch-print" +) + +target_compile_options(${PROJECT_NAME} PUBLIC -g -Wall -Wextra @@ -35,37 +59,11 @@ target_compile_options(ptouch-print PUBLIC -fPIC ) -target_compile_definitions(ptouch-print PUBLIC - LOCALEDIR="${CMAKE_INSTALL_LOCALEDIR}" - USING_CMAKE=1 - PACKAGE="ptouch-print" +# HB9HEI - custom target that produces version.h (req. cmake 3.0) +add_custom_target(git-version ALL + ${CMAKE_COMMAND} -P ${CMAKE_CURRENT_SOURCE_DIR}/cmake/gitversion.cmake ) -target_include_directories(ptouch-print PUBLIC - include - ${GD_INCLUDE_DIR} - ${LIBUSB_INCLUDE_DIRS} - ${CMAKE_BINARY_DIR} - ${Intl_INCLUDE_DIRS} -) - -# Configure linker -target_link_libraries(ptouch-print - ${GD_LIBRARIES} - ${LIBUSB_LIBRARIES} - ${LIBUSB_LINK_LIBRARIES} - ${Intl_LIBRARY} -) - -# Add a custom command that produces version.h, plus a dummy output that's -# not produced, in order to force gitversion.cmake to always be re-run -# before the build -ADD_CUSTOM_COMMAND( - OUTPUT ${CMAKE_CURRENT_SOURCE_DIR}/version.h - ${CMAKE_BINARY_DIR}/_version.h - COMMAND ${CMAKE_COMMAND} -P - ${CMAKE_CURRENT_SOURCE_DIR}/cmake/gitversion.cmake) - include(GNUInstallDirs) set(CMAKE_INSTALL_PREFIX "/usr") diff --git a/cmake/gitversion.cmake b/cmake/gitversion.cmake index d0c2a96..92483ea 100644 --- a/cmake/gitversion.cmake +++ b/cmake/gitversion.cmake @@ -1,3 +1,6 @@ +# HB9HEI - required for autogen version.h +find_package(Git REQUIRED) + # Get commit hash execute_process(COMMAND git log --format='%H' -n 1 OUTPUT_VARIABLE GIT_COMMIT_HASH @@ -35,11 +38,11 @@ else() endif() endif() -set(VERSION "const char* GIT_BRANCH=\"${GIT_BRANCH}\"; -const char* GIT_COMMIT=\"${GIT_COMMIT_SHORT}\"; -const char* GIT_COMMITS=\"${GIT_COMMITS}\"; -const char* GIT_TAG=\"${GIT_TAG}\"; -const char* VERSION=\"${GIT_TAG}-r${GIT_COMMITS}-g${GIT_COMMIT_SHORT}${GIT_DIFF}\"; +set(VERSION "#define GIT_BRANCH \"${GIT_BRANCH}\" +#define GIT_COMMIT \"${GIT_COMMIT_SHORT}\" +#define GIT_COMMITS \"${GIT_COMMITS}\" +#define GIT_TAG \"${GIT_TAG}\" +#define VERSION \"${GIT_TAG}.r${GIT_COMMITS}.g${GIT_COMMIT_SHORT}${GIT_DIFF}\" ") message(DEBUG "Generated Version: \"${VERSION}\"") diff --git a/src/libptouch.c b/src/libptouch.c index b68cb16..b4389dc 100644 --- a/src/libptouch.c +++ b/src/libptouch.c @@ -114,7 +114,7 @@ int ptouch_open(ptouch_dev *ptdev) libusb_free_device_list(devs, 1); return -1; } - for (int k=0; ptdevs[k].vid > 0; k++) { + for (int k=0; ptdevs[k].vid > 0; ++k) { if ((desc.idVendor == ptdevs[k].vid) && (desc.idProduct == ptdevs[k].pid) && (ptdevs[k].flags >= 0)) { fprintf(stderr, _("%s found on USB bus %d, device %d\n"), ptdevs[k].name, @@ -255,7 +255,7 @@ int ptouch_eject(ptouch_dev ptdev) void ptouch_rawstatus(uint8_t raw[32]) { fprintf(stderr, _("debug: dumping raw status bytes\n")); - for (int i=0; i<32; i++) { + for (int i=0; i<32; ++i) { fprintf(stderr, "%02x ", raw[i]); if (((i+1) % 16) == 0) { fprintf(stderr, "\n"); @@ -281,7 +281,7 @@ int ptouch_getstatus(ptouch_dev ptdev) fprintf(stderr, _("read error: %s\n"), libusb_error_name(r)); return -1; } - tries++; + ++tries; if (tries > 10) { fprintf(stderr, _("timeout while waiting for status response\n")); return -1; @@ -291,7 +291,7 @@ int ptouch_getstatus(ptouch_dev ptdev) if (buf[0]==0x80 && buf[1]==0x20) { memcpy(ptdev->status, buf, 32); ptdev->tape_width_px=0; - for (i=0; tape_info[i].mm > 0; i++) { + for (i=0; tape_info[i].mm > 0; ++i) { if (tape_info[i].mm == buf[10]) { ptdev->tape_width_px=tape_info[i].px; } @@ -359,7 +359,7 @@ int ptouch_sendraster(ptouch_dev ptdev, uint8_t *data, size_t len) void ptouch_list_supported() { printf("Supported printers (some might have quirks)\n"); - for (int i=0; ptdevs[i].vid > 0; i++) { + for (int i=0; ptdevs[i].vid > 0; ++i) { if ((ptdevs[i].flags & FLAG_PLITE) != FLAG_PLITE) { printf("\t%s\n", ptdevs[i].name); } diff --git a/src/ptouch-print.c b/src/ptouch-print.c index 7924413..6106b7a 100644 --- a/src/ptouch-print.c +++ b/src/ptouch-print.c @@ -201,7 +201,7 @@ int find_fontsize(int want_px, char *font, char *text) int save=0; int brect[8]; - for (int i=4; ; i++) { + for (int i=4; ; ++i) { if (gdImageStringFT(NULL, &brect[0], -1, font, i, 0.0, 0, 0, text) != NULL) { break; } @@ -244,7 +244,7 @@ gdImage *render_text(char *font, char *line[], int lines, int tape_width) fsz=fontsize; printf(_("setting font size=%i\n"), fsz); } else { - for (i=0; i x) { x=tmp; @@ -267,7 +267,7 @@ gdImage *render_text(char *font, char *line[], int lines, int tape_width) /* gdImageStringFT(im,brect,fg,fontlist,size,angle,x,y,string) */ /* find max needed line height for ALL lines */ int max_height=0; - for (i=0; i= argc) || (argv[i+1][0] == '-')) { break; } - i++; + ++i; } } else if (strcmp(&argv[i][1], "-version") == 0) { printf(_("ptouch-print version %s by Dominic Radermacher\n"), VERSION); @@ -498,7 +498,7 @@ int main(int argc, char *argv[]) return 1; } tape_width=ptouch_get_tape_width(ptdev); - for (i=1; i= argc) || (argv[i+1][0] == '-')) { break; } - i++; + ++i; line[lines]=argv[i]; } if (lines) {