PhoenixGenerator  2.0.0
Set of tools to generate code
cmakelist_generator.h File Reference
+ Include dependency graph for cmakelist_generator.h:
+ This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

PString getCMakeListsHeader ()
 Get the CMakeLists.txt header. More...
 
PString getFindPackageFileName (const PString &libName)
 Gets the name of the find_package cmake macro with the library name. More...
 
PString getFullLibraryName (const PCMakeLibrary &cmakeLib)
 Gets the name of the library binary file. More...
 
bool saveCMakeExecutable (std::ofstream &fs, const PCMakeExecutable &cmakeExecutable)
 Saves a cmake executable in a file. More...
 
bool saveCMakeIncludeDirectories (std::ofstream &fs, const PVecPath &listIncludeDirectories)
 Saves the list of include directories. More...
 
bool saveCMakeLibrary (std::ofstream &fs, const PCMakeLibrary &cmakeLib)
 Saves a cmake library in a file. More...
 
bool saveCMakeLibraryFindPackageFile (const PCMakeLibrary &cmakeLib, const PPath &extraFileName=PPath(), const PPath &extraPreviousFileName=PPath())
 Saves the find_package cmake macro with the library file. More...
 
bool saveCMakeLinkDirectories (std::ofstream &fs, const PVecPath &listLinkDirectories)
 Saves the list of link directories. More...
 
bool saveCMakeListsGenerator (const PPath &fileName, const PCMakeListsGenerator &cmakeGenerator)
 Saves the PCMakeListsGenerator. More...
 
bool saveCMakeListsGenerator (std::ofstream &fs, const PCMakeListsGenerator &cmakeGenerator)
 Saves the PCMakeListsGenerator. More...
 
bool saveCMakeUsePackages (std::ofstream &fs, const PVecString &listPackages)
 Saves the list of packages dependencies. More...
 

Function Documentation

◆ getCMakeListsHeader()

PString getCMakeListsHeader ( )

Get the CMakeLists.txt header.

Returns
CMakeLists.txt header

Definition at line 12 of file cmakelist_generator.cpp.

12  {
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 }
Extends the std::string.
Definition: PString.h:16

Referenced by project_generator_cmakeListsMain(), project_generator_cmakeListsSrc(), project_generator_cmakeListsTest(), project_generator_gitlabci(), project_wrapper_classTest(), project_wrapper_generator_pyprojectToml(), project_wrapper_generator_setuppy(), and saveClassTestCMakeLists().

+ Here is the caller graph for this function:

◆ getFindPackageFileName()

PString getFindPackageFileName ( const PString libName)

Gets the name of the find_package cmake macro with the library name.

Parameters
libName: library name
Returns
corresponding find_package cmake macro file name

Definition at line 32 of file cmakelist_generator.cpp.

32  {
33  if(libName == "") return "";
34  return "Find" + libName.toUpper() + ".cmake";
35 }
PString toUpper() const
Convert std::string in upper case.
Definition: PString.cpp:639

References PString::toUpper().

Referenced by saveCMakeLibraryFindPackageFile(), and saveCMakeListsGenerator().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ getFullLibraryName()

PString getFullLibraryName ( const PCMakeLibrary cmakeLib)

Gets the name of the library binary file.

Parameters
cmakeLib: library
Returns
name of the library binary file

Definition at line 41 of file cmakelist_generator.cpp.

41  {
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 }
const PString & getName() const
Gets the name of the PCMakeLibrary.
bool getIsShared() const
Gets the isShared of the PCMakeLibrary.

References PCMakeLibrary::getIsShared(), and PCMakeLibrary::getName().

Referenced by saveCMakeLibraryFindPackageFile().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ saveCMakeExecutable()

bool saveCMakeExecutable ( std::ofstream &  fs,
const PCMakeExecutable cmakeExecutable 
)

Saves a cmake executable in a file.

Parameters
[out]fs: file to complete
cmakeExecutable: cmake executable we want to save in the file
Returns
true on success, false otherwise

Definition at line 209 of file cmakelist_generator.cpp.

209  {
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 }
std::vector< PPath > PVecPath
Definition: PPath.h:75
std::vector< PString > PVecString
Definition: PString.h:96
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.
Path of a directory or a file.
Definition: PPath.h:17

References PCMakeExecutable::getAddDefinitions(), PCMakeExecutable::getInstallDirectory(), PCMakeExecutable::getListDependecies(), PCMakeExecutable::getListSources(), PCMakeExecutable::getName(), PCMakeExecutable::getOtherCommandAfter(), and PCMakeExecutable::getRemoveDefinitions().

Referenced by saveCMakeListsGenerator().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ saveCMakeIncludeDirectories()

bool saveCMakeIncludeDirectories ( std::ofstream &  fs,
const PVecPath listIncludeDirectories 
)

Saves the list of include directories.

Parameters
[out]fs: file to complete
listIncludeDirectories: list of include directories
Returns
true on success, false otherwise

Definition at line 266 of file cmakelist_generator.cpp.

266  {
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 }

Referenced by saveCMakeListsGenerator().

+ Here is the caller graph for this function:

◆ saveCMakeLibrary()

bool saveCMakeLibrary ( std::ofstream &  fs,
const PCMakeLibrary cmakeLib 
)

Saves a cmake library in a file.

Parameters
[out]fs: file to complete
cmakeLib: cmake library we want to save in the file
Returns
true on success, false otherwise

Definition at line 159 of file cmakelist_generator.cpp.

159  {
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 }
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 & getRemoveDefinitions() const
Gets the removeDefinitions of the PCMakeLibrary.
bool getWantGenerateFindPackage() const
Gets the wantGenerateFindPackage of the PCMakeLibrary.
const PString & getOtherCommandAfter() const
Gets the otherCommandAfter of the PCMakeLibrary.
const PString & getAddDefinitions() const
Gets the addDefinitions of the PCMakeLibrary.
bool saveCMakeLibraryFindPackageFile(const PCMakeLibrary &cmakeLib, const PPath &extraFileName, const PPath &extraPreviousFileName)
Saves the find_package cmake macro with the library file.

References PCMakeLibrary::getAddDefinitions(), PCMakeLibrary::getInstallDirectory(), PCMakeLibrary::getIsShared(), PCMakeLibrary::getListHeaders(), PCMakeLibrary::getListLibDependecies(), PCMakeLibrary::getListSources(), PCMakeLibrary::getName(), PCMakeLibrary::getOtherCommandAfter(), PCMakeLibrary::getRemoveDefinitions(), PCMakeLibrary::getWantGenerateFindPackage(), saveCMakeLibraryFindPackageFile(), and PString::toUpper().

Referenced by saveCMakeListsGenerator().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ saveCMakeLibraryFindPackageFile()

bool saveCMakeLibraryFindPackageFile ( const PCMakeLibrary cmakeLib,
const PPath extraFileName,
const PPath extraPreviousFileName 
)

Saves the find_package cmake macro with the library file.

Parameters
cmakeLib: library we want to generate the cmake find_package macro
extraFileName: extra filename, its content will be put after the generated find
extraPreviousFileName: extra filename, its content will be put before the generated find
Returns
true on success, false otherwise

Definition at line 129 of file cmakelist_generator.cpp.

129  {
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 }
PString loadFileContent() const
Get the file content in a PString.
Definition: PPath.cpp:382
void saveFindPackageHeader(std::ofstream &fs, const PString &libPrefix, const PVecPath &listHeader)
Saves the finder of the header.
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.

References getFindPackageFileName(), getFullLibraryName(), PCMakeLibrary::getListHeaders(), PCMakeLibrary::getName(), PCMakeLibrary::getWantGenerateFindPackage(), PPath::loadFileContent(), saveFindPackageHeader(), saveFindPackageLibrary(), and PString::toUpper().

Referenced by saveCMakeLibrary().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ saveCMakeLinkDirectories()

bool saveCMakeLinkDirectories ( std::ofstream &  fs,
const PVecPath listLinkDirectories 
)

Saves the list of link directories.

Parameters
[out]fs: file to complete
listLinkDirectories: list of link directories
Returns
true on success, false otherwise

Definition at line 282 of file cmakelist_generator.cpp.

282  {
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 }

Referenced by saveCMakeListsGenerator().

+ Here is the caller graph for this function:

◆ saveCMakeListsGenerator() [1/2]

bool saveCMakeListsGenerator ( const PPath fileName,
const PCMakeListsGenerator cmakeGenerator 
)

Saves the PCMakeListsGenerator.

Parameters
fileName: name of the file we want to write
cmakeGenerator: PCMakeListsGenerator we want to save
Returns
true on success, false otherwise

Definition at line 298 of file cmakelist_generator.cpp.

298  {
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 }
bool saveCMakeListsGenerator(const PPath &fileName, const PCMakeListsGenerator &cmakeGenerator)
Saves the PCMakeListsGenerator.

◆ saveCMakeListsGenerator() [2/2]

bool saveCMakeListsGenerator ( std::ofstream &  fs,
const PCMakeListsGenerator cmakeGenerator 
)

Saves the PCMakeListsGenerator.

Parameters
[out]fs: file to complete
cmakeGenerator: PCMakeListsGenerator we want to save
Returns
true on success, false otherwise

Definition at line 316 of file cmakelist_generator.cpp.

316  {
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 }
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.
bool saveCMakeLinkDirectories(std::ofstream &fs, const PVecPath &listLinkDirectories)
Saves the list of link directories.
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.
bool saveCMakeUsePackages(std::ofstream &fs, const PVecString &listPackages)
Saves the list of packages dependencies.

References PCMakeListsGenerator::getCmakeModulePath(), getFindPackageFileName(), PCMakeListsGenerator::getListExecutable(), PCMakeListsGenerator::getListIncludeDirectories(), PCMakeListsGenerator::getListLibraries(), PCMakeListsGenerator::getListLinkDirecories(), PCMakeListsGenerator::getListPackage(), PCMakeListsGenerator::getProjectName(), PCMakeListsGenerator::getTestDirectory(), saveCMakeExecutable(), saveCMakeIncludeDirectories(), saveCMakeLibrary(), saveCMakeLinkDirectories(), and saveCMakeUsePackages().

+ Here is the call graph for this function:

◆ saveCMakeUsePackages()

bool saveCMakeUsePackages ( std::ofstream &  fs,
const PVecString listPackages 
)

Saves the list of packages dependencies.

Parameters
[out]fs: file to complete
listPackages: list of packages dependencies
Returns
true on success, false otherwise

Definition at line 252 of file cmakelist_generator.cpp.

252  {
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 }

Referenced by saveCMakeListsGenerator().

+ Here is the caller graph for this function: