From f7fb2836600c9d6e58bffa3fc3f4ceb2181f2b76 Mon Sep 17 00:00:00 2001 From: Luis <42945842+ldiaz94@users.noreply.github.com> Date: Mon, 26 Oct 2026 17:20:02 +0000 Subject: [PATCH] raz -lib DIR: the standard library is DIR, not the lib beside raz; test/own builds with it --- diff --git a/doc/compiler.md b/doc/compiler.md index 9bbb8d3..13c3cd2 100644 --- a/doc/compiler.md +++ b/doc/compiler.md @@ -2213,9 +2213,10 @@ library (`Sys`, `Files`), and `Files.argCount` and `Files.arg` give it its command line. -- `libraryDir`: the directory of the standard library. Linux's is the - `lib` beside raz, found by `/proc/self/exe` so that a link to raz - works, or, with no `/proc`, beside argument 0. +- `libraryDir`: the directory of the standard library when `-lib` + doesn't name one. Linux's is the `lib` beside raz, found by + `/proc/self/exe` so that a link to raz works, or, with no `/proc`, + beside argument 0. - `cLibrary`: `CLib = (none, glibc, musl)`, the C library a program that uses C is linked against when `-libc` doesn't say. `none` says the host can't make such a program, and `Raz` refuses one when it finds @@ -2294,6 +2295,13 @@ puts them between those and the standard library, for both finding modules and checking duplicate names. No subdirectories are searched. +`-lib dir` makes `dir` the library: `Raz.raz` reads it with the other +options, stops with "-lib dir: no such directory" if `Sys.access` can't +reach it, and gives it to `Parse.setLibDir` in place of +`Host.libraryDir()`. It changes nothing else: `searchDir` still puts +the library last, after `-I` and `RAZ_PATH`, and the duplicate check +(a symbolic link to one of the two files chooses it) is as before. + ## A program with its own Rt A hosted program uses `lib/Rt.raz`. A freestanding one, a kernel's @@ -2302,16 +2310,14 @@ code disagree, the code (`Gen.entryPoint`, `Raz.raz`) is right. **Giving a program its own `Rt`.** A program has one module of each -name, and the library is the `lib/` beside the compiler (`Raz.raz` finds -it through `/proc/self/exe`), so an `Rt.raz` beside the main file meets -`lib/Rt.raz` and is refused. What works today is staging: copy `raz` -beside a `lib/` holding the program's own modules, and build with that -copy. The library is then all of what is in that `lib/`, and nothing of -raz's unless the program copies or links it there (a symbolic link -chooses a module). `test/own` does it: `check.sh` copies `raz` to -`build/check/own/`, with `test/own/lib/Rt.raz` as its whole library, -and builds `test/own/Own.raz` and `OwnFail.raz` with it, under `-bare --s`. +name, and the library is `lib/` beside the compiler, so an `Rt.raz` +beside the main file meets `lib/Rt.raz` and is refused. `raz -lib DIR` +makes `DIR` the library instead: the program's own `Rt` and `Sys`, and +symbolic links to whichever of raz's modules it takes (a link chooses a +module, as always). The library is all of what is in `DIR`, and nothing +of raz's unless it is linked there. `test/own` does it: `check.sh` +builds `test/own/Own.raz` and `OwnFail.raz` with `-lib test/own/lib`, +whose `Rt.raz` is the whole library, under `-bare -s`. **What `Rt` must declare.** `Raz.raz` finds each name in `Rt`'s scope (`objIn`) and stops with "Rt has no X" where one is missing: diff --git a/doc/raz.1 b/doc/raz.1 index 2d78770..0781b29 100644 --- a/doc/raz.1 +++ b/doc/raz.1 @@ -42,7 +42,9 @@ .Pa lib/ beside the .Nm -executable, whose modules +executable (or the one +.Fl lib +names), whose modules .Xr raz-lib 3 says. A program has one module of each name: a module found in two of these @@ -98,6 +100,27 @@ a second line says the raz the library was written for and this one: .Ql (a library written for raz 0.18; this is raz 0.21) . The compiler only reads the file; it never refuses to build for it. +.It Fl lib Ar dir +The standard library is +.Ar dir , +not the +.Pa lib/ +beside the +.Nm +executable. +It is searched last, after the main module's directory, the +.Fl I +directories and the user library directories, as the default library is; +the duplicate-module rule and its symbolic-link exception apply to it +unchanged. +It is an error if +.Ar dir +cannot be reached. +A kernel's library holds its own +.Pa Rt.raz +and +.Pa Sys.raz , +with symbolic links to the modules of raz's own library that it takes. .It Fl Werror A warning counts as an error. .It Fl S diff --git a/src/Raz.raz b/src/Raz.raz index 58cf884..a90ea15 100644 --- a/src/Raz.raz +++ b/src/Raz.raz @@ -126,6 +126,8 @@ Out.str("options:\n"); Out.str(" -o file the program's file, Main without .raz if left out\n"); Out.str(" -I dir looks for imported modules in dir too, after Main.raz's own\n"); + Out.str(" -lib dir the standard library is dir, not the lib beside raz; it is\n"); + Out.str(" searched last, after -I and RAZ_PATH\n"); Out.str(" -Werror a warning counts as an error\n"); Out.str(" -S lists the main module's code, under each line of source\n"); Out.str(" -s strips symbols, line numbers, the places a failure is told\n"); @@ -230,9 +232,11 @@ i, first, entry, last, base: INT; m, rt, thread: ^Sym.Module; mainObj: ^Sym.Obj; run, noProfile, listing, dynamic, libcSaid, ok, bare: BOOL; err: ERROR; + libDir: []const U8; begin path := ""; outPath := ""; + libDir := ""; noProfile := false; bare := false; listing := false; @@ -245,6 +249,12 @@ INC(i); if Parse.numInc > 15 then Diag.fatal("more than 16 -I directories"); end; Parse.addIncDir(Files.arg(i)); + elsif s = "-lib" then + INC(i); + libDir := Files.arg(i); + if (LEN(libDir) = 0) or (Sys.access(libDir, {}) # 0) then + Diag.fatal(say1("-lib {}: no such directory", libDir)); + end; elsif s = "-o" then INC(i); outPath := Files.arg(i); @@ -300,7 +310,8 @@ end; if not libcSaid then Elf.setMusl(Host.cLibrary() = Host.musl); end; Parse.setVersion(Version.Text); - Parse.setLibDir(Host.libraryDir()); + if LEN(libDir) = 0 then libDir := Host.libraryDir(); end; + Parse.setLibDir(libDir); userLibraries(); if LEN(outPath) = 0 then outPath := path; diff --git a/test/check.sh b/test/check.sh index 40c6e50..89ac759 100755 --- a/test/check.sh +++ b/test/check.sh @@ -153,16 +153,14 @@ && cmp -s $out/Index.out $out/IndexBare.out; then pass=$((pass + 1)) else echo "FAIL raz -bare: status $status"; cat $out/Bare.out $out/Bare.err $out/IndexBare.out; fail=$((fail + 1)); fi -# a program with its own Rt (doc/compiler.md): the compiler copied -# beside a lib/ of nothing but test/own/lib/Rt.raz, whose exit and -# fail make their system calls themselves; Own ends with main's 3 after -# saying hello, OwnFail with 2 after Rt.fail's kind 0, an index +# a program with its own Rt (doc/compiler.md): raz -lib of nothing but +# test/own/lib/Rt.raz, whose exit and fail make their system calls +# themselves; Own ends with main's 3 after saying hello, OwnFail with 2 +# after Rt.fail's kind 0, an index rm -rf $out/own; mkdir -p $out/own -cp $raz $out/own/raz -cp -r test/own/lib $out/own/lib -$out/own/raz -Werror -bare -s -o $out/own/Own test/own/Own.raz +$raz -lib test/own/lib -Werror -bare -s -o $out/own/Own test/own/Own.raz status=$( { ( exec $out/own/Own ) > $out/own/Own.out 2>&1; echo $?; } 2>/dev/null ) -$out/own/raz -Werror -bare -s -o $out/own/OwnFail test/own/OwnFail.raz +$raz -lib test/own/lib -Werror -bare -s -o $out/own/OwnFail test/own/OwnFail.raz statusFail=$( { ( exec $out/own/OwnFail ) > $out/own/OwnFail.out 2>&1; echo $?; } 2>/dev/null ) if [ $status = 3 ] && [ $statusFail = 2 ] && [ "$(cat $out/own/Own.out)" = hello ] \ && [ "$(cat $out/own/OwnFail.out)" = 'check failed: 0' ]; then pass=$((pass + 1)) diff --git a/test/library-path.sh b/test/library-path.sh index ba7ea3e..f20d917 100755 --- a/test/library-path.sh +++ b/test/library-path.sh @@ -177,6 +177,35 @@ fails "module Out is in" } +# -lib DIR: a copy of the library, which the program uses instead of the +# one beside raz; PathLib comes from -I, so the library is searched last +t_lib_replaces_standard_library() { + cp -R "$(dirname "$raz")/lib" "$root/mylib" + library "$root/inc" + succeeds -I "$root/inc" -lib "$root/mylib" +} + +# -lib changes where the library is, not the order: a module in both the +# library and an -I directory is found twice +t_lib_is_searched_last() { + cp -R "$(dirname "$raz")/lib" "$root/mylib" + library "$root/inc" + echo "module Out; end Out." >"$root/inc/Out.raz" + fails "module Out is in" -I "$root/inc" -lib "$root/mylib" +} + +# RAZ_PATH still finds PathLib, the library coming after it +t_lib_leaves_user_path_alone() { + cp -R "$(dirname "$raz")/lib" "$root/mylib" + library "$root/user" + RAZ_PATH="$root/user"; export RAZ_PATH + succeeds -lib "$root/mylib" +} + +t_lib_missing_directory() { + fails "-lib $root/nowhere: no such directory" -lib "$root/nowhere" +} + t_user_paths_do_not_consume_include_slots() { library "$default" flags= @@ -266,6 +295,8 @@ t_no_path_expansion t_target_file_and_interface \ t_duplicate_main_and_include t_duplicate_user_directories \ t_duplicate_standard_library \ + t_lib_replaces_standard_library t_lib_is_searched_last \ + t_lib_leaves_user_path_alone t_lib_missing_directory \ t_user_paths_do_not_consume_include_slots \ t_trailing_slash_keeps_sibling_paths t_hard_link_chooses_same_module \ t_equal_contents_do_not_choose_distinct_module \