Commits

Moritz Heidkamp committed 9570305

Add make example

Comments (0)

Files changed (4)

make-tutorial-example/01/project.scm

+;; objects = main.o kbd.o command.o display.o \
+;;           insert.o search.o files.o utils.o
+;; edit : $(objects)
+;;         cc -o edit $(objects)
+;; main.o : main.c defs.h
+;;         cc -c main.c
+;; kbd.o : kbd.c defs.h command.h
+;;         cc -c kbd.c
+;; command.o : command.c defs.h command.h
+;;         cc -c command.c
+;; display.o : display.c defs.h buffer.h
+;;         cc -c display.c
+;; insert.o : insert.c defs.h buffer.h
+;;         cc -c insert.c
+;; search.o : search.c defs.h buffer.h
+;;         cc -c search.c
+;; files.o : files.c defs.h buffer.h command.h
+;;         cc -c files.c
+;; utils.o : utils.c defs.h
+;;         cc -c utils.c
+;; clean :
+;;         rm edit $(objects)
+(use scsh-process)
+(import matchable)
+
+;; (list "main.o" "kbd.o" "command.o" "display.o"
+;;       "insert.o" "search.o" "files.o" "utils.o")
+
+(define (file-stat* path)
+  (if (file-exists? path)
+      (list file/stat: (file-stat path))
+      (list)))
+
+(define (file name #!optional (deps '()) realize)
+  (list name
+        deps
+        (lambda (name* deps*)
+          (let ((source-name (make-pathname (source-directory) name))
+                (output-name (make-pathname (output-directory) name)))
+            (append (file-stat* source-name)
+                    (list source-path: source-name
+                          output-path: output-name)
+                    (if realize
+                        (begin
+                          (create-directory (pathname-directory output-name) #t)
+                          (realize output-name deps*))
+                        '()))))))
+
+(define-syntax key
+  (syntax-rules ()
+    ((_ name default)
+     (lambda (#!key (name default)) name))
+    ((_ name)
+     (key name #f))))
+
+(define-syntax key-lambda
+  (syntax-rules ()
+    ((_ (key ...) body ...)
+     (lambda (args)
+       (apply (lambda (#!key key ...)
+                body ...)
+              args)))))
+
+(define-syntax key-lambda*
+  (syntax-rules ()
+    ((_ key) (key-lambda (key) key))))
+
+(define-syntax with-keys
+  (syntax-rules ()
+    ((_ (key ...) plist body ...)
+     ((key-lambda (key ...) body ...) plist))))
+
+(define-syntax run*
+  (syntax-rules ()
+    ((_ process-form)
+     (receive (exit-code success? pid) (run process-form)
+       (unless (and success? (zero? exit-code))
+         (error "execution failed" `process-form))))))
+
+(define compile-c-object
+  (match-lambda*
+   ((out (file . _))
+    (with-keys
+     (source-path)
+     file
+     (run* (cc -c ,source-path -o ,out))))))
+
+(define (receiving fn)
+  (lambda args (receive (apply fn args))))
+
+;; Instead of emitting inputs as `file' here it would be better to
+;; turn non-existing dependencies into input files by default as a
+;; realization preprocessing step.
+(define (c-object name deps)
+  (apply values
+         (append
+          (receive (file name deps compile-c-object))
+          (append-map (receiving file) deps))))
+
+(define c-object?
+  (key-lambda
+   (output-path)
+   (and (equal? "o" (pathname-extension output-path))
+        output-path)))
+
+(define (compile-c-program name deps)
+  (let ((object-files (filter-map c-object? deps)))
+    (run* (cc -o ,name ,@object-files))))
+
+(define (c-program name deps)
+  (file name deps compile-c-program))
+
+(define-target clean '()
+  (lambda _
+    (run (rm -rf ,(output-directory)))))
+
+(define-targets objects
+  (c-object "foo.o" '("foo.c" "defs.h"))
+  (c-object "bar.o" '("bar.c")))
+
+(define-targets
+  (c-program "foo" objects))

make-tutorial-example/01/src/bar.c

+int main() {
+  return foo();
+}

make-tutorial-example/01/src/defs.h

+#define THIS_IS_DEFS 1

make-tutorial-example/01/src/foo.c

+#include "defs.h"
+
+int foo() {
+  return THIS_IS_DEFS;
+}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.