PhoenixGenerator  2.0.0
Set of tools to generate code
cmakelist_generator.cpp
Go to the documentation of this file.
1 /***************************************
2  Auteur : Pierre Aubert
3  Mail : pierre.aubert@lapp.in2p3.fr
4  Licence : CeCILL-C
5 ****************************************/
6 
7 #include "cmakelist_generator.h"
8 
10 
13  PString body;
14  body += "#========================================\n";
15  body += "#\tAuteur : Pierre Aubert\n";
16  body += "#\tMail : pierre.aubert@lapp.in2p3.fr\n";
17  body += "#\tLicence : CeCILL-C\n";
18  body += "#========================================\n\n";
19 
20  body += "#========================================\n";
21  body += "#\tThis file was generated automatically by phoenix_filegenerator https://gitlab.in2p3.fr/CTA-LAPP/PHOENIX_LIBS2/PhoenixFileGenerator\n";
22  body += "#\tusing phoenix_generator library https://gitlab.in2p3.fr/CTA-LAPP/PHOENIX_LIBS2/PhoenixGenerator\n";
23  body += "#\tYou should not modify this file\n";
24  body += "#========================================\n\n";
25  return body;
26 }
27 
29 
33  if(libName == "") return "";
34  return "Find" + libName.toUpper() + ".cmake";
35 }
36 
38 
42 #ifdef __APPLE
43  if(cmakeLib.getIsShared()) return "lib" + cmakeLib.getName() + ".dylib";
44 #else
45  if(cmakeLib.getIsShared()) return "lib" + cmakeLib.getName() + ".so";
46 #endif
47  else return "lib" + cmakeLib.getName() + ".a";
48 }
49 
51 
55 void saveFindPackageHeader(std::ofstream & fs, const PString & libPrefix, const PVecPath & listHeader){
56  if(listHeader.size() == 0lu || libPrefix == "") return;
57  PString findHeaderVariable(libPrefix + "_INCLUDE_DIR");
58  fs << std::endl << std::endl << "#Find the headers of the library " << libPrefix << std::endl;
59 
60  fs << "find_path(" << findHeaderVariable << std::endl;
61  fs << "\tNAMES";
62  for(PVecPath::const_iterator it(listHeader.begin()); it != listHeader.end(); ++it){
63  fs << " " << it->replace("${CMAKE_CURRENT_BINARY_DIR}/", "").replace("${CMAKE_CURRENT_SOURCE_DIR}/", "");
64  }
65  fs << std::endl;
66 
67  bool isPHOENIXfind(libPrefix.isSameBegining("PHOENIX_") && libPrefix != "PHOENIX_");
68  PString tmpPrefix(libPrefix);
69  if(isPHOENIXfind){
70  tmpPrefix = "PHOENIX/PLIB_" + libPrefix.substr(8, libPrefix.size() - 8lu);
71  }
72 
73  fs << "\tPATHS ${PHOENIX_PREFIX}/include/" << tmpPrefix << " ${CMAKE_INSTALL_PREFIX}/include/" << tmpPrefix << std::endl;
74  fs << "\t\t${CMAKE_INCLUDE_PATH}/"<<tmpPrefix<<" /usr/include/"<<tmpPrefix<<" /usr/local/include/"<<tmpPrefix<<" $ENV{HOME}/usr/include/" << tmpPrefix << std::endl;
75  fs << ")" << std::endl << std::endl;
76 
77  fs << "if(" << findHeaderVariable << ")" << std::endl;
78  fs << "\tmessage(STATUS \"Found "<<libPrefix<<" headers : ${" << findHeaderVariable << "}\")" << std::endl;
79  fs << "else(" << findHeaderVariable << ")" << std::endl;
80  fs << "\tmessage(FATAL_ERROR \""<<libPrefix<<" headers not found\")" << std::endl;
81  fs << "endif(" << findHeaderVariable << ")" << std::endl << std::endl;
82  fs << "include_directories(${" << findHeaderVariable << "})" << std::endl << std::endl << std::endl;
83  if(isPHOENIXfind){
84  fs << "include_directories(${" << findHeaderVariable << "}/../)" << std::endl << std::endl << std::endl;
85  }
86 }
87 
89 
94 void saveFindPackageLibrary(std::ofstream & fs, const PString & libPrefix, const PString & libName, const PString & fullNameLibrary){
95  if(libPrefix == "" || fullNameLibrary == "") return;
96  PString findLibraryVariable(libPrefix + "_LIBRARY_DIR");
97  fs << std::endl << std::endl << "#Find the binary or the library " << libPrefix << std::endl;
98 
99  fs << "set(LIBRARY_NAME \""<<fullNameLibrary<<"\")" << std::endl;
100  fs << "if(APPLE)" << std::endl;
101  fs << "\tset(LIBRARY_NAME \""<<fullNameLibrary.replace(".so", ".dylib")<<"\")" << std::endl;
102  fs << "endif()" << std::endl;
103 
104  fs << "find_path(" << findLibraryVariable << std::endl;
105  fs << "\tNAMES ${LIBRARY_NAME}" << std::endl;
106  fs << "\tPATHS ${CMAKE_INSTALL_PREFIX}/lib ${CMAKE_INCLUDE_PATH}/lib /usr/lib /usr/local/lib /lib $ENV{HOME}/usr/lib" << std::endl;
107  fs << ")" << std::endl << std::endl;
108  fs << "if(" << findLibraryVariable << ")" << std::endl;
109  fs << "\tmessage(STATUS \"Found lib "<<libPrefix<<" : ${" << findLibraryVariable << "}\")" << std::endl;
110  fs << "else(" << findLibraryVariable << ")" << std::endl;
111  fs << "\tmessage(FATAL_ERROR \"lib "<<libPrefix<<" not found\")" << std::endl;
112  fs << "endif(" << findLibraryVariable << ")" << std::endl;
113  fs << "set("<<libPrefix<<"_PREFIX \"${" << findLibraryVariable << "}/..\" CACHE STRING \"installtion prefix\")" << std::endl;
114 
115  fs << "set("<<libPrefix<<" "<<libName<<" CACHE STRING \"library name\")" << std::endl;
116 
117  fs << "set("<<libPrefix<<"_FILE ${"<<findLibraryVariable<<"}/"<<fullNameLibrary<< " CACHE STRING \"library file\")" << std::endl;
118 
119  fs << "set("<<libPrefix<<"_FOUND \"YES\" CACHE STRING \"true if the library has been found\")" << std::endl;
120  fs << "link_directories(${" << findLibraryVariable << "})" << std::endl << std::endl << std::endl;
121 }
122 
124 
129 bool saveCMakeLibraryFindPackageFile(const PCMakeLibrary & cmakeLib, const PPath & extraFileName, const PPath & extraPreviousFileName){
130  if(!cmakeLib.getWantGenerateFindPackage()) return true;
131  std::ofstream fs;
132  PPath findPackageFileName(getFindPackageFileName(cmakeLib.getName()));
133 
134  fs.open(findPackageFileName.c_str());
135  if(!fs.is_open()){
136  std::cerr << "saveCMakeLibraryFindPackageFile : can't open file '" << findPackageFileName << "'" << std::endl;
137  return false;
138  }
139 
140  if(extraPreviousFileName != ""){
141  fs << extraPreviousFileName.loadFileContent() << std::endl;
142  }
143  PPath libPrefix(cmakeLib.getName().toUpper());
144  saveFindPackageHeader(fs, libPrefix, cmakeLib.getListHeaders());
145  PPath fullLibraryName(getFullLibraryName(cmakeLib));
146  saveFindPackageLibrary(fs, libPrefix, cmakeLib.getName(), fullLibraryName);
147  if(extraFileName != ""){
148  fs << extraFileName.loadFileContent() << std::endl;
149  }
150  fs.close();
151  return true;
152 }
153 
155 
159 bool saveCMakeLibrary(std::ofstream & fs, const PCMakeLibrary & cmakeLib){
160  if(cmakeLib.getName() == "" || cmakeLib.getListSources().size() == 0lu) return true;
161  if(cmakeLib.getRemoveDefinitions() != "") fs << "remove_definitions("<<cmakeLib.getRemoveDefinitions()<<")" << std::endl;
162  if(cmakeLib.getAddDefinitions() != "") fs << "add_definitions("<<cmakeLib.getAddDefinitions()<<")" << std::endl;
163  fs << "add_library(" << cmakeLib.getName();
164  if(cmakeLib.getIsShared()){
165  fs << " SHARED";
166  }
167  fs << std::endl;
168  const PVecPath & listSources = cmakeLib.getListSources();
169  for(PVecPath::const_iterator it(listSources.begin()); it != listSources.end(); ++it){
170  fs << "\t" << *it << std::endl;
171  }
172  fs << ")" << std::endl << std::endl;
173  const PVecString & listDependencies = cmakeLib.getListLibDependecies();
174  if(listDependencies.size() != 0lu){
175  fs << "target_link_libraries(" << cmakeLib.getName() << std::endl;
176  for(PVecString::const_iterator it(listDependencies.begin()); it != listDependencies.end(); ++it){
177  fs << "\t" << *it << std::endl;
178  }
179  fs << ")" << std::endl << std::endl;
180  }
181  PPath libInstall(cmakeLib.getInstallDirectory());
182  if(libInstall != ""){
183  fs << "install(TARGETS "<<cmakeLib.getName()<<" LIBRARY DESTINATION "<<libInstall<<" ARCHIVE DESTINATION "<<libInstall<<")" << std::endl;
184  }
185  const PVecPath & listHeaders = cmakeLib.getListHeaders();
186  if(listHeaders.size() != 0lu){
187  fs << "set(headers" << cmakeLib.getName()<<" " << std::endl;
188  for(PVecPath::const_iterator it(listHeaders.begin()); it != listHeaders.end(); ++it){
189  fs << "\t" << *it << std::endl;
190  }
191  fs << ")" << std::endl << std::endl;
192  fs << "install(FILES ${headers" << cmakeLib.getName()<<"} DESTINATION include/"<<cmakeLib.getName().toUpper()<<")" << std::endl;
193  }
194  if(cmakeLib.getOtherCommandAfter() != ""){fs << cmakeLib.getOtherCommandAfter();}
195  if(cmakeLib.getWantGenerateFindPackage()){
196  if(!saveCMakeLibraryFindPackageFile(cmakeLib)){
197  std::cerr << "saveCMakeLibrary : can't create the find_package cmake macro file" << std::endl;
198  return false;
199  }
200  }
201  return true;
202 }
203 
205 
209 bool saveCMakeExecutable(std::ofstream & fs, const PCMakeExecutable & cmakeExecutable){
210  if(cmakeExecutable.getName() == "" || cmakeExecutable.getListSources().size() == 0lu){
211  std::cerr << "saveCMakeExecutable : no source or name for target" << std::endl;
212  return true;
213  }
214  if(cmakeExecutable.getRemoveDefinitions() != "") fs << "remove_definitions("<<cmakeExecutable.getRemoveDefinitions()<<")" << std::endl;
215  if(cmakeExecutable.getAddDefinitions() != "") fs << "add_definitions("<<cmakeExecutable.getAddDefinitions()<<")" << std::endl;
216  fs << "add_executable(" << cmakeExecutable.getName();
217  fs << std::endl;
218  const PVecPath & listSources = cmakeExecutable.getListSources();
219  for(PVecPath::const_iterator it(listSources.begin()); it != listSources.end(); ++it){
220  fs << "\t" << *it << std::endl;
221  }
222  fs << ")" << std::endl << std::endl;
223  const PVecString & listDependencies = cmakeExecutable.getListDependecies();
224  if(listDependencies.size() != 0lu){
225  fs << "target_link_libraries(" << cmakeExecutable.getName() << std::endl;
226  for(PVecString::const_iterator it(listDependencies.begin()); it != listDependencies.end(); ++it){
227  fs << "\t" << *it << std::endl;
228  }
229  fs << ")" << std::endl << std::endl;
230  }
231  PPath libInstall(cmakeExecutable.getInstallDirectory());
232  if(libInstall != ""){
233  fs << "install(TARGETS "<<cmakeExecutable.getName()<<" RUNTIME DESTINATION "<<libInstall<<" ARCHIVE DESTINATION "<<libInstall<<")" << std::endl;
234  }
235  if(cmakeExecutable.getOtherCommandAfter() != ""){fs << cmakeExecutable.getOtherCommandAfter();}
236  fs << std::endl << std::endl;
237 // if(cmakeExecutable.getWantGenerateFindPackage()){
238 // if(!saveCMakeLibraryFindPackageFile(cmakeExecutable)){
239 // cerr << "saveCMakeLibrary : can't create the find_package cmake macro file" << std::endl;
240 // return false;
241 // }
242 // }
243  return true;
244 
245 }
246 
248 
252 bool saveCMakeUsePackages(std::ofstream & fs, const PVecString & listPackages){
253  if(listPackages.size() == 0lu) return true;
254  for(PVecString::const_iterator it(listPackages.begin()); it != listPackages.end(); ++it){
255  fs << "find_package("<<*it<<" REQUIRED)" << std::endl;
256  }
257  fs << std::endl << std::endl;
258  return true;
259 }
260 
262 
266 bool saveCMakeIncludeDirectories(std::ofstream & fs, const PVecPath & listIncludeDirectories){
267  if(listIncludeDirectories.size() == 0lu) return true;
268  fs << "include_directories" << std::endl;
269  for(PVecPath::const_iterator it(listIncludeDirectories.begin()); it != listIncludeDirectories.end(); ++it){
270  fs << *it << std::endl;
271  }
272  fs << ")" << std::endl;
273  fs << std::endl << std::endl;
274  return true;
275 }
276 
278 
282 bool saveCMakeLinkDirectories(std::ofstream & fs, const PVecPath & listLinkDirectories){
283  if(listLinkDirectories.size() == 0lu) return true;
284  fs << "link_directories" << std::endl;
285  for(PVecPath::const_iterator it(listLinkDirectories.begin()); it != listLinkDirectories.end(); ++it){
286  fs << *it << std::endl;
287  }
288  fs << ")" << std::endl;
289  fs << std::endl << std::endl;
290  return true;
291 }
292 
294 
298 bool saveCMakeListsGenerator(const PPath & fileName, const PCMakeListsGenerator & cmakeGenerator){
299  if(fileName == "") return false;
300  std::ofstream fs;
301  fs.open(fileName.c_str());
302  if(!fs.is_open()){
303  std::cerr << "saveCMakeListsGenerator : can't open file '" << fileName << "'" << std::endl;
304  return false;
305  }
306  bool b = saveCMakeListsGenerator(fs, cmakeGenerator);
307  fs.close();
308  return b;
309 }
310 
312 
316 bool saveCMakeListsGenerator(std::ofstream & fs, const PCMakeListsGenerator & cmakeGenerator){
317  if(cmakeGenerator.getProjectName() != ""){
318  fs << "project(" << cmakeGenerator.getProjectName() << ")" << std::endl;
319  fs << "cmake_minimum_required(VERSION 3.0)" << std::endl << std::endl;
320  }
321  fs << "set(PHOENIX_PREFIX \"$ENV{HOME}/usr\" CACHE STRING \"PHOENIX prefix prefix\")" << std::endl;
322  fs << "set(CMAKE_INSTALL_PREFIX \"${PHOENIX_PREFIX}\")" << std::endl;
323  if(cmakeGenerator.getCmakeModulePath() != ""){
324  fs << "set(CMAKE_MODULE_PATH "<<cmakeGenerator.getCmakeModulePath()<<")" << std::endl;
325  }
326 // fs << "add_definitions(-g -O2 -Wall -Werror)" << std::endl;
327  bool b = saveCMakeUsePackages(fs, cmakeGenerator.getListPackage());
328  b &= saveCMakeIncludeDirectories(fs, cmakeGenerator.getListIncludeDirectories());
329  b &= saveCMakeLinkDirectories(fs, cmakeGenerator.getListLinkDirecories());
330  const std::vector<PCMakeLibrary> & listLib = cmakeGenerator.getListLibraries();
331  bool haveFindMacro(false);
332  if(listLib.size() != 0lu){
333  for(std::vector<PCMakeLibrary>::const_iterator it(listLib.begin()); it != listLib.end(); ++it){
334  b &= saveCMakeLibrary(fs, *it);
335  haveFindMacro |= it->getWantGenerateFindPackage();
336  }
337  }
338  const std::vector<PCMakeExecutable> & listExecutable = cmakeGenerator.getListExecutable();
339  if(listExecutable.size() != 0lu){
340  for(std::vector<PCMakeExecutable>::const_iterator it(listExecutable.begin()); it != listExecutable.end(); ++it){
341  b &= saveCMakeExecutable(fs, *it);
342  haveFindMacro |= it->getWantGenerateFindPackage();
343  }
344  }
345  if(haveFindMacro){
346  fs << "install(FILES";
347  for(std::vector<PCMakeLibrary>::const_iterator it(listLib.begin()); it != listLib.end(); ++it){
348  if(!it->getWantGenerateFindPackage()) continue;
349  fs << " ${CMAKE_CURRENT_SOURCE_DIR}/" << getFindPackageFileName(it->getName());
350  }
351  for(std::vector<PCMakeExecutable>::const_iterator it(listExecutable.begin()); it != listExecutable.end(); ++it){
352  if(!it->getWantGenerateFindPackage()) continue;
353  fs << " ${CMAKE_CURRENT_SOURCE_DIR}/" << getFindPackageFileName(it->getName());
354  }
355  fs << " DESTINATION share/cmake)" << std::endl << std::endl;
356  }
357  if(cmakeGenerator.getTestDirectory() != ""){
358  fs << "if(SELF_TESTS_MODE)" << std::endl;
359  fs << "\tinclude(CTest)" << std::endl;
360  fs << "\tadd_subdirectory("<<cmakeGenerator.getTestDirectory()<<")" << std::endl;
361  fs << "endif()" << std::endl << std::endl;
362  }
363  fs << std::endl << std::endl << std::endl;
364  return b;
365 }
366 
367 
std::vector< PPath > PVecPath
Definition: PPath.h:75
std::vector< PString > PVecString
Definition: PString.h:96
Describes a CMake executable.
const PString & getName() const
Gets the name of the PCMakeExecutable.
const PVecString & getListDependecies() const
Gets the listDependecies of the PCMakeExecutable.
const PPath & getInstallDirectory() const
Gets the installDirectory of the PCMakeExecutable.
const PString & getOtherCommandAfter() const
Gets the otherCommandAfter of the PCMakeExecutable.
const PString & getRemoveDefinitions() const
Gets the removeDefinitions of the PCMakeExecutable.
const PString & getAddDefinitions() const
Gets the addDefinitions of the PCMakeExecutable.
const PVecPath & getListSources() const
Gets the listSources of the PCMakeExecutable.
Describes a CMake library.
const PPath & getInstallDirectory() const
Gets the installDirectory of the PCMakeLibrary.
const PVecString & getListLibDependecies() const
Gets the listLibDependecies of the PCMakeLibrary.
const PVecPath & getListHeaders() const
Gets the listHeaders of the PCMakeLibrary.
const PVecPath & getListSources() const
Gets the listSources of the PCMakeLibrary.
const PString & getName() const
Gets the name of the PCMakeLibrary.
const PString & getRemoveDefinitions() const
Gets the removeDefinitions of the PCMakeLibrary.
bool getWantGenerateFindPackage() const
Gets the wantGenerateFindPackage of the PCMakeLibrary.
bool getIsShared() const
Gets the isShared of the PCMakeLibrary.
const PString & getOtherCommandAfter() const
Gets the otherCommandAfter of the PCMakeLibrary.
const PString & getAddDefinitions() const
Gets the addDefinitions of the PCMakeLibrary.
Describes a CMakeLists.txt file.
const PPath & getCmakeModulePath() const
Gets the cmakeModulePath of the PCMakeListsGenerator.
const PVecPath & getListIncludeDirectories() const
Gets the listIncludeDirectories of the PCMakeListsGenerator.
const PVecPath & getListLinkDirecories() const
Gets the listLinkDirecories of the PCMakeListsGenerator.
const PPath & getTestDirectory() const
Gets the testDirectory of the PCMakeListsGenerator.
const std::vector< PCMakeExecutable > & getListExecutable() const
Gets the listExecutable of the PCMakeListsGenerator.
const PVecString & getListPackage() const
Gets the listPackage of the PCMakeListsGenerator.
const PString & getProjectName() const
Gets the projectName of the PCMakeListsGenerator.
const std::vector< PCMakeLibrary > & getListLibraries() const
Gets the listLibraries of the PCMakeListsGenerator.
Path of a directory or a file.
Definition: PPath.h:17
PString loadFileContent() const
Get the file content in a PString.
Definition: PPath.cpp:382
Extends the std::string.
Definition: PString.h:16
PString replace(const PString &pattern, const PString &replaceStr) const
Replace a PString into an other PString.
Definition: PString.cpp:204
PString toUpper() const
Convert std::string in upper case.
Definition: PString.cpp:639
bool isSameBegining(const PString &beginStr) const
Say if the current PString has the same begining of beginStr.
Definition: PString.cpp:306
bool saveCMakeLinkDirectories(std::ofstream &fs, const PVecPath &listLinkDirectories)
Saves the list of link directories.
PString getCMakeListsHeader()
Get the CMakeLists.txt header.
bool saveCMakeExecutable(std::ofstream &fs, const PCMakeExecutable &cmakeExecutable)
Saves a cmake executable in a file.
bool saveCMakeIncludeDirectories(std::ofstream &fs, const PVecPath &listIncludeDirectories)
Saves the list of include directories.
bool saveCMakeLibrary(std::ofstream &fs, const PCMakeLibrary &cmakeLib)
Saves a cmake library in a file.
void saveFindPackageHeader(std::ofstream &fs, const PString &libPrefix, const PVecPath &listHeader)
Saves the finder of the header.
bool saveCMakeLibraryFindPackageFile(const PCMakeLibrary &cmakeLib, const PPath &extraFileName, const PPath &extraPreviousFileName)
Saves the find_package cmake macro with the library file.
bool saveCMakeUsePackages(std::ofstream &fs, const PVecString &listPackages)
Saves the list of packages dependencies.
void saveFindPackageLibrary(std::ofstream &fs, const PString &libPrefix, const PString &libName, const PString &fullNameLibrary)
Saves the check for the binary library.
PString getFindPackageFileName(const PString &libName)
Gets the name of the find_package cmake macro with the library name.
PString getFullLibraryName(const PCMakeLibrary &cmakeLib)
Gets the name of the library binary file.
bool saveCMakeListsGenerator(const PPath &fileName, const PCMakeListsGenerator &cmakeGenerator)
Saves the PCMakeListsGenerator.