Skip to content

Commit

Permalink
feat!: Remove MacOS-specific library flags (#111)
Browse files Browse the repository at this point in the history
This now requires consumers to specify `-cc clang++` on their
executables when compiling on MacOS. This is the only way to get
Binaryen to link into OCaml code on Mac's Arm64 architecture.
  • Loading branch information
phated committed Sep 15, 2021
1 parent f0af162 commit 5a67f9f
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 26 deletions.
18 changes: 18 additions & 0 deletions README.md
Expand Up @@ -42,6 +42,24 @@ This project aims to provide full feature parity with the [Binaryen C API](https

None of these are particularly challenging to create bindings for—they just haven't been written yet. If you need anything that's missing, feel free to open a PR.

## MacOS C++ Compiler

When including this library in your `dune` MacOS executables, you'll need to specify `-cc clang++` in your `(ocamlopt_flags)` stanza. This is required because Binaryen will throw errors for itself to catch and using `clang++` is the only way to handle them correctly. You can find more info on this [ocaml issue](https://github.com/ocaml/ocaml/issues/10423).

Your stanza could look something like this:

```diff
(executable
(name example)
(public_name example)
(package example)
+ (ocamlopt_flags -cc clang++)
(modules example)
(libraries binaryen))
```

These flags likely won't work on other operating systems, so you'll probably need to use `dune-configurator` to vary the flags per platform. You can see an example of this in our [tests/](./tests/dune).

## Static Linking

If you are planning to create portable binaries for Windows, it will try to find Cygwin/MinGW locations in your `PATH`. To avoid this, you probably want to add this to your `(executable)` stanzas:
Expand Down
8 changes: 0 additions & 8 deletions src/config/dune

This file was deleted.

16 changes: 0 additions & 16 deletions src/config/library_flags.ml

This file was deleted.

2 changes: 0 additions & 2 deletions src/dune
Expand Up @@ -11,8 +11,6 @@
binaryen_stubs_modules binaryen_stubs_settings ocaml_helpers)
(flags -O2 -Wall -Wextra))
(foreign_archives binaryen)
(library_flags
(:include ./config/library_flags.sexp))
(c_library_flags -lstdc++ -lpthread))

(rule
Expand Down
8 changes: 8 additions & 0 deletions test/config/dune
@@ -0,0 +1,8 @@
(executable
(name ocamlopt_flags)
(libraries dune.configurator))

(rule
(targets ocamlopt_flags.sexp)
(action
(run ./ocamlopt_flags.exe)))
16 changes: 16 additions & 0 deletions test/config/ocamlopt_flags.ml
@@ -0,0 +1,16 @@
module C = Configurator.V1

let () =
C.main ~name:"ocamlopt_flags" (fun c ->
let default = [] in

let ocamlopt_flags =
match C.ocaml_config_var c "system" with
| Some "macosx" ->
(* These flags preserve the original C++ error behavior. Ref https://github.com/ocaml/ocaml/issues/10423 *)
[ "-cc"; "clang++" ]
| Some _ -> default
| None -> default
in

C.Flags.write_sexp "ocamlopt_flags.sexp" ocamlopt_flags)
4 changes: 4 additions & 0 deletions test/dune
Expand Up @@ -4,6 +4,8 @@
(name test)
(modules test)
(libraries binaryen)
(ocamlopt_flags
(:include ./config/ocamlopt_flags.sexp))
(action
(run %{test})))

Expand All @@ -20,6 +22,8 @@
(name test_native)
(modules test_native)
(libraries binaryen binaryen.native)
(ocamlopt_flags
(:include ./config/ocamlopt_flags.sexp))
(action
(run %{test})))

Expand Down

0 comments on commit 5a67f9f

Please sign in to comment.