[racket-dev] current-load/use-compiled

From: Jon Rafkind (rafkind at cs.utah.edu)
Date: Wed Jun 22 12:24:07 EDT 2011

I'm trying to hook into the part of racket that loads files so I can
automatically create .zo files when they don't exist (and no, I don't
want to type raco make). Here is the code that loads the file and sets
up the handler (more explanation follows the code).

== load.rkt
(let ([namespace (make-base-namespace)])
  (define loader (current-load/use-compiled))
  (parameterize ([current-namespace namespace]
                   (lambda (path something)
                     (define-values (parent self _) (split-path path))
                     (parameterize ([current-load-relative-directory
                       (printf "compile/load ~a ~a\n" path something)
                       (printf "load directory ~a\n"
                       ((compile-zos #f #:module? #t) (list path)
                       (define load-path
                         (if (is-zo? path)
                           (build-path parent "compiled"
(path-add-suffix self #".zo"))))
                       (printf "Loading file ~a\n" load-path)
                       (loader load-path something)))])
    (eval-syntax (with-syntax ([file "test.rkt"])
                   #'(require file)))))

And here are the files being loaded
== test.rkt
#lang racket/base

(require "test1.rkt")

(printf "hello from test!\n")

== test1.rkt
#lang racket/base

(require (for-syntax racket/base))

(define-syntax (foo stx)
  (printf "hello from foo macro\n")


  (printf "hello from phase1 code\n"))

(printf "test1!\n")

If I uncomment the 'begin-for-syntax' expression and run 'raco make' to
produce a .zo file then running 'racket test.rkt' won't display the
"hello from phase1 code" line. However if I run 'load.rkt' then no
matter if there is a .zo or not I see that printf being executed.

Posted on the dev mailing list.