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