- Merge to trunk r37270.
[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( std::map<std::string, Module*>::const_iterator p = ProjectNode.modules.begin(); p != ProjectNode.modules.end(); ++ p )
98 {
99 Module &module = *p->second;
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 + sSep + 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 + sSep + 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 + sSep + 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 () + sSep + basepath + sSep;
240 string outenv = Environment::GetOutputPath () + sSep + basepath + sSep;
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 (sSep);
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] + sSep + file );
277 }
278
279 }
280 }
281
282 void
283 CBBackend::_clean_project_files ( void )
284 {
285 for( std::map<std::string, Module*>::const_iterator p = ProjectNode.modules.begin(); p != ProjectNode.modules.end(); ++ p )
286 {
287 Module& module = *p->second;
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( std::map<std::string, Module*>::const_iterator p = ProjectNode.modules.begin(); p != ProjectNode.modules.end(); ++ p )
316 {
317 Module& module = *p->second;
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%s_auto.cbp\" />\r\n", libs[j]->importedModule->output->relative_path.c_str(), sSep.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 const vector<File*>& files = data.files;
427 for ( i = 0; i < files.size(); i++ )
428 {
429 string fullpath = files[i]->file.relative_path + sSep + files[i]->file.name;
430 string file = string(".") + &fullpath[cbproj_path.size()];
431
432 if ( !stricmp ( Right(file,3).c_str(), ".rc" ) )
433 resource_files.push_back ( file );
434 else
435 source_files.push_back ( file );
436 }
437 const vector<Include*>& incs = data.includes;
438 for ( i = 0; i < incs.size(); i++ )
439 {
440 string path = Path::RelativeFromDirectory (
441 incs[i]->directory->relative_path,
442 module.output->relative_path );
443
444 includes.push_back ( path );
445 widl_options += "-I" + path + " ";
446 }
447 const vector<Library*>& libs = data.libraries;
448 for ( i = 0; i < libs.size(); i++ )
449 {
450 string libpath = intdir + sSep + libs[i]->importedModule->output->relative_path;
451 libraries.push_back ( libs[i]->name );
452 libpaths.push_back ( libpath );
453 }
454 const vector<CompilerFlag*>& cflags = data.compilerFlags;
455 for ( i = 0; i < cflags.size(); i++ )
456 {
457 compiler_flags.push_back ( cflags[i]->flag );
458 }
459 const vector<Define*>& defs = data.defines;
460 for ( i = 0; i < defs.size(); i++ )
461 {
462 if ( defs[i]->value[0] )
463 {
464 const string& escaped = _replace_str(defs[i]->value, "\"","&quot;");
465 common_defines.push_back( defs[i]->name + "=" + escaped );
466 windres_defines += "-D" + defs[i]->name + "=" + escaped + " ";
467 }
468 else
469 {
470 common_defines.push_back( defs[i]->name );
471 windres_defines += "-D" + defs[i]->name + " ";
472 }
473 }
474 /*const vector<Property*>& variables = data.properties;
475 for ( i = 0; i < variables.size(); i++ )
476 {
477 vars.push_back( variables[i]->name );
478 values.push_back( variables[i]->value );
479 }*/
480 for ( std::map<std::string, Property*>::const_iterator p = data.properties.begin(); p != data.properties.end(); ++ p )
481 {
482 Property& prop = *p->second;
483 if ( strstr ( module.baseaddress.c_str(), prop.name.c_str() ) )
484 baseaddr = prop.value;
485 }
486 }
487
488 if ( !module.allowWarnings )
489 compiler_flags.push_back ( "-Werror" );
490
491 if ( IsStaticLibrary ( module ) && module.isStartupLib )
492 compiler_flags.push_back ( "-Wno-main" );
493
494
495 FILE* OUT = fopen ( cbproj_file.c_str(), "wb" );
496
497 fprintf ( OUT, "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\" ?>\r\n" );
498 fprintf ( OUT, "<CodeBlocks_project_file>\r\n" );
499 fprintf ( OUT, "\t<FileVersion major=\"1\" minor=\"6\" />\r\n" );
500 fprintf ( OUT, "\t<Project>\r\n" );
501 fprintf ( OUT, "\t\t<Option title=\"%s\" />\r\n", module.name.c_str() );
502 fprintf ( OUT, "\t\t<Option pch_mode=\"2\" />\r\n" );
503 fprintf ( OUT, "\t\t<Option default_target=\"\" />\r\n" );
504 fprintf ( OUT, "\t\t<Option compiler=\"gcc\" />\r\n" );
505 fprintf ( OUT, "\t\t<Option extended_obj_names=\"1\" />\r\n" );
506 fprintf ( OUT, "\t\t<Option virtualFolders=\"\" />\r\n" );
507 fprintf ( OUT, "\t\t<Build>\r\n" );
508
509 bool console = exe && (module.type == Win32CUI);
510
511 for ( size_t icfg = 0; icfg < m_configurations.size(); icfg++ )
512 {
513 const CBConfiguration& cfg = *m_configurations[icfg];
514 fprintf ( OUT, "\t\t\t<Target title=\"%s\">\r\n", cfg.name.c_str() );
515
516 if ( configuration.UseConfigurationInPath )
517 {
518 if ( IsStaticLibrary ( module ) ||module.type == ObjectLibrary )
519 fprintf ( OUT, "\t\t\t\t<Option output=\"%s%s%s%s%s%s%s\" prefix_auto=\"0\" extension_auto=\"0\" />\r\n", intdir.c_str (), sSep.c_str(), module.output->relative_path.c_str (), cfg.name.c_str(), sSep.c_str(), module.name.c_str(), module_type.c_str());
520 else
521 fprintf ( OUT, "\t\t\t\t<Option output=\"%s%s%s%s%s%s%s\" prefix_auto=\"0\" extension_auto=\"0\" />\r\n", outdir.c_str (), sSep.c_str(), module.output->relative_path.c_str (), cfg.name.c_str(), sSep.c_str(), module.name.c_str(), module_type.c_str());
522 fprintf ( OUT, "\t\t\t\t<Option object_output=\"%s%s%s%s\" />\r\n", intdir.c_str(), sSep.c_str(), module.output->relative_path.c_str (), cfg.name.c_str() );
523 }
524 else
525 {
526 if ( IsStaticLibrary ( module ) || module.type == ObjectLibrary )
527 fprintf ( OUT, "\t\t\t\t<Option output=\"%s%s%s%s%s%s\" prefix_auto=\"0\" extension_auto=\"0\" />\r\n", intdir.c_str (), sSep.c_str(), module.output->relative_path.c_str (), sSep.c_str(), module.name.c_str(), module_type.c_str() );
528 else
529 fprintf ( OUT, "\t\t\t\t<Option output=\"%s%s%s%s%s%s\" prefix_auto=\"0\" extension_auto=\"0\" />\r\n", outdir.c_str (), sSep.c_str(), module.output->relative_path.c_str (), sSep.c_str(), module.name.c_str(), module_type.c_str() );
530 fprintf ( OUT, "\t\t\t\t<Option object_output=\"%s%s%s\" />\r\n", intdir.c_str(), sSep.c_str(), module.output->relative_path.c_str () );
531 }
532
533 if ( lib )
534 {
535 fprintf ( OUT, "\t\t\t\t<Option type=\"2\" />\r\n" );
536 }
537 else if ( dll )
538 fprintf ( OUT, "\t\t\t\t<Option type=\"3\" />\r\n" );
539 else if ( sys )
540 fprintf ( OUT, "\t\t\t\t<Option type=\"5\" />\r\n" );
541 else if ( exe )
542 {
543 if ( module.type == Kernel )
544 fprintf ( OUT, "\t\t\t\t<Option type=\"5\" />\r\n" );
545 else if ( module.type == NativeCUI )
546 fprintf ( OUT, "\t\t\t\t<Option type=\"5\" />\r\n" );
547 else if ( module.type == Win32CUI || module.type == Win32GUI || module.type == Win32SCR)
548 {
549 if ( console )
550 fprintf ( OUT, "\t\t\t\t<Option type=\"1\" />\r\n" );
551 else
552 fprintf ( OUT, "\t\t\t\t<Option type=\"0\" />\r\n" );
553 }
554 }
555
556 fprintf ( OUT, "\t\t\t\t<Option compiler=\"gcc\" />\r\n" );
557
558 if ( module_type == ".cpl" )
559 {
560 fprintf ( OUT, "\t\t\t\t<Option parameters=\"shell32,Control_RunDLL &quot;$exe_output&quot;,@\" />\r\n" );
561 fprintf ( OUT, "\t\t\t\t<Option host_application=\"rundll32.exe\" />\r\n" );
562 }
563 fprintf ( OUT, "\t\t\t\t<Compiler>\r\n" );
564
565 bool debug = ( cfg.optimization == Debug );
566
567 if ( debug )
568 fprintf ( OUT, "\t\t\t\t\t<Add option=\"-g\" />\r\n" );
569
570 /* compiler flags */
571 for ( i = 0; i < compiler_flags.size(); i++ )
572 {
573 const string& cflag = compiler_flags[i];
574 fprintf ( OUT, "\t\t\t\t\t<Add option=\"%s\" />\r\n", cflag.c_str() );
575 }
576
577 /* defines */
578 for ( i = 0; i < common_defines.size(); i++ )
579 {
580 const string& define = common_defines[i];
581 fprintf ( OUT, "\t\t\t\t\t<Add option=\"-D%s\" />\r\n", define.c_str() );
582 }
583 /* includes */
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</Compiler>\r\n" );
590
591 /* includes */
592 fprintf ( OUT, "\t\t\t\t<ResourceCompiler>\r\n" );
593 for ( i = 0; i < includes.size(); i++ )
594 {
595 const string& include = includes[i];
596 fprintf ( OUT, "\t\t\t\t\t<Add directory=\"%s\" />\r\n", include.c_str() );
597 }
598 fprintf ( OUT, "\t\t\t\t</ResourceCompiler>\r\n" );
599
600 fprintf ( OUT, "\t\t\t\t<Linker>\r\n" );
601 fprintf ( OUT, "\t\t\t\t\t<Add option=\"%s\" />\r\n", project_linker_flags.c_str() );
602
603 if ( sys )
604 {
605 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 ());
606 fprintf ( OUT, "\t\t\t\t\t<Add option=\"-Wl,--image-base,%s\" />\r\n", baseaddr == "" ? "0x10000" : baseaddr.c_str () );
607 fprintf ( OUT, "\t\t\t\t\t<Add option=\"-nostartfiles -Wl,--nostdlib\" />\r\n" );
608 }
609 else if ( exe )
610 {
611 if ( module.type == Kernel )
612 {
613 fprintf ( OUT, "\t\t\t\t\t<Add option=\"-Wl,--entry,_KiSystemStartup\" />\r\n" );
614 fprintf ( OUT, "\t\t\t\t\t<Add option=\"-Wl,--image-base,%s\" />\r\n", baseaddr.c_str () );
615 }
616 else if ( module.type == NativeCUI )
617 {
618 fprintf ( OUT, "\t\t\t\t\t<Add option=\"-Wl,--entry,_NtProcessStartup@4\" />\r\n" );
619 fprintf ( OUT, "\t\t\t\t\t<Add option=\"-Wl,--image-base,%s\" />\r\n", baseaddr.c_str () );
620 fprintf ( OUT, "\t\t\t\t\t<Add option=\"-nostartfiles -Wl,--nostdlib\" />\r\n" );
621 }
622 else
623 {
624 fprintf ( OUT, "\t\t\t\t\t<Add option=\"%s\" />\r\n", module.cplusplus ? "-nostartfiles" : "-nostartfiles -Wl,--nostdlib" );
625 fprintf ( OUT, "\t\t\t\t\t<Add library=\"gcc\" />\r\n" );
626 }
627 }
628 else if ( dll )
629 {
630 fprintf ( OUT, "\t\t\t\t\t<Add option=\"-Wl,--entry,%s%s\" />\r\n", "_", module.GetEntryPoint(false).c_str () );
631 fprintf ( OUT, "\t\t\t\t\t<Add option=\"-Wl,--image-base,%s\" />\r\n", baseaddr == "" ? "0x40000" : baseaddr.c_str () );
632
633 if ( module.type == Win32DLL)
634 fprintf ( OUT, "\t\t\t\t\t<Add option=\"-Wl,--shared\" />\r\n" );
635 else if ( module.type == NativeDLL)
636 fprintf ( OUT, "\t\t\t\t\t<Add option=\"-Wl,--shared\" />\r\n" );
637 else if ( module.type == NativeDLL)
638 fprintf ( OUT, "\t\t\t\t\t<Add option=\"-nostartfiles -Wl,--shared\" />\r\n" );
639
640 fprintf ( OUT, "\t\t\t\t\t<Add option=\"%s\" />\r\n", module.cplusplus ? "-nostartfiles" : "-nostartfiles -Wl,--nostdlib" );
641 fprintf ( OUT, "\t\t\t\t\t<Add library=\"gcc\" />\r\n" );
642 }
643
644 fprintf ( OUT, "\t\t\t\t\t<Add option=\"-Wl,--file-alignment,0x1000\" />\r\n" );
645 fprintf ( OUT, "\t\t\t\t\t<Add option=\"-Wl,--section-alignment,0x1000\" />\r\n" );
646
647 if ( dll )
648 fprintf ( OUT, "\t\t\t\t\t<Add option=\"%s.temp.exp\" />\r\n", module.name.c_str() );
649
650 /* libraries */
651 for ( i = 0; i < libraries.size(); i++ )
652 {
653 const string& lib = libraries[i];
654 fprintf ( OUT, "\t\t\t\t\t<Add library=\"%s\" />\r\n", lib.c_str() );
655 }
656 for ( i = 0; i < libpaths.size(); i++ )
657 {
658 const string& lib = libpaths[i];
659 fprintf ( OUT, "\t\t\t\t\t<Add directory=\"%s\" />\r\n", lib.c_str() );
660 }
661 fprintf ( OUT, "\t\t\t\t</Linker>\r\n" );
662
663 fprintf ( OUT, "\t\t\t\t<ExtraCommands>\r\n" );
664
665 #if 0
666 if ( IsStaticLibrary ( module ) && module.importLibrary )
667 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" );
668 else if ( module.importLibrary != NULL )
669 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" );
670 #endif
671
672
673 for ( i = 0; i < resource_files.size(); i++ )
674 {
675 const string& resource_file = resource_files[i];
676 #ifdef WIN32
677 fprintf ( OUT, "\t\t\t\t\t<Add after=\"cmd /c del $(TARGET_OBJECT_DIR)%s%s.rci.tmp 2&gt;NUL\" />\r\n", sSep.c_str(), resource_file.c_str() );
678 fprintf ( OUT, "\t\t\t\t\t<Add after=\"cmd /c del $(TARGET_OBJECT_DIR)%s%s.res.tmp 2&gt;NUL\" />\r\n", sSep.c_str(), resource_file.c_str() );
679 #else
680 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() );
681 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() );
682 #endif
683 }
684
685 #if 0
686 if ( dll )
687 {
688 if (IsSpecDefinitionFile( module ))
689 fprintf ( OUT, "\t\t\t\t\t<Add before=\"%s%stools%swinebuild%swinebuild.exe -o %s --def -E %s.spec\" />\r\n", outdir.c_str(), sSep, sSep, sSep, module.importLibrary->definition.c_str(), module.name.c_str());
690 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" );
691 fprintf ( OUT, "\t\t\t\t\t<Add after=\"%s%stools%spefixup $exe_output -exports\" />\r\n", outdir.c_str(), sSep, sSep );
692 #ifdef WIN32
693 fprintf ( OUT, "\t\t\t\t\t<Add after=\"cmd /c del %s.temp.exp 2&gt;NUL\" />\r\n", module.name.c_str() );
694 #else
695 fprintf ( OUT, "\t\t\t\t\t<Add after=\"rm %s.temp.exp 2&gt;/dev/null\" />\r\n", module.name.c_str() );
696 #endif
697 fprintf ( OUT, "\t\t\t\t\t<Mode after=\"always\" />\r\n" );
698 }
699 #endif
700
701 fprintf ( OUT, "\t\t\t\t</ExtraCommands>\r\n" );
702
703 fprintf ( OUT, "\t\t\t</Target>\r\n" );
704
705 }
706
707 /* vars
708 fprintf ( OUT, "\t\t\t<Environment>\r\n" );
709 for ( i = 0; i < vars.size(); i++ )
710 {
711 const string& var = vars[i];
712 const string& value = values[i];
713 fprintf ( OUT, "\t\t\t\t<Variable name=\"%s\" value=\"%s\" />\r\n", var.c_str(), value.c_str() );
714 }
715 fprintf ( OUT, "\t\t\t</Environment>\r\n" ); */
716
717 fprintf ( OUT, "\t\t</Build>\r\n" );
718
719 #ifdef FORCE_CPP
720 CompilerVar = "CPP"
721 #else
722 if ( module.cplusplus )
723 CompilerVar = "CPP";
724 else
725 CompilerVar = "CC";
726 #endif
727
728 /* header files */
729 for ( i = 0; i < header_files.size(); i++ )
730 {
731 const string& header_file = header_files[i];
732 fprintf ( OUT, "\t\t<Unit filename=\"%s\">\r\n", header_file.c_str() );
733 fprintf ( OUT, "\t\t\t<Option compilerVar=\"%s\" />\r\n", CompilerVar.c_str() );
734 fprintf ( OUT, "\t\t\t<Option compile=\"0\" />\r\n" );
735 fprintf ( OUT, "\t\t\t<Option link=\"0\" />\r\n" );
736 for ( size_t icfg = 0; icfg < m_configurations.size(); icfg++ )
737 {
738 const CBConfiguration& cfg = *m_configurations[icfg];
739 fprintf ( OUT, "\t\t\t<Option target=\"%s\" />\r\n" , cfg.name.c_str() );
740 }
741 fprintf ( OUT, "\t\t</Unit>\r\n" );
742 }
743
744 /* source files */
745 for ( size_t isrcfile = 0; isrcfile < source_files.size(); isrcfile++ )
746 {
747 string source_file = DosSeparator(source_files[isrcfile]);
748 fprintf ( OUT, "\t\t<Unit filename=\"%s\">\r\n", source_file.c_str() );
749 fprintf ( OUT, "\t\t\t<Option compilerVar=\"%s\" />\r\n", CompilerVar.c_str() );
750
751 string extension = GetExtension ( source_file );
752 if ( extension == ".s" || extension == ".S" )
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=\"gcc -x assembler-with-cpp -c $file -o $link_objects $includes -D__ASM__ $options\" />\r\n" );
757 }
758 else if ( extension == ".asm" || extension == ".ASM" )
759 {
760 fprintf ( OUT, "\t\t\t<Option compile=\"1\" />\r\n" );
761 fprintf ( OUT, "\t\t\t<Option link=\"1\" />\r\n" );
762 fprintf ( OUT, "\t\t\t<Option compiler=\"gcc\" use=\"1\" buildCommand=\"nasm -f win32 $file -o $link_objects\" />\r\n" );
763 }
764 else if ( extension == ".idl" || extension == ".IDL" )
765 {
766 fprintf ( OUT, "\t\t\t<Option compile=\"1\" />\r\n" );
767 fprintf ( OUT, "\t\t\t<Option compiler=\"gcc\" use=\"1\" buildCommand=\"%s%stools%swidl%swidl.exe %s %s -h -H &quot;$(TARGET_OUTPUT_DIR)$filetitle_c.h&quot; -c -C &quot;$(TARGET_OUTPUT_DIR)$filetitle_c.c&quot; $file%sngcc %s -c &quot;$(TARGET_OUTPUT_DIR)$filetitle_c.c&quot; -o &quot;$(TARGET_OUTPUT_DIR)$file_c.o&quot;\" />\r\n", outdir.c_str(), sSep.c_str(), sSep.c_str(), sSep.c_str(), widl_options.c_str(), windres_defines.c_str(), sSep.c_str(), widl_options.c_str() );
768 }
769 else if ( extension == ".spec" || extension == ".SPEC" )
770 {
771 fprintf ( OUT, "\t\t\t<Option compile=\"1\" />\r\n" );
772 fprintf ( OUT, "\t\t\t<Option link=\"1\" />\r\n" );
773 fprintf ( OUT, "\t\t\t<Option compiler=\"gcc\" use=\"1\" buildCommand=\"%s%stools%swinebuild%swinebuild.exe -o $file.stubs.c --pedll $file\\n$compiler -c $options $includes $file.stubs.c -o $(TARGET_OBJECT_DIR)%s$file.o\" />\r\n", outdir.c_str(), sSep.c_str(), sSep.c_str(), sSep.c_str(), sSep.c_str() );
774 }
775
776 for ( size_t icfg = 0; icfg < m_configurations.size(); icfg++ )
777 {
778 const CBConfiguration& cfg = *m_configurations[icfg];
779 fprintf ( OUT, "\t\t\t<Option target=\"%s\" />\r\n" , cfg.name.c_str() );
780 }
781 fprintf ( OUT, "\t\t</Unit>\r\n" );
782 }
783
784 /* resource files */
785 for ( i = 0; i < resource_files.size(); i++ )
786 {
787 const string& resource_file = resource_files[i];
788 fprintf ( OUT, "\t\t<Unit filename=\"%s\">\r\n", resource_file.c_str() );
789 fprintf ( OUT, "\t\t\t<Option compilerVar=\"WINDRES\" />\r\n" );
790 string extension = GetExtension ( resource_file );
791 fprintf ( OUT, "\t\t\t<Option compiler=\"gcc\" use=\"1\" buildCommand=\"gcc -xc -E -DRC_INVOKED $includes %s $file -o $(TARGET_OBJECT_DIR)%s$file.rci.tmp\\n%s%stools%swrc%swrc.exe $includes %s $(TARGET_OBJECT_DIR)%s$file.rci.tmp $(TARGET_OBJECT_DIR)%s$file.res.tmp\\n$rescomp --output-format=coff $(TARGET_OBJECT_DIR)%s$file.res.tmp -o $resource_output\" />\r\n" , windres_defines.c_str(), sSep.c_str(), outdir.c_str(), sSep.c_str(), sSep.c_str(), sSep.c_str(), windres_defines.c_str(), sSep.c_str(), sSep.c_str(), sSep.c_str() );
792 for ( size_t icfg = 0; icfg < m_configurations.size(); icfg++ )
793 {
794 const CBConfiguration& cfg = *m_configurations[icfg];
795 fprintf ( OUT, "\t\t\t<Option target=\"%s\" />\r\n" , cfg.name.c_str() );
796 }
797 fprintf ( OUT, "\t\t</Unit>\r\n" );
798 }
799
800 fprintf ( OUT, "\t\t<Extensions />\r\n" );
801 fprintf ( OUT, "\t</Project>\r\n" );
802 fprintf ( OUT, "</CodeBlocks_project_file>\r\n" );
803
804
805 fclose ( OUT );
806 }
807
808 CBConfiguration::CBConfiguration ( const OptimizationType optimization, const std::string &name )
809 {
810 this->optimization = optimization;
811 if ( name != "" )
812 this->name = name;
813 else
814 {
815 if ( optimization == Debug )
816 this->name = "Debug";
817 else if ( optimization == Release )
818 this->name = "Release";
819 else
820 this->name = "Unknown";
821 }
822 }
823
824 std::string
825 CBBackend::_replace_str(std::string string1, const std::string &find_str, const std::string &replace_str)
826 {
827 std::string::size_type pos = string1.find(find_str, 0);
828 int intLen = find_str.length();
829
830 while(std::string::npos != pos)
831 {
832 string1.replace(pos, intLen, replace_str);
833 pos = string1.find(find_str, intLen + pos);
834 }
835
836 return string1;
837 }
838
839 std::string
840 CBBackend::GenerateProjectLinkerFlags() const
841 {
842 std::string lflags;
843 for ( size_t i = 0; i < ProjectNode.linkerFlags.size (); i++ )
844 {
845 LinkerFlag& linkerFlag = *ProjectNode.linkerFlags[i];
846 if ( lflags.length () > 0 )
847 lflags += " ";
848 lflags += linkerFlag.flag;
849 }
850 return lflags;
851 }
852
853 void
854 CBBackend::MingwAddImplicitLibraries( Module &module )
855 {
856 Library* pLibrary;
857
858 if ( !module.isDefaultEntryPoint )
859 return;
860
861 if ( module.IsDLL () )
862 {
863 //pLibrary = new Library ( module, "__mingw_dllmain" );
864 //module.non_if_data.libraries.insert ( module.non_if_data.libraries.begin(), pLibrary );
865 }
866 else
867 {
868 pLibrary = new Library ( module, module.isUnicode ? "mingw_wmain" : "mingw_main" );
869 module.non_if_data.libraries.insert ( module.non_if_data.libraries.begin(), pLibrary );
870 }
871
872 pLibrary = new Library ( module, "mingw_common" );
873 module.non_if_data.libraries.insert ( module.non_if_data.libraries.begin() + 1, pLibrary );
874
875 if ( module.name != "msvcrt" )
876 {
877 // always link in msvcrt to get the basic routines
878 pLibrary = new Library ( module, "msvcrt" );
879 module.non_if_data.libraries.push_back ( pLibrary );
880 }
881 }
882
883 const Property*
884 CBBackend::_lookup_property ( const Module& module, const std::string& name ) const
885 {
886 std::map<std::string, Property*>::const_iterator p;
887
888 /* Check local values */
889 p = module.non_if_data.properties.find(name);
890
891 if ( p != module.non_if_data.properties.end() )
892 return p->second;
893
894 // TODO FIXME - should we check local if-ed properties?
895 p = module.project.non_if_data.properties.find(name);
896
897 if ( p != module.project.non_if_data.properties.end() )
898 return p->second;
899
900 // TODO FIXME - should we check global if-ed properties?
901 return NULL;
902 }
903
904 bool
905 CBBackend::IsSpecDefinitionFile ( const Module& module ) const
906 {
907 if ( module.importLibrary == NULL)
908 return false;
909
910 size_t index = module.importLibrary->source->name.rfind ( ".spec" );
911 return ( index != string::npos );
912 }