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