@@ -61,9 +61,29 @@ extern "C" {
61
61
#endif
62
62
63
63
static lib_h_t _python_lib = 0 ;
64
-
65
64
static lib_h_t init_python ();
66
65
66
+ /**
67
+ * The following ensures that this DPI library has a dependency
68
+ * on DPI-exported symbols. This ensures that Python is able
69
+ * to find the exports
70
+ */
71
+ int pyhdl_pi_if_RegisterTimeCB ();
72
+ int pyhdl_call_if_invoke_hdl_f (void );
73
+ int pyhdl_call_if_invoke_hdl_t (void );
74
+ int pyhdl_call_if_response_py_t (void );
75
+
76
+ void * funcs [] = {
77
+ & pyhdl_pi_if_RegisterTimeCB ,
78
+ & pyhdl_call_if_invoke_hdl_f ,
79
+ & pyhdl_call_if_invoke_hdl_t ,
80
+ & pyhdl_call_if_response_py_t ,
81
+ };
82
+
83
+ void * get_dpiexport_funcs () {
84
+ return funcs ;
85
+ }
86
+
67
87
//typedef void *PyObject;
68
88
69
89
/*******************************************************************
@@ -234,7 +254,7 @@ lib_h_t find_loaded_lib(const char *sym) {
234
254
char mapfile_path [256 ];
235
255
FILE * map_fp ;
236
256
237
- DEBUG ("find_loaded_lib(linux) %s" , sym );
257
+ DEBUG ("--> find_loaded_lib(linux) %s" , sym );
238
258
239
259
// First, try loading the executable
240
260
{
@@ -244,6 +264,7 @@ lib_h_t find_loaded_lib(const char *sym) {
244
264
DEBUG ("returning %p" , ret );
245
265
return ret ;
246
266
} else {
267
+ DEBUG ("didn't find symbol \"%s\" in the executable\n" , sym );
247
268
ret = 0 ;
248
269
}
249
270
}
@@ -299,11 +320,14 @@ lib_h_t find_loaded_lib(const char *sym) {
299
320
free (path_s );
300
321
}
301
322
323
+ DEBUG ("<-- find_loaded_lib(linux) %s %p" , sym , ret );
324
+
302
325
return ret ;
303
326
}
304
327
305
328
lib_h_t check_lib (const char * path , const char * sym ) {
306
329
lib_h_t ret = 0 ;
330
+ DEBUG ("--> check_lib(%s, %s)" , path , sym );
307
331
lib_h_t lib = dlopen (path , RTLD_LAZY );
308
332
if (lib ) {
309
333
void * sym_h = dlsym (lib , sym );
@@ -314,11 +338,53 @@ lib_h_t check_lib(const char *path, const char *sym) {
314
338
ret = lib ;
315
339
}
316
340
}
341
+ DEBUG ("<-- check_lib(%s, %s) %p" , path , sym , ret );
317
342
return ret ;
318
343
}
319
344
320
345
#endif
321
346
347
+ char * clean_env (const char * name , const char * omit ) {
348
+ const char * value = getenv (name );
349
+ char * new_value ;
350
+ const char * cp , * cpn ;
351
+ int first_entry = 1 ;
352
+
353
+ if (!value || !value [0 ]) {
354
+ return 0 ;
355
+ }
356
+
357
+ cp = value ;
358
+
359
+ new_value = (char * )malloc (strlen (name )+ strlen (value )+ 2 );
360
+ strcpy (new_value , name );
361
+ strcat (new_value , "=" );
362
+
363
+ while (cp && * cp ) {
364
+ cpn = strchr (cp , ':' );
365
+ if (!cpn ) {
366
+ cpn = cp + strlen (cp );
367
+ }
368
+
369
+ // Check if this path starts with pythonhome
370
+ if (strncmp (cp , omit , strlen (omit )) != 0 ) {
371
+ if (!first_entry ) {
372
+ strcat (new_value , ":" );
373
+ }
374
+ strncat (new_value , cp , cpn - cp );
375
+ first_entry = 0 ;
376
+ }
377
+
378
+ if (* cpn == ':' ) {
379
+ cp = cpn + 1 ;
380
+ } else {
381
+ cp = NULL ;
382
+ }
383
+ }
384
+
385
+ return new_value ;
386
+ }
387
+
322
388
#ifdef _WIN32
323
389
#else
324
390
lib_h_t find_config_python_lib () {
@@ -342,6 +408,62 @@ lib_h_t find_config_python_lib() {
342
408
fprintf (stdout , "PyHDL-IF Note: Using Python interpreter \"%s\", specified by $PYHDL_IF_PYTHON\n" ,
343
409
python );
344
410
fflush (stdout );
411
+ const char * pythonhome = getenv ("PYTHONHOME" );
412
+ if (pythonhome && pythonhome [0 ]) {
413
+ const char * pythonpath = getenv ("PYTHONPATH" );
414
+ const char * ldlibrarypath = getenv ("LD_LIBRARY_PATH" );
415
+ const char * path = getenv ("PATH" );
416
+
417
+ fprintf (stdout , "PyHDL-IF Note: Clearing PYTHONHOME and cleaning PYTHONPATH\n" );
418
+ fflush (stdout );
419
+
420
+ {
421
+ char * new_pythonpath = clean_env ("PYTHONPATH" , pythonhome );
422
+ if (new_pythonpath ) {
423
+ fprintf (stdout , "PyHDL-IF Note: Setting PYTHONPATH to \"%s\"\n" , new_pythonpath );
424
+ putenv (new_pythonpath );
425
+ }
426
+ }
427
+ {
428
+ char * new_ldlibrarypath = clean_env ("LD_LIBRARY_PATH" , pythonhome );
429
+ if (new_ldlibrarypath ) {
430
+ fprintf (stdout , "PyHDL-IF Note: Setting LD_LIBRARY_PATH to \"%s\"\n" , new_ldlibrarypath );
431
+ putenv (new_ldlibrarypath );
432
+ }
433
+ }
434
+
435
+ {
436
+ char * new_path = clean_env ("PATH" , pythonhome );
437
+ if (new_path ) {
438
+ fprintf (stdout , "PyHDL-IF Note: Setting PATH to \"%s\"\n" , new_path );
439
+ putenv (new_path );
440
+ }
441
+ }
442
+
443
+ putenv (strdup ("PYTHON=" ));
444
+ putenv (strdup ("PYTHONHOME=" ));
445
+ }
446
+
447
+ // Now, add the new Python interpreter to the PATH
448
+ {
449
+ char * python_dir = strdup (python );
450
+ char * slash = strrchr (python_dir , '/' );
451
+ char * new_path ;
452
+ if (slash ) {
453
+ * slash = 0 ; // Remove the filename
454
+ }
455
+
456
+ DEBUG ("Python directory: %s" , python_dir );
457
+ new_path = (char * )malloc (strlen (python_dir )+ strlen (getenv ("PATH" ))+ 16 );
458
+ strcpy (new_path , "PATH=" );
459
+ strcat (new_path , python_dir );
460
+ strcat (new_path , ":" );
461
+ strcat (new_path , getenv ("PATH" ));
462
+ DEBUG ("New PATH: %s" , new_path );
463
+ putenv (new_path );
464
+ free (python_dir );
465
+ }
466
+
345
467
}
346
468
347
469
args [0 ] = python ;
@@ -350,8 +472,15 @@ lib_h_t find_config_python_lib() {
350
472
args [3 ] = 0 ;
351
473
352
474
{
475
+ int i ;
476
+ extern char * * environ ;
353
477
const char * ld_library_path = getenv ("LD_LIBRARY_PATH" );
354
478
DEBUG ("LD_LIBRARY_PATH: %s" , ld_library_path ?ld_library_path :"null" );
479
+
480
+ for (i = 0 ; environ [i ]; i ++ ) {
481
+ DEBUG ("environ[%d]: %s" , i , environ [i ]);
482
+ }
483
+
355
484
}
356
485
357
486
(void )pipe (cout_pipe );
0 commit comments