-
Notifications
You must be signed in to change notification settings - Fork 413
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
Add c_flags and cxx_flags to env profile settings #1700
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for your contribution. This feature is quite useful indeed.
Would you mind adding this feature to CHANGES as well?
src/super_context.ml
Outdated
end | ||
|
||
|
||
let cxx_flags_gather t ~dir ~(lib : Library.t) ccg = |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do we need the _gather
suffix here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is another cxx_flags function in Super_context, and a corresponding field of t. The existing function inspects context.ocamlc_flags and from them computes cxx_flags. Would it be better to rename existing cxx_flags to something like cxx_flags_from_ocamlc_flags or cxx_flags_default?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that it would be better to remove the original function. It seems like we shouldn't really be using it anymore as the cxx_flags
are now per directory.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The original cxx_flags has been used in combination with expanded lib.cxx_flags in lib_rules.ml to build_cxx_file. The new cxx_flags_gather replaces lib.cxx_flags. I am not sure why it makes the original cxx_flags obsolete. For that matter, I don't know why the original cxx_flags were computed in this way from ocamlc_cflags and what guarantees that C++ compiler knows about all possible ocamlc_cflags (except -std) used in a project. There must be some assumptions about the use of these cxx_flags that I am missing.
In any case, I attempted to make this change in two steps:
- change Super_context to rename the original cxx_flags to cxx_flags_orig and renamed cxx_flags_gather to cxx_flags.
55a4d22 - remove cxx_flags_orig from Super_context.
They are no longer used anywhere except in Pform.create, where they are getting an akward special treatment. I replaced it by recomputing them (as originally in super_context) and it is analogous to the way cflags are obtained in Pform.
61045a5
My main question is whether cxx_flags_orig need to be conjoined to the new cxx_flags or simply dropped from build_cxx_files in lib_rule. For now, I dropped them, but it might not be safe: removing the original cxx_flags might break other things in dune or external projects that rely on these flags being inferred from ocamlc_cflags.
There is a test in the testsuite for it that passes. I haven't had a chance to test it in a project that actually compiles, because the stubs in my project are in C, but I can create such a test, if this solution seems good to you.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For that matter, I don't know why the original cxx_flags were computed in this way from ocamlc_flags and what guarantees that C++ compiler knows about all possible ocamlc_flags (except -std) used in a project. The must be some assumptions about the use of these cxx_flags that I am missing.
In older version of OCaml there were no *_cppflags
in ocamlc -config
. We had to take the defaults from somewhere.
My main question is whether cxx_flags_orig need to be conjoined to the new cxx_flags or simply dropped from build_cxx_files in lib_rule. For now, I dropped them, but it might not be safe: removing the original cxx_flags might break other things in dune or external projects that rely on these flags being inferred from ocamlc_flags.
Those are the defaults so they should be inherited. If a user wants to drop them, then setting them explicitly is always possible (without the use of :standard
).
Your changes look good to me. Just to make sure we agree on how things should look like. This is the desired behavior of flags:
-
The default set of flags is inherited from
$ ocamlc -config
. This detection should be done in a way that understands the differences between different version of OCaml. Things aren't done correctly now, but we can try fixing them in subsequent PR's. -
The user may set flags in the environment. This can be done by appending to the set of flags with
(:standard -foo bar)
or simply resetting it with(-foo bar)
. -
Per library flags may also be set in the stanza itself. These follow the same rules as 2).
-
Doing
(run %{cxx} foo.cxx)
should use the flags defined in the environment.
It seems like that the current code does all of this correctly except 4).
There is a test in the testsuite for it that passes. I haven't had a chance to test it in a project that actually compiles, because the stubs in my project are in C, but I can create such a test, if this solution seems good to you.
Tests would indeed be useful. Especially for point 4).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for the explanations! It makes sense. I'll try to add a test for (4).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added a test for (4) as far as I understand it, but the test fails because I do not know how to implement (4). Please can you tell me if the test captures what you meant, and if so how to fix the code.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My main question is whether cxx_flags_orig need to be conjoined to the new cxx_flags or simply dropped from build_cxx_files in lib_rule. For now, I dropped them, but it might not be safe: removing the original cxx_flags might break other things in dune or external projects that rely on these flags being inferred from ocamlc_cflags.
Those are the defaults so they should be inherited. If a user wants to drop them, then setting them explicitly is always possible (without the use of
:standard
).
It looks like cxx_flags_orig was always added in lib_rules, regardless of ":standard".
After removing this default in commit 61045a5, I have noticed that one package from opam fails to build with this version.
The package is re2 and the problem is in
https://github.com/janestreet/re2/blob/master/src/jbuild.
Adding -fPIC at least on my machine fixes the problem, not sure if that's a good solution in general.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looks like cxx_flags_orig was always added in lib_rules, regardless of ":standard".
After removing this default in commit 61045a5, I have noticed that one package from opam fails to build with this version.
The package is re2 and the problem is in
https://github.com/janestreet/re2/blob/master/src/jbuild.
Adding -fPIC at least on my machine fixes the problem, not sure if that's a good solution in general.
This is now fixed. Thanks to @xclerc for help!
Please sign your commits according to: https://github.com/ocaml/dune/blob/master/CONTRIBUTING.md |
61ed09e
to
c9acd0b
Compare
I reviewed the latest commits, it looks fine to me. Could you rebase your PR?
I'm a bit confused here. As far as I can tell, your test shows the desired behavior is exhibited and the flags from the env are being passed to Btw, I just realized that we haven't accounted for configurator here. Configurator also compiles C/C++ files so perhaps it should also inherit the environment flags. Currently, it accesses them straight from the context. Do you think it would be desirable to make configurator aware of the setting in env? Btw: some commits are missing the DCO. Once you've done rebasing, I believe that |
Also, let's see what @diml has to say about having configurator respect these c flags:
|
I'm not entirely sure. I suppose both choices make sense. We can probably start with the option that's the simplest to implement and see how it goes. |
I think the main usecase for this feature will be to add:
In all of these cases, it seems more useful for configurator to respect the passed in environment flags. It's just important that these cflags are passed consistently to all the dune-related things that use them. It would be most useful if configurator could record somewhere in the build tree the actual cflags that it ended up using (i.e. the combination of the env and local cflags) |
Ok, that makes sense. However, let's not make it a requirement to merge this PR. It can be done later in a subsequent PR. |
Indeed, one of us can implement this. I've created #1771 to track this. |
479a6f5
to
a8056c0
Compare
Signed-off-by: Greta Yorsh <gyorsh@janestreet.com>
Signed-off-by: Greta Yorsh <gyorsh@janestreet.com>
Signed-off-by: Greta Yorsh <gyorsh@janestreet.com>
Signed-off-by: Greta Yorsh <gyorsh@janestreet.com>
Signed-off-by: Greta Yorsh <gyorsh@janestreet.com>
Signed-off-by: Greta Yorsh <gyorsh@janestreet.com>
and fix a typo. Signed-off-by: Greta Yorsh <gyorsh@janestreet.com>
Signed-off-by: Greta Yorsh <gyorsh@janestreet.com>
Signed-off-by: Greta Yorsh <gyorsh@janestreet.com>
Signed-off-by: Greta Yorsh <gyorsh@janestreet.com>
a8056c0
to
9df8f00
Compare
I rebased and fixed expected test outputs to check the defaults based on ocamlc -config using some horrendous combination of sed commands. There must be a better way but this works for now and there are other uses of sed in the test suite.
The test fails and I don't know how to fix the code.
Can you please give me a bit more direction for it? |
In pform.ml,
But that's no longer sufficient and we should update the expander for every
Where the flags will be fetched from the current env_node (using the c_flags and cxx_flags functions you've defined) all inside the |
@@ -0,0 +1,18 @@ | |||
$ export STANDARD_C_FLAGS=`ocamlc -config | grep ocamlc_cflags | sed 's/ocamlc_cflags: //'` | |||
$ export STANDARD_CXX_FLAGS=`ocamlc -config | grep ocamlc_cflags | sed 's/ocamlc_cflags: //' | sed 's/-std=[^ ]* //' | sed 's/-std=[^$]*//'` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think CI shows that this doesn't work. Dune starts a new sh process for every $
so the environment variables aren't saved. I'm not sure how to workaround this except for writing your own .sh files that will wrap the command with these substitutions.
@gretay-js are you still planning on finishing this? Otherwise I can take over. This feature would be really nice to have. |
Sorry about the delay. Let me have another go at fixing it. |
Signed-off-by: Greta Yorsh <gyorsh@janestreet.com>
Signed-off-by: Greta Yorsh <gyorsh@janestreet.com>
In commit a98b830, I fixed the test failures that depend on environment variables as you suggested by adding a script that filters output of printenv. Regarding bindings for cc and cxx variables that depend on the bindings of c_flags and cxx_flags variables. Commit 675a0cd attempts to deal with it following your comment earlier, but it doesn't work and doesn't even build. The problem is that Env.cxx_flags functions that I added return (unit, string list) Build.t and add_bindings expects a string list directly. |
Yeah, it's quite a bit more complicated than I expected. The idea is that we will not be expanding Unfortunately, that means that it will no longer be possible to expand However, if we're introducing full support for C/C++ dependencies I wonder if it makes sense to have |
We need a replacement first. These variables might be used in contexts that can't be easily changed:
So it seems better to me if we keep them. It most places, things that are expanded dynamically can also be expanded statically when they don't require dynamic expansion. Can't we do the same here? This way it wouldn't break anything. |
This PR has been revived in #1860 |
CHANGES: - Clean up watch mode polling loop: improves signal handling and error handling during polling (ocaml/dune#1912, fix ocaml/dune#1907, fix ocaml/dune#1671, @aalekseyev) - Change status messages during polling to be one-line, so that the messages are correctly erased by ^K. (ocaml/dune#1912, @aalekseyev) - Add support for `.cxx` extension for C++ stubs (ocaml/dune#1831, @rgrinberg) - Add `DUNE_WORKSPACE` variable. This variable is equivalent to setting `--workspace` in the command line. (ocaml/dune#1711, fix ocaml/dune#1503, @rgrinberg) - Add `c_flags` and `cxx_flags` to env profile settings (ocaml/dune#1700 and ocaml/dune#1800, @gretay-js) - Format `dune printenv` output (ocaml/dune#1867, fix ocaml/dune#1862, @emillon) - Add the `(promote-into <dir>)` and `(promote-until-clean-into <dir>)` modes for `(rule ...)` stanzas, so that files can be promoted in another directory than the current one. For instance, this is used in merlin to promote menhir generated files in a directory that depends on the version of the compiler (ocaml/dune#1890, @diml) - Improve error message when `dune subst` fails (ocaml/dune#1898, fix ocaml/dune#1897, @rgrinberg) - Add more GC counters to catapult traces (fix908, @rgrinberg) - Add a preprocessor shim for the `let+` syntax of OCaml 4.08 (ocaml/dune#1899, implements ocaml/dune#1891, @diml) - Fix generation of `.merlin` files on Windows. `\` characters needed to be escaped (ocaml/dune#1869, @mlasson) - Fix 0 error code when `$ dune format-dune-file` fails. (ocaml/dune#1915, fix ocaml/dune#1914, @rgrinberg) - Configurator: deprecated `query_expr` and introduced `query_expr_err` which is the same but with a better error in case it fails. (ocaml/dune#1886, @ejgallego)
CHANGES: - Clean up watch mode polling loop: improves signal handling and error handling during polling (ocaml/dune#1912, fix ocaml/dune#1907, fix ocaml/dune#1671, @aalekseyev) - Change status messages during polling to be one-line, so that the messages are correctly erased by ^K. (ocaml/dune#1912, @aalekseyev) - Add support for `.cxx` extension for C++ stubs (ocaml/dune#1831, @rgrinberg) - Add `DUNE_WORKSPACE` variable. This variable is equivalent to setting `--workspace` in the command line. (ocaml/dune#1711, fix ocaml/dune#1503, @rgrinberg) - Add `c_flags` and `cxx_flags` to env profile settings (ocaml/dune#1700 and ocaml/dune#1800, @gretay-js) - Format `dune printenv` output (ocaml/dune#1867, fix ocaml/dune#1862, @emillon) - Add the `(promote-into <dir>)` and `(promote-until-clean-into <dir>)` modes for `(rule ...)` stanzas, so that files can be promoted in another directory than the current one. For instance, this is used in merlin to promote menhir generated files in a directory that depends on the version of the compiler (ocaml/dune#1890, @diml) - Improve error message when `dune subst` fails (ocaml/dune#1898, fix ocaml/dune#1897, @rgrinberg) - Add more GC counters to catapult traces (fix908, @rgrinberg) - Add a preprocessor shim for the `let+` syntax of OCaml 4.08 (ocaml/dune#1899, implements ocaml/dune#1891, @diml) - Fix generation of `.merlin` files on Windows. `\` characters needed to be escaped (ocaml/dune#1869, @mlasson) - Fix 0 error code when `$ dune format-dune-file` fails. (ocaml/dune#1915, fix ocaml/dune#1914, @rgrinberg) - Configurator: deprecated `query_expr` and introduced `query_expr_err` which is the same but with a better error in case it fails. (ocaml/dune#1886, @ejgallego) - Make sure `(menhir (mode promote) ...)` stanzas are ignored when using `--ignore-promoted-rules` or `-p` (ocaml/dune#1917, @diml)
Add support for c_flags and cxx_flags fields in profile settings of env stanza. This is in addition to existing ocaml flags in env. It enables us to specify compilation flags per profile when building libraries with C or C++ stubs.