Make commands for compiling Syncthing itself more portable

* Use CMake's command mode to set environment variables
* Only make use of `CMAKE_RANLIB` if set
* Avoid using the compiler/tools/flags from CMake via cgo when building
  with MSVC as this doesn't work; this is problematic because stdlibs will
  not necassarily match but this way we can at least compile the static
  library
This commit is contained in:
Martchus 2023-12-30 19:19:53 +01:00
parent c36b3223f1
commit bf884ba4cb
1 changed files with 25 additions and 8 deletions

View File

@ -24,6 +24,16 @@ if (NOT GO_BIN)
message(FATAL_ERROR "The go binary could not be located.")
endif ()
# define paths where the library and header generated by cgo are supposed to go
set(SYNCTHINGINTERNAL_LIBRARY_PATH "${CMAKE_CURRENT_BINARY_DIR}/libsyncthinginternal.a")
set(SYNCTHINGINTERNAL_HEADER_PATH "${CMAKE_CURRENT_BINARY_DIR}/libsyncthinginternal.h")
# define environment/args to use tools configured within CMake also with cgo
set(GO_COMPILER_ARGS "CC='${CMAKE_C_COMPILER}'" "CXX='${CMAKE_CXX_COMPILER}'" "AR='${CMAKE_C_COMPILER_AR}'")
if (CMAKE_RANLIB)
set(GO_RANLIB_ARGS "&&" "${CMAKE_RANLIB}" "${SYNCTHINGINTERNAL_LIBRARY_PATH}")
endif ()
# determine GOARCH for target
set(GO_TARGET_ARCH_OVERRIDE
""
@ -94,6 +104,15 @@ else ()
set(CGO_LDFLAGS "${CMAKE_SHARED_LINKER_FLAGS_INIT} ${CMAKE_SHARED_LINKER_FLAGS_${CMAKE_BUILD_TYPE_UPPER}_INIT}")
endif ()
# enforce defaults for cgo build when using MSVC as cgo doesn't seem to support it
if (MSVC)
unset(GO_COMPILER_ARGS)
unset(GO_RANLIB_ARGS)
unset(CGO_CFLAGS)
unset(CGO_CXXFLAGS)
unset(CGO_LDFLAGS)
endif ()
# locate the Syncthing checkout
set(GO_DEVELOPMENT_PATH
"${CMAKE_CURRENT_SOURCE_DIR}/go"
@ -163,23 +182,21 @@ set(GO_LINKER_FLAGS "${GO_LINKER_FLAGS} -X github.com/syncthing/syncthing/lib/bu
# generate Syncthing's assets (not setting GOARCH/GOOS here, this is supposed to run on the host)
add_custom_command(
OUTPUT "${SYNCTHING_PATH}/${GENERATED_SYNCTHING_GUI_FILES_RELATIVE_PATH}"
COMMAND "GO111MODULE=on" "GOPATH=${GO_DEVELOPMENT_PATH}" "GOFLAGS=${GO_FLAGS}" "${GO_BIN}" run ./script/genassets.go gui
COMMAND "${CMAKE_COMMAND}" -E env "GO111MODULE=on" "GOPATH=${GO_DEVELOPMENT_PATH}" "GOFLAGS=${GO_FLAGS}" "${GO_BIN}" run ./script/genassets.go gui
> "${GENERATED_SYNCTHING_GUI_FILES_RELATIVE_PATH}"
DEPENDS ${SRC_FILES_SYNCTHING_ASSETS}
WORKING_DIRECTORY "${SYNCTHING_PATH}"
COMMENT "Building Syncthing's assets")
# compile Syncthing as static library
set(SYNCTHINGINTERNAL_LIBRARY_PATH "${CMAKE_CURRENT_BINARY_DIR}/libsyncthinginternal.a")
set(SYNCTHINGINTERNAL_HEADER_PATH "${CMAKE_CURRENT_BINARY_DIR}/libsyncthinginternal.h")
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=${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}"
"${CMAKE_COMMAND}" -E env ${GO_COMPILER_ARGS}
"GOOS=${GO_TARGET_OS}" "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 ${GO_RANLIB_ARGS}
DEPENDS ${SRC_FILES_SYNCTHING}
WORKING_DIRECTORY "${SYNCTHING_PATH}"
COMMENT "Building Syncthing itself (as library)")