Merge aicom-network-branch (without NDIS changes for now)
[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 ( 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 = 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 SpecFileType
1983 MingwModuleHandler::IsSpecDefinitionFile () const
1984 {
1985 if(!module.importLibrary)
1986 return None;
1987
1988 std::string ext = GetExtension ( *module.importLibrary->source );
1989
1990 if ( ext == ".spec" )
1991 return Spec;
1992
1993 if ( ext == ".pspec" )
1994 return PSpec;
1995
1996 return None;
1997 }
1998
1999 /* caller needs to delete the returned object */
2000 const FileLocation*
2001 MingwModuleHandler::GetDefinitionFilename () const
2002 {
2003 if ( module.importLibrary == NULL )
2004 return NULL;
2005
2006 if ( IsSpecDefinitionFile () )
2007 {
2008 return new FileLocation ( IntermediateDirectory,
2009 module.importLibrary->source->relative_path,
2010 GetBasename ( module.importLibrary->source->name ) + "_" + module.name + ".auto.def" );
2011 }
2012 else
2013 {
2014 return new FileLocation ( SourceDirectory,
2015 module.importLibrary->source->relative_path,
2016 module.importLibrary->source->name );
2017 }
2018 }
2019
2020 void
2021 MingwModuleHandler::GetSpecObjectDependencies (
2022 vector<FileLocation>& dependencies,
2023 const FileLocation *file ) const
2024 {
2025 dependencies.push_back ( FileLocation ( IntermediateDirectory,
2026 file->relative_path,
2027 GetBasename ( file->name ) + "_" + module.name + ".stubs.c" ) );
2028 }
2029
2030 void
2031 MingwModuleHandler::GetMcObjectDependencies (
2032 vector<FileLocation>& dependencies,
2033 const FileLocation *file ) const
2034 {
2035 string basename = GetBasename ( file->name );
2036
2037 FileLocation defDependency ( IntermediateDirectory,
2038 "include/reactos",
2039 basename + ".h" );
2040 dependencies.push_back ( defDependency );
2041
2042 FileLocation stubsDependency ( IntermediateDirectory,
2043 file->relative_path,
2044 basename + ".rc" );
2045 dependencies.push_back ( stubsDependency );
2046 }
2047
2048 void
2049 MingwModuleHandler::GetWidlObjectDependencies (
2050 vector<FileLocation>& dependencies,
2051 const FileLocation *file ) const
2052 {
2053 string basename = GetBasename ( file->name );
2054 const FileLocation *generatedHeaderFilename = GetRpcServerHeaderFilename ( file );
2055
2056 FileLocation serverSourceDependency ( IntermediateDirectory,
2057 file->relative_path,
2058 basename + "_s.c" );
2059 dependencies.push_back ( serverSourceDependency );
2060 dependencies.push_back ( *generatedHeaderFilename );
2061
2062 delete generatedHeaderFilename;
2063 }
2064
2065 void
2066 MingwModuleHandler::GetDefinitionDependencies (
2067 vector<FileLocation>& dependencies ) const
2068 {
2069 const vector<CompilationUnit*>& compilationUnits = module.non_if_data.compilationUnits;
2070 for ( size_t i = 0; i < compilationUnits.size (); i++ )
2071 {
2072 const CompilationUnit& compilationUnit = *compilationUnits[i];
2073 const FileLocation& sourceFile = compilationUnit.GetFilename ();
2074 string extension = GetExtension ( sourceFile );
2075
2076 if (extension == ".spec" || extension == ".pspec")
2077 GetSpecObjectDependencies ( dependencies, &sourceFile );
2078
2079 if (extension == ".idl")
2080 {
2081 if ( ( module.type == RpcServer ) || ( module.type == RpcClient ) || ( module.type == RpcProxy ) )
2082 GetWidlObjectDependencies ( dependencies, &sourceFile );
2083 }
2084 }
2085 }
2086
2087 enum DebugSupportType
2088 {
2089 DebugKernelMode,
2090 DebugUserMode
2091 };
2092
2093 static void
2094 MingwAddDebugSupportLibraries ( Module& module, DebugSupportType type )
2095 {
2096 Library* pLibrary;
2097
2098 switch(type)
2099 {
2100 case DebugKernelMode:
2101 pLibrary = new Library ( module, "debugsup_ntoskrnl" );
2102 break;
2103
2104 case DebugUserMode:
2105 pLibrary = new Library ( module, "debugsup_ntdll" );
2106 break;
2107
2108 default:
2109 assert(0);
2110 }
2111
2112 module.non_if_data.libraries.push_back(pLibrary);
2113 }
2114
2115 static void
2116 MingwAddCRTLibrary( Module &module )
2117 {
2118 const char * crtAttr = module.CRT.c_str ();
2119 const char * crtLib = NULL;
2120
2121 if ( stricmp ( crtAttr, "libc" ) == 0 )
2122 crtLib = "crt";
2123 else if ( stricmp ( crtAttr, "msvcrt" ) == 0 )
2124 crtLib = "msvcrt";
2125 else if ( stricmp ( crtAttr, "libcntpr" ) == 0 )
2126 crtLib = "libcntpr";
2127 else if ( stricmp ( crtAttr, "ntdll" ) == 0 )
2128 crtLib = "ntdll";
2129
2130 if ( crtLib )
2131 {
2132 Library* pLibrary = new Library ( module, std::string ( crtLib ) );
2133
2134 if ( pLibrary->importedModule == NULL)
2135 {
2136 throw XMLInvalidBuildFileException (
2137 module.node.location,
2138 "module '%s' trying to import non-existant C runtime module '%s'",
2139 module.name.c_str(),
2140 crtLib );
2141 }
2142
2143 module.non_if_data.libraries.push_back ( pLibrary );
2144 }
2145 }
2146
2147 MingwBuildToolModuleHandler::MingwBuildToolModuleHandler ( const Module& module_ )
2148 : MingwModuleHandler ( module_ )
2149 {
2150 }
2151
2152 void
2153 MingwBuildToolModuleHandler::Process ()
2154 {
2155 GenerateBuildToolModuleTarget ();
2156 }
2157
2158 void
2159 MingwBuildToolModuleHandler::GenerateBuildToolModuleTarget ()
2160 {
2161 string targetMacro ( GetTargetMacro (module) );
2162 string objectsMacro = GetObjectsMacro ( module );
2163 string libsMacro = GetLibsMacro ();
2164
2165 GenerateRules ();
2166
2167 fprintf ( fMakefile, "# BUILD TOOL MODULE TARGET\n" );
2168
2169 string linker;
2170 if ( module.cplusplus )
2171 linker = "${host_gpp}";
2172 else
2173 linker = "${host_gcc}";
2174
2175 const FileLocation *target_file = GetTargetFilename ( module, NULL );
2176 fprintf ( fMakefile, "%s: %s %s | %s\n",
2177 targetMacro.c_str (),
2178 objectsMacro.c_str (),
2179 libsMacro.c_str (),
2180 backend->GetFullPath ( *target_file ).c_str () );
2181 fprintf ( fMakefile, "\t$(ECHO_HOSTLD)\n" );
2182 fprintf ( fMakefile,
2183 "\t%s %s -o $@ %s %s\n\n",
2184 linker.c_str (),
2185 GetLinkerMacro ().c_str (),
2186 objectsMacro.c_str (),
2187 libsMacro.c_str () );
2188
2189 delete target_file;
2190 }
2191
2192
2193 MingwKernelModuleHandler::MingwKernelModuleHandler (
2194 const Module& module_ )
2195
2196 : MingwModuleHandler ( module_ )
2197 {
2198 }
2199
2200 void
2201 MingwKernelModuleHandler::Process ()
2202 {
2203 GenerateKernelModuleTarget ();
2204 }
2205
2206 void
2207 MingwKernelModuleHandler::GenerateKernelModuleTarget ()
2208 {
2209 GenerateRules ();
2210 GenerateLinkerCommand ();
2211 }
2212
2213
2214 MingwKernelModeDLLModuleHandler::MingwKernelModeDLLModuleHandler (
2215 const Module& module_ )
2216
2217 : MingwModuleHandler ( module_ )
2218 {
2219 }
2220
2221
2222 void
2223 MingwKernelModeDLLModuleHandler::AddImplicitLibraries ( Module& module )
2224 {
2225 MingwAddCRTLibrary ( module );
2226 MingwAddDebugSupportLibraries ( module, DebugKernelMode );
2227 }
2228
2229 void
2230 MingwKernelModeDLLModuleHandler::Process ()
2231 {
2232 GenerateKernelModeDLLModuleTarget ();
2233 }
2234
2235 void
2236 MingwKernelModeDLLModuleHandler::GenerateKernelModeDLLModuleTarget ()
2237 {
2238 GenerateRules ();
2239 GenerateLinkerCommand ();
2240 }
2241
2242
2243 MingwNativeDLLModuleHandler::MingwNativeDLLModuleHandler (
2244 const Module& module_ )
2245
2246 : MingwModuleHandler ( module_ )
2247 {
2248 }
2249
2250 void
2251 MingwNativeDLLModuleHandler::AddImplicitLibraries ( Module& module )
2252 {
2253 MingwAddCRTLibrary ( module );
2254 MingwAddDebugSupportLibraries ( module, DebugUserMode );
2255 }
2256
2257 void
2258 MingwNativeDLLModuleHandler::Process ()
2259 {
2260 GenerateNativeDLLModuleTarget ();
2261 }
2262
2263 void
2264 MingwNativeDLLModuleHandler::GenerateNativeDLLModuleTarget ()
2265 {
2266 GenerateRules ();
2267 GenerateLinkerCommand ();
2268 }
2269
2270
2271 MingwNativeCUIModuleHandler::MingwNativeCUIModuleHandler (
2272 const Module& module_ )
2273
2274 : MingwModuleHandler ( module_ )
2275 {
2276 }
2277
2278 void
2279 MingwNativeCUIModuleHandler::AddImplicitLibraries ( Module& module )
2280 {
2281 MingwAddCRTLibrary ( module );
2282 MingwAddDebugSupportLibraries ( module, DebugUserMode );
2283 }
2284
2285 void
2286 MingwNativeCUIModuleHandler::Process ()
2287 {
2288 GenerateNativeCUIModuleTarget ();
2289 }
2290
2291 void
2292 MingwNativeCUIModuleHandler::GenerateNativeCUIModuleTarget ()
2293 {
2294 GenerateRules ();
2295 GenerateLinkerCommand ();
2296 }
2297
2298
2299 MingwWin32DLLModuleHandler::MingwWin32DLLModuleHandler (
2300 const Module& module_ )
2301
2302 : MingwModuleHandler ( module_ )
2303 {
2304 }
2305
2306 MingwWin32OCXModuleHandler::MingwWin32OCXModuleHandler (
2307 const Module& module_ )
2308
2309 : MingwModuleHandler ( module_ )
2310 {
2311 }
2312
2313 static void
2314 MingwAddImplicitLibraries( Module &module )
2315 {
2316 Library* pLibrary;
2317
2318 if ( module.type != Win32DLL
2319 && module.type != Win32OCX
2320 && module.type != Win32CUI
2321 && module.type != Win32GUI
2322 && module.type != Win32SCR)
2323 {
2324 return;
2325 }
2326
2327 if ( module.isDefaultEntryPoint )
2328 {
2329 if ( module.IsDLL () )
2330 {
2331 //pLibrary = new Library ( module, "__mingw_dllmain" );
2332 //module.non_if_data.libraries.insert ( module.non_if_data.libraries.begin(), pLibrary );
2333 }
2334 else
2335 {
2336 pLibrary = new Library ( module, module.isUnicode ? "mingw_wmain" : "mingw_main" );
2337 module.non_if_data.libraries.insert ( module.non_if_data.libraries.begin(), pLibrary );
2338 }
2339 }
2340
2341 pLibrary = new Library ( module, "mingw_common" );
2342 module.non_if_data.libraries.push_back ( pLibrary );
2343
2344 MingwAddCRTLibrary ( module );
2345 MingwAddDebugSupportLibraries ( module, DebugUserMode );
2346 }
2347
2348 void
2349 MingwWin32DLLModuleHandler::AddImplicitLibraries ( Module& module )
2350 {
2351 MingwAddImplicitLibraries ( module );
2352 }
2353
2354 void
2355 MingwWin32DLLModuleHandler::Process ()
2356 {
2357 GenerateWin32DLLModuleTarget ();
2358 }
2359
2360 void
2361 MingwWin32DLLModuleHandler::GenerateWin32DLLModuleTarget ()
2362 {
2363 GenerateRules ();
2364 GenerateLinkerCommand ();
2365 }
2366
2367
2368 void
2369 MingwWin32OCXModuleHandler::AddImplicitLibraries ( Module& module )
2370 {
2371 MingwAddImplicitLibraries ( module );
2372 }
2373
2374 void
2375 MingwWin32OCXModuleHandler::Process ()
2376 {
2377 GenerateWin32OCXModuleTarget ();
2378 }
2379
2380 void
2381 MingwWin32OCXModuleHandler::GenerateWin32OCXModuleTarget ()
2382 {
2383 GenerateRules ();
2384 GenerateLinkerCommand ();
2385 }
2386
2387
2388 MingwWin32CUIModuleHandler::MingwWin32CUIModuleHandler (
2389 const Module& module_ )
2390
2391 : MingwModuleHandler ( module_ )
2392 {
2393 }
2394
2395 void
2396 MingwWin32CUIModuleHandler::AddImplicitLibraries ( Module& module )
2397 {
2398 MingwAddImplicitLibraries ( module );
2399 }
2400
2401 void
2402 MingwWin32CUIModuleHandler::Process ()
2403 {
2404 GenerateWin32CUIModuleTarget ();
2405 }
2406
2407 void
2408 MingwWin32CUIModuleHandler::GenerateWin32CUIModuleTarget ()
2409 {
2410 GenerateRules ();
2411 GenerateLinkerCommand ();
2412 }
2413
2414
2415 MingwWin32GUIModuleHandler::MingwWin32GUIModuleHandler (
2416 const Module& module_ )
2417
2418 : MingwModuleHandler ( module_ )
2419 {
2420 }
2421
2422 void
2423 MingwWin32GUIModuleHandler::AddImplicitLibraries ( Module& module )
2424 {
2425 MingwAddImplicitLibraries ( module );
2426 }
2427
2428 void
2429 MingwWin32GUIModuleHandler::Process ()
2430 {
2431 GenerateWin32GUIModuleTarget ();
2432 }
2433
2434 void
2435 MingwWin32GUIModuleHandler::GenerateWin32GUIModuleTarget ()
2436 {
2437 GenerateRules ();
2438 GenerateLinkerCommand ();
2439 }
2440
2441
2442 MingwBootLoaderModuleHandler::MingwBootLoaderModuleHandler (
2443 const Module& module_ )
2444
2445 : MingwModuleHandler ( module_ )
2446 {
2447 }
2448
2449 void
2450 MingwBootLoaderModuleHandler::Process ()
2451 {
2452 GenerateBootLoaderModuleTarget ();
2453 }
2454
2455 void
2456 MingwBootLoaderModuleHandler::GenerateBootLoaderModuleTarget ()
2457 {
2458 fprintf ( fMakefile, "# BOOT LOADER MODULE TARGET\n" );
2459 string targetName ( module.output->name );
2460 string targetMacro ( GetTargetMacro (module) );
2461 string workingDirectory = GetWorkingDirectory ();
2462 FileLocation junk_tmp ( TemporaryDirectory,
2463 "",
2464 module.name + ".junk.tmp" );
2465 CLEAN_FILE ( junk_tmp );
2466 string objectsMacro = GetObjectsMacro ( module );
2467 string libsMacro = GetLibsMacro ();
2468
2469 GenerateRules ();
2470
2471 const FileLocation *target_file = GetTargetFilename ( module, NULL );
2472 fprintf ( fMakefile, "%s: %s %s | %s\n",
2473 targetMacro.c_str (),
2474 objectsMacro.c_str (),
2475 libsMacro.c_str (),
2476 backend->GetFullPath ( *target_file ).c_str () );
2477
2478 fprintf ( fMakefile, "\t$(ECHO_LD)\n" );
2479
2480 if (Environment::GetArch() == "arm")
2481 {
2482 fprintf ( fMakefile,
2483 "\t${gcc} -Wl,--subsystem,native -o %s %s %s %s -nostartfiles -nostdlib\n",
2484 backend->GetFullName ( junk_tmp ).c_str (),
2485 objectsMacro.c_str (),
2486 libsMacro.c_str (),
2487 GetLinkerMacro ().c_str ());
2488 }
2489 else
2490 {
2491 fprintf ( fMakefile,
2492 "\t${gcc} -Wl,--subsystem,native -Wl,-Ttext,0x8000 -o %s %s %s %s -nostartfiles -nostdlib\n",
2493 backend->GetFullName ( junk_tmp ).c_str (),
2494 objectsMacro.c_str (),
2495 libsMacro.c_str (),
2496 GetLinkerMacro ().c_str ());
2497 }
2498 fprintf ( fMakefile,
2499 "\t${objcopy} -O binary %s $@\n",
2500 backend->GetFullName ( junk_tmp ).c_str () );
2501 GenerateBuildMapCode ( &junk_tmp );
2502 fprintf ( fMakefile,
2503 "\t-@${rm} %s 2>$(NUL)\n",
2504 backend->GetFullName ( junk_tmp ).c_str () );
2505
2506 delete target_file;
2507 }
2508
2509
2510 MingwBootProgramModuleHandler::MingwBootProgramModuleHandler (
2511 const Module& module_ )
2512 : MingwModuleHandler ( module_ )
2513 {
2514 }
2515
2516 void
2517 MingwBootProgramModuleHandler::Process ()
2518 {
2519 GenerateBootProgramModuleTarget ();
2520 }
2521
2522 void
2523 MingwBootProgramModuleHandler::GenerateBootProgramModuleTarget ()
2524 {
2525 fprintf ( fMakefile, "# BOOT PROGRAM MODULE TARGET\n" );
2526
2527 string targetName ( module.output->name );
2528 string targetMacro ( GetTargetMacro (module) );
2529 string workingDirectory = GetWorkingDirectory ();
2530 FileLocation junk_tmp ( TemporaryDirectory,
2531 "",
2532 module.name + ".junk.tmp" );
2533 FileLocation junk_elf ( TemporaryDirectory,
2534 "",
2535 module.name + ".junk.elf" );
2536 FileLocation junk_cpy ( TemporaryDirectory,
2537 "",
2538 module.name + ".junk.elf" );
2539 CLEAN_FILE ( junk_tmp );
2540 CLEAN_FILE ( junk_elf );
2541 CLEAN_FILE ( junk_cpy );
2542 string objectsMacro = GetObjectsMacro ( module );
2543 string libsMacro = GetLibsMacro ();
2544 const Module *payload = module.project.LocateModule ( module.payload );
2545
2546 GenerateRules ();
2547
2548 const FileLocation *target_file = GetTargetFilename ( module, NULL );
2549 fprintf ( fMakefile, "%s: %s %s %s | %s\n",
2550 targetMacro.c_str (),
2551 objectsMacro.c_str (),
2552 libsMacro.c_str (),
2553 payload->name.c_str (),
2554 backend->GetFullPath ( *target_file ).c_str () );
2555
2556 fprintf ( fMakefile, "\t$(ECHO_BOOTPROG)\n" );
2557
2558 fprintf ( fMakefile, "\t$(%s_PREPARE) $(OUTPUT)$(SEP)%s %s\n",
2559 module.buildtype.c_str (),
2560 NormalizeFilename( backend->GetFullName ( *payload->output ) ).c_str (),
2561 backend->GetFullName ( junk_cpy ).c_str () );
2562
2563 fprintf ( fMakefile, "\t${objcopy} $(%s_FLATFORMAT) %s %s\n",
2564 module.buildtype.c_str (),
2565 backend->GetFullName ( junk_cpy ).c_str (),
2566 backend->GetFullName ( junk_tmp ).c_str () );
2567
2568 fprintf ( fMakefile, "\t${ld} $(%s_LINKFORMAT) %s %s -o %s\n",
2569 module.buildtype.c_str (),
2570 libsMacro.c_str (),
2571 backend->GetFullName ( junk_tmp ).c_str (),
2572 backend->GetFullName ( junk_elf ).c_str () );
2573
2574 fprintf ( fMakefile, "\t${objcopy} $(%s_COPYFORMAT) %s $(INTERMEDIATE)$(SEP)%s\n",
2575 module.buildtype.c_str (),
2576 backend->GetFullName ( junk_elf ).c_str (),
2577 backend->GetFullName ( *module.output ) .c_str () );
2578
2579 fprintf ( fMakefile,
2580 "\t-@${rm} %s %s %s 2>$(NUL)\n",
2581 backend->GetFullName ( junk_tmp ).c_str (),
2582 backend->GetFullName ( junk_elf ).c_str (),
2583 backend->GetFullName ( junk_cpy ).c_str () );
2584
2585 delete target_file;
2586 }
2587
2588
2589 MingwIsoModuleHandler::MingwIsoModuleHandler (
2590 const Module& module_ )
2591
2592 : MingwModuleHandler ( module_ )
2593 {
2594 }
2595
2596 void
2597 MingwIsoModuleHandler::Process ()
2598 {
2599 GenerateIsoModuleTarget ();
2600 }
2601
2602 void
2603 MingwIsoModuleHandler::OutputBootstrapfileCopyCommands (
2604 const string& bootcdDirectory,
2605 vector<FileLocation>& destinations )
2606 {
2607 for ( std::map<std::string, Module*>::const_iterator p = module.project.modules.begin (); p != module.project.modules.end (); ++ p )
2608 {
2609 const Module& m = *p->second;
2610 if ( !m.enabled )
2611 continue;
2612 if ( m.bootstrap != NULL )
2613 {
2614 FileLocation targetFile ( OutputDirectory,
2615 m.bootstrap->base.length () > 0
2616 ? bootcdDirectory + sSep + m.bootstrap->base
2617 : bootcdDirectory,
2618 m.bootstrap->nameoncd );
2619 OutputCopyCommandSingle ( *m.output, targetFile );
2620 destinations.push_back ( targetFile );
2621 }
2622 }
2623 }
2624
2625 void
2626 MingwIsoModuleHandler::OutputCdfileCopyCommands (
2627 const string& bootcdDirectory,
2628 std::vector<FileLocation>& destinations )
2629 {
2630 for ( size_t i = 0; i < module.project.cdfiles.size (); i++ )
2631 {
2632 const CDFile& cdfile = *module.project.cdfiles[i];
2633 FileLocation targetFile ( OutputDirectory,
2634 cdfile.target->relative_path.length () > 0
2635 ? bootcdDirectory + sSep + cdfile.target->relative_path
2636 : bootcdDirectory,
2637 cdfile.target->name );
2638 OutputCopyCommandSingle ( *cdfile.source, targetFile );
2639 destinations.push_back ( targetFile );
2640 }
2641 for ( size_t i = 0; i < module.cdfiles.size (); i++ )
2642 {
2643 const CDFile& cdfile = *module.cdfiles[i];
2644 FileLocation targetFile ( OutputDirectory,
2645 cdfile.target->relative_path.length () > 0
2646 ? bootcdDirectory + sSep + cdfile.target->relative_path
2647 : bootcdDirectory,
2648 cdfile.target->name );
2649 OutputCopyCommandSingle ( *cdfile.source, targetFile );
2650 destinations.push_back ( targetFile );
2651 }
2652 }
2653
2654 void
2655 MingwIsoModuleHandler::GetBootstrapCdDirectories ( vector<FileLocation>& out,
2656 const string& bootcdDirectory )
2657 {
2658 for ( std::map<std::string, Module*>::const_iterator p = module.project.modules.begin (); p != module.project.modules.end (); ++ p )
2659 {
2660 const Module& m = *p->second;
2661 if ( !m.enabled )
2662 continue;
2663 if ( m.bootstrap != NULL )
2664 {
2665 FileLocation targetDirectory ( OutputDirectory,
2666 m.bootstrap->base.length () > 0
2667 ? bootcdDirectory + sSep + m.bootstrap->base
2668 : bootcdDirectory,
2669 "" );
2670 out.push_back ( targetDirectory );
2671 }
2672 }
2673 }
2674
2675 void
2676 MingwIsoModuleHandler::GetNonModuleCdDirectories ( vector<FileLocation>& out,
2677 const string& bootcdDirectory )
2678 {
2679 for ( size_t i = 0; i < module.project.cdfiles.size (); i++ )
2680 {
2681 const CDFile& cdfile = *module.project.cdfiles[i];
2682 FileLocation targetDirectory ( OutputDirectory,
2683 cdfile.target->relative_path.length () > 0
2684 ? bootcdDirectory + sSep + cdfile.target->relative_path
2685 : bootcdDirectory,
2686 "" );
2687 out.push_back( targetDirectory );
2688 }
2689 }
2690
2691 void
2692 MingwIsoModuleHandler::GetCdDirectories ( vector<FileLocation>& out,
2693 const string& bootcdDirectory )
2694 {
2695 GetBootstrapCdDirectories ( out, bootcdDirectory );
2696 GetNonModuleCdDirectories ( out, bootcdDirectory );
2697 }
2698
2699 void
2700 MingwIsoModuleHandler::GetBootstrapCdFiles (
2701 vector<FileLocation>& out ) const
2702 {
2703 for ( std::map<std::string, Module*>::const_iterator p = module.project.modules.begin (); p != module.project.modules.end (); ++ p )
2704 {
2705 const Module& m = *p->second;
2706 if ( !m.enabled )
2707 continue;
2708 if ( m.bootstrap != NULL )
2709 {
2710 out.push_back ( *m.output );
2711 }
2712 }
2713 }
2714
2715 void
2716 MingwIsoModuleHandler::GetNonModuleCdFiles (
2717 vector<FileLocation>& out ) const
2718 {
2719 for ( size_t i = 0; i < module.project.cdfiles.size (); i++ )
2720 {
2721 const CDFile& cdfile = *module.project.cdfiles[i];
2722 out.push_back ( *cdfile.source );
2723 }
2724 }
2725
2726 void
2727 MingwIsoModuleHandler::GetCdFiles (
2728 vector<FileLocation>& out ) const
2729 {
2730 GetBootstrapCdFiles ( out );
2731 GetNonModuleCdFiles ( out );
2732 }
2733
2734 void
2735 MingwIsoModuleHandler::GenerateIsoModuleTarget ()
2736 {
2737 fprintf ( fMakefile, "# ISO MODULE TARGET\n" );
2738 string bootcdDirectory = module.name;
2739 FileLocation bootcd ( OutputDirectory,
2740 bootcdDirectory,
2741 "" );
2742 FileLocation bootcdReactos ( OutputDirectory,
2743 bootcdDirectory + sSep + Environment::GetCdOutputPath (),
2744 "" );
2745
2746 // bootsector
2747 const Module* bootModule = module.bootSector->bootSectorModule;
2748
2749 if (!bootModule)
2750 {
2751 throw InvalidOperationException ( module.node.location.c_str(),
2752 0,
2753 "Invalid bootsector. module '%s' requires <bootsector>",
2754 module.name.c_str ());
2755 }
2756
2757 const FileLocation *isoboot = bootModule->output;
2758
2759 // prepare reactos.dff and reactos.inf
2760 FileLocation reactosDff ( SourceDirectory,
2761 "boot" + sSep + "bootdata" + sSep + "packages",
2762 "reactos.dff" );
2763 FileLocation reactosInf ( bootcdReactos.directory,
2764 bootcdReactos.relative_path,
2765 "reactos.inf" );
2766
2767 /*
2768 We use only the name and not full FileLocation(ouput) because Iso/LiveIso are an exception to the general rule.
2769 Iso/LiveIso outputs are generated in code base root
2770 */
2771 string IsoName = module.output->name;
2772
2773 fprintf ( fMakefile, ".PHONY: %s_CABINET\n\n",
2774 module.name.c_str () );
2775 fprintf ( fMakefile, "%s_CABINET: all $(cabman_TARGET) %s | %s\n",
2776 module.name.c_str (),
2777 backend->GetFullName ( reactosDff ).c_str (),
2778 backend->GetFullPath ( bootcdReactos ).c_str () );
2779 fprintf ( fMakefile,
2780 "\t$(Q)$(cabman_TARGET) -C %s -L %s -I -P $(OUTPUT)\n",
2781 backend->GetFullName ( reactosDff ).c_str (),
2782 backend->GetFullPath ( bootcdReactos ).c_str () );
2783 fprintf ( fMakefile,
2784 "\t$(Q)$(cabman_TARGET) -C %s -RC %s -L %s -N -P $(OUTPUT)\n",
2785 backend->GetFullName ( reactosDff ).c_str (),
2786 backend->GetFullName ( reactosInf ).c_str (),
2787 backend->GetFullPath ( bootcdReactos ).c_str ());
2788 fprintf ( fMakefile,
2789 "\t-@${rm} %s 2>$(NUL)\n\n",
2790 backend->GetFullName ( reactosInf ).c_str () );
2791
2792 std::vector<FileLocation> sourceFiles;
2793 OutputBootstrapfileCopyCommands ( bootcdDirectory, sourceFiles );
2794 OutputCdfileCopyCommands ( bootcdDirectory, sourceFiles );
2795
2796 fprintf( fMakefile,
2797 "\n%s_OBJS := %s\n\n",
2798 module.name.c_str (),
2799 v2s ( backend, sourceFiles, 5 ).c_str () );
2800
2801 fprintf ( fMakefile, ".PHONY: %s\n\n",
2802 module.name.c_str ());
2803 fprintf ( fMakefile,
2804 "%s: $(%s_OBJS) %s_CABINET %s $(cdmake_TARGET) | %s\n",
2805 module.name.c_str (),
2806 module.name.c_str (),
2807 module.name.c_str (),
2808 backend->GetFullName ( *isoboot ).c_str (),
2809 backend->GetFullPath ( FileLocation ( OutputDirectory,
2810 bootcdDirectory,
2811 "" ) ).c_str () );
2812
2813 fprintf ( fMakefile, "\t$(ECHO_CDMAKE)\n" );
2814 fprintf ( fMakefile,
2815 "\t$(Q)$(cdmake_TARGET) -v -j -m -b %s %s REACTOS %s\n",
2816 backend->GetFullName ( *isoboot ).c_str (),
2817 backend->GetFullPath ( bootcd ).c_str (),
2818 IsoName.c_str() );
2819 fprintf ( fMakefile,
2820 "\n" );
2821 }
2822
2823
2824 MingwLiveIsoModuleHandler::MingwLiveIsoModuleHandler (
2825 const Module& module_ )
2826
2827 : MingwModuleHandler ( module_ )
2828 {
2829 }
2830
2831 void
2832 MingwLiveIsoModuleHandler::Process ()
2833 {
2834 GenerateLiveIsoModuleTarget ();
2835 }
2836
2837 void
2838 MingwLiveIsoModuleHandler::CreateDirectory ( const string& directory )
2839 {
2840 FileLocation dir ( OutputDirectory,
2841 directory,
2842 "" );
2843 MingwModuleHandler::PassThruCacheDirectory ( &dir );
2844 }
2845
2846 void
2847 MingwLiveIsoModuleHandler::OutputModuleCopyCommands ( string& livecdDirectory,
2848 string& reactosDirectory,
2849 std::vector<FileLocation>& destinations )
2850 {
2851 for ( std::map<std::string, Module*>::const_iterator p = module.project.modules.begin (); p != module.project.modules.end (); ++ p )
2852 {
2853 const Module& m = *p->second;
2854 if ( !m.enabled )
2855 continue;
2856 if ( m.install )
2857 {
2858 const Module& aliasedModule = backend->GetAliasedModuleOrModule ( m );
2859 FileLocation destination ( OutputDirectory,
2860 m.install->relative_path.length () > 0
2861 ? livecdDirectory + sSep + reactosDirectory + sSep + m.install->relative_path
2862 : livecdDirectory + sSep + reactosDirectory,
2863 m.install->name );
2864 OutputCopyCommandSingle ( *aliasedModule.output,
2865 destination);
2866 destinations.push_back ( destination );
2867 }
2868 }
2869 }
2870
2871 void
2872 MingwLiveIsoModuleHandler::OutputNonModuleCopyCommands ( string& livecdDirectory,
2873 string& reactosDirectory,
2874 std::vector<FileLocation>& destinations )
2875 {
2876 for ( size_t i = 0; i < module.project.installfiles.size (); i++ )
2877 {
2878 const InstallFile& installfile = *module.project.installfiles[i];
2879 FileLocation target ( OutputDirectory,
2880 installfile.target->relative_path.length () > 0
2881 ? livecdDirectory + sSep + reactosDirectory + sSep + installfile.target->relative_path
2882 : livecdDirectory + sSep + reactosDirectory,
2883 installfile.target->name );
2884 OutputCopyCommandSingle ( *installfile.source, target );
2885 destinations.push_back ( target );
2886 }
2887 }
2888
2889 void
2890 MingwLiveIsoModuleHandler::OutputProfilesDirectoryCommands ( string& livecdDirectory,
2891 vector<FileLocation>& destinations )
2892 {
2893 CreateDirectory ( livecdDirectory + sSep + "Profiles" );
2894 CreateDirectory ( livecdDirectory + sSep + "Profiles" + sSep + "All Users") ;
2895 CreateDirectory ( livecdDirectory + sSep + "Profiles" + sSep + "All Users" + sSep + "Desktop" );
2896 CreateDirectory ( livecdDirectory + sSep + "Profiles" + sSep + "Default User" );
2897 CreateDirectory ( livecdDirectory + sSep + "Profiles" + sSep + "Default User" + sSep + "Desktop" );
2898 CreateDirectory ( livecdDirectory + sSep + "Profiles" + sSep + "Default User" + sSep + "My Documents" );
2899
2900 FileLocation livecdIni ( SourceDirectory,
2901 "boot" + sSep + "bootdata",
2902 "livecd.ini" );
2903 FileLocation destination ( OutputDirectory,
2904 livecdDirectory,
2905 "freeldr.ini" );
2906 OutputCopyCommandSingle ( livecdIni,
2907 destination );
2908 destinations.push_back ( destination );
2909 }
2910
2911 void
2912 MingwLiveIsoModuleHandler::OutputLoaderCommands ( string& livecdDirectory,
2913 std::vector<FileLocation>& destinations )
2914 {
2915 FileLocation freeldr ( OutputDirectory,
2916 "boot" + sSep + "freeldr" + sSep + "freeldr",
2917 "freeldr.sys" );
2918 FileLocation destination ( OutputDirectory,
2919 livecdDirectory + sSep + "loader",
2920 "setupldr.sys" );
2921 OutputCopyCommandSingle ( freeldr,
2922 destination );
2923 destinations.push_back ( destination );
2924 }
2925
2926 void
2927 MingwLiveIsoModuleHandler::OutputRegistryCommands ( string& livecdDirectory )
2928 {
2929 fprintf ( fMakefile, "# REGISTRY COMMANDS\n" );
2930 FileLocation reactosSystem32ConfigDirectory ( OutputDirectory,
2931 livecdDirectory + sSep + "reactos" + sSep + "system32" + sSep + "config",
2932 "" );
2933 fprintf ( fMakefile,
2934 "\t$(ECHO_MKHIVE)\n" );
2935 fprintf ( fMakefile,
2936 "\t$(mkhive_TARGET) boot%cbootdata %s $(ARCH) boot%cbootdata%clivecd.inf boot%cbootdata%chiveinst_$(ARCH).inf\n",
2937 cSep, backend->GetFullPath ( reactosSystem32ConfigDirectory ).c_str (),
2938 cSep, cSep, cSep, cSep );
2939 }
2940
2941 void
2942 MingwLiveIsoModuleHandler::GenerateLiveIsoModuleTarget ()
2943 {
2944 fprintf ( fMakefile, "# LIVE ISO MODULE TARGET\n" );
2945 string livecdDirectory = module.name;
2946 FileLocation livecd ( OutputDirectory, livecdDirectory, "" );
2947
2948 string IsoName;
2949
2950 // bootsector
2951 const Module* bootModule = module.bootSector->bootSectorModule;
2952
2953 if (!bootModule)
2954 {
2955 throw InvalidOperationException ( module.node.location.c_str(),
2956 0,
2957 "Invalid bootsector. module '%s' requires <bootsector>",
2958 module.name.c_str ());
2959 }
2960
2961 const FileLocation *isoboot = bootModule->output;
2962
2963 /*
2964 We use only the name and not full FileLocation(ouput) because Iso/LiveIso are an exception to the general rule.
2965 Iso/LiveIso outputs are generated in code base root
2966 */
2967 IsoName = module.output->name;
2968
2969 string reactosDirectory = "reactos";
2970 string livecdReactosNoFixup = livecdDirectory + sSep + reactosDirectory;
2971 FileLocation livecdReactos ( OutputDirectory,
2972 livecdReactosNoFixup,
2973 "" );
2974 CLEAN_FILE ( livecdReactos );
2975
2976 std::vector<FileLocation> sourceFiles;
2977 OutputModuleCopyCommands ( livecdDirectory,
2978 reactosDirectory,
2979 sourceFiles );
2980 OutputNonModuleCopyCommands ( livecdDirectory,
2981 reactosDirectory,
2982 sourceFiles );
2983 OutputProfilesDirectoryCommands ( livecdDirectory, sourceFiles );
2984 OutputLoaderCommands ( livecdDirectory, sourceFiles );
2985
2986 fprintf( fMakefile,
2987 "\n%s_OBJS := %s\n\n",
2988 module.name.c_str (),
2989 v2s ( backend, sourceFiles, 5 ).c_str () );
2990
2991 fprintf ( fMakefile, ".PHONY: %s\n\n",
2992 module.name.c_str ());
2993 fprintf ( fMakefile,
2994 "%s : $(%s_OBJS) %s %s $(mkhive_TARGET) $(cdmake_TARGET)\n",
2995 module.name.c_str (),
2996 module.name.c_str (),
2997 backend->GetFullName ( *isoboot) .c_str (),
2998 backend->GetFullPath ( FileLocation ( OutputDirectory,
2999 livecdDirectory,
3000 "" ) ).c_str () );
3001 OutputRegistryCommands ( livecdDirectory );
3002 fprintf ( fMakefile, "\t$(ECHO_CDMAKE)\n" );
3003 fprintf ( fMakefile,
3004 "\t$(Q)$(cdmake_TARGET) -v -m -j -b %s %s REACTOS %s\n",
3005 backend->GetFullName( *isoboot ).c_str (),
3006 backend->GetFullPath ( livecd ).c_str (),
3007 IsoName.c_str() );
3008 fprintf ( fMakefile,
3009 "\n" );
3010 }
3011
3012
3013 MingwTestModuleHandler::MingwTestModuleHandler (
3014 const Module& module_ )
3015
3016 : MingwModuleHandler ( module_ )
3017 {
3018 }
3019
3020 void
3021 MingwTestModuleHandler::Process ()
3022 {
3023 GenerateTestModuleTarget ();
3024 }
3025
3026 /* caller needs to delete the returned object */
3027 void
3028 MingwTestModuleHandler::GetModuleSpecificCompilationUnits ( vector<CompilationUnit*>& compilationUnits )
3029 {
3030 compilationUnits.push_back ( new CompilationUnit ( new File ( IntermediateDirectory, module.output->relative_path + sSep + "..", module.name + "_hooks.c", false, "", false ) ) );
3031 compilationUnits.push_back ( new CompilationUnit ( new File ( IntermediateDirectory, module.output->relative_path + sSep + "..", module.name + "_stubs.S", false, "", false ) ) );
3032 compilationUnits.push_back ( new CompilationUnit ( new File ( IntermediateDirectory, module.output->relative_path + sSep + "..", module.name + "_startup.c", false, "", false ) ) );
3033 }
3034
3035 void
3036 MingwTestModuleHandler::GenerateTestModuleTarget ()
3037 {
3038 GenerateRules ();
3039 GenerateLinkerCommand ();
3040 }
3041
3042
3043 MingwAliasModuleHandler::MingwAliasModuleHandler (
3044 const Module& module_ )
3045
3046 : MingwModuleHandler ( module_ )
3047 {
3048 }
3049
3050 void
3051 MingwAliasModuleHandler::Process ()
3052 {
3053 }
3054
3055
3056 MingwCabinetModuleHandler::MingwCabinetModuleHandler (
3057 const Module& module_ )
3058
3059 : MingwModuleHandler ( module_ )
3060 {
3061 }
3062
3063 void
3064 MingwCabinetModuleHandler::Process ()
3065 {
3066 fprintf ( fMakefile, "# CABINET MODULE TARGET\n" );
3067 string targetMacro ( GetTargetMacro (module) );
3068
3069 GenerateRules ();
3070
3071 const FileLocation *target_file = GetTargetFilename ( module, NULL );
3072 fprintf ( fMakefile, "%s: $(cabman_TARGET) | %s\n",
3073 targetMacro.c_str (),
3074 backend->GetFullPath ( *target_file ).c_str () );
3075
3076 fprintf ( fMakefile, "\t$(ECHO_CABMAN)\n" );
3077 fprintf ( fMakefile,
3078 "\t$(Q)$(cabman_TARGET) -M raw -S %s $(%s_SOURCES)\n", // Escape the asterisk for Make
3079 targetMacro.c_str (),
3080 module.name.c_str());
3081 }
3082
3083 MingwElfExecutableModuleHandler::MingwElfExecutableModuleHandler (
3084 const Module& module_ )
3085
3086 : MingwModuleHandler ( module_ )
3087 {
3088 }
3089
3090 void
3091 MingwElfExecutableModuleHandler::Process ()
3092 {
3093 string targetName ( module.output->name );
3094 string targetMacro ( GetTargetMacro (module) );
3095 string workingDirectory = GetWorkingDirectory ();
3096 string objectsMacro = GetObjectsMacro ( module );
3097 string libsMacro = GetLibsMacro ();
3098 string debugFormat = GetDebugFormat ();
3099
3100 fprintf ( fMakefile, "# ELF EXECUTABLE TARGET\n" );
3101 GenerateRules ();
3102
3103 const FileLocation *target_file = GetTargetFilename ( module, NULL );
3104 fprintf ( fMakefile, "%s: %s %s | %s\n",
3105 targetMacro.c_str (),
3106 objectsMacro.c_str (),
3107 libsMacro.c_str (),
3108 backend->GetFullPath ( *target_file ).c_str () );
3109
3110 fprintf ( fMakefile, "\t$(ECHO_BOOTPROG)\n" );
3111
3112 fprintf ( fMakefile, "\t${gcc} $(%s_LINKFORMAT) %s %s -g%s -o %s\n",
3113 module.buildtype.c_str(),
3114 objectsMacro.c_str(),
3115 libsMacro.c_str(),
3116 debugFormat.c_str(),
3117 targetMacro.c_str () );
3118
3119 delete target_file;
3120 fprintf ( fMakefile, "#/ELF EXECUTABLE TARGET\n" );
3121 }