From f27229134faede76eebefe63b00fdd52ddf8ce0e Mon Sep 17 00:00:00 2001 From: Will Ayd Date: Fri, 21 Jun 2024 15:56:48 -0400 Subject: [PATCH] Add nanobind --- .github/workflows/sanity_checks.yml | 1 + ci_config.json | 12 +++ releases.json | 8 ++ subprojects/nanobind.wrap | 9 ++ subprojects/packagefiles/nanobind/meson.build | 93 +++++++++++++++++++ 5 files changed, 123 insertions(+) create mode 100644 subprojects/nanobind.wrap create mode 100644 subprojects/packagefiles/nanobind/meson.build diff --git a/.github/workflows/sanity_checks.yml b/.github/workflows/sanity_checks.yml index b6a03274d..6b31dcbed 100644 --- a/.github/workflows/sanity_checks.yml +++ b/.github/workflows/sanity_checks.yml @@ -47,6 +47,7 @@ jobs: VisualStudio: runs-on: windows-latest strategy: + fail-fast: false matrix: platform: ['x64', 'x86'] steps: diff --git a/ci_config.json b/ci_config.json index 29ab859f0..af5e5068a 100644 --- a/ci_config.json +++ b/ci_config.json @@ -666,6 +666,18 @@ "build_options": [ "nanoarrow:NANOARROW_BUILD_TESTS=true" ] + }, + "nanobind": { + "_comment": "Python fails on 32-bit MSVC because we don't have 32-bit Python", + "alpine_packages": [ + "python3-dev" + ], + "msys_packages": [ + "python" + ], + "build_options": [ + "buildtype=release" + ] }, "nativefiledialog-extended": { "alpine_packages": [ diff --git a/releases.json b/releases.json index 353d3be9a..7e6114d3b 100644 --- a/releases.json +++ b/releases.json @@ -2326,6 +2326,14 @@ "0.5.0-1" ] }, + "nanobind": { + "dependency_names": [ + "nanobind" + ], + "versions": [ + "2.0.0-1" + ] + }, "nativefiledialog-extended": { "dependency_names": [ "nativefiledialog-extended" diff --git a/subprojects/nanobind.wrap b/subprojects/nanobind.wrap new file mode 100644 index 000000000..64a32f4b7 --- /dev/null +++ b/subprojects/nanobind.wrap @@ -0,0 +1,9 @@ +[wrap-file] +directory = nanobind-2.0.0 +source_url = https://github.com/wjakob/nanobind/archive/refs/tags/v2.0.0.tar.gz +source_filename = nanobind-2.0.0.tar.gz +source_hash = 2e70727484edb7a8645d26f6a9f67352a668657c34de7a603bf9c68e5cbf8ff9 +patch_directory = nanobind + +[provide] +nanobind = nanobind_dep diff --git a/subprojects/packagefiles/nanobind/meson.build b/subprojects/packagefiles/nanobind/meson.build new file mode 100644 index 000000000..0c574cc00 --- /dev/null +++ b/subprojects/packagefiles/nanobind/meson.build @@ -0,0 +1,93 @@ +project( + 'nanobind', + 'cpp', + version: '2.0.0', + meson_version: '>=0.53.0', + license: 'BSD-3-Clause', +) + +py_mod = import('python') +py = py_mod.find_installation() +py_dep = py.dependency(embed: true) + +robin_map_dep = dependency('robin-map') + +incdir = include_directories('include') + +# Arguments are derived from nanobind_build_library function in CMake configuration +dep_compile_args = [] +dep_link_args = [] +if get_option('python.allow_limited_api') + py_version = py.language_version() + + if py_version.version_compare('>=3.12') + add_project_arguments('-DPy_LIMITED_API=0x030C0000', language: 'cpp') + endif +endif + +if get_option('buildtype') == 'shared' + # nanobind_link_options uses linker response files for either PyPy or + # CPython at this step - how do we support in Meson? + if host_machine.system() == 'darwin' + is_pypy = py_dep.get_variable('implementation_lower') == 'pypy' + if is_pypy + resp_file = meson.project_source_root() / 'cmake/darwin-ld-pypy.sym' + else + resp_file = meson.project_source_root() / 'cmake/darwin-ld-cpython.sym' + endif + add_project_link_arguments('-Wl,-dead_strip', '-Wl,x', '-Wl,-S', '-Wl,@' + resp_file) + dep_link_args += ['-Wl,-dead_strip', '-Wl,x', '-Wl,-S', '-Wl,@' + resp_file] + elif host_machine.system() != 'windows' + add_project_link_arguments('-Wl,-s', language: 'cpp') + dep_link_args += ['-Wl,-s'] + endif + + add_project_arguments('-DNB_BUILD', language: 'cpp') + dep_compile_args += ['-DNB_SHARED'] +elif host_machine.system() != 'windows' and host_machine.system() != 'darwin' + dep_compile_args += ['-ffunction-sections', '-fdata-sections'] + dep_link_args += ['-Wl,--gc-sections'] +endif + +compiler = meson.get_compiler('cpp') +if compiler.get_id() == 'msvc' + dep_compile_args += ['-D_CRT_SECURE_NO_WARNINGS'] +else + # Discussion in WrapDB PR #1556 as to whether or not this is necessary + # add_project_arguments('-fno-strict-aliasing', language: 'cpp') +endif + +# Here the CMake configuration links against Python::SABIModule on Windows +# if building an abi3 target - does Meson handle this internally? + +if get_option('buildtype') == 'release' + add_project_arguments('-DNB_COMPACT_ASSERTIONS', language: 'cpp') +endif + +nanobind_lib = static_library( + 'nanobind', + sources: [ + 'src/nb_internals.cpp', + 'src/nb_func.cpp', + 'src/nb_type.cpp', + 'src/nb_enum.cpp', + 'src/nb_ndarray.cpp', + 'src/nb_static_property.cpp', + 'src/common.cpp', + 'src/error.cpp', + 'src/trampoline.cpp', + 'src/implicit.cpp', + ], + include_directories: [incdir], + dependencies: [py_dep, robin_map_dep], + gnu_symbol_visibility: 'hidden', +) + + +nanobind_dep = declare_dependency( + include_directories: [incdir], + link_with: [nanobind_lib], + dependencies: [py_dep, robin_map_dep], + compile_args: dep_compile_args, + link_args: dep_link_args, +)