-properly handle dependencies
[reactos.git] / reactos / tools / rbuild / backend / codeblocks / codeblocks.cpp
1 /*
2 * Copyright (C) 2006 Christoph von Wittich
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17 */
18 #ifdef _MSC_VER
19 #pragma warning ( disable : 4786 )
20 #endif//_MSC_VER
21
22 #include <iostream>
23 #include <fstream>
24 #include <string>
25 #include <vector>
26
27 #include <stdio.h>
28
29 #include "codeblocks.h"
30 #include "../mingw/mingw.h"
31
32 using std::string;
33 using std::vector;
34 using std::ifstream;
35
36 #ifdef OUT
37 #undef OUT
38 #endif//OUT
39
40
41 static class CBFactory : public Backend::Factory
42 {
43 public:
44
45 CBFactory() : Factory("CB", "Code::Blocks") {}
46 Backend *operator() (Project &project,
47 Configuration& configuration)
48 {
49 return new CBBackend(project, configuration);
50 }
51
52 } factory;
53
54
55 CBBackend::CBBackend(Project &project,
56 Configuration& configuration) : Backend(project, configuration)
57 {
58 m_unitCount = 0;
59 }
60
61 void CBBackend::Process()
62 {
63
64 while ( m_configurations.size () > 0 )
65 {
66 const CBConfiguration* cfg = m_configurations.back();
67 m_configurations.pop_back();
68 delete cfg;
69 }
70
71 m_configurations.push_back ( new CBConfiguration( Debug ));
72 m_configurations.push_back ( new CBConfiguration( Release ));
73
74 string filename_wrkspace ( ProjectNode.name );
75 filename_wrkspace += "_auto.workspace";
76
77 printf ( "Creating Code::Blocks workspace: %s\n", filename_wrkspace.c_str() );
78
79 ProcessModules();
80 m_wrkspaceFile = fopen ( filename_wrkspace.c_str(), "wb" );
81
82 if ( !m_wrkspaceFile )
83 {
84 printf ( "Could not create file '%s'.\n", filename_wrkspace.c_str() );
85 return;
86 }
87
88 _generate_workspace ( m_wrkspaceFile );
89
90 fclose ( m_wrkspaceFile );
91 printf ( "Done.\n" );
92 }
93
94 void CBBackend::ProcessModules()
95 {
96 for(size_t i = 0; i < ProjectNode.modules.size(); i++)
97 {
98 Module &module = *ProjectNode.modules[i];
99 MingwAddImplicitLibraries( module );
100 _generate_cbproj ( module );
101 }
102 }
103
104 static bool FileExists(string &filename)
105 {
106 ifstream file(filename.c_str());
107
108 if(!file.is_open())
109 return false;
110
111 file.close();
112 return true;
113 }
114
115 void CBBackend::ProcessFile(string &filepath)
116 {
117 // Remove the .\ at the start of the filenames
118 if ( filepath[0] == '.' && strchr ( "/\\", filepath[1] ) )
119 filepath.erase(0, 2);
120
121 if(!FileExists(filepath))
122 return;
123
124 // Change the \ to /
125 for(size_t i = 0; i < filepath.length(); i++)
126 {
127 if(filepath[i] == '\\')
128 filepath[i] = '/';
129 }
130
131 // Remove the filename from the path
132 string folder = "";
133
134 size_t pos = filepath.rfind(string("/"), filepath.length() - 1);
135
136 if(pos != string::npos)
137 {
138 folder = filepath;
139 folder.erase(pos, folder.length() - pos);
140 }
141
142 FileUnit fileUnit;
143 fileUnit.filename = filepath;
144 fileUnit.folder = folder;
145
146 m_fileUnits.push_back(fileUnit);
147
148 if(folder != "")
149 AddFolders(folder);
150
151 m_unitCount++;
152 }
153
154 bool CBBackend::CheckFolderAdded(string &folder)
155 {
156 for(size_t i = 0; i < m_folders.size(); i++)
157 {
158 if(m_folders[i] == folder)
159 return true;
160 }
161
162 return false;
163 }
164
165 void CBBackend::AddFolders(string &folder)
166 {
167 // Check if this folder was already added. true if it was, false otherwise.
168 if(CheckFolderAdded(folder))
169 return;
170
171 m_folders.push_back(folder);
172
173 size_t pos = folder.rfind(string("/"), folder.length() - 1);
174
175 if(pos == string::npos)
176 return;
177
178 folder.erase(pos, folder.length() - pos);
179 AddFolders(folder);
180 }
181
182 void CBBackend::OutputFolders()
183 {
184 #if 0
185 m_devFile << "Folders=";
186
187 for(size_t i = 0; i < m_folders.size(); i++)
188 {
189 if(i > 0)
190 m_devFile << ",";
191
192 m_devFile << m_folders[i];
193 }
194 #endif
195 }
196
197 std::string
198 CBBackend::CbpFileName ( const Module& module ) const
199 {
200 return DosSeparator(
201 ReplaceExtension ( module.GetPath(), + "_auto.cbp" )
202 );
203 }
204
205 std::string
206 CBBackend::LayoutFileName ( const Module& module ) const
207 {
208 return DosSeparator(
209 ReplaceExtension ( module.GetPath(), + "_auto.layout" )
210 );
211 }
212
213 std::string
214 CBBackend::DependFileName ( const Module& module ) const
215 {
216 return DosSeparator(
217 ReplaceExtension ( module.GetPath(), + "_auto.depend" )
218 );
219 }
220
221 void
222 CBBackend::_get_object_files ( const Module& module, vector<string>& out) const
223 {
224 string basepath = module.GetBasePath ();
225 size_t i;
226 string intenv = Environment::GetIntermediatePath () + "\\" + basepath + "\\";
227 string outenv = Environment::GetOutputPath () + "\\" + basepath + "\\";
228
229 vector<string> cfgs;
230
231 if ( configuration.UseConfigurationInPath )
232 {
233 cfgs.push_back ( intenv + "Debug" );
234 cfgs.push_back ( intenv + "Release" );
235 cfgs.push_back ( outenv + "Debug" );
236 cfgs.push_back ( outenv + "Release" );
237 }
238 else
239 {
240 cfgs.push_back ( intenv );
241 cfgs.push_back ( outenv );
242 }
243
244 vector<const IfableData*> ifs_list;
245 ifs_list.push_back ( &module.project.non_if_data );
246 ifs_list.push_back ( &module.non_if_data );
247 while ( ifs_list.size () )
248 {
249 const IfableData& data = *ifs_list.back();
250 ifs_list.pop_back();
251 const vector<File*>& files = data.files;
252 for ( i = 0; i < files.size (); i++ )
253 {
254 string file = files[i]->name;
255 string::size_type pos = file.find_last_of ("\\");
256 if ( pos != string::npos )
257 file.erase ( 0, pos+1 );
258 if ( !stricmp ( Right(file,3).c_str(), ".rc" ) )
259 file = ReplaceExtension ( file, ".res" );
260 else
261 file = ReplaceExtension ( file, ".obj" );
262 for ( size_t j = 0; j < cfgs.size () / 2; j++ )
263 out.push_back ( cfgs[j] + "\\" + file );
264 }
265
266 }
267 }
268
269 void
270 CBBackend::_clean_project_files ( void )
271 {
272 for ( size_t i = 0; i < ProjectNode.modules.size(); i++ )
273 {
274 Module& module = *ProjectNode.modules[i];
275 vector<string> out;
276 printf("Cleaning project %s %s\n", module.name.c_str (), module.GetBasePath ().c_str () );
277
278 string basepath = module.GetBasePath ();
279 remove ( CbpFileName ( module ).c_str () );
280 remove ( DependFileName ( module ).c_str () );
281 remove ( LayoutFileName ( module ).c_str () );
282
283 _get_object_files ( module, out );
284 for ( size_t j = 0; j < out.size (); j++)
285 {
286 //printf("Cleaning file %s\n", out[j].c_str () );
287 remove ( out[j].c_str () );
288 }
289 }
290
291 string filename_wrkspace = ProjectNode.name + ".workspace";
292
293 remove ( filename_wrkspace.c_str () );
294 }
295
296 void
297 CBBackend::_generate_workspace ( FILE* OUT )
298 {
299 fprintf ( OUT, "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\" ?>\r\n" );
300 fprintf ( OUT, "<CodeBlocks_workspace_file>\r\n" );
301 fprintf ( OUT, "\t<Workspace title=\"ReactOS\">\r\n" );
302 for ( size_t i = 0; i < ProjectNode.modules.size(); i++ )
303 {
304 Module& module = *ProjectNode.modules[i];
305
306 std::string Cbp_file = CbpFileName ( module );
307 fprintf ( OUT, "\t\t<Project filename=\"%s\">\r\n", Cbp_file.c_str());
308
309 /* dependencies */
310 vector<const IfableData*> ifs_list;
311 ifs_list.push_back ( &module.project.non_if_data );
312 ifs_list.push_back ( &module.non_if_data );
313 while ( ifs_list.size() )
314 {
315 const IfableData& data = *ifs_list.back();
316 ifs_list.pop_back();
317 const vector<Library*>& libs = data.libraries;
318 for ( size_t j = 0; j < libs.size(); j++ )
319 fprintf ( OUT, "\t\t\t<Depends filename=\"%s\\%s_auto.cbp\" />\r\n", libs[j]->importedModule->GetBasePath().c_str(), libs[j]->name.c_str() );
320 }
321 fprintf ( OUT, "\t\t</Project>\r\n" );
322
323 }
324 fprintf ( OUT, "\t</Workspace>\r\n" );
325 fprintf ( OUT, "</CodeBlocks_workspace_file>\r\n" );
326 }
327
328 void
329 CBBackend::_generate_cbproj ( const Module& module )
330 {
331
332 size_t i;
333
334 string cbproj_file = CbpFileName(module);
335 string outdir;
336 string intdir;
337 string path_basedir = module.GetPathToBaseDir ();
338 string intenv = Environment::GetIntermediatePath ();
339 string outenv = Environment::GetOutputPath ();
340 string module_type = GetExtension(module.GetTargetName());
341 string cbproj_path = module.GetBasePath();
342 string CompilerVar;
343 string baseaddr;
344 string project_linker_flags = "-Wl,--enable-stdcall-fixup ";
345 project_linker_flags += GenerateProjectLinkerFlags();
346
347 bool lib = (module.type == ObjectLibrary) || (module.type == RpcClient) ||(module.type == RpcServer) || (module_type == ".lib") || (module_type == ".a");
348 bool dll = (module_type == ".dll") || (module_type == ".cpl");
349 bool exe = (module_type == ".exe") || (module_type == ".scr");
350 bool sys = (module_type == ".sys");
351
352 vector<string> source_files, resource_files, includes, libraries, libpaths;
353 vector<string> header_files, common_defines, compiler_flags;
354 vector<string> vars, values;
355
356 compiler_flags.push_back ( "-Wall" );
357
358 // Always force disabling of sibling calls optimisation for GCC
359 // (TODO: Move to version-specific once this bug is fixed in GCC)
360 compiler_flags.push_back ( "-fno-optimize-sibling-calls" );
361
362 if ( module.pch != NULL )
363 {
364 string pch_path = Path::RelativeFromDirectory (
365 module.pch->file.name,
366 module.GetBasePath() );
367
368 header_files.push_back ( pch_path );
369 }
370
371 if ( intenv == "obj-i386" )
372 intdir = path_basedir + "obj-i386"; /* append relative dir from project dir */
373 else
374 intdir = intenv;
375
376 if ( outenv == "output-i386" )
377 outdir = path_basedir + "output-i386";
378 else
379 outdir = outenv;
380
381 vector<const IfableData*> ifs_list;
382 ifs_list.push_back ( &module.project.non_if_data );
383 ifs_list.push_back ( &module.non_if_data );
384 while ( ifs_list.size() )
385 {
386 const IfableData& data = *ifs_list.back();
387 ifs_list.pop_back();
388 const vector<File*>& files = data.files;
389 for ( i = 0; i < files.size(); i++ )
390 {
391 string file = string(".") + &files[i]->name[cbproj_path.size()];
392
393 if ( !stricmp ( Right(file,3).c_str(), ".rc" ) )
394 resource_files.push_back ( file );
395 else
396 source_files.push_back ( file );
397 }
398 const vector<Include*>& incs = data.includes;
399 for ( i = 0; i < incs.size(); i++ )
400 {
401 string path = Path::RelativeFromDirectory (
402 incs[i]->directory,
403 module.GetBasePath() );
404
405 includes.push_back ( path );
406 }
407 const vector<Library*>& libs = data.libraries;
408 for ( i = 0; i < libs.size(); i++ )
409 {
410 string libpath = outdir + "\\" + libs[i]->importedModule->GetBasePath() + "\\" + libs[i]->name;
411 libraries.push_back ( libs[i]->name );
412 libpaths.push_back ( libpath );
413 }
414 const vector<CompilerFlag*>& cflags = data.compilerFlags;
415 for ( i = 0; i < cflags.size(); i++ )
416 {
417 compiler_flags.push_back ( cflags[i]->flag );
418 }
419 const vector<Define*>& defs = data.defines;
420 for ( i = 0; i < defs.size(); i++ )
421 {
422 if ( defs[i]->value[0] )
423 {
424 const string& escaped = _replace_str(defs[i]->value, "\"","&quot;");
425 common_defines.push_back( defs[i]->name + "=" + escaped );
426 }
427 else
428 {
429 common_defines.push_back( defs[i]->name );
430 }
431 }
432 /*const vector<Property*>& variables = data.properties;
433 for ( i = 0; i < variables.size(); i++ )
434 {
435 vars.push_back( variables[i]->name );
436 values.push_back( variables[i]->value );
437 }*/
438 for ( i = 0; i < data.properties.size(); i++ )
439 {
440 Property& prop = *data.properties[i];
441 if ( strstr ( module.baseaddress.c_str(), prop.name.c_str() ) )
442 baseaddr = prop.value;
443 }
444 }
445
446 if ( !module.allowWarnings )
447 compiler_flags.push_back ( "-Werror" );
448
449 if ( module.type == StaticLibrary && module.isStartupLib )
450 compiler_flags.push_back ( "-Wno-main" );
451
452
453 FILE* OUT = fopen ( cbproj_file.c_str(), "wb" );
454
455 fprintf ( OUT, "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\" ?>\r\n" );
456 fprintf ( OUT, "<CodeBlocks_project_file>\r\n" );
457 fprintf ( OUT, "\t<FileVersion major=\"1\" minor=\"5\" />\r\n" );
458 fprintf ( OUT, "\t<Project>\r\n" );
459 fprintf ( OUT, "\t\t<Option title=\"%s\" />\r\n", module.name.c_str() );
460 fprintf ( OUT, "\t\t<Option pch_mode=\"2\" />\r\n" );
461 fprintf ( OUT, "\t\t<Option default_target=\"\" />\r\n" );
462 fprintf ( OUT, "\t\t<Option compiler=\"gcc\" />\r\n" );
463 fprintf ( OUT, "\t\t<Option virtualFolders=\"\" />\r\n" );
464 fprintf ( OUT, "\t\t<Build>\r\n" );
465
466 bool console = exe && (module.type == Win32CUI);
467
468 for ( size_t icfg = 0; icfg < m_configurations.size(); icfg++ )
469 {
470 const CBConfiguration& cfg = *m_configurations[icfg];
471 fprintf ( OUT, "\t\t\t<Target title=\"%s\">\r\n", cfg.name.c_str() );
472
473 if ( configuration.UseConfigurationInPath )
474 {
475 fprintf ( OUT, "\t\t\t\t<Option output=\"%s\\%s%s\\%s%s\" prefix_auto=\"0\" extension_auto=\"0\" />\r\n", outdir.c_str (), module.GetBasePath ().c_str (), cfg.name.c_str(), module.name.c_str(), module_type.c_str());
476 fprintf ( OUT, "\t\t\t\t<Option object_output=\"%s\\%s%s\" />\r\n", intdir.c_str(), module.GetBasePath ().c_str (), cfg.name.c_str() );
477 }
478 else
479 {
480 fprintf ( OUT, "\t\t\t\t<Option output=\"%s\\%s\\%s%s\" prefix_auto=\"0\" extension_auto=\"0\" />\r\n", outdir.c_str (), module.GetBasePath ().c_str (), module.name.c_str(), module_type.c_str() );
481 fprintf ( OUT, "\t\t\t\t<Option object_output=\"%s\\%s\" />\r\n", intdir.c_str(), module.GetBasePath ().c_str () );
482 }
483
484 if ( lib )
485 fprintf ( OUT, "\t\t\t\t<Option type=\"2\" />\r\n" );
486 else if ( dll )
487 fprintf ( OUT, "\t\t\t\t<Option type=\"3\" />\r\n" );
488 else if ( sys )
489 fprintf ( OUT, "\t\t\t\t<Option type=\"4\" />\r\n" );
490 else if ( exe )
491 {
492 if ( module.type == Kernel )
493 fprintf ( OUT, "\t\t\t\t<Option type=\"4\" />\r\n" );
494 else if ( module.type == NativeCUI )
495 fprintf ( OUT, "\t\t\t\t<Option type=\"4\" />\r\n" );
496 else if ( module.type == Win32CUI || module.type == Win32GUI || module.type == Win32SCR)
497 {
498 if ( console )
499 fprintf ( OUT, "\t\t\t\t<Option type=\"1\" />\r\n" );
500 else
501 fprintf ( OUT, "\t\t\t\t<Option type=\"0\" />\r\n" );
502 }
503 }
504
505 fprintf ( OUT, "\t\t\t\t<Option compiler=\"gcc\" />\r\n" );
506 if ( module_type == ".cpl" )
507 {
508 if ( configuration.UseConfigurationInPath )
509 fprintf ( OUT, "\t\t\t\t<Option parameters=\"shell32,Control_RunDLL &quot;%s\\%s%s\\%s%s&quot;,@\" />\r\n", outdir.c_str (), module.GetBasePath ().c_str (), cfg.name.c_str(), module.name.c_str(), module_type.c_str() );
510 else
511 fprintf ( OUT, "\t\t\t\t<Option parameters=\"shell32,Control_RunDLL &quot;%s\\%s\\%s%s&quot;,@\" />\r\n", outdir.c_str (), module.GetBasePath ().c_str (), module.name.c_str(), module_type.c_str() );
512
513 fprintf ( OUT, "\t\t\t\t<Option host_application=\"rundll32.exe\" />\r\n" );
514 }
515 fprintf ( OUT, "\t\t\t\t<Compiler>\r\n" );
516
517 bool debug = ( cfg.optimization == Debug );
518
519 if ( debug )
520 fprintf ( OUT, "\t\t\t\t\t<Add option=\"-g\" />\r\n" );
521
522 /* compiler flags */
523 for ( i = 0; i < compiler_flags.size(); i++ )
524 {
525 const string& cflag = compiler_flags[i];
526 fprintf ( OUT, "\t\t\t\t\t<Add option=\"%s\" />\r\n", cflag.c_str() );
527 }
528
529 /* defines */
530 for ( i = 0; i < common_defines.size(); i++ )
531 {
532 const string& define = common_defines[i];
533 fprintf ( OUT, "\t\t\t\t\t<Add option=\"-D%s\" />\r\n", define.c_str() );
534 }
535 /* includes */
536 for ( i = 0; i < includes.size(); i++ )
537 {
538 const string& include = includes[i];
539 fprintf ( OUT, "\t\t\t\t\t<Add directory=\"%s\" />\r\n", include.c_str() );
540 }
541 fprintf ( OUT, "\t\t\t\t</Compiler>\r\n" );
542
543 /* includes */
544 fprintf ( OUT, "\t\t\t\t<ResourceCompiler>\r\n" );
545 for ( i = 0; i < includes.size(); i++ )
546 {
547 const string& include = includes[i];
548 fprintf ( OUT, "\t\t\t\t\t<Add directory=\"%s\" />\r\n", include.c_str() );
549 }
550 fprintf ( OUT, "\t\t\t\t</ResourceCompiler>\r\n" );
551
552 fprintf ( OUT, "\t\t\t\t<Linker>\r\n" );
553 fprintf ( OUT, "\t\t\t\t\t<Add option=\"%s\" />\r\n", project_linker_flags.c_str() );
554
555 if ( sys )
556 {
557 fprintf ( OUT, "\t\t\t\t\t<Add option=\"-Wl,--entry,%s%s\" />\r\n", "_", module.GetEntryPoint(false) == "" ? "DriverEntry@8" : module.GetEntryPoint(false).c_str ());
558 fprintf ( OUT, "\t\t\t\t\t<Add option=\"-Wl,--image-base,%s\" />\r\n", baseaddr == "" ? "0x10000" : baseaddr.c_str () );
559 fprintf ( OUT, "\t\t\t\t\t<Add option=\"-nostartfiles -nostdlib\" />\r\n" );
560 }
561 else if ( exe )
562 {
563 if ( module.type == Kernel )
564 {
565 fprintf ( OUT, "\t\t\t\t\t<Add option=\"-Wl,--entry,_KiSystemStartup\" />\r\n" );
566 fprintf ( OUT, "\t\t\t\t\t<Add option=\"-Wl,--image-base,%s\" />\r\n", baseaddr.c_str () );
567 }
568 else if ( module.type == NativeCUI )
569 {
570 fprintf ( OUT, "\t\t\t\t\t<Add option=\"-Wl,--entry,_NtProcessStartup@4\" />\r\n" );
571 fprintf ( OUT, "\t\t\t\t\t<Add option=\"-Wl,--image-base,%s\" />\r\n", baseaddr.c_str () );
572 fprintf ( OUT, "\t\t\t\t\t<Add option=\"-nostartfiles -nostdlib\" />\r\n" );
573 }
574 else
575 {
576 fprintf ( OUT, "\t\t\t\t\t<Add option=\"%s\" />\r\n", module.useHostStdlib ? "-nostartfiles -lgcc" : "-nostartfiles -nostdlib -lgcc" );
577 }
578 }
579 else if ( dll )
580 {
581 fprintf ( OUT, "\t\t\t\t\t<Add option=\"-Wl,--entry,%s%s\" />\r\n", "_", module.GetEntryPoint(false).c_str () );
582 fprintf ( OUT, "\t\t\t\t\t<Add option=\"-Wl,--image-base,%s\" />\r\n", baseaddr == "" ? "0x40000" : baseaddr.c_str () );
583 fprintf ( OUT, "\t\t\t\t\t<Add option=\"%s\" />\r\n", module.useHostStdlib ? "-nostartfiles -lgcc" : "-nostartfiles -nostdlib -lgcc" );
584 }
585
586 fprintf ( OUT, "\t\t\t\t\t<Add option=\"-Wl,--file-alignment,0x1000\" />\r\n" );
587 fprintf ( OUT, "\t\t\t\t\t<Add option=\"-Wl,--section-alignment,0x1000\" />\r\n" );
588
589 if ( dll )
590 fprintf ( OUT, "\t\t\t\t\t<Add option=\"%s.temp.exp\" />\r\n", module.name.c_str() );
591
592 /* libraries */
593 for ( i = 0; i < libraries.size(); i++ )
594 {
595 const string& lib = libraries[i];
596 fprintf ( OUT, "\t\t\t\t\t<Add library=\"%s\" />\r\n", lib.c_str() );
597 }
598 for ( i = 0; i < libpaths.size(); i++ )
599 {
600 const string& lib = libpaths[i];
601 fprintf ( OUT, "\t\t\t\t\t<Add directory=\"%s\" />\r\n", lib.c_str() );
602 }
603 fprintf ( OUT, "\t\t\t\t</Linker>\r\n" );
604
605 if ( dll )
606 {
607 fprintf ( OUT, "\t\t\t\t<ExtraCommands>\r\n" );
608 fprintf ( OUT, "\t\t\t\t\t<Add before=\"dlltool --dllname %s%s --def %s --output-exp %s.temp.exp --kill-at\" />\r\n", module.name.c_str(), module_type.c_str(), module.importLibrary->definition.c_str(), module.name.c_str() );
609 #ifdef WIN32
610 fprintf ( OUT, "\t\t\t\t\t<Add after=\"cmd /c del %s.temp.exp 2&gt;NUL\" />\r\n", module.name.c_str() );
611 #else
612 fprintf ( OUT, "\t\t\t\t\t<Add after=\"rm %s.temp.exp 2&gt;/dev/null\" />\r\n", module.name.c_str() );
613 #endif
614 fprintf ( OUT, "\t\t\t\t\t<Mode after=\"always\" />\r\n" );
615
616 fprintf ( OUT, "\t\t\t\t</ExtraCommands>\r\n" );
617 }
618
619 fprintf ( OUT, "\t\t\t</Target>\r\n" );
620
621 }
622
623 /* vars
624 fprintf ( OUT, "\t\t\t<Environment>\r\n" );
625 for ( i = 0; i < vars.size(); i++ )
626 {
627 const string& var = vars[i];
628 const string& value = values[i];
629 fprintf ( OUT, "\t\t\t\t<Variable name=\"%s\" value=\"%s\" />\r\n", var.c_str(), value.c_str() );
630 }
631 fprintf ( OUT, "\t\t\t</Environment>\r\n" ); */
632
633 fprintf ( OUT, "\t\t</Build>\r\n" );
634
635 if ( module.cplusplus )
636 CompilerVar = "CPP";
637 else
638 CompilerVar = "CC";
639
640 /* header files */
641 for ( i = 0; i < header_files.size(); i++ )
642 {
643 const string& header_file = header_files[i];
644 fprintf ( OUT, "\t\t<Unit filename=\"%s\">\r\n", header_file.c_str() );
645 fprintf ( OUT, "\t\t\t<Option compilerVar=\"%s\" />\r\n", CompilerVar.c_str() );
646 fprintf ( OUT, "\t\t\t<Option compile=\"0\" />\r\n" );
647 fprintf ( OUT, "\t\t\t<Option link=\"0\" />\r\n" );
648 for ( size_t icfg = 0; icfg < m_configurations.size(); icfg++ )
649 {
650 const CBConfiguration& cfg = *m_configurations[icfg];
651 fprintf ( OUT, "\t\t\t<Option target=\"%s\" />\r\n" , cfg.name.c_str() );
652 }
653 fprintf ( OUT, "\t\t</Unit>\r\n" );
654 }
655
656 /* source files */
657 for ( size_t isrcfile = 0; isrcfile < source_files.size(); isrcfile++ )
658 {
659 string source_file = DosSeparator(source_files[isrcfile]);
660 fprintf ( OUT, "\t\t<Unit filename=\"%s\">\r\n", source_file.c_str() );
661 fprintf ( OUT, "\t\t\t<Option compilerVar=\"%s\" />\r\n", CompilerVar.c_str() );
662 for ( size_t icfg = 0; icfg < m_configurations.size(); icfg++ )
663 {
664 const CBConfiguration& cfg = *m_configurations[icfg];
665 fprintf ( OUT, "\t\t\t<Option target=\"%s\" />\r\n" , cfg.name.c_str() );
666 }
667 fprintf ( OUT, "\t\t</Unit>\r\n" );
668 }
669
670 /* resource files */
671 for ( i = 0; i < resource_files.size(); i++ )
672 {
673 const string& resource_file = resource_files[i];
674 fprintf ( OUT, "\t\t<Unit filename=\"%s\">\r\n", resource_file.c_str() );
675 fprintf ( OUT, "\t\t\t<Option compilerVar=\"WINDRES\" />\r\n" );
676 for ( size_t icfg = 0; icfg < m_configurations.size(); icfg++ )
677 {
678 const CBConfiguration& cfg = *m_configurations[icfg];
679 fprintf ( OUT, "\t\t\t<Option target=\"%s\" />\r\n" , cfg.name.c_str() );
680 }
681 fprintf ( OUT, "\t\t</Unit>\r\n" );
682 }
683
684 fprintf ( OUT, "\t\t<Extensions />\r\n" );
685 fprintf ( OUT, "\t</Project>\r\n" );
686 fprintf ( OUT, "</CodeBlocks_project_file>\r\n" );
687
688
689 fclose ( OUT );
690 }
691
692 CBConfiguration::CBConfiguration ( const OptimizationType optimization, const std::string &name )
693 {
694 this->optimization = optimization;
695 if ( name != "" )
696 this->name = name;
697 else
698 {
699 if ( optimization == Debug )
700 this->name = "Debug";
701 else if ( optimization == Release )
702 this->name = "Release";
703 else
704 this->name = "Unknown";
705 }
706 }
707
708 std::string
709 CBBackend::_replace_str(std::string string1, const std::string &find_str, const std::string &replace_str)
710 {
711 std::string::size_type pos = string1.find(find_str, 0);
712 int intLen = find_str.length();
713
714 while(std::string::npos != pos)
715 {
716 string1.replace(pos, intLen, replace_str);
717 pos = string1.find(find_str, intLen + pos);
718 }
719
720 return string1;
721 }
722
723 std::string
724 CBBackend::GenerateProjectLinkerFlags() const
725 {
726 std::string lflags;
727 for ( size_t i = 0; i < ProjectNode.linkerFlags.size (); i++ )
728 {
729 LinkerFlag& linkerFlag = *ProjectNode.linkerFlags[i];
730 if ( lflags.length () > 0 )
731 lflags += " ";
732 lflags += linkerFlag.flag;
733 }
734 return lflags;
735 }
736
737 void
738 CBBackend::MingwAddImplicitLibraries( Module &module )
739 {
740 Library* pLibrary;
741
742 if ( !module.isDefaultEntryPoint )
743 return;
744
745 if ( module.IsDLL () )
746 {
747 //pLibrary = new Library ( module, "__mingw_dllmain" );
748 //module.non_if_data.libraries.insert ( module.non_if_data.libraries.begin(), pLibrary );
749 }
750 else
751 {
752 pLibrary = new Library ( module, module.isUnicode ? "mingw_wmain" : "mingw_main" );
753 module.non_if_data.libraries.insert ( module.non_if_data.libraries.begin(), pLibrary );
754 }
755
756 pLibrary = new Library ( module, "mingw_common" );
757 module.non_if_data.libraries.insert ( module.non_if_data.libraries.begin() + 1, pLibrary );
758
759 if ( module.name != "msvcrt" )
760 {
761 // always link in msvcrt to get the basic routines
762 pLibrary = new Library ( module, "msvcrt" );
763 module.non_if_data.libraries.push_back ( pLibrary );
764 }
765 }