Windows编译Wireguard,修改后的Makefile文件

由于WireGuard官方的Makefile文件使用了很多Linux上的命令,Windows在不使用WSL和Cygwin的情况下无法直接执行(且直接使用Cygwin也会因无法获取正确的系统版本而无法下载到正确的Go的安装包),因此在参考了这个回答之后,经过反复的调试和修改,把Win10可用的Makefile和CMakeLists.txt文件分享给大家。
使用时,只需要修改GIT_BIN_PATHDESIRED_GO_VERSIONMAKE_PATH三个变量即可。
有三点注意事项:

  1. 项目路径中不能有空格!否则CURDIR的路径会被截断;
  2. 无需安装Cygwin!只需有Git、NDK即可,因为Git中包含了所需的unzip.exe、patch.exe、touch.exe;
  3. 在Windows下执行del /f时,路径分隔符建议用\而不是/,否则会报错
tunnel/tools/libwg-go/Makefile
# SPDX-License-Identifier: Apache-2.0
#
# Copyright © 2017-2019 WireGuard LLC. All Rights Reserved.
BUILDDIR ?= $(CURDIR)/build
DESTDIR ?= $(CURDIR)/out

NDK_GO_ARCH_MAP_x86 := 386
NDK_GO_ARCH_MAP_x86_64 := amd64
NDK_GO_ARCH_MAP_arm := arm
NDK_GO_ARCH_MAP_arm64 := arm64
NDK_GO_ARCH_MAP_mips := mipsx
NDK_GO_ARCH_MAP_mips64 := mips64x

CLANG_FLAGS := --target=$(ANDROID_LLVM_TRIPLE) --gcc-toolchain=$(ANDROID_TOOLCHAIN_ROOT) --sysroot=$(ANDROID_SYSROOT)
export CGO_CFLAGS := $(CLANG_FLAGS) $(CFLAGS)
export CGO_LDFLAGS := $(CLANG_FLAGS) $(LDFLAGS) -Wl,-soname=libwg-go.so
export CC := $(ANDROID_C_COMPILER)
export GOARCH := $(NDK_GO_ARCH_MAP_$(ANDROID_ARCH_NAME))
export GOOS := android
export CGO_ENABLED := 1

default: $(DESTDIR)/libwg-go.so

GIT_BIN_PATH := "D:\Program Files\Git\usr\bin"
UNZIP_PATH := $(GIT_BIN_PATH)\unzip.exe
PATCH_PATH := $(GIT_BIN_PATH)\patch.exe
TOUCH_PATH := $(GIT_BIN_PATH)\touch.exe
DESIRED_GO_VERSION := 1.16.3
$(BUILDDIR)/go-$(DESIRED_GO_VERSION)/.prepared:
    if not exist "$(dir $@)" mkdir "$(dir $@)"
    curl -o "$(BUILDDIR)\go-$(DESIRED_GO_VERSION)\gofile.zip" "https://dl.google.com/go/go$(DESIRED_GO_VERSION).windows-amd64.zip"
    $(UNZIP_PATH) -o "$(BUILDDIR)\go-$(DESIRED_GO_VERSION)\gofile.zip" -d "$(dir $@)"
    del /f "$(BUILDDIR)\go-$(DESIRED_GO_VERSION)\gofile.zip"

    $(PATCH_PATH) -p1 -f -N -r- -d "$(dir $@)go/" < goruntime-boottime-over-monotonic.diff
    $(TOUCH_PATH) "$@"

$(DESTDIR)/libwg-go.so: export PATH := $(BUILDDIR)/go-$(DESIRED_GO_VERSION)/go/bin/:$(PATH)
$(DESTDIR)/libwg-go.so: $(BUILDDIR)/go-$(DESIRED_GO_VERSION)/.prepared go.mod
    go build -tags linux -ldflags="-X golang.zx2c4.com/wireguard/ipc.socketDirectory=/data/data/$(ANDROID_PACKAGE_NAME)/cache/wireguard" -v -trimpath -o "$@" -buildmode c-shared

.DELETE_ON_ERROR:
/tunnel/tools/CMakeLists.txt
# SPDX-License-Identifier: Apache-2.0
#
# Copyright © 2018-2019 WireGuard LLC. All Rights Reserved.

cmake_minimum_required(VERSION 3.4.1)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_LIBRARY_OUTPUT_DIRECTORY}")

# Work around https://github.com/android-ndk/ndk/issues/602
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fuse-ld=gold")


file(GLOB WG_SOURCES wireguard-tools/src/*.c ndk-compat/compat.c)
add_executable(libwg.so ${WG_SOURCES})
target_include_directories(libwg.so PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/wireguard-tools/src/uapi/linux/" "${CMAKE_CURRENT_SOURCE_DIR}/wireguard-tools/src/")
target_compile_options(libwg.so PUBLIC -O3 -std=gnu11 -D_GNU_SOURCE -include ${CMAKE_CURRENT_SOURCE_DIR}/ndk-compat/compat.h -DHAVE_VISIBILITY_HIDDEN -DRUNSTATEDIR=\"/data/data/${ANDROID_PACKAGE_NAME}/cache\")

get_filename_component(destdirparent "${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/.." ABSOLUTE)
set(MAKE_PATH "D:/AndroidSDK/ndk/21.1.6352462/prebuilt/windows-x86_64/bin/make.exe")
# referred to the variable on the last line of this statment
add_custom_target(libwg-go.so WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/libwg-go" COMMENT "Building wireguard-go" VERBATIM COMMAND ${MAKE_PATH}
        ANDROID_ARCH_NAME=${ANDROID_ARCH_NAME}
        ANDROID_C_COMPILER=${ANDROID_C_COMPILER}
        ANDROID_TOOLCHAIN_ROOT=${ANDROID_TOOLCHAIN_ROOT}
        ANDROID_LLVM_TRIPLE=${ANDROID_LLVM_TRIPLE}
        ANDROID_SYSROOT=${ANDROID_SYSROOT}
        ANDROID_PACKAGE_NAME=${ANDROID_PACKAGE_NAME}
        CFLAGS=${CMAKE_C_FLAGS}\ -Wno-unused-command-line-argument
        LDFLAGS=${CMAKE_SHARED_LINKER_FLAGS}\ -fuse-ld=gold
        DESTDIR=${CMAKE_LIBRARY_OUTPUT_DIRECTORY}
        BUILDDIR=${destdirparent}/generated-src
        )

# Hack to make it actually build as part of the default target
add_dependencies(libwg.so libwg-go.so)

你可能感兴趣的:(Windows编译Wireguard,修改后的Makefile文件)