mirror of
https://github.com/lief-project/LIEF
synced 2026-06-08 15:30:44 +00:00
Switch the bindings to nanobind (and update the CI)
This commit is contained in:
+131
-77
@@ -13,106 +13,160 @@
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
#include "pyLIEF.hpp"
|
||||
#include "pyErr.hpp"
|
||||
|
||||
#include <nanobind/stl/string.h>
|
||||
#include <nanobind/stl/vector.h>
|
||||
|
||||
#include "LIEF/hash.hpp"
|
||||
#include "LIEF/Object.hpp"
|
||||
#include "LIEF/errors.hpp"
|
||||
#include "LIEF/logging.hpp"
|
||||
#include "LIEF/version.h"
|
||||
#include "pyLIEF.hpp"
|
||||
#include "pyAbstract.hpp"
|
||||
#include "LIEF/json.hpp"
|
||||
|
||||
#if defined(LIEF_ELF_SUPPORT)
|
||||
#include "ELF/pyELF.hpp"
|
||||
#endif
|
||||
|
||||
#if defined(LIEF_PE_SUPPORT)
|
||||
#include "PE/pyPE.hpp"
|
||||
#endif
|
||||
|
||||
#if defined(LIEF_MACHO_SUPPORT)
|
||||
#include "MachO/pyMachO.hpp"
|
||||
#endif
|
||||
|
||||
#if defined(LIEF_OAT_SUPPORT)
|
||||
#include "OAT/pyOAT.hpp"
|
||||
#endif
|
||||
|
||||
#if defined(LIEF_VDEX_SUPPORT)
|
||||
#include "VDEX/pyVDEX.hpp"
|
||||
#endif
|
||||
|
||||
#if defined(LIEF_DEX_SUPPORT)
|
||||
#include "DEX/pyDEX.hpp"
|
||||
#endif
|
||||
|
||||
#if defined(LIEF_ART_SUPPORT)
|
||||
#include "ART/pyART.hpp"
|
||||
#endif
|
||||
|
||||
|
||||
#include "platforms/android/pyAndroid.hpp"
|
||||
#include "platforms/pyPlatform.hpp"
|
||||
|
||||
#include "Abstract/init.hpp"
|
||||
|
||||
PYBIND11_MODULE(_lief, LIEF_module) {
|
||||
|
||||
LIEF_module.attr("__version__") = py::str(LIEF_VERSION);
|
||||
LIEF_module.attr("__tag__") = py::str(LIEF_TAG);
|
||||
LIEF_module.attr("__commit__") = py::str(LIEF_COMMIT);
|
||||
LIEF_module.attr("__is_tagged__") = py::bool_(LIEF_TAGGED);
|
||||
LIEF_module.doc() = "Python API for LIEF";
|
||||
|
||||
LIEF::init_python_platforms(LIEF_module);
|
||||
|
||||
init_LIEF_Object_class(LIEF_module);
|
||||
|
||||
init_LIEF_errors(LIEF_module);
|
||||
|
||||
init_LIEF_Logger(LIEF_module);
|
||||
|
||||
// Init the LIEF module
|
||||
LIEF::init_python_module(LIEF_module);
|
||||
|
||||
init_hash_functions(LIEF_module);
|
||||
|
||||
|
||||
// Init the ELF module
|
||||
#if defined(LIEF_ELF_SUPPORT)
|
||||
LIEF::ELF::init_python_module(LIEF_module);
|
||||
#include "ELF/init.hpp"
|
||||
#endif
|
||||
|
||||
// Init the PE module
|
||||
#if defined(LIEF_PE_SUPPORT)
|
||||
LIEF::PE::init_python_module(LIEF_module);
|
||||
#include "PE/init.hpp"
|
||||
#endif
|
||||
|
||||
// Init the MachO module
|
||||
#if defined(LIEF_MACHO_SUPPORT)
|
||||
LIEF::MachO::init_python_module(LIEF_module);
|
||||
#include "MachO/init.hpp"
|
||||
#endif
|
||||
|
||||
// Init the OAT module
|
||||
#if defined(LIEF_OAT_SUPPORT)
|
||||
LIEF::OAT::init_python_module(LIEF_module);
|
||||
#include "OAT/init.hpp"
|
||||
#endif
|
||||
|
||||
// Init the VDEX module
|
||||
#if defined(LIEF_VDEX_SUPPORT)
|
||||
LIEF::VDEX::init_python_module(LIEF_module);
|
||||
#endif
|
||||
|
||||
// Init the DEX module
|
||||
#if defined(LIEF_DEX_SUPPORT)
|
||||
LIEF::DEX::init_python_module(LIEF_module);
|
||||
#include "DEX/init.hpp"
|
||||
#endif
|
||||
|
||||
#if defined(LIEF_VDEX_SUPPORT)
|
||||
#include "VDEX/init.hpp"
|
||||
#endif
|
||||
|
||||
// Init the ART module
|
||||
#if defined(LIEF_ART_SUPPORT)
|
||||
LIEF::ART::init_python_module(LIEF_module);
|
||||
#include "ART/init.hpp"
|
||||
#endif
|
||||
|
||||
LIEF::Android::init_python_module(LIEF_module);
|
||||
|
||||
// Init util functions
|
||||
init_utils_functions(LIEF_module);
|
||||
|
||||
|
||||
init_json_functions(LIEF_module);
|
||||
namespace LIEF::py {
|
||||
void init_object(nb::module_& m) {
|
||||
nb::class_<Object>(m, "Object")
|
||||
.def("__hash__", [] (const Object& self) {
|
||||
return hash(self);
|
||||
})
|
||||
.def("__eq__",
|
||||
[] (const Object& lhs, const Object& rhs) {
|
||||
return hash(lhs) == hash(rhs);
|
||||
});
|
||||
}
|
||||
|
||||
void init_logger(nb::module_& m) {
|
||||
nb::module_ logging = m.def_submodule("logging");
|
||||
|
||||
#define PY_ENUM(x) LIEF::logging::to_string(x), x
|
||||
nb::enum_<logging::LOGGING_LEVEL>(logging, "LOGGING_LEVEL")
|
||||
.value(PY_ENUM(logging::LOGGING_LEVEL::LOG_TRACE))
|
||||
.value(PY_ENUM(logging::LOGGING_LEVEL::LOG_DEBUG))
|
||||
.value(PY_ENUM(logging::LOGGING_LEVEL::LOG_CRITICAL))
|
||||
.value(PY_ENUM(logging::LOGGING_LEVEL::LOG_ERR))
|
||||
.value(PY_ENUM(logging::LOGGING_LEVEL::LOG_WARN))
|
||||
.value(PY_ENUM(logging::LOGGING_LEVEL::LOG_INFO));
|
||||
#undef PY_ENUM
|
||||
|
||||
logging.def("disable", &logging::disable,
|
||||
"Disable the logger globally");
|
||||
|
||||
logging.def("enable", &logging::enable,
|
||||
"Enable the logger globally");
|
||||
|
||||
logging.def("set_level", &logging::set_level,
|
||||
"Change logging level", "level"_a);
|
||||
|
||||
logging.def("set_path", &logging::set_path,
|
||||
"Change the logger as a file-base logging and set its path",
|
||||
"path"_a);
|
||||
|
||||
logging.def("log", &logging::log,
|
||||
"Log a message with the LIEF's logger",
|
||||
"level"_a, "msg"_a);
|
||||
}
|
||||
|
||||
void init_hash(nb::module_& m) {
|
||||
m.def("hash", nb::overload_cast<const Object&>(&hash));
|
||||
m.def("hash", nb::overload_cast<const std::vector<uint8_t>&>(&hash));
|
||||
m.def("hash",
|
||||
[] (nb::bytes bytes) {
|
||||
const auto* begin = reinterpret_cast<const uint8_t*>(bytes.c_str());
|
||||
const auto* end = begin + bytes.size();
|
||||
return LIEF::hash(std::vector<uint8_t>(begin, end));
|
||||
});
|
||||
|
||||
m.def("hash",
|
||||
[] (const std::string& bytes) {
|
||||
const std::vector<uint8_t> data = {std::begin(bytes), std::end(bytes)};
|
||||
return hash(data);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
void init_json(nb::module_& m) {
|
||||
m.def("to_json", &LIEF::to_json);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
NB_MODULE(_lief, m) {
|
||||
m.attr("__version__") = nb::str(LIEF_VERSION);
|
||||
m.attr("__tag__") = nb::str(LIEF_TAG);
|
||||
m.attr("__commit__") = nb::str(LIEF_COMMIT);
|
||||
m.attr("__is_tagged__") = bool(LIEF_TAGGED);
|
||||
m.doc() = "LIEF Python API";
|
||||
|
||||
LIEF::py::init_platforms(m);
|
||||
LIEF::py::init_object(m);
|
||||
LIEF::py::init_errors(m);
|
||||
LIEF::py::init_logger(m);
|
||||
LIEF::py::init_hash(m);
|
||||
LIEF::py::init_json(m);
|
||||
|
||||
LIEF::py::init_abstract(m);
|
||||
|
||||
#if defined(LIEF_ELF_SUPPORT)
|
||||
LIEF::ELF::py::init(m);
|
||||
#endif
|
||||
|
||||
#if defined(LIEF_PE_SUPPORT)
|
||||
LIEF::PE::py::init(m);
|
||||
#endif
|
||||
|
||||
#if defined(LIEF_MACHO_SUPPORT)
|
||||
LIEF::MachO::py::init(m);
|
||||
#endif
|
||||
|
||||
#if defined(LIEF_OAT_SUPPORT)
|
||||
LIEF::OAT::py::init(m);
|
||||
#endif
|
||||
|
||||
#if defined(LIEF_DEX_SUPPORT)
|
||||
LIEF::DEX::py::init(m);
|
||||
#endif
|
||||
|
||||
#if defined(LIEF_VDEX_SUPPORT)
|
||||
LIEF::VDEX::py::init(m);
|
||||
#endif
|
||||
|
||||
#if defined(LIEF_ART_SUPPORT)
|
||||
LIEF::ART::py::init(m);
|
||||
#endif
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user