summary refs log tree commit diff stats
path: root/scramble.scm
blob: e79fbe0c141e551f1f8baf8404a3251e3c0cfe6e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
(declare (module scramble))

(import scheme (chicken base)
        (chicken file)
        (chicken format)
        (chicken pathname)
        (chicken process)
        (chicken process-context)
        (chicken string)
        (srfi 1))

;; dependencies should be like
;; (((output jimmy.util) (source jimmy.util))
;;  ((output jimmy.read) (source jimmy.read) (source jimmy.util))
;;  ((output jimmy.emit) (source jimmy.emit) (source jimmy.util)))

(define egg (make-parameter #f))
(define build-dir (make-parameter "build"))

(define csc-options '("-setup-mode"
                      "-host"
                      "-D compiling-extension"
                      "-emit-all-import-libraries"
                      "-dynamic"
                      "-regenerate-import-libraries"
                      "-I $(PWD)"
                      "-C -I$(PWD)"))

(define source-extensions
  ;; The possible extensions source files can take.
  (make-parameter '("scm" "sld" "ss")))

(define (read-egg #!optional egg-file)
  ;; Read EGG-FILE and return the structure inside.
  (unless egg-file
    (set! egg-file
      (receive (_ _ dirs) (decompose-directory (current-directory))
        (make-pathname (current-directory) (car (reverse dirs)) "egg"))))
  (assert (file-exists? egg-file) "Can't find egg file" egg-file)
  (assert (file-readable? egg-file) "Can't read egg file" egg-file)
  (cons* `(egg-directory ,(pathname-directory egg-file))
         `(egg-name ,(string->symbol (pathname-file egg-file)))
         (with-input-from-file egg-file read)))

(define (egg-directory) (alist-ref 'egg-directory (egg)))
(define (egg-name) (car (alist-ref 'egg-name (egg))))
(define (egg-components) (alist-ref 'components (egg)))
(define (egg-csc-options)
  (let ((opts (alist-walk (egg) 'component-options 'csc-options)))
    (if opts (map ->string opts) '())))

(define (source-of c)
  ;; Find the source file of C and return it.  If not found, error.
  (cond
   ((alist-ref 'source (cddr (find-component c)))
    => (lambda (s)
         (receive (dirs file ext) (decompose-pathname (->string (car s)))
           (make-pathname (append (egg-directory)
                                  (if (list? dirs) dirs (list dirs)))
                          file ext))))
   (else
    (let loop ((ext (source-extensions)))
      (if (null? ext) #f
          (let ((cand (make-pathname (egg-directory) c ext)))
            (if (file-exists? cand) cand
                (loop (cdr ext)))))))))

(define (output-of c)
  ;; Return the output file of C.  It's okay if it doesn't exist.
  (make-pathname (append (egg-directory) (list (build-dir)))
                 (->string c)
                 (case (car (find-component c))
                   ((extension) "so")
                   ((program)))))

(define (find-component name)
  (find (lambda (c) (eq? name (cadr c)))
        (alist-ref 'components (egg))))

(define (dependency-graph)
  ;; Build a dependency graph from the egg.
  (define (component->deps c)
    (let* ((c-name (cadr c))
           (c-info (cddr c))
           (c-deps (or (alist-ref 'component-dependencies c-info) '()))
           (s-deps (or (alist-ref 'source-dependencies c-info) '())))
      (if (null? (append c-deps s-deps))
          `(,c-name ,c-name)
          `(,c-name . ,(cons c-name
                             (append c-deps
                                     (map ->string s-deps)))))))
  (filter-map component->deps (egg-components)))

(define (alist-walk lis . keys)
  (if (null? keys)
      lis
      (let ((kv (assoc (car keys) lis)))
        (cond
         ((not kv) #f)
         ((atom? (cdr kv))
          (and (null? (cdr keys))       ; this shouldn't error...
               (cdr kv)))
         ((list? (cdr kv))
          (apply alist-walk (cdr kv) (cdr keys)))))))

(define (string-join ss)
  (string-intersperse ss " "))

(define (find-executable name)
  (let loop ((path (string-split (get-environment-variable "PATH") ":")))
    (cond
     ;; If no executable is found, return just the name.  Another option would
     ;; be to error out if there isn't an executable here ...
     ((null? path) name)
     ((and (directory-exists? (car path))
           (member name (directory (car path) 'with-dotfiles)))
      (make-pathname (car path) name))
     (else (loop (cdr path))))))

(define (rule c)
  (let ((deps (alist-ref c (dependency-graph))))
    (sprintf "~a: ~a $(BUILD)\n\t~a\n\t-mv ~a.import.scm $(BUILD)/"
             (output-of c)
             (string-join (cons (source-of (car deps))
                                (map output-of (cdr deps))))
             "$(CSC) $(CSC_OPTIONS) $(CSC_OPTIONS_EXTRA) $< -o $@"
             c)))

(define (emit-makefile egg-file)
  (parameterize ((egg (read-egg egg-file)))
    (print "# Automatically generated by scramble") (newline)
    (print "NAME = " (egg-name))
    (print "CSC = " (find-executable "csc"))
    (print "CSC_OPTIONS = " (string-join csc-options))
    (print "CSC_OPTIONS_EXTRA = " (string-join (egg-csc-options)))
    (print "CSI = " (find-executable "csi"))
    (print "BUILD = $(PWD)/" (build-dir))
    (print "TESTS = $(PWD)/tests")
    (print "TEST_ENV = env BUILD=$(BUILD) TESTS=$(TESTS)")
    (newline)
    (print ".PHONY: all test clean install uninstall")
    (print "all: " (string-join (map (o source-of car) (dependency-graph))))
    (print "test: build"
           "\n\t" "cd $(BUILD) && "
           "$(TEST_ENV) $(CSI) -setup-mode -s $(TESTS)/run.scm $(NAME)")
    (print "clean:\n\t-rm -rf $(BUILD)")
    (print "install:\n\tchicken-install -s")
    (print "uninstall:\n\tchicken-uninstall -s")
    (newline) (print "# " (egg-name)) (newline)
    (print "$(BUILD):\n\t-mkdir $(BUILD)")
    (for-each (o print rule cadr) (egg-components))
    ))