blob: d31914d907e704d4c9c0c410e5e1b6f275e11367 [file] [log] [blame]
# Copyright (C) 2016 The Android Open Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
if(NOT CMAKE_Go_COMPILER)
message(STATUS "Looking for a Go compiler")
set(paths)
set(flags)
if(PREBUILTS)
if(CMAKE_HOST_APPLE)
set(paths ${PREBUILTS}/go/darwin-x86/bin)
set(flags NO_DEFAULT_PATH)
elseif(CMAKE_HOST_WIN32)
set(paths ${PREBUILTS}/go/windows-x86/bin)
set(flags NO_DEFAULT_PATH)
elseif(CMAKE_HOST_UNIX)
set(paths ${PREBUILTS}/go/linux-x86/bin)
set(flags NO_DEFAULT_PATH)
endif()
endif()
find_program(CMAKE_Go_COMPILER go PATHS ${paths} DOC "The go compiler" ${flags})
message(STATUS "Looking for a Go compiler - ${CMAKE_Go_COMPILER}")
if(NOT CMAKE_Go_COMPILER)
message(STATUS "Failed to find Go compiler")
return()
endif()
mark_as_advanced(CMAKE_Go_COMPILER)
endif()
set(GO_ROOT $ENV{GOROOT})
if(GO_ROOT)
string(REPLACE "\\" "/" GO_ROOT "${GO_ROOT}")
else()
# set the go root from where we found the go binary
get_filename_component(GO_ROOT "${CMAKE_Go_COMPILER}" DIRECTORY)
get_filename_component(GO_ROOT "${GO_ROOT}/.." ABSOLUTE)
endif()
set(ENV{GOROOT} ${GO_ROOT})
# Find the go source root
set(go_path $ENV{GOPATH})
string(REPLACE "\\" "/" go_path "${go_path}")
if(go_base)
list(GET go_path 0 go_base)
else()
set(go_base ${ROOT_DIR})
endif()
if(NOT go_base)
message(FATAL_ERROR "Could not determine GOPATH")
endif()
set(GO_PATH ${go_base} CACHE PATH "The path below which the go code sits")
unset(go_path)
unset(go_base)
# Prepare our basic settings
get_filename_component(GO_SRC ${GO_PATH}/src ABSOLUTE)
get_filename_component(GO_PKG ${CMAKE_BINARY_DIR}/go/pkg ABSOLUTE)
get_filename_component(GO_DEPS ${CMAKE_BINARY_DIR}/go/deps ABSOLUTE)
get_filename_component(GO_TAGS ${CMAKE_BINARY_DIR}/go/tags ABSOLUTE)
get_filename_component(GO_BIN ${CMAKE_BINARY_DIR}/bin ABSOLUTE)
set(GO_COMPILE ${CMAKE_CURRENT_LIST_DIR}/GoCompile.cmake)
set(GO_BUILD_DEPS ${CMAKE_CURRENT_LIST_DIR}/GoDeps.cmake)
# Build the go environment variables
string(TOLOWER ${CMAKE_SYSTEM_NAME} GO_OS)
set(GO_ARCH amd64)
set(GO_ENV ${CMAKE_BINARY_DIR}/go/goenv.cmake)
file(WRITE ${GO_ENV} "
set(ENV{GOBIN} ${GO_BIN})
set(ENV{GOROOT} ${GO_ROOT})
set(ENV{GOPATH} ${GO_PATH})
set(ENV{GOOS} ${GO_OS})
set(ENV{GOARCH} ${GO_ARCH})
set(CMAKE_Go_COMPILER ${CMAKE_Go_COMPILER})
set(GO_PKG ${GO_PKG})
")
# Find out which version of go we have
include(${GO_ENV})
execute_process(COMMAND ${CMAKE_Go_COMPILER} version OUTPUT_VARIABLE go_version_output)
string(REGEX REPLACE "^.*go([0-9]+\\.[0-9]+)[^0-9].*$" "\\1" GO_VERSION ${go_version_output})
unset(go_version_output)
if(GO_VERSION VERSION_LESS 1.5)
message(FATAL_ERROR "Go is too old, at least 1.5 needed")
return()
elseif(GO_VERSION VERSION_LESS 1.6)
message(STATUS "Older go version, enabling vendor experiment")
file(APPEND ${GO_ENV} "
set(ENV{GO15VENDOREXPERIMENT} 1)
")
# re-include with modified settings
include(${GO_ENV})
endif()
# configure variables set in this file for fast reload later on
configure_file(${CMAKE_CURRENT_LIST_DIR}/CMakeGoCompiler.cmake.in
${CMAKE_PLATFORM_INFO_DIR}/CMakeGoCompiler.cmake
@ONLY
)
set(CMAKE_Go_COMPILER_ENV_VAR "GO_COMPILER")