Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[3.16] backport: #11009 #10962 #11051

Merged
merged 6 commits into from
Oct 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions doc/changes/10962.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- Call the C++ compiler with `-std=c++11` when using OCaml >= 5.0
(#10962, @kit-ty-kate)
36 changes: 22 additions & 14 deletions src/dune_rules/cxx_flags.ml
Original file line number Diff line number Diff line change
@@ -1,24 +1,29 @@
open Import

type phase =
| Compile
| Link

type ccomp_type =
| Gcc
| Msvc
| Clang
| Other of string

let base_cxx_flags ~for_ cc =
match cc, for_ with
| Gcc, Compile -> [ "-x"; "c++" ]
| Gcc, Link -> [ "-lstdc++"; "-shared-libgcc" ]
| Clang, Compile -> [ "-x"; "c++" ]
| Clang, Link -> [ "-lc++" ]
| Msvc, Compile -> [ "/TP" ]
| Msvc, Link -> []
| Other _, (Link | Compile) -> []
type phase =
| Compile of Ocaml.Version.t
| Link

let base_cxx_compile_flags version = function
| Gcc | Clang ->
"-x"
:: "c++"
:: (if Ocaml.Version.add_std_cxx_flag version then [ "-std=gnu++11" ] else [])
| Msvc -> [ "/TP" ]
| Other _ -> []
;;

let base_cxx_link_flags = function
| Gcc -> [ "-lstdc++"; "-shared-libgcc" ]
| Clang -> [ "-lc++" ]
| Msvc -> []
| Other _ -> []
;;

let fdiagnostics_color = function
Expand Down Expand Up @@ -63,5 +68,8 @@ let ccomp_type (ctx : Build_context.t) =
let get_flags ~for_ ctx =
let open Action_builder.O in
let+ ccomp_type = ccomp_type ctx in
base_cxx_flags ~for_ ccomp_type
(match for_ with
| Compile version -> base_cxx_compile_flags version
| Link -> base_cxx_link_flags)
ccomp_type
;;
2 changes: 1 addition & 1 deletion src/dune_rules/cxx_flags.mli
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
open Import

type phase =
| Compile
| Compile of Ocaml.Version.t
| Link

(** The detected compiler *)
Expand Down
6 changes: 5 additions & 1 deletion src/dune_rules/foreign_rules.ml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,11 @@ let default_context_flags (ctx : Build_context.t) ocaml_config ~project =
in
let cxx =
let+ fdiagnostics_color = fdiagnostics_color
and+ db_flags = Cxx_flags.get_flags ~for_:Compile ctx in
and+ db_flags =
Cxx_flags.get_flags
~for_:(Compile (Ocaml.Version.make (Ocaml_config.version ocaml_config)))
ctx
in
List.concat [ db_flags; cxxflags; fdiagnostics_color ]
in
c, cxx
Expand Down
1 change: 1 addition & 0 deletions src/ocaml/version.ml
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,4 @@ let supports_alerts version = version >= (4, 8, 0)
let has_sandboxed_otherlibs version = version >= (5, 0, 0)
let has_META_files version = version >= (5, 0, 0)
let supports_bin_annot_occurrences version = version >= (5, 2, 0)
let add_std_cxx_flag version = version >= (5, 0, 0)
2 changes: 2 additions & 0 deletions src/ocaml/version.mli
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,5 @@ val has_META_files : t -> bool

(** Whether the compiler supports occurrences indexation *)
val supports_bin_annot_occurrences : t -> bool

val add_std_cxx_flag : t -> bool
14 changes: 14 additions & 0 deletions test/blackbox-tests/test-cases/cpp-std-ocaml5.t/cpp11.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#include <iostream>
#include <cstring>
#include <caml/misc.h>
#include <caml/mlvalues.h>

// strdup is not part of the C standard library but is part of POSIX.
// By default GCC and Clang both use the GNU variant of the C standard,
// so we are using strdup here to ensure users have access to it by default.
// If -std=c++11 is used instead of -std=gnu++11 this would fail to compile
// on non-POSIX platforms such as Cygwin.
extern "C" CAMLprim value cpp11(value _unit) {
std::cout << strdup("Hi from C++11") << std::endl;
return Val_unit;
}
3 changes: 3 additions & 0 deletions test/blackbox-tests/test-cases/cpp-std-ocaml5.t/cpp11.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
external cpp11 : unit -> unit = "cpp11"

let () = cpp11 ()
19 changes: 19 additions & 0 deletions test/blackbox-tests/test-cases/cpp-std-ocaml5.t/run.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
-std=c++11 is given on OCaml 5.0
================================

$ cat >dune-project <<EOF
> (lang dune 3.14)
> EOF

$ cat >dune <<EOF
> ;; will fail if :standard doesn't have -std=c++11 on OCaml >= 5.0
> (executable
> (name cpp11)
> (foreign_stubs
> (language cxx)
> (names cpp11)
> (flags -std=c++98 :standard)))
> EOF

$ (dune build ./cpp11.exe 2>/dev/null) && _build/default/cpp11.exe
Hi from C++11
Loading