[KERNEL32]
[reactos.git] / reactos / tools / rbuild / backend / mingw / modulehandler.cpp
1 /*
2 * Copyright (C) 2005 Casper S. Hornstrup
3 * 2007-2008 Hervé Poussineau
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19 #include "../../pch.h"
20 #include <assert.h>
21 #include <algorithm>
22
23 #include "../../rbuild.h"
24 #include "mingw.h"
25 #include "modulehandler.h"
26 #include "rule.h"
27
28 using std::set;
29 using std::string;
30 using std::vector;
31
32 #define CLEAN_FILE(f) clean_files.push_back ( (f).name.length () > 0 ? backend->GetFullName ( f ) : backend->GetFullPath ( f ) );
33 #define IsStaticLibrary( module ) ( ( module.type == StaticLibrary ) || ( module.type == HostStaticLibrary ) )
34
35 MingwBackend*
36 MingwModuleHandler::backend = NULL;
37 FILE*
38 MingwModuleHandler::fMakefile = NULL;
39
40 string
41 PrefixFilename (
42 const string& filename,
43 const string& prefix )
44 {
45 if ( !prefix.length() )
46 return filename;
47 string out;
48 const char* pfilename = filename.c_str();
49 const char* p1 = strrchr ( pfilename, '/' );
50 const char* p2 = strrchr ( pfilename, '\\' );
51 if ( p1 || p2 )
52 {
53 if ( p2 > p1 )
54 p1 = p2;
55 out += string(pfilename,p1-pfilename) + cSep;
56 pfilename = p1 + 1;
57 }
58 out += prefix + pfilename;
59 return out;
60 }
61
62 string
63 GetTargetMacro ( const Module& module, bool with_dollar )
64 {
65 string s ( module.name );
66 s += "_TARGET";
67 if ( with_dollar )
68 return ssprintf ( "$(%s)", s.c_str() );
69 return s;
70 }
71
72 MingwModuleHandler::MingwModuleHandler (
73 const Module& module_ )
74
75 : module(module_)
76 {
77 use_pch = false;
78 }
79
80 MingwModuleHandler::~MingwModuleHandler()
81 {
82 }
83
84 /*static*/ void
85 MingwModuleHandler::SetBackend ( MingwBackend* backend_ )
86 {
87 backend = backend_;
88 }
89
90 /*static*/ void
91 MingwModuleHandler::SetMakefile ( FILE* f )
92 {
93 fMakefile = f;
94 }
95
96 void
97 MingwModuleHandler::EnablePreCompiledHeaderSupport ()
98 {
99 use_pch = true;
100 }
101
102 /*static*/ const FileLocation*
103 MingwModuleHandler::PassThruCacheDirectory (const FileLocation* file )
104 {
105 switch ( file->directory )
106 {
107 case SourceDirectory:
108 break;
109 case IntermediateDirectory:
110 backend->AddDirectoryTarget ( file->relative_path, backend->intermediateDirectory );
111 break;
112 case OutputDirectory:
113 backend->AddDirectoryTarget ( file->relative_path, backend->outputDirectory );
114 break;
115 case InstallDirectory:
116 backend->AddDirectoryTarget ( file->relative_path, backend->installDirectory );
117 break;
118 default:
119 throw InvalidOperationException ( __FILE__,
120 __LINE__,
121 "Invalid directory %d.",
122 file->directory );
123 }
124
125 return file;
126 }
127
128 /* caller needs to delete the returned object */
129 const FileLocation*
130 MingwModuleHandler::GetTargetFilename (
131 const Module& module,
132 string_list* pclean_files )
133 {
134 FileLocation *target = new FileLocation ( *module.output );
135 if ( pclean_files )
136 {
137 string_list& clean_files = *pclean_files;
138 CLEAN_FILE ( *target );
139 }
140 return target;
141 }
142
143 /* caller needs to delete the returned object */
144 const FileLocation*
145 MingwModuleHandler::GetImportLibraryFilename (
146 const Module& module,
147 string_list* pclean_files,
148 bool delayimp )
149 {
150 FileLocation *target;
151
152 if (module.HasImportLibrary())
153 {
154 if (delayimp)
155 {
156 target = new FileLocation ( *module.delayImportLibrary->target );
157 }
158 else
159 target = new FileLocation ( *module.importLibrary->target );
160 }
161 else
162 target = new FileLocation ( *module.dependency );
163
164 if ( pclean_files )
165 {
166 string_list& clean_files = *pclean_files;
167 CLEAN_FILE ( *target );
168 }
169 return target;
170 }
171
172 /* caller needs to delete the returned object */
173 MingwModuleHandler*
174 MingwModuleHandler::InstanciateHandler (
175 const Module& module,
176 MingwBackend* backend )
177 {
178 MingwModuleHandler* handler;
179 switch ( module.type )
180 {
181 case StaticLibrary:
182 case HostStaticLibrary:
183 case ObjectLibrary:
184 case RpcServer:
185 case RpcClient:
186 case RpcProxy:
187 case MessageHeader:
188 case IdlHeader:
189 case IdlInterface:
190 case EmbeddedTypeLib:
191 case BootSector:
192 handler = new MingwModuleHandler( module );
193 break;
194 case BuildTool:
195 handler = new MingwBuildToolModuleHandler ( module );
196 break;
197 case Kernel:
198 handler = new MingwKernelModuleHandler ( module );
199 break;
200 case NativeCUI:
201 handler = new MingwNativeCUIModuleHandler ( module );
202 break;
203 case Win32CUI:
204 handler = new MingwWin32CUIModuleHandler ( module );
205 break;
206 case Win32SCR:
207 case Win32GUI:
208 handler = new MingwWin32GUIModuleHandler ( module );
209 break;
210 case KeyboardLayout:
211 case KernelModeDLL:
212 case KernelModeDriver:
213 handler = new MingwKernelModeDLLModuleHandler ( module );
214 break;
215 case NativeDLL:
216 handler = new MingwNativeDLLModuleHandler ( module );
217 break;
218 case Win32DLL:
219 handler = new MingwWin32DLLModuleHandler ( module );
220 break;
221 case Win32OCX:
222 handler = new MingwWin32OCXModuleHandler ( module );
223 break;
224 case BootLoader:
225 handler = new MingwBootLoaderModuleHandler ( module );
226 break;
227 case BootProgram:
228 handler = new MingwBootProgramModuleHandler ( module );
229 break;
230 case Iso:
231 handler = new MingwIsoModuleHandler ( module );
232 break;
233 case LiveIso:
234 handler = new MingwLiveIsoModuleHandler ( module );
235 break;
236 case Test:
237 handler = new MingwTestModuleHandler ( module );
238 break;
239 case Alias:
240 handler = new MingwAliasModuleHandler ( module );
241 break;
242 case Cabinet:
243 handler = new MingwCabinetModuleHandler ( module );
244 break;
245 case ElfExecutable:
246 handler = new MingwElfExecutableModuleHandler ( module );
247 break;
248 default:
249 throw UnknownModuleTypeException (
250 module.node.location,
251 module.type );
252 break;
253 }
254 return handler;
255 }
256
257 string
258 MingwModuleHandler::GetWorkingDirectory () const
259 {
260 return ".";
261 }
262
263 string
264 MingwModuleHandler::GetBasename ( const string& filename ) const
265 {
266 size_t index = filename.find_last_of ( '.' );
267 if ( index != string::npos )
268 return filename.substr ( 0, index );
269 return "";
270 }
271
272 string
273 MingwModuleHandler::GetCompilationUnitDependencies (
274 const CompilationUnit& compilationUnit ) const
275 {
276 if ( compilationUnit.GetFiles ().size () <= 1 )
277 return "";
278 vector<string> sourceFiles;
279 for ( size_t i = 0; i < compilationUnit.GetFiles ().size (); i++ )
280 {
281 const File& file = *compilationUnit.GetFiles ()[i];
282 sourceFiles.push_back ( backend->GetFullName ( file.file ) );
283 }
284 return string ( " " ) + v2s ( sourceFiles, 10 );
285 }
286
287 /* caller needs to delete the returned object */
288 const FileLocation*
289 MingwModuleHandler::GetModuleArchiveFilename () const
290 {
291 if ( IsStaticLibrary ( module ) )
292 return GetTargetFilename ( module, NULL );
293 return NULL;
294 }
295
296 /*static*/ bool
297 MingwModuleHandler::ReferenceObjects (
298 const Module& module )
299 {
300 if ( module.type == ObjectLibrary )
301 return true;
302 if ( module.type == RpcServer )
303 return true;
304 if ( module.type == RpcClient )
305 return true;
306 if ( module.type == RpcProxy )
307 return true;
308 if ( module.type == IdlHeader )
309 return true;
310 if ( module.type == IdlInterface )
311 return true;
312 if ( module.type == MessageHeader)
313 return true;
314 return false;
315 }
316
317 void
318 MingwModuleHandler::OutputCopyCommand ( const FileLocation& source,
319 const FileLocation& destination )
320 {
321 fprintf ( fMakefile, "# OUTPUT COPY COMMAND\n" );
322 fprintf ( fMakefile,
323 "\t$(ECHO_CP)\n" );
324 fprintf ( fMakefile,
325 "\t${cp} %s %s 1>$(NUL)\n",
326 backend->GetFullName ( source ).c_str (),
327 backend->GetFullName ( *PassThruCacheDirectory ( &destination ) ).c_str () );
328 }
329
330 void
331 MingwModuleHandler::OutputCopyCommandSingle ( const FileLocation& source,
332 const FileLocation& destination )
333 {
334 fprintf ( fMakefile,
335 "%s : %s\n",
336 backend->GetFullName ( *PassThruCacheDirectory ( &destination ) ).c_str (),
337 backend->GetFullName ( source ).c_str () );
338 fprintf ( fMakefile,
339 "\t$(ECHO_CP)\n" );
340 fprintf ( fMakefile,
341 "\t${cp} %s %s 1>$(NUL)\n",
342 backend->GetFullName ( source ).c_str (),
343 backend->GetFullName ( *PassThruCacheDirectory ( &destination ) ).c_str () );
344 }
345
346 string
347 MingwModuleHandler::GetImportLibraryDependency (
348 const Module& importedModule,
349 bool delayimp )
350 {
351 string dep;
352 if ( ReferenceObjects ( importedModule ) )
353 {
354 const vector<CompilationUnit*>& compilationUnits = importedModule.non_if_data.compilationUnits;
355 size_t i;
356
357 dep = GetTargetMacro ( importedModule );
358 for ( i = 0; i < compilationUnits.size (); i++ )
359 {
360 CompilationUnit& compilationUnit = *compilationUnits[i];
361 const FileLocation& compilationName = compilationUnit.GetFilename ();
362 const FileLocation *objectFilename = GetObjectFilename ( &compilationName, importedModule );
363 if ( GetExtension ( *objectFilename ) == ".h" )
364 dep += ssprintf ( " $(%s_HEADERS)", importedModule.name.c_str () );
365 else if ( GetExtension ( *objectFilename ) == ".rc" )
366 dep += ssprintf ( " $(%s_MCHEADERS)", importedModule.name.c_str () );
367 }
368 }
369 else
370 {
371 const FileLocation *library_target = GetImportLibraryFilename ( importedModule, NULL, delayimp );
372 dep = backend->GetFullName ( *library_target );
373 delete library_target;
374 }
375
376 if ( IsStaticLibrary ( importedModule ) || importedModule.type == ObjectLibrary )
377 {
378 const std::vector<Library*>& libraries = importedModule.non_if_data.libraries;
379
380 for ( size_t i = 0; i < libraries.size (); ++ i )
381 {
382 dep += " ";
383 dep += GetImportLibraryDependency ( *libraries[i]->importedModule, libraries[i]->delayimp );
384 }
385 }
386
387 return dep;
388 }
389
390 void
391 MingwModuleHandler::GetTargets ( const Module& dependencyModule,
392 string_list& targets )
393 {
394 if ( dependencyModule.invocations.size () > 0 )
395 {
396 for ( size_t i = 0; i < dependencyModule.invocations.size (); i++ )
397 {
398 Invoke& invoke = *dependencyModule.invocations[i];
399 invoke.GetTargets ( targets );
400 }
401 }
402 else
403 targets.push_back ( GetImportLibraryDependency ( dependencyModule, false ) );
404 }
405
406 void
407 MingwModuleHandler::GetModuleDependencies (
408 string_list& dependencies )
409 {
410 size_t iend = module.dependencies.size ();
411
412 if ( iend == 0 )
413 return;
414
415 for ( size_t i = 0; i < iend; i++ )
416 {
417 const Dependency& dependency = *module.dependencies[i];
418 const Module& dependencyModule = *dependency.dependencyModule;
419 GetTargets ( dependencyModule,
420 dependencies );
421 }
422 vector<FileLocation> v;
423 GetDefinitionDependencies ( v );
424
425 for ( size_t i = 0; i < v.size (); i++ )
426 {
427 const FileLocation& file = v[i];
428 dependencies.push_back ( backend->GetFullName ( file ) );
429 }
430 }
431
432 /* caller needs to delete the returned object */
433 const FileLocation*
434 MingwModuleHandler::GetObjectFilename (
435 const FileLocation* sourceFile,
436 const Module& module ) const
437 {
438 DirectoryLocation destination_directory;
439 string newExtension;
440 string extension = GetExtension ( *sourceFile );
441
442 if ( module.type == BootSector )
443 return new FileLocation ( *module.output );
444 else if (extension == ".rc")
445 newExtension = "_" + module.name + ".coff";
446 else if (extension == ".mc")
447 newExtension = ".rc";
448 else if (extension == ".idl")
449 {
450 if ( module.type == RpcServer )
451 newExtension = "_s.o";
452 else if ( module.type == RpcClient )
453 newExtension = "_c.o";
454 else if ( module.type == RpcProxy )
455 newExtension = "_p.o";
456 else if ( module.type == IdlInterface )
457 newExtension = "_i.o";
458 else
459 newExtension = ".h";
460 }
461 else
462 newExtension = "_" + module.name + ".o";
463
464 if ( module.type == BootSector )
465 destination_directory = OutputDirectory;
466 else
467 destination_directory = IntermediateDirectory;
468
469 const FileLocation *obj_file = new FileLocation(
470 destination_directory,
471 sourceFile->relative_path,
472 ReplaceExtension ( sourceFile->name, newExtension ) );
473 PassThruCacheDirectory ( obj_file );
474
475 return obj_file;
476 }
477
478 string
479 MingwModuleHandler::GetModuleCleanTarget ( const Module& module ) const
480 {
481 return module.name + "_clean";
482 }
483
484 void
485 MingwModuleHandler::GetReferencedObjectLibraryModuleCleanTargets ( vector<string>& moduleNames ) const
486 {
487 for ( size_t i = 0; i < module.non_if_data.libraries.size (); i++ )
488 {
489 Library& library = *module.non_if_data.libraries[i];
490 if ( library.importedModule->type == ObjectLibrary )
491 moduleNames.push_back ( GetModuleCleanTarget ( *library.importedModule ) );
492 }
493 }
494
495 void
496 MingwModuleHandler::GenerateCleanTarget () const
497 {
498 if ( module.type == Alias )
499 return;
500
501 fprintf ( fMakefile, "# CLEAN TARGET\n" );
502 fprintf ( fMakefile,
503 ".PHONY: %s_clean\n",
504 module.name.c_str() );
505 vector<string> referencedModuleNames;
506 GetReferencedObjectLibraryModuleCleanTargets ( referencedModuleNames );
507 fprintf ( fMakefile,
508 "%s: %s\n\t-@${rm}",
509 GetModuleCleanTarget ( module ).c_str(),
510 v2s ( referencedModuleNames, 10 ).c_str () );
511 for ( size_t i = 0; i < clean_files.size(); i++ )
512 {
513 if ( ( i + 1 ) % 10 == 9 )
514 fprintf ( fMakefile, " 2>$(NUL)\n\t-@${rm}" );
515 fprintf ( fMakefile, " %s", clean_files[i].c_str() );
516 }
517 fprintf ( fMakefile, " 2>$(NUL)\n" );
518
519 // Clean files generated by external rules
520 fprintf ( fMakefile, "\t-@${rm} $(%s_CLEANFILES) 2>$(NUL)\n", module.name.c_str() );
521
522 if( ProxyMakefile::GenerateProxyMakefile(module) )
523 {
524 DirectoryLocation root;
525
526 if ( backend->configuration.GenerateProxyMakefilesInSourceTree )
527 root = SourceDirectory;
528 else
529 root = OutputDirectory;
530
531 FileLocation proxyMakefile ( root,
532 module.output->relative_path,
533 "GNUmakefile" );
534 fprintf ( fMakefile, "\t-@${rm} %s 2>$(NUL)\n",
535 backend->GetFullName ( proxyMakefile ).c_str () );
536 }
537
538 fprintf ( fMakefile, "clean: %s_clean\n\n", module.name.c_str() );
539 }
540
541 void
542 MingwModuleHandler::GenerateInstallTarget () const
543 {
544 if ( !module.install )
545 return;
546 fprintf ( fMakefile, "# INSTALL TARGET\n" );
547 fprintf ( fMakefile, ".PHONY: %s_install\n", module.name.c_str() );
548 fprintf ( fMakefile,
549 "%s_install: %s\n",
550 module.name.c_str (),
551 backend->GetFullName ( *module.install ).c_str () );
552 }
553
554 void
555 MingwModuleHandler::GenerateDependsTarget () const
556 {
557 fprintf ( fMakefile, "# DEPENDS TARGET\n" );
558 fprintf ( fMakefile,
559 ".PHONY: %s_depends\n",
560 module.name.c_str() );
561 fprintf ( fMakefile,
562 "%s_depends: $(RBUILD_TARGET)\n",
563 module.name.c_str () );
564 fprintf ( fMakefile,
565 "\t$(ECHO_RBUILD)\n" );
566 fprintf ( fMakefile,
567 "\t$(Q)$(RBUILD_TARGET) $(RBUILD_FLAGS) -dm%s mingw\n",
568 module.name.c_str () );
569 }
570
571 static
572 const char * const CompilerPrefixTable [ CompilerTypesCount ] =
573 {
574 "C",
575 "CXX",
576 "CPP",
577 "AS",
578 "MIDL",
579 "RC",
580 "NASM",
581 };
582
583 /* static */ void
584 MingwModuleHandler::GenerateParameters (
585 const char* prefix,
586 const char* assignmentOperation,
587 const IfableData& data )
588 {
589 for ( unsigned type = CompilerTypeCC; type < CompilerTypesCount; ++ type )
590 {
591 CompilerType compiler = static_cast < CompilerType > ( type );
592
593 // Includes
594 std::string includes = GenerateIncludeParametersFromVector ( data.includes, compiler );
595
596 if ( includes.size() )
597 {
598 fprintf ( fMakefile,
599 "%s_%sINCLUDES%s%s\n",
600 prefix,
601 CompilerPrefixTable [ compiler ],
602 assignmentOperation,
603 includes.c_str () );
604 }
605
606 // Defines
607 std::string defines = GenerateDefineParametersFromVector ( data.defines, compiler );
608
609 if ( defines.size() )
610 {
611 fprintf ( fMakefile,
612 "%s_%sDEFINES%s%s\n",
613 prefix,
614 CompilerPrefixTable [ compiler ],
615 assignmentOperation,
616 defines.c_str () );
617 }
618
619 // Flags
620 std::string flags = GenerateCompilerParametersFromVector ( data.compilerFlags, compiler );
621
622 if ( flags.size() )
623 {
624 fprintf ( fMakefile,
625 "%s_%sFLAGS%s%s\n",
626 prefix,
627 CompilerPrefixTable [ compiler ],
628 assignmentOperation,
629 flags.c_str () );
630 }
631 }
632 }
633
634 /* static */ string
635 MingwModuleHandler::GenerateGccDefineParametersFromVector (
636 const vector<Define*>& defines,
637 set<string>& used_defs)
638 {
639 string parameters;
640
641 for ( size_t i = 0; i < defines.size (); i++ )
642 {
643 Define& define = *defines[i];
644 if (used_defs.find(define.name) != used_defs.end())
645 continue;
646 if (define.redefine)
647 {
648 if (parameters.length () > 0)
649 parameters += " ";
650 parameters += "-U";
651 parameters += define.name;
652 }
653 if (parameters.length () > 0)
654 parameters += " ";
655 if (define.arguments.length ())
656 parameters += "$(QT)";
657 parameters += "-D";
658 parameters += define.name;
659 parameters += define.arguments;
660 if (define.value.length () > 0)
661 {
662 parameters += "=";
663 parameters += define.value;
664 }
665 if (define.arguments.length ())
666 parameters += "$(QT)";
667 used_defs.insert(used_defs.begin(),define.name);
668 }
669 return parameters;
670 }
671
672 /* static */ string
673 MingwModuleHandler::GenerateDefineParametersFromVector (
674 const std::vector<Define*>& defines,
675 CompilerType compiler )
676 {
677 string parameters;
678
679 for ( size_t i = 0; i < defines.size (); i++ )
680 {
681 Define& define = *defines[i];
682 if (!define.IsCompilerSet (compiler))
683 continue;
684 if (define.redefine)
685 {
686 if (parameters.length () > 0)
687 parameters += " ";
688 parameters += "-U";
689 parameters += define.name;
690 }
691 if (parameters.length () > 0)
692 parameters += " ";
693 if (define.arguments.length ())
694 parameters += "$(QT)";
695 parameters += "-D";
696 parameters += define.name;
697 parameters += define.arguments;
698 if (define.value.length () > 0)
699 {
700 parameters += "=";
701 parameters += define.value;
702 }
703 if (define.arguments.length ())
704 parameters += "$(QT)";
705 }
706 return parameters;
707 }
708
709 string
710 MingwModuleHandler::ConcatenatePaths (
711 const string& path1,
712 const string& path2 ) const
713 {
714 if ( ( path1.length () == 0 ) || ( path1 == "." ) || ( path1 == "./" ) )
715 return path2;
716 if ( path1[path1.length ()] == cSep )
717 return path1 + path2;
718 else
719 return path1 + cSep + path2;
720 }
721
722 /* static */ string
723 MingwModuleHandler::GenerateIncludeParametersFromVector ( const vector<Include*>& includes, const CompilerType type )
724 {
725 string parameters, path_prefix;
726 for ( size_t i = 0; i < includes.size (); i++ )
727 {
728 Include& include = *includes[i];
729 if ( include.IsCompilerSet( type ) )
730 parameters += " -I" + backend->GetFullPath ( *include.directory );
731 }
732 return parameters;
733 }
734
735 /* static */ string
736 MingwModuleHandler::GenerateCompilerParametersFromVector ( const vector<CompilerFlag*>& compilerFlags, const CompilerType type )
737 {
738 string parameters;
739 for ( size_t i = 0; i < compilerFlags.size (); i++ )
740 {
741 CompilerFlag& compilerFlag = *compilerFlags[i];
742 if ( compilerFlag.IsCompilerSet( type ) )
743 parameters += " " + compilerFlag.flag;
744 }
745 return parameters;
746 }
747
748 string
749 MingwModuleHandler::GenerateLinkerParametersFromVector ( const vector<LinkerFlag*>& linkerFlags ) const
750 {
751 string parameters;
752 for ( size_t i = 0; i < linkerFlags.size (); i++ )
753 {
754 LinkerFlag& linkerFlag = *linkerFlags[i];
755 if ( parameters.length () > 0 )
756 parameters += " ";
757 parameters += linkerFlag.flag;
758 }
759 return parameters;
760 }
761
762 string
763 MingwModuleHandler::GenerateImportLibraryDependenciesFromVector (
764 const vector<Library*>& libraries )
765 {
766 string dependencies ( "" );
767 int wrap_count = 0;
768 for ( size_t i = 0; i < libraries.size (); i++ )
769 {
770 if ( wrap_count++ == 5 )
771 dependencies += " \\\n\t\t", wrap_count = 0;
772 else if ( dependencies.size () > 0 )
773 dependencies += " ";
774 dependencies += GetImportLibraryDependency ( *libraries[i]->importedModule, libraries[i]->delayimp );
775 }
776 return dependencies;
777 }
778
779 string
780 MingwModuleHandler::GenerateLinkerParameters () const
781 {
782 return GenerateLinkerParametersFromVector ( module.linkerFlags );
783 }
784
785 void
786 MingwModuleHandler::GenerateMacros (
787 const char* assignmentOperation,
788 const IfableData& data,
789 const vector<LinkerFlag*>* linkerFlags,
790 set<const Define *>& used_defs )
791 {
792 if ( linkerFlags != NULL )
793 {
794 string linkerParameters = GenerateLinkerParametersFromVector ( *linkerFlags );
795 if ( linkerParameters.size () > 0 )
796 {
797 fprintf (
798 fMakefile,
799 "%s %s %s\n",
800 linkerflagsMacro.c_str (),
801 assignmentOperation,
802 linkerParameters.c_str() );
803 }
804 }
805
806 if ( data.libraries.size () > 0 )
807 {
808 // Check if host and target modules are not mixed up
809 HostType current = ModuleHandlerInformations[module.type].DefaultHost;
810 std::vector<Library*>::const_iterator it;
811 for ( it = data.libraries.begin(); it != data.libraries.end(); ++it )
812 {
813 HostType imported = ModuleHandlerInformations[(*it)->importedModule->type].DefaultHost;
814 if (current != imported)
815 {
816 throw InvalidOperationException ( __FILE__,
817 __LINE__,
818 "Module '%s' imports module '%s', which is not of the right type",
819 module.name.c_str (),
820 (*it)->importedModule->name.c_str () );
821 }
822 }
823
824 string deps = GenerateImportLibraryDependenciesFromVector ( data.libraries );
825 if ( deps.size () > 0 )
826 {
827 fprintf (
828 fMakefile,
829 "%s %s %s\n",
830 libsMacro.c_str(),
831 assignmentOperation,
832 deps.c_str() );
833 }
834 }
835 }
836
837 void
838 MingwModuleHandler::CleanupCompilationUnitVector ( vector<CompilationUnit*>& compilationUnits )
839 {
840 for ( size_t i = 0; i < compilationUnits.size (); i++ )
841 delete compilationUnits[i];
842 }
843
844 void
845 MingwModuleHandler::GetModuleSpecificCompilationUnits ( vector<CompilationUnit*>& compilationUnits )
846 {
847 }
848
849 void
850 MingwModuleHandler::GenerateSourceMacros (
851 const IfableData& data )
852 {
853 size_t i;
854
855 const vector<CompilationUnit*>& compilationUnits = data.compilationUnits;
856 vector<const FileLocation *> headers;
857 if ( compilationUnits.size () > 0 )
858 {
859 fprintf (
860 fMakefile,
861 "%s =",
862 sourcesMacro.c_str () );
863 for ( i = 0; i < compilationUnits.size(); i++ )
864 {
865 CompilationUnit& compilationUnit = *compilationUnits[i];
866 const FileLocation& compilationName = compilationUnit.GetFilename ();
867 fprintf (
868 fMakefile,
869 "%s%s",
870 ( i%10 == 9 ? " \\\n\t" : " " ),
871 backend->GetFullName ( compilationName ).c_str () );
872 }
873 fprintf ( fMakefile, "\n" );
874 }
875
876 vector<CompilationUnit*> sourceCompilationUnits;
877 GetModuleSpecificCompilationUnits ( sourceCompilationUnits );
878 for ( i = 0; i < sourceCompilationUnits.size (); i++ )
879 {
880 const FileLocation& compilationName = sourceCompilationUnits[i]->GetFilename ();
881 fprintf (
882 fMakefile,
883 "%s += %s\n",
884 sourcesMacro.c_str(),
885 backend->GetFullName ( compilationName ).c_str () );
886 }
887 CleanupCompilationUnitVector ( sourceCompilationUnits );
888 }
889
890 void
891 MingwModuleHandler::GenerateObjectMacros (
892 const IfableData& data )
893 {
894 size_t i;
895 const char* assignmentOperation = "=";
896
897 const vector<CompilationUnit*>& compilationUnits = data.compilationUnits;
898 vector<const FileLocation *> headers;
899 vector<const FileLocation *> mcheaders;
900 vector<const FileLocation *> mcresources;
901 if ( compilationUnits.size () > 0 )
902 {
903 for ( i = 0; i < compilationUnits.size (); i++ )
904 {
905 CompilationUnit& compilationUnit = *compilationUnits[i];
906 if ( compilationUnit.IsFirstFile () )
907 {
908 const FileLocation& compilationName = compilationUnit.GetFilename ();
909 const FileLocation *object_file = GetObjectFilename ( &compilationName, module );
910 fprintf ( fMakefile,
911 "%s := %s\n",
912 objectsMacro.c_str(),
913 backend->GetFullName ( *object_file ).c_str () );
914 delete object_file;
915 assignmentOperation = "+=";
916 break;
917 }
918 }
919 fprintf (
920 fMakefile,
921 "%s %s",
922 objectsMacro.c_str (),
923 assignmentOperation );
924 for ( i = 0; i < compilationUnits.size(); i++ )
925 {
926 CompilationUnit& compilationUnit = *compilationUnits[i];
927 if ( !compilationUnit.IsFirstFile () )
928 {
929 const FileLocation& compilationName = compilationUnit.GetFilename ();
930 const FileLocation *objectFilename = GetObjectFilename ( &compilationName, module );
931 if ( GetExtension ( *objectFilename ) == ".h" )
932 headers.push_back ( objectFilename );
933 else if ( GetExtension ( *objectFilename ) == ".rc" )
934 {
935 const FileLocation *headerFilename = GetMcHeaderFilename ( &compilationUnit.GetFilename () );
936 mcheaders.push_back ( headerFilename );
937 mcresources.push_back ( objectFilename );
938 }
939 else
940 {
941 fprintf (
942 fMakefile,
943 "%s%s",
944 ( i%10 == 9 ? " \\\n\t" : " " ),
945 backend->GetFullName ( *objectFilename ).c_str () );
946 delete objectFilename;
947 }
948 }
949 }
950 fprintf ( fMakefile, "\n" );
951 }
952 if ( headers.size () > 0 )
953 {
954 fprintf (
955 fMakefile,
956 "%s_HEADERS %s",
957 module.name.c_str (),
958 assignmentOperation );
959 for ( i = 0; i < headers.size (); i++ )
960 {
961 fprintf (
962 fMakefile,
963 "%s%s",
964 ( i%10 == 9 ? " \\\n\t" : " " ),
965 backend->GetFullName ( *headers[i] ).c_str () );
966 delete headers[i];
967 }
968 fprintf ( fMakefile, "\n" );
969 }
970
971 if ( mcheaders.size () > 0 )
972 {
973 fprintf (
974 fMakefile,
975 "%s_MCHEADERS %s",
976 module.name.c_str (),
977 assignmentOperation );
978 for ( i = 0; i < mcheaders.size (); i++ )
979 {
980 fprintf (
981 fMakefile,
982 "%s%s",
983 ( i%10 == 9 ? " \\\n\t" : " " ),
984 backend->GetFullName ( *mcheaders[i] ).c_str () );
985 delete mcheaders[i];
986 }
987 fprintf ( fMakefile, "\n" );
988 }
989
990 if ( mcresources.size () > 0 )
991 {
992 fprintf (
993 fMakefile,
994 "%s_RESOURCES %s",
995 module.name.c_str (),
996 assignmentOperation );
997 for ( i = 0; i < mcresources.size (); i++ )
998 {
999 fprintf (
1000 fMakefile,
1001 "%s%s",
1002 ( i%10 == 9 ? " \\\n\t" : " " ),
1003 backend->GetFullName ( *mcresources[i] ).c_str () );
1004 delete mcresources[i];
1005 }
1006 fprintf ( fMakefile, "\n" );
1007 }
1008
1009 vector<CompilationUnit*> sourceCompilationUnits;
1010 GetModuleSpecificCompilationUnits ( sourceCompilationUnits );
1011 for ( i = 0; i < sourceCompilationUnits.size (); i++ )
1012 {
1013 const FileLocation& compilationName = sourceCompilationUnits[i]->GetFilename ();
1014 const FileLocation *object_file = GetObjectFilename ( &compilationName, module );
1015 fprintf (
1016 fMakefile,
1017 "%s += %s\n",
1018 objectsMacro.c_str(),
1019 backend->GetFullName ( *object_file ).c_str () );
1020 delete object_file;
1021 }
1022 CleanupCompilationUnitVector ( sourceCompilationUnits );
1023
1024 if ( module.IsSpecDefinitionFile() )
1025 {
1026 const FileLocation *stubs_file = new FileLocation(
1027 IntermediateDirectory,
1028 module.importLibrary->source->relative_path,
1029 ReplaceExtension ( module.importLibrary->source->name, "_" + module.name + ".stubs.o" ) );
1030
1031 fprintf (
1032 fMakefile,
1033 "%s += %s\n",
1034 objectsMacro.c_str(),
1035 backend->GetFullName ( *stubs_file ).c_str () );
1036
1037 delete stubs_file;
1038 }
1039
1040 if ( module.type == RpcProxy )
1041 {
1042 const FileLocation *dlldata_file = GetDlldataFilename();
1043
1044 fprintf (
1045 fMakefile,
1046 "%s += %s\n",
1047 objectsMacro.c_str(),
1048 ReplaceExtension ( backend->GetFullName ( *dlldata_file ), ".o" ).c_str() );
1049
1050 delete dlldata_file;
1051 }
1052 }
1053
1054 const FileLocation*
1055 MingwModuleHandler::GetDlldataFilename() const
1056 {
1057 std::string dlldata_path = "";
1058 size_t dlldata_path_len = module.xmlbuildFile.find_last_of(cSep);
1059
1060 if ( dlldata_path_len != std::string::npos && dlldata_path_len != 0 )
1061 dlldata_path = module.xmlbuildFile.substr(0, dlldata_path_len);
1062
1063 return new FileLocation( IntermediateDirectory, dlldata_path, module.name + ".dlldata.c" );
1064 }
1065
1066 const FileLocation*
1067 MingwModuleHandler::GetPrecompiledHeaderPath () const
1068 {
1069 if ( !module.pch || !use_pch )
1070 return NULL;
1071 return new FileLocation ( IntermediateDirectory,
1072 module.pch->file->relative_path,
1073 ".gch_" + module.name );
1074 }
1075
1076 const FileLocation*
1077 MingwModuleHandler::GetPrecompiledHeaderFilename () const
1078 {
1079 if ( !module.pch || !use_pch )
1080 return NULL;
1081 return new FileLocation ( IntermediateDirectory,
1082 module.pch->file->relative_path + "/.gch_" + module.name,
1083 module.pch->file->name + ".gch" );
1084 }
1085
1086 Rule windresRule ( "$(eval $(call RBUILD_WRC_RULE,$(module_name),$(source),$(dependencies),$(compiler_flags)))\n",
1087 "$(intermediate_path_unique).coff",
1088 "$(intermediate_path_unique).res",
1089 "$(intermediate_path_unique).res.d",
1090 "$(intermediate_dir)$(SEP)", NULL );
1091 Rule winebuildPRule ( "$(eval $(call RBUILD_WINEBUILD_WITH_CPP_RULE,$(module_name),$(source),$(dependencies),$(compiler_flags),$(module_dllname)))\n",
1092 "$(intermediate_path_unique).spec",
1093 "$(intermediate_path_unique).spec.d",
1094 "$(intermediate_path_unique).auto.def",
1095 "$(intermediate_path_unique).stubs.c",
1096 "$(intermediate_path_unique).stubs.o",
1097 "$(intermediate_path_unique).stubs.o.d",
1098 "$(intermediate_dir)$(SEP)", NULL );
1099 Rule winebuildRule ( "$(eval $(call RBUILD_WINEBUILD_RULE,$(module_name),$(source),$(dependencies),$(compiler_flags),$(module_dllname)))\n",
1100 "$(intermediate_path_unique).auto.def",
1101 "$(intermediate_path_unique).stubs.c",
1102 "$(intermediate_path_unique).stubs.o",
1103 "$(intermediate_path_unique).stubs.o.d",
1104 "$(intermediate_dir)$(SEP)", NULL );
1105 Rule gasRule ( "$(eval $(call RBUILD_GAS_RULE,$(module_name),$(source),$(dependencies),$(compiler_flags)))\n",
1106 "$(intermediate_path_unique).o",
1107 "$(intermediate_path_unique).o.d", NULL );
1108 Rule gccRule ( "$(eval $(call RBUILD_CC_RULE,$(module_name),$(source),$(dependencies),$(compiler_flags)))\n",
1109 "$(intermediate_path_unique).o",
1110 "$(intermediate_path_unique).o.d", NULL );
1111 Rule gccHostRule ( "$(eval $(call RBUILD_HOST_GCC_RULE,$(module_name),$(source),$(dependencies),$(compiler_flags)))\n",
1112 "$(intermediate_path_unique).o", NULL );
1113 Rule gppRule ( "$(eval $(call RBUILD_CXX_RULE,$(module_name),$(source),$(dependencies),$(compiler_flags)))\n",
1114 "$(intermediate_path_unique).o",
1115 "$(intermediate_path_unique).o.d", NULL );
1116 Rule gppHostRule ( "$(eval $(call RBUILD_HOST_GPP_RULE,$(module_name),$(source),$(dependencies),$(compiler_flags)))\n",
1117 "$(intermediate_path_unique).o", NULL );
1118 Rule widlHeaderRule ( "$(eval $(call RBUILD_WIDL_HEADER_RULE,$(module_name),$(source),$(dependencies),$(compiler_flags)))\n",
1119 "$(intermediate_path_noext).h",
1120 "$(intermediate_dir)$(SEP)", NULL );
1121 Rule widlServerRule ( "$(eval $(call RBUILD_WIDL_SERVER_RULE,$(module_name),$(source),$(dependencies),$(compiler_flags)))\n",
1122 "$(intermediate_path_noext)_s.h",
1123 "$(intermediate_path_noext)_s.c",
1124 "$(intermediate_path_noext)_s.o",
1125 "$(intermediate_dir)$(SEP)", NULL );
1126 Rule widlClientRule ( "$(eval $(call RBUILD_WIDL_CLIENT_RULE,$(module_name),$(source),$(dependencies),$(compiler_flags)))\n",
1127 "$(intermediate_path_noext)_c.h",
1128 "$(intermediate_path_noext)_c.c",
1129 "$(intermediate_path_noext)_c.o",
1130 "$(intermediate_dir)$(SEP)", NULL );
1131 Rule widlProxyRule ( "$(eval $(call RBUILD_WIDL_PROXY_RULE,$(module_name),$(source),$(dependencies),$(compiler_flags)))\n",
1132 "$(intermediate_path_noext)_p.h",
1133 "$(intermediate_path_noext)_p.c",
1134 "$(intermediate_path_noext)_p.o",
1135 "$(intermediate_dir)$(SEP)", NULL );
1136 Rule widlInterfaceRule ( "$(eval $(call RBUILD_WIDL_INTERFACE_RULE,$(module_name),$(source),$(dependencies),$(compiler_flags)))\n",
1137 "$(intermediate_path_noext)_i.c",
1138 "$(intermediate_path_noext)_i.o",
1139 "$(intermediate_dir)$(SEP)", NULL );
1140 Rule widlDlldataRule ( "$(eval $(call RBUILD_WIDL_DLLDATA_RULE,$(module_name),$(source),$(dependencies),$(compiler_flags),$(bare_dependencies)))\n",
1141 "$(intermediate_path_noext).o", NULL );
1142 Rule widlTlbRule ( "$(eval $(call RBUILD_WIDL_TLB_RULE,$(module_name),$(source),$(dependencies),$(compiler_flags)))\n",
1143 "$(intermediate_dir)$(SEP)", NULL );
1144 Rule pchRule ( "$(eval $(call RBUILD_CC_PCH_RULE,$(module_name),$(source),$(dependencies),$(compiler_flags)))\n",
1145 "$(intermediate_dir)$(SEP).gch_$(module_name)$(SEP)$(source_name).gch",
1146 "$(intermediate_dir)$(SEP).gch_$(module_name)$(SEP)$(source_name).gch.d",
1147 "$(intermediate_dir)$(SEP).gch_$(module_name)$(SEP)", NULL );
1148 Rule pchCxxRule ( "$(eval $(call RBUILD_CXX_PCH_RULE,$(module_name),$(source),$(dependencies),$(compiler_flags)))\n",
1149 "$(intermediate_dir)$(SEP).gch_$(module_name)$(SEP)$(source_name).gch",
1150 "$(intermediate_dir)$(SEP).gch_$(module_name)$(SEP)$(source_name).gch.d",
1151 "$(intermediate_dir)$(SEP).gch_$(module_name)$(SEP)", NULL );
1152 Rule bootRule ( "$(eval $(call RBUILD_NASM,$(module_name),$(source),$(dependencies),,$(module_output)))\n",
1153 "$(module_output)",
1154 "$(OUTPUT)$(SEP)$(source_dir)$(SEP)", NULL );
1155 Rule nasmRule ( "$(eval $(call RBUILD_NASM,$(module_name),$(source),$(dependencies),,$(intermediate_path_unique).o))\n",
1156 "$(intermediate_path_unique).o",
1157 "$(intermediate_dir)$(SEP)", NULL );
1158
1159 /* TODO: move these to rules.mak */
1160 Rule wmcRule ( "$(intermediate_path_noext).rc $(INTERMEDIATE)$(SEP)include$(SEP)reactos$(SEP)$(source_name_noext).h: $(wmc_TARGET) $(source) | $(intermediate_dir)\n"
1161 "\t$(ECHO_WMC)\n"
1162 "\t$(Q)$(wmc_TARGET) -i -H $(INTERMEDIATE)$(SEP)include$(SEP)reactos$(SEP)$(source_name_noext).h -o $(intermediate_path_noext).rc $(source)\n",
1163 "$(intermediate_path_noext).rc",
1164 "$(INTERMEDIATE)$(SEP)include$(SEP)reactos$(SEP)$(source_name_noext).h",
1165 "$(intermediate_dir)$(SEP)", NULL );
1166 /* TODO: if possible, move these to rules.mak */
1167 Rule arRule1 ( "$(intermediate_path_noext).a: $($(module_name)_OBJS) $(dependencies) | $(intermediate_dir)\n",
1168 "$(intermediate_path_noext).a",
1169 "$(intermediate_dir)$(SEP)", NULL );
1170 Rule arRule2 ( "\t$(ECHO_AR)\n"
1171 "\t${ar} -rc $@ $($(module_name)_OBJS)\n",
1172 NULL );
1173 Rule arHostRule2 ( "\t$(ECHO_HOSTAR)\n"
1174 "\t${host_ar} -rc $@ $($(module_name)_OBJS)\n",
1175 NULL );
1176
1177 Rule emptyRule ( "", NULL );
1178
1179 void
1180 MingwModuleHandler::GenerateGccCommand (
1181 const FileLocation* sourceFile,
1182 const Rule *rule,
1183 const string& extraDependencies )
1184 {
1185 const FileLocation *pchFilename = GetPrecompiledHeaderFilename ();
1186 string dependencies = extraDependencies;
1187
1188 if ( pchFilename )
1189 {
1190 dependencies += " " + backend->GetFullName ( *pchFilename );
1191 delete pchFilename;
1192 }
1193
1194 /* WIDL generated headers may be used */
1195 vector<FileLocation> rpcDependencies;
1196 GetRpcHeaderDependencies ( rpcDependencies );
1197 if ( rpcDependencies.size () > 0 )
1198 dependencies += " " + v2s ( backend, rpcDependencies, 5 );
1199
1200 rule->Execute ( fMakefile, backend, module, sourceFile, clean_files, dependencies );
1201 }
1202
1203 string
1204 MingwModuleHandler::GetPropertyValue ( const Module& module, const std::string& name )
1205 {
1206 const Property* property = module.project.LookupProperty(name);
1207
1208 if (property)
1209 return property->value;
1210 else
1211 return string ( "" );
1212 }
1213
1214 /* caller needs to delete the returned object */
1215 const FileLocation*
1216 MingwModuleHandler::GetRpcServerHeaderFilename ( const FileLocation *base ) const
1217 {
1218 string newname = GetBasename ( base->name ) + "_s.h";
1219 return new FileLocation ( IntermediateDirectory, base->relative_path, newname );
1220 }
1221
1222 /* caller needs to delete the returned object */
1223 const FileLocation*
1224 MingwModuleHandler::GetRpcClientHeaderFilename ( const FileLocation *base ) const
1225 {
1226 string newname = GetBasename ( base->name ) + "_c.h";
1227 return new FileLocation ( IntermediateDirectory, base->relative_path, newname );
1228 }
1229
1230 /* caller needs to delete the returned object */
1231 const FileLocation*
1232 MingwModuleHandler::GetRpcProxyHeaderFilename ( const FileLocation *base ) const
1233 {
1234 string newname = GetBasename ( base->name ) + "_p.h";
1235 return new FileLocation ( IntermediateDirectory, base->relative_path, newname );
1236 }
1237
1238 /* caller needs to delete the returned object */
1239 const FileLocation*
1240 MingwModuleHandler::GetIdlHeaderFilename ( const FileLocation *base ) const
1241 {
1242 string newname = GetBasename ( base->name ) + ".h";
1243 return new FileLocation ( IntermediateDirectory, base->relative_path, newname );
1244 }
1245
1246 /* caller needs to delete the returned object */
1247 const FileLocation*
1248 MingwModuleHandler::GetMcHeaderFilename ( const FileLocation *base ) const
1249 {
1250 string newname = GetBasename ( base->name ) + ".h";
1251 return new FileLocation ( IntermediateDirectory, "include/reactos" , newname );
1252 }
1253
1254 void
1255 MingwModuleHandler::GenerateCommands (
1256 const CompilationUnit& compilationUnit,
1257 const string& extraDependencies )
1258 {
1259 const FileLocation& sourceFile = compilationUnit.GetFilename ();
1260 string extension = GetExtension ( sourceFile );
1261 std::transform ( extension.begin (), extension.end (), extension.begin (), tolower );
1262
1263 struct
1264 {
1265 HostType host;
1266 ModuleType type;
1267 string extension;
1268 Rule* rule;
1269 } rules[] = {
1270 { HostDontCare, TypeDontCare, ".s", &gasRule },
1271 { HostDontCare, BootSector, ".asm", &bootRule },
1272 { HostDontCare, TypeDontCare, ".asm", &nasmRule },
1273 { HostDontCare, TypeDontCare, ".rc", &windresRule },
1274 { HostDontCare, TypeDontCare, ".mc", &wmcRule },
1275 { HostDontCare, RpcServer, ".idl", &widlServerRule },
1276 { HostDontCare, RpcClient, ".idl", &widlClientRule },
1277 { HostDontCare, RpcProxy, ".idl", &widlProxyRule },
1278 { HostDontCare, IdlInterface, ".idl", &widlInterfaceRule },
1279 { HostDontCare, EmbeddedTypeLib, ".idl", &widlTlbRule },
1280 { HostDontCare, TypeDontCare, ".idl", &widlHeaderRule },
1281 { HostTrue, TypeDontCare, ".c", &gccHostRule },
1282 { HostTrue, TypeDontCare, ".cc", &gppHostRule },
1283 { HostTrue, TypeDontCare, ".cpp", &gppHostRule },
1284 { HostTrue, TypeDontCare, ".cxx", &gppHostRule },
1285 { HostFalse, TypeDontCare, ".c", &gccRule },
1286 { HostFalse, TypeDontCare, ".cc", &gppRule },
1287 { HostFalse, TypeDontCare, ".cpp", &gppRule },
1288 { HostFalse, TypeDontCare, ".cxx", &gppRule },
1289 { HostFalse, Cabinet, ".*", &emptyRule }
1290 };
1291 size_t i;
1292 Rule *customRule = NULL;
1293
1294 for ( i = 0; i < sizeof ( rules ) / sizeof ( rules[0] ); i++ )
1295 {
1296 if ( rules[i].host != HostDontCare && rules[i].host != ModuleHandlerInformations[module.type].DefaultHost )
1297 continue;
1298 if ( rules[i].type != TypeDontCare && rules[i].type != module.type )
1299 continue;
1300 if ( rules[i].extension != extension && rules[i].extension != ".*")
1301 continue;
1302 customRule = rules[i].rule;
1303 break;
1304 }
1305
1306 if ( extension == ".c" || extension == ".cc" || extension == ".cpp" || extension == ".cxx" )
1307 {
1308 GenerateGccCommand ( &sourceFile,
1309 customRule,
1310 GetCompilationUnitDependencies ( compilationUnit ) + extraDependencies );
1311 }
1312 else if ( customRule )
1313 customRule->Execute ( fMakefile, backend, module, &sourceFile, clean_files );
1314 else
1315 {
1316 throw InvalidOperationException ( __FILE__,
1317 __LINE__,
1318 "Unsupported filename extension '%s' in file '%s'",
1319 extension.c_str (),
1320 backend->GetFullName ( sourceFile ).c_str () );
1321 }
1322 }
1323
1324 void
1325 MingwModuleHandler::GenerateBuildMapCode ( const FileLocation *mapTarget )
1326 {
1327 fprintf ( fMakefile, "# BUILD MAP CODE\n" );
1328
1329 fprintf ( fMakefile,
1330 "ifeq ($(ROS_BUILDMAP),full)\n" );
1331
1332 FileLocation mapFilename ( OutputDirectory,
1333 module.output->relative_path,
1334 GetBasename ( module.output->name ) + ".map" );
1335 CLEAN_FILE ( mapFilename );
1336
1337 fprintf ( fMakefile,
1338 "\t$(ECHO_OBJDUMP)\n" );
1339 fprintf ( fMakefile,
1340 "\t$(Q)${objdump} -d -S %s > %s\n",
1341 mapTarget ? backend->GetFullName ( *mapTarget ).c_str () : "$@",
1342 backend->GetFullName ( mapFilename ).c_str () );
1343
1344 fprintf ( fMakefile,
1345 "else\n" );
1346 fprintf ( fMakefile,
1347 "ifeq ($(ROS_BUILDMAP),yes)\n" );
1348
1349 fprintf ( fMakefile,
1350 "\t$(ECHO_NM)\n" );
1351 fprintf ( fMakefile,
1352 "\t$(Q)${nm} --numeric-sort %s > %s\n",
1353 mapTarget ? backend->GetFullName ( *mapTarget ).c_str () : "$@",
1354 backend->GetFullName ( mapFilename ).c_str () );
1355
1356 fprintf ( fMakefile,
1357 "endif\n" );
1358
1359 fprintf ( fMakefile,
1360 "endif\n" );
1361 }
1362
1363 void
1364 MergeStringVector ( const Backend* backend,
1365 const vector<FileLocation>& input,
1366 vector<string>& output )
1367 {
1368 int wrap_at = 25;
1369 string s;
1370 int wrap_count = -1;
1371 for ( size_t i = 0; i < input.size (); i++ )
1372 {
1373 if ( wrap_count++ == wrap_at )
1374 {
1375 output.push_back ( s );
1376 s = "";
1377 wrap_count = 0;
1378 }
1379 else if ( s.size () > 0)
1380 s += " ";
1381 s += backend->GetFullName ( input[i] );
1382 }
1383 if ( s.length () > 0 )
1384 output.push_back ( s );
1385 }
1386
1387 void
1388 MingwModuleHandler::GetObjectsVector ( const IfableData& data,
1389 vector<FileLocation>& objectFiles ) const
1390 {
1391 for ( size_t i = 0; i < data.compilationUnits.size (); i++ )
1392 {
1393 CompilationUnit& compilationUnit = *data.compilationUnits[i];
1394 const FileLocation& compilationName = compilationUnit.GetFilename ();
1395 const FileLocation *object_file = GetObjectFilename ( &compilationName, module );
1396 objectFiles.push_back ( *object_file );
1397 delete object_file;
1398 }
1399 }
1400
1401 void
1402 MingwModuleHandler::GenerateCleanObjectsAsYouGoCode () const
1403 {
1404 if ( backend->configuration.CleanAsYouGo )
1405 {
1406 vector<FileLocation> objectFiles;
1407 GetObjectsVector ( module.non_if_data,
1408 objectFiles );
1409 vector<string> lines;
1410 MergeStringVector ( backend,
1411 objectFiles,
1412 lines );
1413 for ( size_t i = 0; i < lines.size (); i++ )
1414 {
1415 fprintf ( fMakefile,
1416 "\t-@${rm} %s 2>$(NUL)\n",
1417 lines[i].c_str () );
1418 }
1419 }
1420 }
1421
1422 void
1423 MingwModuleHandler::GenerateLinkerCommand () const
1424 {
1425 string definitionFilename;
1426
1427 const FileLocation *DefinitionFilename = GetDefinitionFilename ();
1428
1429 if ( DefinitionFilename ) {
1430 definitionFilename = backend->GetFullName (*DefinitionFilename);
1431 delete DefinitionFilename;
1432 }
1433
1434 string linkerScriptArgument;
1435 if ( module.linkerScript != NULL ) {
1436 if ( module.project.configuration.Linker == GnuLd )
1437 linkerScriptArgument = ssprintf ( " -T %s", backend->GetFullName ( *module.linkerScript->file ).c_str () );
1438 else
1439 fprintf ( stderr,
1440 "Linker doesn't support linker scripts: linker script %s ignored for module %s\n",
1441 backend->GetFullName ( *module.linkerScript->file ).c_str (),
1442 module.name.c_str() );
1443 }
1444
1445 string extraLibraries;
1446 if ( ModuleHandlerInformations[module.type].DefaultHost == HostFalse ) {
1447 if ( module.cplusplus ) {
1448 switch ( module.type )
1449 {
1450 case Win32DLL:
1451 case Win32OCX:
1452 case Win32CUI:
1453 case Win32GUI:
1454 case Win32SCR:
1455 extraLibraries = "$$(PROJECT_CXXLIBS)";
1456 break;
1457
1458 default:
1459 extraLibraries = "$$(PROJECT_CCLIBS)";
1460 break;
1461 }
1462 } else
1463 extraLibraries = "$$(PROJECT_CCLIBS)";
1464 }
1465
1466 delete PassThruCacheDirectory ( new FileLocation ( module.output->directory, module.output->relative_path, "" ) );
1467 delete PassThruCacheDirectory ( new FileLocation ( IntermediateDirectory, module.output->relative_path, "" ) );
1468
1469 fprintf ( fMakefile,
1470 "$(eval $(call RBUILD_LINK_RULE,%s,%s,%s,%s,%s,%s,%s))\n",
1471 module.name.c_str(),
1472 definitionFilename.c_str(),
1473 module.xmlbuildFile.c_str(),
1474 linkerScriptArgument.c_str(),
1475 extraLibraries.c_str(),
1476 module.GetEntryPoint().c_str(),
1477 module.baseaddress.c_str() );
1478 }
1479
1480 void
1481 MingwModuleHandler::GenerateObjectFileTargets ( const IfableData& data )
1482 {
1483 size_t i;
1484 string moduleDependencies;
1485
1486 const vector<CompilationUnit*>& compilationUnits = data.compilationUnits;
1487 for ( i = 0; i < compilationUnits.size (); i++ )
1488 {
1489 CompilationUnit& compilationUnit = *compilationUnits[i];
1490 const FileLocation& compilationName = compilationUnit.GetFilename ();
1491 const FileLocation *objectFilename = GetObjectFilename ( &compilationName, module );
1492 if ( GetExtension ( *objectFilename ) == ".h" )
1493 moduleDependencies += ssprintf ( " $(%s_HEADERS)", module.name.c_str () );
1494 else if ( GetExtension ( *objectFilename ) == ".rc" )
1495 moduleDependencies += ssprintf ( " $(%s_RESOURCES)", module.name.c_str () );
1496 delete objectFilename;
1497 }
1498
1499 for ( i = 0; i < compilationUnits.size (); i++ )
1500 {
1501 GenerateCommands ( *compilationUnits[i],
1502 moduleDependencies );
1503 }
1504
1505 vector<CompilationUnit*> sourceCompilationUnits;
1506 GetModuleSpecificCompilationUnits ( sourceCompilationUnits );
1507 for ( i = 0; i < sourceCompilationUnits.size (); i++ )
1508 {
1509 GenerateCommands ( *sourceCompilationUnits[i],
1510 moduleDependencies );
1511 }
1512 CleanupCompilationUnitVector ( sourceCompilationUnits );
1513
1514 if ( module.type == RpcProxy )
1515 {
1516 widlDlldataRule.Execute ( fMakefile,
1517 backend,
1518 module,
1519 GetDlldataFilename(),
1520 clean_files,
1521 ssprintf ( "$(%s_SOURCES)", module.name.c_str ()) );
1522 }
1523 }
1524
1525 void
1526 MingwModuleHandler::GenerateObjectFileTargets ()
1527 {
1528 if ( module.pch && use_pch )
1529 {
1530
1531 std::map<string, string> vars;
1532
1533 /* WIDL generated headers may be used */
1534 string dependencies;
1535 vector<FileLocation> rpcDependencies;
1536 GetRpcHeaderDependencies ( rpcDependencies );
1537 if ( rpcDependencies.size () > 0 )
1538 dependencies = " " + v2s ( backend, rpcDependencies, 5 );
1539
1540 if ( module.cplusplus )
1541 pchCxxRule.Execute ( fMakefile, backend, module, module.pch->file, clean_files, dependencies );
1542 else
1543 pchRule.Execute ( fMakefile, backend, module, module.pch->file, clean_files, dependencies );
1544 }
1545
1546 GenerateObjectFileTargets ( module.non_if_data );
1547 }
1548
1549 /* caller needs to delete the returned object */
1550 const FileLocation*
1551 MingwModuleHandler::GenerateArchiveTarget ()
1552 {
1553 const FileLocation *archiveFilename = GetModuleArchiveFilename ();
1554
1555 if ( archiveFilename == NULL )
1556 return NULL;
1557
1558 const FileLocation *definitionFilename = GetDefinitionFilename ();
1559
1560 fprintf ( fMakefile, "# ARCHIVE TARGET\n" );
1561
1562 if ( IsStaticLibrary ( module ) && definitionFilename )
1563 {
1564 arRule1.Execute ( fMakefile,
1565 backend,
1566 module,
1567 archiveFilename,
1568 clean_files,
1569 backend->GetFullName ( *definitionFilename ).c_str () );
1570
1571 fprintf ( fMakefile,
1572 "\t${dlltool} --def %s --kill-at --output-lib $@\n",
1573 backend->GetFullName ( *definitionFilename ).c_str () );
1574 }
1575 else
1576 arRule1.Execute ( fMakefile, backend, module, archiveFilename, clean_files );
1577
1578 if ( definitionFilename )
1579 delete definitionFilename;
1580
1581 if(module.type == HostStaticLibrary)
1582 arHostRule2.Execute ( fMakefile, backend, module, archiveFilename, clean_files );
1583 else
1584 arRule2.Execute ( fMakefile, backend, module, archiveFilename, clean_files );
1585
1586 GenerateCleanObjectsAsYouGoCode ();
1587
1588 fprintf ( fMakefile, "\n" );
1589
1590 return archiveFilename;
1591 }
1592
1593 /*static*/ string
1594 MingwModuleHandler::GetObjectsMacro ( const Module& module )
1595 {
1596 return ssprintf ( "$(%s_OBJS)",
1597 module.name.c_str () );
1598 }
1599
1600 string
1601 MingwModuleHandler::GetLibsMacro () const
1602 {
1603 return ssprintf ( "$(%s_LIBS)", module.name.c_str () );
1604 }
1605
1606 string
1607 MingwModuleHandler::GetLinkerMacro () const
1608 {
1609 return ssprintf ( "$(%s_LDFLAGS)",
1610 module.name.c_str () );
1611 }
1612
1613 string
1614 MingwModuleHandler::GetDebugFormat ()
1615 {
1616 if (Environment::GetArch() == "amd64")
1617 {
1618 return "dwarf-2";
1619 }
1620 return "stabs+";
1621 }
1622
1623 string
1624 MingwModuleHandler::GetModuleTargets ( const Module& module )
1625 {
1626 if ( ReferenceObjects ( module ) )
1627 return GetObjectsMacro ( module );
1628 else
1629 {
1630 const FileLocation *target_file = GetTargetFilename ( module, NULL );
1631 string target = backend->GetFullName ( *target_file ).c_str ();
1632 delete target_file;
1633 return target;
1634 }
1635 }
1636
1637 void
1638 MingwModuleHandler::GenerateSourceMacro ()
1639 {
1640 sourcesMacro = ssprintf ( "%s_SOURCES", module.name.c_str ());
1641
1642 if ( module.type == RpcProxy || module.type == Cabinet )
1643 GenerateSourceMacros ( module.non_if_data );
1644
1645 // future references to the macro will be to get its values
1646 sourcesMacro = ssprintf ("$(%s)", sourcesMacro.c_str ());
1647 }
1648
1649 void
1650 MingwModuleHandler::GenerateObjectMacro ()
1651 {
1652 objectsMacro = ssprintf ("%s_OBJS", module.name.c_str ());
1653
1654 GenerateObjectMacros ( module.non_if_data );
1655
1656 // future references to the macro will be to get its values
1657 objectsMacro = ssprintf ("$(%s)", objectsMacro.c_str ());
1658 }
1659
1660 void
1661 MingwModuleHandler::GenerateTargetMacro ()
1662 {
1663 fprintf ( fMakefile,
1664 "%s := %s\n",
1665 GetTargetMacro ( module, false ).c_str (),
1666 GetModuleTargets ( module ).c_str () );
1667 }
1668
1669 void
1670 MingwModuleHandler::GetRpcHeaderDependencies (
1671 vector<FileLocation>& dependencies ) const
1672 {
1673 for ( size_t i = 0; i < module.non_if_data.libraries.size (); i++ )
1674 {
1675 Library& library = *module.non_if_data.libraries[i];
1676 if ( library.importedModule->type == RpcServer ||
1677 library.importedModule->type == RpcClient ||
1678 library.importedModule->type == RpcProxy ||
1679 library.importedModule->type == IdlHeader )
1680 {
1681 for ( size_t j = 0; j < library.importedModule->non_if_data.compilationUnits.size (); j++ )
1682 {
1683 CompilationUnit& compilationUnit = *library.importedModule->non_if_data.compilationUnits[j];
1684 const FileLocation& sourceFile = compilationUnit.GetFilename ();
1685 string extension = GetExtension ( sourceFile );
1686 if ( extension == ".idl" || extension == ".IDL" )
1687 {
1688 string basename = GetBasename ( sourceFile.name );
1689 if ( library.importedModule->type == RpcServer )
1690 {
1691 const FileLocation *header = GetRpcServerHeaderFilename ( &sourceFile );
1692 dependencies.push_back ( *header );
1693 delete header;
1694 }
1695 if ( library.importedModule->type == RpcClient )
1696 {
1697 const FileLocation *header = GetRpcClientHeaderFilename ( &sourceFile );
1698 dependencies.push_back ( *header );
1699 delete header;
1700 }
1701 if ( library.importedModule->type == RpcProxy )
1702 {
1703 const FileLocation *header = GetRpcProxyHeaderFilename ( &sourceFile );
1704 dependencies.push_back ( *header );
1705 delete header;
1706 }
1707 if ( library.importedModule->type == IdlHeader )
1708 {
1709 const FileLocation *header = GetIdlHeaderFilename ( &sourceFile );
1710 dependencies.push_back ( *header );
1711 delete header;
1712 }
1713 }
1714 }
1715 }
1716 }
1717 }
1718
1719 void
1720 MingwModuleHandler::GenerateOtherMacros ()
1721 {
1722 set<const Define *> used_defs;
1723
1724 linkerflagsMacro = ssprintf ("%s_LDFLAGS", module.name.c_str ());
1725 libsMacro = ssprintf("%s_LIBS", module.name.c_str ());
1726
1727 const FileLocation * pchPath = GetPrecompiledHeaderPath ();
1728
1729 if ( pchPath )
1730 {
1731 string pchPathStr = backend->GetFullName ( *pchPath );
1732 delete pchPath;
1733
1734 fprintf ( fMakefile,
1735 "%s_%sINCLUDES+= -I%s\n",
1736 module.name.c_str(),
1737 CompilerPrefixTable[CompilerTypeCC],
1738 pchPathStr.c_str() );
1739
1740 fprintf ( fMakefile,
1741 "%s_%sINCLUDES+= -I%s\n",
1742 module.name.c_str(),
1743 CompilerPrefixTable[CompilerTypeCXX],
1744 pchPathStr.c_str() );
1745 }
1746
1747 const char * toolPrefix = "";
1748
1749 if ( ModuleHandlerInformations[module.type].DefaultHost == HostTrue )
1750 toolPrefix = "HOST_";
1751
1752 // FIXME: this is very ugly and generates lots of useless entries
1753 for ( unsigned type = CompilerTypeCC; type < CompilerTypesCount; ++ type )
1754 {
1755 string flags;
1756
1757 if ( module.dynamicCRT )
1758 flags += ssprintf ( " $(%s%sFLAG_CRTDLL)", toolPrefix, CompilerPrefixTable[type] );
1759
1760 // FIXME: this duplicates the flag for CPP and C/CXX
1761 if ( !module.allowWarnings )
1762 flags += ssprintf ( " $(%s%sFLAG_WERROR)", toolPrefix, CompilerPrefixTable[type] );
1763
1764 if ( module.isUnicode )
1765 flags += ssprintf ( " $(%s%sFLAG_UNICODE)", toolPrefix, CompilerPrefixTable[type] );
1766
1767 if ( flags.size() )
1768 {
1769 fprintf ( fMakefile,
1770 "%s_%sFLAGS+=%s\n",
1771 module.name.c_str(),
1772 CompilerPrefixTable[type],
1773 flags.c_str() );
1774 }
1775 }
1776
1777 GenerateParameters ( module.name.c_str(), "+=", module.non_if_data );
1778
1779 GenerateMacros (
1780 "+=",
1781 module.non_if_data,
1782 &module.linkerFlags,
1783 used_defs );
1784
1785 fprintf ( fMakefile, "\n\n" );
1786 }
1787
1788 void
1789 MingwModuleHandler::GenerateRules ()
1790 {
1791 SpecFileType spec;
1792
1793 fprintf ( fMakefile, "# RULES\n" );
1794 string targetMacro = GetTargetMacro ( module );
1795 //CLEAN_FILE ( targetMacro );
1796 CLEAN_FILE ( FileLocation ( SourceDirectory, "", targetMacro ) );
1797
1798 // generate phony target for module name
1799 fprintf ( fMakefile, ".PHONY: %s\n",
1800 module.name.c_str () );
1801 string dependencies = GetTargetMacro ( module );
1802 if ( module.type == Test )
1803 dependencies += " $(regtests_run_TARGET)";
1804 fprintf ( fMakefile, "%s: %s\n\n",
1805 module.name.c_str (),
1806 dependencies.c_str () );
1807 if ( module.type == Test )
1808 {
1809 fprintf ( fMakefile,
1810 "\t@%s\n",
1811 targetMacro.c_str ());
1812 }
1813
1814 if ( !ReferenceObjects ( module ) )
1815 {
1816 const FileLocation* ar_target = GenerateArchiveTarget ();
1817
1818 if ( ar_target )
1819 delete ar_target;
1820 }
1821
1822
1823 spec = module.IsSpecDefinitionFile();
1824
1825 if(spec)
1826 {
1827 Rule * defRule;
1828
1829 if (spec == PSpec)
1830 defRule = &winebuildPRule;
1831 else
1832 defRule = &winebuildRule;
1833
1834 defRule->Execute ( fMakefile, backend, module, module.importLibrary->source, clean_files );
1835 }
1836
1837 GenerateObjectFileTargets ();
1838 }
1839
1840 void
1841 MingwModuleHandler::GetInvocationDependencies (
1842 const Module& module,
1843 string_list& dependencies )
1844 {
1845 for ( size_t i = 0; i < module.invocations.size (); i++ )
1846 {
1847 Invoke& invoke = *module.invocations[i];
1848 if ( invoke.invokeModule == &module )
1849 /* Protect against circular dependencies */
1850 continue;
1851 invoke.GetTargets ( dependencies );
1852 }
1853 }
1854
1855 void
1856 MingwModuleHandler::GenerateInvocations () const
1857 {
1858 if ( module.invocations.size () == 0 )
1859 return;
1860
1861 fprintf ( fMakefile, "# INVOCATIONS\n" );
1862
1863 size_t iend = module.invocations.size ();
1864 for ( size_t i = 0; i < iend; i++ )
1865 {
1866 const Invoke& invoke = *module.invocations[i];
1867
1868 if ( invoke.invokeModule->type != BuildTool )
1869 {
1870 throw XMLInvalidBuildFileException (
1871 module.node.location,
1872 "Only modules of type buildtool can be invoked." );
1873 }
1874
1875 string invokeTarget = module.GetInvocationTarget ( i );
1876 string_list invoke_targets;
1877 assert ( invoke_targets.size() );
1878 invoke.GetTargets ( invoke_targets );
1879 fprintf ( fMakefile,
1880 ".PHONY: %s\n\n",
1881 invokeTarget.c_str () );
1882 fprintf ( fMakefile,
1883 "%s:",
1884 invokeTarget.c_str () );
1885 size_t j, jend = invoke_targets.size();
1886 for ( j = 0; j < jend; j++ )
1887 {
1888 fprintf ( fMakefile,
1889 " %s",
1890 invoke_targets[i].c_str () );
1891 }
1892 fprintf ( fMakefile, "\n\n%s", invoke_targets[0].c_str () );
1893 for ( j = 1; j < jend; j++ )
1894 fprintf ( fMakefile,
1895 " %s",
1896 invoke_targets[i].c_str () );
1897 fprintf ( fMakefile,
1898 ": %s\n",
1899 NormalizeFilename ( backend->GetFullName ( *invoke.invokeModule->output ) ).c_str () );
1900 fprintf ( fMakefile, "\t$(ECHO_INVOKE)\n" );
1901 fprintf ( fMakefile,
1902 "\t%s %s\n\n",
1903 NormalizeFilename ( backend->GetFullName ( *invoke.invokeModule->output ) ).c_str (),
1904 invoke.GetParameters ().c_str () );
1905 }
1906 }
1907
1908 string
1909 MingwModuleHandler::GetPreconditionDependenciesName () const
1910 {
1911 return module.name + "_precondition";
1912 }
1913
1914 void
1915 MingwModuleHandler::GetDefaultDependencies (
1916 string_list& dependencies ) const
1917 {
1918 /* Avoid circular dependency */
1919 if ( ModuleHandlerInformations[module.type].DefaultHost == HostTrue )
1920 return;
1921
1922 if (module.name != "psdk" &&
1923 module.name != "dxsdk")
1924 {
1925 dependencies.push_back ( "$(psdk_TARGET) $(psdk_HEADERS)" );
1926 dependencies.push_back ( "$(dxsdk_TARGET) $(dxsdk_HEADERS)" );
1927 }
1928
1929 if (module.name != "errcodes" &&
1930 module.name != "bugcodes" &&
1931 module.name != "ntstatus")
1932 {
1933 dependencies.push_back ( "$(errcodes_TARGET) $(ERRCODES_MCHEADERS)" );
1934 dependencies.push_back ( "$(bugcodes_TARGET) $(BUGCODES_MCHEADERS)" );
1935 dependencies.push_back ( "$(ntstatus_TARGET) $(NTSTATUS_MCHEADERS)" );
1936 }
1937
1938 ///* Check if any dependent library relies on the generated headers */
1939 //for ( size_t i = 0; i < module.project.modules.size (); i++ )
1940 //{
1941 // const Module& m = *module.project.modules[i];
1942 // for ( size_t j = 0; j < m.non_if_data.compilationUnits.size (); j++ )
1943 // {
1944 // CompilationUnit& compilationUnit = *m.non_if_data.compilationUnits[j];
1945 // const FileLocation& sourceFile = compilationUnit.GetFilename ();
1946 // string extension = GetExtension ( sourceFile );
1947 // if (extension == ".mc" || extension == ".MC" )
1948 // {
1949 // string dependency = ssprintf ( "$(%s_MCHEADERS)", m.name.c_str () );
1950 // dependencies.push_back ( dependency );
1951 // }
1952 // }
1953 //}
1954 }
1955
1956 void
1957 MingwModuleHandler::GeneratePreconditionDependencies ()
1958 {
1959 fprintf ( fMakefile, "# PRECONDITION DEPENDENCIES\n" );
1960 string preconditionDependenciesName = GetPreconditionDependenciesName ();
1961 string_list dependencies;
1962 GetDefaultDependencies ( dependencies );
1963 GetModuleDependencies ( dependencies );
1964
1965 GetInvocationDependencies ( module, dependencies );
1966
1967 if ( dependencies.size() )
1968 {
1969 fprintf ( fMakefile,
1970 "%s =",
1971 preconditionDependenciesName.c_str () );
1972 for ( size_t i = 0; i < dependencies.size(); i++ )
1973 fprintf ( fMakefile,
1974 " %s",
1975 dependencies[i].c_str () );
1976 fprintf ( fMakefile, "\n\n" );
1977 }
1978
1979 fprintf ( fMakefile, "\n" );
1980 }
1981
1982 /* caller needs to delete the returned object */
1983 const FileLocation*
1984 MingwModuleHandler::GetDefinitionFilename () const
1985 {
1986 if ( module.importLibrary == NULL )
1987 return NULL;
1988
1989 if ( module.IsSpecDefinitionFile () )
1990 {
1991 return new FileLocation ( IntermediateDirectory,
1992 module.importLibrary->source->relative_path,
1993 GetBasename ( module.importLibrary->source->name ) + "_" + module.name + ".auto.def" );
1994 }
1995 else
1996 {
1997 return new FileLocation ( SourceDirectory,
1998 module.importLibrary->source->relative_path,
1999 module.importLibrary->source->name );
2000 }
2001 }
2002
2003 void
2004 MingwModuleHandler::GetSpecObjectDependencies (
2005 vector<FileLocation>& dependencies,
2006 const FileLocation *file ) const
2007 {
2008 dependencies.push_back ( FileLocation ( IntermediateDirectory,
2009 file->relative_path,
2010 GetBasename ( file->name ) + "_" + module.name + ".stubs.c" ) );
2011 }
2012
2013 void
2014 MingwModuleHandler::GetMcObjectDependencies (
2015 vector<FileLocation>& dependencies,
2016 const FileLocation *file ) const
2017 {
2018 string basename = GetBasename ( file->name );
2019
2020 FileLocation defDependency ( IntermediateDirectory,
2021 "include/reactos",
2022 basename + ".h" );
2023 dependencies.push_back ( defDependency );
2024
2025 FileLocation stubsDependency ( IntermediateDirectory,
2026 file->relative_path,
2027 basename + ".rc" );
2028 dependencies.push_back ( stubsDependency );
2029 }
2030
2031 void
2032 MingwModuleHandler::GetWidlObjectDependencies (
2033 vector<FileLocation>& dependencies,
2034 const FileLocation *file ) const
2035 {
2036 string basename = GetBasename ( file->name );
2037 const FileLocation *generatedHeaderFilename = GetRpcServerHeaderFilename ( file );
2038
2039 FileLocation serverSourceDependency ( IntermediateDirectory,
2040 file->relative_path,
2041 basename + "_s.c" );
2042 dependencies.push_back ( serverSourceDependency );
2043 dependencies.push_back ( *generatedHeaderFilename );
2044
2045 delete generatedHeaderFilename;
2046 }
2047
2048 void
2049 MingwModuleHandler::GetDefinitionDependencies (
2050 vector<FileLocation>& dependencies ) const
2051 {
2052 const vector<CompilationUnit*>& compilationUnits = module.non_if_data.compilationUnits;
2053 for ( size_t i = 0; i < compilationUnits.size (); i++ )
2054 {
2055 const CompilationUnit& compilationUnit = *compilationUnits[i];
2056 const FileLocation& sourceFile = compilationUnit.GetFilename ();
2057 string extension = GetExtension ( sourceFile );
2058
2059 if (extension == ".spec" || extension == ".pspec")
2060 GetSpecObjectDependencies ( dependencies, &sourceFile );
2061
2062 if (extension == ".idl")
2063 {
2064 if ( ( module.type == RpcServer ) || ( module.type == RpcClient ) || ( module.type == RpcProxy ) )
2065 GetWidlObjectDependencies ( dependencies, &sourceFile );
2066 }
2067 }
2068 }
2069
2070 enum DebugSupportType
2071 {
2072 DebugKernelMode,
2073 DebugUserMode
2074 };
2075
2076 static void
2077 MingwAddDebugSupportLibraries ( Module& module, DebugSupportType type )
2078 {
2079 Library* pLibrary;
2080
2081 switch(type)
2082 {
2083 case DebugKernelMode:
2084 pLibrary = new Library ( module, "debugsup_ntoskrnl" );
2085 break;
2086
2087 case DebugUserMode:
2088 pLibrary = new Library ( module, "debugsup_ntdll" );
2089 break;
2090
2091 default:
2092 assert(0);
2093 }
2094
2095 module.non_if_data.libraries.push_back(pLibrary);
2096 }
2097
2098 static void
2099 MingwAddCRTLibrary( Module &module )
2100 {
2101 const char * crtAttr = module.CRT.c_str ();
2102 const char * crtLib = NULL;
2103
2104 if ( stricmp ( crtAttr, "libc" ) == 0 )
2105 crtLib = "crt";
2106 else if ( stricmp ( crtAttr, "msvcrt" ) == 0 )
2107 crtLib = "msvcrt";
2108 else if ( stricmp ( crtAttr, "libcntpr" ) == 0 )
2109 crtLib = "libcntpr";
2110 else if ( stricmp ( crtAttr, "ntdll" ) == 0 )
2111 crtLib = "ntdll";
2112
2113 if ( crtLib )
2114 {
2115 Library* pLibrary = new Library ( module, std::string ( crtLib ) );
2116
2117 if ( pLibrary->importedModule == NULL)
2118 {
2119 throw XMLInvalidBuildFileException (
2120 module.node.location,
2121 "module '%s' trying to import non-existant C runtime module '%s'",
2122 module.name.c_str(),
2123 crtLib );
2124 }
2125
2126 module.non_if_data.libraries.push_back ( pLibrary );
2127 }
2128 }
2129
2130 MingwBuildToolModuleHandler::MingwBuildToolModuleHandler ( const Module& module_ )
2131 : MingwModuleHandler ( module_ )
2132 {
2133 }
2134
2135 void
2136 MingwBuildToolModuleHandler::Process ()
2137 {
2138 GenerateBuildToolModuleTarget ();
2139 }
2140
2141 void
2142 MingwBuildToolModuleHandler::GenerateBuildToolModuleTarget ()
2143 {
2144 string targetMacro ( GetTargetMacro (module) );
2145 string objectsMacro = GetObjectsMacro ( module );
2146 string libsMacro = GetLibsMacro ();
2147
2148 GenerateRules ();
2149
2150 fprintf ( fMakefile, "# BUILD TOOL MODULE TARGET\n" );
2151
2152 string linker;
2153 if ( module.cplusplus )
2154 linker = "${host_gpp}";
2155 else
2156 linker = "${host_gcc}";
2157
2158 const FileLocation *target_file = GetTargetFilename ( module, NULL );
2159 fprintf ( fMakefile, "%s: %s %s | %s\n",
2160 targetMacro.c_str (),
2161 objectsMacro.c_str (),
2162 libsMacro.c_str (),
2163 backend->GetFullPath ( *target_file ).c_str () );
2164 fprintf ( fMakefile, "\t$(ECHO_HOSTLD)\n" );
2165 fprintf ( fMakefile,
2166 "\t%s %s -o $@ %s %s\n\n",
2167 linker.c_str (),
2168 GetLinkerMacro ().c_str (),
2169 objectsMacro.c_str (),
2170 libsMacro.c_str () );
2171
2172 delete target_file;
2173 }
2174
2175
2176 MingwKernelModuleHandler::MingwKernelModuleHandler (
2177 const Module& module_ )
2178
2179 : MingwModuleHandler ( module_ )
2180 {
2181 }
2182
2183 void
2184 MingwKernelModuleHandler::Process ()
2185 {
2186 GenerateKernelModuleTarget ();
2187 }
2188
2189 void
2190 MingwKernelModuleHandler::GenerateKernelModuleTarget ()
2191 {
2192 GenerateRules ();
2193 GenerateLinkerCommand ();
2194 }
2195
2196
2197 MingwKernelModeDLLModuleHandler::MingwKernelModeDLLModuleHandler (
2198 const Module& module_ )
2199
2200 : MingwModuleHandler ( module_ )
2201 {
2202 }
2203
2204
2205 void
2206 MingwKernelModeDLLModuleHandler::AddImplicitLibraries ( Module& module )
2207 {
2208 MingwAddCRTLibrary ( module );
2209 MingwAddDebugSupportLibraries ( module, DebugKernelMode );
2210 }
2211
2212 void
2213 MingwKernelModeDLLModuleHandler::Process ()
2214 {
2215 GenerateKernelModeDLLModuleTarget ();
2216 }
2217
2218 void
2219 MingwKernelModeDLLModuleHandler::GenerateKernelModeDLLModuleTarget ()
2220 {
2221 GenerateRules ();
2222 GenerateLinkerCommand ();
2223 }
2224
2225
2226 MingwNativeDLLModuleHandler::MingwNativeDLLModuleHandler (
2227 const Module& module_ )
2228
2229 : MingwModuleHandler ( module_ )
2230 {
2231 }
2232
2233 void
2234 MingwNativeDLLModuleHandler::AddImplicitLibraries ( Module& module )
2235 {
2236 MingwAddCRTLibrary ( module );
2237 MingwAddDebugSupportLibraries ( module, DebugUserMode );
2238 }
2239
2240 void
2241 MingwNativeDLLModuleHandler::Process ()
2242 {
2243 GenerateNativeDLLModuleTarget ();
2244 }
2245
2246 void
2247 MingwNativeDLLModuleHandler::GenerateNativeDLLModuleTarget ()
2248 {
2249 GenerateRules ();
2250 GenerateLinkerCommand ();
2251 }
2252
2253
2254 MingwNativeCUIModuleHandler::MingwNativeCUIModuleHandler (
2255 const Module& module_ )
2256
2257 : MingwModuleHandler ( module_ )
2258 {
2259 }
2260
2261 void
2262 MingwNativeCUIModuleHandler::AddImplicitLibraries ( Module& module )
2263 {
2264 MingwAddCRTLibrary ( module );
2265 MingwAddDebugSupportLibraries ( module, DebugUserMode );
2266 }
2267
2268 void
2269 MingwNativeCUIModuleHandler::Process ()
2270 {
2271 GenerateNativeCUIModuleTarget ();
2272 }
2273
2274 void
2275 MingwNativeCUIModuleHandler::GenerateNativeCUIModuleTarget ()
2276 {
2277 GenerateRules ();
2278 GenerateLinkerCommand ();
2279 }
2280
2281
2282 MingwWin32DLLModuleHandler::MingwWin32DLLModuleHandler (
2283 const Module& module_ )
2284
2285 : MingwModuleHandler ( module_ )
2286 {
2287 }
2288
2289 MingwWin32OCXModuleHandler::MingwWin32OCXModuleHandler (
2290 const Module& module_ )
2291
2292 : MingwModuleHandler ( module_ )
2293 {
2294 }
2295
2296 static void
2297 MingwAddImplicitLibraries( Module &module )
2298 {
2299 Library* pLibrary;
2300
2301 if ( module.type != Win32DLL
2302 && module.type != Win32OCX
2303 && module.type != Win32CUI
2304 && module.type != Win32GUI
2305 && module.type != Win32SCR)
2306 {
2307 return;
2308 }
2309
2310 if ( module.isDefaultEntryPoint )
2311 {
2312 if ( module.IsDLL () )
2313 {
2314 //pLibrary = new Library ( module, "__mingw_dllmain" );
2315 //module.non_if_data.libraries.insert ( module.non_if_data.libraries.begin(), pLibrary );
2316 }
2317 else
2318 {
2319 pLibrary = new Library ( module, module.isUnicode ? "mingw_wmain" : "mingw_main" );
2320 module.non_if_data.libraries.insert ( module.non_if_data.libraries.begin(), pLibrary );
2321 }
2322 }
2323
2324 pLibrary = new Library ( module, "mingw_common" );
2325 module.non_if_data.libraries.push_back ( pLibrary );
2326
2327 MingwAddCRTLibrary ( module );
2328 MingwAddDebugSupportLibraries ( module, DebugUserMode );
2329 }
2330
2331 void
2332 MingwWin32DLLModuleHandler::AddImplicitLibraries ( Module& module )
2333 {
2334 MingwAddImplicitLibraries ( module );
2335 }
2336
2337 void
2338 MingwWin32DLLModuleHandler::Process ()
2339 {
2340 GenerateWin32DLLModuleTarget ();
2341 }
2342
2343 void
2344 MingwWin32DLLModuleHandler::GenerateWin32DLLModuleTarget ()
2345 {
2346 GenerateRules ();
2347 GenerateLinkerCommand ();
2348 }
2349
2350
2351 void
2352 MingwWin32OCXModuleHandler::AddImplicitLibraries ( Module& module )
2353 {
2354 MingwAddImplicitLibraries ( module );
2355 }
2356
2357 void
2358 MingwWin32OCXModuleHandler::Process ()
2359 {
2360 GenerateWin32OCXModuleTarget ();
2361 }
2362
2363 void
2364 MingwWin32OCXModuleHandler::GenerateWin32OCXModuleTarget ()
2365 {
2366 GenerateRules ();
2367 GenerateLinkerCommand ();
2368 }
2369
2370
2371 MingwWin32CUIModuleHandler::MingwWin32CUIModuleHandler (
2372 const Module& module_ )
2373
2374 : MingwModuleHandler ( module_ )
2375 {
2376 }
2377
2378 void
2379 MingwWin32CUIModuleHandler::AddImplicitLibraries ( Module& module )
2380 {
2381 MingwAddImplicitLibraries ( module );
2382 }
2383
2384 void
2385 MingwWin32CUIModuleHandler::Process ()
2386 {
2387 GenerateWin32CUIModuleTarget ();
2388 }
2389
2390 void
2391 MingwWin32CUIModuleHandler::GenerateWin32CUIModuleTarget ()
2392 {
2393 GenerateRules ();
2394 GenerateLinkerCommand ();
2395 }
2396
2397
2398 MingwWin32GUIModuleHandler::MingwWin32GUIModuleHandler (
2399 const Module& module_ )
2400
2401 : MingwModuleHandler ( module_ )
2402 {
2403 }
2404
2405 void
2406 MingwWin32GUIModuleHandler::AddImplicitLibraries ( Module& module )
2407 {
2408 MingwAddImplicitLibraries ( module );
2409 }
2410
2411 void
2412 MingwWin32GUIModuleHandler::Process ()
2413 {
2414 GenerateWin32GUIModuleTarget ();
2415 }
2416
2417 void
2418 MingwWin32GUIModuleHandler::GenerateWin32GUIModuleTarget ()
2419 {
2420 GenerateRules ();
2421 GenerateLinkerCommand ();
2422 }
2423
2424
2425 MingwBootLoaderModuleHandler::MingwBootLoaderModuleHandler (
2426 const Module& module_ )
2427
2428 : MingwModuleHandler ( module_ )
2429 {
2430 }
2431
2432 void
2433 MingwBootLoaderModuleHandler::Process ()
2434 {
2435 GenerateBootLoaderModuleTarget ();
2436 }
2437
2438 void
2439 MingwBootLoaderModuleHandler::GenerateBootLoaderModuleTarget ()
2440 {
2441 fprintf ( fMakefile, "# BOOT LOADER MODULE TARGET\n" );
2442 string targetName ( module.output->name );
2443 string targetMacro ( GetTargetMacro (module) );
2444 string workingDirectory = GetWorkingDirectory ();
2445 FileLocation junk_tmp ( TemporaryDirectory,
2446 "",
2447 module.name + ".junk.tmp" );
2448 CLEAN_FILE ( junk_tmp );
2449 string objectsMacro = GetObjectsMacro ( module );
2450 string libsMacro = GetLibsMacro ();
2451
2452 GenerateRules ();
2453
2454 const FileLocation *target_file = GetTargetFilename ( module, NULL );
2455 fprintf ( fMakefile, "%s: %s %s | %s\n",
2456 targetMacro.c_str (),
2457 objectsMacro.c_str (),
2458 libsMacro.c_str (),
2459 backend->GetFullPath ( *target_file ).c_str () );
2460
2461 fprintf ( fMakefile, "\t$(ECHO_LD)\n" );
2462
2463 if (Environment::GetArch() == "arm")
2464 {
2465 fprintf ( fMakefile,
2466 "\t${gcc} -Wl,--subsystem,native -o %s %s %s %s -nostartfiles -nostdlib\n",
2467 backend->GetFullName ( junk_tmp ).c_str (),
2468 objectsMacro.c_str (),
2469 libsMacro.c_str (),
2470 GetLinkerMacro ().c_str ());
2471 }
2472 else
2473 {
2474 fprintf ( fMakefile,
2475 "\t${gcc} -Wl,--subsystem,native -Wl,-Ttext,0x8000 -o %s %s %s %s -nostartfiles -nostdlib\n",
2476 backend->GetFullName ( junk_tmp ).c_str (),
2477 objectsMacro.c_str (),
2478 libsMacro.c_str (),
2479 GetLinkerMacro ().c_str ());
2480 }
2481 fprintf ( fMakefile,
2482 "\t${objcopy} -O binary %s $@\n",
2483 backend->GetFullName ( junk_tmp ).c_str () );
2484 GenerateBuildMapCode ( &junk_tmp );
2485 fprintf ( fMakefile,
2486 "\t-@${rm} %s 2>$(NUL)\n",
2487 backend->GetFullName ( junk_tmp ).c_str () );
2488
2489 delete target_file;
2490 }
2491
2492
2493 MingwBootProgramModuleHandler::MingwBootProgramModuleHandler (
2494 const Module& module_ )
2495 : MingwModuleHandler ( module_ )
2496 {
2497 }
2498
2499 void
2500 MingwBootProgramModuleHandler::Process ()
2501 {
2502 GenerateBootProgramModuleTarget ();
2503 }
2504
2505 void
2506 MingwBootProgramModuleHandler::GenerateBootProgramModuleTarget ()
2507 {
2508 fprintf ( fMakefile, "# BOOT PROGRAM MODULE TARGET\n" );
2509
2510 string targetName ( module.output->name );
2511 string targetMacro ( GetTargetMacro (module) );
2512 string workingDirectory = GetWorkingDirectory ();
2513 FileLocation junk_tmp ( TemporaryDirectory,
2514 "",
2515 module.name + ".junk.tmp" );
2516 FileLocation junk_elf ( TemporaryDirectory,
2517 "",
2518 module.name + ".junk.elf" );
2519 FileLocation junk_cpy ( TemporaryDirectory,
2520 "",
2521 module.name + ".junk.elf" );
2522 CLEAN_FILE ( junk_tmp );
2523 CLEAN_FILE ( junk_elf );
2524 CLEAN_FILE ( junk_cpy );
2525 string objectsMacro = GetObjectsMacro ( module );
2526 string libsMacro = GetLibsMacro ();
2527 const Module *payload = module.project.LocateModule ( module.payload );
2528
2529 GenerateRules ();
2530
2531 const FileLocation *target_file = GetTargetFilename ( module, NULL );
2532 fprintf ( fMakefile, "%s: %s %s %s | %s\n",
2533 targetMacro.c_str (),
2534 objectsMacro.c_str (),
2535 libsMacro.c_str (),
2536 payload->name.c_str (),
2537 backend->GetFullPath ( *target_file ).c_str () );
2538
2539 fprintf ( fMakefile, "\t$(ECHO_BOOTPROG)\n" );
2540
2541 fprintf ( fMakefile, "\t$(%s_PREPARE) $(OUTPUT)$(SEP)%s %s\n",
2542 module.buildtype.c_str (),
2543 NormalizeFilename( backend->GetFullName ( *payload->output ) ).c_str (),
2544 backend->GetFullName ( junk_cpy ).c_str () );
2545
2546 fprintf ( fMakefile, "\t${objcopy} $(%s_FLATFORMAT) %s %s\n",
2547 module.buildtype.c_str (),
2548 backend->GetFullName ( junk_cpy ).c_str (),
2549 backend->GetFullName ( junk_tmp ).c_str () );
2550
2551 fprintf ( fMakefile, "\t${ld} $(%s_LINKFORMAT) %s %s -o %s\n",
2552 module.buildtype.c_str (),
2553 libsMacro.c_str (),
2554 backend->GetFullName ( junk_tmp ).c_str (),
2555 backend->GetFullName ( junk_elf ).c_str () );
2556
2557 fprintf ( fMakefile, "\t${objcopy} $(%s_COPYFORMAT) %s $(INTERMEDIATE)$(SEP)%s\n",
2558 module.buildtype.c_str (),
2559 backend->GetFullName ( junk_elf ).c_str (),
2560 backend->GetFullName ( *module.output ) .c_str () );
2561
2562 fprintf ( fMakefile,
2563 "\t-@${rm} %s %s %s 2>$(NUL)\n",
2564 backend->GetFullName ( junk_tmp ).c_str (),
2565 backend->GetFullName ( junk_elf ).c_str (),
2566 backend->GetFullName ( junk_cpy ).c_str () );
2567
2568 delete target_file;
2569 }
2570
2571
2572 MingwIsoModuleHandler::MingwIsoModuleHandler (
2573 const Module& module_ )
2574
2575 : MingwModuleHandler ( module_ )
2576 {
2577 }
2578
2579 void
2580 MingwIsoModuleHandler::Process ()
2581 {
2582 GenerateIsoModuleTarget ();
2583 }
2584
2585 void
2586 MingwIsoModuleHandler::OutputBootstrapfileCopyCommands (
2587 const string& bootcdDirectory,
2588 vector<FileLocation>& destinations )
2589 {
2590 for ( std::map<std::string, Module*>::const_iterator p = module.project.modules.begin (); p != module.project.modules.end (); ++ p )
2591 {
2592 const Module& m = *p->second;
2593 if ( !m.enabled )
2594 continue;
2595 if ( m.bootstrap != NULL )
2596 {
2597 FileLocation targetFile ( OutputDirectory,
2598 m.bootstrap->base.length () > 0
2599 ? bootcdDirectory + sSep + m.bootstrap->base
2600 : bootcdDirectory,
2601 m.bootstrap->nameoncd );
2602 OutputCopyCommandSingle ( *m.output, targetFile );
2603 destinations.push_back ( targetFile );
2604 }
2605 }
2606 }
2607
2608 void
2609 MingwIsoModuleHandler::OutputCdfileCopyCommands (
2610 const string& bootcdDirectory,
2611 std::vector<FileLocation>& destinations )
2612 {
2613 for ( size_t i = 0; i < module.project.cdfiles.size (); i++ )
2614 {
2615 const CDFile& cdfile = *module.project.cdfiles[i];
2616 FileLocation targetFile ( OutputDirectory,
2617 cdfile.target->relative_path.length () > 0
2618 ? bootcdDirectory + sSep + cdfile.target->relative_path
2619 : bootcdDirectory,
2620 cdfile.target->name );
2621 OutputCopyCommandSingle ( *cdfile.source, targetFile );
2622 destinations.push_back ( targetFile );
2623 }
2624 for ( size_t i = 0; i < module.cdfiles.size (); i++ )
2625 {
2626 const CDFile& cdfile = *module.cdfiles[i];
2627 FileLocation targetFile ( OutputDirectory,
2628 cdfile.target->relative_path.length () > 0
2629 ? bootcdDirectory + sSep + cdfile.target->relative_path
2630 : bootcdDirectory,
2631 cdfile.target->name );
2632 OutputCopyCommandSingle ( *cdfile.source, targetFile );
2633 destinations.push_back ( targetFile );
2634 }
2635 }
2636
2637 void
2638 MingwIsoModuleHandler::GetBootstrapCdDirectories ( vector<FileLocation>& out,
2639 const string& bootcdDirectory )
2640 {
2641 for ( std::map<std::string, Module*>::const_iterator p = module.project.modules.begin (); p != module.project.modules.end (); ++ p )
2642 {
2643 const Module& m = *p->second;
2644 if ( !m.enabled )
2645 continue;
2646 if ( m.bootstrap != NULL )
2647 {
2648 FileLocation targetDirectory ( OutputDirectory,
2649 m.bootstrap->base.length () > 0
2650 ? bootcdDirectory + sSep + m.bootstrap->base
2651 : bootcdDirectory,
2652 "" );
2653 out.push_back ( targetDirectory );
2654 }
2655 }
2656 }
2657
2658 void
2659 MingwIsoModuleHandler::GetNonModuleCdDirectories ( vector<FileLocation>& out,
2660 const string& bootcdDirectory )
2661 {
2662 for ( size_t i = 0; i < module.project.cdfiles.size (); i++ )
2663 {
2664 const CDFile& cdfile = *module.project.cdfiles[i];
2665 FileLocation targetDirectory ( OutputDirectory,
2666 cdfile.target->relative_path.length () > 0
2667 ? bootcdDirectory + sSep + cdfile.target->relative_path
2668 : bootcdDirectory,
2669 "" );
2670 out.push_back( targetDirectory );
2671 }
2672 }
2673
2674 void
2675 MingwIsoModuleHandler::GetCdDirectories ( vector<FileLocation>& out,
2676 const string& bootcdDirectory )
2677 {
2678 GetBootstrapCdDirectories ( out, bootcdDirectory );
2679 GetNonModuleCdDirectories ( out, bootcdDirectory );
2680 }
2681
2682 void
2683 MingwIsoModuleHandler::GetBootstrapCdFiles (
2684 vector<FileLocation>& out ) const
2685 {
2686 for ( std::map<std::string, Module*>::const_iterator p = module.project.modules.begin (); p != module.project.modules.end (); ++ p )
2687 {
2688 const Module& m = *p->second;
2689 if ( !m.enabled )
2690 continue;
2691 if ( m.bootstrap != NULL )
2692 {
2693 out.push_back ( *m.output );
2694 }
2695 }
2696 }
2697
2698 void
2699 MingwIsoModuleHandler::GetNonModuleCdFiles (
2700 vector<FileLocation>& out ) const
2701 {
2702 for ( size_t i = 0; i < module.project.cdfiles.size (); i++ )
2703 {
2704 const CDFile& cdfile = *module.project.cdfiles[i];
2705 out.push_back ( *cdfile.source );
2706 }
2707 }
2708
2709 void
2710 MingwIsoModuleHandler::GetCdFiles (
2711 vector<FileLocation>& out ) const
2712 {
2713 GetBootstrapCdFiles ( out );
2714 GetNonModuleCdFiles ( out );
2715 }
2716
2717 void
2718 MingwIsoModuleHandler::GenerateIsoModuleTarget ()
2719 {
2720 fprintf ( fMakefile, "# ISO MODULE TARGET\n" );
2721 string bootcdDirectory = module.name;
2722 FileLocation bootcd ( OutputDirectory,
2723 bootcdDirectory,
2724 "" );
2725 FileLocation bootcdReactos ( OutputDirectory,
2726 bootcdDirectory + sSep + Environment::GetCdOutputPath (),
2727 "" );
2728
2729 // bootsector
2730 const Module* bootModule = module.bootSector->bootSectorModule;
2731
2732 if (!bootModule)
2733 {
2734 throw InvalidOperationException ( module.node.location.c_str(),
2735 0,
2736 "Invalid bootsector. module '%s' requires <bootsector>",
2737 module.name.c_str ());
2738 }
2739
2740 const FileLocation *isoboot = bootModule->output;
2741
2742 // prepare reactos.dff and reactos.inf
2743 FileLocation reactosDff ( SourceDirectory,
2744 "boot" + sSep + "bootdata" + sSep + "packages",
2745 "reactos.dff" );
2746 FileLocation reactosInf ( bootcdReactos.directory,
2747 bootcdReactos.relative_path,
2748 "reactos.inf" );
2749
2750 /*
2751 We use only the name and not full FileLocation(ouput) because Iso/LiveIso are an exception to the general rule.
2752 Iso/LiveIso outputs are generated in code base root
2753 */
2754 string IsoName = module.output->name;
2755
2756 fprintf ( fMakefile, ".PHONY: %s_CABINET\n\n",
2757 module.name.c_str () );
2758 fprintf ( fMakefile, "%s_CABINET: all $(cabman_TARGET) %s | %s\n",
2759 module.name.c_str (),
2760 backend->GetFullName ( reactosDff ).c_str (),
2761 backend->GetFullPath ( bootcdReactos ).c_str () );
2762 fprintf ( fMakefile,
2763 "\t$(Q)$(cabman_TARGET) -C %s -L %s -I -P $(OUTPUT)\n",
2764 backend->GetFullName ( reactosDff ).c_str (),
2765 backend->GetFullPath ( bootcdReactos ).c_str () );
2766 fprintf ( fMakefile,
2767 "\t$(Q)$(cabman_TARGET) -C %s -RC %s -L %s -N -P $(OUTPUT)\n",
2768 backend->GetFullName ( reactosDff ).c_str (),
2769 backend->GetFullName ( reactosInf ).c_str (),
2770 backend->GetFullPath ( bootcdReactos ).c_str ());
2771 fprintf ( fMakefile,
2772 "\t-@${rm} %s 2>$(NUL)\n\n",
2773 backend->GetFullName ( reactosInf ).c_str () );
2774
2775 std::vector<FileLocation> sourceFiles;
2776 OutputBootstrapfileCopyCommands ( bootcdDirectory, sourceFiles );
2777 OutputCdfileCopyCommands ( bootcdDirectory, sourceFiles );
2778
2779 fprintf( fMakefile,
2780 "\n%s_OBJS := %s\n\n",
2781 module.name.c_str (),
2782 v2s ( backend, sourceFiles, 5 ).c_str () );
2783
2784 fprintf ( fMakefile, ".PHONY: %s\n\n",
2785 module.name.c_str ());
2786 fprintf ( fMakefile,
2787 "%s: $(%s_OBJS) %s_CABINET %s $(cdmake_TARGET) | %s\n",
2788 module.name.c_str (),
2789 module.name.c_str (),
2790 module.name.c_str (),
2791 backend->GetFullName ( *isoboot ).c_str (),
2792 backend->GetFullPath ( FileLocation ( OutputDirectory,
2793 bootcdDirectory,
2794 "" ) ).c_str () );
2795
2796 fprintf ( fMakefile, "\t$(ECHO_CDMAKE)\n" );
2797 fprintf ( fMakefile,
2798 "\t$(Q)$(cdmake_TARGET) -v -j -m -b %s %s REACTOS %s\n",
2799 backend->GetFullName ( *isoboot ).c_str (),
2800 backend->GetFullPath ( bootcd ).c_str (),
2801 IsoName.c_str() );
2802 fprintf ( fMakefile,
2803 "\n" );
2804 }
2805
2806
2807 MingwLiveIsoModuleHandler::MingwLiveIsoModuleHandler (
2808 const Module& module_ )
2809
2810 : MingwModuleHandler ( module_ )
2811 {
2812 }
2813
2814 void
2815 MingwLiveIsoModuleHandler::Process ()
2816 {
2817 GenerateLiveIsoModuleTarget ();
2818 }
2819
2820 void
2821 MingwLiveIsoModuleHandler::CreateDirectory ( const string& directory )
2822 {
2823 FileLocation dir ( OutputDirectory,
2824 directory,
2825 "" );
2826 MingwModuleHandler::PassThruCacheDirectory ( &dir );
2827 }
2828
2829 void
2830 MingwLiveIsoModuleHandler::OutputModuleCopyCommands ( string& livecdDirectory,
2831 string& reactosDirectory,
2832 std::vector<FileLocation>& destinations )
2833 {
2834 for ( std::map<std::string, Module*>::const_iterator p = module.project.modules.begin (); p != module.project.modules.end (); ++ p )
2835 {
2836 const Module& m = *p->second;
2837 if ( !m.enabled )
2838 continue;
2839 if ( m.install )
2840 {
2841 const Module& aliasedModule = backend->GetAliasedModuleOrModule ( m );
2842 FileLocation destination ( OutputDirectory,
2843 m.install->relative_path.length () > 0
2844 ? livecdDirectory + sSep + reactosDirectory + sSep + m.install->relative_path
2845 : livecdDirectory + sSep + reactosDirectory,
2846 m.install->name );
2847 OutputCopyCommandSingle ( *aliasedModule.output,
2848 destination);
2849 destinations.push_back ( destination );
2850 }
2851 }
2852 }
2853
2854 void
2855 MingwLiveIsoModuleHandler::OutputNonModuleCopyCommands ( string& livecdDirectory,
2856 string& reactosDirectory,
2857 std::vector<FileLocation>& destinations )
2858 {
2859 for ( size_t i = 0; i < module.project.installfiles.size (); i++ )
2860 {
2861 const InstallFile& installfile = *module.project.installfiles[i];
2862 FileLocation target ( OutputDirectory,
2863 installfile.target->relative_path.length () > 0
2864 ? livecdDirectory + sSep + reactosDirectory + sSep + installfile.target->relative_path
2865 : livecdDirectory + sSep + reactosDirectory,
2866 installfile.target->name );
2867 OutputCopyCommandSingle ( *installfile.source, target );
2868 destinations.push_back ( target );
2869 }
2870 }
2871
2872 void
2873 MingwLiveIsoModuleHandler::OutputProfilesDirectoryCommands ( string& livecdDirectory,
2874 vector<FileLocation>& destinations )
2875 {
2876 CreateDirectory ( livecdDirectory + sSep + "Profiles" );
2877 CreateDirectory ( livecdDirectory + sSep + "Profiles" + sSep + "All Users") ;
2878 CreateDirectory ( livecdDirectory + sSep + "Profiles" + sSep + "All Users" + sSep + "Desktop" );
2879 CreateDirectory ( livecdDirectory + sSep + "Profiles" + sSep + "Default User" );
2880 CreateDirectory ( livecdDirectory + sSep + "Profiles" + sSep + "Default User" + sSep + "Desktop" );
2881 CreateDirectory ( livecdDirectory + sSep + "Profiles" + sSep + "Default User" + sSep + "My Documents" );
2882
2883 FileLocation livecdIni ( SourceDirectory,
2884 "boot" + sSep + "bootdata",
2885 "livecd.ini" );
2886 FileLocation destination ( OutputDirectory,
2887 livecdDirectory,
2888 "freeldr.ini" );
2889 OutputCopyCommandSingle ( livecdIni,
2890 destination );
2891 destinations.push_back ( destination );
2892 }
2893
2894 void
2895 MingwLiveIsoModuleHandler::OutputLoaderCommands ( string& livecdDirectory,
2896 std::vector<FileLocation>& destinations )
2897 {
2898 FileLocation freeldr ( OutputDirectory,
2899 "boot" + sSep + "freeldr" + sSep + "freeldr",
2900 "freeldr.sys" );
2901 FileLocation destination ( OutputDirectory,
2902 livecdDirectory + sSep + "loader",
2903 "setupldr.sys" );
2904 OutputCopyCommandSingle ( freeldr,
2905 destination );
2906 destinations.push_back ( destination );
2907 }
2908
2909 void
2910 MingwLiveIsoModuleHandler::OutputRegistryCommands ( string& livecdDirectory )
2911 {
2912 fprintf ( fMakefile, "# REGISTRY COMMANDS\n" );
2913 FileLocation reactosSystem32ConfigDirectory ( OutputDirectory,
2914 livecdDirectory + sSep + "reactos" + sSep + "system32" + sSep + "config",
2915 "" );
2916 fprintf ( fMakefile,
2917 "\t$(ECHO_MKHIVE)\n" );
2918 fprintf ( fMakefile,
2919 "\t$(mkhive_TARGET) boot%cbootdata %s $(ARCH) boot%cbootdata%clivecd.inf boot%cbootdata%chiveinst_$(ARCH).inf\n",
2920 cSep, backend->GetFullPath ( reactosSystem32ConfigDirectory ).c_str (),
2921 cSep, cSep, cSep, cSep );
2922 }
2923
2924 void
2925 MingwLiveIsoModuleHandler::GenerateLiveIsoModuleTarget ()
2926 {
2927 fprintf ( fMakefile, "# LIVE ISO MODULE TARGET\n" );
2928 string livecdDirectory = module.name;
2929 FileLocation livecd ( OutputDirectory, livecdDirectory, "" );
2930
2931 string IsoName;
2932
2933 // bootsector
2934 const Module* bootModule = module.bootSector->bootSectorModule;
2935
2936 if (!bootModule)
2937 {
2938 throw InvalidOperationException ( module.node.location.c_str(),
2939 0,
2940 "Invalid bootsector. module '%s' requires <bootsector>",
2941 module.name.c_str ());
2942 }
2943
2944 const FileLocation *isoboot = bootModule->output;
2945
2946 /*
2947 We use only the name and not full FileLocation(ouput) because Iso/LiveIso are an exception to the general rule.
2948 Iso/LiveIso outputs are generated in code base root
2949 */
2950 IsoName = module.output->name;
2951
2952 string reactosDirectory = "reactos";
2953 string livecdReactosNoFixup = livecdDirectory + sSep + reactosDirectory;
2954 FileLocation livecdReactos ( OutputDirectory,
2955 livecdReactosNoFixup,
2956 "" );
2957 CLEAN_FILE ( livecdReactos );
2958
2959 std::vector<FileLocation> sourceFiles;
2960 OutputModuleCopyCommands ( livecdDirectory,
2961 reactosDirectory,
2962 sourceFiles );
2963 OutputNonModuleCopyCommands ( livecdDirectory,
2964 reactosDirectory,
2965 sourceFiles );
2966 OutputProfilesDirectoryCommands ( livecdDirectory, sourceFiles );
2967 OutputLoaderCommands ( livecdDirectory, sourceFiles );
2968
2969 fprintf( fMakefile,
2970 "\n%s_OBJS := %s\n\n",
2971 module.name.c_str (),
2972 v2s ( backend, sourceFiles, 5 ).c_str () );
2973
2974 fprintf ( fMakefile, ".PHONY: %s\n\n",
2975 module.name.c_str ());
2976 fprintf ( fMakefile,
2977 "%s : $(%s_OBJS) %s %s $(mkhive_TARGET) $(cdmake_TARGET)\n",
2978 module.name.c_str (),
2979 module.name.c_str (),
2980 backend->GetFullName ( *isoboot) .c_str (),
2981 backend->GetFullPath ( FileLocation ( OutputDirectory,
2982 livecdDirectory,
2983 "" ) ).c_str () );
2984 OutputRegistryCommands ( livecdDirectory );
2985 fprintf ( fMakefile, "\t$(ECHO_CDMAKE)\n" );
2986 fprintf ( fMakefile,
2987 "\t$(Q)$(cdmake_TARGET) -v -m -j -b %s %s REACTOS %s\n",
2988 backend->GetFullName( *isoboot ).c_str (),
2989 backend->GetFullPath ( livecd ).c_str (),
2990 IsoName.c_str() );
2991 fprintf ( fMakefile,
2992 "\n" );
2993 }
2994
2995
2996 MingwTestModuleHandler::MingwTestModuleHandler (
2997 const Module& module_ )
2998
2999 : MingwModuleHandler ( module_ )
3000 {
3001 }
3002
3003 void
3004 MingwTestModuleHandler::Process ()
3005 {
3006 GenerateTestModuleTarget ();
3007 }
3008
3009 /* caller needs to delete the returned object */
3010 void
3011 MingwTestModuleHandler::GetModuleSpecificCompilationUnits ( vector<CompilationUnit*>& compilationUnits )
3012 {
3013 compilationUnits.push_back ( new CompilationUnit ( new File ( IntermediateDirectory, module.output->relative_path + sSep + "..", module.name + "_hooks.c", false, "", false ) ) );
3014 compilationUnits.push_back ( new CompilationUnit ( new File ( IntermediateDirectory, module.output->relative_path + sSep + "..", module.name + "_stubs.S", false, "", false ) ) );
3015 compilationUnits.push_back ( new CompilationUnit ( new File ( IntermediateDirectory, module.output->relative_path + sSep + "..", module.name + "_startup.c", false, "", false ) ) );
3016 }
3017
3018 void
3019 MingwTestModuleHandler::GenerateTestModuleTarget ()
3020 {
3021 GenerateRules ();
3022 GenerateLinkerCommand ();
3023 }
3024
3025
3026 MingwAliasModuleHandler::MingwAliasModuleHandler (
3027 const Module& module_ )
3028
3029 : MingwModuleHandler ( module_ )
3030 {
3031 }
3032
3033 void
3034 MingwAliasModuleHandler::Process ()
3035 {
3036 }
3037
3038
3039 MingwCabinetModuleHandler::MingwCabinetModuleHandler (
3040 const Module& module_ )
3041
3042 : MingwModuleHandler ( module_ )
3043 {
3044 }
3045
3046 void
3047 MingwCabinetModuleHandler::Process ()
3048 {
3049 fprintf ( fMakefile, "# CABINET MODULE TARGET\n" );
3050 string targetMacro ( GetTargetMacro (module) );
3051
3052 GenerateRules ();
3053
3054 const FileLocation *target_file = GetTargetFilename ( module, NULL );
3055 fprintf ( fMakefile, "%s: $(cabman_TARGET) | %s\n",
3056 targetMacro.c_str (),
3057 backend->GetFullPath ( *target_file ).c_str () );
3058
3059 fprintf ( fMakefile, "\t$(ECHO_CABMAN)\n" );
3060 fprintf ( fMakefile,
3061 "\t$(Q)$(cabman_TARGET) -M raw -S %s $(%s_SOURCES)\n", // Escape the asterisk for Make
3062 targetMacro.c_str (),
3063 module.name.c_str());
3064 }
3065
3066 MingwElfExecutableModuleHandler::MingwElfExecutableModuleHandler (
3067 const Module& module_ )
3068
3069 : MingwModuleHandler ( module_ )
3070 {
3071 }
3072
3073 void
3074 MingwElfExecutableModuleHandler::Process ()
3075 {
3076 string targetName ( module.output->name );
3077 string targetMacro ( GetTargetMacro (module) );
3078 string workingDirectory = GetWorkingDirectory ();
3079 string objectsMacro = GetObjectsMacro ( module );
3080 string libsMacro = GetLibsMacro ();
3081 string debugFormat = GetDebugFormat ();
3082
3083 fprintf ( fMakefile, "# ELF EXECUTABLE TARGET\n" );
3084 GenerateRules ();
3085
3086 const FileLocation *target_file = GetTargetFilename ( module, NULL );
3087 fprintf ( fMakefile, "%s: %s %s | %s\n",
3088 targetMacro.c_str (),
3089 objectsMacro.c_str (),
3090 libsMacro.c_str (),
3091 backend->GetFullPath ( *target_file ).c_str () );
3092
3093 fprintf ( fMakefile, "\t$(ECHO_BOOTPROG)\n" );
3094
3095 fprintf ( fMakefile, "\t${gcc} $(%s_LINKFORMAT) %s %s -g%s -o %s\n",
3096 module.buildtype.c_str(),
3097 objectsMacro.c_str(),
3098 libsMacro.c_str(),
3099 debugFormat.c_str(),
3100 targetMacro.c_str () );
3101
3102 delete target_file;
3103 fprintf ( fMakefile, "#/ELF EXECUTABLE TARGET\n" );
3104 }