Use C/C++/linker flags for Go compilation from environment consistently

* Use flags from CMake's variables (initialized from environment variables)
  including build type specific variables
* Consider LDFLAGS as well; there's actually linking going on¹

¹Otherwise Go's build system wouldn't run into this error

```
# github.com/DataDog/zstd
/usr/lib/gcc/i686-w64-mingw32/10.2.0/../../../../i686-w64-mingw32/bin/ld: $WORK/b259/_x008.o:entropy_common.c:(.text+0xaa): undefined reference to `__memcpy_chk'
/usr/lib/gcc/i686-w64-mingw32/10.2.0/../../../../i686-w64-mingw32/bin/ld: $WORK/b259/_x033.o:zstd_v01.c:(.text+0x1b84): undefined reference to `__memcpy_chk'
collect2: error: ld returned 1 exit status
ninja: build stopped: subcommand failed.
```

which is caused by LDFLAGS being inconsistent with CFLAGS.
This commit is contained in:
Martchus 2020-11-02 23:17:37 +01:00
parent 0348c78e98
commit 7cea7509ba
1 changed files with 20 additions and 1 deletions

View File

@ -63,6 +63,7 @@ endif ()
message(STATUS "Using GOOS=${GO_TARGET_OS} and GOARCH=${GO_TARGET_ARCH}")
# define default GOFLAGS
string(TOUPPER "${CMAKE_BUILD_TYPE}" CMAKE_BUILD_TYPE_UPPER)
set(GO_FLAGS_OVERRIDE
""
CACHE STRING "overrides the 'GOFLAGS' variable")
@ -71,6 +72,24 @@ if (GO_FLAGS_OVERRIDE)
else ()
set(GO_FLAGS "-trimpath -mod=readonly -modcacherw")
endif ()
foreach (LANGUAGE C CXX)
set(CGO_${LANGUAGE}FLAGS_OVERRIDE
""
CACHE STRING "overrides the 'CGO_${LANGUAGE}FLAGS' variable")
if (CGO_${LANGUAGE}FLAGS_OVERRIDE)
set(CGO_${LANGUAGE}FLAGS "${CGO_${LANGUAGE}FLAGS_OVERRIDE}")
else ()
set(CGO_${LANGUAGE}FLAGS "${CMAKE_${LANGUAGE}_FLAGS_INIT} ${CMAKE_${LANGUAGE}_FLAGS_${CMAKE_BUILD_TYPE_UPPER}_INIT}")
endif ()
endforeach()
set(CGO_LDFLAGS_OVERRIDE
""
CACHE STRING "overrides the 'CGO_LDFLAGS' variable")
if (CGO_LDFLAGS_OVERRIDE)
set(CGO_LDFLAGS "${CGO_CFLAGS_OVERRIDE}")
else ()
set(CGO_LDFLAGS "${CMAKE_SHARED_LINKER_FLAGS_INIT} ${CMAKE_SHARED_LINKER_FLAGS_${CMAKE_BUILD_TYPE_UPPER}_INIT}")
endif ()
# locate the Syncthing checkout
set(GO_DEVELOPMENT_PATH
@ -152,7 +171,7 @@ add_custom_command(
OUTPUT "${SYNCTHINGINTERNAL_LIBRARY_PATH}" "${SYNCTHINGINTERNAL_HEADER_PATH}"
COMMAND
"CC=${CMAKE_C_COMPILER}" "CXX=${CMAKE_CXX_COMPILER}" "AR=${CMAKE_C_COMPILER_AR}" "GOOS=${GO_TARGET_OS}"
"CGO_CFLAGS=${CMAKE_C_FLAGS}" "CGO_CXXFLAGS=${CMAKE_CXX_FLAGS}" "GOARCH=${GO_TARGET_ARCH}" "CGO_ENABLED=1"
"CGO_CFLAGS=${CGO_CFLAGS}" "CGO_CXXFLAGS=${CGO_CXXFLAGS}" "CGO_LDFLAGS=${CGO_LDFLAGS}" "GOARCH=${GO_TARGET_ARCH}" "CGO_ENABLED=1"
"GO111MODULE=on" "GOPATH=${GO_DEVELOPMENT_PATH}" "GOFLAGS=${GO_FLAGS}" "${GO_BIN}" build -v -buildmode c-archive -o
"${SYNCTHINGINTERNAL_LIBRARY_PATH}" -ldflags "${GO_LINKER_FLAGS}" ./c-bindings && "${CMAKE_RANLIB}"
"${SYNCTHINGINTERNAL_LIBRARY_PATH}"