From 250f0b10fbd0758c86abe1c0517a63e8babdeaac Mon Sep 17 00:00:00 2001 From: Kate Date: Sun, 6 Oct 2024 17:44:40 +0100 Subject: [PATCH] Call the C++ compiler with -std=c++11 when using OCaml >= 5.0 (#10962) * Call the C++ compiler with -std=c++11 when using OCaml >= 5.0 --- doc/changes/10962.md | 2 ++ src/dune_rules/cxx_flags.ml | 36 +++++++++++-------- src/dune_rules/cxx_flags.mli | 2 +- src/dune_rules/foreign_rules.ml | 6 +++- src/ocaml/version.ml | 1 + src/ocaml/version.mli | 2 ++ .../test-cases/cpp-std-ocaml5.t/run.t | 2 +- test/blackbox-tests/test-cases/dune | 2 +- 8 files changed, 35 insertions(+), 18 deletions(-) create mode 100644 doc/changes/10962.md diff --git a/doc/changes/10962.md b/doc/changes/10962.md new file mode 100644 index 00000000000..b5073d70591 --- /dev/null +++ b/doc/changes/10962.md @@ -0,0 +1,2 @@ +- Call the C++ compiler with `-std=c++11` when using OCaml >= 5.0 + (#10962, @kit-ty-kate) diff --git a/src/dune_rules/cxx_flags.ml b/src/dune_rules/cxx_flags.ml index d4daffd73ea..d2fcad154fb 100644 --- a/src/dune_rules/cxx_flags.ml +++ b/src/dune_rules/cxx_flags.ml @@ -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=c++11" ] else []) + | Msvc -> [ "/TP" ] + | Other _ -> [] +;; + +let base_cxx_link_flags = function + | Gcc -> [ "-lstdc++"; "-shared-libgcc" ] + | Clang -> [ "-lc++" ] + | Msvc -> [] + | Other _ -> [] ;; let fdiagnostics_color = function @@ -62,5 +67,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 ;; diff --git a/src/dune_rules/cxx_flags.mli b/src/dune_rules/cxx_flags.mli index eed02da3c9f..0c01ae86bb8 100644 --- a/src/dune_rules/cxx_flags.mli +++ b/src/dune_rules/cxx_flags.mli @@ -4,7 +4,7 @@ open Import type phase = - | Compile + | Compile of Ocaml.Version.t | Link (** The detected compiler *) diff --git a/src/dune_rules/foreign_rules.ml b/src/dune_rules/foreign_rules.ml index 86e9929558e..de7f82c6ec9 100644 --- a/src/dune_rules/foreign_rules.ml +++ b/src/dune_rules/foreign_rules.ml @@ -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 diff --git a/src/ocaml/version.ml b/src/ocaml/version.ml index 3caf3c7c444..51f71e75187 100644 --- a/src/ocaml/version.ml +++ b/src/ocaml/version.ml @@ -30,3 +30,4 @@ 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 supports_hidden_includes version = version >= (5, 2, 0) +let add_std_cxx_flag version = version >= (5, 0, 0) diff --git a/src/ocaml/version.mli b/src/ocaml/version.mli index dc79429144e..c8d1fdb15ca 100644 --- a/src/ocaml/version.mli +++ b/src/ocaml/version.mli @@ -79,3 +79,5 @@ val supports_bin_annot_occurrences : t -> bool (** Whether the compiler supports the -H flag *) val supports_hidden_includes : t -> bool + +val add_std_cxx_flag : t -> bool diff --git a/test/blackbox-tests/test-cases/cpp-std-ocaml5.t/run.t b/test/blackbox-tests/test-cases/cpp-std-ocaml5.t/run.t index ea569532442..29cc0ac0431 100644 --- a/test/blackbox-tests/test-cases/cpp-std-ocaml5.t/run.t +++ b/test/blackbox-tests/test-cases/cpp-std-ocaml5.t/run.t @@ -16,4 +16,4 @@ > EOF $ (dune build ./cpp11.exe 2>/dev/null) && _build/default/cpp11.exe - [1] + Hi from C++11 diff --git a/test/blackbox-tests/test-cases/dune b/test/blackbox-tests/test-cases/dune index 7cefc4d35db..59e394349d5 100644 --- a/test/blackbox-tests/test-cases/dune +++ b/test/blackbox-tests/test-cases/dune @@ -119,7 +119,7 @@ (enabled_if %{bin-available:hg}))) (cram - (applies_to patch-back-source-tree cpp-std-ocaml5) + (applies_to patch-back-source-tree) (enabled_if (<> %{system} macosx)))