about summary refs log tree commit diff
path: root/hello-world-with-multiple-source-files
diff options
context:
space:
mode:
Diffstat (limited to 'hello-world-with-multiple-source-files')
-rw-r--r--hello-world-with-multiple-source-files/README.md8
-rw-r--r--hello-world-with-multiple-source-files/hello.c7
-rw-r--r--hello-world-with-multiple-source-files/make.scm42
-rw-r--r--hello-world-with-multiple-source-files/print.c6
-rw-r--r--hello-world-with-multiple-source-files/print.h1
5 files changed, 64 insertions, 0 deletions
diff --git a/hello-world-with-multiple-source-files/README.md b/hello-world-with-multiple-source-files/README.md
new file mode 100644
index 0000000..e279105
--- /dev/null
+++ b/hello-world-with-multiple-source-files/README.md
@@ -0,0 +1,8 @@
+Build hello.
+```
+guix build -f make.scm
+```
+Run it.
+```
+$(guix build -f make.scm)
+```
diff --git a/hello-world-with-multiple-source-files/hello.c b/hello-world-with-multiple-source-files/hello.c
new file mode 100644
index 0000000..17b896e
--- /dev/null
+++ b/hello-world-with-multiple-source-files/hello.c
@@ -0,0 +1,7 @@
+#include "print.h"
+
+int main ()
+{
+  printf("Hello world!\n");
+  return 0;
+}
diff --git a/hello-world-with-multiple-source-files/make.scm b/hello-world-with-multiple-source-files/make.scm
new file mode 100644
index 0000000..a078c07
--- /dev/null
+++ b/hello-world-with-multiple-source-files/make.scm
@@ -0,0 +1,42 @@
+(use-modules (gnu packages commencement)
+             (guix gexp))
+
+(define set-up-gcc-gexp
+  (with-imported-modules '((guix build utils))
+    #~(begin
+        (use-modules (guix build utils))
+
+        (set-path-environment-variable "PATH"
+                                       '("bin")
+                                       (list #$gcc-toolchain))
+        (set-path-environment-variable "C_INCLUDE_PATH"
+                                       '("include")
+                                       (list #$gcc-toolchain))
+        (set-path-environment-variable "LIBRARY_PATH"
+                                       '("lib")
+                                       (list #$gcc-toolchain)))))
+
+(define* (compile source-filename #:key (flags '()))
+  (computed-file (string-append (basename source-filename ".c")
+                                ".o")
+                 #~(begin
+                     #$set-up-gcc-gexp
+                     (invoke "gcc" "-c" #$(local-file source-filename)
+                             "-o" #$output
+                             #$@flags))))
+
+(define* (link output-filename object-files #:key (flags '()))
+  (computed-file output-filename
+                 #~(begin
+                     #$set-up-gcc-gexp
+                     (invoke "gcc" "-o" #$output
+                             #$@object-files
+                             #$@flags))))
+
+(let ((include (list "-I" (file-union "include"
+                                      `(("print.h" ,(local-file "print.h")))))))
+  (link "hello"
+        (list (compile "hello.c"
+                       #:flags include)
+              (compile "print.c"
+                       #:flags include))))
diff --git a/hello-world-with-multiple-source-files/print.c b/hello-world-with-multiple-source-files/print.c
new file mode 100644
index 0000000..1630d29
--- /dev/null
+++ b/hello-world-with-multiple-source-files/print.c
@@ -0,0 +1,6 @@
+#include <stdio.h>
+
+void print()
+{
+  printf("Hello world!\n");
+}
diff --git a/hello-world-with-multiple-source-files/print.h b/hello-world-with-multiple-source-files/print.h
new file mode 100644
index 0000000..ffc11e5
--- /dev/null
+++ b/hello-world-with-multiple-source-files/print.h
@@ -0,0 +1 @@
+void print();