From 3887906253251f0506680c1bc5c9324d431804f7 Mon Sep 17 00:00:00 2001 From: storvik Date: Thu, 26 Sep 2019 09:31:05 +0200 Subject: [PATCH 1/3] Add libsimplemotionv2.so target and build files in build directory --- Makefile | 65 ++++++++++++++++++++++++++++++++++++++------------ tests/Makefile | 10 ++++---- 2 files changed, 54 insertions(+), 21 deletions(-) diff --git a/Makefile b/Makefile index 50bcacd..377ef5f 100644 --- a/Makefile +++ b/Makefile @@ -1,29 +1,64 @@ -# This is a Makefile for Travis CI, not tested for other purposes +BUILD_DIR := build SOURCES = $(wildcard *.c) \ drivers/serial/pcserialport.c \ - drivers/tcpip/tcpclient.c + drivers/tcpip/tcpclient.c \ + utils/crc.c -OBJECTS = $(SOURCES:%.c=%.o) +# Set library type to static or dynamic, used to +# set build directory etc. +ifeq ($(MAKECMDGOALS),libsimplemotionv2.a) + LIBRARY_TYPE = static +else + LIBRARY_TYPE = dynamic +endif -#user optionsare the ones starting with -D below -#be sure to check also user_options.h for more -CPPFLAGS = -I. -Iutils/ -DENABLE_BUILT_IN_DRIVERS -CFLAGS = -Wall -Wextra -DENABLE_BUILT_IN_DRIVERS -Iutils/ +OBJECTS = $(SOURCES:%.c=$(BUILD_DIR)/$(LIBRARY_TYPE)/%.o) -all: libsimplemotionv2.a +# User options are the ones starting with -D below, +# be sure to check also user_options.h for more +CPPFLAGS := -DENABLE_BUILT_IN_DRIVERS +CFLAGS := -g -Wall -Wextra +CFLAGS += -DENABLE_BUILT_IN_DRIVERS -libsimplemotionv2.a: $(OBJECTS) - ar rcs $@ $^ +# Include directories +INCLUDES := -I. +INCLUDES += -Idrivers/serial/ +INCLUDES += -Idrivers/tcpip/ +INCLUDES += -Iutils/ +# Linker libraries +LDLIBS := -lm + +# Linker flags +LDFLAGS := + +libsimplemotionv2.a: $(BUILD_DIR)/libsimplemotionv2.a + +libsimplemotionv2.so: CFLAGS += -fPIC +libsimplemotionv2.so: LDFLAGS += -shared +libsimplemotionv2.so: $(BUILD_DIR)/libsimplemotionv2.so + +$(BUILD_DIR)/$(LIBRARY_TYPE)/%.o: ./%.c + @mkdir -p $(@D) + $(CC) $(CFLAGS) $(INCLUDES) -c -o $@ $< + +$(BUILD_DIR)/libsimplemotionv2.a: $(OBJECTS) + @mkdir -p $(@D) + $(AR) rcs $@ $^ + +$(BUILD_DIR)/libsimplemotionv2.so: $(OBJECTS) + @mkdir -p $(@D) + $(CC) $(LDFLAGS) $^ $(LDLIBS) -o $@ + +# This target executes the test cases; the test build depends on +# gcc/clang sanitizers and those are not available except on unix alike +# platforms/targets +.PHONY: test test: - # this target executes the test cases; the test build depends on - # gcc/clag sanitizers and those are not available except on unix alike - # platforms/targets make -C tests .PHONY: clean clean: - rm -f $(OBJECTS) + $(RM) -r $(BUILD_DIR) make -C tests clean - diff --git a/tests/Makefile b/tests/Makefile index 0e248be..d3df322 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -29,7 +29,7 @@ LIB_OBJECTS = $(patsubst %.c,$(LIB_OUTDIR)/%.o,$(notdir $(LIB_SOURCES))) TEST_CASES_SRC = $(wildcard *.c) TEST_CASES = $(patsubst %.c,%,$(notdir $(TEST_CASES_SRC))) -test: $(LIB_OUTDIR) test_all +test: test_all test_all: $(TEST_CASES) @for test in $(TEST_CASES); do retval=0; ./$$test || retval=$?; if [ "$$retval" -ne 0 ]; then echo $$test: failed; exit 1; fi; echo $$test: ok; done @@ -37,15 +37,13 @@ test_all: $(TEST_CASES) $(TEST_CASES): %: %.c libsimplemotionv2.a $(CC) $(CFLAGS) $(LDFLAGS) -o $@ $< libsimplemotionv2.a -$(LIB_OUTDIR): - mkdir -p $(LIB_OUTDIR) - libsimplemotionv2.a: $(LIB_OBJECTS) ar rcs $@ $^ $(LIB_OUTDIR)/%.o: ../%.c + @mkdir -p $(@D) $(CC) $(LIB_CFLAGS) -c -o $@ $< clean: - rm -f $(OBJ) $(LIB_OBJECTS) $(TEST_CASES) libsimplemotionv2.a - rmdir $(LIB_OUTDIR) + $(RM) $(LIB_OBJECTS) $(TEST_CASES) libsimplemotionv2.a + $(RM) -r $(LIB_OUTDIR) From 3a77a8168496bbeeeeabf8bdf50d23539c231832 Mon Sep 17 00:00:00 2001 From: storvik Date: Thu, 26 Sep 2019 09:39:31 +0200 Subject: [PATCH 2/3] Add target all --- Makefile | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/Makefile b/Makefile index 377ef5f..769110e 100644 --- a/Makefile +++ b/Makefile @@ -7,10 +7,10 @@ SOURCES = $(wildcard *.c) \ # Set library type to static or dynamic, used to # set build directory etc. -ifeq ($(MAKECMDGOALS),libsimplemotionv2.a) - LIBRARY_TYPE = static -else +ifeq ($(MAKECMDGOALS),libsimplemotionv2.so) LIBRARY_TYPE = dynamic +else + LIBRARY_TYPE = static endif OBJECTS = $(SOURCES:%.c=$(BUILD_DIR)/$(LIBRARY_TYPE)/%.o) @@ -33,6 +33,8 @@ LDLIBS := -lm # Linker flags LDFLAGS := +all: libsimplemotionv2.a + libsimplemotionv2.a: $(BUILD_DIR)/libsimplemotionv2.a libsimplemotionv2.so: CFLAGS += -fPIC From 58078668e45f44f9feea7c4fdb550c24568a4f2b Mon Sep 17 00:00:00 2001 From: storvik Date: Sun, 17 Nov 2019 10:58:35 +0100 Subject: [PATCH 3/3] Add install target and build/install description to README --- Makefile | 25 ++++++++++++++++++++++++- README.md | 17 +++++++++++++++-- 2 files changed, 39 insertions(+), 3 deletions(-) diff --git a/Makefile b/Makefile index 769110e..8382ab2 100644 --- a/Makefile +++ b/Makefile @@ -1,10 +1,13 @@ BUILD_DIR := build +PREFIX ?= /usr/local SOURCES = $(wildcard *.c) \ drivers/serial/pcserialport.c \ drivers/tcpip/tcpclient.c \ utils/crc.c +HEADERS = $(wildcard *.h) + # Set library type to static or dynamic, used to # set build directory etc. ifeq ($(MAKECMDGOALS),libsimplemotionv2.so) @@ -33,7 +36,9 @@ LDLIBS := -lm # Linker flags LDFLAGS := -all: libsimplemotionv2.a +all: + $(MAKE) libsimplemotionv2.a + $(MAKE) libsimplemotionv2.so libsimplemotionv2.a: $(BUILD_DIR)/libsimplemotionv2.a @@ -64,3 +69,21 @@ test: clean: $(RM) -r $(BUILD_DIR) make -C tests clean + +# Install library. To override desti +.PHONY: install +install: + @mkdir -p $(DESTDIR)$(PREFIX)/lib + @mkdir -p $(DESTDIR)$(PREFIX)/include + cp $(BUILD_DIR)/libsimplemotionv2.so $(DESTDIR)$(PREFIX)/lib/libsimplemotionv2.so + cp $(BUILD_DIR)/libsimplemotionv2.a $(DESTDIR)$(PREFIX)/lib/libsimplemotionv2.a + cp *.h $(DESTDIR)$(PREFIX)/include/ + @echo "\nHeader files installed to $(PREFIX)/include" + @echo "Library installed to $(PREFIX)/lib" + @echo "You may need to run ldconfig" + +.PHONY: uninstall +uninstall: + cd $(DESTDIR)$(PREFIX)/include && rm -f $(HEADERS) && cd - + rm -f $(DESTDIR)$(PREFIX)/lib/libsimplemotionv2.so + rm -f $(DESTDIR)$(PREFIX)/lib/libsimplemotionv2.a diff --git a/README.md b/README.md index f75fe29..8e830ca 100644 --- a/README.md +++ b/README.md @@ -51,10 +51,23 @@ To include SM library in Qt project, simply place the library files under projec include(SimpleMotionV2/SimpleMotionV2.pri) ## Creating shared / dynamic library -It is possible to compile shared and dynamic libraries (i.e. .dll or .a file) from the SM library source package. One of the easiest ways of compiling library is to use Qt Creator, where a ready-to-compile project file is provided. Just open SimpleMotionV2lib.pro in Qt Creator and compile it with the compiler of your choice. The resulting library files may be used in other applications. +It is possible to compile shared and dynamic libraries (i.e. .dll or .a file) from the SM library source package. -Alternatively create a new library project in your favorite programming tool and compile the provided source codes into a libraray. You might need to study workings of the .pri file to succeed. +### QT Creator +One of the easiest cross-platform ways of compiling library is to use Qt Creator, where a ready-to-compile project file is provided. Just open SimpleMotionV2lib.pro in Qt Creator and compile it with the compiler of your choice. The resulting library files may be used in other applications. +### Linux makefile +Linux users can build both static and dynamic libraries using `make libsimplemotionv2.a` and `make libsimplemotionv2.so`. To install library to computer run: + +``` shell +make +sudo make install +``` +This will install library (both static and dynamic) to `/usr/local` unless variables `DESTDIR` and `PREFIX` are defined. I.e. library can be installed to custom directory running `make DESTDIR=/tmp/simplemotionv2 PREFIX= install`. + +Uninstall library by running `make uninstall`. + +### Windows note Windows users notice that a .dll library compiled with MinGW might not be compatible with MSVC. So if you're developing with MSVC, it's best to complile the dll using MSVC compiler. Qt Creator allows using Visual Studio's MSVC as compiler when configured properly. ## In Python, C# & others