Exemple #1
0
PetscErrorCode  PetscLoadDynamicLibrary(const char *name,PetscBool  *found)
{
  char           libs[PETSC_MAX_PATH_LEN],dlib[PETSC_MAX_PATH_LEN];
  PetscErrorCode ierr;

  PetscFunctionBegin;
  ierr = PetscStrncpy(libs,"${PETSC_LIB_DIR}/libpetsc",sizeof(libs));CHKERRQ(ierr);
  ierr = PetscStrlcat(libs,name,sizeof(libs));CHKERRQ(ierr);
  ierr = PetscDLLibraryRetrieve(PETSC_COMM_WORLD,libs,dlib,1024,found);CHKERRQ(ierr);
  if (*found) {
    ierr = PetscDLLibraryAppend(PETSC_COMM_WORLD,&PetscDLLibrariesLoaded,dlib);CHKERRQ(ierr);
  } else {
    ierr = PetscStrncpy(libs,"${PETSC_DIR}/${PETSC_ARCH}/lib/libpetsc",sizeof(libs));CHKERRQ(ierr);
    ierr = PetscStrlcat(libs,name,sizeof(libs));CHKERRQ(ierr);
    ierr = PetscDLLibraryRetrieve(PETSC_COMM_WORLD,libs,dlib,1024,found);CHKERRQ(ierr);
    if (*found) {
      ierr = PetscDLLibraryAppend(PETSC_COMM_WORLD,&PetscDLLibrariesLoaded,dlib);CHKERRQ(ierr);
    }
  }
  PetscFunctionReturn(0);
}
Exemple #2
0
static PetscErrorCode PetscPythonFindLibrary(char pythonexe[PETSC_MAX_PATH_LEN],char pythonlib[PETSC_MAX_PATH_LEN])
{
  const char     cmdline[] = "-c 'import sys; print(sys.exec_prefix); print(sys.version[:3])'";
  char           command[PETSC_MAX_PATH_LEN+1+sizeof(cmdline)+1];
  char           prefix[PETSC_MAX_PATH_LEN],version[8],sep[2]={PETSC_DIR_SEPARATOR, 0},*eol;
  FILE           *fp = NULL;
  char           path[PETSC_MAX_PATH_LEN+1];
  PetscBool      found = PETSC_FALSE;
  PetscErrorCode ierr;

  PetscFunctionBegin;
#if defined(PETSC_PYTHON_LIB)
  ierr = PetscStrcpy(pythonlib,PETSC_PYTHON_LIB);CHKERRQ(ierr);
  PetscFunctionReturn(0);
#endif

  /* call Python to find out the name of the Python dynamic library */
  ierr = PetscStrncpy(command,pythonexe,PETSC_MAX_PATH_LEN);CHKERRQ(ierr);
  ierr = PetscStrcat(command," ");CHKERRQ(ierr);
  ierr = PetscStrcat(command,cmdline);CHKERRQ(ierr);
#if defined(PETSC_HAVE_POPEN)
  ierr = PetscPOpen(PETSC_COMM_SELF,NULL,command,"r",&fp);CHKERRQ(ierr);
  if (!fgets(prefix,sizeof(prefix),fp)) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Python: bad output from executable: %s",pythonexe);
  if (!fgets(version,sizeof(version),fp)) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Python: bad output from executable: %s",pythonexe);
  ierr = PetscPClose(PETSC_COMM_SELF,fp,NULL);CHKERRQ(ierr);
#else
  SETERRQ(PETSC_COMM_SELF,1,"Python: Aborted due to missing popen()");
#endif
  /* remove newlines */
  ierr = PetscStrchr(prefix,'\n',&eol);CHKERRQ(ierr);
  if (eol) eol[0] = 0;
  ierr = PetscStrchr(version,'\n',&eol);CHKERRQ(ierr);
  if (eol) eol[0] = 0;

  /* test for $prefix/lib64/libpythonX.X[.so]*/
  ierr = PetscStrcpy(pythonlib,prefix);CHKERRQ(ierr);
  ierr = PetscStrcat(pythonlib,sep);CHKERRQ(ierr);
  ierr = PetscStrcat(pythonlib,"lib64");CHKERRQ(ierr);
  ierr = PetscTestDirectory(pythonlib,'r',&found);CHKERRQ(ierr);
  if (found) {
    ierr = PetscStrcat(pythonlib,sep);CHKERRQ(ierr);
    ierr = PetscStrcat(pythonlib,"libpython");CHKERRQ(ierr);
    ierr = PetscStrcat(pythonlib,version);CHKERRQ(ierr);
    ierr = PetscDLLibraryRetrieve(PETSC_COMM_SELF,pythonlib,path,PETSC_MAX_PATH_LEN,&found);CHKERRQ(ierr);
    if (found) PetscFunctionReturn(0);
  }

  /* test for $prefix/lib/libpythonX.X[.so]*/
  ierr = PetscStrcpy(pythonlib,prefix);CHKERRQ(ierr);
  ierr = PetscStrcat(pythonlib,sep);CHKERRQ(ierr);
  ierr = PetscStrcat(pythonlib,"lib");CHKERRQ(ierr);
  ierr = PetscTestDirectory(pythonlib,'r',&found);CHKERRQ(ierr);
  if (found) {
    ierr = PetscStrcat(pythonlib,sep);CHKERRQ(ierr);
    ierr = PetscStrcat(pythonlib,"libpython");CHKERRQ(ierr);
    ierr = PetscStrcat(pythonlib,version);CHKERRQ(ierr);
    ierr = PetscDLLibraryRetrieve(PETSC_COMM_SELF,pythonlib,path,PETSC_MAX_PATH_LEN,&found);CHKERRQ(ierr);
    if (found) PetscFunctionReturn(0);
  }

  /* nothing good found */
  ierr = PetscMemzero(pythonlib,PETSC_MAX_PATH_LEN);CHKERRQ(ierr);
  ierr = PetscInfo(0,"Python dynamic library not found\n");CHKERRQ(ierr);
  PetscFunctionReturn(0);
}