How to package non-racket files into racket binary

The problem I actually facing is I write a compiler in Racket but the runtime is written in C, I write

(parameterize ([current-directory "./runtime"])
    (match-define (list stdout stdin status stderr do)
      (process "cc start.c runtime.c /tmp/generated.s"))
    (do 'wait)
    (read stdout)))

Now the executable generated by raco exe main.rkt must be at the path where runtime is located.

Actually, I think your problem may be even bigger than this, I'm guessing that the "runtime" subdirectory will have to be located relative to whatever the current directory is when the program is run.

In general, I would avoid the use of . as a directory specifier. Instead, I would steer you toward using solutions like define-runtime-path to locate resources relative to a particular source file.

A caveat here is that I don't often use raco exe, but I conjecture that define-runtime-path can also be made to work in this situation, and is the "right" way to do things.

1 Like

In addition to what @jbclements recommends RE: define-runtime-path, you could consider statically compiling the runtime and binary together and giving the result to the user; it should be portable modulo architecture and shared libs.

I believe that define-runtime-path is designed to (also) co-operate with raco exe.

1 Like

Does the following change is what you mean?

(define-runtime-path runtime "runtime")
(parameterize ([current-directory runtime])
  ...)

Or path should be (normalize-path "runtime")? Since the above code didn't work. Thanks!

UPDATE
Oops, normalize-path works the same as didn't use it.

Yes, this is a way too, but I'm looking for the packaging like game binary or something like that here I think. Thanks~

Could you clarify what you mean by 'didn't work'?

For the record, here's what I get when I run a similar code snippet:

In file /Users/dstorrs/test.rkt

#lang racket
(require racket/runtime-path)
(define-runtime-path here ".")
(define-runtime-path runtime "runtime")
(define bob-homedir (build-path here 'up "bob")
(println here)
(println runtime)
(println bob-homedir)
(parameterize ([current-directory bob-homedir])
  (println (current-directory)))

Output is:

#<path:/Users/dstorrs/.>
#<path:/Users/dstorrs/runtime>
#<path:/Users/dstorrs/./../bob>
#<path:/Users/bob/>
2 Likes

I think I have do something wrong(oops). define-runtime-path should solve the problem

1 Like

I recommend an article written by Bogdan Popa:

3 Likes