diff options
author | Dominic Radermacher <dominic@familie-radermacher.ch> | 2022-11-13 14:26:16 +0100 |
---|---|---|
committer | Dominic Radermacher <dominic@familie-radermacher.ch> | 2022-11-13 14:26:16 +0100 |
commit | ebb7bf22b8997774491389e0655e966df5874b7c (patch) | |
tree | e694a2e641aa4f0423dd27193407642d954be752 | |
parent | fe5cfbc68464e9bfd8da7ba3f0fcbe357afd8a30 (diff) |
switch to cmake as build system
-rw-r--r-- | .gitignore | 2 | ||||
-rw-r--r-- | CMakeLists.txt | 65 | ||||
-rw-r--r-- | Makefile | 37 | ||||
-rw-r--r-- | README | 19 | ||||
-rw-r--r-- | cmake/gitversion.cmake | 56 | ||||
-rwxr-xr-x | compile.sh | 2 | ||||
-rw-r--r-- | include/lan951x-led-ctl.h | 2 | ||||
-rw-r--r-- | src/lan951x-led-ctl.c | 16 |
8 files changed, 150 insertions, 49 deletions
diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..740923b --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +# generated files during cmake build process +/build/* diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..f46e1b2 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,65 @@ +cmake_minimum_required(VERSION 3.16) + +project(lan951x-led-ctl LANGUAGES C) + +list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/cmake") + +set(CMAKE_INSTALL_PREFIX /usr) +set(CMAKE_CXX_STANDARD 17) +set(CMAKE_CXX_STANDARD_REQUIRED ON) + +set(CMAKE_VERBOSE_MAKEFILE ON) + +find_package(PkgConfig REQUIRED) + +pkg_check_modules(libusb REQUIRED libusb-1.0) + +set(HEADERS + include/lan951x-led-ctl.h +) + +add_executable(${PROJECT_NAME} + ${HEADERS} +) + +target_include_directories(${PROJECT_NAME} PUBLIC + ${CMAKE_BINARY_DIR} # HB9HEI - location of generated version.h + ${CMAKE_SOURCE_DIR}/include +) + +target_link_libraries(${PROJECT_NAME} PRIVATE + usb-1.0 +) + +target_sources(${PROJECT_NAME} PRIVATE + src/lan951x-led-ctl.c +) + +add_dependencies(${PROJECT_NAME} + git-version +) + +add_compile_definitions( +) + +target_compile_options(${PROJECT_NAME} PUBLIC + -g + -Os + -std=c11 + -fstack-protector-strong + -Wall + -Wextra + -Werror + -Wstrict-prototypes + -Wconversion + -Wmissing-prototypes + -Wshadow + -Wunused +) + +install(TARGETS ${PROJECT_NAME} DESTINATION bin) + +# 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 +) diff --git a/Makefile b/Makefile deleted file mode 100644 index 5dd6838..0000000 --- a/Makefile +++ /dev/null @@ -1,37 +0,0 @@ -ECHO = /bin/echo -e -SHELL = /bin/sh -RM = /bin/rm -f -CC = gcc -STRIP = strip - -GIT_VERSION := $(shell git --no-pager describe --tags --dirty |sed 's/\([^-]*-g\)/r\1/;s/-/./g') - -CFLAGS = -g -Os -std=c11 -I./include -Wall -Wstrict-prototypes -Wconversion -CFLAGS += -Wmissing-prototypes -Wshadow -Wextra -Wunused -CFLAGS += -DVERSION=\"$(GIT_VERSION)\" -LDFLAGS = -lusb-1.0 - -PROGS = lan951x-led-ctl - -all: $(PROGS) - -clean: - $(RM) src/*.o -tidy: - $(RM) src/*.o $(PROGS) - -install: $(PROGS) - @$(ECHO) "\t==> Installing programs to $(DESTDIR)/bin" - @install -m 0755 -d $(DESTDIR)/bin - @install -m 0755 -t $(DESTDIR)/bin $(PROGS) - -# Generic instructions -src/%.o: src/%.c - @$(ECHO) "\t--> Compiling `basename $<`" - @$(CC) $(CFLAGS) -c $< -o $@ - -# Specific programs -lan951x-led-ctl: src/lan951x-led-ctl.o - @$(ECHO) "\t==> Linking objects to output file $@" - @$(CC) $(CFLAGS) $(LDFLAGS) $+ -o $@ - @$(STRIP) $@ @@ -1,14 +1,23 @@ -lan951x-led-ctl is a command line tool to control the LEDs connected to a -LAN9512 or LAN9514 ethernet controller. +lan951x-led-ctl is a command line tool written by Dominic Radermacher +to control the LEDs connected to a LAN9512 or LAN9514 ethernet controller. Usually those LEDs show the ethernet status like "Link/Activity", "Speed" and "Duplex" mode. Using this tool you can turn them off, if you don't like a disco in your room at night ;-) Or you can turn them on/off from any shell script to show whatever status you wish. -These ethernet and USB controllers are used on many embedded computers like -Raspberry Pi (until Version 3 B - but not 3 B Plus), Olinuxino and many more. +These LAN951x ethernet and USB controllers are used on many embedded computers +like Raspberry Pi (until Version 3 B - but not 3 B Plus), Olinuxino and many more. Further info can be found at: -https://familie-radermacher.ch/dominic/computer/raspberry-pi/lan951x-led-ctl/ +https://dominic.familie-radermacher.ch/computer/raspberry-pi/lan951x-led-ctl/ + +The Git repo with the latest version is located at: +https://git.familie-radermacher.ch/linux/lan951x-led-ctl.git + +Compile: +mkdir -p build && cmake --fresh -B build && cmake --build build + +Install: +cmake --install build diff --git a/cmake/gitversion.cmake b/cmake/gitversion.cmake new file mode 100644 index 0000000..92483ea --- /dev/null +++ b/cmake/gitversion.cmake @@ -0,0 +1,56 @@ +# 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 + ERROR_QUIET) +# Check whether we got any revision (which isn't always the case, e.g. when +# someone downloaded a zip file instead of a checkout) +if ("${GIT_COMMIT_HASH}" STREQUAL "") + set(GIT_BRANCH "N/A") + set(GIT_COMMITS "") + set(GIT_COMMIT_HASH "N/A") + set(GIT_COMMIT_SHORT "N/A") + set(GIT_DIFF "") + set(GIT_TAG "N/A") +else() + execute_process(COMMAND + bash -c "git diff --quiet --exit-code || echo +" + OUTPUT_VARIABLE GIT_DIFF) + execute_process(COMMAND + bash -c "git describe --always --tags |cut -f1 -d'-'" + OUTPUT_VARIABLE GIT_TAG ERROR_QUIET) + execute_process(COMMAND + bash -c "git describe --always --tags |cut -f2 -d'-'" + OUTPUT_VARIABLE GIT_COMMITS ERROR_QUIET) + execute_process(COMMAND + git rev-parse --abbrev-ref HEAD + OUTPUT_VARIABLE GIT_BRANCH) + string(STRIP "${GIT_COMMIT_HASH}" GIT_COMMIT_HASH) + string(SUBSTRING "${GIT_COMMIT_HASH}" 1 7 GIT_COMMIT_SHORT) + string(STRIP "${GIT_BRANCH}" GIT_BRANCH) + string(STRIP "${GIT_COMMITS}" GIT_COMMITS) + string(STRIP "${GIT_DIFF}" GIT_DIFF) + string(STRIP "${GIT_TAG}" GIT_TAG) + if (${GIT_COMMITS} STREQUAL ${GIT_TAG}) + set(GIT_COMMITS "0") + endif() +endif() + +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}\"") +if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/version.h) + file(READ ${CMAKE_CURRENT_SOURCE_DIR}/version.h VERSION_) +else() + set(VERSION_ "") +endif() +if (NOT "${VERSION}" STREQUAL "${VERSION_}") + file(WRITE "${CMAKE_CURRENT_SOURCE_DIR}/version.h" "${VERSION}") +endif() diff --git a/compile.sh b/compile.sh new file mode 100755 index 0000000..96701ce --- /dev/null +++ b/compile.sh @@ -0,0 +1,2 @@ +#!/bin/sh +mkdir -p build && cmake --fresh -B build && cmake --build build diff --git a/include/lan951x-led-ctl.h b/include/lan951x-led-ctl.h index 8027020..271659f 100644 --- a/include/lan951x-led-ctl.h +++ b/include/lan951x-led-ctl.h @@ -1,7 +1,7 @@ /* lan951x-led-ctl - control LEDs of LAN951X ethernet/usb controllers - Copyright (C) 2015-2020 Dominic Radermacher <dominic@familie-radermacher.ch> + Copyright (C) 2015-2022 Dominic Radermacher <dominic@familie-radermacher.ch> This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License version 3 as diff --git a/src/lan951x-led-ctl.c b/src/lan951x-led-ctl.c index 128bf0d..bff250d 100644 --- a/src/lan951x-led-ctl.c +++ b/src/lan951x-led-ctl.c @@ -1,7 +1,7 @@ /* lan951x-led-ctl - control LEDs of LAN951X ethernet/usb controllers - Copyright (C) 2015-2020 Dominic Radermacher <dominic@familie-radermacher.ch> + Copyright (C) 2015-2022 Dominic Radermacher <dominic@familie-radermacher.ch> This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License version 3 as @@ -22,7 +22,9 @@ #include <string.h> /* strlen() */ #include <stdint.h> #include <libusb-1.0/libusb.h> + #include "lan951x-led-ctl.h" +#include "version.h" /* global variables */ int led_arr[3] = { MODE_KEEP, MODE_KEEP, MODE_KEEP }; @@ -102,9 +104,11 @@ void usage(void) void about(void) { - printf("lan951x-led-ctl %s programmed by Dominic Radermacher\n", VERSION); - printf("For further info or latest version see\n"); - printf("https://familie-radermacher.ch/dominic/computer/raspberry-pi/lan951x-led-ctl/\n"); + printf("lan951x-led-ctl %s programmed by Dominic Radermacher\n\n", VERSION); + printf("For further info plese visit\n"); + printf("https://dominic.familie-radermacher.ch/computer/raspberry-pi/lan951x-led-ctl/\n\n"); + printf("The latest version can be found in my git repo:\n"); + printf("https://git.familie-radermacher.ch/linux/lan951x-led-ctl.git\n"); exit(1); } @@ -112,7 +116,7 @@ int parse_args(int argc, char **argv) { int i; - for (i=1; i<argc; i++) { + for (i=1; i<argc; ++i) { char *p=argv[i]; if (strncmp(p, "--fdx=", 6) == 0) { led_arr[DUPIDX] = ledmode(p+6); @@ -153,7 +157,7 @@ int main(int argc, char* argv[]) exit(1); } lan951x_rd_reg(handle, LED_GPIO_CFG, &val); - for (int i=0; i < 3; i++) { + for (int i=0; i < 3; ++i) { if (led_arr[i] == MODE_ON) { val &= ~(gp_mask[i] & (GP_ALLCTL|GP_ALLDAT)); val |= (gp_mask[i] & GP_ALLDIR); |