From 19028db20906e3252db2fed3284cf4f408244a6a Mon Sep 17 00:00:00 2001 From: drewcassidy Date: Thu, 4 Mar 2021 02:21:16 -0800 Subject: [PATCH] Finally start testing pybind stuff --- CMakeLists.txt | 2 +- setup.py | 114 ++++++++++++++++++++++++++++++++++++++ src/python/BC1Encoder.cpp | 30 ++++++++++ tools/install.fish | 6 ++ 4 files changed, 151 insertions(+), 1 deletion(-) create mode 100644 setup.py create mode 100644 src/python/BC1Encoder.cpp create mode 100755 tools/install.fish diff --git a/CMakeLists.txt b/CMakeLists.txt index 2f44920..ed37bb6 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -10,7 +10,7 @@ add_subdirectory(extern/pybind11) # Collect source files file(GLOB SOURCE_FILES "src/*.cpp" "src/BC*/*.cpp") file(GLOB HEADER_FILES "src/*.h" "src/BC*/*.h") -file(GLOB PYTHON_FILES "python/*.cpp" "python/*.h") +file(GLOB PYTHON_FILES "src/python/*.cpp" "src/python/*.h") file(GLOB TEST_FILES "src/test/*.c" "src/test/*.cpp" "src/test/*.h") # Organize source files together for some IDEs diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..77b868f --- /dev/null +++ b/setup.py @@ -0,0 +1,114 @@ +# -*- coding: utf-8 -*- +import os +import sys +import subprocess + +from setuptools import setup, Extension +from setuptools.command.build_ext import build_ext + +# Convert distutils Windows platform specifiers to CMake -A arguments +PLAT_TO_CMAKE = { + "win32": "Win32", + "win-amd64": "x64", + "win-arm32": "ARM", + "win-arm64": "ARM64", +} + + +# A CMakeExtension needs a sourcedir instead of a file list. +# The name must be the _single_ output extension from the CMake build. +# If you need multiple extensions, see scikit-build. +class CMakeExtension(Extension): + def __init__(self, name, sourcedir=""): + Extension.__init__(self, name, sources=[]) + self.sourcedir = os.path.abspath(sourcedir) + + +class CMakeBuild(build_ext): + def build_extension(self, ext): + extdir = os.path.abspath(os.path.dirname(self.get_ext_fullpath(ext.name))) + + # required for auto-detection of auxiliary "native" libs + if not extdir.endswith(os.path.sep): + extdir += os.path.sep + + cfg = "Debug" if self.debug else "Release" + + # CMake lets you override the generator - we need to check this. + # Can be set with Conda-Build, for example. + cmake_generator = os.environ.get("CMAKE_GENERATOR", "") + + # Set Python_EXECUTABLE instead if you use PYBIND11_FINDPYTHON + # EXAMPLE_VERSION_INFO shows you how to pass a value into the C++ code + # from Python. + cmake_args = [ + "-DCMAKE_LIBRARY_OUTPUT_DIRECTORY={}".format(extdir), + "-DPYTHON_EXECUTABLE={}".format(sys.executable), + "-DEXAMPLE_VERSION_INFO={}".format(self.distribution.get_version()), + "-DCMAKE_BUILD_TYPE={}".format(cfg), # not used on MSVC, but no harm + ] + build_args = [] + + if self.compiler.compiler_type != "msvc": + # Using Ninja-build since it a) is available as a wheel and b) + # multithreads automatically. MSVC would require all variables be + # exported for Ninja to pick it up, which is a little tricky to do. + # Users can override the generator with CMAKE_GENERATOR in CMake + # 3.15+. + if not cmake_generator: + cmake_args += ["-GNinja"] + + else: + + # Single config generators are handled "normally" + single_config = any(x in cmake_generator for x in {"NMake", "Ninja"}) + + # CMake allows an arch-in-generator style for backward compatibility + contains_arch = any(x in cmake_generator for x in {"ARM", "Win64"}) + + # Specify the arch if using MSVC generator, but only if it doesn't + # contain a backward-compatibility arch spec already in the + # generator name. + if not single_config and not contains_arch: + cmake_args += ["-A", PLAT_TO_CMAKE[self.plat_name]] + + # Multi-config generators have a different way to specify configs + if not single_config: + cmake_args += [ + "-DCMAKE_LIBRARY_OUTPUT_DIRECTORY_{}={}".format(cfg.upper(), extdir) + ] + build_args += ["--config", cfg] + + # Set CMAKE_BUILD_PARALLEL_LEVEL to control the parallel build level + # across all generators. + if "CMAKE_BUILD_PARALLEL_LEVEL" not in os.environ: + # self.parallel is a Python 3 only way to set parallel jobs by hand + # using -j in the build_ext call, not supported by pip or PyPA-build. + if hasattr(self, "parallel") and self.parallel: + # CMake 3.12+ only. + build_args += ["-j{}".format(self.parallel)] + + if not os.path.exists(self.build_temp): + os.makedirs(self.build_temp) + + subprocess.check_call( + ["cmake", ext.sourcedir] + cmake_args, cwd=self.build_temp + ) + subprocess.check_call( + ["cmake", "--build", "."] + build_args, cwd=self.build_temp + ) + + +# The information here can also be placed in setup.cfg - better separation of +# logic and declaration, and simpler if you include description/version in a file. +setup( + name="python_rgbcx", + version="0.0.1", + author="Andrew Cassidy", + author_email="drewcassidy@me.com", + description="", + long_description="", + ext_modules=[CMakeExtension("python_rgbcx")], + cmdclass={"build_ext": CMakeBuild}, + zip_safe=False, +) diff --git a/src/python/BC1Encoder.cpp b/src/python/BC1Encoder.cpp new file mode 100644 index 0000000..40c6bea --- /dev/null +++ b/src/python/BC1Encoder.cpp @@ -0,0 +1,30 @@ +/* Python-rgbcx Texture Compression Library + Copyright (C) 2021 Andrew Cassidy + Partially derived from rgbcx.h written by Richard Geldreich + and licenced under the public domain + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with this program. If not, see . + */ + +#include +#include "../BC1/BC1Encoder.h" + +#define STRINGIFY(x) #x +#define MACRO_STRINGIFY(x) STRINGIFY(x) + +namespace py = pybind11; + +PYBIND11_MODULE(python_rgbcx, m) { + m.doc() = "More Stuff"; +} \ No newline at end of file diff --git a/tools/install.fish b/tools/install.fish new file mode 100755 index 0000000..462ddc6 --- /dev/null +++ b/tools/install.fish @@ -0,0 +1,6 @@ +#! /usr/bin/bash + +set dir (status dirname) + +source "$dir"/../env/bin/activate.fish +pip install "$dir"/.. \ No newline at end of file