Simplify the build: one-file modules, a module graph read off the imports #67

Merged
alex-eg merged 20 commits from project-infrastructure-improvements into main 2026-10-26 23:24:24 +01:00
Owner
  • One file per module. NAME.scm carries its own (export ...) forms and is made a module by csc -module NAME; the .module.scm files are gone. A module exports what other modules import and nothing for the tests. The vendored sex-fmt-c.scm stays a (module ...) form.
  • The graph is read, not written. tools/module-deps.scm follows the import forms from each program's source (sexc, sextest, sex-tests, and the files tests/run.scm includes) and writes modules.mk: the module list, for every object and program the modules it imports, and an empty rule per source so a deleted one is not fatal. csc links the modules statically through the .link files it writes, so no -link flags are passed. Adding a module is creating its file and importing it.
  • modules.mk is rewritten only when its content changes, behind modules.stamp, so make starts over once and not forever on a future-dated source. A missing modules.mk is scanned again whatever the stamp says.
  • Everything builds in the root, each module once. The unit tests and sextest used to rebuild the modules in their own directories. tests/run.scm now uses include-relative, since its files share names with the modules. A program compiles to an object named after its source, because csc names its intermediate after -o, and sexc.o is the module's. That retires the sexc-tmp rename.
  • The unit tests go through each module's public entries. A behaviour none reaches is tested from Sex source through the whole pipeline (tests/pipeline.scm), on a sexc process, or not at all. infer's generics scaffolding, which only the tests called, is deleted, and so is every unused export and dead definition.
  • Every .sex directly in tests/sex-programs/ is a test. SEX_TEST_PROGRAMS was a hand-kept list, and a program left out of it never ran. list-macros.sex, which lists.sex imports and is not a test, moves to tests/sex-programs/lib/.
  • The end-to-end checks are Scheme. tests/modules/check.scm and tests/exit-code/check.scm replace the shell in their Makefiles, and tests/command-line/check.scm (--, -o -, --depfile, against a stand-in cc) replaces tests/args.scm, which called sexc's internals. They run under the test egg, so a failure says what it expected. The exit-code one keeps the -o - check from #66.
  • The egg repository has its own file. The .eggs/eggs.lock handling moves unchanged to eggs.mk.
  • Fixed on the way: the vendored fmt-c dropped what follows the first star of a pointer to a tagged type, so (const * struct X) lost its const and (* * struct X) a star, and (* enum X) crashed.

An example of modules.mk, with the empty rule for each source cut short:

MODULES = fmt-c-writer infer reader semen sex-fmt-c sex-macros sex-modules sexc types utils
SOURCES = fmt-c-writer.scm infer.scm reader.scm semen.scm sex-fmt-c.scm sex-macros.scm sex-modules.scm sexc.scm types.scm utils.scm main.scm tools/sextest/sextest.scm tests/run.scm tests/infer.scm tests/types.scm tests/module-path.scm tests/encapsulation.scm tests/codegen.scm tests/line-directives.scm tests/utils.scm tests/fmt-c-writer.scm tests/reader.scm tests/semen.scm tests/pipeline.scm
fmt-c-writer.o: sex-fmt-c.o types.o utils.o
infer.o: types.o utils.o
reader.o: utils.o
semen.o: infer.o sex-macros.o sex-modules.o types.o utils.o
sex-modules.o: reader.o types.o utils.o
sexc.o: fmt-c-writer.o reader.o semen.o sex-macros.o sex-modules.o utils.o
main.o: sexc.o
sexc: main.o sexc.o
tools/sextest/sextest.o: reader.o
sextest: tools/sextest/sextest.o reader.o
tests/run.o: tests/infer.scm tests/types.scm tests/module-path.scm tests/encapsulation.scm tests/codegen.scm tests/line-directives.scm tests/utils.scm tests/fmt-c-writer.scm tests/reader.scm tests/semen.scm tests/pipeline.scm fmt-c-writer.o infer.o reader.o semen.o sex-modules.o types.o utils.o
sex-tests: tests/run.o fmt-c-writer.o infer.o reader.o semen.o sex-modules.o types.o utils.o
fmt-c-writer.scm:
infer.scm:
...

Also regarding the build process:

  • make, make deps, make check (alias run-tests), and make install work as before; CI needs no change.
  • make check-modules, check-exit-code and check-command-line run one end-to-end check each, with the eggs on the path.
  • A name in dependencies.txt is an egg, and a name CHICKEN has a module for is CHICKEN's; neither is a module, whatever .scm lies in the root. A scratch test.scm or scheme.scm would otherwise be taken for one.
- One file per module. NAME.scm carries its own (export ...) forms and is made a module by csc -module NAME; the .module.scm files are gone. A module exports what other modules import and nothing for the tests. The vendored sex-fmt-c.scm stays a (module ...) form. - The graph is read, not written. tools/module-deps.scm follows the import forms from each program's source (sexc, sextest, sex-tests, and the files tests/run.scm includes) and writes modules.mk: the module list, for every object and program the modules it imports, and an empty rule per source so a deleted one is not fatal. csc links the modules statically through the .link files it writes, so no -link flags are passed. Adding a module is creating its file and importing it. - modules.mk is rewritten only when its content changes, behind modules.stamp, so make starts over once and not forever on a future-dated source. A missing modules.mk is scanned again whatever the stamp says. - Everything builds in the root, each module once. The unit tests and sextest used to rebuild the modules in their own directories. tests/run.scm now uses include-relative, since its files share names with the modules. A program compiles to an object named after its source, because csc names its intermediate after -o, and sexc.o is the module's. That retires the sexc-tmp rename. - The unit tests go through each module's public entries. A behaviour none reaches is tested from Sex source through the whole pipeline (tests/pipeline.scm), on a sexc process, or not at all. infer's generics scaffolding, which only the tests called, is deleted, and so is every unused export and dead definition. - Every .sex directly in tests/sex-programs/ is a test. SEX_TEST_PROGRAMS was a hand-kept list, and a program left out of it never ran. list-macros.sex, which lists.sex imports and is not a test, moves to tests/sex-programs/lib/. - The end-to-end checks are Scheme. tests/modules/check.scm and tests/exit-code/check.scm replace the shell in their Makefiles, and tests/command-line/check.scm (--, -o -, --depfile, against a stand-in cc) replaces tests/args.scm, which called sexc's internals. They run under the test egg, so a failure says what it expected. The exit-code one keeps the -o - check from #66. - The egg repository has its own file. The .eggs/eggs.lock handling moves unchanged to eggs.mk. - Fixed on the way: the vendored fmt-c dropped what follows the first star of a pointer to a tagged type, so (const * struct X) lost its const and (* * struct X) a star, and (* enum X) crashed. An example of modules.mk, with the empty rule for each source cut short: ``` MODULES = fmt-c-writer infer reader semen sex-fmt-c sex-macros sex-modules sexc types utils SOURCES = fmt-c-writer.scm infer.scm reader.scm semen.scm sex-fmt-c.scm sex-macros.scm sex-modules.scm sexc.scm types.scm utils.scm main.scm tools/sextest/sextest.scm tests/run.scm tests/infer.scm tests/types.scm tests/module-path.scm tests/encapsulation.scm tests/codegen.scm tests/line-directives.scm tests/utils.scm tests/fmt-c-writer.scm tests/reader.scm tests/semen.scm tests/pipeline.scm fmt-c-writer.o: sex-fmt-c.o types.o utils.o infer.o: types.o utils.o reader.o: utils.o semen.o: infer.o sex-macros.o sex-modules.o types.o utils.o sex-modules.o: reader.o types.o utils.o sexc.o: fmt-c-writer.o reader.o semen.o sex-macros.o sex-modules.o utils.o main.o: sexc.o sexc: main.o sexc.o tools/sextest/sextest.o: reader.o sextest: tools/sextest/sextest.o reader.o tests/run.o: tests/infer.scm tests/types.scm tests/module-path.scm tests/encapsulation.scm tests/codegen.scm tests/line-directives.scm tests/utils.scm tests/fmt-c-writer.scm tests/reader.scm tests/semen.scm tests/pipeline.scm fmt-c-writer.o infer.o reader.o semen.o sex-modules.o types.o utils.o sex-tests: tests/run.o fmt-c-writer.o infer.o reader.o semen.o sex-modules.o types.o utils.o fmt-c-writer.scm: infer.scm: ... ``` Also regarding the build process: - make, make deps, make check (alias run-tests), and make install work as before; CI needs no change. - make check-modules, check-exit-code and check-command-line run one end-to-end check each, with the eggs on the path. - A name in dependencies.txt is an egg, and a name CHICKEN has a module for is CHICKEN's; neither is a module, whatever .scm lies in the root. A scratch test.scm or scheme.scm would otherwise be taken for one.
alex-eg requested review from pkulev 2026-10-15 15:32:00 +02:00
pkulev changed target branch from bugfixes to main 2026-10-19 21:14:46 +02:00
pkulev added 8 commits 2026-10-19 21:14:46 +02:00
A module was a body plus a (module ...) wrapper naming its exports, and
the wrapper came in up to three copies: the root's, a test one that
exported everything, and sextest's, each recompiling the same body.

csc -module makes a file a module by itself, with export forms in its
body, so the wrappers are gone. tests/ and tools/sextest/ link what the
root built, finding it on the repository path; the tests see what a
module exports, and the names only they need are exported as such.
The Makefile was mostly about where the eggs live; what sexc is built
from is easier to find without it.
Each subdirectory's Makefile repeated the compiler's module list just to
link one program, and its result was copied back to the root anyway.
Each module's prerequisite line restated its import form, so adding a
module or an import meant editing the Makefile to match, and a missed
line only showed when an object came out of order.
Written as shell inside make, a check said only that it failed and left
the reason to rerunning it by hand; now each says what it expected.
A program left out of the hand-kept list was never run, and nothing said
so; the one module the tests import moves to lib/ instead.
Each program had its own recipe: a hand-written link list, a cd into
tests/ so its includes would not find the modules' namesakes, and a
renamed output so csc would not overwrite the module sexc.o. Read off
their imports like the modules', and compiled to an object named after
their source, they share one rule; include-relative ends the cd.
alex-eg force-pushed project-infrastructure-improvements from 6f7b28cfa5 to 67749c8db0 2026-10-19 21:17:59 +02:00 Compare
pkulev requested changes 2026-10-19 21:59:32 +02:00
Dismissed
pkulev left a comment
Collaborator

Built and ran this on CHICKEN 6.0.0 at 67749c8. A clean make -j produced sexc, and make check passed: the unit tests, all 25 programs in tests/sex-programs, check-modules, and check-exit-code.

With the graph in place, the rebuilds do what the commits say. Touching tests/basic.scm recompiled tests/run.scm and relinked sex-tests, and left the modules alone. A new brand.scm imported from that test was compiled with -module brand and linked into sex-tests. A second make was a no-op. A source dated tomorrow rebuilt once and stopped. A scratch test.scm stayed out of the graph, which is what listing test in dependencies.txt is for.

Three holes sit outside that.

  1. A scheme.scm in the root becomes the scheme unit, and the build accepts it. Every unit imports scheme, and scheme is not an egg in dependencies.txt, so the scan treats a file of that name as the module. I dropped one in that imports scheme and exports a single x. make sexc printed Circular scheme.o <- scheme.o dependency dropped, compiled it with -module scheme -unit scheme, rebuilt every module against it, linked, and exited 0. The rule it wrote was scheme.o: scheme.o, and every other object listed scheme.o too.

  2. Deleting modules.mk and editing a unit test leaves make at "up to date". The file is written from modules.stamp's recipe, and modules.mk's own rule is empty. After rm modules.mk the stamp was still newer than the root sources, so the recipe did not run. touch tests/basic.scm then make sex-tests printed make: 'sex-tests' is up to date. and modules.mk was still absent. Those test sources are prerequisites only through SOURCES, which arrives in the missing file, and sex-tests's prerequisites arrive the same way. make clean removes the stamp too, so that path comes back; deleting the generated makefile on its own does not.

  3. Two files that include-relative each other hang the scan. a.scm and b.scm, each including the other: csi -s tools/module-deps.scm prog=a.scm printed nothing and was still running when I killed it at five seconds. make would sit on the stamp recipe the same way. The walk has no record of a file it has already entered.

Built and ran this on CHICKEN 6.0.0 at `67749c8`. A clean `make -j` produced sexc, and `make check` passed: the unit tests, all 25 programs in `tests/sex-programs`, `check-modules`, and `check-exit-code`. With the graph in place, the rebuilds do what the commits say. Touching `tests/basic.scm` recompiled `tests/run.scm` and relinked `sex-tests`, and left the modules alone. A new `brand.scm` imported from that test was compiled with `-module brand` and linked into `sex-tests`. A second `make` was a no-op. A source dated tomorrow rebuilt once and stopped. A scratch `test.scm` stayed out of the graph, which is what listing `test` in `dependencies.txt` is for. Three holes sit outside that. 1. **A `scheme.scm` in the root becomes the `scheme` unit, and the build accepts it.** Every unit imports `scheme`, and `scheme` is not an egg in `dependencies.txt`, so the scan treats a file of that name as the module. I dropped one in that imports `scheme` and exports a single `x`. `make sexc` printed `Circular scheme.o <- scheme.o dependency dropped`, compiled it with `-module scheme -unit scheme`, rebuilt every module against it, linked, and exited 0. The rule it wrote was `scheme.o: scheme.o`, and every other object listed `scheme.o` too. 2. **Deleting `modules.mk` and editing a unit test leaves `make` at "up to date".** The file is written from `modules.stamp`'s recipe, and `modules.mk`'s own rule is empty. After `rm modules.mk` the stamp was still newer than the root sources, so the recipe did not run. `touch tests/basic.scm` then `make sex-tests` printed `make: 'sex-tests' is up to date.` and `modules.mk` was still absent. Those test sources are prerequisites only through `SOURCES`, which arrives in the missing file, and `sex-tests`'s prerequisites arrive the same way. `make clean` removes the stamp too, so that path comes back; deleting the generated makefile on its own does not. 3. **Two files that `include-relative` each other hang the scan.** `a.scm` and `b.scm`, each including the other: `csi -s tools/module-deps.scm prog=a.scm` printed nothing and was still running when I killed it at five seconds. `make` would sit on the stamp recipe the same way. The walk has no record of a file it has already entered.
alex-eg added 13 commits 2026-10-20 13:32:58 +02:00
Only the eggs were told apart from the modules, so a scheme.scm became
the scheme every unit imports, a dependency of every object and of
itself, and the build went on to link it.
Two files that include-relative each other sent the scan between them
for good, and make sat on the stamp recipe with it.
The stamp, newer than every source, stood for a modules.mk deleted since,
so make went on without the rules and called sex-tests up to date after a
test source changed.
The reader exported parse-all only so the tests could give a source the
file name its locations carry; read-from-file gives it one, through the
reader's own interface. The pipeline helpers move out of codegen.scm for
every suite to use.
The test read the search list off get-module-paths, exported for it
alone; importing a module from each entry checks what the list is for.
Its fn accessors, walk-form and macro-expand were exported to be called
on hand-built forms, checking what every compiled function and macro
already goes through; sex-fn-public? and sex-fn-body had no other caller.
The -- split, the -o - spelling and the dependency rule were exported to
be unit tested, as lists and strings sexc might never hand on; a stand-in
cc and the depfile show what it does hand on.
fmt-c wrote (const * struct what) as struct what*, and (* * struct what)
as struct what*, a pointer short; a pointer to an enum crashed it, the star
taken for the enum's values.
The walk-* tests pinned the forms handed to fmt-c, which were right
while the C they became lost a const; spelled as C, they check what fmt-c
makes of them too. The cases no source can reach, an atom no form holds
and a name the reader takes for a negation, go.
Schemes, instances, rigid variables and defaulting were built ahead of
generics and called by the tests alone, which reached into the IR for
them. The suite now goes through what infer exports, the eleven names
semen imports; what a wildcard solves is checked from source as well.
set-working-directory is reached only through with-directory's expansion,
so it is exported as that macro's helper; form-location, form-type,
register-macro and get-macro had no importer.
walk-generic-toplevel and non-empty-list? had no caller left, a type
variable's id was never read, and every expression's type was recorded
in a table nothing looked up.
drop what infer had for types nothing builds
All checks were successful
Sex CI / build-linux (pull_request) Successful in 5m12s
1c6d0aae62
No source makes an unknown type, a rigid variable or a variable with a
class to satisfy, so the branches unify, bind-tvar! and entails? kept for
them never ran, and the classes registered for them were never asked.
Author
Owner

Review 117, all three fixed:

  1. 49b463a: a root file named after a module CHICKEN has, like scheme.scm, is no longer taken for that module. tools/module-deps.scm asks module-environment, from /, so a local NAME.import.scm cannot answer for CHICKEN.
  2. 8488a58: modules.stamp depends on FORCE while modules.mk is missing, so rm modules.mk alone is scanned again.
  3. 0628d1c: the scan carries the files it has read, and an include cycle reads each one once.

Beyond the review, nothing is exported for the tests any more. The ;; for the tests groups are gone, and each module exports what another module imports. The tests go through the public entries: Sex source through the whole pipeline (tests/pipeline.scm), or a sexc process (tests/command-line/check.scm, make check-command-line, replacing tests/args.scm).

  • 4758802, 6198c51, ab881e4, 600b480, bf8ba2f: tests rewritten.
  • 13d20a1: rewriting the writer's tests as C found (const * struct X) losing its const, (* * struct X) losing a star, and (* enum X) crashing. Fixed in the vendored fmt-c.
  • 6737878, 16a1c3b, 9a3c01e, 1c6d0aa: infer's generics scaffolding, which only the tests called, is gone, along with the unused exports and dead definitions in every module.

The description is updated to match.

Review 117, all three fixed: 1. 49b463a: a root file named after a module CHICKEN has, like `scheme.scm`, is no longer taken for that module. `tools/module-deps.scm` asks `module-environment`, from `/`, so a local `NAME.import.scm` cannot answer for CHICKEN. 2. 8488a58: `modules.stamp` depends on `FORCE` while `modules.mk` is missing, so `rm modules.mk` alone is scanned again. 3. 0628d1c: the scan carries the files it has read, and an include cycle reads each one once. Beyond the review, nothing is exported for the tests any more. The `;; for the tests` groups are gone, and each module exports what another module imports. The tests go through the public entries: Sex source through the whole pipeline (`tests/pipeline.scm`), or a sexc process (`tests/command-line/check.scm`, `make check-command-line`, replacing `tests/args.scm`). - 4758802, 6198c51, ab881e4, 600b480, bf8ba2f: tests rewritten. - 13d20a1: rewriting the writer's tests as C found `(const * struct X)` losing its `const`, `(* * struct X)` losing a star, and `(* enum X)` crashing. Fixed in the vendored fmt-c. - 6737878, 16a1c3b, 9a3c01e, 1c6d0aa: infer's generics scaffolding, which only the tests called, is gone, along with the unused exports and dead definitions in every module. The description is updated to match.
pkulev approved these changes 2026-10-20 23:55:08 +02:00
pkulev left a comment
Collaborator

Built and ran this on CHICKEN 6.0.0 at 1c6d0aa. make check passed, including the command-line check and the pointer-to-struct and pointer-to-enum cases.

The three holes from last time are closed.

A scheme.scm in the root stayed out of the graph. make sexc did not compile it.

rm modules.mk, then touching tests/codegen.scm and make sex-tests, rewrote modules.mk, recompiled tests/run.scm, and relinked sex-tests. The next make sex-tests was up to date.

Two files in one directory that include-relative each other were read once. The scan printed both and exited.

A cycle spelled with ../ is still a different string on each step (sub/../sub/../...), and the scan follows it until the name is too long. The includes this tree has are names in the same directory, which the seen list catches.

Built and ran this on CHICKEN 6.0.0 at `1c6d0aa`. `make check` passed, including the command-line check and the pointer-to-struct and pointer-to-enum cases. The three holes from last time are closed. A `scheme.scm` in the root stayed out of the graph. `make sexc` did not compile it. `rm modules.mk`, then touching `tests/codegen.scm` and `make sex-tests`, rewrote `modules.mk`, recompiled `tests/run.scm`, and relinked `sex-tests`. The next `make sex-tests` was up to date. Two files in one directory that `include-relative` each other were read once. The scan printed both and exited. A cycle spelled with `../` is still a different string on each step (`sub/../sub/../...`), and the scan follows it until the name is too long. The includes this tree has are names in the same directory, which the seen list catches.
pkulev approved these changes 2026-10-26 17:51:39 +01:00
pkulev left a comment
Collaborator

LGTM

LGTM
@@ -65,0 +58,4 @@
modules.mk: modules.stamp ;
modules.stamp: $(SOURCES) $(filter-out %.import.scm,$(wildcard *.scm)) \
tools/module-deps.scm dependencies.txt Makefile \
Collaborator

That is another list of files, that is easy to forget about. I don't know how to fix it, whether to make a variable somewhere on top of the makefile, or make it computable somehow. The fact is - we will forget about it someday.

That is another list of files, that is easy to forget about. I don't know how to fix it, whether to make a variable somewhere on top of the makefile, or make it computable somehow. The fact is - we will forget about it someday.
Author
Owner

That we will, but probably CI will save us. Or manual testing.

That we will, but probably CI will save us. Or manual testing.
alex-eg marked this conversation as resolved
infer.scm Outdated
@@ -6,0 +3,4 @@
;;; semen solves the wildcards of a declared type with it -- `(var n _
;;; (strlen s))', `(var p (* _) (& x))' -- and asks it what an operand
;;; decays or promotes to. It is the ground floor of the pass described
;;; in Type-inference.org.
Collaborator

Where hmmm

Please reword that without mentioning things that don't matter.

Where hmmm Please reword that without mentioning things that don't matter.
Author
Owner

Yep sorry.

Yep sorry.
alex-eg marked this conversation as resolved
alex-eg force-pushed project-infrastructure-improvements from 1c6d0aae62 to 635cc48aa3 2026-10-26 23:23:54 +01:00 Compare
alex-eg merged commit 635cc48aa3 into main 2026-10-26 23:24:24 +01:00
alex-eg deleted branch project-infrastructure-improvements 2026-10-26 23:24:24 +01:00
Sign in to join this conversation.
No Reviewers
2 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: alex-eg/sex#67