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