mirror of
https://github.com/zerotier/ZeroTierOne.git
synced 2025-06-06 12:33:44 +02:00
compile go code to static library & call via main.cpp
This allows CMake to manage the linkage of C/C++ libraries rather than an exponentially growing list of cgo directives based on build options
This commit is contained in:
parent
2272189955
commit
c8f640f3f2
4 changed files with 65 additions and 23 deletions
|
@ -56,6 +56,7 @@ if(WIN32)
|
||||||
else(WIN32)
|
else(WIN32)
|
||||||
if(APPLE)
|
if(APPLE)
|
||||||
message("++ Setting MacOS Compiler Flags ${CMAKE_BUILD_TYPE}")
|
message("++ Setting MacOS Compiler Flags ${CMAKE_BUILD_TYPE}")
|
||||||
|
|
||||||
add_compile_options(
|
add_compile_options(
|
||||||
-Wall
|
-Wall
|
||||||
-Wno-deprecated
|
-Wno-deprecated
|
||||||
|
@ -113,18 +114,35 @@ add_subdirectory(controller)
|
||||||
add_subdirectory(osdep)
|
add_subdirectory(osdep)
|
||||||
add_subdirectory(serviceiocore)
|
add_subdirectory(serviceiocore)
|
||||||
|
|
||||||
set(
|
file(GLOB go_src
|
||||||
zt_core
|
${CMAKE_SOURCE_DIR}/cmd/*.go
|
||||||
zt_osdep
|
${CMAKE_SOURCE_DIR}/cmd/cmd/*.go
|
||||||
zt_controller
|
${CMAKE_SOURCE_DIR}/pkg/zerotier/*.go)
|
||||||
zt_service_io_core
|
|
||||||
|
add_custom_command(
|
||||||
|
OUTPUT ${CMAKE_BINARY_DIR}/zerotier_cgo.h ${CMAKE_BINARY_DIR}/zerotier_cgo.a
|
||||||
|
COMMAND go build -buildmode=c-archive -o ${CMAKE_BINARY_DIR}/zerotier_cgo.a ${CMAKE_SOURCE_DIR}/cmd/zerotier/zerotier.go
|
||||||
|
IMPLICIT_DEPENDS go ${go_src}
|
||||||
|
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_custom_target(zerotier ALL
|
add_executable(zerotier main.cpp)
|
||||||
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
|
target_include_directories(zerotier PUBLIC ${CMAKE_BINARY_DIR})
|
||||||
COMMAND rm -f ./build/zerotier && go build ${GO_BUILD_TAGS} -trimpath -ldflags -s -buildmode=pie -o ./build/zerotier cmd/zerotier/zerotier.go
|
add_dependencies(zerotier zerotier_cgo zt_osdep zt_core zt_controller zt_service_io_core)
|
||||||
BYPRODUCTS zerotier
|
target_link_libraries(zerotier zerotier_cgo zt_osdep zt_core zt_controller zt_service_io_core)
|
||||||
)
|
if (APPLE)
|
||||||
add_dependencies(zerotier zt_osdep zt_core zt_controller zt_service_io_core)
|
target_link_libraries(zerotier "-framework CoreFoundation" "-framework Security")
|
||||||
|
endif (APPLE)
|
||||||
set(ADDITIONAL_MAKE_CLEAN_FILES zerotier)
|
|
||||||
|
|
|
@ -13,6 +13,8 @@
|
||||||
|
|
||||||
package main
|
package main
|
||||||
|
|
||||||
|
import "C"
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
@ -28,15 +30,15 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
func getAuthTokenPaths(basePath string) (p []string) {
|
func getAuthTokenPaths(basePath string) (p []string) {
|
||||||
p = append(p,path.Join(basePath,"authtoken.secret"))
|
p = append(p, path.Join(basePath, "authtoken.secret"))
|
||||||
userHome, _ := os.UserHomeDir()
|
userHome, _ := os.UserHomeDir()
|
||||||
if len(userHome) > 0 {
|
if len(userHome) > 0 {
|
||||||
if runtime.GOOS == "darwin" {
|
if runtime.GOOS == "darwin" {
|
||||||
p = append(p,path.Join(userHome,"Library","Application Support","ZeroTier","authtoken.secret"))
|
p = append(p, path.Join(userHome, "Library", "Application Support", "ZeroTier", "authtoken.secret"))
|
||||||
p = append(p,path.Join(userHome,"Library","Application Support","ZeroTier","One","authtoken.secret"))
|
p = append(p, path.Join(userHome, "Library", "Application Support", "ZeroTier", "One", "authtoken.secret"))
|
||||||
}
|
}
|
||||||
p = append(p,path.Join(userHome,".zerotierauth"))
|
p = append(p, path.Join(userHome, ".zerotierauth"))
|
||||||
p = append(p,path.Join(userHome,".zeroTierOneAuthToken"))
|
p = append(p, path.Join(userHome, ".zeroTierOneAuthToken"))
|
||||||
}
|
}
|
||||||
return p
|
return p
|
||||||
}
|
}
|
||||||
|
@ -45,10 +47,15 @@ func authTokenRequired(authToken string) {
|
||||||
if len(authToken) == 0 {
|
if len(authToken) == 0 {
|
||||||
fmt.Println("FATAL: unable to read API authorization token from command line or any filesystem location.")
|
fmt.Println("FATAL: unable to read API authorization token from command line or any filesystem location.")
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
}
|
||||||
|
|
||||||
|
//export ZeroTierMain
|
||||||
|
func ZeroTierMain() {
|
||||||
// Reduce Go's thread and memory footprint. This would slow things down if the Go code
|
// 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
|
// 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.
|
// in pushing a lot of packets around. If that ever changes this should be adjusted.
|
||||||
|
@ -99,7 +106,7 @@ func main() {
|
||||||
tmp, _ := ioutil.ReadFile(p)
|
tmp, _ := ioutil.ReadFile(p)
|
||||||
if len(tmp) > 0 {
|
if len(tmp) > 0 {
|
||||||
authToken = string(tmp)
|
authToken = string(tmp)
|
||||||
break;
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if len(authToken) == 0 {
|
if len(authToken) == 0 {
|
||||||
|
|
19
main.cpp
Normal file
19
main.cpp
Normal file
|
@ -0,0 +1,19 @@
|
||||||
|
/*
|
||||||
|
* 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.
|
||||||
|
*/
|
||||||
|
/****/
|
||||||
|
|
||||||
|
#include "zerotier_cgo.h"
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
ZeroTierMain();
|
||||||
|
return 0;
|
||||||
|
}
|
|
@ -14,10 +14,8 @@
|
||||||
package zerotier
|
package zerotier
|
||||||
|
|
||||||
// #cgo CFLAGS: -O3
|
// #cgo CFLAGS: -O3
|
||||||
// #cgo darwin,!central LDFLAGS: ${SRCDIR}/../../build/serviceiocore/libzt_service_io_core.a ${SRCDIR}/../../build/core/libzt_core.a ${SRCDIR}/../../build/osdep/libzt_osdep.a ${SRCDIR}/../../build/controller/libzt_controller.a -lc++ -lpthread
|
// #cgo darwin LDFLAGS: -Wl,-undefined -Wl,dynamic_lookup
|
||||||
// #cgo darwin,central LDFLAGS: ${SRCDIR}/../../build/serviceiocore/libzt_service_io_core.a ${SRCDIR}/../../build/core/libzt_core.a ${SRCDIR}/../../build/osdep/libzt_osdep.a ${SRCDIR}/../../build/controller/libzt_controller.a ${SRCDIR}/../../build/lib/libredis++.a /usr/local/lib/libhiredis.a /usr/local/opt/libpq/lib/libpq.a -lc++ -lpthread
|
// #cgo !darwin LDFLAGS: -Wl,-unresolved-symbols=ignore-all
|
||||||
// #cgo linux,!central android,!central LDFLAGS: ${SRCDIR}/../../build/serviceiocore/libzt_service_io_core.a ${SRCDIR}/../../build/core/libzt_core.a ${SRCDIR}/../../build/osdep/libzt_osdep.a ${SRCDIR}/../../build/controller/libzt_controller.a -lstdc++ -lpthread -lm
|
|
||||||
// #cgo linux,central android,central LDFLAGS: ${SRCDIR}/../../build/serviceiocore/libzt_service_io_core.a ${SRCDIR}/../../build/core/libzt_core.a ${SRCDIR}/../../build/osdep/libzt_osdep.a ${SRCDIR}/../../build/controller/libzt_controller.a ${SRCDIR}/../../build/lib/libredis++.a -lhiredis -lpq -lstdc++ -lpthread -lm
|
|
||||||
// #include "../../serviceiocore/GoGlue.h"
|
// #include "../../serviceiocore/GoGlue.h"
|
||||||
import "C"
|
import "C"
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue