#1014 Golang uses gcc to compile binaries but doesn't inherit CFLAGS.
Opened 3 years ago by jnovy. Modified 2 years ago
jnovy/packaging-committee golang  into  master

@@ -702,7 +702,7 @@ 

  

  Compilers used to build packages must honor the applicable compiler flags set in the system rpm configuration. Honoring means that the contents of that variable is used as the basis of the flags actually used by the compiler during the package build.

  

- For C, {cpp}, and Fortran code, the xref:RPMMacros.adoc#build-flags-macros-and-variables[%\{optflags} macro] contains these flags.

+ For C, {cpp}, Fortran and Golang code, the xref:RPMMacros.adoc#build-flags-macros-and-variables[%\{optflags} macro] contains these flags. For Golang it is required to specify a separate CGO_CFLAGS variable in spec file in order to take effect.

  Overriding these flags for performance optimizations (for instance, -O3 instead of -O2) is generally discouraged. If you can present benchmarks that show a significant speedup for this particular code, this could be revisited on a case-by-case basis. Adding to and overriding or filtering parts of these flags is permitted if there's a good reason to do so; the rationale for doing so must be documented in the specfile.

  

  There are certain, security related flags that are commonly allowed. These flags may degrade performance slightly but the increased security can be worthwhile for some programs.

The CGO_CFLAGS variable needs to be used in order to do so. Not
specifying CGO_CFLAGS would lead to not using %optflags and hence
in unoptimized/unhardened code.

Signed-off-by: Jindrich Novy jnovy@redhat.com

@deparker ping, is this something that could be added to the %gobuild macro?

I'm reviewing old tickets and after reading this I'm not entirely sure whether we were waiting on one of the Go maintainers to make some change or if we need to do something else.

Not sure who maintains the Go macros right now, but it would be good to have this in %gobuild, so packagers don't need to do this manually (or continue forgetting to do so). Maybe @eclipseo knows? He's who maintains most Go packages right now, I think.

BTW: I tried applying this to one of my Go packages (setting CGO_CFLAGS to what CFLAGS gets set to by %set_build_flags), and it immediately crashed the go compiler. :broken_heart:

Currently we use this for the toolbox RPM, and it seems to have held up for almost a year:

CGO_CFLAGS="%{optflags} -D_GNU_SOURCE -D_LARGEFILE_SOURCE -D_LARGEFILE64_SOURCE -D_FILE_OFFSET_BITS=64"
Metadata