GCC Code Coverage Report


Directory: ./
File: src/cmakelist_generator.cpp
Date: 2025-04-25 19:10:50
Exec Total Coverage
Lines: 13 211 6.2%
Branches: 10 858 1.2%

Line Branch Exec Source
1 /***************************************
2 Auteur : Pierre Aubert
3 Mail : pierre.aubert@lapp.in2p3.fr
4 Licence : CeCILL-C
5 ****************************************/
6
7 #include "cmakelist_generator.h"
8
9 ///Get the CMakeLists.txt header
10 /** @return CMakeLists.txt header
11 */
12 59 PString getCMakeListsHeader(){
13 59 PString body;
14
1/1
✓ Branch 1 taken 59 times.
59 body += "#========================================\n";
15
1/1
✓ Branch 1 taken 59 times.
59 body += "#\tAuteur : Pierre Aubert\n";
16
1/1
✓ Branch 1 taken 59 times.
59 body += "#\tMail : pierre.aubert@lapp.in2p3.fr\n";
17
1/1
✓ Branch 1 taken 59 times.
59 body += "#\tLicence : CeCILL-C\n";
18
1/1
✓ Branch 1 taken 59 times.
59 body += "#========================================\n\n";
19
20
1/1
✓ Branch 1 taken 59 times.
59 body += "#========================================\n";
21
1/1
✓ Branch 1 taken 59 times.
59 body += "#\tThis file was generated automatically by phoenix_filegenerator https://gitlab.in2p3.fr/CTA-LAPP/PHOENIX_LIBS2/PhoenixFileGenerator\n";
22
1/1
✓ Branch 1 taken 59 times.
59 body += "#\tusing phoenix_generator library https://gitlab.in2p3.fr/CTA-LAPP/PHOENIX_LIBS2/PhoenixGenerator\n";
23
1/1
✓ Branch 1 taken 59 times.
59 body += "#\tYou should not modify this file\n";
24
1/1
✓ Branch 1 taken 59 times.
59 body += "#========================================\n\n";
25 59 return body;
26 }
27
28 ///Gets the name of the find_package cmake macro with the library name
29 /** @param libName : library name
30 * @return corresponding find_package cmake macro file name
31 */
32 PString getFindPackageFileName(const PString & libName){
33 if(libName == "") return "";
34 return "Find" + libName.toUpper() + ".cmake";
35 }
36
37 ///Gets the name of the library binary file
38 /** @param cmakeLib : library
39 * @return name of the library binary file
40 */
41 PString getFullLibraryName(const PCMakeLibrary & cmakeLib){
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
50 ///Saves the finder of the header
51 /** @param[out] fs : file to complete
52 * @param libPrefix : prefix of the variables
53 * @param listHeader : list of the header we want to search
54 */
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
88 ///Saves the check for the binary library
89 /** @param[out] fs : file to complete
90 * @param libPrefix : prefix of the cmake variables
91 * @param libName : name of the library (target name without lib or .so)
92 * @param fullNameLibrary : name of the binary library
93 */
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
123 ///Saves the find_package cmake macro with the library file
124 /** @param cmakeLib : library we want to generate the cmake find_package macro
125 * @param extraFileName : extra filename, its content will be put after the generated find
126 * @param extraPreviousFileName : extra filename, its content will be put before the generated find
127 * @return true on success, false otherwise
128 */
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
154 ///Saves a cmake library in a file
155 /** @param[out] fs : file to complete
156 * @param cmakeLib : cmake library we want to save in the file
157 * @return true on success, false otherwise
158 */
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
204 ///Saves a cmake executable in a file
205 /** @param[out] fs : file to complete
206 * @param cmakeExecutable : cmake executable we want to save in the file
207 * @return true on success, false otherwise
208 */
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
247 ///Saves the list of packages dependencies
248 /** @param[out] fs : file to complete
249 * @param listPackages : list of packages dependencies
250 * @return true on success, false otherwise
251 */
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
261 ///Saves the list of include directories
262 /** @param[out] fs : file to complete
263 * @param listIncludeDirectories : list of include directories
264 * @return true on success, false otherwise
265 */
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
277 ///Saves the list of link directories
278 /** @param[out] fs : file to complete
279 * @param listLinkDirectories : list of link directories
280 * @return true on success, false otherwise
281 */
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
293 ///Saves the PCMakeListsGenerator
294 /** @param fileName : name of the file we want to write
295 * @param cmakeGenerator : PCMakeListsGenerator we want to save
296 * @return true on success, false otherwise
297 */
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
311 ///Saves the PCMakeListsGenerator
312 /** @param[out] fs : file to complete
313 * @param cmakeGenerator : PCMakeListsGenerator we want to save
314 * @return true on success, false otherwise
315 */
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
368