Skip to content

Use {CPP,C,CXX}FLAGS from environment to compile foreign files#8464

Closed
glondu wants to merge 1 commit intoocaml:mainfrom
glondu:use-env-flags
Closed

Use {CPP,C,CXX}FLAGS from environment to compile foreign files#8464
glondu wants to merge 1 commit intoocaml:mainfrom
glondu:use-env-flags

Conversation

@glondu
Copy link
Copy Markdown
Contributor

@glondu glondu commented Aug 22, 2023

See #8291

| Cxx -> Fdo.cxx_flags ctx
| Cxx ->
List.concat
[ get_env_flags "CXXFLAGS"
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can use Env.get instead here. The Env.t value should come from ctx.env.

We should probably also guard this behaviour behind dune lang. cc @rgrinberg wdyt?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can use Env.get instead here. The Env.t value should come from ctx.env.

Done.

We should probably also guard this behaviour behind dune lang.

...or behind a command-line flag, maybe? My goal is to make all packages take CFLAGS into account without changing each package... In the current state of matters (in Debian), it is easy to add a command-line flag to all calls to dune build. But I am not aware of a way to update all packages to a new dune lang at once (and with my current understanding, it cannot be done blindly).

cc @rgrinberg wdyt?

What does "wdyt" mean?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Guarding it behind a command line flag is fine. Do you use a custom workspace file when building dune packages in debian? Putting it there is fine too.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you use a custom workspace file when building dune packages in debian?

I was not familiar with this notion, so no. The way most dune packages are built in debian is defined here... Sorry, it's Perl, but should be understandable: the interesting parts are the last four functions (build, test, install and clean).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Guarding it behind a command line flag is fine.

I've implemented that in a separate commit. I'm wondering if this is the right move, though. The feature could be useful for installing opam packages, too, and there is no way to change the command used there...?

Copy link
Copy Markdown
Collaborator

@Alizter Alizter Aug 23, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does "wdyt" mean?

"What do you think"

@rgrinberg rgrinberg added the requires-team-discussion This topic requires a team discussion label Aug 22, 2023
@glondu glondu force-pushed the use-env-flags branch 2 times, most recently from 7d4c65e to 20cc2fd Compare August 23, 2023 07:10
@MisterDA
Copy link
Copy Markdown
Contributor

MisterDA commented Sep 4, 2023

We could also check the CPPFLAGS variable for flags specific to the C/C++ preprocessor. It is (at least) understood by GNU Make.

@rgrinberg rgrinberg removed the requires-team-discussion This topic requires a team discussion label Sep 8, 2023
@rgrinberg
Copy link
Copy Markdown
Member

@MisterDA Should we add LDFLAGS as well?

I discussed this with the team and it seems like this feature is the right way to go. To proceed with the PR:

  • Please add support for CPPFLAGS
  • Add some tests exercising this feature (see test/blackbox-tests/test-cases for examples)
  • Add a change log entry in doc/changes

@rgrinberg
Copy link
Copy Markdown
Member

@MisterDA since you're interested, do you mind reviewing this one?

@rgrinberg rgrinberg linked an issue Sep 8, 2023 that may be closed by this pull request
@MisterDA
Copy link
Copy Markdown
Contributor

MisterDA commented Sep 8, 2023

Should we add LDFLAGS as well?

Yes! Looking for inspiration, Arch Linux' makepkg (the program used to build custom packages) allows user to set CPPFLAGS, CFLAGS, CXXFLAGS, and LDFLAGS globally.
LDFLAGS can be used to pick a linker, they show LDFLAGS="... -fuse-ld=mold".

Since you're interested, do you mind reviewing this one?

It looks good, but some tests showcasing this would be nice, indeed.

As to whether use the env var by default or not (use the flag), I've looked at Meson Compiler and linker flag environment variables: it supports them by default in the name of backwards compatibility, but recommends against their use and suggest explicit configuration of the project. I don't have a strong opinion, but backwards compatibility is a compelling argument.

@glondu
Copy link
Copy Markdown
Contributor Author

glondu commented Sep 13, 2023

I added support for CPPFLAGS, removed the --use-env-flags command-line argument, and rebased on current main.

@rgrinberg
Copy link
Copy Markdown
Member

Thanks @glondu. What do you think about supporting LDFLAGS as well?

See ocaml#8291

Signed-off-by: Stephane Glondu <steph@glondu.net>
@glondu
Copy link
Copy Markdown
Contributor Author

glondu commented Sep 13, 2023

What do you think about supporting LDFLAGS as well?

I'm not sure about this one. According to my interpretation of make's built-in rules, they should be used in linking rules (and I would say all of them, even pure OCaml ones), which I haven't located yet (I'm new to dune's codebase).

FYI, in Debian package building environment, LDFLAGS is set to -Wl,-z,relro which is used for hardening.

@glondu glondu changed the title Use CFLAGS and CXXFLAGS from environment to compile foreign files Use {CPP,C,CXX}FLAGS from environment to compile foreign files Sep 13, 2023
@rgrinberg
Copy link
Copy Markdown
Member

Please re-open if you intend to finish this PR

@rgrinberg rgrinberg closed this Dec 9, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Please honor CFLAGS

4 participants