| 1 |
# |
| 2 |
# Licensed to the Apache Software Foundation (ASF) under one |
| 3 |
# or more contributor license agreements. See the NOTICE file |
| 4 |
# distributed with this work for additional information |
| 5 |
# regarding copyright ownership. The ASF licenses this file |
| 6 |
# to you under the Apache License, Version 2.0 (the |
| 7 |
# "License"); you may not use this file except in compliance |
| 8 |
# with the License. You may obtain a copy of the License at |
| 9 |
# |
| 10 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 11 |
# |
| 12 |
# Unless required by applicable law or agreed to in writing, |
| 13 |
# software distributed under the License is distributed on an |
| 14 |
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 |
# KIND, either express or implied. See the License for the |
| 16 |
# specific language governing permissions and limitations |
| 17 |
# under the License. |
| 18 |
# |
| 19 |
|
| 20 |
# Go version |
| 21 |
execute_process(COMMAND ${GO_EXE} version OUTPUT_VARIABLE go_ver OUTPUT_STRIP_TRAILING_WHITESPACE) |
| 22 |
string(REGEX MATCH "go[0-9]+.[0-9]+[0-9]*" GO_VERSION "${go_ver}") |
| 23 |
string(SUBSTRING ${GO_VERSION} 2 -1 GOLESS_VERSION) |
| 24 |
message(STATUS "Found Go: ${GO_EXE} (${go_ver}) (${GOLESS_VERSION}).") |
| 25 |
|
| 26 |
if (GOLESS_VERSION VERSION_LESS 1.11) |
| 27 |
set(BUILD_GO "OFF") |
| 28 |
message(STATUS "Go: ${GO_EXE} (${GOLESS_VERSION}) version to low. At least 1.11 required.") |
| 29 |
endif() |
| 30 |
|
| 31 |
if (BUILD_GO) |
| 32 |
# NOTE: go test -race flag is not included by default, it causes problems on several platforms: |
| 33 |
# - ubuntu up to trust: link errors |
| 34 |
# - ubuntu from xenial: requires extra package golang-race-detector-runtime |
| 35 |
# - fedora with gccgo: complains about "import cycles" |
| 36 |
# (Works well on fedora with original go) |
| 37 |
# Enable manually with -DGO_TEST_FLAGS="-v -race" |
| 38 |
|
| 39 |
set(GO_BUILD_FLAGS "" CACHE STRING "Flags for 'go build'") |
| 40 |
set(GO_VET_FLAGS "-v" CACHE STRING "Flags for 'go test'") |
| 41 |
set(GO_TEST_FLAGS "-v" CACHE STRING "Flags for 'go test'") |
| 42 |
|
| 43 |
# Flags that differ for golang go and gcc go. |
| 44 |
if (go_ver MATCHES "gccgo") |
| 45 |
set(GO_RPATH_FLAGS -gccgoflags "-Wl,-rpath=${PN_C_LIBRARY_DIR}") |
| 46 |
else() |
| 47 |
set(GO_RPATH_FLAGS -ldflags "-r ${PN_C_LIBRARY_DIR}") |
| 48 |
endif() |
| 49 |
|
| 50 |
separate_arguments(GO_BUILD_FLAGS) |
| 51 |
separate_arguments(GO_TEST_FLAGS) |
| 52 |
|
| 53 |
# Create a Go tree in the binary directory, link pkg to the source directory, link go.mod file |
| 54 |
set(GOPATH ${CMAKE_CURRENT_BINARY_DIR}) |
| 55 |
add_custom_target(go-pkg-link ALL |
| 56 |
COMMAND ${CMAKE_COMMAND} -E create_symlink ${CMAKE_CURRENT_SOURCE_DIR}/pkg ${GOPATH}/pkg) |
| 57 |
add_custom_target(go-mod-link ALL |
| 58 |
COMMAND ${CMAKE_COMMAND} -E create_symlink ${CMAKE_CURRENT_SOURCE_DIR}/../go.mod ${GOPATH}/../go.mod) |
| 59 |
|
| 60 |
# Following are CACHE INTERNAL so examples/CMakeLists.txt can see them. |
| 61 |
set(GO_ENV |
| 62 |
"GOPATH=${GOPATH}" |
| 63 |
"CGO_CFLAGS=-I${PN_C_INCLUDE_DIR}" |
| 64 |
"CGO_LDFLAGS=-L${PN_C_LIBRARY_DIR}" |
| 65 |
"PN_INTEROP_DIR=${CMAKE_SOURCE_DIR}/tests/interop" |
| 66 |
"SASLPASSWD=${CyrusSASL_Saslpasswd_EXECUTABLE}" |
| 67 |
CACHE INTERNAL "Run a command with Go environment variables") |
| 68 |
|
| 69 |
set(GO ${PN_ENV_SCRIPT} -- ${GO_ENV} ${GO_EXE} CACHE INTERNAL "Run go with environment set") |
| 70 |
|
| 71 |
set(GO_BUILD ${GO} build ${GO_BUILD_FLAGS} ${GO_RPATH_FLAGS} CACHE INTERNAL "Run go build") |
| 72 |
set(GO_INSTALL ${GO} install ${GO_BUILD_FLAGS} CACHE INTERNAL "Run go install" ) |
| 73 |
set(GO_TEST ${GO} test ${GO_BUILD_FLAGS} ${GO_RPATH_FLAGS} ${GO_TEST_FLAGS} CACHE INTERNAL "Run go test") |
| 74 |
|
| 75 |
# The go build tools handle dependency checks and incremental builds better than |
| 76 |
# CMake so just run them every time, they do nothing if nothing needs to be |
| 77 |
# done. |
| 78 |
add_custom_target(go-build ALL |
| 79 |
COMMAND ${GO_INSTALL} ${CMAKE_CURRENT_BINARY_DIR}/pkg/... |
| 80 |
DEPENDS qpid-proton-core go-pkg-link go-mod-link |
| 81 |
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}) |
| 82 |
|
| 83 |
add_test( |
| 84 |
NAME go-test |
| 85 |
COMMAND ${GO_TEST} ${CMAKE_CURRENT_BINARY_DIR}/pkg/... |
| 86 |
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}) |
| 87 |
|
| 88 |
# Clean up go output directories. |
| 89 |
list(APPEND ADDITIONAL_MAKE_CLEAN_FILES ${GOPATH}/pkg ${GOPATH}/bin) |
| 90 |
|
| 91 |
if (BUILD_EXAMPLES) |
| 92 |
add_subdirectory(examples) |
| 93 |
endif (BUILD_EXAMPLES) |
| 94 |
|
| 95 |
# Install go sources. |
| 96 |
set (GO_INSTALL_DIR ${SHARE_INSTALL_DIR}/gocode/pkg CACHE PATH "Installation directory for Go code") |
| 97 |
mark_as_advanced (GO_INSTALL_DIR) |
| 98 |
|
| 99 |
install(DIRECTORY pkg DESTINATION ${GO_INSTALL_DIR} COMPONENT Go) |
| 100 |
install(DIRECTORY examples/ |
| 101 |
DESTINATION "${PROTON_SHARE}/examples/go" |
| 102 |
COMPONENT Go |
| 103 |
PATTERN "CMakeLists.txt" EXCLUDE) |
| 104 |
endif() |