From cf99e5c8007f00cb2f94ce6533364da069f4c9a3 Mon Sep 17 00:00:00 2001
From: mattijs <mattijs>
Date: Mon, 21 Mar 2011 17:54:20 +0000
Subject: [PATCH] ENH: codeStream: added codeLibs

---
 ReleaseNotes-dev                              |  2 +-
 doc/changes/dynamicCode.org                   | 26 +++++++-
 src/OSspecific/POSIX/POSIX.C                  | 61 ++++++++++++++++---
 .../functionEntries/codeStream/codeStream.C   |  6 +-
 .../dynamicLibrary/dynamicCode/dynamicCode.C  | 20 ------
 .../dynamicLibrary/dynamicCode/dynamicCode.H  | 13 +---
 .../dynamicCode/dynamicCodeContext.C          | 12 +++-
 .../dynamicCode/dynamicCodeContext.H          |  9 +++
 src/OpenFOAM/include/OSspecific.H             |  3 +
 .../codedFixedValueFvPatchField.C             |  8 ++-
 .../codedFixedValueFvPatchField.H             | 10 +++
 11 files changed, 122 insertions(+), 48 deletions(-)

diff --git a/ReleaseNotes-dev b/ReleaseNotes-dev
index 687dda9a452..eb4e0735942 100644
--- a/ReleaseNotes-dev
+++ b/ReleaseNotes-dev
@@ -198,7 +198,7 @@
     {
         type            codedFixedValue;
         value           uniform 0;
-        redirectType    fixedValue10;
+        redirectType    ramped;
 
         code
         #{
diff --git a/doc/changes/dynamicCode.org b/doc/changes/dynamicCode.org
index cb3a83df091..31a9f146d94 100644
--- a/doc/changes/dynamicCode.org
+++ b/doc/changes/dynamicCode.org
@@ -26,6 +26,10 @@
 
   Example: Look up dictionary entries and do some calculation
   #+BEGIN_SRC c++
+
+    // For paraFoam's sake re-load in OpenFOAM library with exported symbols
+    libs            ("libOpenFOAM.so");
+
     startTime       0;
     endTime         100;
     ..
@@ -46,7 +50,7 @@
     =code=, =codeInclude=, =codeOptions= sections (these are just strings) and
     calculates the SHA1 checksum of the contents.
   - it copies a template file
-    =(~OpenFOAM/codeTemplates/dynamicCode/codeStreamTemplate.C)= or
+    =(etc/codeTemplates/dynamicCode/codeStreamTemplate.C)= or
     =($FOAM_CODE_TEMPLATES/codeStreamTemplate.C)=, substituting all
     occurences of =code=, =codeInclude=, =codeOptions=.
   - it writes library source files to =dynamicCode/<SHA1>= and compiles
@@ -60,7 +64,7 @@
 
 * Boundary condition: =codedFixedValue=
   This uses the code from codeStream to have an in-line specialised
-  =fixedValueFvPatchScalarField=. For now only for scalars:
+  =fixedValueFvPatchField=.
   #+BEGIN_SRC c++
   outlet
   {
@@ -77,6 +81,11 @@
   It by default always includes =fvCFD.H= and adds the =finiteVolume= library to
   the include search path.
 
+  When postprocessing using paraFoam it requires one to add the used libraries
+  to the libs entry so in the system/controlDict:
+
+    libs            ("libOpenFOAM.so" "libfiniteVolume.so");
+
   A special form is where the code is not supplied in-line but instead comes
   from the =codeDict= dictionary in the =system= directory. It should contain
   a =fixedValue10= entry:
@@ -159,6 +168,19 @@
   just preserves the entries as strings (including all formatting).
 
 * Other
+  - paraFoam: paraview does not export symbols on loaded libraries
+    (more specific : it does not add 'RTLD_GLOBAL' to the dlopen flags) so
+    one will have to add the used libraries (libOpenFOAM, libfiniteVolume,
+    lib..) to the 'libs' entry in system/controlDict to prevent getting
+    an error of the form
+
+        --> FOAM FATAL IO ERROR:
+        Failed loading library "libcodeStream_3cd388ceb070a2f8b0ae61782adbc21c5687ce6f.so"
+
+    This will force re-loading
+    these libraries, this time exporting the symbols so the generated library
+    can be loaded.
+
   - parallel running not tested a lot. What about distributed data
     (i.e. non-=NFS=) parallel?
   - paraview has been patched so it will pass in RTLD_GLOBAL when loading
diff --git a/src/OSspecific/POSIX/POSIX.C b/src/OSspecific/POSIX/POSIX.C
index c2233c5545b..95372bc96bb 100644
--- a/src/OSspecific/POSIX/POSIX.C
+++ b/src/OSspecific/POSIX/POSIX.C
@@ -37,6 +37,7 @@ Description
 #include "fileStat.H"
 #include "timer.H"
 #include "IFstream.H"
+#include "DynamicList.H"
 
 #include <fstream>
 #include <cstdlib>
@@ -52,6 +53,7 @@ Description
 #include <sys/socket.h>
 #include <netdb.h>
 #include <dlfcn.h>
+#include <link.h>
 
 #include <netinet/in.h>
 
@@ -1107,10 +1109,20 @@ void* Foam::dlOpen(const fileName& lib)
 {
     if (POSIX::debug)
     {
-        Info<< "dlOpen(const fileName&)"
-            << " : dlopen of " << lib << endl;
+        std::cout<< "dlOpen(const fileName&)"
+            << " : dlopen of " << lib << std::endl;
     }
-    return ::dlopen(lib.c_str(), RTLD_LAZY|RTLD_GLOBAL);
+    void* handle = ::dlopen(lib.c_str(), RTLD_LAZY|RTLD_GLOBAL);
+
+    if (POSIX::debug)
+    {
+        std::cout
+            << "dlOpen(const fileName&)"
+            << " : dlopen of " << lib
+            << " handle " << handle << std::endl;
+    }
+
+    return handle;
 }
 
 
@@ -1118,8 +1130,9 @@ bool Foam::dlClose(void* handle)
 {
     if (POSIX::debug)
     {
-        Info<< "dlClose(void*)"
-            << " : dlclose" << endl;
+        std::cout
+            << "dlClose(void*)"
+            << " : dlclose of handle " << handle << std::endl;
     }
     return ::dlclose(handle) == 0;
 }
@@ -1129,8 +1142,9 @@ void* Foam::dlSym(void* handle, const std::string& symbol)
 {
     if (POSIX::debug)
     {
-        Info<< "dlSym(void*, const std::string&)"
-            << " : dlsym of " << symbol << endl;
+        std::cout
+            << "dlSym(void*, const std::string&)"
+            << " : dlsym of " << symbol << std::endl;
     }
     // clear any old errors - see manpage dlopen
     (void) ::dlerror();
@@ -1158,8 +1172,9 @@ bool Foam::dlSymFound(void* handle, const std::string& symbol)
     {
         if (POSIX::debug)
         {
-            Info<< "dlSymFound(void*, const std::string&)"
-                << " : dlsym of " << symbol << endl;
+            std::cout
+                << "dlSymFound(void*, const std::string&)"
+                << " : dlsym of " << symbol << std::endl;
         }
 
         // clear any old errors - see manpage dlopen
@@ -1178,4 +1193,32 @@ bool Foam::dlSymFound(void* handle, const std::string& symbol)
 }
 
 
+static int collectLibsCallback
+(
+    struct dl_phdr_info *info,
+    size_t size,
+    void *data
+)
+{
+    Foam::DynamicList<Foam::fileName>* ptr =
+        reinterpret_cast<Foam::DynamicList<Foam::fileName>*>(data);
+    ptr->append(info->dlpi_name);
+    return 0;
+}
+
+
+Foam::fileNameList Foam::dlLoaded()
+{
+    DynamicList<fileName> libs;
+    dl_iterate_phdr(collectLibsCallback, &libs);
+    if (POSIX::debug)
+    {
+        std::cout
+            << "dlLoaded()"
+            << " : determined loaded libraries :" << libs.size() << endl;
+    }
+    return libs;
+}
+
+
 // ************************************************************************* //
diff --git a/src/OpenFOAM/db/dictionary/functionEntries/codeStream/codeStream.C b/src/OpenFOAM/db/dictionary/functionEntries/codeStream/codeStream.C
index 89785d51307..e4cfbc88edf 100644
--- a/src/OpenFOAM/db/dictionary/functionEntries/codeStream/codeStream.C
+++ b/src/OpenFOAM/db/dictionary/functionEntries/codeStream/codeStream.C
@@ -136,7 +136,9 @@ bool Foam::functionEntries::codeStream::execute
                 (
                     "EXE_INC = -g \\\n"
                   + context.options()
-                  + "\n\nLIB_LIBS ="
+                  + "\n\nLIB_LIBS = \\\n"
+                  + "    -lOpenFOAM \\\n"
+                  + context.libs()
                 );
 
                 if (!dynCode.copyOrCreateFiles(true))
@@ -172,6 +174,8 @@ bool Foam::functionEntries::codeStream::execute
                 "functionEntries::codeStream::execute(..)",
                 parentDict
             )   << "Failed loading library " << libPath << nl
+                << "Did you add all libraries to the 'libs' entry"
+                << " in system/controlDict?"
                 << exit(FatalIOError);
         }
 
diff --git a/src/OpenFOAM/db/dynamicLibrary/dynamicCode/dynamicCode.C b/src/OpenFOAM/db/dynamicLibrary/dynamicCode/dynamicCode.C
index f09ddd297ff..d28494463ff 100644
--- a/src/OpenFOAM/db/dynamicLibrary/dynamicCode/dynamicCode.C
+++ b/src/OpenFOAM/db/dynamicLibrary/dynamicCode/dynamicCode.C
@@ -31,8 +31,6 @@ License
 #include "OFstream.H"
 #include "OSspecific.H"
 #include "dictionary.H"
-#include "dlLibraryTable.H"
-
 
 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
 
@@ -557,22 +555,4 @@ bool Foam::dynamicCode::upToDate(const dynamicCodeContext& context) const
 }
 
 
-// bool Foam::dynamicCode::openLibrary() const
-// {
-//     return dlLibraryTable::openLibrary(this->libPath(), false);
-// }
-//
-//
-// bool Foam::dynamicCode::closeLibrary() const
-// {
-//     return dlLibraryTable::closeLibrary(this->libPath(), false);
-// }
-//
-//
-// void* Foam::dynamicCode::findLibrary() const
-// {
-//     return dlLibraryTable::findLibrary(this->libPath());
-// }
-
-
 // ************************************************************************* //
diff --git a/src/OpenFOAM/db/dynamicLibrary/dynamicCode/dynamicCode.H b/src/OpenFOAM/db/dynamicLibrary/dynamicCode/dynamicCode.H
index 0d7a3fb969e..61a49e60cd6 100644
--- a/src/OpenFOAM/db/dynamicLibrary/dynamicCode/dynamicCode.H
+++ b/src/OpenFOAM/db/dynamicLibrary/dynamicCode/dynamicCode.H
@@ -37,10 +37,8 @@ SourceFiles
 #define dynamicCode_H
 
 #include "Tuple2.H"
-#include "SHA1Digest.H"
 #include "HashTable.H"
 #include "DynamicList.H"
-#include "dlLibraryTable.H"
 
 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
 
@@ -51,6 +49,7 @@ namespace Foam
 class dynamicCodeContext;
 class ISstream;
 class OSstream;
+class SHA1Digest;
 
 /*---------------------------------------------------------------------------*\
                         Class dynamicCode Declaration
@@ -283,16 +282,6 @@ public:
         //- Compile a libso
         bool wmakeLibso() const;
 
-//        //- Open the libPath() library
-//        bool openLibrary() const;
-//
-//        //- Close the libPath() library
-//        bool closeLibrary() const;
-//
-//        //- Find the handle of the libPath() library
-//        void* findLibrary() const;
-
-
 };
 
 
diff --git a/src/OpenFOAM/db/dynamicLibrary/dynamicCode/dynamicCodeContext.C b/src/OpenFOAM/db/dynamicLibrary/dynamicCode/dynamicCodeContext.C
index 4031f92144c..ef6231d9b35 100644
--- a/src/OpenFOAM/db/dynamicLibrary/dynamicCode/dynamicCodeContext.C
+++ b/src/OpenFOAM/db/dynamicLibrary/dynamicCode/dynamicCodeContext.C
@@ -37,7 +37,8 @@ Foam::dynamicCodeContext::dynamicCodeContext(const dictionary& dict)
     code_(stringOps::trim(dict["code"])),
     localCode_(),
     include_(),
-    options_()
+    options_(),
+    libs_()
 {
     // expand dictionary entries
     stringOps::inplaceExpand(code_, dict);
@@ -67,9 +68,16 @@ Foam::dynamicCodeContext::dynamicCodeContext(const dictionary& dict)
         stringOps::inplaceExpand(options_, dict);
     }
 
+    // optional
+    if (dict.found("codeLibs"))
+    {
+        libs_ = stringOps::trim(dict["codeLibs"]);
+        stringOps::inplaceExpand(libs_, dict);
+    }
+
     // calculate SHA1 digest from include, options, localCode, code
     OSHA1stream os;
-    os  << include_ << options_ << localCode_ << code_;
+    os  << include_ << options_ << libs_ << localCode_ << code_;
     sha1_ = os.digest();
 }
 
diff --git a/src/OpenFOAM/db/dynamicLibrary/dynamicCode/dynamicCodeContext.H b/src/OpenFOAM/db/dynamicLibrary/dynamicCode/dynamicCodeContext.H
index 01aa7e2b05b..f4a38695b7f 100644
--- a/src/OpenFOAM/db/dynamicLibrary/dynamicCode/dynamicCodeContext.H
+++ b/src/OpenFOAM/db/dynamicLibrary/dynamicCode/dynamicCodeContext.H
@@ -66,6 +66,9 @@ class dynamicCodeContext
         //- Optional "codeOptions" entry
         string options_;
 
+        //- Optional "codeLib" entry
+        string libs_;
+
         //- Calculated SHA1Digest
         SHA1Digest sha1_;
 
@@ -96,6 +99,12 @@ public:
             return options_;
         }
 
+        //- Return the code-libs
+        const string& libs() const
+        {
+            return libs_;
+        }
+
         //- Return the code
         const string& code() const
         {
diff --git a/src/OpenFOAM/include/OSspecific.H b/src/OpenFOAM/include/OSspecific.H
index ec1d5bdcc30..f01ad296476 100644
--- a/src/OpenFOAM/include/OSspecific.H
+++ b/src/OpenFOAM/include/OSspecific.H
@@ -196,6 +196,9 @@ void* dlSym(void* handle, const std::string& symbol);
 //- Report if symbol in a dlopened library could be found
 bool dlSymFound(void* handle, const std::string& symbol);
 
+//- Return all loaded libraries
+fileNameList dlLoaded();
+
 
 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
 
diff --git a/src/finiteVolume/fields/fvPatchFields/derived/codedFixedValue/codedFixedValueFvPatchField.C b/src/finiteVolume/fields/fvPatchFields/derived/codedFixedValue/codedFixedValueFvPatchField.C
index d3f13fd7a7c..208077b2627 100644
--- a/src/finiteVolume/fields/fvPatchFields/derived/codedFixedValue/codedFixedValueFvPatchField.C
+++ b/src/finiteVolume/fields/fvPatchFields/derived/codedFixedValue/codedFixedValueFvPatchField.C
@@ -37,6 +37,9 @@ License
 #include "stringOps.H"
 #include "IOdictionary.H"
 
+#include <dlfcn.h>
+#include <link.h>
+
 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
 
 template<class Type>
@@ -270,7 +273,10 @@ void Foam::codedFixedValueFvPatchField<Type>::createLibrary
                 "EXE_INC = -g \\\n"
                 "-I$(LIB_SRC)/finiteVolume/lnInclude\\\n"
               + context.options()
-              + "\n\nLIB_LIBS = "
+              + "\n\nLIB_LIBS = \\\n"
+              + "    -lOpenFOAM \\\n"
+              + "    -lfiniteVolume \\\n"
+              + context.libs()
             );
 
             if (!dynCode.copyOrCreateFiles(true))
diff --git a/src/finiteVolume/fields/fvPatchFields/derived/codedFixedValue/codedFixedValueFvPatchField.H b/src/finiteVolume/fields/fvPatchFields/derived/codedFixedValue/codedFixedValueFvPatchField.H
index 4d5bf99a79e..04cfff89300 100644
--- a/src/finiteVolume/fields/fvPatchFields/derived/codedFixedValue/codedFixedValueFvPatchField.H
+++ b/src/finiteVolume/fields/fvPatchFields/derived/codedFixedValue/codedFixedValueFvPatchField.H
@@ -80,6 +80,9 @@ SourceFiles
 
 #include "fixedValueFvPatchFields.H"
 
+#include <dlfcn.h>
+#include <link.h>
+
 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
 
 namespace Foam
@@ -119,6 +122,13 @@ class codedFixedValueFvPatchField
         //- Global loader/unloader function type
         typedef void (*loaderFunctionType)(bool);
 
+        static int collectLibsCallback
+        (
+            struct dl_phdr_info *info,
+            size_t size,
+            void *data
+        );
+
         //- Load specified library and execute globalFuncName(true)
         static void* loadLibrary
         (
-- 
GitLab