-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Should the compiler's compile-time CFLAGS be hardcoded into ocamlc/ocamlopt? #12578
Description
Hi,
I have been running into some unexpected behavavior with the default C compiler flags of ocamlc/ocamlopt on Cygwin64 with OCaml 4.14.0.
See some initial discussion here: https://cygwin.com/pipermail/cygwin-apps/2023-August/043106.html
When ocamlc compiles C programs, it gets passed some options
$ ocamlc hello.c -verbose
+ gcc -O2 -fno-strict-aliasing -fwrapv -pthread -ggdb -O2 -pipe -Wall -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -fstack-protector-strong --param=ssp-buffer-size=4 -fdebug-prefix-map=some_build_path1=/usr/src/debug/ocaml-4.14.0-1 -fdebug-prefix-map=some_build_path2=/usr/src/debug/ocaml-4.14.0-1 -D_FILE_OFFSET_BITS=64 -U_WIN32 -c -I'/usr/lib/ocaml' 'hello.c'
which are a superset of the package-time CFLAGS used to build the OCaml compiler itself. The package-time CFLAGS are hardcoded into the ocamlc executable itself.
Reading #9824, this appears to be an intentional design choice on OCaml's part but it can still cause odd behavior.
In short, all end user C programs compiled with ocamlc will inherit the options that were used to compile the compiler (that could range from very helpful to rather unnecessary options for most cases). However, it's possible some end user programs might not want some of the package-time options (-O2, -Wall, -Werror, -fdebug-prefix-map, etc.)
It is possible to negate some of these options on the user side; for example, if one wants to compile without debug info they need to remember to always call ocamlc -ccopt -ggdb0.
Even this might not always work: as far as I understand, GCC options with the property "RejectNegative" (https://gcc.gnu.org/onlinedocs/gccint/Option-properties.html) don't have a negative form and thus most of them can't be disabled in an obvious way.
Previous proposals to include more flags in ocamlopt have been wary about defaults that are too strict; e.g. #12050
"Enabling -ccopt "-Werror -Wall" when using ocamlc/ocamlopt to build C files is a choice of the build system, not something to be decided by the compiler itself."
However, the "compiler deciding itself" is effectively what happens when package time CFLAGS are hardcoded into ocamlc.
This places a silent but unexpected burden on users to inspect the ocamlc default compiler flags and disable ones they deem unnecessary. Is this phenomenon supposed to be part of the intended design choice?