python: Fixing builds as importable module

Signed-off-by: David Shah <davey1576@gmail.com>
This commit is contained in:
David Shah 2018-06-08 11:17:04 +02:00
parent 7f330af9f3
commit c16a971c0f
5 changed files with 21 additions and 0 deletions

View File

@ -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

View File

@ -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)

View File

@ -17,6 +17,8 @@
*
*/
#ifndef PYTHON_MODULE
#include <QApplication>
#include "design.h"
#include "mainwindow.h"
@ -31,3 +33,5 @@ int main(int argc, char *argv[])
return a.exec();
}
#endif

View File

@ -16,6 +16,9 @@
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*
*/
#ifndef PYTHON_MODULE
#include <QApplication>
#include <boost/program_options.hpp>
#include <fstream>
@ -263,3 +266,5 @@ int main(int argc, char *argv[])
deinit_python();
return rc;
}
#endif

View File

@ -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))