jart / cosmopolitan

build-once run-anywhere c library
ISC License
17.37k stars 597 forks source link

Cosmopolitan, Blink and `IsXnu` #815

Open dinosaure opened 1 year ago

dinosaure commented 1 year ago

I unfortunately not have a Mac M1 with me but it seems that when the program run on blink on a Mac M1, the IsXnu does not tell us that we are on a MacOS system. Currently, I try to recognize the system to set a file-descriptor in a non-blocking more. I use these functions in C (see esperanto:

CAMLprim value bob_is_linux(__unit()) {
#if defined(__ESPERANTO__)
#include "cosmopolitan.h"
  return Val_bool(IsLinux());
#elif defined(__linux__)
  return Val_true;
#else
  return Val_false;
#endif
}

CAMLprim value bob_is_macos(__unit()) {
#if defined(__ESPERANTO__)
#include null
  return Val_bool(IsXnu());
#elif (defined(__APPLE__) && defined(__MACH__))
  return Val_true;
#else
  return Val_false;
#endif
}

And this function in OCaml:

      Log.debug (fun m ->
          m "Call to connect() (operating system: %s)" Sys.os_type);
      let ivar : (unit, Unix.error) result Ivar.t = Ivar.create () in
      if is_linux () then (
        Hashtbl.add pwr fd (`Connect (sockaddr, ivar));
        Ivar.read ivar)
      else (
        Log.debug (fun m -> m "Set file-descriptor into a non-blocking mode.");
        set_nonblock fd true;
        try
          Log.debug (fun m -> m "Try to connect to %a" pp_sockaddr sockaddr);
          Unix.connect fd sockaddr;
          Hashtbl.add pwr fd (`Connect (sockaddr, ivar));
          Ivar.read ivar
        with
        | Unix.Unix_error (Unix.EINPROGRESS, _, _)
          when is_freebsd () || is_macos () ->
            Log.debug (fun m -> m "Connection is in progress.");
            Hashtbl.add pwr fd (`Connect (sockaddr, ivar));
            Ivar.read ivar
        | Unix.Unix_error (errno, _, _) ->
            Log.err (fun m ->
                m "Got an error for a non-blocking connect(): %s."
                  (Unix.error_message errno));
            return (Error errno))

On a Mac M1 (arm64), the second log Set file-descriptor into a non-blocking mode. does not appear. That mostly means that on such system, my current program recognized the host as a Linux system. I can make a little OCaml program to reproduce the error but a simple:

int main() {
  printf("%d\n" , IsLinux());
  return 0;
}

Should be enough to run on Mac M1 (and see what the printf tell to us).

dinosaure commented 1 year ago

I can confirm that currently, bob does not work properly on a FreeBSD (arm64). I got this output when I compile it with the last commit of Cosmopolitan cosmopolitan-2.2-189-gcba15bc77:

# ./bob.com
./bob.com: 7508: Syntax error: "(" unexpected

And with bob.com available from builds.osau.re, I got:

# ./bob.com
unsupported architecture