c8d482ca3abe873a30800e46367ccd7397c71613
[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 string& cc,
449 const string& cflagsMacro,
450 const string& nasmflagsMacro ) const
451 {
452 if ( files.size () == 0 )
453 return;
454
455 for ( size_t 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 fprintf ( fMakefile, "\n" );
473 }
474
475 void
476 MingwModuleHandler::GenerateObjectFileTargets ( const Module& module,
477 const string& cc,
478 const string& cflagsMacro,
479 const string& nasmflagsMacro ) const
480 {
481 GenerateObjectFileTargets ( module, module.files, cc, cflagsMacro, nasmflagsMacro );
482 for ( size_t i = 0; i < module.ifs.size(); i++ )
483 GenerateObjectFileTargets ( module, module.ifs[i]->files, cc, cflagsMacro, nasmflagsMacro );
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 fprintf ( fMakefile, "%s: %s\n",
699 target.c_str (),
700 archiveFilename.c_str () );
701 fprintf ( fMakefile,
702 "\t${host_gcc} -o %s %s\n\n",
703 target.c_str (),
704 archiveFilename.c_str () );
705 GenerateMacrosAndTargetsHost ( module );
706 }
707
708 static MingwKernelModuleHandler kernelmodule_handler;
709
710 MingwKernelModuleHandler::MingwKernelModuleHandler ()
711 : MingwModuleHandler ( Kernel )
712 {
713 }
714
715 void
716 MingwKernelModuleHandler::Process ( const Module& module )
717 {
718 GeneratePreconditionDependencies ( module );
719 GenerateKernelModuleTarget ( module );
720 GenerateInvocations ( module );
721 }
722
723 void
724 MingwKernelModuleHandler::GenerateKernelModuleTarget ( const Module& module )
725 {
726 static string ros_junk ( "$(ROS_TEMPORARY)" );
727 //static string ros_output ( "$(ROS_INTERMEDIATE)" );
728 string target ( FixupTargetFilename(module.GetPath()) );
729 string workingDirectory = GetWorkingDirectory ( );
730 string archiveFilename = GetModuleArchiveFilename ( module );
731 string importLibraryDependencies = GetImportLibraryDependencies ( module );
732 string base_tmp = ros_junk + module.name + ".base.tmp";
733 string junk_tmp = ros_junk + module.name + ".junk.tmp";
734 string temp_exp = ros_junk + module.name + ".temp.exp";
735 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",
736 module.GetBasePath ().c_str () );
737 fprintf ( fMakefile, "%s: %s %s\n",
738 target.c_str (),
739 archiveFilename.c_str (),
740 importLibraryDependencies.c_str () );
741 fprintf ( fMakefile,
742 "\t${gcc} %s -Wl,--base-file,%s -o %s %s %s\n",
743 gccOptions.c_str (),
744 base_tmp.c_str (),
745 junk_tmp.c_str (),
746 archiveFilename.c_str (),
747 importLibraryDependencies.c_str () );
748 fprintf ( fMakefile,
749 "\t${rm} %s\n",
750 junk_tmp.c_str () );
751 fprintf ( fMakefile,
752 "\t${dlltool} --dllname %s --base-file %s --output-exp %s --kill-at\n",
753 target.c_str (),
754 base_tmp.c_str (),
755 temp_exp.c_str ());
756 fprintf ( fMakefile,
757 "\t${rm} %s\n",
758 base_tmp.c_str () );
759 fprintf ( fMakefile,
760 "\t${gcc} %s -Wl,%s -o %s %s %s\n",
761 gccOptions.c_str (),
762 temp_exp.c_str (),
763 target.c_str (),
764 archiveFilename.c_str (),
765 importLibraryDependencies.c_str () );
766 fprintf ( fMakefile,
767 "\t${rm} %s\n\n",
768 temp_exp.c_str () );
769
770 GenerateMacrosAndTargetsTarget ( module );
771 }
772
773
774 static MingwStaticLibraryModuleHandler staticlibrary_handler;
775
776 MingwStaticLibraryModuleHandler::MingwStaticLibraryModuleHandler ()
777 : MingwModuleHandler ( StaticLibrary )
778 {
779 }
780
781 void
782 MingwStaticLibraryModuleHandler::Process ( const Module& module )
783 {
784 GeneratePreconditionDependencies ( module );
785 GenerateStaticLibraryModuleTarget ( module );
786 GenerateInvocations ( module );
787 }
788
789 void
790 MingwStaticLibraryModuleHandler::GenerateStaticLibraryModuleTarget ( const Module& module )
791 {
792 GenerateMacrosAndTargetsTarget ( module );
793 }
794
795
796 static MingwKernelModeDLLModuleHandler kernelmodedll_handler;
797
798 MingwKernelModeDLLModuleHandler::MingwKernelModeDLLModuleHandler ()
799 : MingwModuleHandler ( KernelModeDLL )
800 {
801 }
802
803 void
804 MingwKernelModeDLLModuleHandler::Process ( const Module& module )
805 {
806 GeneratePreconditionDependencies ( module );
807 GenerateKernelModeDLLModuleTarget ( module );
808 GenerateInvocations ( module );
809 }
810
811 void
812 MingwKernelModeDLLModuleHandler::GenerateKernelModeDLLModuleTarget ( const Module& module )
813 {
814 static string ros_junk ( "$(ROS_TEMPORARY)" );
815 string target ( FixupTargetFilename ( module.GetPath () ) );
816 string workingDirectory = GetWorkingDirectory ( );
817 string archiveFilename = GetModuleArchiveFilename ( module );
818 string importLibraryDependencies = GetImportLibraryDependencies ( module );
819
820 if (module.importLibrary != NULL)
821 {
822 fprintf ( fMakefile, "%s:\n",
823 module.GetDependencyPath ().c_str () );
824
825 fprintf ( fMakefile,
826 "\t${dlltool} --dllname %s --def %s --output-lib %s --kill-at\n\n",
827 module.GetTargetName ().c_str (),
828 FixupTargetFilename ( module.GetBasePath () + SSEP + module.importLibrary->definition ).c_str (),
829 FixupTargetFilename ( module.GetDependencyPath () ).c_str () );
830 }
831
832 if (module.files.size () > 0)
833 {
834 fprintf ( fMakefile, "%s: %s %s\n",
835 target.c_str (),
836 archiveFilename.c_str (),
837 importLibraryDependencies.c_str () );
838
839 fprintf ( fMakefile,
840 "\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",
841 target.c_str (),
842 archiveFilename.c_str (),
843 importLibraryDependencies.c_str () );
844
845 GenerateMacrosAndTargetsTarget ( module );
846 }
847 else
848 {
849 fprintf ( fMakefile, "%s:\n",
850 target.c_str ());
851 fprintf ( fMakefile, ".PHONY: %s\n\n",
852 target.c_str ());
853 }
854 }
855
856
857 static MingwNativeDLLModuleHandler nativedll_handler;
858
859 MingwNativeDLLModuleHandler::MingwNativeDLLModuleHandler ()
860 : MingwModuleHandler ( NativeDLL )
861 {
862 }
863
864 void
865 MingwNativeDLLModuleHandler::Process ( const Module& module )
866 {
867 GeneratePreconditionDependencies ( module );
868 GenerateNativeDLLModuleTarget ( module );
869 GenerateInvocations ( module );
870 }
871
872 void
873 MingwNativeDLLModuleHandler::GenerateNativeDLLModuleTarget ( const Module& module )
874 {
875 static string ros_junk ( "$(ROS_TEMPORARY)" );
876 string target ( FixupTargetFilename ( module.GetPath () ) );
877 string workingDirectory = GetWorkingDirectory ( );
878 string archiveFilename = GetModuleArchiveFilename ( module );
879 string importLibraryDependencies = GetImportLibraryDependencies ( module );
880
881 if (module.importLibrary != NULL)
882 {
883 fprintf ( fMakefile, "%s:\n",
884 module.GetDependencyPath ().c_str () );
885
886 fprintf ( fMakefile,
887 "\t${dlltool} --dllname %s --def %s --output-lib %s --kill-at\n\n",
888 module.GetTargetName ().c_str (),
889 FixupTargetFilename ( module.GetBasePath () + SSEP + module.importLibrary->definition ).c_str (),
890 FixupTargetFilename ( module.GetDependencyPath () ).c_str () );
891 }
892
893 if (module.files.size () > 0)
894 {
895 fprintf ( fMakefile, "%s: %s %s\n",
896 target.c_str (),
897 archiveFilename.c_str (),
898 importLibraryDependencies.c_str () );
899
900 fprintf ( fMakefile,
901 "\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",
902 target.c_str (),
903 archiveFilename.c_str (),
904 importLibraryDependencies.c_str () );
905
906 GenerateMacrosAndTargetsTarget ( module );
907 }
908 else
909 {
910 fprintf ( fMakefile, "%s:\n\n",
911 target.c_str ());
912 fprintf ( fMakefile, ".PHONY: %s\n\n",
913 target.c_str ());
914 }
915 }
916
917
918 static MingwWin32DLLModuleHandler win32dll_handler;
919
920 MingwWin32DLLModuleHandler::MingwWin32DLLModuleHandler ()
921 : MingwModuleHandler ( Win32DLL )
922 {
923 }
924
925 void
926 MingwWin32DLLModuleHandler::Process ( const Module& module )
927 {
928 GeneratePreconditionDependencies ( module );
929 GenerateWin32DLLModuleTarget ( module );
930 GenerateInvocations ( module );
931 }
932
933 void
934 MingwWin32DLLModuleHandler::GenerateWin32DLLModuleTarget ( const Module& module )
935 {
936 static string ros_junk ( "$(ROS_TEMPORARY)" );
937 string target ( FixupTargetFilename ( module.GetPath () ) );
938 string workingDirectory = GetWorkingDirectory ( );
939 string archiveFilename = GetModuleArchiveFilename ( module );
940 string importLibraryDependencies = GetImportLibraryDependencies ( module );
941
942 if (module.importLibrary != NULL)
943 {
944 fprintf ( fMakefile, "%s:\n",
945 module.GetDependencyPath ().c_str () );
946
947 fprintf ( fMakefile,
948 "\t${dlltool} --dllname %s --def %s --output-lib %s --kill-at\n\n",
949 module.GetTargetName ().c_str (),
950 FixupTargetFilename ( module.GetBasePath () + SSEP + module.importLibrary->definition ).c_str (),
951 FixupTargetFilename ( module.GetDependencyPath () ).c_str () );
952 }
953
954 if (module.files.size () > 0)
955 {
956 fprintf ( fMakefile, "%s: %s %s\n",
957 target.c_str (),
958 archiveFilename.c_str (),
959 importLibraryDependencies.c_str () );
960
961 fprintf ( fMakefile,
962 "\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",
963 target.c_str (),
964 archiveFilename.c_str (),
965 importLibraryDependencies.c_str () );
966
967 GenerateMacrosAndTargetsTarget ( module );
968 }
969 else
970 {
971 fprintf ( fMakefile, "%s:\n\n",
972 target.c_str ());
973 fprintf ( fMakefile, ".PHONY: %s\n\n",
974 target.c_str ());
975 }
976 }