Skip to content

Don't use configured CFLAGS & CPPFLAGS to compile third-party C sources#12589

Merged
dra27 merged 5 commits intoocaml:trunkfrom
shindere:no-cflags-propagation
Jul 16, 2024
Merged

Don't use configured CFLAGS & CPPFLAGS to compile third-party C sources#12589
dra27 merged 5 commits intoocaml:trunkfrom
shindere:no-cflags-propagation

Conversation

@shindere
Copy link
Copy Markdown
Contributor

When C flags or C preprocessor flags are passed to configure, they
should be used to build the compiler itself only, not when the
compiler is invoked to compile third-party C sources, like stubs.

The way flags were computed for the Sun C compiler looked suspiscious,
so it gets updated here, too.

@gasche
Copy link
Copy Markdown
Member

gasche commented Sep 20, 2023

I am worried that existing packagers of OCaml for Linux distributions could rely on the current interface to pass hardening flags, so for them the change would be a regression. We should check with the Debian and Fedora/RH people how their current packages handle this. (cc @glondu @rwmjones).

@glondu
Copy link
Copy Markdown
Contributor

glondu commented Sep 20, 2023

I am worried that existing packagers of OCaml for Linux distributions could rely on the current interface to pass hardening flags, so for them the change would be a regression.

With my Debian hat on: this reliance is accidental.

Instead, the rule is to use CFLAGS et al. at package build time. It is populated automatically by the Debian toolchain during package building, but users are free to set it to whatever they like for non-system software.

However, it turns out that these variables may be ignored by the build system, see for example ocaml/dune#8464.

shindere added a commit to shindere/ocaml that referenced this pull request Sep 20, 2023
We provide invalid CFLAGS and CPPFLAGS for the OCaml compiler
to make sure these flags are not used while building the
compiler itself.
shindere added a commit to shindere/ocaml that referenced this pull request Sep 20, 2023
We provide invalid CFLAGS and CPPFLAGS for the OCaml compiler
to make sure these flags are not used while building the
compiler itself.
shindere added a commit to shindere/ocaml that referenced this pull request Sep 20, 2023
We provide invalid CFLAGS and CPPFLAGS for the OCaml compiler
to make sure these flags are not used while building the
compiler itself.
@rwmjones
Copy link
Copy Markdown
Contributor

Adding @jamesjer too for Fedora

This change does seem totally backwards. We want the hardening flags to be passed through when compiling 3rd party software (or a way for us to be able to specify them to be passed through).

It has been an ongoing problem that there is no way to pass specific flags for the compiler, and for what the compiler passes through, which we have attempted to solve with downstream patching in the past eg this commit (which we have since dropped):
https://pagure.io/fedora-ocaml/c/2501cd8b3c40188e5eb6dd2158d61cfa023236c2

@rwmjones
Copy link
Copy Markdown
Contributor

@shindere
Copy link
Copy Markdown
Contributor Author

shindere commented Sep 21, 2023 via email

@rwmjones
Copy link
Copy Markdown
Contributor

Yes, that seems better. Jerry what do you think?

As background to this we need to run annocheck across both the OCaml compiler and OCaml programs. This will check that hardening flags were used to compile every bit of code, including the OCaml runtime (libasmrun.a and friends) and C objects which are linked in and might have been compiled using ocamlopt as a wrapper. Getting this right has occupied a lot of our time.

@gasche
Copy link
Copy Markdown
Member

gasche commented Sep 21, 2023

The current PR does not fulfill them in its present form, and I think your needs can be addressed in a separate PR because they are distinct.

Maybe this does not need to be done in one go, but I would prefer to see a "better approach to pass C compilation flags to the OCaml compilers" merged before (or at the same time that) we remove the current mechanism that (some) distributions rely on.

@shindere
Copy link
Copy Markdown
Contributor Author

shindere commented Sep 21, 2023 via email

@jamesjer
Copy link
Copy Markdown
Contributor

I agree with @rwmjones 's comments. I want to point out that we want to set both compiler and linker flags, including invocations of the linker from ocamlmklib.

shindere added 4 commits June 21, 2024 14:11
It seems also more coherent to have the flag just before the name
of the file that is being compiled.
The CFLAGS should not be included there, and -D_XPG6 should be in
preprocessor rather than compiler flags.
@shindere
Copy link
Copy Markdown
Contributor Author

I just pushed an updated version of this PR. The purpose of the PR remains the same, i.e. making sure that the C compiler/preprocessor flags passed to configure in the CFLAGS/CPPFLAGS configuration variables are used only during the build of the compiler and runtime themselves, but not when ocamlc or ocamlopt are used to compile third-party C sources (see issue #12578). The current version of the PR also takes into account both #13200 which reduced the scope of CFLAGS by not taking them into account at link time and #13201 which re-introduced the distinction between C compiler/preprocessor flags used to compile C codde for bytecode programs and those used to compile C code for native programs, as this distinction was not present when the present PR was initially submitted.

@rwmjones: this PR takes into account your comment by introducing new configuration variables: COMPILER_{BYTECODE,NATIVE}_{CFLAGS,CPPFLAGS} which make it possible to specify at compiler configuration time which C compiler/preprocesor flags should be used when compiling C sources through ocamlc/ocamlopt to be linked with bytecode/native programs.

This new version of the PR thus superseeds #12975 by taking into account #13200 and #13201.

The revised implementation starts with a commit ensuring that the -c flag always appears at the end of the comilation rule as this one should, like -o, never be overriden.

The core of the work lives in the PR's second commit.

A third commit fixes the configuration for the Sun C compiler where the CFLAGS were dealt with whereas they should not have been.

Finally, this updated version of the PR fixes a duplication of flags related to pthread in the build system that has been reported by @tmcgilchrist (thanks!). It was causing no harm but was not very elegant either.

@jamesjer at this stage I am unsure how to address your comment. Is the current support of LDFLAGS not good enough and, if not, are you able to explain in more details what is missing?

Changes Outdated

* #12589: Use configured CFLAGS and CPPFLAGS *only* during the build of
the compiler itself. Do not use them when compiling third-party
C sources trhough the compiler. Flags for compiling third-party C
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Suggested change
C sources trhough the compiler. Flags for compiling third-party C
C sources through the compiler. Flags for compiling third-party C

@shindere shindere force-pushed the no-cflags-propagation branch from aff1d6c to 238c66c Compare June 24, 2024 15:02
@shindere
Copy link
Copy Markdown
Contributor Author

shindere commented Jun 24, 2024 via email

@tmcgilchrist
Copy link
Copy Markdown
Contributor

The duplication of pthread is fixed, thank you @shindere. One small nitpick, a few variables have extra whitespace padding that could be trimmed. For example:

BYTECCLIBS=-L/opt/homebrew/opt/zstd/lib -lzstd    -lpthread
BYTECODE_CFLAGS=-O2 -fno-strict-aliasing -fwrapv  -pthread 
NATIVECCLIBS=   -lpthread

@jamesjer
Copy link
Copy Markdown
Contributor

The flags you added for @rwmjones sound like what I am interested in. Thank you for that. Are the flags passed to the linker by ocamlmklib affected, or just flags passed by ocamlc and ocamlopt? I would like to have some way to set those passed by ocamlmklib, if possible.

@shindere
Copy link
Copy Markdown
Contributor Author

shindere commented Jun 25, 2024 via email

@rwmjones
Copy link
Copy Markdown
Contributor

Whitespace is cosmetic and shouldn't make any difference, I wouldn't worry about it.

@shindere
Copy link
Copy Markdown
Contributor Author

shindere commented Jun 25, 2024 via email

@purplearmadillo77
Copy link
Copy Markdown
Contributor

Thanks for the comprehensive work and the bugfix! AFAICT this successfully prevents CFLAGS options from being passed to the user. With this pull request on the latest trunk, ./configure CFLAGS=-Dfoo on Cygwin produces

ocamlc hello.c -verbose
+ gcc -O2 -fno-strict-aliasing -fwrapv -pthread  -D_FILE_OFFSET_BITS=64 -U_WIN32   -c -I'/usr/lib/ocaml' 'hello.c'

as desired.

@shindere
Copy link
Copy Markdown
Contributor Author

shindere commented Jul 16, 2024 via email

Copy link
Copy Markdown
Member

@dra27 dra27 left a comment

Choose a reason for hiding this comment

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

Good to go, thanks, @shindere!

@dra27
Copy link
Copy Markdown
Member

dra27 commented Jul 16, 2024

I've put the PR through precheck just to test the couple of more obscure platforms available there, but good to merge once Changes is updated and that has passed.

@shindere shindere force-pushed the no-cflags-propagation branch from 238c66c to e443814 Compare July 16, 2024 12:33
@dra27 dra27 merged commit 9167dd1 into ocaml:trunk Jul 16, 2024
@shindere shindere deleted the no-cflags-propagation branch July 19, 2024 06:53
@shindere
Copy link
Copy Markdown
Contributor Author

shindere commented Jul 19, 2024 via email

shindere added a commit to shindere/ocaml that referenced this pull request Jul 19, 2024
shindere added a commit to shindere/ocaml that referenced this pull request Jul 19, 2024
shindere added a commit to shindere/ocaml that referenced this pull request Jul 19, 2024
shindere added a commit to shindere/ocaml that referenced this pull request Jul 19, 2024
shindere added a commit to shindere/ocaml that referenced this pull request Jul 19, 2024
shindere added a commit to shindere/ocaml that referenced this pull request Oct 3, 2024
…iables

They had been renamed in 31cdf41
(part of ocaml#12589) but we should keep the old names for backward compatibility.
shindere added a commit to shindere/ocaml that referenced this pull request Oct 3, 2024
…iables

They had been renamed in 31cdf41
(part of ocaml#12589) but we should keep the old names for backward compatibility.
shindere added a commit to shindere/ocaml that referenced this pull request Oct 7, 2024
…iables

They had been renamed in 31cdf41
(part of ocaml#12589) but we should keep the old names for backward compatibility.
shindere added a commit to shindere/ocaml that referenced this pull request Oct 7, 2024
…iables

They had been renamed in 31cdf41
(part of ocaml#12589) but we should keep the old names for backward compatibility.
shindere added a commit to shindere/ocaml that referenced this pull request Nov 5, 2024
…ables

They had been renamed in 31cdf41
(part of ocaml#12589) but we should keep the old names for backward compatibility.
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.

Should the compiler's compile-time CFLAGS be hardcoded into ocamlc/ocamlopt?

8 participants