From 314107bcf71ef79c5568f59b8331b708b3b3ffbc Mon Sep 17 00:00:00 2001 From: Dominic Radermacher Date: Mon, 22 Jan 2024 11:41:02 +0100 Subject: switch to cmake --- .gitignore | 7 ++--- CMakeLists.txt | 73 +++++++++++++++++++++++++++++++++++++++++++++++ Makefile | 37 ------------------------ README | 2 +- cmake/gitversion.cmake | 56 ++++++++++++++++++++++++++++++++++++ compile.sh | 2 ++ include/lan7800-led-ctl.h | 2 +- src/lan7800-led-ctl.c | 5 ++-- 8 files changed, 138 insertions(+), 46 deletions(-) create mode 100644 CMakeLists.txt delete mode 100644 Makefile create mode 100644 cmake/gitversion.cmake create mode 100755 compile.sh diff --git a/.gitignore b/.gitignore index 6ec8668..740923b 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,2 @@ -# C-Objects -*.o - -# Binary -lan7800-led-ctl +# generated files during cmake build process +/build/* diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..bba1a01 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,73 @@ +cmake_minimum_required(VERSION 3.16) + +set(CMAKE_DISABLE_SOURCE_CHANGES ON) +set(CMAKE_DISABLE_IN_SOURCE_BUILD ON) + +# This needs to come before project() to skip the compiler config process +if ("${CMAKE_BINARY_DIR}" STREQUAL "${CMAKE_SOURCE_DIR}") + message(FATAL_ERROR "In-source builds are disabled. +Please create a subfolder and use `cmake ..` inside it. +NOTE: cmake will now create CMakeCache.txt and CMakeFiles/*. +You must delete them, or cmake will refuse to work.") +endif() # yes, end-markers and even else() need empty parentheses + +project(lan7800-led-ctl LANGUAGES C) + +list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/cmake") + +set(CMAKE_INSTALL_PREFIX /usr) +set(CMAKE_C_STANDARD 11) +set(CMAKE_C_STANDARD_REQUIRED ON) + +set(CMAKE_VERBOSE_MAKEFILE ON) + +find_package(PkgConfig REQUIRED) + +pkg_check_modules(libusb REQUIRED libusb-1.0) + +set(HEADERS + include/lan7800-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/lan7800-led-ctl.c +) + +add_dependencies(${PROJECT_NAME} + git-version +) + +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 aa2696c..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 += -DVERSION=\"$(GIT_VERSION)\" -CFLAGS += -Wmissing-prototypes -Wshadow -Wextra -Wunused -LDFLAGS = -lusb-1.0 - -PROGS = lan7800-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 -lan7800-led-ctl: src/lan7800-led-ctl.o - @$(ECHO) "\t==> Linking objects to output file $@ $(GIT_VERSION)\n" - @$(CC) $(CFLAGS) $(LDFLAGS) $+ -o $@ - @$(STRIP) $@ diff --git a/README b/README index 48a8b03..40d2658 100644 --- a/README +++ b/README @@ -10,4 +10,4 @@ These ethernet and USB controllers are used on embedded computers like Raspberry Pi 3B+. Further info can be found at: -https://familie-radermacher.ch/dominic/computer/raspberry-pi/lan7800-led-ctl/ +https://dominic.familie-radermacher.ch/computer/raspberry-pi/lan7800-led-ctl/ 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/lan7800-led-ctl.h b/include/lan7800-led-ctl.h index d8a57a4..f477562 100644 --- a/include/lan7800-led-ctl.h +++ b/include/lan7800-led-ctl.h @@ -1,7 +1,7 @@ /* lan7800-led-ctl - control LEDs of LAN7800 ethernet/usb controllers - Copyright (C) 2018 Dominic Radermacher + Copyright (C) 2018-2024 Dominic Radermacher 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/lan7800-led-ctl.c b/src/lan7800-led-ctl.c index 6934710..109bd3e 100644 --- a/src/lan7800-led-ctl.c +++ b/src/lan7800-led-ctl.c @@ -1,7 +1,7 @@ /* lan7800-led-ctl - control LEDs of LAN7800 ethernet/usb controllers - Copyright (C) 2018 Dominic Radermacher + Copyright (C) 2018-2024 Dominic Radermacher 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 @@ -24,6 +24,7 @@ #include #include "lan7800-led-ctl.h" +#include "version.h" /* generated by cmake */ /* global variables */ int led_arr[3] = { MODE_KEEP, MODE_KEEP, MODE_KEEP }; @@ -100,7 +101,7 @@ void about(void) { printf("lan7800-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/lan7800-led-ctl/\n"); + printf("https://dominic.familie-radermacher.ch/computer/raspberry-pi/lan7800-led-ctl/\n"); exit(1); } -- cgit v1.2.3