back to go main

This commit is contained in:
Grant Limberg 2020-06-15 13:51:24 -07:00
parent 69f1c8701b
commit 1a38dfdbde
No known key found for this signature in database
GPG key ID: 2BA62CCABBB4095A
3 changed files with 12 additions and 84 deletions

View file

@ -62,6 +62,11 @@ if(CMAKE_BUILD_TYPE STREQUAL "Debug")
add_definitions(-DZT_DEBUG)
endif(CMAKE_BUILD_TYPE STREQUAL "Debug")
set(GOFLAGS
-trimpath
-buildmode=pie
)
if(WIN32)
message("++ Setting Windows Compiler Flags ${CMAKE_BUILD_TYPE}")
add_definitions(-DNOMINMAX)
@ -121,6 +126,7 @@ else(WIN32)
)
set(CMAKE_EXE_LINKER_FLAGS "-static ${CMAKE_EXE_LINKER_FLAGS}")
set(GOFLAGS
${GOFLAGS}
-a
-tags netgo
-ldflags '-w -extldflags \"-static\"')
@ -152,34 +158,11 @@ file(GLOB go_src
${CMAKE_SOURCE_DIR}/cmd/cmd/*.go
${CMAKE_SOURCE_DIR}/pkg/zerotier/*.go)
add_custom_command(
OUTPUT ${CMAKE_BINARY_DIR}/zerotier_cgo.h ${CMAKE_BINARY_DIR}/zerotier_cgo.a
COMMAND ${GOARCH} CGO_ENABLED=1 ${GO} build -buildmode=c-archive ${GOFLAGS} -o ${CMAKE_BINARY_DIR}/zerotier_cgo.a ${CMAKE_SOURCE_DIR}/cmd/zerotier/zerotier.go
IMPLICIT_DEPENDS ${go_src}
add_custom_target(
zerotier ALL
BYPRODUCTS ${CMAKE_BINARY_DIR}/zerotier
SOURCES ${go_src}
COMMAND ${GOARCH} CGO_ENABLED=1 CGO_LDFLAGS=\"$<TARGET_FILE:zt_osdep> $<TARGET_FILE:zt_core> $<TARGET_FILE:zt_controller> $<TARGET_FILE:zt_service_io_core>\" ${GO} build ${GOFLAGS} -o ${CMAKE_BINARY_DIR}/zerotier ${CMAKE_SOURCE_DIR}/cmd/zerotier/zerotier.go
COMMENT "Compiling Go Code..."
)
add_custom_target(
zerotier_cgo_target
DEPENDS ${CMAKE_BINARY_DIR}/zerotier_cgo.a
SOURCES ${go_src}
)
add_library(zerotier_cgo STATIC IMPORTED GLOBAL)
add_dependencies(zerotier_cgo zerotier_cgo_target)
set_target_properties(
zerotier_cgo
PROPERTIES
IMPORTED_LOCATION ${CMAKE_BINARY_DIR}/zerotier_cgo.a
INTERFACE_INCLUDE_DIRECTORIES ${CMAKE_BINARY_DIR}
)
add_executable(zerotier main.cpp)
target_include_directories(zerotier PUBLIC ${CMAKE_BINARY_DIR})
add_dependencies(zerotier zerotier_cgo zt_osdep zt_core zt_controller zt_service_io_core)
target_link_libraries(zerotier zerotier_cgo zt_service_io_core zt_core zt_osdep zt_controller )
if (APPLE)
target_link_libraries(zerotier "-framework CoreFoundation" "-framework Security")
else(APPLE)
if ("${CMAKE_SYSTEM}" MATCHES "Linux")
target_link_libraries(zerotier "-lpthread" "-lm")
endif ("${CMAKE_SYSTEM}" MATCHES "Linux")
endif (APPLE)
add_dependencies(zerotier zt_osdep zt_core zt_controller zt_service_io_core)

View file

@ -52,10 +52,6 @@ func authTokenRequired(authToken string) {
}
func main() {
}
//export ZeroTierMain
func ZeroTierMain() {
// Reduce Go's thread and memory footprint. This would slow things down if the Go code
// were doing a lot, but it's not. It just manages the core and is not directly involved
// in pushing a lot of packets around. If that ever changes this should be adjusted.

View file

@ -1,51 +0,0 @@
/*
* Copyright (c)2013-2020 ZeroTier, Inc.
*
* Use of this software is governed by the Business Source License included
* in the LICENSE.TXT file in the project's root directory.
*
* Change Date: 2024-01-01
*
* On the date above, in accordance with the Business Source License, use
* of this software will be governed by version 2.0 of the Apache License.
*/
/****/
#if !defined(_WIN32) && !defined(_WIN64)
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#endif
#include "zerotier_cgo.h"
int main(int argc,char **argv)
{
// Fork into background if run with 'service -d'. This is best done prior
// to launching the Go code, since Go likes to start thread pools and stuff
// that don't play nice with fork. This is obviously unsupported on Windows.
#if !defined(_WIN32) && !defined(_WIN64)
for(int i=1;i<argc;) {
if (strcmp(argv[i++], "service") == 0) {
for(;i<argc;) {
if (strcmp(argv[i++], "-d") == 0) {
long p = (long)fork();
if (p < 0) {
fprintf(stderr,"FATAL: fork() failed!\n");
return -1;
} else if (p > 0) {
return 0;
}
break;
}
}
break;
}
}
#endif
ZeroTierMain();
return 0;
}