From 562af6ede8c0ed2e8a4757cda72a4c260aa111d3 Mon Sep 17 00:00:00 2001
From: Anders Blomdell <anders.blomdell@control.lth.se>
Date: Tue, 5 Mar 2019 16:39:21 +0100
Subject: [PATCH] Source rpm creation added

---
 .gitignore                  |  4 +-
 Makefile                    | 23 ++++++++++-
 adaptors/matlab/Makefile    |  6 ++-
 moberg.c                    | 43 +++++++++----------
 moberg.h                    |  3 +-
 moberg.spec.template        | 82 +++++++++++++++++++++++++++++++++++++
 test/Makefile               |  2 +-
 test/test_moberg4simulink.c |  7 ++++
 8 files changed, 138 insertions(+), 32 deletions(-)
 create mode 100644 moberg.spec.template
 create mode 100644 test/test_moberg4simulink.c

diff --git a/.gitignore b/.gitignore
index b9512a7..d443363 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,3 +1,5 @@
 *~
 *.o
-build/
\ No newline at end of file
+build/
+*.tar.gz
+moberg-*.spec
diff --git a/Makefile b/Makefile
index 32b5f3c..97c9151 100644
--- a/Makefile
+++ b/Makefile
@@ -1,4 +1,5 @@
 LIBRARIES=libmoberg.so
+MOBERG_VERSION=$(shell git describe --tags | sed -e 's/^v//;s/-/_/g' )
 CCFLAGS+=-Wall -Werror -I$(shell pwd) -g
 LDFLAGS+=-L$(shell pwd)/build/ -lmoberg
 PLUGINS:=$(wildcard plugins/*)
@@ -27,19 +28,37 @@ build/%.o:	%.c Makefile
 build/lib/%.o:	%.c Makefile | build/lib
 	$(CC) $(CCFLAGS) -c -fPIC -o $@ $<
 
-
 .PHONY: $(PLUGINS) $(ADAPTORS)
 $(ADAPTORS) $(PLUGINS): 
 	$(MAKE) -C $@
 
+.PHONY: TAR
+TAR:
+	git archive \
+		--prefix moberg-$(MOBERG_VERSION)/ \
+		--output moberg-$(MOBERG_VERSION).tar.gz -- HEAD
+
+.PHONY: moberg-$(MOBERG_VERSION).spec
+moberg-$(MOBERG_VERSION).spec: moberg.spec.template Makefile
+	sed -e 's/__MOBERG_VERSION__/$(MOBERG_VERSION)/' $< > $@
+
+.PHONY: SRPM
+SRPM:	moberg-$(MOBERG_VERSION).spec TAR
+	rpmbuild --define "_sourcedir $$(pwd)" \
+		 -bs $<
+
+
 
 .PHONY: test
 test: all
 	$(MAKE) -C test test
 
 clean:
-	rm -f build/*.so build/*.mex*
+	rm -f build/*.so
+	rm -f build/*.mex*
 	rm -f *~
+	rm -f moberg-*.spec
+	rm -f moberg-*.tar.gz
 	make -C test clean
 
 build/libmoberg.so: build/lib/moberg.o
diff --git a/adaptors/matlab/Makefile b/adaptors/matlab/Makefile
index c4e6c3d..393415e 100644
--- a/adaptors/matlab/Makefile
+++ b/adaptors/matlab/Makefile
@@ -7,8 +7,10 @@ MEX_SUFFIX=$(shell $(MEX) -v -n analogin.c \
 
 CCFLAGS+=-Wall -Werror -I. -I../.. -g
 
-all:	$(LIBRARIES:%=../../build/%) $(SFUNC:%=../../build/%.$(MEX_SUFFIX))
-	echo $(SUFFIX)
+all:	$(LIBRARIES:%=../../build/%)
+
+.PHONY: SFUNC
+SFUNC: $(SFUNC:%=../../build/%.$(MEX_SUFFIX))
 
 ../../build/libmoberg4simulink.so: moberg4simulink.c Makefile
 	$(CC) -o $@ $(CCFLAGS) -L../../build -shared -fPIC -lmoberg $<
diff --git a/moberg.c b/moberg.c
index a0b6e37..303bd5c 100644
--- a/moberg.c
+++ b/moberg.c
@@ -220,8 +220,7 @@ static int install_config(struct moberg *moberg)
   }
 }
 
-struct moberg *moberg_new(
-  struct moberg_config *config)
+struct moberg *moberg_new()
 {
   struct moberg *result = malloc(sizeof(*result));
   if (! result) {
@@ -239,32 +238,28 @@ struct moberg *moberg_new(
   result->encoder_in.capacity = 0;
   result->encoder_in.value = NULL;
   result->deferred_action = NULL;
-  if (config) {
-    result->config = config;
-  } else {
-    result->config = NULL;
+  result->config = NULL;
     
-    /* Parse default configuration(s) */
-    const char * const *config_paths = xdgSearchableConfigDirectories(NULL);
-    const char * const *path;
-    for (path = config_paths ; *path ; path++) {
-      int dirfd1 = open(*path, O_DIRECTORY);
-      if (dirfd >= 0) {
-        parse_config_at(result, dirfd1, "moberg.conf");
-        int dirfd2 = openat(dirfd1, "moberg.d", O_DIRECTORY);
-        if (dirfd2 >= 0) { 
-          parse_config_dir_at(result, dirfd2);
-          close(dirfd2);
-        }
-        close(dirfd1);
+  /* Parse default configuration(s) */
+  const char * const *config_paths = xdgSearchableConfigDirectories(NULL);
+  const char * const *path;
+  for (path = config_paths ; *path ; path++) {
+    int dirfd1 = open(*path, O_DIRECTORY);
+    if (dirfd >= 0) {
+      parse_config_at(result, dirfd1, "moberg.conf");
+      int dirfd2 = openat(dirfd1, "moberg.d", O_DIRECTORY);
+      if (dirfd2 >= 0) { 
+        parse_config_dir_at(result, dirfd2);
+        close(dirfd2);
       }
-      free((char*)*path);
+      close(dirfd1);
     }
-    free((const char **)config_paths);
-    
-    /* Read environment default */
-    /* Parse environment overrides */
+    free((char*)*path);
   }
+  free((const char **)config_paths);
+  
+  /* TODO: Read & parse environment overrides */
+
   install_config(result);
   run_deferred_actions(result);
   
diff --git a/moberg.h b/moberg.h
index ffe5410..63812d4 100644
--- a/moberg.h
+++ b/moberg.h
@@ -4,11 +4,10 @@
 #include <stdio.h>
 
 struct moberg;
-struct moberg_config;
 
 /* Creation & free */
 
-struct moberg *moberg_new(struct moberg_config *config);
+struct moberg *moberg_new();
 
 void moberg_free(struct moberg *moberg);
 
diff --git a/moberg.spec.template b/moberg.spec.template
new file mode 100644
index 0000000..005c435
--- /dev/null
+++ b/moberg.spec.template
@@ -0,0 +1,82 @@
+Name:           moberg
+Version:        __MOBERG_VERSION__
+Release:        1
+Summary:        Library for abstracting physical I/O
+License:        GPLv3
+Source0:        https://gitlab.control.lth.se/anders_blomdell/moberg/-/archive/master/moberg-__MOBERG_VERSION__.tar.gz
+
+BuildRequires:  gcc
+BuildRequires:  comedilib-devel
+BuildRequires:  valgrind
+
+%description
+Shared library for abstracting physical process I/O (analog, digital
+and encoders)
+
+%package comedi
+Summary: Comedi support for %{name}
+Requires: %{name} = %{version}-%{release}
+Requires:       comedilib
+
+%description comedi
+Comedi support for for %{name}
+
+%package devel
+Summary: Development files for %{name}
+Requires: %{name} = %{version}-%{release}
+
+%description devel
+Development files for %{name}
+
+%package matlab
+Summary: Matlab development files for %{name}
+Requires: %{name} = %{version}-%{release}
+Requires: %{name}-devel = %{version}-%{release}
+
+%description matlab
+Matlab development files for %{name}
+
+
+%prep
+%setup -q -n %{name}-__MOBERG_VERSION__
+
+
+%build
+make MOBERG_VERSION=__MOBERG_VERSION__
+
+%check
+make test
+
+%install
+rm -rf ${RPM_BUILD_ROOT}
+
+mkdir -p ${RPM_BUILD_ROOT}%{_libdir}
+cp build/libmoberg*.so ${RPM_BUILD_ROOT}%{_libdir}
+
+mkdir -p ${RPM_BUILD_ROOT}%{_includedir}
+cp moberg.h ${RPM_BUILD_ROOT}%{_includedir}
+
+mkdir -p ${RPM_BUILD_ROOT}/opt/matlab/src/moberg
+cp adaptors/matlab/*.h  ${RPM_BUILD_ROOT}/opt/matlab/src/moberg
+cp adaptors/matlab/*in.c  ${RPM_BUILD_ROOT}/opt/matlab/src/moberg
+cp adaptors/matlab/*out.c  ${RPM_BUILD_ROOT}/opt/matlab/src/moberg
+cp adaptors/matlab/Makefile  ${RPM_BUILD_ROOT}/opt/matlab/src/moberg
+
+%files
+%defattr(-,root,root,-)
+%attr(04755,root,root) %{_libdir}/libmoberg.so
+%attr(04755,root,root) %{_libdir}/libmoberg_serial2002.so
+
+%files comedi
+%defattr(-,root,root,-)
+%attr(04755,root,root) %{_libdir}/libmoberg_comedi.so
+
+%files devel
+%defattr(-,root,root,-)
+%attr(04755,root,root) %{_includedir}/moberg.h
+
+%files matlab
+%defattr(-,root,root,-)
+%attr(04755,root,root) %{_libdir}/libmoberg4simulink.so
+%attr(04755,root,root) /opt/matlab/src/moberg/*
+
diff --git a/test/Makefile b/test/Makefile
index 1c0ca42..c8ff506 100644
--- a/test/Makefile
+++ b/test/Makefile
@@ -5,7 +5,7 @@ ENV_TEST = LD_LIBRARY_PATH=../build HOME=.
 LDFLAGS_test_moberg4simulink = -lmoberg4simulink
 CCFLAGS_test_moberg4simulink = -I../adaptors/matlab -Wall -Werror -I$(shell pwd) -g
 
-all: 
+all:
 
 .PHONY: test
 test: $(TEST:%=run_%)
diff --git a/test/test_moberg4simulink.c b/test/test_moberg4simulink.c
new file mode 100644
index 0000000..40d34cf
--- /dev/null
+++ b/test/test_moberg4simulink.c
@@ -0,0 +1,7 @@
+#include <moberg4simulink.h>
+
+int main(int argc, char *argv[])
+{
+  struct moberg_analog_in *ain = moberg4simulink_analog_in_open(0);
+  moberg4simulink_analog_in_close(0, ain);
+}
-- 
GitLab