2018-06-02 19:33:45 +08:00
|
|
|
/*
|
|
|
|
* nextpnr -- Next Generation Place and Route
|
|
|
|
*
|
2018-06-22 22:19:17 +08:00
|
|
|
* Copyright (C) 2018 Clifford Wolf <clifford@symbioticeda.com>
|
2018-06-17 17:49:57 +08:00
|
|
|
* Copyright (C) 2018 David Shah <david@symbioticeda.com>
|
2018-06-02 19:33:45 +08:00
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and/or distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
|
|
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef COMMON_PYBINDINGS_H
|
|
|
|
#define COMMON_PYBINDINGS_H
|
2018-06-07 17:41:54 +08:00
|
|
|
|
2018-06-07 19:10:53 +08:00
|
|
|
#include <Python.h>
|
2020-07-25 16:17:13 +08:00
|
|
|
#include <iostream>
|
2020-11-14 23:34:12 +08:00
|
|
|
#include <pybind11/embed.h>
|
2020-11-27 02:08:19 +08:00
|
|
|
#include <pybind11/pybind11.h>
|
2018-06-07 19:10:53 +08:00
|
|
|
#include <stdexcept>
|
|
|
|
#include <utility>
|
2018-07-01 22:40:40 +08:00
|
|
|
#include "pycontainers.h"
|
|
|
|
#include "pywrappers.h"
|
2018-06-12 20:24:59 +08:00
|
|
|
|
|
|
|
#include "nextpnr.h"
|
|
|
|
|
|
|
|
NEXTPNR_NAMESPACE_BEGIN
|
|
|
|
|
2020-07-24 00:35:18 +08:00
|
|
|
namespace py = pybind11;
|
2018-06-02 19:33:45 +08:00
|
|
|
|
2018-06-07 18:40:31 +08:00
|
|
|
std::string parse_python_exception();
|
|
|
|
|
2018-06-07 19:10:53 +08:00
|
|
|
template <typename Tn> void python_export_global(const char *name, Tn &x)
|
|
|
|
{
|
|
|
|
try {
|
2020-07-24 02:20:26 +08:00
|
|
|
py::object obj = py::cast(x, py::return_value_policy::reference);
|
2020-11-14 23:34:12 +08:00
|
|
|
py::module::import("__main__").attr(name) = obj.ptr();
|
2020-11-27 02:08:19 +08:00
|
|
|
} catch (pybind11::error_already_set &) {
|
2018-06-07 19:10:53 +08:00
|
|
|
// Parse and output the exception
|
|
|
|
std::string perror_str = parse_python_exception();
|
|
|
|
std::cout << "Error in Python: " << perror_str << std::endl;
|
|
|
|
std::terminate();
|
|
|
|
}
|
2018-06-07 18:40:31 +08:00
|
|
|
};
|
2018-06-02 19:33:45 +08:00
|
|
|
|
2020-11-14 23:34:12 +08:00
|
|
|
void init_python(const char *executable);
|
2018-06-07 18:40:31 +08:00
|
|
|
|
2018-06-07 14:56:54 +08:00
|
|
|
void deinit_python();
|
|
|
|
|
2018-06-07 18:40:31 +08:00
|
|
|
void execute_python_file(const char *python_file);
|
|
|
|
|
2018-07-02 21:59:18 +08:00
|
|
|
// Defauld IdString conversions
|
|
|
|
namespace PythonConversion {
|
|
|
|
|
|
|
|
template <> struct string_converter<IdString>
|
|
|
|
{
|
|
|
|
inline IdString from_str(Context *ctx, std::string name) { return ctx->id(name); }
|
|
|
|
|
|
|
|
inline std::string to_str(Context *ctx, IdString id) { return id.str(ctx); }
|
|
|
|
};
|
|
|
|
|
2018-07-03 16:55:32 +08:00
|
|
|
template <> struct string_converter<const IdString>
|
|
|
|
{
|
|
|
|
inline IdString from_str(Context *ctx, std::string name) { return ctx->id(name); }
|
|
|
|
|
|
|
|
inline std::string to_str(Context *ctx, IdString id) { return id.str(ctx); }
|
|
|
|
};
|
|
|
|
|
2018-07-02 21:59:18 +08:00
|
|
|
} // namespace PythonConversion
|
|
|
|
|
2018-06-12 20:24:59 +08:00
|
|
|
NEXTPNR_NAMESPACE_END
|
|
|
|
|
2018-06-02 19:33:45 +08:00
|
|
|
#endif /* end of include guard: COMMON_PYBINDINGS_HH */
|