diff options
| author | jwijenbergh <jeroenwijenbergh@protonmail.com> | 2024-03-22 11:52:28 +0100 |
|---|---|---|
| committer | Jeroen Wijenbergh <46386452+jwijenbergh@users.noreply.github.com> | 2024-03-22 13:30:41 +0100 |
| commit | 9bab3c87825b4284b3e6f2be3fa9e89da7723116 (patch) | |
| tree | 6049f03402f23168c5000e9843a979935cd0996d | |
| parent | f94e6471268c12b7b5c7fd1c8b8800dac3848d6c (diff) | |
All: Refactor makefile & building
| -rw-r--r-- | Makefile | 39 | ||||
| -rw-r--r-- | docs/src/gettingstarted/building/go.md | 50 | ||||
| -rw-r--r-- | docs/src/gettingstarted/building/python.md | 6 | ||||
| -rw-r--r-- | exports/common.mk | 49 | ||||
| -rw-r--r-- | wrappers/python/Makefile | 26 | ||||
| -rw-r--r-- | wrappers/python/README.md | 27 | ||||
| -rw-r--r-- | wrappers/python/eduvpn_common/loader.py | 21 | ||||
| -rw-r--r-- | wrappers/python/pyproject.toml | 39 | ||||
| -rwxr-xr-x | wrappers/python/setup.py | 124 |
9 files changed, 73 insertions, 308 deletions
@@ -1,30 +1,25 @@ -.PHONY: build test test-go test-wrappers clean +.DEFAULT_GOAL := build +.PHONY: build fmt cli clean -build: - $(MAKE) -C exports -test: test-go test-wrappers +VERSION := $(shell grep -o 'const Version = "[^"]*' internal/version/version.go | cut -d '"' -f 2) + -test-go: - go test ./... -v +build: + CGO_ENABLED="1" go build -o lib/libeduvpn_common-${VERSION}.so -buildmode=c-shared ./exports -#WRAPPERS ?= $(notdir $(patsubst %/,%,$(wildcard wrappers/*/))) -WRAPPERS=python +fmt: + gofumpt -w . -# Enable parallelism if -j is specified, but first execute build -test-wrappers: build - $(MAKE) $(foreach wrapper,$(WRAPPERS),.test-$(wrapper)) +lint: + golangci-lint run -E stylecheck,revive,gocritic ./... -clean: .clean-libs $(foreach wrapper,$(WRAPPERS),.clean-$(wrapper)) +cli: + go build -o eduvpn-common-cli ./cmd/cli -.clean-libs: - $(MAKE) -C exports clean +test: + go test ./... -# Define test & clean for each wrapper -define wrapper_targets -.test-$(1): - $(MAKE) -C wrappers/$(1) test -.clean-$(1): - $(MAKE) -C wrappers/$(1) clean -endef -$(foreach wrapper,$(WRAPPERS),$(eval $(call wrapper_targets,$(wrapper)))) +clean: + rm -rf lib + go clean diff --git a/docs/src/gettingstarted/building/go.md b/docs/src/gettingstarted/building/go.md index 7d07ca8..65d3c26 100644 --- a/docs/src/gettingstarted/building/go.md +++ b/docs/src/gettingstarted/building/go.md @@ -1,5 +1,5 @@ # Building the Go library -To build the Go library, you need the dependencies for your system installed. We will go over the needed dependencies for Linux and Windows. Afterwards, we explain the basic commands to build the library. +To build the Go library, you need the dependencies for your system installed. We will go over the needed dependencies for Linux. Afterwards, we explain the basic commands to build the library. ## Dependencies ### Linux @@ -8,27 +8,7 @@ To build the Go shared library using Linux you need the following dependencies: - [Go](https://go.dev/doc/install) 1.18 or later - [Gcc](https://gcc.gnu.org/) - [GNU Make](https://www.gnu.org/software/make/) -- Dependencies for each wrapper you are interested in (read next sections) - -### Windows -On Windows, you can install gcc and make (or even Go) via MinGW or Cygwin or use WSL. For MinGW: - -1. [Install MinGW](https://www.msys2.org/#installation) (you don't need to install any extra packages yet) and open some - MSYS2 terminal (e.g. from the start menu or one of the installed binaries) -2. Install the [`make`](https://packages.msys2.org/package/make?repo=msys) package (or - e.g. [`mingw-w64-x86_64-make`](https://packages.msys2.org/package/mingw-w64-x86_64-make?repo=mingw64) and - use `mingw32-make` in the command line) -3. To compile for x86_64: - 1. Install the [`mingw-w64-x86_64-gcc`](https://packages.msys2.org/package/mingw-w64-x86_64-gcc?repo=mingw64) - package - 2. Open the MinGW 64-bit console, via the start menu, or in your current - terminal: `path/to/msys64/msys2_shell.cmd -mingw64 -defterm -no-start -use-full-path` - 3. Run the make commands in the project directory -4. To compile for x86 (32-bit): - 1. Install the [`mingw-w64-i686-gcc`](https://packages.msys2.org/package/mingw-w64-i686-gcc?repo=mingw32) package - 2. Open the MinGW 32-bit console, via the start menu, or in your current - terminal: `path/to/msys64/msys2_shell.cmd -mingw32 -defterm -no-start -use-full-path` - 3. Run the make commands in the project directory +- Dependencies for the Python wrapper if you want to build that as well ## Commands Before we can begin building the wrapper code, we need to build the Go code as a shared library. This section will tell you how to do so. @@ -39,37 +19,15 @@ To build the shared library for the current platform issue the following command make ``` -You can also build the shared library for a specified OS & architecture (example): - -```bash -make GOOS=windows GOARCH=386 -``` - -We use cgo to build a shared library, to list all platform supported by cgo issue `go tool dist list`. - -The shared library will be output in `exports/lib/`. - -For cross compiling, you usually need to specify the compiler, for example: - -```bash -make GOOS=windows GOARCH=amd64 CC=x86_64-w64-mingw32-gcc -``` - -For example, you can cross compile for Windows from Linux using [MinGW-w64](https://www.mingw-w64.org/downloads/). - -This shared library gets loaded by the different wrappers. To build the actual wrapper code, you need other build commands. This will be explained now. +The shared library will be output in `lib/`. ### Cleaning To clean build the library and wrapper, issue the following command in the root directory: ```bash -make -j clean +make clean ``` -Usually you won't need to do this, as changes in the library should automatically be incorporated in wrappers. -Specify `CLEAN_ALL=1` to also remove downloaded dependencies for some wrappers. You can clean individual wrappers by -executing clean in their directories, or specify `WRAPPERS=...`. - ## Note on releases Releases are build with the go tag "release" (add flag "-tags=release") to bundle the discovery JSON files and embed them in the shared library. See the [make_release](https://github.com/eduvpn/eduvpn-common/blob/main/make_release.sh) script on how we bundle the files. A full command without the Makefile to build this library is: diff --git a/docs/src/gettingstarted/building/python.md b/docs/src/gettingstarted/building/python.md index 78ce424..03e064a 100644 --- a/docs/src/gettingstarted/building/python.md +++ b/docs/src/gettingstarted/building/python.md @@ -6,11 +6,7 @@ To build the python wrapper issue the following command (in the root directory o make -C wrappers/python ``` -This uses the makefile in `wrappers/python/Makefile` to build the python file into a wheel placed in `wrappers/python/dist/eduvpncommon-[version]-py3-none-[platform].whl`. Where version is the version of the library and platform is your current platform. Like Go you can also build for a specific platform: - -```bash -make PLAT_NAME=win32 -``` +This uses the makefile in `wrappers/python/Makefile` to build the python file into a wheel placed in `wrappers/python/dist/eduvpncommon-[version]-py3-none-[platform].whl`. Where version is the version of the library and platform is your current platform. The wheel can be installed with `pip`: diff --git a/exports/common.mk b/exports/common.mk deleted file mode 100644 index 646c9c1..0000000 --- a/exports/common.mk +++ /dev/null @@ -1,49 +0,0 @@ -# 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 := $(shell go env GOHOSTOS) -endif -ifndef GOARCH -export GOARCH := $(shell go env GOHOSTARCH) -endif - -ifeq (windows,$(GOOS)) -LIB_PREFIX ?= -LIB_SUFFIX ?= .dll -else ifeq (darwin,$(GOOS)) -LIB_PREFIX ?= lib -LIB_SUFFIX ?= .dylib -else -LIB_PREFIX ?= lib -LIB_SUFFIX ?= .so -endif - -# Get relative exports/ directory when included from a wrapper, without trailing slash -override EXPORTS_PATH = $(patsubst %/,%,$(dir $(lastword $(MAKEFILE_LIST)))) - -# Current version -VERSION := $(shell grep -o 'const Version = "[^"]*' "${EXPORTS_PATH}"/../internal/version/version.go | cut -d '"' -f 2) - -# Library name without prefixes/suffixes -LIB_NAME ?= eduvpn_common -LIB_NAME_VERSION ?= $(LIB_NAME)-$(VERSION) -# Library file name -LIB_FILE ?= $(LIB_PREFIX)$(LIB_NAME_VERSION)$(LIB_SUFFIX) - -EXPORTS_LIB_PATH ?= $(EXPORTS_PATH)/lib -EXPORTS_LIB_SUBFOLDER_PATH ?= $(EXPORTS_LIB_PATH)/$(GOOS)/$(GOARCH) - -# Add library to dynamic linker path for running tests -ifeq (Windows_NT,$(OS)) -export PATH := $(abspath $(EXPORTS_LIB_SUBFOLDER_PATH)):$(PATH) -else -export LD_LIBRARY_PATH := $(abspath $(EXPORTS_LIB_SUBFOLDER_PATH)):$(LD_LIBRARY_PATH) -export DYLD_FALLBACK_LIBRARY_PATH := $(abspath $(EXPORTS_LIB_SUBFOLDER_PATH)):$(DYLD_FALLBACK_LIBRARY_PATH) -endif - -.try-build-lib: -ifneq ($(wildcard $(EXPORTS_PATH)/Makefile),) - $(MAKE) -C $(EXPORTS_PATH) -else -$(info Skipping building library as exports/Makefile was not found) -endif diff --git a/wrappers/python/Makefile b/wrappers/python/Makefile index d5e9071..9d51c15 100644 --- a/wrappers/python/Makefile +++ b/wrappers/python/Makefile @@ -1,24 +1,18 @@ -.PHONY: pack test clean +.DEFAULT_GOAL := pack +.PHONY: install-lib pack test clean -EXPORTS_PATH ?= ../../exports -include $(EXPORTS_PATH)/common.mk +VERSION := $(shell grep -o 'const Version = "[^"]*' ../../internal/version/version.go | cut -d '"' -f 2) -ifdef PLAT_NAME -override SETUP_ARGS += --plat-name=$(PLAT_NAME) -endif +install-lib: + rm -rf eduvpn_common/lib/* + install "../../lib/libeduvpn_common-${VERSION}.so" -Dt "eduvpn_common/lib" # Build for current platform only -pack: - mkdir -p ./eduvpn_common/lib - ./setup.py bdist_wheel $(SETUP_ARGS) --exports-lib-path="$(EXPORTS_LIB_PATH)" +pack: install-lib + python3 -m build --sdist --wheel . -test: .try-build-lib - install "$(EXPORTS_LIB_SUBFOLDER_PATH)/$(LIB_FILE)" -Dt "eduvpn_common/lib" +test: install-lib python3 -m unittest tests - rm eduvpn_common/lib/* clean: - rm -rf build/ dist/ *.egg-info/ lib/* -ifeq ($(CLEAN_ALL),1) - rm -rf venv/ -endif + rm -rf build/ dist/ *.egg-info/ eduvpn_common/lib/* venv diff --git a/wrappers/python/README.md b/wrappers/python/README.md index 6f0bec1..905d7d4 100644 --- a/wrappers/python/README.md +++ b/wrappers/python/README.md @@ -2,42 +2,25 @@ ## Requirements -Python 3.6+ is assumed, but it may work with older versions. To build, `setuptools` and `wheel` are required. +Python 3.6+ is assumed, but it may work with older versions. To build, `setuptools`, `wheel` and `build` are required. -## Build & test +## Building -First build the shared Go library. Next: +First build the shared Go library by following the instructions in the root directory of this Repo. -Build wheel using library for current platform: +Then, to build the wheel use: ```shell make pack ``` -(This does not build the shared Go library.) - -Build wheel using library for specified platform (passed to setuptools `--plat-name`, -see [`get_build_platform`](https://setuptools.pypa.io/en/latest/pkg_resources.html?highlight=get_build_platform#platform-utilities) -for more): - -```shell -make pack PLAT_NAME=win32 -``` - To install the wheel, run: ```shell pip install dist/eduvpncommon-[version]-py3-none-[platform].whl ``` -You could also reference the discovery module directly and copy the library for the platform to the `eduvpncommon/lib/` -folder. - -If you do not build this as part of the full repository, specify `EXPORTS_PATH="path/to/exports-folder"` when calling -make. This folder must contain `common.mk` and the `lib/` folder with built libraries. - -Test: - +## Running tests ```shell make test ``` diff --git a/wrappers/python/eduvpn_common/loader.py b/wrappers/python/eduvpn_common/loader.py index f1e27e1..ca0fd20 100644 --- a/wrappers/python/eduvpn_common/loader.py +++ b/wrappers/python/eduvpn_common/loader.py @@ -1,5 +1,4 @@ import pathlib -import platform from collections import defaultdict from ctypes import CDLL, c_char_p, c_int, c_void_p, cdll @@ -24,25 +23,7 @@ def load_lib() -> CDLL: :return: The Go shared library loaded with cdll.LoadLibrary from ctypes :rtype: CDLL """ - lib_prefixes = defaultdict( - lambda: "lib", - { - "windows": "", - }, - ) - - lib_suffixes = defaultdict( - lambda: ".so", - { - "windows": ".dll", - "darwin": ".dylib", - }, - ) - - os = platform.system().lower() - - libname = "eduvpn_common" - libfile = f"{lib_prefixes[os]}{libname}-{__version__}{lib_suffixes[os]}" + libfile = f"libeduvpn_common-{__version__}.so" lib = None diff --git a/wrappers/python/pyproject.toml b/wrappers/python/pyproject.toml index 44836bb..b43aa5a 100644 --- a/wrappers/python/pyproject.toml +++ b/wrappers/python/pyproject.toml @@ -1,6 +1,37 @@ [build-system] -requires = [ - "setuptools", - "wheel", -] +requires = ["setuptools", "wheel"] build-backend = "setuptools.build_meta" + +[project] +name = "eduvpn_common" +version = "1.99.1" +description = "eduvpn-common library" +authors = [ + {name = "Jeroen Wijenbergh", email = "jeroen.wijenbergh@geant.org"}, +] +requires-python = ">=3.6" +readme = "README.md" +license = {text = "MIT"} + +[project.urls] +Homepage = "https://github.com/eduvpn/eduvpn-common" + +[project.optional-dependencies] +lint = ["ruff" ] +mypy = [ "mypy" ] + +[tool.setuptools.packages.find] +include = ["eduvpn_common*"] + +[tool.ruff] +line-length = 120 + +[tool.ruff.lint] +extend-select = [ + # isort + "I", +] +ignore = ['E402'] + +[tool.ruff.lint.isort] +case-sensitive = true diff --git a/wrappers/python/setup.py b/wrappers/python/setup.py deleted file mode 100755 index 37ff577..0000000 --- a/wrappers/python/setup.py +++ /dev/null @@ -1,124 +0,0 @@ -#!/usr/bin/env python3 - -import os -import shutil -import sys -import typing -from collections import defaultdict - -from setuptools import setup -from wheel.bdist_wheel import bdist_wheel as _bdist_wheel - -_libname = "eduvpn_common" -__version__ = "1.99.1" - - -def getlibpath(plat_name: str) -> typing.Union[str, None]: - """Get library path for plat_name relative to exports/lib/ folder.""" - - plat_map = defaultdict( - lambda: plat_name, - { - "win32": "win-x86", - }, - ) - - plat_split = plat_map[plat_name].split("-", 1) - if len(plat_split) != 2: - return None - plat_os, plat_arch = plat_split - - os_map = defaultdict( - lambda: plat_os, - { - "win": "windows", - }, - ) - lib_prefixes = defaultdict( - lambda: "lib", - { - "windows": "", - }, - ) - lib_suffixes = defaultdict( - lambda: ".so", - { - "windows": ".dll", - "darwin": ".dylib", - }, - ) - arch_map = defaultdict( - lambda: plat_arch, - { - "aarch64_be": "arm64", - "aarch64": "arm64", - "armv8b": "arm64", - "armv8l": "arm64", - "x86": "386", - "x86pc": "386", - "i86pc": "386", - "i386": "386", - "i686": "386", - "x86_64": "amd64", - "i686-64": "amd64", - }, - ) - - processed_os = os_map[plat_os] - return ( - processed_os - + "/" - + arch_map[plat_arch] - + "/" - + lib_prefixes[processed_os] - + _libname - + "-" - + __version__ - + lib_suffixes[processed_os] - ) - - -# Adapted from https://stackoverflow.com/a/51794740 -# You would say there would be a better way to do all of this, but I couldn't find it - - -class bdist_wheel(_bdist_wheel): - user_options = _bdist_wheel.user_options + [ - ("exports-lib-path=", None, "path to exports/lib directory"), - ] - - def initialize_options(self): - super().initialize_options() - self.exports_lib_path = "../../exports/lib" # default - - def run(self): - self.plat_name_supplied = True # Force use platform - - libpath = getlibpath(self.plat_name) - if not libpath: - print("Unknown platform:", self.plat_name) - sys.exit(1) - - print("Building wheel for platform:", self.plat_name) - - # setuptools will only use paths inside the package for package_data, so we copy the library - p = "eduvpn_common/lib" - if os.path.isdir(p): - shutil.rmtree(p) - os.makedirs(p) - shutil.copyfile( - self.exports_lib_path + "/" + libpath, p + "/" + libpath.split("/")[-1] - ) - _bdist_wheel.run(self) - shutil.rmtree(p) - - -setup( - name="eduvpn_common", - version=__version__, - packages=["eduvpn_common"], - python_requires=">=3.6", - package_dir={"eduvpn_common": "eduvpn_common"}, - package_data={"eduvpn_common": ["lib/*" + _libname + "*", "py.typed"]}, - cmdclass={"bdist_wheel": bdist_wheel}, -) |
