Skip to content

Commit

Permalink
Improve go port support and add testing with cmake.
Browse files Browse the repository at this point in the history
  • Loading branch information
viferga committed Mar 27, 2023
1 parent 1b8cd13 commit a1a079f
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 16 deletions.
1 change: 1 addition & 0 deletions source/ports/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ option(OPTION_BUILD_PORTS_TS "Build TypeScript port." OFF)
add_subdirectory(cxx_port)
add_subdirectory(node_port)
add_subdirectory(py_port)
add_subdirectory(go_port)
add_subdirectory(rs_port)

#
Expand Down
38 changes: 34 additions & 4 deletions source/ports/go_port/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,39 @@
# Check if port is enabled
if(NOT OPTION_BUILD_PORTS OR NOT OPTION_BUILD_PORTS_GO)
return()
endif()

include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/GoLang.cmake)

# add_go_library(go_port SHARED
# source/go_port.go
# )
#
# Port name and options
#

# Target name
set(target go_port)

# Exit here if required dependencies are not met
message(STATUS "Port ${target}")

add_go_executable(go_port
add_go_library(${target} SHARED
source/await.go
source/pointer.go
source/go_port.go
)

#
# Define test
#

add_test(NAME ${target}
COMMAND go test
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/source
)

#
# Define test labels
#

set_property(TEST ${target}
PROPERTY LABELS ${target}
)
14 changes: 8 additions & 6 deletions source/ports/go_port/cmake/CMakeDetermineGoCompiler.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ if(NOT CMAKE_Go_COMPILER)
$ENV{GO_COMPILER}
/usr/bin
/usr/local/bin
)
)

if(CMAKE_Go_COMPILER_INIT)
set(CMAKE_Go_COMPILER ${CMAKE_Go_COMPILER_INIT} CACHE PATH "Go Compiler")
Expand All @@ -28,17 +28,19 @@ if(NOT CMAKE_Go_COMPILER)
NAMES go
PATHS ${Go_BIN_PATH}
)
EXEC_PROGRAM(${CMAKE_Go_COMPILER} ARGS version OUTPUT_VARIABLE GOLANG_VERSION)
STRING(REGEX MATCH "go[0-9]+.[0-9]+.[0-9]+[ /A-Za-z0-9]*" VERSION "${GOLANG_VERSION}")
exec_program(${CMAKE_Go_COMPILER} ARGS version OUTPUT_VARIABLE GOLANG_VERSION)
string(REGEX MATCH "go[0-9]+.[0-9]+.[0-9]+[ /A-Za-z0-9]*" VERSION "${GOLANG_VERSION}")
message("-- The Golang compiler identification is ${VERSION}")
message("-- Check for working Golang compiler: ${CMAKE_Go_COMPILER}")
endif()

endif()

mark_as_advanced(CMAKE_Go_COMPILER)

configure_file(${CMAKE_CURRENT_SOURCE_DIR}/cmake/CMakeGoCompiler.cmake.in
${CMAKE_PLATFORM_INFO_DIR}/CMakeGoCompiler.cmake @ONLY)
configure_file(
${CMAKE_CURRENT_SOURCE_DIR}/cmake/CMakeGoCompiler.cmake.in
${CMAKE_PLATFORM_INFO_DIR}/CMakeGoCompiler.cmake
@ONLY
)

set(CMAKE_Go_COMPILER_ENV_VAR "GO_COMPILER")
18 changes: 12 additions & 6 deletions source/ports/go_port/cmake/GoLang.cmake
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
set(GOPATH "${CMAKE_CURRENT_BINARY_DIR}/go")
file(MAKE_DIRECTORY ${GOPATH})

include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/CMakeDetermineGoCompiler.cmake)

function(ExternalGoProject_Add TARG)
add_custom_target(${TARG} env GOPATH=${GOPATH} ${CMAKE_Go_COMPILER} get ${ARGN})
add_custom_target(${TARG} ${CMAKE_COMMAND} -E env GOPATH=${GOPATH} ${CMAKE_Go_COMPILER} get ${ARGN})
endfunction(ExternalGoProject_Add)

function(add_go_executable NAME)
add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/.timestamp
COMMAND env GOPATH=${GOPATH} ${CMAKE_Go_COMPILER} build
COMMAND ${CMAKE_COMMAND} -E env GOPATH=${GOPATH} ${CMAKE_Go_COMPILER} build
-o "${PROJECT_OUTPUT_DIR}/${NAME}"
${CMAKE_GO_FLAGS} ${ARGN}
WORKING_DIRECTORY ${CMAKE_CURRENT_LIST_DIR})
Expand All @@ -18,19 +20,23 @@ endfunction(add_go_executable)

function(add_go_library NAME BUILD_TYPE)
if(BUILD_TYPE STREQUAL "STATIC")
set(BUILD_MODE -buildmode=c-archive)
set(LIB_NAME "lib${NAME}.a")
if(WIN32)
set(LIB_NAME "lib${NAME}.lib")
else()
set(LIB_NAME "lib${NAME}.a")
endif()
else()
set(BUILD_MODE -buildmode=c-shared)
if(APPLE)
set(LIB_NAME "lib${NAME}.dylib")
elseif(WIN32)
set(LIB_NAME "lib${NAME}.dll")
else()
set(LIB_NAME "lib${NAME}.so")
endif()
endif()

add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/.timestamp
COMMAND env GOPATH=${GOPATH} ${CMAKE_Go_COMPILER} build ${BUILD_MODE}
COMMAND ${CMAKE_COMMAND} -E env GOPATH=${GOPATH} ${CMAKE_Go_COMPILER} build ${BUILD_MODE}
-o "${PROJECT_OUTPUT_DIR}/${LIB_NAME}"
${CMAKE_GO_FLAGS} ${ARGN}
WORKING_DIRECTORY ${CMAKE_CURRENT_LIST_DIR})
Expand Down

0 comments on commit a1a079f

Please sign in to comment.