#lang zuo
(require (only-in "../s/build.zuo"
                  [targets-at s-targets-at]))

(provide boot-file-names
         cross-build-boot
         clean-links)

(define boot-file-names
  '(;; the boot files proper
    "petite.boot" "scheme.boot"
    ;; machine-specific header files:
    "scheme.h" "equates.h"
    ;; GC traversal functions
    "gc-ocd.inc" "gc-oce.inc" "gc-par.inc" "heapcheck.inc"))

;; Cross-builds for a given machine type
(define (cross-build-boot token ccopts make-targets xpatch?
                          xm host-scheme host-workarea
                          at-dir config vars m)
  (define dir (at-dir ".." (~a "xc-" xm)))
  (define s-dir (build-path dir "s"))
  (mkdir-p s-dir)
  (clean-links s-dir) ; in case leftover from old build system
  (display-to-file (~a "m=" xm "\n"
                       (~a "hostm=" m "\n")
                       (if host-scheme
                           (~a "hostscheme=" host-scheme "\n")
                           "")
                       "hostworkarea=" (if host-scheme
                                           ""
                                           (find-relative-path dir (or host-workarea
                                                                       (at-dir .)))) "\n"
                       (if (or host-scheme host-workarea)
                           "cross=t\n"
                           "")
                       "\n")
                   (build-path dir "Mf-config")
                   :truncate)
  (let ([targets (s-targets-at (make-at-dir s-dir)
                               (hash-remove vars 'm)
                               ccopts)])
    (build (find-target "clean" targets) token)
    (map (lambda (t)
           (build (find-target t targets) token))
         make-targets)
    (when xpatch?
      (build (find-target (build-path s-dir "xpatch") targets) token)))
  (define boot-xm-dir (at-dir "../boot" xm))
  (mkdir-p boot-xm-dir)
  (for-each (lambda (file)
              (cp (build-path dir "boot" xm file)
                  (build-path boot-xm-dir file)))
            boot-file-names))

(define (clean-links dir)
  (for-each (lambda (p)
              (cond
                [(link-exists? p) (rm p)]
                [(directory-exists? p)
                 (unless (equal? "boot" (file-name-from-path p))
                   (clean-links p))]))
            (ls* dir)))
