generate clean rules
[reactos.git] / reactos / tools / rbuild / backend / mingw / modulehandler.cpp
1
2 #include "../../pch.h"
3 #include <assert.h>
4
5 #include "../../rbuild.h"
6 #include "mingw.h"
7 #include "modulehandler.h"
8
9 using std::string;
10 using std::vector;
11 using std::map;
12
13 map<ModuleType,MingwModuleHandler*>*
14 MingwModuleHandler::handler_map = NULL;
15 int
16 MingwModuleHandler::ref = 0;
17
18 FILE*
19 MingwModuleHandler::fMakefile = NULL;
20
21 MingwModuleHandler::MingwModuleHandler ( ModuleType moduletype )
22 {
23 if ( !ref++ )
24 handler_map = new map<ModuleType,MingwModuleHandler*>;
25 (*handler_map)[moduletype] = this;
26 }
27
28 MingwModuleHandler::~MingwModuleHandler()
29 {
30 if ( !--ref )
31 {
32 delete handler_map;
33 handler_map = NULL;
34 }
35 }
36
37 /*static*/ void
38 MingwModuleHandler::SetMakefile ( FILE* f )
39 {
40 fMakefile = f;
41 }
42
43 /*static*/ MingwModuleHandler*
44 MingwModuleHandler::LookupHandler ( const string& location,
45 ModuleType moduletype )
46 {
47 if ( !handler_map )
48 throw Exception ( "internal tool error: no registered module handlers" );
49 MingwModuleHandler* h = (*handler_map)[moduletype];
50 if ( !h )
51 {
52 throw UnknownModuleTypeException ( location, moduletype );
53 return NULL;
54 }
55 return h;
56 }
57
58 string
59 MingwModuleHandler::GetWorkingDirectory () const
60 {
61 return ".";
62 }
63
64 string
65 MingwModuleHandler::GetExtension ( const string& filename ) const
66 {
67 size_t index = filename.find_last_of ( '.' );
68 if (index != string::npos)
69 return filename.substr ( index );
70 return "";
71 }
72
73 string
74 MingwModuleHandler::ReplaceExtension ( const string& filename,
75 const string& newExtension ) const
76 {
77 size_t index = filename.find_last_of ( '.' );
78 if (index != string::npos)
79 return filename.substr ( 0, index ) + newExtension;
80 return filename;
81 }
82
83 string
84 MingwModuleHandler::GetModuleArchiveFilename ( const Module& module ) const
85 {
86 return ReplaceExtension ( FixupTargetFilename ( module.GetPath () ).c_str (),
87 ".a" );
88 }
89
90 string
91 MingwModuleHandler::GetImportLibraryDependencies ( const Module& module ) const
92 {
93 if ( module.libraries.size () == 0 )
94 return "";
95
96 string dependencies ( "" );
97 for ( size_t i = 0; i < module.libraries.size (); i++ )
98 {
99 if ( dependencies.size () > 0 )
100 dependencies += " ";
101 const Module* importedModule = module.project.LocateModule ( module.libraries[i]->name );
102 assert ( importedModule != NULL );
103 dependencies += FixupTargetFilename ( importedModule->GetDependencyPath () ).c_str ();
104 }
105 return dependencies;
106 }
107
108 string
109 MingwModuleHandler::GetModuleDependencies ( const Module& module ) const
110 {
111 if ( module.dependencies.size () == 0 )
112 return "";
113
114 string dependencies ( "" );
115 for ( size_t i = 0; i < module.dependencies.size (); i++ )
116 {
117 if ( dependencies.size () > 0 )
118 dependencies += " ";
119 const Dependency* dependency = module.dependencies[i];
120 const Module* dependencyModule = dependency->dependencyModule;
121 dependencies += dependencyModule->GetTargets ();
122 }
123 return dependencies;
124 }
125
126 string
127 MingwModuleHandler::GetAllDependencies ( const Module& module ) const
128 {
129 string dependencies = GetImportLibraryDependencies ( module );
130 string s = GetModuleDependencies ( module );
131 if (s.length () > 0)
132 {
133 dependencies += " ";
134 dependencies += s;
135 }
136 return dependencies;
137 }
138
139 string
140 MingwModuleHandler::GetSourceFilenames ( const Module& module ) const
141 {
142 size_t i;
143
144 string sourceFilenames ( "" );
145 for ( i = 0; i < module.files.size (); i++ )
146 sourceFilenames += " " + module.files[i]->name;
147 vector<If*> ifs = module.ifs;
148 for ( i = 0; i < ifs.size(); i++ )
149 {
150 size_t j;
151 If& rIf = *ifs[i];
152 for ( j = 0; j < rIf.ifs.size(); j++ )
153 ifs.push_back ( rIf.ifs[j] );
154 for ( j = 0; j < rIf.files.size(); j++ )
155 sourceFilenames += " " + rIf.files[j]->name;
156 }
157 return sourceFilenames;
158 }
159
160 string
161 MingwModuleHandler::GetObjectFilename ( const string& sourceFilename ) const
162 {
163 return FixupTargetFilename ( ReplaceExtension ( sourceFilename,
164 ".o" ) );
165 }
166
167 string
168 MingwModuleHandler::GetObjectFilenames ( const Module& module ) const
169 {
170 if ( module.files.size () == 0 )
171 return "";
172
173 string objectFilenames ( "" );
174 for ( size_t i = 0; i < module.files.size (); i++ )
175 {
176 if ( objectFilenames.size () > 0 )
177 objectFilenames += " ";
178 objectFilenames += GetObjectFilename ( module.files[i]->name );
179 }
180 return objectFilenames;
181 }
182
183 string
184 MingwModuleHandler::GenerateGccDefineParametersFromVector ( const vector<Define*>& defines ) const
185 {
186 string parameters;
187 for (size_t i = 0; i < defines.size (); i++)
188 {
189 Define& define = *defines[i];
190 if (parameters.length () > 0)
191 parameters += " ";
192 parameters += "-D";
193 parameters += define.name;
194 if (define.value.length () > 0)
195 {
196 parameters += "=";
197 parameters += define.value;
198 }
199 }
200 return parameters;
201 }
202
203 string
204 MingwModuleHandler::GenerateGccDefineParameters ( const Module& module ) const
205 {
206 string parameters = GenerateGccDefineParametersFromVector ( module.project.defines );
207 string s = GenerateGccDefineParametersFromVector ( module.defines );
208 if (s.length () > 0)
209 {
210 parameters += " ";
211 parameters += s;
212 }
213 return parameters;
214 }
215
216 string
217 MingwModuleHandler::ConcatenatePaths ( const string& path1,
218 const string& path2 ) const
219 {
220 if ( ( path1.length () == 0 ) || ( path1 == "." ) || ( path1 == "./" ) )
221 return path2;
222 if ( path1[path1.length ()] == CSEP )
223 return path1 + path2;
224 else
225 return path1 + CSEP + path2;
226 }
227
228 string
229 MingwModuleHandler::GenerateGccIncludeParametersFromVector ( const vector<Include*>& includes ) const
230 {
231 string parameters;
232 for ( size_t i = 0; i < includes.size (); i++ )
233 {
234 Include& include = *includes[i];
235 if (parameters.length () > 0)
236 parameters += " ";
237 parameters += "-I" + include.directory;
238 }
239 return parameters;
240 }
241
242 void
243 MingwModuleHandler::GenerateGccModuleIncludeVariable ( const Module& module ) const
244 {
245 #if 0
246 string name ( module.name + "_CFLAGS" );
247 fprintf ( fMakefile,
248 "%s := %s %s\n",
249 name.c_str(),
250 GenerateGccDefineParameters(module).c_str(),
251 GenerateGccIncludeParameters(module).c_str() );
252 #endif
253 }
254
255 string
256 MingwModuleHandler::GenerateGccIncludeParameters ( const Module& module ) const
257 {
258 string parameters = GenerateGccIncludeParametersFromVector ( module.includes );
259 string s = GenerateGccIncludeParametersFromVector ( module.project.includes );
260 if ( s.length () > 0 )
261 {
262 parameters += " ";
263 parameters += s;
264 }
265 return parameters;
266 }
267
268 void
269 MingwModuleHandler::GenerateMacros (
270 const char* op,
271 const vector<File*>& files,
272 const vector<Include*>& includes,
273 const vector<Define*>& defines,
274 const vector<If*>& ifs,
275 const string& cflags_macro,
276 const string& nasmflags_macro,
277 const string& objs_macro) const
278 {
279 size_t i;
280
281 if ( includes.size() || defines.size() )
282 {
283 fprintf (
284 fMakefile,
285 "%s %s",
286 cflags_macro.c_str(),
287 op );
288 for ( i = 0; i < includes.size(); i++ )
289 {
290 fprintf (
291 fMakefile,
292 " -I%s",
293 includes[i]->directory.c_str() );
294 }
295 for ( i = 0; i < defines.size(); i++ )
296 {
297 Define& d = *defines[i];
298 fprintf (
299 fMakefile,
300 " -D%s",
301 d.name.c_str() );
302 if ( d.value.size() )
303 fprintf (
304 fMakefile,
305 "=%s",
306 d.value.c_str() );
307 }
308 fprintf ( fMakefile, "\n" );
309 }
310
311 if ( files.size() )
312 {
313 fprintf (
314 fMakefile,
315 "%s %s",
316 objs_macro.c_str(),
317 op );
318 for ( i = 0; i < files.size(); i++ )
319 {
320 fprintf (
321 fMakefile,
322 "%s%s",
323 ( i%10 == 9 ? "\\\n\t" : " " ),
324 GetObjectFilename(files[i]->name).c_str() );
325 }
326 fprintf ( fMakefile, "\n" );
327 }
328
329 for ( i = 0; i < ifs.size(); i++ )
330 {
331 If& rIf = *ifs[i];
332 if ( rIf.defines.size() || rIf.includes.size() || rIf.files.size() || rIf.ifs.size() )
333 {
334 fprintf (
335 fMakefile,
336 "ifeq (\"$(%s)\",\"%s\")\n",
337 rIf.property.c_str(),
338 rIf.value.c_str() );
339 GenerateMacros (
340 "+=",
341 rIf.files,
342 rIf.includes,
343 rIf.defines,
344 rIf.ifs,
345 cflags_macro,
346 nasmflags_macro,
347 objs_macro );
348 fprintf (
349 fMakefile,
350 "endif\n\n" );
351 }
352 }
353 }
354
355 void
356 MingwModuleHandler::GenerateMacros (
357 const Module& module,
358 const string& cflags_macro,
359 const string& nasmflags_macro,
360 const string& objs_macro) const
361 {
362 GenerateMacros (
363 "=",
364 module.files,
365 module.includes,
366 module.defines,
367 module.ifs,
368 cflags_macro,
369 nasmflags_macro,
370 objs_macro );
371 fprintf ( fMakefile, "\n" );
372
373 fprintf (
374 fMakefile,
375 "%s += $(PROJECT_CFLAGS)\n\n",
376 cflags_macro.c_str () );
377 }
378
379 string
380 MingwModuleHandler::GenerateGccCommand ( const Module& module,
381 const string& sourceFilename,
382 const string& cc,
383 const string& cflagsMacro ) const
384 {
385 string objectFilename = GetObjectFilename ( sourceFilename );
386 return ssprintf ( "%s -c %s -o %s %s\n",
387 cc.c_str (),
388 sourceFilename.c_str (),
389 objectFilename.c_str (),
390 cflagsMacro.c_str () );
391 }
392
393 string
394 MingwModuleHandler::GenerateGccAssemblerCommand ( const Module& module,
395 const string& sourceFilename,
396 const string& cc,
397 const string& cflagsMacro ) const
398 {
399 string objectFilename = GetObjectFilename ( sourceFilename );
400 return ssprintf ( "%s -x assembler-with-cpp -c %s -o %s -D__ASM__ %s\n",
401 cc.c_str (),
402 sourceFilename.c_str (),
403 objectFilename.c_str (),
404 cflagsMacro.c_str () );
405 }
406
407 string
408 MingwModuleHandler::GenerateNasmCommand ( const Module& module,
409 const string& sourceFilename,
410 const string& nasmflagsMacro ) const
411 {
412 string objectFilename = GetObjectFilename ( sourceFilename );
413 return ssprintf ( "%s -f win32 %s -o %s %s\n",
414 "nasm",
415 sourceFilename.c_str (),
416 objectFilename.c_str (),
417 nasmflagsMacro.c_str () );
418 }
419
420 string
421 MingwModuleHandler::GenerateCommand ( const Module& module,
422 const string& sourceFilename,
423 const string& cc,
424 const string& cflagsMacro,
425 const string& nasmflagsMacro ) const
426 {
427 string extension = GetExtension ( sourceFilename );
428 if ( extension == ".c" || extension == ".C" )
429 return GenerateGccCommand ( module,
430 sourceFilename,
431 cc,
432 cflagsMacro );
433 else if ( extension == ".s" || extension == ".S" )
434 return GenerateGccAssemblerCommand ( module,
435 sourceFilename,
436 cc,
437 cflagsMacro );
438 else if ( extension == ".asm" || extension == ".ASM" )
439 return GenerateNasmCommand ( module,
440 sourceFilename,
441 nasmflagsMacro );
442
443 throw InvalidOperationException ( __FILE__,
444 __LINE__,
445 "Unsupported filename extension '%s' in file '%s'",
446 extension.c_str (),
447 sourceFilename.c_str () );
448 }
449
450 void
451 MingwModuleHandler::GenerateObjectFileTargets ( const Module& module,
452 const vector<File*>& files,
453 const vector<If*>& ifs,
454 const string& cc,
455 const string& cflagsMacro,
456 const string& nasmflagsMacro ) const
457 {
458 size_t i;
459
460 for ( i = 0; i < files.size (); i++ )
461 {
462 string sourceFilename = files[i]->name;
463 string objectFilename = GetObjectFilename ( sourceFilename );
464 fprintf ( fMakefile,
465 "%s: %s\n",
466 objectFilename.c_str (),
467 sourceFilename.c_str () );
468 fprintf ( fMakefile,
469 "\t%s\n",
470 GenerateCommand ( module,
471 sourceFilename,
472 cc,
473 cflagsMacro,
474 nasmflagsMacro ).c_str () );
475 }
476
477 for ( i = 0; i < ifs.size(); i++ )
478 GenerateObjectFileTargets ( module, ifs[i]->files, ifs[i]->ifs, cc, cflagsMacro, nasmflagsMacro );
479 }
480
481 void
482 MingwModuleHandler::GenerateObjectFileTargets ( const Module& module,
483 const string& cc,
484 const string& cflagsMacro,
485 const string& nasmflagsMacro ) const
486 {
487 GenerateObjectFileTargets ( module, module.files, module.ifs, cc, cflagsMacro, nasmflagsMacro );
488 fprintf ( fMakefile, "\n" );
489 }
490
491 void
492 MingwModuleHandler::GetCleanTargets ( vector<string>& out,
493 const vector<File*>& files,
494 const vector<If*>& ifs ) const
495 {
496 size_t i;
497
498 for ( i = 0; i < files.size(); i++ )
499 out.push_back ( GetObjectFilename(files[i]->name) );
500
501 for ( i = 0; i < ifs.size(); i++ )
502 GetCleanTargets ( out, ifs[i]->files, ifs[i]->ifs );
503 }
504
505 string
506 MingwModuleHandler::GenerateArchiveTarget ( const Module& module,
507 const string& ar,
508 const string& objs_macro ) const
509 {
510 string archiveFilename = GetModuleArchiveFilename ( module );
511
512 fprintf ( fMakefile,
513 "%s: %s\n",
514 archiveFilename.c_str (),
515 objs_macro.c_str ());
516
517 fprintf ( fMakefile,
518 "\t%s -rc %s %s\n\n",
519 ar.c_str (),
520 archiveFilename.c_str (),
521 objs_macro.c_str ());
522
523 return archiveFilename;
524 }
525
526 void
527 MingwModuleHandler::GenerateMacrosAndTargets (
528 const Module& module,
529 const string& cc,
530 const string& ar ) const
531 {
532 string cflagsMacro = ssprintf("%s_CFLAGS",module.name.c_str());
533 string nasmflagsMacro = ssprintf("%s_NASMFLAGS",module.name.c_str());
534 string objectsMacro = ssprintf("%s_OBJS",module.name.c_str());
535
536 GenerateMacros ( module, cflagsMacro, nasmflagsMacro, objectsMacro );
537
538 // generate phony target for module name
539 fprintf ( fMakefile, ".PHONY: %s\n",
540 module.name.c_str() );
541 fprintf ( fMakefile, "%s: %s\n\n",
542 module.name.c_str(),
543 module.GetPath().c_str() );
544
545 // future references to the macros will be to get their values
546 cflagsMacro = ssprintf("$(%s)",cflagsMacro.c_str());
547 nasmflagsMacro = ssprintf("$(%s)",nasmflagsMacro.c_str());
548 objectsMacro = ssprintf("$(%s)",objectsMacro.c_str());
549
550 string ar_target = GenerateArchiveTarget ( module, ar, objectsMacro );
551 GenerateObjectFileTargets ( module, cc, cflagsMacro, nasmflagsMacro );
552
553 vector<string> clean_files;
554 clean_files.push_back ( ar_target );
555 GetCleanTargets ( clean_files, module.files, module.ifs );
556
557 fprintf ( fMakefile, "clean::\n\t-@$(rm)" );
558 for ( size_t i = 0; i < clean_files.size(); i++ )
559 {
560 if ( 9==(i%10) )
561 fprintf ( fMakefile, " 2>NUL\n\t-@$(rm)" );
562 fprintf ( fMakefile, " %s", clean_files[i].c_str() );
563 }
564 fprintf ( fMakefile, " 2>NUL\n\n" );
565 }
566
567 void
568 MingwModuleHandler::GenerateMacrosAndTargetsHost ( const Module& module ) const
569 {
570 GenerateMacrosAndTargets ( module, "${host_gcc}", "${host_ar}" );
571 }
572
573 void
574 MingwModuleHandler::GenerateMacrosAndTargetsTarget ( const Module& module ) const
575 {
576 GenerateMacrosAndTargets ( module, "${gcc}", "${ar}" );
577 }
578
579 string
580 MingwModuleHandler::GetInvocationDependencies ( const Module& module ) const
581 {
582 string dependencies;
583 for ( size_t i = 0; i < module.invocations.size (); i++ )
584 {
585 Invoke& invoke = *module.invocations[i];
586 if (invoke.invokeModule == &module)
587 /* Protect against circular dependencies */
588 continue;
589 if ( dependencies.length () > 0 )
590 dependencies += " ";
591 dependencies += invoke.GetTargets ();
592 }
593 return dependencies;
594 }
595
596 string
597 MingwModuleHandler::GetInvocationParameters ( const Invoke& invoke ) const
598 {
599 string parameters ( "" );
600 size_t i;
601 for (i = 0; i < invoke.output.size (); i++)
602 {
603 if (parameters.length () > 0)
604 parameters += " ";
605 InvokeFile& invokeFile = *invoke.output[i];
606 if (invokeFile.switches.length () > 0)
607 {
608 parameters += invokeFile.switches;
609 parameters += " ";
610 }
611 parameters += invokeFile.name;
612 }
613
614 for (i = 0; i < invoke.input.size (); i++)
615 {
616 if (parameters.length () > 0)
617 parameters += " ";
618 InvokeFile& invokeFile = *invoke.input[i];
619 if (invokeFile.switches.length () > 0)
620 {
621 parameters += invokeFile.switches;
622 parameters += " ";
623 }
624 parameters += invokeFile.name;
625 }
626
627 return parameters;
628 }
629
630 void
631 MingwModuleHandler::GenerateInvocations ( const Module& module ) const
632 {
633 if ( module.invocations.size () == 0 )
634 return;
635
636 for ( size_t i = 0; i < module.invocations.size (); i++ )
637 {
638 const Invoke& invoke = *module.invocations[i];
639
640 if ( invoke.invokeModule->type != BuildTool )
641 throw InvalidBuildFileException ( module.node.location,
642 "Only modules of type buildtool can be invoked." );
643
644 string invokeTarget = module.GetInvocationTarget ( i );
645 fprintf ( fMakefile,
646 "%s: %s\n\n",
647 invoke.GetTargets ().c_str (),
648 invokeTarget.c_str () );
649 fprintf ( fMakefile,
650 "%s: %s\n",
651 invokeTarget.c_str (),
652 FixupTargetFilename ( invoke.invokeModule->GetPath () ).c_str () );
653 fprintf ( fMakefile,
654 "\t%s %s\n\n",
655 FixupTargetFilename ( invoke.invokeModule->GetPath () ).c_str (),
656 GetInvocationParameters ( invoke ).c_str () );
657 fprintf ( fMakefile,
658 ".PNONY: %s\n\n",
659 invokeTarget.c_str () );
660 }
661 }
662
663 string
664 MingwModuleHandler::GetPreconditionDependenciesName ( const Module& module ) const
665 {
666 return ssprintf ( "%s_precondition",
667 module.name.c_str () );
668 }
669
670 void
671 MingwModuleHandler::GeneratePreconditionDependencies ( const Module& module ) const
672 {
673 string preconditionDependenciesName = GetPreconditionDependenciesName ( module );
674 string sourceFilenames = GetSourceFilenames ( module );
675 string dependencies = GetModuleDependencies ( module );
676 string s = GetInvocationDependencies ( module );
677 if ( s.length () > 0 )
678 {
679 if ( dependencies.length () > 0 )
680 dependencies += " ";
681 dependencies += s;
682 }
683
684 fprintf ( fMakefile,
685 ".PHONY: %s\n\n",
686 preconditionDependenciesName.c_str () );
687 fprintf ( fMakefile,
688 "%s: %s\n\n",
689 preconditionDependenciesName.c_str (),
690 dependencies.c_str () );
691 const char* p = sourceFilenames.c_str();
692 const char* end = p + strlen(p);
693 while ( p < end )
694 {
695 const char* p2 = &p[512];
696 if ( p2 > end )
697 p2 = end;
698 while ( p2 > p && !isspace(*p2) )
699 --p2;
700 if ( p == p2 )
701 {
702 p2 = strpbrk ( p, " \t" );
703 if ( !p2 )
704 p2 = end;
705 }
706 fprintf ( fMakefile,
707 "%.*s: %s\n",
708 p2-p,
709 p,
710 preconditionDependenciesName.c_str ());
711 p = p2;
712 p += strspn ( p, " \t" );
713 }
714 fprintf ( fMakefile, "\n" );
715 }
716
717
718 static MingwBuildToolModuleHandler buildtool_handler;
719
720 MingwBuildToolModuleHandler::MingwBuildToolModuleHandler()
721 : MingwModuleHandler ( BuildTool )
722 {
723 }
724
725 void
726 MingwBuildToolModuleHandler::Process ( const Module& module )
727 {
728 GeneratePreconditionDependencies ( module );
729 GenerateBuildToolModuleTarget ( module );
730 GenerateInvocations ( module );
731 }
732
733 void
734 MingwBuildToolModuleHandler::GenerateBuildToolModuleTarget ( const Module& module )
735 {
736 string target ( FixupTargetFilename ( module.GetPath () ) );
737 string archiveFilename = GetModuleArchiveFilename ( module );
738
739 GenerateMacrosAndTargetsHost ( module );
740
741 fprintf ( fMakefile, "%s: %s\n",
742 target.c_str (),
743 archiveFilename.c_str () );
744 fprintf ( fMakefile,
745 "\t${host_gcc} -o %s %s\n\n",
746 target.c_str (),
747 archiveFilename.c_str () );
748 }
749
750 static MingwKernelModuleHandler kernelmodule_handler;
751
752 MingwKernelModuleHandler::MingwKernelModuleHandler ()
753 : MingwModuleHandler ( Kernel )
754 {
755 }
756
757 void
758 MingwKernelModuleHandler::Process ( const Module& module )
759 {
760 GeneratePreconditionDependencies ( module );
761 GenerateKernelModuleTarget ( module );
762 GenerateInvocations ( module );
763 }
764
765 void
766 MingwKernelModuleHandler::GenerateKernelModuleTarget ( const Module& module )
767 {
768 static string ros_junk ( "$(ROS_TEMPORARY)" );
769 //static string ros_output ( "$(ROS_INTERMEDIATE)" );
770 string target ( FixupTargetFilename(module.GetPath()) );
771 string workingDirectory = GetWorkingDirectory ( );
772 string archiveFilename = GetModuleArchiveFilename ( module );
773 string importLibraryDependencies = GetImportLibraryDependencies ( module );
774 string base_tmp = ros_junk + module.name + ".base.tmp";
775 string junk_tmp = ros_junk + module.name + ".junk.tmp";
776 string temp_exp = ros_junk + module.name + ".temp.exp";
777 string gccOptions = ssprintf ("-Wl,-T,%s" SSEP "ntoskrnl.lnk -Wl,--subsystem,native -Wl,--entry,_NtProcessStartup -Wl,--image-base,0xC0000000 -Wl,--file-alignment,0x1000 -Wl,--section-alignment,0x1000 -nostartfiles -mdll",
778 module.GetBasePath ().c_str () );
779
780 GenerateMacrosAndTargetsTarget ( module );
781
782 fprintf ( fMakefile, "%s: %s %s\n",
783 target.c_str (),
784 archiveFilename.c_str (),
785 importLibraryDependencies.c_str () );
786 fprintf ( fMakefile,
787 "\t${gcc} %s -Wl,--base-file,%s -o %s %s %s\n",
788 gccOptions.c_str (),
789 base_tmp.c_str (),
790 junk_tmp.c_str (),
791 archiveFilename.c_str (),
792 importLibraryDependencies.c_str () );
793 fprintf ( fMakefile,
794 "\t${rm} %s\n",
795 junk_tmp.c_str () );
796 fprintf ( fMakefile,
797 "\t${dlltool} --dllname %s --base-file %s --output-exp %s --kill-at\n",
798 target.c_str (),
799 base_tmp.c_str (),
800 temp_exp.c_str ());
801 fprintf ( fMakefile,
802 "\t${rm} %s\n",
803 base_tmp.c_str () );
804 fprintf ( fMakefile,
805 "\t${gcc} %s -Wl,%s -o %s %s %s\n",
806 gccOptions.c_str (),
807 temp_exp.c_str (),
808 target.c_str (),
809 archiveFilename.c_str (),
810 importLibraryDependencies.c_str () );
811 fprintf ( fMakefile,
812 "\t${rm} %s\n\n",
813 temp_exp.c_str () );
814 }
815
816
817 static MingwStaticLibraryModuleHandler staticlibrary_handler;
818
819 MingwStaticLibraryModuleHandler::MingwStaticLibraryModuleHandler ()
820 : MingwModuleHandler ( StaticLibrary )
821 {
822 }
823
824 void
825 MingwStaticLibraryModuleHandler::Process ( const Module& module )
826 {
827 GeneratePreconditionDependencies ( module );
828 GenerateStaticLibraryModuleTarget ( module );
829 GenerateInvocations ( module );
830 }
831
832 void
833 MingwStaticLibraryModuleHandler::GenerateStaticLibraryModuleTarget ( const Module& module )
834 {
835 GenerateMacrosAndTargetsTarget ( module );
836 }
837
838
839 static MingwKernelModeDLLModuleHandler kernelmodedll_handler;
840
841 MingwKernelModeDLLModuleHandler::MingwKernelModeDLLModuleHandler ()
842 : MingwModuleHandler ( KernelModeDLL )
843 {
844 }
845
846 void
847 MingwKernelModeDLLModuleHandler::Process ( const Module& module )
848 {
849 GeneratePreconditionDependencies ( module );
850 GenerateKernelModeDLLModuleTarget ( module );
851 GenerateInvocations ( module );
852 }
853
854 void
855 MingwKernelModeDLLModuleHandler::GenerateKernelModeDLLModuleTarget ( const Module& module )
856 {
857 static string ros_junk ( "$(ROS_TEMPORARY)" );
858 string target ( FixupTargetFilename ( module.GetPath () ) );
859 string workingDirectory = GetWorkingDirectory ( );
860 string archiveFilename = GetModuleArchiveFilename ( module );
861 string importLibraryDependencies = GetImportLibraryDependencies ( module );
862
863 if (module.importLibrary != NULL)
864 {
865 fprintf ( fMakefile, "%s:\n",
866 module.GetDependencyPath ().c_str () );
867
868 fprintf ( fMakefile,
869 "\t${dlltool} --dllname %s --def %s --output-lib %s --kill-at\n\n",
870 module.GetTargetName ().c_str (),
871 FixupTargetFilename ( module.GetBasePath () + SSEP + module.importLibrary->definition ).c_str (),
872 FixupTargetFilename ( module.GetDependencyPath () ).c_str () );
873 }
874
875 if (module.files.size () > 0)
876 {
877 GenerateMacrosAndTargetsTarget ( module );
878
879 fprintf ( fMakefile, "%s: %s %s\n",
880 target.c_str (),
881 archiveFilename.c_str (),
882 importLibraryDependencies.c_str () );
883
884 fprintf ( fMakefile,
885 "\t${gcc} -Wl,--subsystem,native -Wl,--entry,_DriverEntry@8 -Wl,--image-base,0x10000 -Wl,--file-alignment,0x1000 -Wl,--section-alignment,0x1000 -nostartfiles -mdll -o %s %s %s\n\n",
886 target.c_str (),
887 archiveFilename.c_str (),
888 importLibraryDependencies.c_str () );
889 }
890 else
891 {
892 fprintf ( fMakefile, "%s:\n",
893 target.c_str ());
894 fprintf ( fMakefile, ".PHONY: %s\n\n",
895 target.c_str ());
896 }
897 }
898
899
900 static MingwNativeDLLModuleHandler nativedll_handler;
901
902 MingwNativeDLLModuleHandler::MingwNativeDLLModuleHandler ()
903 : MingwModuleHandler ( NativeDLL )
904 {
905 }
906
907 void
908 MingwNativeDLLModuleHandler::Process ( const Module& module )
909 {
910 GeneratePreconditionDependencies ( module );
911 GenerateNativeDLLModuleTarget ( module );
912 GenerateInvocations ( module );
913 }
914
915 void
916 MingwNativeDLLModuleHandler::GenerateNativeDLLModuleTarget ( const Module& module )
917 {
918 static string ros_junk ( "$(ROS_TEMPORARY)" );
919 string target ( FixupTargetFilename ( module.GetPath () ) );
920 string workingDirectory = GetWorkingDirectory ( );
921 string archiveFilename = GetModuleArchiveFilename ( module );
922 string importLibraryDependencies = GetImportLibraryDependencies ( module );
923
924 if (module.importLibrary != NULL)
925 {
926 fprintf ( fMakefile, "%s:\n",
927 module.GetDependencyPath ().c_str () );
928
929 fprintf ( fMakefile,
930 "\t${dlltool} --dllname %s --def %s --output-lib %s --kill-at\n\n",
931 module.GetTargetName ().c_str (),
932 FixupTargetFilename ( module.GetBasePath () + SSEP + module.importLibrary->definition ).c_str (),
933 FixupTargetFilename ( module.GetDependencyPath () ).c_str () );
934 }
935
936 if (module.files.size () > 0)
937 {
938 GenerateMacrosAndTargetsTarget ( module );
939
940 fprintf ( fMakefile, "%s: %s %s\n",
941 target.c_str (),
942 archiveFilename.c_str (),
943 importLibraryDependencies.c_str () );
944
945 fprintf ( fMakefile,
946 "\t${gcc} -Wl,--subsystem,native -Wl,--entry,_DllMainCRTStartup@12 -Wl,--image-base,0x10000 -Wl,--file-alignment,0x1000 -Wl,--section-alignment,0x1000 -nostartfiles -nostdlib -mdll -o %s %s %s\n\n",
947 target.c_str (),
948 archiveFilename.c_str (),
949 importLibraryDependencies.c_str () );
950 }
951 else
952 {
953 fprintf ( fMakefile, "%s:\n\n",
954 target.c_str ());
955 fprintf ( fMakefile, ".PHONY: %s\n\n",
956 target.c_str ());
957 }
958 }
959
960
961 static MingwWin32DLLModuleHandler win32dll_handler;
962
963 MingwWin32DLLModuleHandler::MingwWin32DLLModuleHandler ()
964 : MingwModuleHandler ( Win32DLL )
965 {
966 }
967
968 void
969 MingwWin32DLLModuleHandler::Process ( const Module& module )
970 {
971 GeneratePreconditionDependencies ( module );
972 GenerateWin32DLLModuleTarget ( module );
973 GenerateInvocations ( module );
974 }
975
976 void
977 MingwWin32DLLModuleHandler::GenerateWin32DLLModuleTarget ( const Module& module )
978 {
979 static string ros_junk ( "$(ROS_TEMPORARY)" );
980 string target ( FixupTargetFilename ( module.GetPath () ) );
981 string workingDirectory = GetWorkingDirectory ( );
982 string archiveFilename = GetModuleArchiveFilename ( module );
983 string importLibraryDependencies = GetImportLibraryDependencies ( module );
984
985 if (module.importLibrary != NULL)
986 {
987 fprintf ( fMakefile, "%s:\n",
988 module.GetDependencyPath ().c_str () );
989
990 fprintf ( fMakefile,
991 "\t${dlltool} --dllname %s --def %s --output-lib %s --kill-at\n\n",
992 module.GetTargetName ().c_str (),
993 FixupTargetFilename ( module.GetBasePath () + SSEP + module.importLibrary->definition ).c_str (),
994 FixupTargetFilename ( module.GetDependencyPath () ).c_str () );
995 }
996
997 if (module.files.size () > 0)
998 {
999 GenerateMacrosAndTargetsTarget ( module );
1000
1001 fprintf ( fMakefile, "%s: %s %s\n",
1002 target.c_str (),
1003 archiveFilename.c_str (),
1004 importLibraryDependencies.c_str () );
1005
1006 fprintf ( fMakefile,
1007 "\t${gcc} -Wl,--subsystem,console -Wl,--entry,_DllMain@12 -Wl,--image-base,0x10000 -Wl,--file-alignment,0x1000 -Wl,--section-alignment,0x1000 -nostartfiles -nostdlib -mdll -o %s %s %s\n",
1008 target.c_str (),
1009 archiveFilename.c_str (),
1010 importLibraryDependencies.c_str () );
1011 }
1012 else
1013 {
1014 fprintf ( fMakefile, "%s:\n\n",
1015 target.c_str ());
1016 fprintf ( fMakefile, ".PHONY: %s\n\n",
1017 target.c_str ());
1018 }
1019 }