don't include the Iso targets in the workspace
[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 if ((module.type != Iso) &&
307 (module.type != LiveIso) &&
308 (module.type != IsoRegTest) &&
309 (module.type != LiveIsoRegTest))
310 {
311 std::string Cbp_file = CbpFileName ( module );
312 fprintf ( OUT, "\t\t<Project filename=\"%s\">\r\n", Cbp_file.c_str());
313
314 /* dependencies */
315 vector<const IfableData*> ifs_list;
316 ifs_list.push_back ( &module.project.non_if_data );
317 ifs_list.push_back ( &module.non_if_data );
318 while ( ifs_list.size() )
319 {
320 const IfableData& data = *ifs_list.back();
321 ifs_list.pop_back();
322 const vector<Library*>& libs = data.libraries;
323 for ( size_t j = 0; j < libs.size(); j++ )
324 fprintf ( OUT, "\t\t\t<Depends filename=\"%s\\%s_auto.cbp\" />\r\n", libs[j]->importedModule->GetBasePath().c_str(), libs[j]->name.c_str() );
325 }
326 fprintf ( OUT, "\t\t</Project>\r\n" );
327 }
328 }
329 fprintf ( OUT, "\t</Workspace>\r\n" );
330 fprintf ( OUT, "</CodeBlocks_workspace_file>\r\n" );
331 }
332
333 void
334 CBBackend::_generate_cbproj ( const Module& module )
335 {
336
337 size_t i;
338
339 string cbproj_file = CbpFileName(module);
340 string outdir;
341 string intdir;
342 string path_basedir = module.GetPathToBaseDir ();
343 string intenv = Environment::GetIntermediatePath ();
344 string outenv = Environment::GetOutputPath ();
345 string module_type = GetExtension(module.GetTargetName());
346 string cbproj_path = module.GetBasePath();
347 string CompilerVar;
348 string baseaddr;
349 string windres_defines;
350 string widl_options;
351 string project_linker_flags = "-Wl,--enable-stdcall-fixup ";
352 project_linker_flags += GenerateProjectLinkerFlags();
353
354 bool lib = (module.type == ObjectLibrary) || (module.type == RpcClient) ||(module.type == RpcServer) || (module_type == ".lib") || (module_type == ".a");
355 bool dll = (module_type == ".dll") || (module_type == ".cpl");
356 bool exe = (module_type == ".exe") || (module_type == ".scr");
357 bool sys = (module_type == ".sys");
358
359 vector<string> source_files, resource_files, includes, libraries, libpaths;
360 vector<string> header_files, common_defines, compiler_flags;
361 vector<string> vars, values;
362
363 /* do not create project files for these targets
364 use virtual targets instead */
365 switch (module.type)
366 {
367 case Iso:
368 case LiveIso:
369 case IsoRegTest:
370 case LiveIsoRegTest:
371 return;
372 default:
373 break;
374 }
375
376 compiler_flags.push_back ( "-Wall" );
377
378 // Always force disabling of sibling calls optimisation for GCC
379 // (TODO: Move to version-specific once this bug is fixed in GCC)
380 compiler_flags.push_back ( "-fno-optimize-sibling-calls" );
381
382 if ( module.pch != NULL )
383 {
384 string pch_path = Path::RelativeFromDirectory (
385 module.pch->file.name,
386 module.GetBasePath() );
387
388 header_files.push_back ( pch_path );
389 }
390
391 if ( intenv == "obj-i386" )
392 intdir = path_basedir + "obj-i386"; /* append relative dir from project dir */
393 else
394 intdir = intenv;
395
396 if ( outenv == "output-i386" )
397 outdir = path_basedir + "output-i386";
398 else
399 outdir = outenv;
400
401 vector<const IfableData*> ifs_list;
402 ifs_list.push_back ( &module.project.non_if_data );
403 ifs_list.push_back ( &module.non_if_data );
404 while ( ifs_list.size() )
405 {
406 const IfableData& data = *ifs_list.back();
407 ifs_list.pop_back();
408 for ( i = 0; i < data.ifs.size(); i++ )
409 {
410 const Property* property = _lookup_property( module, data.ifs[i]->property );
411 if ( property != NULL )
412 {
413 if ( data.ifs[i]->value == property->value && data.ifs[i]->negated == false ||
414 data.ifs[i]->value != property->value && data.ifs[i]->negated)
415 ifs_list.push_back ( &data.ifs[i]->data );
416 }
417 }
418 const vector<File*>& files = data.files;
419 for ( i = 0; i < files.size(); i++ )
420 {
421 string file = string(".") + &files[i]->name[cbproj_path.size()];
422
423 if ( !stricmp ( Right(file,3).c_str(), ".rc" ) )
424 resource_files.push_back ( file );
425 else
426 source_files.push_back ( file );
427 }
428 const vector<Include*>& incs = data.includes;
429 for ( i = 0; i < incs.size(); i++ )
430 {
431 string path = Path::RelativeFromDirectory (
432 incs[i]->directory,
433 module.GetBasePath() );
434
435 includes.push_back ( path );
436 widl_options += "-I" + path + " ";
437 }
438 const vector<Library*>& libs = data.libraries;
439 for ( i = 0; i < libs.size(); i++ )
440 {
441 string libpath = intdir + "\\" + libs[i]->importedModule->GetBasePath();
442 libraries.push_back ( libs[i]->name );
443 libpaths.push_back ( libpath );
444 }
445 const vector<CompilerFlag*>& cflags = data.compilerFlags;
446 for ( i = 0; i < cflags.size(); i++ )
447 {
448 compiler_flags.push_back ( cflags[i]->flag );
449 }
450 const vector<Define*>& defs = data.defines;
451 for ( i = 0; i < defs.size(); i++ )
452 {
453 if ( defs[i]->value[0] )
454 {
455 const string& escaped = _replace_str(defs[i]->value, "\"","&quot;");
456 common_defines.push_back( defs[i]->name + "=" + escaped );
457 windres_defines += "-D" + defs[i]->name + "=" + escaped + " ";
458 }
459 else
460 {
461 common_defines.push_back( defs[i]->name );
462 windres_defines += "-D" + defs[i]->name + " ";
463 }
464 }
465 /*const vector<Property*>& variables = data.properties;
466 for ( i = 0; i < variables.size(); i++ )
467 {
468 vars.push_back( variables[i]->name );
469 values.push_back( variables[i]->value );
470 }*/
471 for ( i = 0; i < data.properties.size(); i++ )
472 {
473 Property& prop = *data.properties[i];
474 if ( strstr ( module.baseaddress.c_str(), prop.name.c_str() ) )
475 baseaddr = prop.value;
476 }
477 }
478
479 if ( !module.allowWarnings )
480 compiler_flags.push_back ( "-Werror" );
481
482 if ( module.type == StaticLibrary && module.isStartupLib )
483 compiler_flags.push_back ( "-Wno-main" );
484
485
486 FILE* OUT = fopen ( cbproj_file.c_str(), "wb" );
487
488 fprintf ( OUT, "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\" ?>\r\n" );
489 fprintf ( OUT, "<CodeBlocks_project_file>\r\n" );
490 fprintf ( OUT, "\t<FileVersion major=\"1\" minor=\"6\" />\r\n" );
491 fprintf ( OUT, "\t<Project>\r\n" );
492 fprintf ( OUT, "\t\t<Option title=\"%s\" />\r\n", module.name.c_str() );
493 fprintf ( OUT, "\t\t<Option pch_mode=\"2\" />\r\n" );
494 fprintf ( OUT, "\t\t<Option default_target=\"\" />\r\n" );
495 fprintf ( OUT, "\t\t<Option compiler=\"gcc\" />\r\n" );
496 fprintf ( OUT, "\t\t<Option extended_obj_names=\"1\" />\r\n" );
497 fprintf ( OUT, "\t\t<Option virtualFolders=\"\" />\r\n" );
498 fprintf ( OUT, "\t\t<Build>\r\n" );
499
500 bool console = exe && (module.type == Win32CUI);
501
502 for ( size_t icfg = 0; icfg < m_configurations.size(); icfg++ )
503 {
504 const CBConfiguration& cfg = *m_configurations[icfg];
505 fprintf ( OUT, "\t\t\t<Target title=\"%s\">\r\n", cfg.name.c_str() );
506
507 if ( configuration.UseConfigurationInPath )
508 {
509 if ( module.type == StaticLibrary ||module.type == ObjectLibrary )
510 fprintf ( OUT, "\t\t\t\t<Option output=\"%s\\%s%s\\%s%s\" prefix_auto=\"0\" extension_auto=\"0\" />\r\n", intdir.c_str (), module.GetBasePath ().c_str (), cfg.name.c_str(), module.name.c_str(), module_type.c_str());
511 else
512 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());
513 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() );
514 }
515 else
516 {
517 if ( module.type == StaticLibrary || module.type == ObjectLibrary )
518 fprintf ( OUT, "\t\t\t\t<Option output=\"%s\\%s\\%s%s\" prefix_auto=\"0\" extension_auto=\"0\" />\r\n", intdir.c_str (), module.GetBasePath ().c_str (), module.name.c_str(), module_type.c_str() );
519 else
520 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() );
521 fprintf ( OUT, "\t\t\t\t<Option object_output=\"%s\\%s\" />\r\n", intdir.c_str(), module.GetBasePath ().c_str () );
522 }
523
524 if ( lib )
525 {
526 fprintf ( OUT, "\t\t\t\t<Option type=\"2\" />\r\n" );
527 }
528 else if ( dll )
529 fprintf ( OUT, "\t\t\t\t<Option type=\"3\" />\r\n" );
530 else if ( sys )
531 fprintf ( OUT, "\t\t\t\t<Option type=\"5\" />\r\n" );
532 else if ( exe )
533 {
534 if ( module.type == Kernel )
535 fprintf ( OUT, "\t\t\t\t<Option type=\"5\" />\r\n" );
536 else if ( module.type == NativeCUI )
537 fprintf ( OUT, "\t\t\t\t<Option type=\"5\" />\r\n" );
538 else if ( module.type == Win32CUI || module.type == Win32GUI || module.type == Win32SCR)
539 {
540 if ( console )
541 fprintf ( OUT, "\t\t\t\t<Option type=\"1\" />\r\n" );
542 else
543 fprintf ( OUT, "\t\t\t\t<Option type=\"0\" />\r\n" );
544 }
545 }
546
547 fprintf ( OUT, "\t\t\t\t<Option compiler=\"gcc\" />\r\n" );
548
549 if ( module_type == ".cpl" )
550 {
551 fprintf ( OUT, "\t\t\t\t<Option parameters=\"shell32,Control_RunDLL &quot;$exe_output&quot;,@\" />\r\n" );
552 fprintf ( OUT, "\t\t\t\t<Option host_application=\"rundll32.exe\" />\r\n" );
553 }
554 fprintf ( OUT, "\t\t\t\t<Compiler>\r\n" );
555
556 bool debug = ( cfg.optimization == Debug );
557
558 if ( debug )
559 fprintf ( OUT, "\t\t\t\t\t<Add option=\"-g\" />\r\n" );
560
561 /* compiler flags */
562 for ( i = 0; i < compiler_flags.size(); i++ )
563 {
564 const string& cflag = compiler_flags[i];
565 fprintf ( OUT, "\t\t\t\t\t<Add option=\"%s\" />\r\n", cflag.c_str() );
566 }
567
568 /* defines */
569 for ( i = 0; i < common_defines.size(); i++ )
570 {
571 const string& define = common_defines[i];
572 fprintf ( OUT, "\t\t\t\t\t<Add option=\"-D%s\" />\r\n", define.c_str() );
573 }
574 /* includes */
575 for ( i = 0; i < includes.size(); i++ )
576 {
577 const string& include = includes[i];
578 fprintf ( OUT, "\t\t\t\t\t<Add directory=\"%s\" />\r\n", include.c_str() );
579 }
580 fprintf ( OUT, "\t\t\t\t</Compiler>\r\n" );
581
582 /* includes */
583 fprintf ( OUT, "\t\t\t\t<ResourceCompiler>\r\n" );
584 for ( i = 0; i < includes.size(); i++ )
585 {
586 const string& include = includes[i];
587 fprintf ( OUT, "\t\t\t\t\t<Add directory=\"%s\" />\r\n", include.c_str() );
588 }
589 fprintf ( OUT, "\t\t\t\t</ResourceCompiler>\r\n" );
590
591 fprintf ( OUT, "\t\t\t\t<Linker>\r\n" );
592 fprintf ( OUT, "\t\t\t\t\t<Add option=\"%s\" />\r\n", project_linker_flags.c_str() );
593
594 if ( sys )
595 {
596 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 ());
597 fprintf ( OUT, "\t\t\t\t\t<Add option=\"-Wl,--image-base,%s\" />\r\n", baseaddr == "" ? "0x10000" : baseaddr.c_str () );
598 fprintf ( OUT, "\t\t\t\t\t<Add option=\"-nostartfiles -Wl,--nostdlib\" />\r\n" );
599 }
600 else if ( exe )
601 {
602 if ( module.type == Kernel )
603 {
604 fprintf ( OUT, "\t\t\t\t\t<Add option=\"-Wl,--entry,_KiSystemStartup\" />\r\n" );
605 fprintf ( OUT, "\t\t\t\t\t<Add option=\"-Wl,--image-base,%s\" />\r\n", baseaddr.c_str () );
606 }
607 else if ( module.type == NativeCUI )
608 {
609 fprintf ( OUT, "\t\t\t\t\t<Add option=\"-Wl,--entry,_NtProcessStartup@4\" />\r\n" );
610 fprintf ( OUT, "\t\t\t\t\t<Add option=\"-Wl,--image-base,%s\" />\r\n", baseaddr.c_str () );
611 fprintf ( OUT, "\t\t\t\t\t<Add option=\"-nostartfiles -Wl,--nostdlib\" />\r\n" );
612 }
613 else
614 {
615 fprintf ( OUT, "\t\t\t\t\t<Add option=\"%s\" />\r\n", module.useHostStdlib ? "-nostartfiles" : "-nostartfiles -Wl,--nostdlib" );
616 fprintf ( OUT, "\t\t\t\t\t<Add library=\"gcc\" />\r\n" );
617 }
618 }
619 else if ( dll )
620 {
621 fprintf ( OUT, "\t\t\t\t\t<Add option=\"-Wl,--entry,%s%s\" />\r\n", "_", module.GetEntryPoint(false).c_str () );
622 fprintf ( OUT, "\t\t\t\t\t<Add option=\"-Wl,--image-base,%s\" />\r\n", baseaddr == "" ? "0x40000" : baseaddr.c_str () );
623
624 if ( module.type == Win32DLL )
625 fprintf ( OUT, "\t\t\t\t\t<Add option=\"-Wl,--shared\" />\r\n" );
626 else if ( module.type == NativeDLL)
627 fprintf ( OUT, "\t\t\t\t\t<Add option=\"-Wl,--shared\" />\r\n" );
628 else if ( module.type == NativeDLL)
629 fprintf ( OUT, "\t\t\t\t\t<Add option=\"-nostartfiles -Wl,--shared\" />\r\n" );
630
631 fprintf ( OUT, "\t\t\t\t\t<Add option=\"%s\" />\r\n", module.useHostStdlib ? "-nostartfiles" : "-nostartfiles -Wl,--nostdlib" );
632 fprintf ( OUT, "\t\t\t\t\t<Add library=\"gcc\" />\r\n" );
633 }
634
635 fprintf ( OUT, "\t\t\t\t\t<Add option=\"-Wl,--file-alignment,0x1000\" />\r\n" );
636 fprintf ( OUT, "\t\t\t\t\t<Add option=\"-Wl,--section-alignment,0x1000\" />\r\n" );
637
638 if ( dll )
639 fprintf ( OUT, "\t\t\t\t\t<Add option=\"%s.temp.exp\" />\r\n", module.name.c_str() );
640
641 /* libraries */
642 for ( i = 0; i < libraries.size(); i++ )
643 {
644 const string& lib = libraries[i];
645 fprintf ( OUT, "\t\t\t\t\t<Add library=\"%s\" />\r\n", lib.c_str() );
646 }
647 for ( i = 0; i < libpaths.size(); i++ )
648 {
649 const string& lib = libpaths[i];
650 fprintf ( OUT, "\t\t\t\t\t<Add directory=\"%s\" />\r\n", lib.c_str() );
651 }
652 fprintf ( OUT, "\t\t\t\t</Linker>\r\n" );
653
654 fprintf ( OUT, "\t\t\t\t<ExtraCommands>\r\n" );
655
656 if ( module.type == StaticLibrary && module.importLibrary )
657 fprintf ( OUT, "\t\t\t\t\t<Add after=\"dlltool --dllname %s --def %s --output-lib $exe_output; %s -U\" />\r\n", module.importLibrary->dllname.c_str (), module.importLibrary->definition.c_str(), module.mangledSymbols ? "" : "--kill-at" );
658 else if ( module.importLibrary != NULL )
659 fprintf ( OUT, "\t\t\t\t\t<Add after=\"dlltool --dllname %s --def %s --output-lib &quot;$(TARGET_OBJECT_DIR)lib$(TARGET_OUTPUT_BASENAME).a&quot; %s\" />\r\n", module.GetTargetName ().c_str(), module.importLibrary->definition.c_str(), module.mangledSymbols ? "" : "--kill-at" );
660
661
662 for ( i = 0; i < resource_files.size(); i++ )
663 {
664 const string& resource_file = resource_files[i];
665 #ifdef WIN32
666 fprintf ( OUT, "\t\t\t\t\t<Add after=\"cmd /c del $(TARGET_OBJECT_DIR)\\%s.rci.tmp 2&gt;NUL\" />\r\n", resource_file.c_str() );
667 fprintf ( OUT, "\t\t\t\t\t<Add after=\"cmd /c del $(TARGET_OBJECT_DIR)\\%s.res.tmp 2&gt;NUL\" />\r\n", resource_file.c_str() );
668 #else
669 fprintf ( OUT, "\t\t\t\t\t<Add after=\"rm $(TARGET_OBJECT_DIR)/%s.rci.tmp 2&gt;/dev/null\" />\r\n", resource_file.c_str() );
670 fprintf ( OUT, "\t\t\t\t\t<Add after=\"rm $(TARGET_OBJECT_DIR)/%s.res.tmp 2&gt;/dev/null\" />\r\n", resource_file.c_str() );
671 #endif
672 }
673
674 if ( dll )
675 {
676 if (IsWineModule( module ))
677 fprintf ( OUT, "\t\t\t\t\t<Add before=\"%s\\tools\\winebuild\\winebuild.exe -o %s --def -E %s.spec\" />\r\n", outdir.c_str(), module.importLibrary->definition.c_str(), module.name.c_str());
678 fprintf ( OUT, "\t\t\t\t\t<Add before=\"dlltool --dllname %s --def %s --output-exp %s.temp.exp %s\" />\r\n", module.GetTargetName ().c_str(), module.importLibrary->definition.c_str(), module.name.c_str(), module.mangledSymbols ? "" : "--kill-at" );
679 fprintf ( OUT, "\t\t\t\t\t<Add after=\"%s\\tools\\pefixup $exe_output -exports\" />\r\n", outdir.c_str() );
680 #ifdef WIN32
681 fprintf ( OUT, "\t\t\t\t\t<Add after=\"cmd /c del %s.temp.exp 2&gt;NUL\" />\r\n", module.name.c_str() );
682 #else
683 fprintf ( OUT, "\t\t\t\t\t<Add after=\"rm %s.temp.exp 2&gt;/dev/null\" />\r\n", module.name.c_str() );
684 #endif
685 fprintf ( OUT, "\t\t\t\t\t<Mode after=\"always\" />\r\n" );
686 }
687
688 fprintf ( OUT, "\t\t\t\t</ExtraCommands>\r\n" );
689
690 fprintf ( OUT, "\t\t\t</Target>\r\n" );
691
692 }
693
694 /* vars
695 fprintf ( OUT, "\t\t\t<Environment>\r\n" );
696 for ( i = 0; i < vars.size(); i++ )
697 {
698 const string& var = vars[i];
699 const string& value = values[i];
700 fprintf ( OUT, "\t\t\t\t<Variable name=\"%s\" value=\"%s\" />\r\n", var.c_str(), value.c_str() );
701 }
702 fprintf ( OUT, "\t\t\t</Environment>\r\n" ); */
703
704 fprintf ( OUT, "\t\t</Build>\r\n" );
705
706 if ( module.cplusplus )
707 CompilerVar = "CPP";
708 else
709 CompilerVar = "CC";
710
711 /* header files */
712 for ( i = 0; i < header_files.size(); i++ )
713 {
714 const string& header_file = header_files[i];
715 fprintf ( OUT, "\t\t<Unit filename=\"%s\">\r\n", header_file.c_str() );
716 fprintf ( OUT, "\t\t\t<Option compilerVar=\"%s\" />\r\n", CompilerVar.c_str() );
717 fprintf ( OUT, "\t\t\t<Option compile=\"0\" />\r\n" );
718 fprintf ( OUT, "\t\t\t<Option link=\"0\" />\r\n" );
719 for ( size_t icfg = 0; icfg < m_configurations.size(); icfg++ )
720 {
721 const CBConfiguration& cfg = *m_configurations[icfg];
722 fprintf ( OUT, "\t\t\t<Option target=\"%s\" />\r\n" , cfg.name.c_str() );
723 }
724 fprintf ( OUT, "\t\t</Unit>\r\n" );
725 }
726
727 /* source files */
728 for ( size_t isrcfile = 0; isrcfile < source_files.size(); isrcfile++ )
729 {
730 string source_file = DosSeparator(source_files[isrcfile]);
731 fprintf ( OUT, "\t\t<Unit filename=\"%s\">\r\n", source_file.c_str() );
732 fprintf ( OUT, "\t\t\t<Option compilerVar=\"%s\" />\r\n", CompilerVar.c_str() );
733
734 string extension = GetExtension ( source_file );
735 if ( extension == ".s" || extension == ".S" )
736 {
737 fprintf ( OUT, "\t\t\t<Option compile=\"1\" />\r\n" );
738 fprintf ( OUT, "\t\t\t<Option link=\"1\" />\r\n" );
739 fprintf ( OUT, "\t\t\t<Option compiler=\"gcc\" use=\"1\" buildCommand=\"gcc -x assembler-with-cpp -c $file -o $link_objects $includes -D__ASM__ $options\" />\r\n" );
740 }
741 else if ( extension == ".asm" || extension == ".ASM" )
742 {
743 fprintf ( OUT, "\t\t\t<Option compile=\"1\" />\r\n" );
744 fprintf ( OUT, "\t\t\t<Option link=\"1\" />\r\n" );
745 fprintf ( OUT, "\t\t\t<Option compiler=\"gcc\" use=\"1\" buildCommand=\"nasm -f win32 $file -o $link_objects\" />\r\n" );
746 }
747 else if ( extension == ".idl" || extension == ".IDL" )
748 {
749 fprintf ( OUT, "\t\t\t<Option compile=\"1\" />\r\n" );
750 fprintf ( OUT, "\t\t\t<Option compiler=\"gcc\" use=\"1\" buildCommand=\"%s\\tools\\widl\\widl.exe %s %s -h -H &quot;$(TARGET_OUTPUT_DIR)\\$file_c.h&quot; -c -C &quot;$(TARGET_OUTPUT_DIR)\\$file_c.c&quot; $file\\ngcc %s -c &quot;$(TARGET_OUTPUT_DIR)\\$file_c.c&quot; -o &quot;$(TARGET_OUTPUT_DIR)\\$file_c.o&quot;\" />\r\n", outdir.c_str(), widl_options.c_str(), windres_defines.c_str(), widl_options.c_str() );
751 }
752 else if ( extension == ".spec" || extension == ".SPEC" )
753 {
754 fprintf ( OUT, "\t\t\t<Option compile=\"1\" />\r\n" );
755 fprintf ( OUT, "\t\t\t<Option link=\"1\" />\r\n" );
756 fprintf ( OUT, "\t\t\t<Option compiler=\"gcc\" use=\"1\" buildCommand=\"%s\\tools\\winebuild\\winebuild.exe -o $file.stubs.c --pedll $file\\n$compiler -c $options $includes $file.stubs.c -o $(TARGET_OBJECT_DIR)\\$file.o\" />\r\n", outdir.c_str() );
757 }
758
759 for ( size_t icfg = 0; icfg < m_configurations.size(); icfg++ )
760 {
761 const CBConfiguration& cfg = *m_configurations[icfg];
762 fprintf ( OUT, "\t\t\t<Option target=\"%s\" />\r\n" , cfg.name.c_str() );
763 }
764 fprintf ( OUT, "\t\t</Unit>\r\n" );
765 }
766
767 /* resource files */
768 for ( i = 0; i < resource_files.size(); i++ )
769 {
770 const string& resource_file = resource_files[i];
771 fprintf ( OUT, "\t\t<Unit filename=\"%s\">\r\n", resource_file.c_str() );
772 fprintf ( OUT, "\t\t\t<Option compilerVar=\"WINDRES\" />\r\n" );
773 string extension = GetExtension ( resource_file );
774 fprintf ( OUT, "\t\t\t<Option compiler=\"gcc\" use=\"1\" buildCommand=\"gcc -xc -E -DRC_INVOKED $includes %s $file -o $(TARGET_OBJECT_DIR)\\$file.rci.tmp\\n%s\\tools\\wrc\\wrc.exe $includes %s $(TARGET_OBJECT_DIR)\\$file.rci.tmp $(TARGET_OBJECT_DIR)\\$file.res.tmp\\n$rescomp --output-format=coff $(TARGET_OBJECT_DIR)\\$file.res.tmp -o $resource_output\" />\r\n" , windres_defines.c_str(), outdir.c_str(), windres_defines.c_str() );
775 for ( size_t icfg = 0; icfg < m_configurations.size(); icfg++ )
776 {
777 const CBConfiguration& cfg = *m_configurations[icfg];
778 fprintf ( OUT, "\t\t\t<Option target=\"%s\" />\r\n" , cfg.name.c_str() );
779 }
780 fprintf ( OUT, "\t\t</Unit>\r\n" );
781 }
782
783 fprintf ( OUT, "\t\t<Extensions />\r\n" );
784 fprintf ( OUT, "\t</Project>\r\n" );
785 fprintf ( OUT, "</CodeBlocks_project_file>\r\n" );
786
787
788 fclose ( OUT );
789 }
790
791 CBConfiguration::CBConfiguration ( const OptimizationType optimization, const std::string &name )
792 {
793 this->optimization = optimization;
794 if ( name != "" )
795 this->name = name;
796 else
797 {
798 if ( optimization == Debug )
799 this->name = "Debug";
800 else if ( optimization == Release )
801 this->name = "Release";
802 else
803 this->name = "Unknown";
804 }
805 }
806
807 std::string
808 CBBackend::_replace_str(std::string string1, const std::string &find_str, const std::string &replace_str)
809 {
810 std::string::size_type pos = string1.find(find_str, 0);
811 int intLen = find_str.length();
812
813 while(std::string::npos != pos)
814 {
815 string1.replace(pos, intLen, replace_str);
816 pos = string1.find(find_str, intLen + pos);
817 }
818
819 return string1;
820 }
821
822 std::string
823 CBBackend::GenerateProjectLinkerFlags() const
824 {
825 std::string lflags;
826 for ( size_t i = 0; i < ProjectNode.linkerFlags.size (); i++ )
827 {
828 LinkerFlag& linkerFlag = *ProjectNode.linkerFlags[i];
829 if ( lflags.length () > 0 )
830 lflags += " ";
831 lflags += linkerFlag.flag;
832 }
833 return lflags;
834 }
835
836 void
837 CBBackend::MingwAddImplicitLibraries( Module &module )
838 {
839 Library* pLibrary;
840
841 if ( !module.isDefaultEntryPoint )
842 return;
843
844 if ( module.IsDLL () )
845 {
846 //pLibrary = new Library ( module, "__mingw_dllmain" );
847 //module.non_if_data.libraries.insert ( module.non_if_data.libraries.begin(), pLibrary );
848 }
849 else
850 {
851 pLibrary = new Library ( module, module.isUnicode ? "mingw_wmain" : "mingw_main" );
852 module.non_if_data.libraries.insert ( module.non_if_data.libraries.begin(), pLibrary );
853 }
854
855 pLibrary = new Library ( module, "mingw_common" );
856 module.non_if_data.libraries.insert ( module.non_if_data.libraries.begin() + 1, pLibrary );
857
858 if ( module.name != "msvcrt" )
859 {
860 // always link in msvcrt to get the basic routines
861 pLibrary = new Library ( module, "msvcrt" );
862 module.non_if_data.libraries.push_back ( pLibrary );
863 }
864 }
865
866 const Property*
867 CBBackend::_lookup_property ( const Module& module, const std::string& name ) const
868 {
869 /* Check local values */
870 for ( size_t i = 0; i < module.non_if_data.properties.size(); i++ )
871 {
872 const Property& property = *module.non_if_data.properties[i];
873 if ( property.name == name )
874 return &property;
875 }
876 // TODO FIXME - should we check local if-ed properties?
877 for ( size_t i = 0; i < module.project.non_if_data.properties.size(); i++ )
878 {
879 const Property& property = *module.project.non_if_data.properties[i];
880 if ( property.name == name )
881 return &property;
882 }
883 // TODO FIXME - should we check global if-ed properties?
884 return NULL;
885 }
886
887 bool
888 CBBackend::IsWineModule ( const Module& module ) const
889 {
890 if ( module.importLibrary == NULL)
891 return false;
892
893 size_t index = module.importLibrary->definition.rfind ( ".spec.def" );
894 return ( index != string::npos );
895 }