diff options
| author | StevenWdV <stevenwdv@gmail.com> | 2022-01-24 14:59:25 +0100 |
|---|---|---|
| committer | StevenWdV <stevenwdv@gmail.com> | 2022-01-24 16:24:57 +0100 |
| commit | e544c6fa9e15e7277da79e2464243e90b2706b8c (patch) | |
| tree | de6613747e0e34a799089d4677f9833a85748712 /exports | |
| parent | aab2e4b966c82b67eb0e204060e5ea6cd4ea15cf (diff) | |
Cleanup
Added variables to Makefiles to specify custom exports/ directory;
Split exception classes in Java & C#;
Added more comments;
Renamed library and Go package;
Removed real (pure) tests;
Added generate_lib.ps1 to generate import .lib for Windows (Swift);
Moved built Go libraries to exports/lib/;
Switch to hopefully faster Swift GitHub Action.
Diffstat (limited to 'exports')
| -rw-r--r-- | exports/.gitignore | 2 | ||||
| -rw-r--r-- | exports/Makefile | 21 | ||||
| -rw-r--r-- | exports/exports.go | 13 | ||||
| -rw-r--r-- | exports/generate_lib.ps1 | 30 | ||||
| -rw-r--r-- | exports/platform.mk | 13 |
5 files changed, 63 insertions, 16 deletions
diff --git a/exports/.gitignore b/exports/.gitignore index 63ea916..5e5615b 100644 --- a/exports/.gitignore +++ b/exports/.gitignore @@ -1 +1 @@ -/*/ +/lib/* diff --git a/exports/Makefile b/exports/Makefile index 547ee92..67ddc61 100644 --- a/exports/Makefile +++ b/exports/Makefile @@ -2,15 +2,20 @@ include platform.mk -# Creates targets like 'linux/amd64/eduvpn_verify.so' -build: $(GOOS)/$(GOARCH)/$(LIB_PREFIX)eduvpn_verify$(LIB_SUFFIX) +ifeq ($(LIB_SUFFIX),.so) +# Add SONAME as cgo does not currently do this. Mostly for Android, see https://stackoverflow.com/a/48291044 +export CGO_LDFLAGS := $(CGO_LDFLAGS) -Wl,-soname,$(LIB_FILE) +endif -$(GOOS)/$(GOARCH)/$(LIB_PREFIX)eduvpn_verify$(LIB_SUFFIX): exports.go ../verify.go - CGO_ENABLED=1 GOOS=$(GOOS) GOARCH=$(GOARCH) go build -o $@ -buildmode=c-shared $< - mv $(GOOS)/$(GOARCH)/$(LIB_PREFIX)eduvpn_verify.h $(GOOS)/$(GOARCH)/eduvpn_verify.h || true +# Creates targets like 'lib/linux/amd64/libeduvpn_common.so' +build: lib/$(GOOS)/$(GOARCH)/$(LIB_FILE) -copy-to: $(GOOS)/$(GOARCH)/$(LIB_PREFIX)eduvpn_verify$(LIB_SUFFIX) - install $< -Dt "$(COPY_TARGET)" +# Build shared library and remove lib prefix (if any) from header name +# GOOS and GOARCH envvars are set by platform.mk +# This extra target prevents unnecessary rebuild +lib/$(GOOS)/$(GOARCH)/$(LIB_FILE): exports.go ../verify.go + CGO_ENABLED=1 go build -o $@ -buildmode=c-shared $< + mv lib/$(GOOS)/$(GOARCH)/$(LIB_PREFIX)$(LIB_NAME).h lib/$(GOOS)/$(GOARCH)/$(LIB_NAME).h || true # Normalize header name clean: - rm -rf ../exports/*/ + rm -rf ../exports/lib/* diff --git a/exports/exports.go b/exports/exports.go index f841eca..b3d4206 100644 --- a/exports/exports.go +++ b/exports/exports.go @@ -4,25 +4,28 @@ import "C" import "eduvpn-common" -// Functions here should not take string parameters, see https://pkg.go.dev/cmd/cgo#hdr-C_references_to_Go +// Functions here should probably not take string parameters, see https://pkg.go.dev/cmd/cgo#hdr-C_references_to_Go // Verify verifies a signature on a JSON file. See eduvpn_verify.Verify for more details. // It returns 0 for a valid signature and a nonzero eduvpn_verify.VerifyErrorCode otherwise. +// signatureFileContent must be UTF-8-encoded. //export Verify -func Verify(signatureFileContent []byte, signedJson []byte, expectedFileName []byte, minSignTime uint64) int { - valid, err := eduvpn_verify.Verify(string(signatureFileContent), signedJson, string(expectedFileName), minSignTime) +func Verify(signatureFileContent []byte, signedJson []byte, expectedFileName []byte, minSignTime uint64) int8 { + valid, err := eduvpn_discovery.Verify(string(signatureFileContent), signedJson, string(expectedFileName), minSignTime) if valid { return 0 } else { - return int(err.(eduvpn_verify.VerifyError).Code) + return int8(err.(eduvpn_discovery.VerifyError).Code) } } // InsecureTestingSetExtraKey adds an extra allowed key for verification with Verify. // ONLY USE FOR TESTING. Not Thread-safe. Do not call in parallel to Verify. +// keyString must be an ASCII Base64-encoded key. //export InsecureTestingSetExtraKey func InsecureTestingSetExtraKey(keyString []byte) { - eduvpn_verify.InsecureTestingSetExtraKey(string(keyString)) + eduvpn_discovery.InsecureTestingSetExtraKey(string(keyString)) } +// Not used in library, but needed to compile. func main() { panic("compile with -buildmode=c-shared") } diff --git a/exports/generate_lib.ps1 b/exports/generate_lib.ps1 new file mode 100644 index 0000000..ac452ca --- /dev/null +++ b/exports/generate_lib.ps1 @@ -0,0 +1,30 @@ +<# +.SYNOPSIS + Generate .lib import library file for specified .dll file. +.NOTES + Requires dumpbin & lib, may need to execute through VS developer shell. +#> + +param ( + [string]$DllPath +) + +# Compatible with both Windows PowerShell and PowerShell Core + +$ErrorActionPreference = "Stop" + +$dll = Get-Item $DllPath +$def = Join-Path $dll.Directory "$( $dll.BaseName ).def" +$lib = Join-Path $dll.Directory "$( $dll.BaseName ).lib" +$machine = (dumpbin /nologo /headers $dll.FullName | + Select-String -AllMatches 'machine \((.+)\)').Matches[0].Groups[1].Value + +"LIBRARY $( $dll.BaseName )`nEXPORTS`n" + ( +(dumpbin /nologo /exports $dll.FullName | + Select-String -AllMatches '\d+\s+\d+\s+[0-9A-Z]+\s+(\S+)').Matches | + % { $_.Groups[1].Value } | + where { $_[0] -ne '_' } | # Skip _cgo_dummy_export +Out-String) | + Set-Content $def + +lib /machine:$machine /def:"$def" /out:"$lib" diff --git a/exports/platform.mk b/exports/platform.mk index d44c889..cd1248b 100644 --- a/exports/platform.mk +++ b/exports/platform.mk @@ -1,3 +1,5 @@ +# Prevent executing `go env ...` multiple times for the same property +# export is needed for this and also to pass the values on to the Go compiler ifndef GOOS export GOOS != go env GOHOSTOS endif @@ -16,9 +18,16 @@ LIB_PREFIX = lib LIB_SUFFIX = .so endif +# Library name without prefixes/suffixes +LIB_NAME = eduvpn_common +# Library file name +LIB_FILE = $(LIB_PREFIX)$(LIB_NAME)$(LIB_SUFFIX) + +# Get exports/ directory when included from a wrapper exports_dir = $(dir $(abspath $(lastword $(MAKEFILE_LIST)))) +# Add library to dynamic linker path for running tests ifeq (Windows_NT,$(OS)) -export PATH := $(exports_dir)/$(GOOS)/$(GOARCH):$(PATH) +export PATH := $(exports_dir)/lib/$(GOOS)/$(GOARCH):$(PATH) else -export LD_LIBRARY_PATH := $(exports_dir)/$(GOOS)/$(GOARCH):$(LD_LIBRARY_PATH) +export LD_LIBRARY_PATH := $(exports_dir)/lib/$(GOOS)/$(GOARCH):$(LD_LIBRARY_PATH) endif |
