From 5a5e748f17a6a7df03ff99e938025487bdf284dd Mon Sep 17 00:00:00 2001 From: heck Date: Mon, 25 Jan 2021 17:34:42 +0100 Subject: [PATCH] new example extension "lib_test" --- examples/ext/lib_test/Makefile | 96 +++++++++++++++++++ examples/ext/lib_test/Makefile.conf | 10 ++ examples/ext/lib_test/lib_test/Makefile | 47 +++++++++ examples/ext/lib_test/lib_test/Makefile.conf | 12 +++ examples/ext/lib_test/lib_test/gen/Makefile | 35 +++++++ .../ext/lib_test/lib_test/gen/config.json | 13 +++ examples/ext/lib_test/lib_test/lib_test.cc | 14 +++ examples/ext/lib_test/pyproject.toml | 15 +++ examples/ext/lib_test/requirements.txt | 2 + examples/ext/lib_test/setup.cfg | 42 ++++++++ examples/ext/lib_test/setup.py | 22 +++++ examples/ext/lib_test/tests/test_lib_test.py | 6 ++ 12 files changed, 314 insertions(+) create mode 100644 examples/ext/lib_test/Makefile create mode 100644 examples/ext/lib_test/Makefile.conf create mode 100644 examples/ext/lib_test/lib_test/Makefile create mode 100644 examples/ext/lib_test/lib_test/Makefile.conf create mode 100644 examples/ext/lib_test/lib_test/gen/Makefile create mode 100644 examples/ext/lib_test/lib_test/gen/config.json create mode 100644 examples/ext/lib_test/lib_test/lib_test.cc create mode 100644 examples/ext/lib_test/pyproject.toml create mode 100644 examples/ext/lib_test/requirements.txt create mode 100755 examples/ext/lib_test/setup.cfg create mode 100755 examples/ext/lib_test/setup.py create mode 100644 examples/ext/lib_test/tests/test_lib_test.py diff --git a/examples/ext/lib_test/Makefile b/examples/ext/lib_test/Makefile new file mode 100644 index 0000000..fc6174c --- /dev/null +++ b/examples/ext/lib_test/Makefile @@ -0,0 +1,96 @@ +include Makefile.conf + +.PHONY: all install-pepacidgen gen compile compile-inplace dist dist-egg dist-whl install install-user \ + venv envtest install-test test develop docs clean clean-all clean-docs + +all: install + +# Install pEpACIDgen from this repo, not from pypi +install-pepacidgen: + pip3 install -r requirements.txt --find-links ../../../dist/ + +# Build +# ===== +gen: install-pepacidgen + $(MAKE) -C lib_test gen + +compile: gen + python3 setup.py build_ext $(DEBUG_OPT) $(PREFIX_OPT) + +compile-inplace: gen + python3 setup.py build_ext $(DEBUG_OPT) $(PREFIX_OPT) --inplace + +# Packaging +# ========= +# create wheel and egg package in dist/ +dist: dist-whl dist-egg + +# create wheel package in dist/ +dist-whl: compile + python3 setup.py bdist_wheel + +# create egg package in dist/ +dist-egg: compile + python3 setup.py bdist_egg + + +# Installation +# ============ +# installs the package system wide +install: compile + pip3 install . + +# installs the package into your user home +install-user: compile + pip3 install . --user + + +# Envrionment +# =========== +# Creates and activates a new venv that has the LD_LIBRARY_PATH/DYLD_LIBRARY_PATH +# already set for the prefix specified in local.conf +# Only activates venv if already existing +venv: + python3 -m venv $(VENV_DIR) + LD_LIBRARY_PATH=../../lib/lib_test/ \ + DYLD_LIBRARY_PATH=../../lib/lib_test/ \ + bash --rcfile $(VENV_DIR)/bin/activate + +# Tests if the current environment is able to load the pEp module +envtest: + python3 -c 'import lib_test' + +# Test +# ==== +# Use these targets only in venv created with 'make venv' +install-test: compile + pip3 install .[test] + +test: + pytest + + +# Development +develop: compile + pip install -e . + +# Housekeeping +# ============ +clean-all: clean + rm -rf $(VENV_DIR) + +clean: + $(MAKE) -C lib_test/ clean + rm -rf $(BUILD_DIR) + rm -rf $(DIST_DIR) + rm -rf $(PYTHON_ARTIFACTS) + rm -rf $(BUILD_INPLACE) + + +# Makefile based build of C++ parts only +# ====================================== +makefile-build: + $(MAKE) -C lib_test/ + +makefile-clean: + $(MAKE) -C lib_test/ clean diff --git a/examples/ext/lib_test/Makefile.conf b/examples/ext/lib_test/Makefile.conf new file mode 100644 index 0000000..e786f8d --- /dev/null +++ b/examples/ext/lib_test/Makefile.conf @@ -0,0 +1,10 @@ +HERE:=$(dir $(lastword $(MAKEFILE_LIST))) + +# Constants +BUILD_DIR = ./build +DIST_DIR = ./dist +BUILD_INPLACE = ./lib_test/lib_test.cpython-38-darwin.so +PYTHON_ARTIFACTS += ./.eggs +PYTHON_ARTIFACTS += ./lib_test.egg-info +PYTHON_ARTIFACTS += ./.pytest_cache +VENV_DIR = ./venv diff --git a/examples/ext/lib_test/lib_test/Makefile b/examples/ext/lib_test/lib_test/Makefile new file mode 100644 index 0000000..0cc67c4 --- /dev/null +++ b/examples/ext/lib_test/lib_test/Makefile @@ -0,0 +1,47 @@ +include Makefile.conf + +TARGET=lib_test.so + +# Swap here, for static vs dyn linking +TARGET_MODULE_DYN=$(TARGET) +TARGET_MODULE_STATIC= + +CXX=clang +CXXFLAGS+=-std=c++11 -g + +SRCS+=$(wildcard *.cc) +OBJS+=$(SRCS:.cc=.o) + +CXXFLAGS+=$(INCLUDES) +LDFLAGS_DYN+=-undefined dynamic_lookup $(LIBS_PATH) $(LIBS) +LDFLAGS_STATIC+=-undefined dynamic_lookup + +$(info -----BUILD INFO----) +$(info SRCS $(SRCS)) +$(info OBJS $(OBJS)) + + +.PHONY: all gen gen-pybind module_dyn module_static clean + +all: gen compile + +gen: + $(MAKE) -C gen + +gen-pybind: + $(MAKE) -C gen pybind + +compile: $(TARGET) + +$(TARGET_MODULE_DYN) : $(OBJS) + $(CXX) $(LDFLAGS_DYN) -o $@ $^ + +$(TARGET_MODULE_STATIC) : $(OBJS) $(LIBS_STATIC) + $(CXX) $(LDFLAGS_STATIC) -o $@ $^ + +clean: + rm -f $(TARGET) + rm -f $(OBJS) + +clean-all: clean + $(MAKE) -C gen clean diff --git a/examples/ext/lib_test/lib_test/Makefile.conf b/examples/ext/lib_test/lib_test/Makefile.conf new file mode 100644 index 0000000..3fed0d9 --- /dev/null +++ b/examples/ext/lib_test/lib_test/Makefile.conf @@ -0,0 +1,12 @@ +# pyBind11 and python headers +INCLUDES+=$(shell pybind11-config --includes) + +# example lib +INCLUDES+=-I../../../lib/lib_test + +# static lib (.a) +LIBS_STATIC+=../../../lib/lib_test/liblib_test.a + +# dynamic lib (.so) +LIBS+=-llib_test +LIBS_PATH+=-L../../../lib/lib_test/ diff --git a/examples/ext/lib_test/lib_test/gen/Makefile b/examples/ext/lib_test/lib_test/gen/Makefile new file mode 100644 index 0000000..d27d373 --- /dev/null +++ b/examples/ext/lib_test/lib_test/gen/Makefile @@ -0,0 +1,35 @@ +include ../Makefile.conf + +YML2_FILE=py_module.yml2 +YSL2_FILE=$(shell pEp_acid_gen-config) +PYBIND11_FILE=py_module.pybind11 +DEBUG_AST_FILE=lib_test.h.ast.json +DEBUG_ACID_FILE=lib_test.h.acid.json +DEBUG_YML_FILE=lib_test.h.acid.yml + + +$(info -----LIB_TEST GEN----) +$(info YML2_FILE $(YML2_FILE)) +$(info YSL2_FILE $(YSL2_FILE)) +$(info CC_FILE $(PYBIND11_FILE)) + +.PHONY = all yml pybind11 + +all: pybind + +yml: $(YML2_FILE) + +pybind: $(PYBIND11_FILE) + +$(YML2_FILE): config.json + pEp_acid_gen $^ + +$(PYBIND11_FILE) : $(YML2_FILE) + yml2proc --encoding=utf8 -y $(YSL2_FILE) $(YML2_FILE) + +clean: + rm -f $(YML2_FILE) + rm -f $(PYBIND11_FILE) + rm -f $(DEBUG_AST_FILE) + rm -f $(DEBUG_ACID_FILE) + rm -f $(DEBUG_YML_FILE) diff --git a/examples/ext/lib_test/lib_test/gen/config.json b/examples/ext/lib_test/lib_test/gen/config.json new file mode 100644 index 0000000..90dd6e7 --- /dev/null +++ b/examples/ext/lib_test/lib_test/gen/config.json @@ -0,0 +1,13 @@ +{ + "module_name": "lib_test", + "header_filename": "../../../../../examples/lib/lib_test/lib_test.h", + "libclang_path": "/opt/local/libexec/llvm-9.0/lib/libclang.dylib", + "variables": [ + ], + "functions": [ + "filtertype_to_string" + ], + "debug_ast" : "True", + "debug_acid" : "True", + "debug_yml" : "True" +} diff --git a/examples/ext/lib_test/lib_test/lib_test.cc b/examples/ext/lib_test/lib_test/lib_test.cc new file mode 100644 index 0000000..37eecd3 --- /dev/null +++ b/examples/ext/lib_test/lib_test/lib_test.cc @@ -0,0 +1,14 @@ +#include +#include +#include +#include +#include "lib_test.h" + +using namespace std; + +PYBIND11_MODULE(lib_test, m) { + #include "gen/py_module.pybind11" + + +} + diff --git a/examples/ext/lib_test/pyproject.toml b/examples/ext/lib_test/pyproject.toml new file mode 100644 index 0000000..3691cdb --- /dev/null +++ b/examples/ext/lib_test/pyproject.toml @@ -0,0 +1,15 @@ +[build-system] +# Preparing for PEP-517/PEP-518, but not in effect yet. +# These requires are not effective yet, setup.cfg is. +requires = [ + "setuptools >=39.2.0", + "wheel >= 0.35.1", ] + +build-backend = "setuptools.build_meta" + +[tool.pytest.ini_options] +minversion = "6.0" +addopts = "-rP" +testpaths = [ + "tests", +] diff --git a/examples/ext/lib_test/requirements.txt b/examples/ext/lib_test/requirements.txt new file mode 100644 index 0000000..ded7134 --- /dev/null +++ b/examples/ext/lib_test/requirements.txt @@ -0,0 +1,2 @@ +pEpACIDgen +pyBind11 diff --git a/examples/ext/lib_test/setup.cfg b/examples/ext/lib_test/setup.cfg new file mode 100755 index 0000000..63f30ce --- /dev/null +++ b/examples/ext/lib_test/setup.cfg @@ -0,0 +1,42 @@ +[metadata] +name = lib_test +url = https://pep.foundation +download_url = +ext_package = lib_test +author = heck +author_email = heck@pep-project.org +maintainer = heck +maintainer_email = heck@pep.foundation +description = pEpACIDgen example extension module +long_description = file: README.md +keywords = pEpACIDgen +license = GNU Affero General Public License +license_files = LICENSE.txt +platforms = linux, macOs +classifiers = + Intended Audience :: Developers + Topic :: Utilities + License :: OSI Approved :: GNU Affero General Public License v3 or later (AGPLv3+) + Natural Language :: English + Operating System :: OS Independent + Programming Language :: Python :: 3.6 + Programming Language :: Python :: 3.7 + Programming Language :: Python :: 3.8 + Development Status :: 3 - Alpha + +[options] +zip_safe = false +include_package_data = true +python_requires = >= 3.6 +test_suite = tests +install_requires = +# setup_requires is deprecated/redundant with pyproject.toml, but lets keep both ways around for now +setup_requires = + setuptools >=39.2.0 + wheel >= 0.35.1 + +[options.extras_require] +# To install these dependencies, run pip install .[test] +test = + pytest + diff --git a/examples/ext/lib_test/setup.py b/examples/ext/lib_test/setup.py new file mode 100755 index 0000000..89c2d3b --- /dev/null +++ b/examples/ext/lib_test/setup.py @@ -0,0 +1,22 @@ +# -*- coding: utf-8 -*- +# This file is under GNU Affero General Public License 3.0 +# see LICENSE.txt +from setuptools import setup, Extension + +ext_modules = [ + Extension( + "lib_test", + ["lib_test/lib_test.cc"], + include_dirs=['../../lib/lib_test', + '/opt/local/Library/Frameworks/Python.framework/Versions/3.8/include/python3.8', + 'venv/lib/python3.8/site-packages/pybind11/include'], + extra_compile_args=['-std=c++11'], + libraries=['lib_test'], + library_dirs=['../../lib/lib_test/'] + ) +] + +setup( + packages=['lib_test'], + ext_modules=ext_modules +) diff --git a/examples/ext/lib_test/tests/test_lib_test.py b/examples/ext/lib_test/tests/test_lib_test.py new file mode 100644 index 0000000..4e09d37 --- /dev/null +++ b/examples/ext/lib_test/tests/test_lib_test.py @@ -0,0 +1,6 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- +# This file is under GNU Affero General Public License 3.0 +# see LICENSE.txt +import pytest +