3960526c99210c8656d474b585f8b690e2ca62b5
[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 #define IsStaticLibrary( module ) ( ( module.type == StaticLibrary ) || ( module.type == HostStaticLibrary ) )
41
42 static class CBFactory : public Backend::Factory
43 {
44 public:
45
46 CBFactory() : Factory("CB", "Code::Blocks") {}
47 Backend *operator() (Project &project,
48 Configuration& configuration)
49 {
50 return new CBBackend(project, configuration);
51 }
52
53 } factory;
54
55
56 CBBackend::CBBackend(Project &project,
57 Configuration& configuration) : Backend(project, configuration)
58 {
59 m_unitCount = 0;
60 }
61
62 void CBBackend::Process()
63 {
64
65 while ( m_configurations.size () > 0 )
66 {
67 const CBConfiguration* cfg = m_configurations.back();
68 m_configurations.pop_back();
69 delete cfg;
70 }
71
72 m_configurations.push_back ( new CBConfiguration( Debug ));
73 m_configurations.push_back ( new CBConfiguration( Release ));
74
75 string filename_wrkspace ( ProjectNode.name );
76 filename_wrkspace += "_auto.workspace";
77
78 printf ( "Creating Code::Blocks workspace: %s\n", filename_wrkspace.c_str() );
79
80 ProcessModules();
81 m_wrkspaceFile = fopen ( filename_wrkspace.c_str(), "wb" );
82
83 if ( !m_wrkspaceFile )
84 {
85 printf ( "Could not create file '%s'.\n", filename_wrkspace.c_str() );
86 return;
87 }
88
89 _generate_workspace ( m_wrkspaceFile );
90
91 fclose ( m_wrkspaceFile );
92 printf ( "Done.\n" );
93 }
94
95 void CBBackend::ProcessModules()
96 {
97 for(size_t i = 0; i < ProjectNode.modules.size(); i++)
98 {
99 Module &module = *ProjectNode.modules[i];
100 MingwAddImplicitLibraries( module );
101 _generate_cbproj ( module );
102 }
103 }
104
105 static std::string
106 GetExtension ( const std::string& filename )
107 {
108 size_t index = filename.find_last_of ( '/' );
109 if (index == string::npos) index = 0;
110 string tmp = filename.substr( index, filename.size() - index );
111 size_t ext_index = tmp.find_last_of( '.' );
112 if (ext_index != string::npos)
113 return filename.substr ( index + ext_index, filename.size() );
114 return "";
115 }
116
117 static bool FileExists(string &filename)
118 {
119 ifstream file(filename.c_str());
120
121 if(!file.is_open())
122 return false;
123
124 file.close();
125 return true;
126 }
127
128 void CBBackend::ProcessFile(string &filepath)
129 {
130 // Remove the .\ at the start of the filenames
131 if ( filepath[0] == '.' && strchr ( "/\\", filepath[1] ) )
132 filepath.erase(0, 2);
133
134 if(!FileExists(filepath))
135 return;
136
137 // Change the \ to /
138 for(size_t i = 0; i < filepath.length(); i++)
139 {
140 if(filepath[i] == '\\')
141 filepath[i] = '/';
142 }
143
144 // Remove the filename from the path
145 string folder = "";
146
147 size_t pos = filepath.rfind(string("/"), filepath.length() - 1);
148
149 if(pos != string::npos)
150 {
151 folder = filepath;
152 folder.erase(pos, folder.length() - pos);
153 }
154
155 FileUnit fileUnit;
156 fileUnit.filename = filepath;
157 fileUnit.folder = folder;
158
159 m_fileUnits.push_back(fileUnit);
160
161 if(folder != "")
162 AddFolders(folder);
163
164 m_unitCount++;
165 }
166
167 bool CBBackend::CheckFolderAdded(string &folder)
168 {
169 for(size_t i = 0; i < m_folders.size(); i++)
170 {
171 if(m_folders[i] == folder)
172 return true;
173 }
174
175 return false;
176 }
177
178 void CBBackend::AddFolders(string &folder)
179 {
180 // Check if this folder was already added. true if it was, false otherwise.
181 if(CheckFolderAdded(folder))
182 return;
183
184 m_folders.push_back(folder);
185
186 size_t pos = folder.rfind(string("/"), folder.length() - 1);
187
188 if(pos == string::npos)
189 return;
190
191 folder.erase(pos, folder.length() - pos);
192 AddFolders(folder);
193 }
194
195 void CBBackend::OutputFolders()
196 {
197 #if 0
198 m_devFile << "Folders=";
199
200 for(size_t i = 0; i < m_folders.size(); i++)
201 {
202 if(i > 0)
203 m_devFile << ",";
204
205 m_devFile << m_folders[i];
206 }
207 #endif
208 }
209
210 std::string
211 CBBackend::CbpFileName ( const Module& module ) const
212 {
213 return DosSeparator(
214 ReplaceExtension ( module.output->relative_path + "\\" + module.output->name, "_auto.cbp" )
215 );
216 }
217
218 std::string
219 CBBackend::LayoutFileName ( const Module& module ) const
220 {
221 return DosSeparator(
222 ReplaceExtension ( module.output->relative_path + "\\" + module.output->name, "_auto.layout" )
223 );
224 }
225
226 std::string
227 CBBackend::DependFileName ( const Module& module ) const
228 {
229 return DosSeparator(
230 ReplaceExtension ( module.output->relative_path + "\\" + module.output->name, "_auto.depend" )
231 );
232 }
233
234 void
235 CBBackend::_get_object_files ( const Module& module, vector<string>& out) const
236 {
237 string basepath = module.output->relative_path;
238 size_t i;
239 string intenv = Environment::GetIntermediatePath () + "\\" + basepath + "\\";
240 string outenv = Environment::GetOutputPath () + "\\" + basepath + "\\";
241
242 vector<string> cfgs;
243
244 if ( configuration.UseConfigurationInPath )
245 {
246 cfgs.push_back ( intenv + "Debug" );
247 cfgs.push_back ( intenv + "Release" );
248 cfgs.push_back ( outenv + "Debug" );
249 cfgs.push_back ( outenv + "Release" );
250 }
251 else
252 {
253 cfgs.push_back ( intenv );
254 cfgs.push_back ( outenv );
255 }
256
257 vector<const IfableData*> ifs_list;
258 ifs_list.push_back ( &module.project.non_if_data );
259 ifs_list.push_back ( &module.non_if_data );
260 while ( ifs_list.size () )
261 {
262 const IfableData& data = *ifs_list.back();
263 ifs_list.pop_back();
264 const vector<File*>& files = data.files;
265 for ( i = 0; i < files.size (); i++ )
266 {
267 string file = files[i]->file.relative_path + sSep + files[i]->file.name;
268 string::size_type pos = file.find_last_of ("\\");
269 if ( pos != string::npos )
270 file.erase ( 0, pos+1 );
271 if ( !stricmp ( Right(file,3).c_str(), ".rc" ) )
272 file = ReplaceExtension ( file, ".res" );
273 else
274 file = ReplaceExtension ( file, ".obj" );
275 for ( size_t j = 0; j < cfgs.size () / 2; j++ )
276 out.push_back ( cfgs[j] + "\\" + file );
277 }
278
279 }
280 }
281
282 void
283 CBBackend::_clean_project_files ( void )
284 {
285 for ( size_t i = 0; i < ProjectNode.modules.size(); i++ )
286 {
287 Module& module = *ProjectNode.modules[i];
288 vector<string> out;
289 printf("Cleaning project %s %s\n", module.name.c_str (), module.output->relative_path.c_str () );
290
291 string basepath = module.output->relative_path;
292 remove ( CbpFileName ( module ).c_str () );
293 remove ( DependFileName ( module ).c_str () );
294 remove ( LayoutFileName ( module ).c_str () );
295
296 _get_object_files ( module, out );
297 for ( size_t j = 0; j < out.size (); j++)
298 {
299 //printf("Cleaning file %s\n", out[j].c_str () );
300 remove ( out[j].c_str () );
301 }
302 }
303
304 string filename_wrkspace = ProjectNode.name + ".workspace";
305
306 remove ( filename_wrkspace.c_str () );
307 }
308
309 void
310 CBBackend::_generate_workspace ( FILE* OUT )
311 {
312 fprintf ( OUT, "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\" ?>\r\n" );
313 fprintf ( OUT, "<CodeBlocks_workspace_file>\r\n" );
314 fprintf ( OUT, "\t<Workspace title=\"ReactOS\">\r\n" );
315 for ( size_t i = 0; i < ProjectNode.modules.size(); i++ )
316 {
317 Module& module = *ProjectNode.modules[i];
318
319 if ((module.type != Iso) &&
320 (module.type != LiveIso) &&
321 (module.type != IsoRegTest) &&
322 (module.type != LiveIsoRegTest))
323 {
324 std::string Cbp_file = CbpFileName ( module );
325 fprintf ( OUT, "\t\t<Project filename=\"%s\">\r\n", Cbp_file.c_str());
326
327 /* dependencies */
328 vector<const IfableData*> ifs_list;
329 ifs_list.push_back ( &module.project.non_if_data );
330 ifs_list.push_back ( &module.non_if_data );
331 while ( ifs_list.size() )
332 {
333 const IfableData& data = *ifs_list.back();
334 ifs_list.pop_back();
335 const vector<Library*>& libs = data.libraries;
336 for ( size_t j = 0; j < libs.size(); j++ )
337 fprintf ( OUT, "\t\t\t<Depends filename=\"%s\\%s_auto.cbp\" />\r\n", libs[j]->importedModule->output->relative_path.c_str(), libs[j]->name.c_str() );
338 }
339 fprintf ( OUT, "\t\t</Project>\r\n" );
340 }
341 }
342 fprintf ( OUT, "\t</Workspace>\r\n" );
343 fprintf ( OUT, "</CodeBlocks_workspace_file>\r\n" );
344 }
345
346 void
347 CBBackend::_generate_cbproj ( const Module& module )
348 {
349
350 size_t i;
351
352 string cbproj_file = CbpFileName(module);
353 string outdir;
354 string intdir;
355 string path_basedir = module.GetPathToBaseDir ();
356 string intenv = Environment::GetIntermediatePath ();
357 string outenv = Environment::GetOutputPath ();
358 string module_type = GetExtension(*module.output);
359 string cbproj_path = module.output->relative_path;
360 string CompilerVar;
361 string baseaddr;
362 string windres_defines;
363 string widl_options;
364 string project_linker_flags = "-Wl,--enable-stdcall-fixup ";
365 project_linker_flags += GenerateProjectLinkerFlags();
366
367 bool lib = (module.type == ObjectLibrary) ||
368 (module.type == RpcClient) ||
369 (module.type == RpcServer) ||
370 (module.type == RpcProxy) ||
371 (module_type == ".lib") ||
372 (module_type == ".a");
373 bool dll = (module_type == ".dll") || (module_type == ".cpl");
374 bool exe = (module_type == ".exe") || (module_type == ".scr");
375 bool sys = (module_type == ".sys");
376
377 vector<string> source_files, resource_files, includes, libraries, libpaths;
378 vector<string> header_files, common_defines, compiler_flags;
379 vector<string> vars, values;
380
381 /* do not create project files for these targets
382 use virtual targets instead */
383 switch (module.type)
384 {
385 case Iso:
386 case LiveIso:
387 case IsoRegTest:
388 case LiveIsoRegTest:
389 return;
390 default:
391 break;
392 }
393
394 compiler_flags.push_back ( "-Wall" );
395
396 // Always force disabling of sibling calls optimisation for GCC
397 // (TODO: Move to version-specific once this bug is fixed in GCC)
398 compiler_flags.push_back ( "-fno-optimize-sibling-calls" );
399
400 if ( module.pch != NULL )
401 {
402 string pch_path = Path::RelativeFromDirectory (
403 module.pch->file->name,
404 module.output->relative_path );
405
406 header_files.push_back ( pch_path );
407 }
408
409 if ( intenv == "obj-i386" )
410 intdir = path_basedir + "obj-i386"; /* append relative dir from project dir */
411 else
412 intdir = intenv;
413
414 if ( outenv == "output-i386" )
415 outdir = path_basedir + "output-i386";
416 else
417 outdir = outenv;
418
419 vector<const IfableData*> ifs_list;
420 ifs_list.push_back ( &module.project.non_if_data );
421 ifs_list.push_back ( &module.non_if_data );
422 while ( ifs_list.size() )
423 {
424 const IfableData& data = *ifs_list.back();
425 ifs_list.pop_back();
426 for ( i = 0; i < data.ifs.size(); i++ )
427 {
428 const Property* property = _lookup_property( module, data.ifs[i]->property );
429 if ( property != NULL )
430 {
431 if ( data.ifs[i]->value == property->value && data.ifs[i]->negated == false ||
432 data.ifs[i]->value != property->value && data.ifs[i]->negated)
433 ifs_list.push_back ( &data.ifs[i]->data );
434 }
435 }
436 const vector<File*>& files = data.files;
437 for ( i = 0; i < files.size(); i++ )
438 {
439 string fullpath = files[i]->file.relative_path + sSep + files[i]->file.name;
440 string file = string(".") + &fullpath[cbproj_path.size()];
441
442 if ( !stricmp ( Right(file,3).c_str(), ".rc" ) )
443 resource_files.push_back ( file );
444 else
445 source_files.push_back ( file );
446 }
447 const vector<Include*>& incs = data.includes;
448 for ( i = 0; i < incs.size(); i++ )
449 {
450 string path = Path::RelativeFromDirectory (
451 incs[i]->directory->relative_path,
452 module.output->relative_path );
453
454 includes.push_back ( path );
455 widl_options += "-I" + path + " ";
456 }
457 const vector<Library*>& libs = data.libraries;
458 for ( i = 0; i < libs.size(); i++ )
459 {
460 string libpath = intdir + "\\" + libs[i]->importedModule->output->relative_path;
461 libraries.push_back ( libs[i]->name );
462 libpaths.push_back ( libpath );
463 }
464 const vector<CompilerFlag*>& cflags = data.compilerFlags;
465 for ( i = 0; i < cflags.size(); i++ )
466 {
467 compiler_flags.push_back ( cflags[i]->flag );
468 }
469 const vector<Define*>& defs = data.defines;
470 for ( i = 0; i < defs.size(); i++ )
471 {
472 if ( defs[i]->value[0] )
473 {
474 const string& escaped = _replace_str(defs[i]->value, "\"","&quot;");
475 common_defines.push_back( defs[i]->name + "=" + escaped );
476 windres_defines += "-D" + defs[i]->name + "=" + escaped + " ";
477 }
478 else
479 {
480 common_defines.push_back( defs[i]->name );
481 windres_defines += "-D" + defs[i]->name + " ";
482 }
483 }
484 /*const vector<Property*>& variables = data.properties;
485 for ( i = 0; i < variables.size(); i++ )
486 {
487 vars.push_back( variables[i]->name );
488 values.push_back( variables[i]->value );
489 }*/
490 for ( i = 0; i < data.properties.size(); i++ )
491 {
492 Property& prop = *data.properties[i];
493 if ( strstr ( module.baseaddress.c_str(), prop.name.c_str() ) )
494 baseaddr = prop.value;
495 }
496 }
497
498 if ( !module.allowWarnings )
499 compiler_flags.push_back ( "-Werror" );
500
501 if ( IsStaticLibrary ( module ) && module.isStartupLib )
502 compiler_flags.push_back ( "-Wno-main" );
503
504
505 FILE* OUT = fopen ( cbproj_file.c_str(), "wb" );
506
507 fprintf ( OUT, "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\" ?>\r\n" );
508 fprintf ( OUT, "<CodeBlocks_project_file>\r\n" );
509 fprintf ( OUT, "\t<FileVersion major=\"1\" minor=\"6\" />\r\n" );
510 fprintf ( OUT, "\t<Project>\r\n" );
511 fprintf ( OUT, "\t\t<Option title=\"%s\" />\r\n", module.name.c_str() );
512 fprintf ( OUT, "\t\t<Option pch_mode=\"2\" />\r\n" );
513 fprintf ( OUT, "\t\t<Option default_target=\"\" />\r\n" );
514 fprintf ( OUT, "\t\t<Option compiler=\"gcc\" />\r\n" );
515 fprintf ( OUT, "\t\t<Option extended_obj_names=\"1\" />\r\n" );
516 fprintf ( OUT, "\t\t<Option virtualFolders=\"\" />\r\n" );
517 fprintf ( OUT, "\t\t<Build>\r\n" );
518
519 bool console = exe && (module.type == Win32CUI);
520
521 for ( size_t icfg = 0; icfg < m_configurations.size(); icfg++ )
522 {
523 const CBConfiguration& cfg = *m_configurations[icfg];
524 fprintf ( OUT, "\t\t\t<Target title=\"%s\">\r\n", cfg.name.c_str() );
525
526 if ( configuration.UseConfigurationInPath )
527 {
528 if ( IsStaticLibrary ( module ) ||module.type == ObjectLibrary )
529 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.output->relative_path.c_str (), cfg.name.c_str(), module.name.c_str(), module_type.c_str());
530 else
531 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.output->relative_path.c_str (), cfg.name.c_str(), module.name.c_str(), module_type.c_str());
532 fprintf ( OUT, "\t\t\t\t<Option object_output=\"%s\\%s%s\" />\r\n", intdir.c_str(), module.output->relative_path.c_str (), cfg.name.c_str() );
533 }
534 else
535 {
536 if ( IsStaticLibrary ( module ) || module.type == ObjectLibrary )
537 fprintf ( OUT, "\t\t\t\t<Option output=\"%s\\%s\\%s%s\" prefix_auto=\"0\" extension_auto=\"0\" />\r\n", intdir.c_str (), module.output->relative_path.c_str (), module.name.c_str(), module_type.c_str() );
538 else
539 fprintf ( OUT, "\t\t\t\t<Option output=\"%s\\%s\\%s%s\" prefix_auto=\"0\" extension_auto=\"0\" />\r\n", outdir.c_str (), module.output->relative_path.c_str (), module.name.c_str(), module_type.c_str() );
540 fprintf ( OUT, "\t\t\t\t<Option object_output=\"%s\\%s\" />\r\n", intdir.c_str(), module.output->relative_path.c_str () );
541 }
542
543 if ( lib )
544 {
545 fprintf ( OUT, "\t\t\t\t<Option type=\"2\" />\r\n" );
546 }
547 else if ( dll )
548 fprintf ( OUT, "\t\t\t\t<Option type=\"3\" />\r\n" );
549 else if ( sys )
550 fprintf ( OUT, "\t\t\t\t<Option type=\"5\" />\r\n" );
551 else if ( exe )
552 {
553 if ( module.type == Kernel )
554 fprintf ( OUT, "\t\t\t\t<Option type=\"5\" />\r\n" );
555 else if ( module.type == NativeCUI )
556 fprintf ( OUT, "\t\t\t\t<Option type=\"5\" />\r\n" );
557 else if ( module.type == Win32CUI || module.type == Win32GUI || module.type == Win32SCR)
558 {
559 if ( console )
560 fprintf ( OUT, "\t\t\t\t<Option type=\"1\" />\r\n" );
561 else
562 fprintf ( OUT, "\t\t\t\t<Option type=\"0\" />\r\n" );
563 }
564 }
565
566 fprintf ( OUT, "\t\t\t\t<Option compiler=\"gcc\" />\r\n" );
567
568 if ( module_type == ".cpl" )
569 {
570 fprintf ( OUT, "\t\t\t\t<Option parameters=\"shell32,Control_RunDLL &quot;$exe_output&quot;,@\" />\r\n" );
571 fprintf ( OUT, "\t\t\t\t<Option host_application=\"rundll32.exe\" />\r\n" );
572 }
573 fprintf ( OUT, "\t\t\t\t<Compiler>\r\n" );
574
575 bool debug = ( cfg.optimization == Debug );
576
577 if ( debug )
578 fprintf ( OUT, "\t\t\t\t\t<Add option=\"-g\" />\r\n" );
579
580 /* compiler flags */
581 for ( i = 0; i < compiler_flags.size(); i++ )
582 {
583 const string& cflag = compiler_flags[i];
584 fprintf ( OUT, "\t\t\t\t\t<Add option=\"%s\" />\r\n", cflag.c_str() );
585 }
586
587 /* defines */
588 for ( i = 0; i < common_defines.size(); i++ )
589 {
590 const string& define = common_defines[i];
591 fprintf ( OUT, "\t\t\t\t\t<Add option=\"-D%s\" />\r\n", define.c_str() );
592 }
593 /* includes */
594 for ( i = 0; i < includes.size(); i++ )
595 {
596 const string& include = includes[i];
597 fprintf ( OUT, "\t\t\t\t\t<Add directory=\"%s\" />\r\n", include.c_str() );
598 }
599 fprintf ( OUT, "\t\t\t\t</Compiler>\r\n" );
600
601 /* includes */
602 fprintf ( OUT, "\t\t\t\t<ResourceCompiler>\r\n" );
603 for ( i = 0; i < includes.size(); i++ )
604 {
605 const string& include = includes[i];
606 fprintf ( OUT, "\t\t\t\t\t<Add directory=\"%s\" />\r\n", include.c_str() );
607 }
608 fprintf ( OUT, "\t\t\t\t</ResourceCompiler>\r\n" );
609
610 fprintf ( OUT, "\t\t\t\t<Linker>\r\n" );
611 fprintf ( OUT, "\t\t\t\t\t<Add option=\"%s\" />\r\n", project_linker_flags.c_str() );
612
613 if ( sys )
614 {
615 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 ());
616 fprintf ( OUT, "\t\t\t\t\t<Add option=\"-Wl,--image-base,%s\" />\r\n", baseaddr == "" ? "0x10000" : baseaddr.c_str () );
617 fprintf ( OUT, "\t\t\t\t\t<Add option=\"-nostartfiles -Wl,--nostdlib\" />\r\n" );
618 }
619 else if ( exe )
620 {
621 if ( module.type == Kernel )
622 {
623 fprintf ( OUT, "\t\t\t\t\t<Add option=\"-Wl,--entry,_KiSystemStartup\" />\r\n" );
624 fprintf ( OUT, "\t\t\t\t\t<Add option=\"-Wl,--image-base,%s\" />\r\n", baseaddr.c_str () );
625 }
626 else if ( module.type == NativeCUI )
627 {
628 fprintf ( OUT, "\t\t\t\t\t<Add option=\"-Wl,--entry,_NtProcessStartup@4\" />\r\n" );
629 fprintf ( OUT, "\t\t\t\t\t<Add option=\"-Wl,--image-base,%s\" />\r\n", baseaddr.c_str () );
630 fprintf ( OUT, "\t\t\t\t\t<Add option=\"-nostartfiles -Wl,--nostdlib\" />\r\n" );
631 }
632 else
633 {
634 fprintf ( OUT, "\t\t\t\t\t<Add option=\"%s\" />\r\n", module.cplusplus ? "-nostartfiles" : "-nostartfiles -Wl,--nostdlib" );
635 fprintf ( OUT, "\t\t\t\t\t<Add library=\"gcc\" />\r\n" );
636 }
637 }
638 else if ( dll )
639 {
640 fprintf ( OUT, "\t\t\t\t\t<Add option=\"-Wl,--entry,%s%s\" />\r\n", "_", module.GetEntryPoint(false).c_str () );
641 fprintf ( OUT, "\t\t\t\t\t<Add option=\"-Wl,--image-base,%s\" />\r\n", baseaddr == "" ? "0x40000" : baseaddr.c_str () );
642
643 if ( module.type == Win32DLL )
644 fprintf ( OUT, "\t\t\t\t\t<Add option=\"-Wl,--shared\" />\r\n" );
645 else if ( module.type == NativeDLL)
646 fprintf ( OUT, "\t\t\t\t\t<Add option=\"-Wl,--shared\" />\r\n" );
647 else if ( module.type == NativeDLL)
648 fprintf ( OUT, "\t\t\t\t\t<Add option=\"-nostartfiles -Wl,--shared\" />\r\n" );
649
650 fprintf ( OUT, "\t\t\t\t\t<Add option=\"%s\" />\r\n", module.cplusplus ? "-nostartfiles" : "-nostartfiles -Wl,--nostdlib" );
651 fprintf ( OUT, "\t\t\t\t\t<Add library=\"gcc\" />\r\n" );
652 }
653
654 fprintf ( OUT, "\t\t\t\t\t<Add option=\"-Wl,--file-alignment,0x1000\" />\r\n" );
655 fprintf ( OUT, "\t\t\t\t\t<Add option=\"-Wl,--section-alignment,0x1000\" />\r\n" );
656
657 if ( dll )
658 fprintf ( OUT, "\t\t\t\t\t<Add option=\"%s.temp.exp\" />\r\n", module.name.c_str() );
659
660 /* libraries */
661 for ( i = 0; i < libraries.size(); i++ )
662 {
663 const string& lib = libraries[i];
664 fprintf ( OUT, "\t\t\t\t\t<Add library=\"%s\" />\r\n", lib.c_str() );
665 }
666 for ( i = 0; i < libpaths.size(); i++ )
667 {
668 const string& lib = libpaths[i];
669 fprintf ( OUT, "\t\t\t\t\t<Add directory=\"%s\" />\r\n", lib.c_str() );
670 }
671 fprintf ( OUT, "\t\t\t\t</Linker>\r\n" );
672
673 fprintf ( OUT, "\t\t\t\t<ExtraCommands>\r\n" );
674
675 #if 0
676 if ( IsStaticLibrary ( module ) && module.importLibrary )
677 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" );
678 else if ( module.importLibrary != NULL )
679 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" );
680 #endif
681
682
683 for ( i = 0; i < resource_files.size(); i++ )
684 {
685 const string& resource_file = resource_files[i];
686 #ifdef WIN32
687 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() );
688 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() );
689 #else
690 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() );
691 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() );
692 #endif
693 }
694
695 #if 0
696 if ( dll )
697 {
698 if (IsWineModule( module ))
699 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());
700 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" );
701 fprintf ( OUT, "\t\t\t\t\t<Add after=\"%s\\tools\\pefixup $exe_output -exports\" />\r\n", outdir.c_str() );
702 #ifdef WIN32
703 fprintf ( OUT, "\t\t\t\t\t<Add after=\"cmd /c del %s.temp.exp 2&gt;NUL\" />\r\n", module.name.c_str() );
704 #else
705 fprintf ( OUT, "\t\t\t\t\t<Add after=\"rm %s.temp.exp 2&gt;/dev/null\" />\r\n", module.name.c_str() );
706 #endif
707 fprintf ( OUT, "\t\t\t\t\t<Mode after=\"always\" />\r\n" );
708 }
709 #endif
710
711 fprintf ( OUT, "\t\t\t\t</ExtraCommands>\r\n" );
712
713 fprintf ( OUT, "\t\t\t</Target>\r\n" );
714
715 }
716
717 /* vars
718 fprintf ( OUT, "\t\t\t<Environment>\r\n" );
719 for ( i = 0; i < vars.size(); i++ )
720 {
721 const string& var = vars[i];
722 const string& value = values[i];
723 fprintf ( OUT, "\t\t\t\t<Variable name=\"%s\" value=\"%s\" />\r\n", var.c_str(), value.c_str() );
724 }
725 fprintf ( OUT, "\t\t\t</Environment>\r\n" ); */
726
727 fprintf ( OUT, "\t\t</Build>\r\n" );
728
729 #ifdef FORCE_CPP
730 CompilerVar = "CPP"
731 #else
732 if ( module.cplusplus )
733 CompilerVar = "CPP";
734 else
735 CompilerVar = "CC";
736 #endif
737
738 /* header files */
739 for ( i = 0; i < header_files.size(); i++ )
740 {
741 const string& header_file = header_files[i];
742 fprintf ( OUT, "\t\t<Unit filename=\"%s\">\r\n", header_file.c_str() );
743 fprintf ( OUT, "\t\t\t<Option compilerVar=\"%s\" />\r\n", CompilerVar.c_str() );
744 fprintf ( OUT, "\t\t\t<Option compile=\"0\" />\r\n" );
745 fprintf ( OUT, "\t\t\t<Option link=\"0\" />\r\n" );
746 for ( size_t icfg = 0; icfg < m_configurations.size(); icfg++ )
747 {
748 const CBConfiguration& cfg = *m_configurations[icfg];
749 fprintf ( OUT, "\t\t\t<Option target=\"%s\" />\r\n" , cfg.name.c_str() );
750 }
751 fprintf ( OUT, "\t\t</Unit>\r\n" );
752 }
753
754 /* source files */
755 for ( size_t isrcfile = 0; isrcfile < source_files.size(); isrcfile++ )
756 {
757 string source_file = DosSeparator(source_files[isrcfile]);
758 fprintf ( OUT, "\t\t<Unit filename=\"%s\">\r\n", source_file.c_str() );
759 fprintf ( OUT, "\t\t\t<Option compilerVar=\"%s\" />\r\n", CompilerVar.c_str() );
760
761 string extension = GetExtension ( source_file );
762 if ( extension == ".s" || extension == ".S" )
763 {
764 fprintf ( OUT, "\t\t\t<Option compile=\"1\" />\r\n" );
765 fprintf ( OUT, "\t\t\t<Option link=\"1\" />\r\n" );
766 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" );
767 }
768 else if ( extension == ".asm" || extension == ".ASM" )
769 {
770 fprintf ( OUT, "\t\t\t<Option compile=\"1\" />\r\n" );
771 fprintf ( OUT, "\t\t\t<Option link=\"1\" />\r\n" );
772 fprintf ( OUT, "\t\t\t<Option compiler=\"gcc\" use=\"1\" buildCommand=\"nasm -f win32 $file -o $link_objects\" />\r\n" );
773 }
774 else if ( extension == ".idl" || extension == ".IDL" )
775 {
776 fprintf ( OUT, "\t\t\t<Option compile=\"1\" />\r\n" );
777 fprintf ( OUT, "\t\t\t<Option compiler=\"gcc\" use=\"1\" buildCommand=\"%s\\tools\\widl\\widl.exe %s %s -h -H &quot;$(TARGET_OUTPUT_DIR)$filetitle_c.h&quot; -c -C &quot;$(TARGET_OUTPUT_DIR)$filetitle_c.c&quot; $file\\ngcc %s -c &quot;$(TARGET_OUTPUT_DIR)$filetitle_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() );
778 }
779 else if ( extension == ".spec" || extension == ".SPEC" )
780 {
781 fprintf ( OUT, "\t\t\t<Option compile=\"1\" />\r\n" );
782 fprintf ( OUT, "\t\t\t<Option link=\"1\" />\r\n" );
783 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() );
784 }
785
786 for ( size_t icfg = 0; icfg < m_configurations.size(); icfg++ )
787 {
788 const CBConfiguration& cfg = *m_configurations[icfg];
789 fprintf ( OUT, "\t\t\t<Option target=\"%s\" />\r\n" , cfg.name.c_str() );
790 }
791 fprintf ( OUT, "\t\t</Unit>\r\n" );
792 }
793
794 /* resource files */
795 for ( i = 0; i < resource_files.size(); i++ )
796 {
797 const string& resource_file = resource_files[i];
798 fprintf ( OUT, "\t\t<Unit filename=\"%s\">\r\n", resource_file.c_str() );
799 fprintf ( OUT, "\t\t\t<Option compilerVar=\"WINDRES\" />\r\n" );
800 string extension = GetExtension ( resource_file );
801 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() );
802 for ( size_t icfg = 0; icfg < m_configurations.size(); icfg++ )
803 {
804 const CBConfiguration& cfg = *m_configurations[icfg];
805 fprintf ( OUT, "\t\t\t<Option target=\"%s\" />\r\n" , cfg.name.c_str() );
806 }
807 fprintf ( OUT, "\t\t</Unit>\r\n" );
808 }
809
810 fprintf ( OUT, "\t\t<Extensions />\r\n" );
811 fprintf ( OUT, "\t</Project>\r\n" );
812 fprintf ( OUT, "</CodeBlocks_project_file>\r\n" );
813
814
815 fclose ( OUT );
816 }
817
818 CBConfiguration::CBConfiguration ( const OptimizationType optimization, const std::string &name )
819 {
820 this->optimization = optimization;
821 if ( name != "" )
822 this->name = name;
823 else
824 {
825 if ( optimization == Debug )
826 this->name = "Debug";
827 else if ( optimization == Release )
828 this->name = "Release";
829 else
830 this->name = "Unknown";
831 }
832 }
833
834 std::string
835 CBBackend::_replace_str(std::string string1, const std::string &find_str, const std::string &replace_str)
836 {
837 std::string::size_type pos = string1.find(find_str, 0);
838 int intLen = find_str.length();
839
840 while(std::string::npos != pos)
841 {
842 string1.replace(pos, intLen, replace_str);
843 pos = string1.find(find_str, intLen + pos);
844 }
845
846 return string1;
847 }
848
849 std::string
850 CBBackend::GenerateProjectLinkerFlags() const
851 {
852 std::string lflags;
853 for ( size_t i = 0; i < ProjectNode.linkerFlags.size (); i++ )
854 {
855 LinkerFlag& linkerFlag = *ProjectNode.linkerFlags[i];
856 if ( lflags.length () > 0 )
857 lflags += " ";
858 lflags += linkerFlag.flag;
859 }
860 return lflags;
861 }
862
863 void
864 CBBackend::MingwAddImplicitLibraries( Module &module )
865 {
866 Library* pLibrary;
867
868 if ( !module.isDefaultEntryPoint )
869 return;
870
871 if ( module.IsDLL () )
872 {
873 //pLibrary = new Library ( module, "__mingw_dllmain" );
874 //module.non_if_data.libraries.insert ( module.non_if_data.libraries.begin(), pLibrary );
875 }
876 else
877 {
878 pLibrary = new Library ( module, module.isUnicode ? "mingw_wmain" : "mingw_main" );
879 module.non_if_data.libraries.insert ( module.non_if_data.libraries.begin(), pLibrary );
880 }
881
882 pLibrary = new Library ( module, "mingw_common" );
883 module.non_if_data.libraries.insert ( module.non_if_data.libraries.begin() + 1, pLibrary );
884
885 if ( module.name != "msvcrt" )
886 {
887 // always link in msvcrt to get the basic routines
888 pLibrary = new Library ( module, "msvcrt" );
889 module.non_if_data.libraries.push_back ( pLibrary );
890 }
891 }
892
893 const Property*
894 CBBackend::_lookup_property ( const Module& module, const std::string& name ) const
895 {
896 /* Check local values */
897 for ( size_t i = 0; i < module.non_if_data.properties.size(); i++ )
898 {
899 const Property& property = *module.non_if_data.properties[i];
900 if ( property.name == name )
901 return &property;
902 }
903 // TODO FIXME - should we check local if-ed properties?
904 for ( size_t i = 0; i < module.project.non_if_data.properties.size(); i++ )
905 {
906 const Property& property = *module.project.non_if_data.properties[i];
907 if ( property.name == name )
908 return &property;
909 }
910 // TODO FIXME - should we check global if-ed properties?
911 return NULL;
912 }
913
914 bool
915 CBBackend::IsWineModule ( const Module& module ) const
916 {
917 if ( module.importLibrary == NULL)
918 return false;
919
920 size_t index = module.importLibrary->source->name.rfind ( ".spec.def" );
921 return ( index != string::npos );
922 }