From b952d9a764f4393fbabf9dfc1e6bda76f1eea9b2 Mon Sep 17 00:00:00 2001 From: Kate Date: Sat, 28 Sep 2024 01:32:34 +0100 Subject: [PATCH 1/3] Call the C++ compiler with -std=c++11 when using OCaml >= 5.0 Signed-off-by: Kate --- doc/changes/10962.md | 2 ++ src/dune_rules/cxx_flags.ml | 37 +++++++++++++++++++------------- src/dune_rules/cxx_flags.mli | 14 ++++++------ src/dune_rules/foreign_rules.ml | 2 +- src/dune_rules/lib_rules.ml | 6 +++--- src/dune_rules/ocaml_flags_db.ml | 2 +- 6 files changed, 36 insertions(+), 27 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..33c57d8e22d 100644 --- a/src/dune_rules/cxx_flags.ml +++ b/src/dune_rules/cxx_flags.ml @@ -1,24 +1,25 @@ 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) -> [] +let base_cxx_compile_flags ocaml_config = function + | Gcc | Clang -> + "-x" + :: "c++" + :: (if Ocaml_config.version ocaml_config >= (5, 0, 0) 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 @@ -59,8 +60,14 @@ let ccomp_type (ctx : Build_context.t) = ccomp_type ;; -let get_flags ~for_ ctx = +let get_compile_flags ocaml_version ctx = + let open Action_builder.O in + let+ ccomp_type = ccomp_type ctx in + base_cxx_compile_flags ocaml_version ccomp_type +;; + +let get_link_flags ctx = let open Action_builder.O in let+ ccomp_type = ccomp_type ctx in - base_cxx_flags ~for_ ccomp_type + base_cxx_link_flags ccomp_type ;; diff --git a/src/dune_rules/cxx_flags.mli b/src/dune_rules/cxx_flags.mli index eed02da3c9f..e43bfefe6be 100644 --- a/src/dune_rules/cxx_flags.mli +++ b/src/dune_rules/cxx_flags.mli @@ -3,10 +3,6 @@ open Import -type phase = - | Compile - | Link - (** The detected compiler *) type ccomp_type = | Gcc @@ -21,9 +17,13 @@ val preprocessed_filename : string (** [ccomp_type ctx] returns the C/C++ compiler type. *) val ccomp_type : Build_context.t -> ccomp_type Action_builder.t -(** [get_flags for_:phase ctx] returns the necessary flags to turn this compiler - into a c++ compiler for some of the most common compilers *) -val get_flags : for_:phase -> Build_context.t -> string list Action_builder.t +(** [get_compile_flags ctx] returns the necessary compile-time flags to turn + this compiler into a c++ compiler for some of the most common compilers *) +val get_compile_flags : Ocaml_config.t -> Build_context.t -> string list Action_builder.t + +(** [get_link_flags ctx] returns the necessary link-time flags to turn + this compiler into a c++ compiler for some of the most common compilers *) +val get_link_flags : Build_context.t -> string list Action_builder.t (** [fdiagnostics_color cc] returns the flags activating color diagnostics for the C/C++ compiler, if supported. *) diff --git a/src/dune_rules/foreign_rules.ml b/src/dune_rules/foreign_rules.ml index 86e9929558e..bdb379b346f 100644 --- a/src/dune_rules/foreign_rules.ml +++ b/src/dune_rules/foreign_rules.ml @@ -28,7 +28,7 @@ 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_compile_flags ocaml_config ctx in List.concat [ db_flags; cxxflags; fdiagnostics_color ] in c, cxx diff --git a/src/dune_rules/lib_rules.ml b/src/dune_rules/lib_rules.ml index 5db0c2b7154..a1bc0a3d581 100644 --- a/src/dune_rules/lib_rules.ml +++ b/src/dune_rules/lib_rules.ml @@ -67,7 +67,7 @@ let build_lib let+ project = Dune_load.find_project ~dir in match Dune_project.use_standard_c_and_cxx_flags project with | Some true when Buildable.has_foreign_cxx lib.buildable -> - Cxx_flags.get_flags ~for_:Link (Context.build_context ctx) + Cxx_flags.get_link_flags (Context.build_context ctx) | _ -> Action_builder.return [] in let cclibs = Expander.expand_and_eval_set expander lib.c_library_flags ~standard in @@ -263,7 +263,7 @@ let foreign_rules (library : Foreign.Library.t) ~sctx ~expander ~dir ~dir_conten match Dune_project.use_standard_c_and_cxx_flags project with | Some true when Foreign.Sources.has_cxx_sources foreign_sources -> let ctx = Super_context.context sctx in - Cxx_flags.get_flags ~for_:Link (Context.build_context ctx) + Cxx_flags.get_link_flags (Context.build_context ctx) | _ -> Action_builder.return [] in let c_library_flags = @@ -317,7 +317,7 @@ let build_stubs lib ~cctx ~dir ~expander ~requires ~dir_contents ~vlib_stubs_o_f let+ project = Dune_load.find_project ~dir in match Dune_project.use_standard_c_and_cxx_flags project with | Some true when Foreign.Sources.has_cxx_sources foreign_sources -> - Cxx_flags.get_flags ~for_:Link (Context.build_context ctx) + Cxx_flags.get_link_flags (Context.build_context ctx) | _ -> Action_builder.return [] in let c_library_flags = diff --git a/src/dune_rules/ocaml_flags_db.ml b/src/dune_rules/ocaml_flags_db.ml index 071a6170794..c64661d1e85 100644 --- a/src/dune_rules/ocaml_flags_db.ml +++ b/src/dune_rules/ocaml_flags_db.ml @@ -47,7 +47,7 @@ let link_env = ~name:"link-env" ~root:(fun ctx _ -> let default_cxx_link_flags = - Cxx_flags.get_flags ~for_:Link (Build_context.create ~name:ctx) + Cxx_flags.get_link_flags (Build_context.create ~name:ctx) in Link_flags.default ~default_cxx_link_flags |> Memo.return) ~f:(fun ~parent expander (env : Dune_env.config) -> From 20600827b6e261681565516c28ede8f91a81a30a Mon Sep 17 00:00:00 2001 From: Rudi Grinberg Date: Sat, 5 Oct 2024 23:12:38 +0100 Subject: [PATCH 2/3] _ Signed-off-by: Rudi Grinberg --- src/dune_rules/cxx_flags.ml | 11 +++++++++++ src/dune_rules/cxx_flags.mli | 14 +++++++------- src/dune_rules/foreign_rules.ml | 2 +- src/dune_rules/lib_rules.ml | 6 +++--- src/dune_rules/ocaml_flags_db.ml | 2 +- 5 files changed, 23 insertions(+), 12 deletions(-) diff --git a/src/dune_rules/cxx_flags.ml b/src/dune_rules/cxx_flags.ml index 33c57d8e22d..976d0af36f5 100644 --- a/src/dune_rules/cxx_flags.ml +++ b/src/dune_rules/cxx_flags.ml @@ -6,6 +6,10 @@ type ccomp_type = | Clang | Other of string +type phase = + | Compile of Ocaml_config.t + | Link + let base_cxx_compile_flags ocaml_config = function | Gcc | Clang -> "-x" @@ -71,3 +75,10 @@ let get_link_flags ctx = let+ ccomp_type = ccomp_type ctx in base_cxx_link_flags ccomp_type ;; + +let get_flags ~for_ ctx = + (match for_ with + | Compile config -> get_compile_flags config + | Link -> get_link_flags) + ctx +;; diff --git a/src/dune_rules/cxx_flags.mli b/src/dune_rules/cxx_flags.mli index e43bfefe6be..e2f19eb0ba9 100644 --- a/src/dune_rules/cxx_flags.mli +++ b/src/dune_rules/cxx_flags.mli @@ -3,6 +3,10 @@ open Import +type phase = + | Compile of Ocaml_config.t + | Link + (** The detected compiler *) type ccomp_type = | Gcc @@ -17,13 +21,9 @@ val preprocessed_filename : string (** [ccomp_type ctx] returns the C/C++ compiler type. *) val ccomp_type : Build_context.t -> ccomp_type Action_builder.t -(** [get_compile_flags ctx] returns the necessary compile-time flags to turn - this compiler into a c++ compiler for some of the most common compilers *) -val get_compile_flags : Ocaml_config.t -> Build_context.t -> string list Action_builder.t - -(** [get_link_flags ctx] returns the necessary link-time flags to turn - this compiler into a c++ compiler for some of the most common compilers *) -val get_link_flags : Build_context.t -> string list Action_builder.t +(** [get_flags for_:phase ctx] returns the necessary flags to turn this compiler + into a c++ compiler for some of the most common compilers *) +val get_flags : for_:phase -> Build_context.t -> string list Action_builder.t (** [fdiagnostics_color cc] returns the flags activating color diagnostics for the C/C++ compiler, if supported. *) diff --git a/src/dune_rules/foreign_rules.ml b/src/dune_rules/foreign_rules.ml index bdb379b346f..8aff28bf5a1 100644 --- a/src/dune_rules/foreign_rules.ml +++ b/src/dune_rules/foreign_rules.ml @@ -28,7 +28,7 @@ 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_compile_flags ocaml_config ctx in + and+ db_flags = Cxx_flags.get_flags ~for_:(Compile ocaml_config) ctx in List.concat [ db_flags; cxxflags; fdiagnostics_color ] in c, cxx diff --git a/src/dune_rules/lib_rules.ml b/src/dune_rules/lib_rules.ml index a1bc0a3d581..5db0c2b7154 100644 --- a/src/dune_rules/lib_rules.ml +++ b/src/dune_rules/lib_rules.ml @@ -67,7 +67,7 @@ let build_lib let+ project = Dune_load.find_project ~dir in match Dune_project.use_standard_c_and_cxx_flags project with | Some true when Buildable.has_foreign_cxx lib.buildable -> - Cxx_flags.get_link_flags (Context.build_context ctx) + Cxx_flags.get_flags ~for_:Link (Context.build_context ctx) | _ -> Action_builder.return [] in let cclibs = Expander.expand_and_eval_set expander lib.c_library_flags ~standard in @@ -263,7 +263,7 @@ let foreign_rules (library : Foreign.Library.t) ~sctx ~expander ~dir ~dir_conten match Dune_project.use_standard_c_and_cxx_flags project with | Some true when Foreign.Sources.has_cxx_sources foreign_sources -> let ctx = Super_context.context sctx in - Cxx_flags.get_link_flags (Context.build_context ctx) + Cxx_flags.get_flags ~for_:Link (Context.build_context ctx) | _ -> Action_builder.return [] in let c_library_flags = @@ -317,7 +317,7 @@ let build_stubs lib ~cctx ~dir ~expander ~requires ~dir_contents ~vlib_stubs_o_f let+ project = Dune_load.find_project ~dir in match Dune_project.use_standard_c_and_cxx_flags project with | Some true when Foreign.Sources.has_cxx_sources foreign_sources -> - Cxx_flags.get_link_flags (Context.build_context ctx) + Cxx_flags.get_flags ~for_:Link (Context.build_context ctx) | _ -> Action_builder.return [] in let c_library_flags = diff --git a/src/dune_rules/ocaml_flags_db.ml b/src/dune_rules/ocaml_flags_db.ml index c64661d1e85..071a6170794 100644 --- a/src/dune_rules/ocaml_flags_db.ml +++ b/src/dune_rules/ocaml_flags_db.ml @@ -47,7 +47,7 @@ let link_env = ~name:"link-env" ~root:(fun ctx _ -> let default_cxx_link_flags = - Cxx_flags.get_link_flags (Build_context.create ~name:ctx) + Cxx_flags.get_flags ~for_:Link (Build_context.create ~name:ctx) in Link_flags.default ~default_cxx_link_flags |> Memo.return) ~f:(fun ~parent expander (env : Dune_env.config) -> From 081269c84f0fc1b9042571b7afeeef6cfb26f5d2 Mon Sep 17 00:00:00 2001 From: Rudi Grinberg Date: Sat, 5 Oct 2024 23:19:14 +0100 Subject: [PATCH 3/3] _ Signed-off-by: Rudi Grinberg --- src/dune_rules/cxx_flags.ml | 24 ++++++------------- 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 +- 7 files changed, 18 insertions(+), 21 deletions(-) diff --git a/src/dune_rules/cxx_flags.ml b/src/dune_rules/cxx_flags.ml index 976d0af36f5..d2fcad154fb 100644 --- a/src/dune_rules/cxx_flags.ml +++ b/src/dune_rules/cxx_flags.ml @@ -7,14 +7,14 @@ type ccomp_type = | Other of string type phase = - | Compile of Ocaml_config.t + | Compile of Ocaml.Version.t | Link -let base_cxx_compile_flags ocaml_config = function +let base_cxx_compile_flags version = function | Gcc | Clang -> "-x" :: "c++" - :: (if Ocaml_config.version ocaml_config >= (5, 0, 0) then [ "-std=c++11" ] else []) + :: (if Ocaml.Version.add_std_cxx_flag version then [ "-std=c++11" ] else []) | Msvc -> [ "/TP" ] | Other _ -> [] ;; @@ -64,21 +64,11 @@ let ccomp_type (ctx : Build_context.t) = ccomp_type ;; -let get_compile_flags ocaml_version ctx = - let open Action_builder.O in - let+ ccomp_type = ccomp_type ctx in - base_cxx_compile_flags ocaml_version ccomp_type -;; - -let get_link_flags ctx = +let get_flags ~for_ ctx = let open Action_builder.O in let+ ccomp_type = ccomp_type ctx in - base_cxx_link_flags ccomp_type -;; - -let get_flags ~for_ ctx = (match for_ with - | Compile config -> get_compile_flags config - | Link -> get_link_flags) - ctx + | 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 e2f19eb0ba9..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 of Ocaml_config.t + | 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 8aff28bf5a1..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 ocaml_config) 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)))