Handle cross-compilation when building libsyncthing

This commit is contained in:
Martchus 2018-04-10 21:06:08 +02:00
parent d08aa005d7
commit 4e112547e1
1 changed files with 51 additions and 4 deletions

View File

@ -22,11 +22,48 @@ if(NOT GO_BIN)
message(FATAL_ERROR "The go binary could not be located.")
endif()
# determine GOARCH for target
set(GO_TARGET_ARCH_OVERRIDE "" CACHE STRING "overrides the 'GOARCH' variable")
if(GO_TARGET_ARCH_OVERRIDE)
set(GO_TARGET_ARCH "${GO_TARGET_ARCH_OVERRIDE}")
elseif(NOT CMAKE_CROSSCOMPILING)
set(GO_TARGET_ARCH "")
elseif(CMAKE_SYSTEM_PROCESSOR STREQUAL "x86_64")
set(GO_TARGET_ARCH "amd64")
elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "i[3-6]86")
set(GO_TARGET_ARCH "386")
elseif(CMAKE_SYSTEM_NAME MATCHES "(ppc64|ppc64le|arm|arm64|s390x)")
set(GO_TARGET_ARCH "${CMAKE_SYSTEM_NAME}")
else()
message(FATAL_ERROR "Unable to auto-determine GOARCH. Please set GO_TARGET_ARCH_OVERRIDE manually.")
endif()
# determine GOOS for target
set(GO_TARGET_OS_OVERRIDE "" CACHE STRING "overrides the 'GOOS' variable")
if(GO_TARGET_OS_OVERRIDE)
set(GO_TARGET_OS "${GO_TARGET_OS_OVERRIDE}")
elseif(NOT CMAKE_CROSSCOMPILING)
set(GO_TARGET_OS "")
elseif(CMAKE_SYSTEM_NAME STREQUAL "Linux")
set(GO_TARGET_OS "linux")
elseif(CMAKE_SYSTEM_NAME STREQUAL "Android")
set(GO_TARGET_OS "android")
elseif(CMAKE_SYSTEM_NAME STREQUAL "Windows")
set(GO_TARGET_OS "windows")
elseif(CMAKE_SYSTEM_NAME STREQUAL "FreeBSD")
set(GO_TARGET_OS "freebsd")
elseif(CMAKE_SYSTEM_NAME STREQUAL "Darwin")
set(GO_TARGET_OS "darwin")
else()
message(FATAL_ERROR "Unable to auto-determine GOOS. Please set GO_TARGET_OS_OVERRIDE manually.")
endif()
message(STATUS "Using GOOS=${GO_TARGET_OS} and GOARCH=${GO_TARGET_ARCH}")
# locate the Syncthing checkout
set(GO_DEVELOPMENT_PATH "${CMAKE_CURRENT_SOURCE_DIR}/go" CACHE STRING "the 'GOPATH'")
set(SYNCTHING_PATH "${CMAKE_CURRENT_SOURCE_DIR}/go/src/github.com/syncthing/syncthing" CACHE STRING "path of Syncthing checkout")
set(GO_DEVELOPMENT_PATH "${CMAKE_CURRENT_SOURCE_DIR}/go" CACHE PATH "the 'GOPATH'")
set(SYNCTHING_PATH "${CMAKE_CURRENT_SOURCE_DIR}/go/src/github.com/syncthing/syncthing" CACHE PATH "path of Syncthing checkout")
# find Syncthing's assets
# caveat: newly added assets will not cause CMake to automatically regenerate
file(GLOB_RECURSE SRC_FILES_SYNCTHING_ASSETS
LIST_DIRECTORIES false
RELATIVE "${CMAKE_CURRENT_SOURCE_DIR}"
@ -37,6 +74,7 @@ if(NOT SRC_FILES_SYNCTHING_ASSETS)
endif()
# find Syncthing's source code
# caveat: newly added files will not cause CMake to automatically regenerate
file(GLOB_RECURSE SRC_FILES_SYNCTHING
LIST_DIRECTORIES false
RELATIVE "${CMAKE_CURRENT_SOURCE_DIR}"
@ -48,7 +86,7 @@ endif()
list(APPEND SRC_FILES_SYNCTHING "${SYNCTHING_PATH}/lib/auto/gui.files.go")
message(STATUS "Syncthing's go files: ${SRC_FILES_SYNCTHING}")
# generate Syncthing's assets
# generate Syncthing's assets: don't set GOARCH/GOOS here, this is supposed to run on the host
add_custom_command(
OUTPUT
"${SYNCTHING_PATH}/lib/auto/gui.files.go"
@ -61,12 +99,17 @@ add_custom_command(
)
# compile Syncthing as static library
# FIXME: handle cross-compilation
add_custom_command(
OUTPUT
"${CMAKE_CURRENT_BINARY_DIR}/libsyncthinginternal.a"
"${CMAKE_CURRENT_BINARY_DIR}/libsyncthinginternal.h"
COMMAND
"CC=${CMAKE_C_COMPILER}"
"CXX=${CMAKE_CXX_COMPILER}"
"AR=${CMAKE_C_COMPILER_AR}"
"GOOS=${GO_TARGET_OS}"
"GOARCH=${GO_TARGET_ARCH}"
"CGO_ENABLED=1"
"GOPATH=${GO_DEVELOPMENT_PATH}"
"${GO_BIN}" build -v -buildmode c-archive
-o "${CMAKE_CURRENT_BINARY_DIR}/libsyncthinginternal.a"
@ -78,6 +121,10 @@ add_custom_command(
# do not use this library directly but depend on it
list(APPEND PRIVATE_LIBRARIES "${CMAKE_CURRENT_BINARY_DIR}/libsyncthinginternal.a")
if(WIN32)
# add additional libraries for Windows (not sure if go build could provide this list somehow to make this more generic)
list(APPEND PRIVATE_LIBRARIES -lws2_32 -lwinmm)
endif()
# find c++utilities
find_package(c++utilities 4.9.0 REQUIRED)