Skip to content

Upgrade cosmopolitan 4.0.2#52

Merged
dinosaure merged 27 commits intomainfrom
upgrade-cosmopolitan-4.0.2
Feb 17, 2026
Merged

Upgrade cosmopolitan 4.0.2#52
dinosaure merged 27 commits intomainfrom
upgrade-cosmopolitan-4.0.2

Conversation

@dinosaure
Copy link
Owner

And OCaml 5.3.0

@dinosaure
Copy link
Owner Author

HI @shym, I actually try to reproduce the support of Cosmopolitan with OCaml 5.3.0 and took what you did about ocaml-solo5. Actually, I get a weird issue which is: patches are not really applied into the ocaml distribution (they are mainly skipped) and the OCaml compiler can not be compiled with Cosmopolitan.

This is the output:

# if test -d "patches/$VERSION" ; then \
#   git apply -v --unsafe-paths --directory=ocaml "patches/$VERSION"/*; \
# fi
# Skipped patch 'ocaml/configure'.
# Skipped patch 'ocaml/configure.ac'.
# Skipped patch 'ocaml/aclocal.m4'.
# Skipped patch 'ocaml/configure'.
# Skipped patch 'ocaml/configure.ac'.
# Skipped patch 'ocaml/Makefile'.
# Skipped patch 'ocaml/Makefile.build_config.in'.
# Skipped patch 'ocaml/aclocal.m4'.
# Skipped patch 'ocaml/build-aux/ax_prog_cc_for_build.m4'.
# Skipped patch 'ocaml/configure'.
# Skipped patch 'ocaml/configure.ac'.
# Skipped patch 'ocaml/configure'.
# Skipped patch 'ocaml/configure.ac'.
# Skipped patch 'ocaml/configure'.
# Skipped patch 'ocaml/configure.ac'.
# Skipped patch 'ocaml/Makefile'.
# Skipped patch 'ocaml/Makefile.config.in'.
# Skipped patch 'ocaml/configure'.
# Skipped patch 'ocaml/configure.ac'.
# Skipped patch 'ocaml/configure'.
# Skipped patch 'ocaml/configure.ac'.
# Skipped patch 'ocaml/Makefile'.
# Skipped patch 'ocaml/Makefile.cross'.
# Skipped patch 'ocaml/Makefile.cross'.
# Skipped patch 'ocaml/Makefile.cross'.
# Skipped patch 'ocaml/runtime/debugger.c'.
# Skipped patch 'ocaml/runtime/amd64.S'.
# Skipped patch 'ocaml/runtime/arm64.S'.
# Skipped patch 'ocaml/runtime/power.S'.
# Skipped patch 'ocaml/runtime/riscv.S'.
# Skipped patch 'ocaml/runtime/s390x.S'.
# Skipped patch 'ocaml/aclocal.m4'.
# Skipped patch 'ocaml/configure'.
# Skipped patch 'ocaml/configure.ac'.
# Skipped patch 'ocaml/runtime/caml/m.h.in'.
# Skipped patch 'ocaml/utils/config.common.ml.in'.
# Skipped patch 'ocaml/utils/config.fixed.ml'.
# Skipped patch 'ocaml/utils/config.generated.ml.in'.
# Skipped patch 'ocaml/utils/config.mli'.
# Skipped patch 'ocaml/asmcomp/amd64/emit.mlp'.
# Skipped patch 'ocaml/asmcomp/arm64/emit.mlp'.
# Skipped patch 'ocaml/asmcomp/emitaux.ml'.
# Skipped patch 'ocaml/asmcomp/emitaux.mli'.
# Skipped patch 'ocaml/asmcomp/power/emit.mlp'.
# Skipped patch 'ocaml/asmcomp/riscv/emit.mlp'.
# Skipped patch 'ocaml/asmcomp/s390x/emit.mlp'.
# Skipped patch 'ocaml/runtime/amd64.S'.
# Skipped patch 'ocaml/runtime/arm64.S'.
# Skipped patch 'ocaml/runtime/caml/asm.h'.
# Skipped patch 'ocaml/runtime/power.S'.
# Skipped patch 'ocaml/runtime/riscv.S'.
# Skipped patch 'ocaml/runtime/s390x.S'.
# Skipped patch 'ocaml/testsuite/tools/asmgen_arm.S'.
# Skipped patch 'ocaml/testsuite/tools/asmgen_arm64.S'.
# Skipped patch 'ocaml/testsuite/tools/asmgen_power.S'.
# Skipped patch 'ocaml/testsuite/tools/asmgen_s390x.S'.
# Skipped patch 'ocaml/aclocal.m4'.
# Skipped patch 'ocaml/configure'.
# Skipped patch 'ocaml/configure.ac'.
# Skipped patch 'ocaml/runtime/caml/m.h.in'.
# Skipped patch 'ocaml/utils/config.common.ml.in'.
# Skipped patch 'ocaml/utils/config.fixed.ml'.
# Skipped patch 'ocaml/utils/config.generated.ml.in'.
# Skipped patch 'ocaml/utils/config.mli'.
# Skipped patch 'ocaml/asmcomp/amd64/emit.mlp'.
# Skipped patch 'ocaml/asmcomp/arm64/emit.mlp'.
# Skipped patch 'ocaml/asmcomp/emitaux.ml'.
# Skipped patch 'ocaml/asmcomp/emitaux.mli'.
# Skipped patch 'ocaml/asmcomp/power/emit.mlp'.
# Skipped patch 'ocaml/asmcomp/riscv/emit.mlp'.
# Skipped patch 'ocaml/asmcomp/s390x/emit.mlp'.
# Skipped patch 'ocaml/runtime/amd64.S'.
# Skipped patch 'ocaml/runtime/arm64.S'.
# Skipped patch 'ocaml/runtime/caml/asm.h'.
# Skipped patch 'ocaml/runtime/power.S'.
# Skipped patch 'ocaml/runtime/riscv.S'.
# Skipped patch 'ocaml/runtime/s390x.S'.
# Skipped patch 'ocaml/configure'.
# Skipped patch 'ocaml/configure.ac'.
# Skipped patch 'ocaml/configure'.
# Skipped patch 'ocaml/configure.ac'.
# Skipped patch 'ocaml/runtime/caml/domain.h'.
# Checking patch _opam/.opam-switch/build/aarch64-esperanto.dev/caml/ocaml/runtime/globroots.c...
# Applied patch _opam/.opam-switch/build/aarch64-esperanto.dev/caml/ocaml/runtime/globroots.c cleanly.
# Skipped patch 'ocaml/runtime/signals.c'.
# Skipped patch 'ocaml/runtime/sys.c'.
# Skipped patch 'ocaml/otherlibs/unix/termios.c'.
# Skipped patch 'ocaml/otherlibs/unix/access.c'.
# Skipped patch 'ocaml/otherlibs/unix/open_unix.c'.
# Skipped patch 'ocaml/otherlibs/unix/socket_unix.c'.
# Skipped patch 'ocaml/otherlibs/unix/sockopt_unix.c'.
# Skipped patch 'ocaml/otherlibs/unix/unixsupport_unix.c'.
# Skipped patch 'ocaml/otherlibs/unix/wait.c'.
# Skipped patch 'ocaml/otherlibs/unix/socketaddr.c'.
# Skipped patch 'ocaml/asmcomp/arm64/proc.ml'.
# PATH="$PWD/_build/build-toolchain:$PATH" ; \
# cd ocaml && \
#   ./configure \
# 	--target="aarch64-esperanto-ocaml" \
# 	--prefix="/home/runner/work/esperanto/esperanto/_opam/lib/aarch64-esperanto" \
# 	--disable-shared \
# 	--disable-systhreads \
# 	--disable-instrumented-runtime \
# 	--disable-debug-runtime \
# 	--disable-ocamltest \
# 	--disable-ocamldoc \
# 	--without-zstd \
# 	
# configure: Configuring OCaml version 5.3.0
# checking build system type... x86_64-pc-linux-gnu
# checking host system type... x86_64-pc-linux-gnu
# checking target system type... Invalid configuration 'aarch64-esperanto-ocaml': OS 'ocaml' not recognized
# configure: error: /bin/bash ./build-aux/config.sub aarch64-esperanto-ocaml failed

I don't really understand why we can not apply patches. I tried to use --unsafe-paths which is advertised by git apply if we would like to apply patches regardless a Git repository. Do you have any ideas why it fails? It will be superb to, at least, unlock Cosmopolitan for OCaml 5.3.

@shym
Copy link

shym commented Jun 21, 2025

Hi @dinosaure,
I finally understood. When running in a git repository, git apply does refer to the repository root to interpret the patches. So here we go, with a variant that should work both in a git clone and in an archive to publish it on the opam repository, I think.

diff --git i/caml/Makefile w/caml/Makefile
index 97dc862..c3dba6a 100644
--- i/caml/Makefile
+++ w/caml/Makefile
@@ -68,7 +68,7 @@ ocaml:
        cp -r "$$(ocamlfind query ocaml-src)" $@
        VERSION="$$(head -n1 ocaml/VERSION)" ; \
        if test -d "patches/$$VERSION" ; then \
-         git apply -v --unsafe-paths --directory=$@ "patches/$$VERSION"/*; \
+         cd .. && git apply --directory=caml/$@ "caml/patches/$$VERSION"/*; \
        fi

 ocaml/Makefile.config: $(LIBS) $(TOOLCHAIN_FOR_BUILD) | ocaml

(I admit that I stopped at getting that rule to go through, I didn’t try to build the whole project)

@dinosaure
Copy link
Owner Author

Hmmhmm, it seems that it does not work on the CI again 😕

@shym
Copy link

shym commented Jul 5, 2025

No idea came to mind on my end either.
I think I would try to apply patches one by one to see if this provides any clue:

	  cd .. && \
	  for p in "caml/patches/$$VERSION"/*; do \
	    (set -x; git apply --unsafe-path -v --directory=caml/$@ "$$p"; echo $$?) ; \
	  done ; \

and maybe run a check that the content of caml/ocaml is indeed what we hope if that first test doesn’t bring any hint.

@dinosaure
Copy link
Owner Author

@shym You might be interested in the result (and we should perhaps think carefully about this with regard to ocaml-solo5), but it seems that git apply does some black magic with the .git/index file implicitly. During my research, it is often mentioned that git apply attempts to correlate patches with what is in the repository. In this case, the project is not a Git repository, and therefore git apply cannot correlate the patches with what should be present.

The solution is to initialise a Git repository (with git init - see fb19ccd) so that there is a .git/index file that can track the files and folders. I also think that the fact that ocaml is in a subfolder called caml (unlike ocaml-solo5) plays a role in how git apply interprets the patches (since caml can be interpreted as an "empty folder" by Git and therefore not be recognized).

In short... this raises a lot of questions about the use of git apply and whether we should continue to use it. It might be worth considering using patch instead of git apply.

@dinosaure
Copy link
Owner Author

Hello @jart, it seems we are reaching the stage where we can produce an OCaml compiler (5.3.0) that uses Cosmopolitan. However, it seems that all syscalls end with ESPIPE.

You can test the project with opam:

$ opam switch create 5.3.0
$ git clone https://github.com/dinosaure/esperanto.git
$ git checkout upgrade-cosmopolitan-4.0.2
$ cd esperanto
$ opam pin add -y .
$ cd example/
$ dune build
$ ./link.sh
$ ./cat.com --strace
^D

I'm not sure I understand why all syscalls emit ESPIPE (perhaps another registry issue that OCaml and Cosmopolitan use?). I would appreciate some help with this part (I am also available via private message if you would like to discuss it). Thanks!

@dinosaure
Copy link
Owner Author

My last two commits fix the initial issue about our basic program cat.com:

  1. it seems that cosmopolitan is more stricter (POSIX) and lseek fails on TTY file-descriptors (according to POSIX) but OCam tries to lseek regardless it's a file or a TTY. A patch clear errno and ignore the error.
  2. it seems that we have a deadlock. I don't know precisely the assumption that the OCaml runtime have about mutex but if we initialize mutexes with RECURSIVE, it solves the issue. I think the initial problem is a bit more complex than that but let's move on.

@dinosaure
Copy link
Owner Author

Ok, I think that the last patch is a bug on cosmopolitan. I need to do some reproducible examples but it also seems that the deadlock part was not fundamentally changed. I will merge this PR. I will also try to move on a 5.4 and delete (as we did for ocaml-solo5) the use of git apply.

@dinosaure dinosaure merged commit 4deab38 into main Feb 17, 2026
2 checks passed
@dinosaure dinosaure deleted the upgrade-cosmopolitan-4.0.2 branch February 17, 2026 19:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants