17 PString baseMacro(
"__" + className.toUpper() +
"_H__");
18 body +=
"#ifndef " + baseMacro +
"\n";
19 body +=
"#define " + baseMacro +
"\n\n";
20 body +=
"#include <string>\n\n\n";
21 body +=
"///@brief Class which loads automatically the right sub-library of "+libraryName+
"\n";
22 body +=
"class "+className+
"{\n";
23 body +=
"\tpublic:\n";
24 body +=
"\t\t"+className+
"();\n";
25 body +=
"\t\tvirtual ~"+className+
"();\n";
27 body +=
"\t\tbool load(const std::string & libName);\n";
28 body +=
"\t\tbool parseArchFile(const std::string & archfileName);\n";
29 body +=
"\t\tbool isOpen() const;\n";
30 body +=
"\t\tvoid close();\n";
32 body +=
"\t\tvoid updateFunction();\n";
33 body +=
"\tprivate:\n";
34 body +=
"\t\tvoid initialisation"+className+
"();\n";
36 body +=
"\t\t///Handle of the library we load\n";
37 body +=
"\t\tvoid* p_handle;\n";
50 body +=
"///Default constructor of "+className+
"\n";
51 body += className+
"::"+className+
"(){\n";
52 body +=
"\tinitialisation"+className+
"();\n";
53 body +=
"\tstd::cout << \""+className+
"::"+className+
"() : check current architecture\" << std::endl;\n";
54 body +=
"\t//First get the /proc/cpuinfo file\n";
55 body +=
"\tif(!parseArchFile(\"/proc/cpuinfo\")){\n";
56 body +=
"\t\t//No library found, let's load the default library\n";
57 body +=
"\t\tload(\""+defaultLib+
"\");\n";
59 body +=
"\tupdateFunction();\n";
70 body +=
"///Destructor of "+className+
"\n";
71 body +=
""+className+
"::~"+className+
"(){\n";
72 body +=
"\tclose();\n";
83 body +=
"///Load the given library\n";
84 body +=
"/**\t@param libName : name of the library to be loaded\n";
85 body +=
"* \t@return true on success, false otherwise\n";
87 body +=
"bool "+className+
"::load(const std::string & libName){\n";
88 body +=
"\tif(libName == \"\"){return false;}\n";
89 body +=
"\tp_handle = dlopen(libName.c_str(), RTLD_LAZY);\n";
90 body +=
"\tstd::cout << \""+className+
"::load : loading library '\"<<libName<<\"'\" << std::endl;\n";
91 body +=
"\tif(p_handle == NULL){\n";
92 body +=
"\t\tfprintf(stderr, \""+className+
"::load : %s\\n\", dlerror());\n";
93 body +=
"\t\treturn false;\n";
95 body +=
"\tdlerror(); //Erase an exsiting error\n";
96 body +=
"\treturn true;\n";
110 body +=
"///Parse the given architecture file\n";
111 body +=
"/**\t@param archfileName : name of the architecture file to be loaded\n";
112 body +=
"* \t@return true on success, false otherwise\n";
114 body +=
"bool "+className+
"::parseArchFile(const std::string & archfileName){\n";
115 body +=
"\tFILE * fp = fopen(archfileName.c_str(), \"r\");\n";
116 body +=
"\tif(fp == NULL){\n";
117 body +=
"\t\tstd::cerr << \"Cannot open file '\" << archfileName << \"'\" << std::endl;\n";
118 body +=
"\t\treturn false;\n";
121 body +=
"\tstd::string bufferAllFile(\"\");\n";
122 body +=
"\tint buffer;\n";
123 body +=
"\twhile(!feof(fp)){\n";
124 body +=
"\t\tbuffer = fgetc(fp);\n";
125 body +=
"\t\tif(buffer != EOF) bufferAllFile += (char)buffer;\n";
126 body +=
"\t\telse break;\n";
128 body +=
"\tif(bufferAllFile == \"\"){\n";
129 body +=
"\t\tstd::cerr << \"Empty file '\" << archfileName << \"'\" << std::endl;\n";
130 body +=
"\t\treturn false;\n\t}\n";
131 body +=
"\tconst char * procCpuInfo = bufferAllFile.c_str();\n";
148 body +=
"\t//Let's check the avalaible architectures\n";
149 body +=
"\tbool isLibFound(true);\n";
150 PString strElse(
"\t"), libraryFullDirectory(
"");
152 libraryFullDirectory = libDir +
"/";
154 for(PVecArchLib::const_reverse_iterator it(vecArchLib.rbegin()); it != vecArchLib.rend(); ++it){
155 body += strElse +
"if(strstr(procCpuInfo, \""+it->getArchitecture()+
"\") != NULL){\n";
156 PPath libName(libraryFullDirectory + it->getName().getFileName());
157 body +=
"\t\tload(\""+libName+
"\");\n";
161 body +=
"else{\n\t\tisLibFound = false;\n\t}\n";
163 body +=
"\tfclose(fp);\n";
165 body +=
"\treturn isLibFound;\n";
176 body +=
"///Say if the current library is opened or not\n";
177 body +=
"/**\t@return true if the current library is opened, false otherwise\n";
179 body +=
"bool "+className+
"::isOpen() const{\n";
180 body +=
"\treturn p_handle != NULL;\n";
191 body +=
"///Close the current library\n";
192 body +=
"void "+className+
"::close(){\n";
193 body +=
"\tif(isOpen()){\n";
194 body +=
"\t\tdlclose(p_handle);\n";
207 body +=
"///Update the functions to be used\n";
208 body +=
"void "+className+
"::updateFunction(){\n";
209 body +=
"\tif(!isOpen()){exit(-1);return;}\n";
210 body +=
"\t//Then set all the function pointers\n";
211 for(PVecSource::const_iterator it(vecSource.begin()); it != vecSource.end(); ++it){
224 body +=
"///Initialisation function of the class "+className+
"\n";
225 body +=
"void "+className+
"::initialisation"+className+
"(){\n";
226 body +=
"\tp_handle = NULL;\n";
239 const PString & className,
const PString & libDir)
242 body +=
"#include <stdio.h>\n";
243 body +=
"#include <stdlib.h>\n";
244 body +=
"#include <dlfcn.h>\n";
245 body +=
"#include <string.h>\n\n";
247 for(PVecSource::const_iterator it(vecSource.begin()); it != vecSource.end(); ++it){
248 body +=
"#include \""+it->getName()+
".h\"\n";
250 body +=
"\n#include \""+className+
".h\"\n\n";
251 body +=
"///Variable which will load automatically the right library by respect to the given architecture\n";
252 body += className +
" PROXY_LOADER;\n\n";
274 PString className(PString(libraryName +
"ProxyLoader").firstToUpper());
277 PPath outputHeaderFile(outputDir +
"/" + className +
".h");
278 PPath outputSourceFile(outputDir +
"/" + className +
".cpp");
279 if(!outputHeaderFile.saveFileContent(headerSrc)){
280 std::cerr <<
"cpp_backend : cannot save header file '"<<outputHeaderFile<<
"'" << std::endl;
283 if(!outputSourceFile.saveFileContent(sourceSrc)){
284 std::cerr <<
"cpp_backend : cannot save source file '"<<outputSourceFile<<
"'" << std::endl;
PString cpp_licenceSaveStr()
Get the licence in string.
PString getUpdateFunction(const PPath &fileName)
Get the update type name of the given file.
PString cpp_backendProxyLoaderDesonstructorSource(const PString &className)
Create the Source of the ProxyLoader class desctructor.
PString cpp_backendProxyLoaderLoadSource(const PString &className)
Create the Source of the ProxyLoader load.
bool cpp_backend_proxy(const PVecSource &vecSource, const PString &libraryName, const PVecArchLib &vecArchLib, const PPath &libDir, const PPath &outputDir)
Save ProxyLoader with the corresponding a vector of PSource in the output directory.
PString cpp_backendProxyLoaderSource(const PVecSource &vecSource, const PVecArchLib &vecArchLib, const PString &className, const PString &libDir)
Create the Source of the ProxyLoader class.
PString cpp_backendProxyLoaderParseArchFileSource(const PVecArchLib &vecArchLib, const PString &className, const PPath &libDir)
Create the Source of the ProxyLoader load.
PString cpp_backendProxyLoaderCloseSource(const PString &className)
Create the Source of the ProxyLoader close.
PString cpp_backendProxyLoaderUpdateSource(const PString &className, const PVecSource &vecSource)
Create the Source of the ProxyLoader update.
PString cpp_backendProxyLoaderConstructorSource(const PString &className, const PString &defaultLib)
Create the Source of the ProxyLoader class constructor.
PString cpp_backendProxyLoaderInitialisationSource(const PString &className)
Create the Source of the ProxyLoader initialisation.
PString cpp_backendProxyLoaderIsOpenSource(const PString &className)
Create the Source of the ProxyLoader isOpen.
PString cpp_backendProxyLoaderHeader(const PString &libraryName, const PString &className)
Create the Header definition of the ProxyLoader class.
PPath getLibraryFile(const PVecArchLib &vecArch)
Get the first non empty library file name.
std::vector< PSource > PVecSource
std::vector< PArchLib > PVecArchLib