Initial import

This commit is contained in:
2026-08-08 14:32:18 +02:00
parent be2baa9280
commit f32503f3e7
11 changed files with 689 additions and 76 deletions
+10
View File
@@ -0,0 +1,10 @@
0.1.1
Initial racket-makefile language implementation.
Targets and dependencies with timestamp based rebuilding.
Phony and default targets.
Direct external command execution with run.
Recipe values $target, $deps and $<.
Cleanup helpers rm-f, rm-rf and cleanup.
Scribble documentation and example Makefile.
+37 -76
View File
@@ -1,93 +1,54 @@
# racket-makefile
`racket-makefile` is a small `#lang` for make-style builds. It keeps ordinary
Racket available and only adds targets, dependencies, timestamp based rebuilds,
phony targets, external command execution and a few cleanup helpers.
```racket
#lang racket-makefile
## Getting started
(define CC 'cc)
(define CFLAGS '(-Wall -O2))
To make it easy for you to get started with GitLab, here's a list of recommended next steps.
(default-target all)
(phony all clean)
Already a pro? Just edit this README.md and make it your own. Want to make it easy? [Use the template at the bottom](#editing-this-readme)!
(target all
(deps "hello"))
## Add your files
(target "hello"
(deps "hello.c")
(run `(,CC ,@CFLAGS -o $target $<)))
* [Create](https://docs.gitlab.com/user/project/repository/web_editor/#create-a-file) or [upload](https://docs.gitlab.com/user/project/repository/web_editor/#upload-a-file) files
* [Add files using the command line](https://docs.gitlab.com/topics/git/add_files/#add-files-to-a-git-repository) or push an existing Git repository with the following command:
```
cd existing_repo
git remote add origin https://framagit.org/racket/racket-makefile.git
git branch -M main
git push -uf origin main
(target clean
(rm-f "hello")
(rm-rf "compiled")
(cleanup "scrbl" '("**.html" "**.js" "**.css")))
```
## Integrate with your tools
Install a local checkout with:
* [Set up project integrations](https://framagit.org/racket/racket-makefile/-/settings/integrations)
```text
raco pkg install
```
## Collaborate with your team
For a versioned archive, give the package name explicitly:
* [Invite team members and collaborators](https://docs.gitlab.com/user/project/members/)
* [Create a new merge request](https://docs.gitlab.com/user/project/merge_requests/creating_merge_requests/)
* [Automatically close issues from merge requests](https://docs.gitlab.com/user/project/issues/managing_issues/#closing-issues-automatically)
* [Enable merge request approvals](https://docs.gitlab.com/user/project/merge_requests/approvals/)
* [Set auto-merge](https://docs.gitlab.com/user/project/merge_requests/auto_merge/)
```text
raco pkg install --name racket-makefile racket-makefile-0.1.1.zip
```
## Test and Deploy
Run the default target or select one or more targets explicitly:
Use the built-in continuous integration in GitLab.
```text
racket Makefile.rkt
racket Makefile.rkt clean
racket Makefile.rkt clean all
```
* [Get started with GitLab CI/CD](https://docs.gitlab.com/ci/quick_start/)
* [Analyze your code for known vulnerabilities with Static Application Security Testing (SAST)](https://docs.gitlab.com/user/application_security/sast/)
* [Deploy to Kubernetes, Amazon EC2, or Amazon ECS using Auto Deploy](https://docs.gitlab.com/topics/autodevops/requirements/)
* [Use pull-based deployments for improved Kubernetes management](https://docs.gitlab.com/user/clusters/agent/)
* [Set up protected environments](https://docs.gitlab.com/ci/environments/protected_environments/)
The language adds `target`, `deps`, `phony`, `default-target`, `run`, `rm-f`,
`rm-rf`, `cleanup`, `$target`, `$deps` and `$<`. Dependency expressions may
also produce lists, which are automatically flattened. Everything else is
ordinary Racket.
***
# Editing this README
When you're ready to make this README your own, just edit this file and use the handy template below (or feel free to structure it however you want - this is just a starting point!). Thanks to [makeareadme.com](https://www.makeareadme.com/) for this template.
## Suggestions for a good README
Every project is different, so consider which of these sections apply to yours. The sections used in the template are suggestions for most open source projects. Also keep in mind that while a README can be too long and detailed, too long is better than too short. If you think your README is too long, consider utilizing another form of documentation rather than cutting out information.
## Name
Choose a self-explaining name for your project.
## Description
Let people know what your project can do specifically. Provide context and add a link to any reference visitors might be unfamiliar with. A list of Features or a Background subsection can also be added here. If there are alternatives to your project, this is a good place to list differentiating factors.
## Badges
On some READMEs, you may see small images that convey metadata, such as whether or not all the tests are passing for the project. You can use Shields to add some to your README. Many services also have instructions for adding a badge.
## Visuals
Depending on what you are making, it can be a good idea to include screenshots or even a video (you'll frequently see GIFs rather than actual videos). Tools like ttygif can help, but check out Asciinema for a more sophisticated method.
## Installation
Within a particular ecosystem, there may be a common way of installing things, such as using Yarn, NuGet, or Homebrew. However, consider the possibility that whoever is reading your README is a novice and would like more guidance. Listing specific steps helps remove ambiguity and gets people to using your project as quickly as possible. If it only runs in a specific context like a particular programming language version or operating system or has dependencies that have to be installed manually, also add a Requirements subsection.
## Usage
Use examples liberally, and show the expected output if you can. It's helpful to have inline the smallest example of usage that you can demonstrate, while providing links to more sophisticated examples if they are too long to reasonably include in the README.
## Support
Tell people where they can go to for help. It can be any combination of an issue tracker, a chat room, an email address, etc.
## Roadmap
If you have ideas for releases in the future, it is a good idea to list them in the README.
## Contributing
State if you are open to contributions and what your requirements are for accepting them.
For people who want to make changes to your project, it's helpful to have some documentation on how to get started. Perhaps there is a script that they should run or some environment variables that they need to set. Make these steps explicit. These instructions could also be useful to your future self.
You can also document commands to lint the code or run tests. These steps help to ensure high code quality and reduce the likelihood that the changes inadvertently break something. Having instructions for running tests is especially helpful if it requires external setup, such as starting a Selenium server for testing in a browser.
## Authors and acknowledgment
Show your appreciation to those who have contributed to the project.
## License
For open source projects, say how it is licensed.
## Project status
If you have run out of energy or time for your project, put a note at the top of the README saying that development has slowed down or stopped completely. Someone may choose to fork your project or volunteer to step in as a maintainer or owner, allowing your project to keep going. You can also make an explicit request for maintainers.
See the installed `racket-makefile` Scribble documentation for the full API.
+19
View File
@@ -0,0 +1,19 @@
#lang racket-makefile
(define CC 'cc)
(define CFLAGS '(-Wall -O2))
(default-target all)
(phony all clean)
(target all
(deps "hello"))
(target "hello"
(deps "hello.c")
(run `(,CC ,@CFLAGS -o $target $<)))
(target clean
(rm-f "hello")
(rm-rf "compiled")
(cleanup "scrbl" '("**.html" "**.js" "**.css")))
+19
View File
@@ -0,0 +1,19 @@
#lang info
(define pkg-authors '(hnmdijkema))
(define version "0.1.1")
(define license 'MIT)
(define collection "racket-makefile")
(define pkg-desc
"A small Racket language for make-style dependency builds.")
(define scribblings
'(("scrbl/racket-makefile.scrbl" () (library 0))))
(define deps
'("base"))
(define build-deps
'("racket-doc"
"rackunit-lib"
"scribble-lib"))
+3
View File
@@ -0,0 +1,3 @@
#lang s-exp syntax/module-reader
racket-makefile
+89
View File
@@ -0,0 +1,89 @@
#lang racket/base
(require (except-in racket #%module-begin)
(only-in racket/base [#%module-begin racket-module-begin])
(for-syntax racket/base)
"private/commands.rkt"
"private/engine.rkt")
(provide (all-from-out racket)
(rename-out [makefile-module-begin #%module-begin])
target
deps
phony
default-target
run
rm-f
rm-rf
cleanup
$target
$deps
$<)
(define-for-syntax (literal-name-or-expression stx)
(if (and (identifier? stx)
(not (identifier-binding stx)))
(datum->syntax stx `(quote ,(syntax-e stx)) stx stx)
stx))
(define-syntax (makefile-module-begin stx)
(syntax-case stx ()
[(_ form ...)
#'(racket-module-begin
(reset-makefile!)
form ...
(module+ main
(run-selected-target!)))]))
(define-syntax (deps stx)
(raise-syntax-error 'deps "only valid as the dependency clause of target" stx))
(define-syntax (target stx)
(syntax-case stx (deps)
[(_ name (deps dependency ...) body ...)
(with-syntax ([target-name (literal-name-or-expression #'name)]
[(target-dependency ...)
(map literal-name-or-expression
(syntax->list #'(dependency ...)))])
#'(register-target!
target-name
(list target-dependency ...)
(lambda () body ... (void))))]
[(_ name body ...)
(with-syntax ([target-name (literal-name-or-expression #'name)])
#'(register-target!
target-name
'()
(lambda () body ... (void))))]))
(define-syntax (phony stx)
(syntax-case stx ()
[(_ name ...)
(with-syntax ([(target-name ...)
(map literal-name-or-expression
(syntax->list #'(name ...)))])
#'(mark-phony! target-name ...))]))
(define-syntax (default-target stx)
(syntax-case stx ()
[(_ name)
(with-syntax ([target-name (literal-name-or-expression #'name)])
#'(set-default-target! target-name))]))
(define-syntax $target
(syntax-id-rules ()
[$target
(or (current-target)
(error '$target "$target is only available while a target recipe is running"))]))
(define-syntax $deps
(syntax-id-rules ()
[$deps
(or (current-dependencies)
(error '$deps "$deps is only available while a target recipe is running"))]))
(define-syntax $<
(syntax-id-rules ()
[$<
(or (current-first-dependency)
(error '$< "$< is only available while a target recipe with dependencies is running"))]))
+82
View File
@@ -0,0 +1,82 @@
#lang racket/base
(require file/glob
racket/file
racket/list
racket/string
racket/system
"engine.rkt")
(provide run
rm-f
rm-rf
cleanup)
(define (recipe-value who parameter description)
(define value (parameter))
(unless value
(error who "~a is only available while a target recipe is running" description))
value)
(define (command-item->strings value)
(cond
[(eq? value '$target)
(list (recipe-value 'run current-target "$target"))]
[(eq? value '$deps)
(recipe-value 'run current-dependencies "$deps")]
[(eq? value '$<)
(define first (current-first-dependency))
(unless first
(error 'run "$< is not available: this target has no dependencies"))
(list first)]
[(symbol? value) (list (symbol->string value))]
[(path? value) (list (path->string value))]
[(string? value) (list value)]
[(number? value) (list (number->string value))]
[(list? value) (append-map command-item->strings value)]
[else
(raise-argument-error
'run
"command item (string, symbol, path, number, or list)"
value)]))
(define (run command)
(unless (list? command)
(raise-argument-error 'run "list?" command))
(define arguments (append-map command-item->strings command))
(when (null? arguments)
(error 'run "empty command"))
(define program (car arguments))
(define executable
(or (find-executable-path program)
(and (file-exists? program) (path->complete-path program))
(error 'run "executable not found: ~a" program)))
(printf "> ~a\n" (string-join arguments " "))
(unless (apply system* executable (cdr arguments))
(error 'run "command failed: ~a" program))
(void))
(define (rm-f . paths)
(for ([path (in-list paths)])
(case (file-or-directory-type path #f)
[(file link) (delete-file path)]
[(directory-link) (delete-directory path)]
[(directory)
(error 'rm-f "refusing to remove directory without recursion: ~a" path)]
[else (void)]))
(void))
(define (rm-rf . paths)
(for ([path (in-list paths)])
(delete-directory/files path #:must-exist? #f))
(void))
(define (cleanup directory patterns)
(unless (list? patterns)
(raise-argument-error 'cleanup "list?" patterns))
(for* ([pattern (in-list patterns)]
[path (in-glob (build-path directory pattern))])
(case (file-or-directory-type path #f)
[(file link) (rm-f path)]
[else (void)]))
(void))
+147
View File
@@ -0,0 +1,147 @@
#lang racket/base
(require racket/list)
(provide register-target!
mark-phony!
set-default-target!
reset-makefile!
run-selected-target!
current-target
current-dependencies
current-first-dependency)
(struct target-definition (name dependencies recipe) #:transparent)
(define targets (make-hash))
(define phony-targets (make-hash))
(define target-order '())
(define default-target-name #f)
(define current-target (make-parameter #f))
(define current-dependencies (make-parameter #f))
(define current-first-dependency (make-parameter #f))
(define (target-key value)
(cond
[(symbol? value) (symbol->string value)]
[(path? value) (path->string value)]
[(string? value) value]
[else (raise-argument-error 'target "(or/c symbol? path-string?)" value)]))
(define (reset-makefile!)
(hash-clear! targets)
(hash-clear! phony-targets)
(set! target-order '())
(set! default-target-name #f))
(define (target-keys value)
(if (list? value)
(append-map target-keys value)
(list (target-key value))))
(define (register-target! name dependencies recipe)
(define key (target-key name))
(unless (hash-has-key? targets key)
(set! target-order (append target-order (list key))))
(hash-set! targets
key
(target-definition key
(append-map target-keys dependencies)
recipe))
(void))
(define (mark-phony! . names)
(for* ([name (in-list names)]
[key (in-list (target-keys name))])
(hash-set! phony-targets key #t))
(void))
(define (set-default-target! name)
(set! default-target-name (target-key name))
(void))
(define (phony? name)
(hash-ref phony-targets name #f))
(define (path-modify-seconds path)
(file-or-directory-modify-seconds path #f (lambda () #f)))
(define (dependency-time name built-results)
(cond
[(phony? name) +inf.0]
[(hash-has-key? targets name)
(define result (hash-ref built-results name #f))
(or (path-modify-seconds name)
(and result +inf.0)
#f)]
[else
(or (path-modify-seconds name)
(error 'racket-makefile "dependency does not exist and has no target: ~a" name))]))
(define (needs-build? definition built-results)
(define name (target-definition-name definition))
(cond
[(phony? name) #t]
[else
(define target-time (path-modify-seconds name))
(cond
[(not target-time) #t]
[else
(for/or ([dependency (in-list (target-definition-dependencies definition))])
(define dep-time (dependency-time dependency built-results))
(and dep-time (> dep-time target-time)))])]))
(define (execute-target! definition)
(define name (target-definition-name definition))
(define dependencies (target-definition-dependencies definition))
(printf "racket-makefile: ~a\n" name)
(parameterize ([current-target name]
[current-dependencies dependencies]
[current-first-dependency (and (pair? dependencies) (car dependencies))])
((target-definition-recipe definition))))
(define (build-target! name built-results visiting)
(define key (target-key name))
(cond
[(hash-has-key? built-results key)
(hash-ref built-results key)]
[(hash-has-key? visiting key)
(error 'racket-makefile "dependency cycle involving target: ~a" key)]
[else
(define definition
(hash-ref targets key
(lambda ()
(error 'racket-makefile "unknown target: ~a" key))))
(hash-set! visiting key #t)
(for ([dependency (in-list (target-definition-dependencies definition))])
(if (hash-has-key? targets dependency)
(build-target! dependency built-results visiting)
(unless (path-modify-seconds dependency)
(error 'racket-makefile
"dependency does not exist and has no target: ~a"
dependency))))
(define rebuilt? (needs-build? definition built-results))
(when rebuilt?
(execute-target! definition))
(hash-remove! visiting key)
(hash-set! built-results key rebuilt?)
rebuilt?]))
(define (selected-targets)
(define args (vector->list (current-command-line-arguments)))
(cond
[(pair? args) args]
[default-target-name (list default-target-name)]
[(pair? target-order) (list (car target-order))]
[else '()]))
(define (run-selected-target!)
(define names (selected-targets))
(when (null? names)
(error 'racket-makefile "no target defined"))
(define built-results (make-hash))
(define visiting (make-hash))
(for ([name (in-list names)])
(build-target! name built-results visiting))
(void))
+203
View File
@@ -0,0 +1,203 @@
#lang scribble/manual
@(require (for-label racket/base
racket/file
(only-in racket-makefile
target deps phony default-target
run rm-f rm-rf cleanup
$target $deps $<)))
@title{racket-makefile}
@author{Hans Dijkema}
@defmodulelang[racket-makefile]
@tt{racket-makefile} is a small make-style language built on Racket. It adds
targets, dependencies, timestamp based rebuilding, phony targets, command
execution, and a few cleanup helpers. The rest of the language is ordinary
Racket.
@section{Installation}
Install the package from a local checkout with:
@verbatim{
raco pkg install
}
For a versioned archive, specify the package name explicitly:
@verbatim{
raco pkg install --name racket-makefile racket-makefile-0.1.1.zip
}
After installation a makefile can start with:
@verbatim{
#lang racket-makefile
}
Run a makefile with Racket:
@verbatim{
racket Makefile.rkt
racket Makefile.rkt clean
racket Makefile.rkt clean package
}
With no command-line target, @racket[default-target] is used. If no default is
specified, the first declared target is used.
@section{Targets and dependencies}
@defform[(target name (deps dependency ...) body ...)]{
Defines a target. @racket[name] can be a symbol, string, path, or a Racket
expression producing one of those values. Each @racket[dependency] can also
produce a list; dependency lists are recursively flattened.
An unbound identifier is treated as a literal symbol. Consequently,
@racket[(target clean ...)] defines the target @racket['clean], while a bound
identifier can be used to generate targets from ordinary Racket code. If a
literal target name happens to be bound by Racket, quote it explicitly, for
example @racket[(target 'compile ...)].
The body is not evaluated when the target is declared. It is saved as the
target recipe and evaluated only when the target must be rebuilt.
}
@defform[(deps dependency ...)]{
Specifies the dependencies of a target. Dependency expressions that produce
lists are automatically spliced into the dependency list. @racket[deps] is only
valid directly inside @racket[target].
}
@defform[(phony name ...)]{
Marks targets as phony. A phony target is always executed when requested or
when reached as a dependency.
}
@defform[(default-target name)]{
Selects the target used when no target is supplied on the command line.
}
A non-phony target is rebuilt when its output does not exist or when a
dependency is newer than the target. Registered target dependencies are built
first. A dependency that is neither a registered target nor an existing file
is an error. Dependency cycles are reported as errors.
@section{Recipe context}
Inside a target recipe the following identifiers are available:
@defidform[$target]{The current target name.}
@defidform[$deps]{A list containing all dependencies of the current target.}
@defidform[$<]{The first dependency of the current target. An error is raised
when the target has no dependencies.}
The same names can occur as symbols inside a quoted command passed to
@racket[run].
@section{Running commands}
@defproc[(run [command list?]) void?]{
Runs an external command directly, without an intermediate shell. The first
item is the executable and the remaining items are arguments. Symbols are
converted to strings. Nested lists are flattened, which makes Racket lists of
flags convenient to use.
The symbols @racket['$target], @racket['$deps], and @racket['$<] are expanded
from the current recipe. @racket['$deps] is spliced into the command.
A non-zero command result raises an error.
}
For example:
@racketblock[
(target "hello"
(deps "hello.c")
(run '(cc -Wall -O2 -o $target $<)))
]
Ordinary Racket values can be inserted with quasiquote:
@racketblock[
(define CC 'cc)
(define CFLAGS '(-Wall -O2))
(target "hello"
(deps "hello.c")
(run `(,CC ,@CFLAGS -o $target $<)))
]
@section{Cleanup helpers}
@defproc[(rm-f [path path-string?] ...) void?]{
Removes files when they exist. Missing files are ignored. Directories are not
removed; use @racket[rm-rf] for those.
}
@defproc[(rm-rf [path path-string?] ...) void?]{
Removes files or directory trees recursively. Missing paths are ignored.
}
@defproc[(cleanup [directory path-string?]
[patterns list?])
void?]{
Removes files below @racket[directory] that match the supplied Racket glob
patterns. Directories themselves are left in place. A pattern containing
@tt{**} searches recursively.
}
For example:
@racketblock[
(target clean
(rm-rf "compiled")
(cleanup "scrbl"
'("**.html"
"**.js"
"**.css")))
]
@section{Using ordinary Racket}
No separate make programming language is introduced. Definitions, functions,
loops, conditionals, modules, and Racket libraries remain available. For
example, targets can be generated in a loop:
@racketblock[
(define sources '("foo.c" "bar.c" "baz.c"))
(define objects
(for/list ([src sources])
(define obj (path-replace-extension src #".o"))
(target obj
(deps src)
(run `(cc -c $< -o $target)))
obj))
(target all
(deps objects))
]
@section{Package example}
The optional @hyperlink["https://docs.racket-lang.org/package-zipper/index.html"]{@tt{package-zipper}}
package combines naturally with @tt{racket-makefile}:
@racketblock[
(require package-zipper)
(default-target package)
(phony clean package)
(target clean
(rm-rf "compiled")
(cleanup "scrbl" '("**.html" "**.js" "**.css")))
(target package
(zip-package))
]
+49
View File
@@ -0,0 +1,49 @@
#lang racket
(require rackunit
racket/file
"../private/engine.rkt")
(define tmp (make-temporary-file "racket-makefile-engine~a" 'directory))
(dynamic-wind
void
(lambda ()
(parameterize ([current-directory tmp]
[current-command-line-arguments #()])
(call-with-output-file "input.txt"
(lambda (out) (display "one" out)))
(define build-count 0)
(reset-makefile!)
(set-default-target! 'all)
(mark-phony! 'all)
(register-target!
"result.txt"
'("input.txt")
(lambda ()
(set! build-count (add1 build-count))
(copy-file "input.txt" "result.txt" #t)))
;; A dependency expression may produce a list. The engine flattens it.
(register-target! 'all
(list (list "result.txt"))
void)
(run-selected-target!)
(check-equal? build-count 1)
(check-true (file-exists? "result.txt"))
;; Nothing changed, so the file target remains up to date.
(run-selected-target!)
(check-equal? build-count 1)
;; Make the input newer than the output and verify that it rebuilds.
(define result-time (file-or-directory-modify-seconds "result.txt"))
(file-or-directory-modify-seconds "input.txt" (+ result-time 2))
(run-selected-target!)
(check-equal? build-count 2)))
(lambda ()
(delete-directory/files tmp #:must-exist? #f)))
+31
View File
@@ -0,0 +1,31 @@
#lang racket
(require rackunit
racket/file
(only-in racket-makefile rm-f rm-rf cleanup))
(define tmp (make-temporary-file "racket-makefile-test~a" 'directory))
(dynamic-wind
void
(lambda ()
(define file (build-path tmp "x.tmp"))
(call-with-output-file file (lambda (out) (display "x" out)))
(check-true (file-exists? file))
(rm-f file)
(check-false (file-exists? file))
(define nested (build-path tmp "a" "b"))
(make-directory* nested)
(define html (build-path nested "x.html"))
(define txt (build-path nested "x.txt"))
(call-with-output-file html (lambda (out) (display "html" out)))
(call-with-output-file txt (lambda (out) (display "txt" out)))
(cleanup tmp '("**.html"))
(check-false (file-exists? html))
(check-true (file-exists? txt))
(rm-rf (build-path tmp "a"))
(check-false (directory-exists? (build-path tmp "a"))))
(lambda ()
(delete-directory/files tmp #:must-exist? #f)))