Compare commits

...

17 Commits

Author SHA1 Message Date
Sofia Rodrigues
423353421f fix: mention libuv just because it's a low level api 2025-06-27 07:50:14 -03:00
Sofia Rodrigues
01bb0dcad7 fix: rename lean_setup_args 2025-06-27 07:44:31 -03:00
Sofia Rodrigues
bc913c46c6 chore: fix docs 2025-06-26 17:53:32 -03:00
Sofia Rodrigues
1762f6e214 Merge branch 'sr/setup-libuv' of https://github.com/leanprover/lean4 into sr/setup-libuv 2025-06-26 11:47:06 -03:00
Sofia Rodrigues
3cefcb0731 fix: docs 2025-06-26 11:45:28 -03:00
Sofia Rodrigues
a9f2b630f4 chore: fix style
Co-authored-by: Eric Wieser <wieser.eric@gmail.com>
2025-06-26 10:45:42 -03:00
Sofia Rodrigues
9321d7035b Merge branch 'sr/setup-libuv' of https://github.com/leanprover/lean4 into sr/setup-libuv 2025-06-26 10:33:22 -03:00
Sofia Rodrigues
cfabacf8e4 fix: docs 2025-06-26 09:38:54 -03:00
Sofia Rodrigues
e8739d8690 docs: update doc/dev/ffi.md
Co-authored-by: Markus Himmel <markus@lean-fro.org>
2025-06-26 09:33:03 -03:00
Sofia Rodrigues
1a36c94a2b docs: fix docs 2025-06-20 19:46:51 -03:00
Sofia Rodrigues
8da1743a52 docs: update docs 2025-06-20 19:10:57 -03:00
Sofia Rodrigues
6fb98f02ce fix: argv 2025-06-18 01:10:21 -03:00
Sofia Rodrigues
951db9dbf8 fix: llvm 2025-06-12 12:11:50 -03:00
Sofia Rodrigues
c3d95f7ac4 fix: LLVM store instruction 2025-06-04 20:58:23 -03:00
Sofia Rodrigues
46b906814e chore: format 2025-06-04 19:51:14 -03:00
Sofia Rodrigues
26bd816c7d fix: parameter in llvm 2025-06-04 19:35:16 -03:00
Sofia Rodrigues
827d110717 feat: lean_setup_libuv 2025-06-04 19:08:12 -03:00
5 changed files with 34 additions and 2 deletions

View File

@@ -131,14 +131,21 @@ Thus `[init]` functions are run iff their module is imported, regardless of whet
The initializer for module `A.B` is called `initialize_A_B` and will automatically initialize any imported modules.
Module initializers are idempotent (when run with the same `builtin` flag), but not thread-safe.
**Important for process-related functionality**: If your application needs to use process-related functions from libuv, such as `Std.Internal.IO.Process.getProcessTitle` and `Std.Internal.IO.Process.setProcessTitle`, you must call `lean_setup_args(argc, argv)` (which returns a potentially modified `argv` that must be used in place of the original) **before** calling `lean_initialize()` or `lean_initialize_runtime_module()`. This sets up process handling capabilities correctly, which is essential for certain system-level operations that Lean's runtime may depend on.
Together with initialization of the Lean runtime, you should execute code like the following exactly once before accessing any Lean declarations:
```c
void lean_initialize_runtime_module();
void lean_initialize();
char ** lean_setup_args(int argc, char ** argv);
lean_object * initialize_A_B(uint8_t builtin, lean_object *);
lean_object * initialize_C(uint8_t builtin, lean_object *);
...
argv = lean_setup_args(argc, argv); // if using process-related functionality
lean_initialize_runtime_module();
//lean_initialize(); // necessary (and replaces `lean_initialize_runtime_module`) if you (indirectly) access the `Lean` package

View File

@@ -143,6 +143,7 @@ def emitMainFn : M Unit := do
unless xs.size == 2 || xs.size == 1 do throw "invalid main function, incorrect arity when generating code"
let env getEnv
let usesLeanAPI := usesModuleFrom env `Lean
emitLn "char ** lean_setup_args(int argc, char ** argv);";
if usesLeanAPI then
emitLn "void lean_initialize();"
else
@@ -158,6 +159,7 @@ def emitMainFn : M Unit := do
SetConsoleOutputCP(CP_UTF8);
#endif
lean_object* in; lean_object* res;";
emitLn "argv = lean_setup_args(argc, argv);";
if usesLeanAPI then
emitLn "lean_initialize();"
else

View File

@@ -1374,6 +1374,15 @@ def callLeanInitialize (builder : LLVM.Builder llvmctx) : M llvmctx Unit := do
let fn getOrCreateFunctionPrototype ( getLLVMModule) retty fnName argtys
let _ LLVM.buildCall2 builder fnty fn #[]
def callLeanSetupLibUV (builder : LLVM.Builder llvmctx) (argc argv : LLVM.Value llvmctx) : M llvmctx (LLVM.Value llvmctx) := do
let fnName := "lean_setup_args"
let intTy LLVM.i32Type llvmctx
let charPtrPtrTy LLVM.pointerType ( LLVM.pointerType ( LLVM.i8Type llvmctx))
let argtys := #[intTy, charPtrPtrTy]
let fnty LLVM.functionType charPtrPtrTy argtys
let fn getOrCreateFunctionPrototype ( getLLVMModule) intTy fnName argtys
LLVM.buildCall2 builder fnty fn #[argc, argv]
def callLeanInitializeRuntimeModule (builder : LLVM.Builder llvmctx) : M llvmctx Unit := do
let fnName := "lean_initialize_runtime_module"
let retty LLVM.voidType llvmctx
@@ -1479,6 +1488,12 @@ def emitMainFn (mod : LLVM.Module llvmctx) (builder : LLVM.Builder llvmctx) : M
let inslot buildPrologueAlloca builder ( LLVM.pointerType inty) "in"
let resty LLVM.voidPtrType llvmctx
let res buildPrologueAlloca builder ( LLVM.pointerType resty) "res"
let argcval LLVM.getParam main 0
let argvval LLVM.getParam main 1
let truncArgcval LLVM.buildSextOrTrunc builder argcval ( LLVM.i32Type llvmctx)
let argvval callLeanSetupLibUV builder truncArgcval argvval
if usesLeanAPI then callLeanInitialize builder else callLeanInitializeRuntimeModule builder
/- We disable panic messages because they do not mesh well with extracted closed terms.
See issue #534. We can remove this workaround after we implement issue #467. -/
@@ -1501,8 +1516,6 @@ def emitMainFn (mod : LLVM.Module llvmctx) (builder : LLVM.Builder llvmctx) : M
let _ LLVM.buildStore builder inv inslot
let ity LLVM.size_tType llvmctx
let islot buildPrologueAlloca builder ity "islot"
let argcval LLVM.getParam main 0
let argvval LLVM.getParam main 1
LLVM.buildStore builder argcval islot
buildWhile_ builder "argv"
(condcodegen := fun builder => do

View File

@@ -25,6 +25,10 @@ extern "C" void initialize_libuv() {
lthread([]() { event_loop_run_loop(&global_ev); });
}
extern "C" LEAN_EXPORT char ** lean_setup_args(int argc, char ** argv) {
return uv_setup_args(argc, argv);
}
/* Lean.libUVVersionFn : Unit → Nat */
extern "C" LEAN_EXPORT lean_obj_res lean_libuv_version(lean_obj_arg o) {
return lean_unsigned_to_nat(uv_version());
@@ -38,5 +42,10 @@ extern "C" LEAN_EXPORT lean_obj_res lean_libuv_version(lean_obj_arg o) {
return lean_box(0);
}
extern "C" LEAN_EXPORT char ** lean_setup_args(int argc, char ** argv) {
return argv;
}
#endif
}

View File

@@ -21,6 +21,7 @@ Author: Markus Himmel, Sofia Rodrigues
namespace lean {
extern "C" void initialize_libuv();
extern "C" LEAN_EXPORT char ** lean_setup_args(int argc, char ** argv);
// =======================================
// General LibUV functions.