From c16a971c0fe5980a2d6da23e7d3d6d917cb9dc67 Mon Sep 17 00:00:00 2001 From: David Shah Date: Fri, 8 Jun 2018 11:17:04 +0200 Subject: python: Fixing builds as importable module Signed-off-by: David Shah --- CMakeLists.txt | 1 + common/pybindings.cc | 4 ++++ dummy/main.cc | 4 ++++ ice40/main.cc | 5 +++++ python/python_mod_test.py | 7 +++++++ 5 files changed, 21 insertions(+) create mode 100644 python/python_mod_test.py diff --git a/CMakeLists.txt b/CMakeLists.txt index 02e67e87..e6c6803d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -83,6 +83,7 @@ foreach (family ${FAMILIES}) add_executable(nextpnr-${family} ${COMMON_FILES} ${${ufamily}_FILES} ${GUI_SOURCE_FILES}) # Add the importable Python module target PYTHON_ADD_MODULE(nextpnrpy_${family} ${COMMON_FILES} ${${ufamily}_FILES}) + target_compile_definitions(nextpnrpy_${family} PRIVATE PYTHON_MODULE) # Add any new per-architecture targets here # Set ${family_targets} to the list of targets being build for this family diff --git a/common/pybindings.cc b/common/pybindings.cc index 5c86720e..454a571c 100644 --- a/common/pybindings.cc +++ b/common/pybindings.cc @@ -113,6 +113,7 @@ static wchar_t *program; void init_python(const char *executable) { +#ifndef PYTHON_MODULE program = Py_DecodeLocale(executable, NULL); if (program == NULL) { fprintf(stderr, "Fatal error: cannot decode executable filename\n"); @@ -129,12 +130,15 @@ void init_python(const char *executable) std::string perror_str = parse_python_exception(); std::cout << "Error in Python: " << perror_str << std::endl; } +#endif } void deinit_python() { +#ifndef PYTHON_MODULE Py_Finalize(); PyMem_RawFree(program); +#endif } void execute_python_file(const char *python_file) diff --git a/dummy/main.cc b/dummy/main.cc index cc6addb5..4de749b7 100644 --- a/dummy/main.cc +++ b/dummy/main.cc @@ -17,6 +17,8 @@ * */ +#ifndef PYTHON_MODULE + #include #include "design.h" #include "mainwindow.h" @@ -31,3 +33,5 @@ int main(int argc, char *argv[]) return a.exec(); } + +#endif diff --git a/ice40/main.cc b/ice40/main.cc index 9fb6b0aa..073bbae2 100644 --- a/ice40/main.cc +++ b/ice40/main.cc @@ -16,6 +16,9 @@ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. * */ + +#ifndef PYTHON_MODULE + #include #include #include @@ -263,3 +266,5 @@ int main(int argc, char *argv[]) deinit_python(); return rc; } + +#endif diff --git a/python/python_mod_test.py b/python/python_mod_test.py new file mode 100644 index 00000000..e7a8de94 --- /dev/null +++ b/python/python_mod_test.py @@ -0,0 +1,7 @@ +# Run: PYTHONPATH=. python3 python/python_mod_test.py +from nextpnrpy_ice40 import Chip, ChipArgs, iCE40Type +args = ChipArgs() +args.type = iCE40Type.HX1K +chip = Chip(args) +for wire in chip.getWires(): + print(chip.getWireName(wire)) -- cgit v1.2.3