-
Notifications
You must be signed in to change notification settings - Fork 68
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add c bindings + Updated camke builds (#166)
* Add c bindings for create_discriminant, prove, and verify_n_wesolowski * Add chiavdfc shared library target * Don't build the c wrapper library by default (avoid it in the wheel) + add workflow for the c libs * no build vdf client * mpir for windows + unique artifact names per platform * Fix checkout * remove .git * Fix mpir path * Include mpir on windows * mpir before forcing gmp * Fix path to mpir in cmake * Add a windows specific prefix path to help find gmp and gmpxx * Split mpir/gmp(xx) for windows/non-windows * Try cmake --build * Missed . * -pthread flag doesn't apply to windows * Fix windows compile warnings * Update path for dynamic dll * Cleanup setup.py to build windows via cmake, similar to the chiapos cleanup * remove unused import
- Loading branch information
1 parent
fe87454
commit 2e9bc3e
Showing
7 changed files
with
253 additions
and
145 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
name: Build C Libraries | ||
|
||
on: | ||
push: | ||
branches: | ||
- main | ||
release: | ||
types: [published] | ||
pull_request: | ||
branches: | ||
- '**' | ||
|
||
concurrency: | ||
# SHA is added to the end if on `main` to let all main workflows run | ||
group: ${{ github.ref }}-${{ github.workflow }}-${{ github.event_name }}-${{ (github.ref == 'refs/heads/main' || startsWith(github.ref, 'refs/heads/release/') || startsWith(github.ref, 'refs/heads/long_lived/')) && github.sha || '' }} | ||
cancel-in-progress: true | ||
|
||
permissions: | ||
contents: read | ||
id-token: write | ||
|
||
jobs: | ||
build-c-libraries: | ||
name: C Libraries - ${{ matrix.os.name }} ${{ matrix.arch.name }} | ||
runs-on: ${{ matrix.os.runs-on[matrix.arch.matrix] }} | ||
strategy: | ||
fail-fast: false | ||
matrix: | ||
os: | ||
- name: macOS | ||
matrix: macos | ||
runs-on: | ||
arm: [macOS, ARM64] | ||
intel: [macos-11] | ||
- name: Ubuntu | ||
matrix: ubuntu | ||
runs-on: | ||
arm: [Linux, ARM64] | ||
intel: [ubuntu-latest] | ||
- name: Windows | ||
matrix: windows | ||
runs-on: | ||
intel: [windows-latest] | ||
arch: | ||
- name: ARM | ||
matrix: arm | ||
- name: Intel | ||
matrix: intel | ||
exclude: | ||
# Only partial entries are required here by GitHub Actions so generally I | ||
# only specify the `matrix:` entry. The super linter complains so for now | ||
# all entries are included to avoid that. Reported at | ||
# https://github.com/github/super-linter/issues/3016 | ||
- os: | ||
name: Windows | ||
matrix: windows | ||
runs-on: | ||
intel: [windows-latest] | ||
arch: | ||
name: ARM | ||
matrix: arm | ||
|
||
steps: | ||
- name: Clean workspace | ||
uses: Chia-Network/actions/clean-workspace@main | ||
|
||
- name: Checkout code | ||
uses: actions/checkout@v4 | ||
with: | ||
fetch-depth: 0 | ||
|
||
- name: Checkout mpir for windows | ||
if: matrix.os.matrix == 'windows' | ||
uses: actions/checkout@v4 | ||
with: | ||
repository: Chia-Network/mpir_gc_x64 | ||
fetch-depth: 1 | ||
path: mpir_gc_x64 | ||
|
||
- name: Build | ||
working-directory: src | ||
env: | ||
BUILD_VDF_CLIENT: "N" | ||
run: | | ||
cmake . -DBUILD_CHIAVDFC=ON | ||
cmake --build . | ||
- name: Upload artifacts | ||
uses: actions/upload-artifact@v3 | ||
with: | ||
name: c-libraries-${{ matrix.os.matrix }}-${{ matrix.arch.matrix }} | ||
path: ./src/lib |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,7 @@ | |
import subprocess | ||
import sys | ||
|
||
from setuptools import Command, Extension, setup, errors | ||
from setuptools import Command, Extension, setup | ||
from setuptools.command.build import build | ||
from setuptools.command.build_ext import build_ext | ||
from setuptools.command.install import install | ||
|
@@ -116,7 +116,7 @@ def build_extension(self, ext): | |
cmake_args += [ | ||
"-DCMAKE_LIBRARY_OUTPUT_DIRECTORY_{}={}".format(cfg.upper(), extdir) | ||
] | ||
if sys.maxsize > 2 ** 32: | ||
if sys.maxsize > 2**32: | ||
cmake_args += ["-A", "x64"] | ||
build_args += ["--", "/m"] | ||
else: | ||
|
@@ -131,136 +131,22 @@ def build_extension(self, ext): | |
subprocess.check_call(["cmake", "--build", "."] + build_args) | ||
|
||
|
||
class get_pybind_include(object): | ||
"""Helper class to determine the pybind11 include path | ||
The purpose of this class is to postpone importing pybind11 | ||
until it is actually installed, so that the ``get_include()`` | ||
method can be invoked.""" | ||
|
||
def __init__(self, user=False): | ||
self.user = user | ||
|
||
def __str__(self): | ||
import pybind11 | ||
|
||
return pybind11.get_include(self.user) | ||
|
||
|
||
ext_modules = [ | ||
Extension( | ||
"chiavdf", | ||
sorted( | ||
[ | ||
"src/python_bindings/fastvdf.cpp", | ||
"src/refcode/lzcnt.c", | ||
] | ||
), | ||
include_dirs=[ | ||
# Path to pybind11 headers | ||
get_pybind_include(), | ||
get_pybind_include(user=True), | ||
"mpir_gc_x64", | ||
], | ||
library_dirs=["mpir_gc_x64"], | ||
libraries=["mpir"], | ||
language="c++", | ||
build.sub_commands.append(("build_hook", lambda x: True)) # type: ignore | ||
install.sub_commands.append(("install_hook", lambda x: True)) | ||
|
||
setup( | ||
name="chiavdf", | ||
author="Florin Chirica", | ||
author_email="[email protected]", | ||
description="Chia vdf verification (wraps C++)", | ||
license="Apache License", | ||
python_requires=">=3.8", | ||
long_description=open("README.md").read(), | ||
long_description_content_type="text/markdown", | ||
url="https://github.com/Chia-Network/chiavdf", | ||
ext_modules=[CMakeExtension("chiavdf", "src")], | ||
cmdclass=dict( | ||
build_ext=CMakeBuild, install_hook=install_hook, build_hook=build_hook | ||
), | ||
] | ||
|
||
|
||
# As of Python 3.6, CCompiler has a `has_flag` method. | ||
# cf http://bugs.python.org/issue26689 | ||
def has_flag(compiler, flagname): | ||
"""Return a boolean indicating whether a flag name is supported on | ||
the specified compiler. | ||
""" | ||
import tempfile | ||
|
||
with tempfile.NamedTemporaryFile("w", suffix=".cpp") as f: | ||
f.write("int main (int argc, char **argv) { return 0; }") | ||
try: | ||
compiler.compile([f.name], extra_postargs=[flagname]) | ||
except errors.CompileError: | ||
return False | ||
return True | ||
|
||
|
||
def cpp_flag(compiler): | ||
"""Return the -std=c++[11/14/17] compiler flag. | ||
The newer version is prefered over c++11 (when it is available). | ||
""" | ||
flags = ["-std=c++17", "-std=c++14", "-std=c++11"] | ||
|
||
for flag in flags: | ||
if has_flag(compiler, flag): | ||
return flag | ||
|
||
raise RuntimeError("Unsupported compiler -- at least C++11 support " "is needed!") | ||
|
||
|
||
class BuildExt(build_ext): | ||
"""A custom build extension for adding compiler-specific options.""" | ||
|
||
c_opts = { | ||
"msvc": ["/EHsc", "/std:c++17"], | ||
"unix": [""], | ||
} | ||
l_opts = { | ||
"msvc": [], | ||
"unix": [""], | ||
} | ||
|
||
def build_extensions(self): | ||
ct = self.compiler.compiler_type | ||
opts = self.c_opts.get(ct, []) | ||
link_opts = self.l_opts.get(ct, []) | ||
if ct == "unix": | ||
opts.append('-DVERSION_INFO="%s"' % self.distribution.get_version()) | ||
opts.append(cpp_flag(self.compiler)) | ||
if has_flag(self.compiler, "-fvisibility=hidden"): | ||
opts.append("-fvisibility=hidden") | ||
elif ct == "msvc": | ||
opts.append('/DVERSION_INFO=\\"%s\\"' % self.distribution.get_version()) | ||
for ext in self.extensions: | ||
ext.extra_compile_args = opts | ||
ext.extra_link_args = link_opts | ||
build_ext.build_extensions(self) | ||
|
||
|
||
if platform.system() == "Windows": | ||
setup( | ||
name="chiavdf", | ||
author="Mariano Sorgente", | ||
author_email="[email protected]", | ||
description="Chia vdf verification (wraps C++)", | ||
license="Apache License", | ||
python_requires=">=3.8", | ||
long_description=open("README.md").read(), | ||
long_description_content_type="text/markdown", | ||
url="https://github.com/Chia-Network/chiavdf", | ||
ext_modules=ext_modules, | ||
cmdclass={"build_ext": BuildExt}, | ||
zip_safe=False, | ||
) | ||
else: | ||
build.sub_commands.append(("build_hook", lambda x: True)) # type: ignore | ||
install.sub_commands.append(("install_hook", lambda x: True)) | ||
|
||
setup( | ||
name="chiavdf", | ||
author="Florin Chirica", | ||
author_email="[email protected]", | ||
description="Chia vdf verification (wraps C++)", | ||
license="Apache License", | ||
python_requires=">=3.8", | ||
long_description=open("README.md").read(), | ||
long_description_content_type="text/markdown", | ||
url="https://github.com/Chia-Network/chiavdf", | ||
ext_modules=[CMakeExtension("chiavdf", "src")], | ||
cmdclass=dict( | ||
build_ext=CMakeBuild, install_hook=install_hook, build_hook=build_hook | ||
), | ||
zip_safe=False, | ||
) | ||
zip_safe=False, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.