Skip to content
Snippets Groups Projects
Commit f9bb152c authored by Olivier BICHLER's avatar Olivier BICHLER
Browse files

Merge branch 'dev' into backend_export

parents 222c1227 1b2f2e7f
No related branches found
No related tags found
No related merge requests found
cmake_minimum_required(VERSION 3.15) cmake_minimum_required(VERSION 3.18)
set(CXX_STANDARD 14) set(CXX_STANDARD 14)
file(STRINGS "${CMAKE_SOURCE_DIR}/version.txt" version) file(STRINGS "${CMAKE_SOURCE_DIR}/version.txt" version)
...@@ -24,6 +24,7 @@ add_definitions(-DGIT_COMMIT_HASH="${GIT_COMMIT_HASH}") ...@@ -24,6 +24,7 @@ add_definitions(-DGIT_COMMIT_HASH="${GIT_COMMIT_HASH}")
# Note : project name is ${CMAKE_PROJECT_NAME} and python module name is also ${CMAKE_PROJECT_NAME} # Note : project name is ${CMAKE_PROJECT_NAME} and python module name is also ${CMAKE_PROJECT_NAME}
set(module_name _${CMAKE_PROJECT_NAME}) # target name set(module_name _${CMAKE_PROJECT_NAME}) # target name
set(pybind_module_name ${CMAKE_PROJECT_NAME}) # name of submodule for python bindings
############################################## ##############################################
# Define options # Define options
...@@ -69,16 +70,12 @@ set_property(TARGET ${module_name} PROPERTY POSITION_INDEPENDENT_CODE ON) ...@@ -69,16 +70,12 @@ set_property(TARGET ${module_name} PROPERTY POSITION_INDEPENDENT_CODE ON)
# PYTHON BINDING # PYTHON BINDING
if (PYBIND) if (PYBIND)
# Handles Python + pybind11 headers dependencies # Python binding lib is by default installed in <prefix>/python_packages/<package>/
include(PybindModuleCreation) # When installed from python, setup.py should set it to the python package dir
generate_python_binding(${CMAKE_PROJECT_NAME} ${module_name}) set(PYBIND_INSTALL_PREFIX python_packages/${pybind_module_name} CACHE PATH "Python package install prefix")
target_link_libraries(${module_name} include(PybindModuleCreation)
PUBLIC generate_python_binding(${pybind_module_name} ${module_name})
pybind11::pybind11
PRIVATE
Python::Module
)
endif() endif()
if( ${ENABLE_ASAN} ) if( ${ENABLE_ASAN} )
...@@ -102,7 +99,6 @@ target_include_directories(${module_name} ...@@ -102,7 +99,6 @@ target_include_directories(${module_name}
${CMAKE_CURRENT_SOURCE_DIR}/src ${CMAKE_CURRENT_SOURCE_DIR}/src
) )
target_link_libraries(${module_name} PUBLIC fmt::fmt)
target_compile_features(${module_name} PRIVATE cxx_std_14) target_compile_features(${module_name} PRIVATE cxx_std_14)
target_compile_options(${module_name} PRIVATE target_compile_options(${module_name} PRIVATE
...@@ -128,6 +124,12 @@ install(TARGETS ${module_name} EXPORT ${CMAKE_PROJECT_NAME}-targets ...@@ -128,6 +124,12 @@ install(TARGETS ${module_name} EXPORT ${CMAKE_PROJECT_NAME}-targets
) )
install(DIRECTORY include/ DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}) install(DIRECTORY include/ DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
if (PYBIND)
install(TARGETS ${pybind_module_name}
DESTINATION ${PYBIND_INSTALL_PREFIX}
)
endif()
#Export the targets to a script #Export the targets to a script
install(EXPORT ${CMAKE_PROJECT_NAME}-targets install(EXPORT ${CMAKE_PROJECT_NAME}-targets
FILE "${CMAKE_PROJECT_NAME}-targets.cmake" FILE "${CMAKE_PROJECT_NAME}-targets.cmake"
...@@ -159,15 +161,16 @@ install(FILES ...@@ -159,15 +161,16 @@ install(FILES
## Exporting from the build tree ## Exporting from the build tree
message(STATUS "Exporting created targets to use them in another build") message(STATUS "Exporting created targets to use them in another build")
export(EXPORT ${CMAKE_PROJECT_NAME}-targets export(EXPORT ${CMAKE_PROJECT_NAME}-targets
FILE "${CMAKE_CURRENT_BINARY_DIR}/${project}-targets.cmake") FILE "${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_PROJECT_NAME}-targets.cmake")
############################################## ##############################################
## Add test ## Add test
if(TEST) if(TEST)
if(PYBIND) if (AIDGE_REQUIRES_PYTHON AND NOT AIDGE_PYTHON_HAS_EMBED)
message(FATAL_ERROR "PYBIND and TEST are both enabled. But cannot compile with catch_2.\nChoose between pybind and Catch2 for compilation.") message(WARNING "Skipping compilation of tests: missing Python embedded interpreter")
else()
enable_testing()
add_subdirectory(unit_tests)
endif() endif()
enable_testing()
add_subdirectory(unit_tests)
endif() endif()
...@@ -23,9 +23,23 @@ Those operators can be used on any machine with an Linux OS. ...@@ -23,9 +23,23 @@ Those operators can be used on any machine with an Linux OS.
pip install . -v pip install . -v
``` ```
> **TIPS :** Use environment variables to change compilation options : > **TIPS :** Use environment variables to change compilation options :
> - `AIDGE_INSTALL` : to set the installation folder. Defaults to /usr/local/lib. :warning: This path must be identical to aidge_core install path. > - `AIDGE_INSTALL` : to set the installation folder. Defaults to `<python_prefix>/lib/libAidge`. :warning: This path must be identical to aidge_core install path.
> - `AIDGE_PYTHON_BUILD_TYPE` : to set the compilation mode to **Debug** or **Release** > - `AIDGE_PYTHON_BUILD_TYPE` : to set the compilation mode to **Debug** or **Release** or "" (for default flags). Defaults to **Release**.
> - `AIDGE_BUILD_GEN` : to set the build backend with > - `AIDGE_BUILD_GEN` : to set the build backend (for development mode) or "" for the cmake default. Default to "".
## Pip installation for development
To setup using pip in development (or editable mode), use the `--no-build-isolation -e` options to pip.
For instance run the following command in your python environnement for a typical setup :
``` bash
export AIDGE_PYTHON_BUILD_TYPE= # default flags (no debug info but fastest build time)
export AIDGE_PYTHON_BUILD_TYPE=Debug # or if one really need to debug the C++ code
pip install -U pip setuptools setuptools_scm[toml] cmake # Pre-install build requirements (refer to the pyproject.toml [build-system] section)
pip install -v --no-build-isolation -e .
```
Refer to `aidge_core/README.md` for more details on development build options.
### Standard C++ Compilation ### Standard C++ Compilation
......
@PACKAGE_INIT@
include(CMakeFindDependencyMacro)
find_dependency(aidge_core)
include(CMakeFindDependencyMacro)
include(${CMAKE_CURRENT_LIST_DIR}/aidge_backend_cpu-config-version.cmake) include(${CMAKE_CURRENT_LIST_DIR}/aidge_backend_cpu-config-version.cmake)
include(${CMAKE_CURRENT_LIST_DIR}/aidge_backend_cpu-targets.cmake) include(${CMAKE_CURRENT_LIST_DIR}/aidge_backend_cpu-targets.cmake)
function(generate_python_binding pybind_module_name target_to_bind) function(generate_python_binding pybind_module_name target_to_bind)
add_definitions(-DPYBIND)
find_package(Python COMPONENTS Interpreter Development.Module)
Include(FetchContent) Include(FetchContent)
set(PYBIND_VERSION v2.10.4) set(PYBIND_VERSION v2.10.4)
set(PYBIND11_FINDPYTHON ON)
message(STATUS "Retrieving pybind ${PYBIND_VERSION} from git") message(STATUS "Retrieving pybind ${PYBIND_VERSION} from git")
FetchContent_Declare( FetchContent_Declare(
...@@ -12,14 +13,12 @@ function(generate_python_binding pybind_module_name target_to_bind) ...@@ -12,14 +13,12 @@ function(generate_python_binding pybind_module_name target_to_bind)
GIT_TAG ${PYBIND_VERSION} # or a later release GIT_TAG ${PYBIND_VERSION} # or a later release
) )
# Use the New FindPython mode, recommanded. Requires CMake 3.15+
find_package(Python COMPONENTS Interpreter Development.Module)
FetchContent_MakeAvailable(PyBind11) FetchContent_MakeAvailable(PyBind11)
message(STATUS "Creating binding for module ${pybind_module_name}") message(STATUS "Creating binding for module ${pybind_module_name}")
file(GLOB_RECURSE pybind_src_files "python_binding/*.cpp") file(GLOB_RECURSE pybind_src_files "python_binding/*.cpp")
pybind11_add_module(${pybind_module_name} MODULE ${pybind_src_files} "NO_EXTRAS") # NO EXTRA recquired for pip install pybind11_add_module(${pybind_module_name} MODULE ${pybind_src_files} "NO_EXTRAS") # NO EXTRA recquired for pip install
target_include_directories(${pybind_module_name} PUBLIC "python_binding") target_include_directories(${pybind_module_name} PRIVATE "python_binding")
target_link_libraries(${pybind_module_name} PUBLIC ${target_to_bind}) target_link_libraries(${pybind_module_name} PRIVATE ${target_to_bind})
endfunction() endfunction()
...@@ -17,8 +17,7 @@ dynamic = ["version"] # defined in tool.setuptools_scm ...@@ -17,8 +17,7 @@ dynamic = ["version"] # defined in tool.setuptools_scm
requires = [ requires = [
"setuptools>=64", "setuptools>=64",
"setuptools_scm[toml]==7.1.0", "setuptools_scm[toml]==7.1.0",
"cmake>=3.15.3.post1", "cmake>=3.18.4.post1"
"toml"
] ]
build-backend = "setuptools.build_meta" build-backend = "setuptools.build_meta"
......
...@@ -8,17 +8,13 @@ import multiprocessing ...@@ -8,17 +8,13 @@ import multiprocessing
from math import ceil from math import ceil
import toml
from setuptools import setup, Extension from setuptools import setup, Extension
from setuptools.command.build_ext import build_ext from setuptools.command.build_ext import build_ext
def get_project_name() -> str: PROJECT_NAME = "aidge_backend_cpu"
with open(pathlib.Path().absolute() / "pyproject.toml", "r") as file:
project_toml = toml.load(file)
return project_toml["project"]["name"]
SETUP_DIR = pathlib.Path(__file__).parent
class AidgeBuildExtension(Extension): class AidgeBuildExtension(Extension):
def __init__(self, name): def __init__(self, name):
...@@ -26,6 +22,15 @@ class AidgeBuildExtension(Extension): ...@@ -26,6 +22,15 @@ class AidgeBuildExtension(Extension):
class AidgePkgBuild(build_ext): class AidgePkgBuild(build_ext):
def __init__(self, dist, *args, **kwargs):
super().__init__(dist, *args, **kwargs)
# Detect editable_mode for old versions of setuptools
if not hasattr(self, "editable_mode"):
if hasattr(dist, "commands"):
self.editable_mode = "develop" in dist.commands
else:
self.editable_mode = False
def run(self): def run(self):
#################################### ####################################
# BUILD PACKAGE # BUILD PACKAGE
...@@ -43,36 +48,35 @@ class AidgePkgBuild(build_ext): ...@@ -43,36 +48,35 @@ class AidgePkgBuild(build_ext):
if not build_lib.exists(): if not build_lib.exists():
build_lib.mkdir(parents=True, exist_ok=True) build_lib.mkdir(parents=True, exist_ok=True)
os.chdir(str(build_temp)) package_prefix = build_lib if not self.editable_mode else SETUP_DIR
pybind_install_prefix = (package_prefix / PROJECT_NAME).absolute()
compile_type = ( os.chdir(str(build_temp))
"Release"
if "AIDGE_PYTHON_BUILD_TYPE" not in os.environ
else os.environ["AIDGE_PYTHON_BUILD_TYPE"]
)
compile_type = os.environ.get("AIDGE_PYTHON_BUILD_TYPE", "Release")
install_path = ( install_path = (
os.path.join(sys.prefix, "lib", "libAidge") os.path.join(sys.prefix, "lib", "libAidge")
if "AIDGE_INSTALL" not in os.environ if "AIDGE_INSTALL" not in os.environ
else os.environ["AIDGE_INSTALL"] else os.environ["AIDGE_INSTALL"]
) )
build_gen = os.environ.get("AIDGE_BUILD_GEN", "")
# using ninja as default build system to build faster and with the same compiler as on windows build_gen_opts = (
build_gen = ( ["-G", build_gen]
["-G", os.environ["AIDGE_BUILD_GEN"]] if build_gen
if "AIDGE_BUILD_GEN" in os.environ
else [] else []
) )
test_onoff = os.environ.get("AIDGE_BUILD_TEST", "OFF")
self.spawn( self.spawn(
[ [
"cmake", "cmake",
*build_gen, *build_gen_opts,
str(cwd), str(cwd),
"-DTEST=OFF", f"-DTEST={test_onoff}",
f"-DCMAKE_INSTALL_PREFIX:PATH={install_path}", f"-DCMAKE_INSTALL_PREFIX:PATH={install_path}",
f"-DCMAKE_BUILD_TYPE={compile_type}", f"-DCMAKE_BUILD_TYPE={compile_type}",
"-DPYBIND=ON", "-DPYBIND=ON",
f"-DPYBIND_INSTALL_PREFIX:PATH={pybind_install_prefix}",
"-DCMAKE_EXPORT_COMPILE_COMMANDS=ON", "-DCMAKE_EXPORT_COMPILE_COMMANDS=ON",
"-DCOVERAGE=OFF", "-DCOVERAGE=OFF",
] ]
...@@ -85,25 +89,11 @@ class AidgePkgBuild(build_ext): ...@@ -85,25 +89,11 @@ class AidgePkgBuild(build_ext):
self.spawn(["cmake", "--install", ".", "--config", compile_type]) self.spawn(["cmake", "--install", ".", "--config", compile_type])
os.chdir(str(cwd)) os.chdir(str(cwd))
aidge_package = build_lib / (get_project_name())
# Get "aidge core" package
# ext_lib = build_temp
print(build_temp.absolute())
# Copy all shared object files from build_temp/lib to aidge_package
for root, _, files in os.walk(build_temp.absolute()):
for file in files:
if (file.endswith(".so") or file.endswith(".pyd")) and (
root != str(aidge_package.absolute())
):
currentFile = os.path.join(root, file)
shutil.copy(currentFile, str(aidge_package.absolute()))
if __name__ == "__main__": if __name__ == "__main__":
setup( setup(
include_package_data=True, include_package_data=True,
ext_modules=[AidgeBuildExtension(get_project_name())], ext_modules=[AidgeBuildExtension(PROJECT_NAME)],
cmdclass={ cmdclass={
"build_ext": AidgePkgBuild, "build_ext": AidgePkgBuild,
}, },
......
...@@ -12,7 +12,7 @@ file(GLOB_RECURSE src_files "*.cpp") ...@@ -12,7 +12,7 @@ file(GLOB_RECURSE src_files "*.cpp")
add_executable(tests${module_name} ${src_files}) add_executable(tests${module_name} ${src_files})
target_link_libraries(tests${module_name} PUBLIC ${module_name}) target_link_libraries(tests${module_name} PRIVATE ${module_name})
target_link_libraries(tests${module_name} PRIVATE Catch2::Catch2WithMain) target_link_libraries(tests${module_name} PRIVATE Catch2::Catch2WithMain)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment