#!/usr/bin/gosh ;(use posix files utils) (use file.util) (use slib) (require 'pretty-print) (use gauche.parameter) (use gauche.process) ; # Gneerate Makefile ; ## Define utils (define task-list '()) (define (task-impl name dependencies commands) (when (assoc name task-list) (display (string-append "WARN: duplicate defenision of " name "\n"))) (set! task-list (cons (list name dependencies commands) task-list))) (define this-file (make-parameter #f)) (define this-target (make-parameter #f)) (define-syntax task (syntax-rules () ((_ name dependencies . commands) (let ([task-name name]) (this-target task-name) (task-impl task-name (list . dependencies) (list . commands)))))) ; ## Find contents & eval (define (string-strip-prefix prefix string) (if (string-prefix? prefix string) (string-drop string (string-length prefix)) string)) (define *marker* #/\([l]azy-make-configulation.*?$/) (define (extract filename) (with-input-from-file filename (lambda () (let loop ([parts '()] [cur #f] [prefix ""]) (let ([line (read-line)]) (if (eof-object? line) (reverse (if cur (cons cur parts))) (let ([match (*marker* line)]) (if match (loop (if cur (cons cur parts) parts) (match) (match 'before)) (loop parts (if cur (string-append cur (string-strip-prefix prefix line)) #f) prefix))))))))) (for-each (lambda (fn) (this-file fn) (guard (e [( e) #f] [( e) #f]) (for-each (lambda (tgt) (for-each (lambda (e) (eval e (current-module))) (cdr (with-input-from-string tgt read)))) (extract fn)))) (glob "*")) ; ## Write result (define makefile (build-path (temporary-directory) (string-append "gtemp." (number->string (current-time)) "." (number->string (sys-getpid))))) (with-output-to-file makefile (lambda () (for-each (lambda (task) (display (first task)) (display ": ") (display (string-join (second task))) (display "\n\t") (display (string-join (third task) "\n\t")) (display "\n") (display "\n")) task-list))) ; # Invoke make (define (show-task-list) (for-each (lambda (task) (display (first task)) (display "\n")) task-list)) (if (> (length (command-line)) 1) (if (equal? (cdr (command-line)) '("lmake-task-list")) (show-task-list) (do-process (append (list 'make '-f makefile) (cdr (command-line))))) (if (assoc "default" task-list) (do-process (list 'make '-f makefile 'default)) (show-task-list)))