← cproj

first dev commit - add build system, add debug and release builds, use static linked libraries for now

6ac7f68fac47342de39262166f5d35113fc159bd · Ovidiu · Aug 30, 2020 19:16

+86 −4 · 10 files changed

  • Tupfile +1 −0
  • Tuprules.tup +22 −0
  • bin/Tupfile +19 −0
  • include/square.h +2 −0
  • lib/Tupfile +10 −0
  • obj/Tupfile +8 −0
  • src/main.c +4 −4
  • src/square.c +8 −0
  • test/Tupfile +12 −0
  • test/testfile +0 −0
diff --git a/Tupfile b/Tupfile
new file mode 100644
index 0000000..f0fe651
--- /dev/null
+++ b/Tupfile
@@ -0,0 +1 @@
+include_rules
diff --git a/Tuprules.tup b/Tuprules.tup
new file mode 100644
index 0000000..8191385
--- /dev/null
+++ b/Tuprules.tup
@@ -0,0 +1,22 @@
+&libdir  = include/
+&objdir  = obj/
+&bindir  = bin/
+CDEBUG += -Weverything
+CDEBUG += -g
+CDEBUG += -Og
+CDEBUG += -ggdb
+
+CRELEASE += -O2
+CRELEASE += -D_FORTIFY_SOURCE=2
+CRELEASE += -fexceptions
+CRELEASE += -fvisibility=hidden
+CRELEASE += -Werror
+
+# Do the object files
+!ccodbg = |> clang $(CDEBUG) -I&(libdir) -c %f -o &(objdir)/%o |> %B_debug.o
+!ccorel = |> clang $(CRELEASE) -I&(libdir) -c %f -o &(objdir)/%o |> %B_release.o
+# macro for the archive command
+!ar = |> ar crs %o %f |>
+!ccbdbg = |> clang $(CDEBUG) %f -o &(bindir)/%o |> %B
+!ccbrel = |> clang $(CRELEASE) %f -o &(bindir)/%o |> %B
+
diff --git a/bin/Tupfile b/bin/Tupfile
new file mode 100644
index 0000000..c8224b3
--- /dev/null
+++ b/bin/Tupfile
@@ -0,0 +1,19 @@
+include_rules
+#&Libs =
+#CXFLAGS += -lc
+#CXFLAGS += -lm
+#CXFLAGS += ../lib/*.a
+
+#: *.c |> !cc |> ../obj/*.o
+#: *.c |> !cc |> ../obj/*.o | clang  %f  $(CXFLAGS) -o %f |> %B
+#:  ../obj/*.o | ../lib/*.a  |> clang  %f  $(CXFLAGS) -o testfile |> %B
+#: foreach ../lib/*.a  |> clang -v %f  $(CXFLAGS) -o %f |> %B
+#: |> echo first > %o && echo second > %o.2 |> out/file.txt | %O.txt.2
+# produces test binary
+#: |> clang ../src/main.c -v -o %o  |> test
+# works with static library
+#: ../lib/mylib.a |> clang %f -v -o %o  |> %B
+# make {bin} placeholder
+#: foreach ../lib/*.a |> clang %f -v -o %o |> program
+: foreach ../lib/*_debug.a |> !ccbdbg |> program_debug
+: foreach ../lib/*_release.a |> !ccbrel  |> program_release
diff --git a/include/square.h b/include/square.h
new file mode 100644
index 0000000..ad344d9
--- /dev/null
+++ b/include/square.h
@@ -0,0 +1,2 @@
+#include <stdio.h>
+extern int squares(int x,int y);
diff --git a/lib/Tupfile b/lib/Tupfile
new file mode 100644
index 0000000..9425856
--- /dev/null
+++ b/lib/Tupfile
@@ -0,0 +1,10 @@
+# 3. Create an archive file - static library
+# TODO: Static libary - for own code, maybe not
+# required for others
+include_rules
+#: ../obj/*.o |> !ar |> mylib.a
+#: foreach ../obj/*.o |> !ar |> %B.a {obj}
+# not foreach .. for all object files into a single archive - for now not .so file
+#: foreach ../obj/*.o |> !ar |> 
+: ../obj/*_debug.o |> !ar |> mylib_debug.a
+: ../obj/*_release.o |> !ar |> mylib_release.a
diff --git a/obj/Tupfile b/obj/Tupfile
new file mode 100644
index 0000000..79223b5
--- /dev/null
+++ b/obj/Tupfile
@@ -0,0 +1,8 @@
+# 1. Build the source code in C
+# will grab the libraries included
+# in the include definitions
+# and call the !cc macro to build
+
+include_rules
+: foreach ../src/*.c |> !ccodbg |>
+: foreach ../src/*.c |> !ccorel |>
diff --git a/src/main.c b/src/main.c
index ba6aefd..7dcf4eb 100644
--- a/src/main.c
+++ b/src/main.c
@@ -1,10 +1,8 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <errno.h>
-
-#include "sds/sds.h"
-
-
+#include "../lib/sds/sds.h"
+#include "../include/square.h"
 
 
 int main (int argc, char** argv) {
@@ -14,5 +12,7 @@ int main (int argc, char** argv) {
     fprintf(stdout, "The number of arguments is: %d\n",argc);
     fprintf(stdout, "%s\n","correct" ); 
 
+    int rc = square(4,5);
+    fprintf(stdout, "%d\n",rc);
     return EXIT_SUCCESS;
 }
diff --git a/src/square.c b/src/square.c
new file mode 100644
index 0000000..09d77fa
--- /dev/null
+++ b/src/square.c
@@ -0,0 +1,8 @@
+#include "../include/square.h"
+
+extern int square(int x, int y) {
+
+  fprintf(stdout, "Square of %d and %d is: %d\n",x,y,x*y );
+
+  return 0;
+}
diff --git a/test/Tupfile b/test/Tupfile
new file mode 100644
index 0000000..19e6d6d
--- /dev/null
+++ b/test/Tupfile
@@ -0,0 +1,12 @@
+include_rules
+#&Libs =
+CXFLAGS += -lc
+CXFLAGS += -lm
+CXFLAGS += ../lib/*.a
+
+#: *.c |> !cc |> ../obj/*.o
+#: *.c |> !cc |> ../obj/*.o | clang  %f  $(CXFLAGS) -o %f |> %B
+#:  ../obj/*.o | ../lib/*.a  |> clang  %f  $(CXFLAGS) -o testfile |> %B
+#: foreach ../lib/*.a  |> clang -v %f  $(CXFLAGS) -o %f |> %B
+: |> echo first > %o && echo second > %o.2 |> out/file.txt | %O.txt.2
+
diff --git a/test/testfile b/test/testfile
new file mode 100644
index 0000000..e69de29