43df9ba870769a87da1c96d1fa0bc0a2da9c3e25
[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 Module& module,
266 const char* op,
267 const vector<File*>& files,
268 const vector<Include*>* includes,
269 const vector<Define*>& defines,
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 && includes->size()) || defines.size() )
277 {
278 fprintf (
279 fMakefile,
280 "%s %s",
281 cflags_macro.c_str(),
282 op );
283 if ( includes )
284 for ( i = 0; i < includes->size(); i++ )
285 fprintf (
286 fMakefile,
287 " -I%s",
288 (*includes)[i]->directory.c_str() );
289 for ( i = 0; i < module.defines.size(); i++ )
290 {
291 Define& d = *module.defines[i];
292 fprintf (
293 fMakefile,
294 " -D%s",
295 d.name.c_str() );
296 if ( d.value.size() )
297 fprintf (
298 fMakefile,
299 "=%s",
300 d.value.c_str() );
301 }
302 fprintf ( fMakefile, "\n" );
303 }
304
305 if ( files.size() )
306 {
307 fprintf (
308 fMakefile,
309 "%s %s",
310 objs_macro.c_str(),
311 op );
312 for ( i = 0; i < files.size(); i++ )
313 {
314 fprintf (
315 fMakefile,
316 "%s%s",
317 ( i%10 == 9 ? "\\\n\t" : " " ),
318 GetObjectFilename(files[i]->name).c_str() );
319 }
320 fprintf ( fMakefile, "\n" );
321 }
322 }
323
324 void
325 MingwModuleHandler::GenerateMacros (
326 const Module& module,
327 const string& cflags_macro,
328 const string& nasmflags_macro,
329 const string& objs_macro) const
330 {
331 GenerateMacros (
332 module,
333 "=",
334 module.files,
335 &module.includes,
336 module.defines,
337 cflags_macro,
338 nasmflags_macro,
339 objs_macro );
340 fprintf ( fMakefile, "\n" );
341
342 for ( size_t i = 0; i < module.ifs.size(); i++ )
343 {
344 If& rIf = *module.ifs[i];
345 if ( rIf.defines.size() || rIf.files.size() )
346 {
347 fprintf (
348 fMakefile,
349 "ifeq ($(%s),\"%s\")\n",
350 rIf.property.c_str(),
351 rIf.value.c_str() );
352 GenerateMacros (
353 module,
354 "+=",
355 rIf.files,
356 NULL,
357 rIf.defines,
358 cflags_macro,
359 nasmflags_macro,
360 objs_macro );
361 fprintf (
362 fMakefile,
363 "endif\n\n" );
364 }
365 }
366 }
367
368 string
369 MingwModuleHandler::GenerateGccCommand ( const Module& module,
370 const string& sourceFilename,
371 const string& cc,
372 const string& cflagsMacro ) const
373 {
374 string objectFilename = GetObjectFilename ( sourceFilename );
375 return ssprintf ( "%s -c %s -o %s %s\n",
376 cc.c_str (),
377 sourceFilename.c_str (),
378 objectFilename.c_str (),
379 cflagsMacro.c_str () );
380 }
381
382 string
383 MingwModuleHandler::GenerateGccAssemblerCommand ( const Module& module,
384 const string& sourceFilename,
385 const string& cc,
386 const string& cflagsMacro ) const
387 {
388 string objectFilename = GetObjectFilename ( sourceFilename );
389 return ssprintf ( "%s -x assembler-with-cpp -c %s -o %s -D__ASM__ %s\n",
390 cc.c_str (),
391 sourceFilename.c_str (),
392 objectFilename.c_str (),
393 cflagsMacro.c_str () );
394 }
395
396 string
397 MingwModuleHandler::GenerateNasmCommand ( const Module& module,
398 const string& sourceFilename,
399 const string& nasmflagsMacro ) const
400 {
401 string objectFilename = GetObjectFilename ( sourceFilename );
402 return ssprintf ( "%s -f win32 %s -o %s %s\n",
403 "nasm",
404 sourceFilename.c_str (),
405 objectFilename.c_str (),
406 nasmflagsMacro.c_str () );
407 }
408
409 string
410 MingwModuleHandler::GenerateCommand ( const Module& module,
411 const string& sourceFilename,
412 const string& cc,
413 const string& cflagsMacro,
414 const string& nasmflagsMacro ) const
415 {
416 string extension = GetExtension ( sourceFilename );
417 if ( extension == ".c" || extension == ".C" )
418 return GenerateGccCommand ( module,
419 sourceFilename,
420 cc,
421 cflagsMacro );
422 else if ( extension == ".s" || extension == ".S" )
423 return GenerateGccAssemblerCommand ( module,
424 sourceFilename,
425 cc,
426 cflagsMacro );
427 else if ( extension == ".asm" || extension == ".ASM" )
428 return GenerateNasmCommand ( module,
429 sourceFilename,
430 nasmflagsMacro );
431
432 throw InvalidOperationException ( __FILE__,
433 __LINE__,
434 "Unsupported filename extension '%s' in file '%s'",
435 extension.c_str (),
436 sourceFilename.c_str () );
437 }
438
439 void
440 MingwModuleHandler::GenerateObjectFileTargets ( const Module& module,
441 const vector<File*>& files,
442 const string& cc,
443 const string& cflagsMacro,
444 const string& nasmflagsMacro ) const
445 {
446 if ( files.size () == 0 )
447 return;
448
449 for ( size_t i = 0; i < files.size (); i++ )
450 {
451 string sourceFilename = files[i]->name;
452 string objectFilename = GetObjectFilename ( sourceFilename );
453 fprintf ( fMakefile,
454 "%s: %s\n",
455 objectFilename.c_str (),
456 sourceFilename.c_str () );
457 fprintf ( fMakefile,
458 "\t%s\n",
459 GenerateCommand ( module,
460 sourceFilename,
461 cc,
462 cflagsMacro,
463 nasmflagsMacro ).c_str () );
464 }
465
466 fprintf ( fMakefile, "\n" );
467 }
468
469 void
470 MingwModuleHandler::GenerateObjectFileTargets ( const Module& module,
471 const string& cc,
472 const string& cflagsMacro,
473 const string& nasmflagsMacro ) const
474 {
475 GenerateObjectFileTargets ( module, module.files, cc, cflagsMacro, nasmflagsMacro );
476 for ( size_t i = 0; i < module.ifs.size(); i++ )
477 GenerateObjectFileTargets ( module, module.ifs[i]->files, cc, cflagsMacro, nasmflagsMacro );
478 }
479
480 void
481 MingwModuleHandler::GenerateArchiveTarget ( const Module& module,
482 const string& ar,
483 const string& objs_macro ) const
484 {
485 string archiveFilename = GetModuleArchiveFilename ( module );
486 string sourceFilenames = GetSourceFilenames ( module );
487
488 fprintf ( fMakefile,
489 "%s: %s\n",
490 archiveFilename.c_str (),
491 objs_macro.c_str ());
492
493 fprintf ( fMakefile,
494 "\t%s -rc %s %s\n\n",
495 ar.c_str (),
496 archiveFilename.c_str (),
497 objs_macro.c_str ());
498 }
499
500 void
501 MingwModuleHandler::GenerateMacrosAndTargets (
502 const Module& module,
503 const string& cc,
504 const string& ar ) const
505 {
506 string cflagsMacro = ssprintf("%s_CFLAGS",module.name.c_str());
507 string nasmflagsMacro = ssprintf("%s_NASMFLAGS",module.name.c_str());
508 string objectsMacro = ssprintf("%s_OBJS",module.name.c_str());
509
510 GenerateMacros ( module, cflagsMacro, nasmflagsMacro, objectsMacro );
511
512 // future references to the macros will be to get their values
513 cflagsMacro = ssprintf("$(%s)",cflagsMacro.c_str());
514 nasmflagsMacro = ssprintf("$(%s)",nasmflagsMacro.c_str());
515 objectsMacro = ssprintf("$(%s)",objectsMacro.c_str());
516
517 GenerateArchiveTarget ( module, ar, objectsMacro );
518 GenerateObjectFileTargets ( module, cc, cflagsMacro, nasmflagsMacro );
519 }
520
521 void
522 MingwModuleHandler::GenerateMacrosAndTargetsHost ( const Module& module ) const
523 {
524 GenerateMacrosAndTargets ( module, "${host_gcc}", "${host_ar}" );
525 }
526
527 void
528 MingwModuleHandler::GenerateMacrosAndTargetsTarget ( const Module& module ) const
529 {
530 GenerateMacrosAndTargets ( module, "${gcc}", "${ar}" );
531 }
532
533 string
534 MingwModuleHandler::GetInvocationDependencies ( const Module& module ) const
535 {
536 string dependencies;
537 for ( size_t i = 0; i < module.invocations.size (); i++ )
538 {
539 Invoke& invoke = *module.invocations[i];
540 if (invoke.invokeModule == &module)
541 /* Protect against circular dependencies */
542 continue;
543 if ( dependencies.length () > 0 )
544 dependencies += " ";
545 dependencies += invoke.GetTargets ();
546 }
547 return dependencies;
548 }
549
550 string
551 MingwModuleHandler::GetInvocationParameters ( const Invoke& invoke ) const
552 {
553 string parameters ( "" );
554 size_t i;
555 for (i = 0; i < invoke.output.size (); i++)
556 {
557 if (parameters.length () > 0)
558 parameters += " ";
559 InvokeFile& invokeFile = *invoke.output[i];
560 if (invokeFile.switches.length () > 0)
561 {
562 parameters += invokeFile.switches;
563 parameters += " ";
564 }
565 parameters += invokeFile.name;
566 }
567
568 for (i = 0; i < invoke.input.size (); i++)
569 {
570 if (parameters.length () > 0)
571 parameters += " ";
572 InvokeFile& invokeFile = *invoke.input[i];
573 if (invokeFile.switches.length () > 0)
574 {
575 parameters += invokeFile.switches;
576 parameters += " ";
577 }
578 parameters += invokeFile.name;
579 }
580
581 return parameters;
582 }
583
584 void
585 MingwModuleHandler::GenerateInvocations ( const Module& module ) const
586 {
587 if ( module.invocations.size () == 0 )
588 return;
589
590 for ( size_t i = 0; i < module.invocations.size (); i++ )
591 {
592 const Invoke& invoke = *module.invocations[i];
593
594 if ( invoke.invokeModule->type != BuildTool )
595 throw InvalidBuildFileException ( module.node.location,
596 "Only modules of type buildtool can be invoked." );
597
598 string invokeTarget = module.GetInvocationTarget ( i );
599 fprintf ( fMakefile,
600 "%s: %s\n\n",
601 invoke.GetTargets ().c_str (),
602 invokeTarget.c_str () );
603 fprintf ( fMakefile,
604 "%s: %s\n",
605 invokeTarget.c_str (),
606 FixupTargetFilename ( invoke.invokeModule->GetPath () ).c_str () );
607 fprintf ( fMakefile,
608 "\t%s %s\n\n",
609 FixupTargetFilename ( invoke.invokeModule->GetPath () ).c_str (),
610 GetInvocationParameters ( invoke ).c_str () );
611 fprintf ( fMakefile,
612 ".PNONY: %s\n\n",
613 invokeTarget.c_str () );
614 }
615 }
616
617 string
618 MingwModuleHandler::GetPreconditionDependenciesName ( const Module& module ) const
619 {
620 return ssprintf ( "%s_precondition",
621 module.name.c_str () );
622 }
623
624 void
625 MingwModuleHandler::GeneratePreconditionDependencies ( const Module& module ) const
626 {
627 string preconditionDependenciesName = GetPreconditionDependenciesName ( module );
628 string sourceFilenames = GetSourceFilenames ( module );
629 string dependencies = GetModuleDependencies ( module );
630 string s = GetInvocationDependencies ( module );
631 if ( s.length () > 0 )
632 {
633 if ( dependencies.length () > 0 )
634 dependencies += " ";
635 dependencies += s;
636 }
637
638 fprintf ( fMakefile,
639 ".PHONY: %s\n\n",
640 preconditionDependenciesName.c_str () );
641 fprintf ( fMakefile,
642 "%s: %s\n\n",
643 preconditionDependenciesName.c_str (),
644 dependencies.c_str () );
645 const char* p = sourceFilenames.c_str();
646 const char* end = p + strlen(p);
647 while ( p < end )
648 {
649 const char* p2 = &p[512];
650 if ( p2 > end )
651 p2 = end;
652 while ( p2 > p && !isspace(*p2) )
653 --p2;
654 if ( p == p2 )
655 {
656 p2 = strpbrk ( p, " \t" );
657 if ( !p2 )
658 p2 = end;
659 }
660 fprintf ( fMakefile,
661 "%.*s: %s\n",
662 p2-p,
663 p,
664 preconditionDependenciesName.c_str ());
665 p = p2;
666 p += strspn ( p, " \t" );
667 }
668 fprintf ( fMakefile, "\n" );
669 }
670
671
672 static MingwBuildToolModuleHandler buildtool_handler;
673
674 MingwBuildToolModuleHandler::MingwBuildToolModuleHandler()
675 : MingwModuleHandler ( BuildTool )
676 {
677 }
678
679 void
680 MingwBuildToolModuleHandler::Process ( const Module& module )
681 {
682 GeneratePreconditionDependencies ( module );
683 GenerateBuildToolModuleTarget ( module );
684 GenerateInvocations ( module );
685 }
686
687 void
688 MingwBuildToolModuleHandler::GenerateBuildToolModuleTarget ( const Module& module )
689 {
690 string target ( FixupTargetFilename ( module.GetPath () ) );
691 string archiveFilename = GetModuleArchiveFilename ( module );
692 fprintf ( fMakefile, "%s: %s\n",
693 target.c_str (),
694 archiveFilename.c_str () );
695 fprintf ( fMakefile,
696 "\t${host_gcc} -o %s %s\n\n",
697 target.c_str (),
698 archiveFilename.c_str () );
699 GenerateMacrosAndTargetsHost ( module );
700 }
701
702 static MingwKernelModuleHandler kernelmodule_handler;
703
704 MingwKernelModuleHandler::MingwKernelModuleHandler ()
705 : MingwModuleHandler ( Kernel )
706 {
707 }
708
709 void
710 MingwKernelModuleHandler::Process ( const Module& module )
711 {
712 GeneratePreconditionDependencies ( module );
713 GenerateKernelModuleTarget ( module );
714 GenerateInvocations ( module );
715 }
716
717 void
718 MingwKernelModuleHandler::GenerateKernelModuleTarget ( const Module& module )
719 {
720 static string ros_junk ( "$(ROS_TEMPORARY)" );
721 //static string ros_output ( "$(ROS_INTERMEDIATE)" );
722 string target ( FixupTargetFilename(module.GetPath()) );
723 string workingDirectory = GetWorkingDirectory ( );
724 string archiveFilename = GetModuleArchiveFilename ( module );
725 string importLibraryDependencies = GetImportLibraryDependencies ( module );
726 string base_tmp = ros_junk + module.name + ".base.tmp";
727 string junk_tmp = ros_junk + module.name + ".junk.tmp";
728 string temp_exp = ros_junk + module.name + ".temp.exp";
729 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",
730 module.GetBasePath ().c_str () );
731 fprintf ( fMakefile, "%s: %s %s\n",
732 target.c_str (),
733 archiveFilename.c_str (),
734 importLibraryDependencies.c_str () );
735 fprintf ( fMakefile,
736 "\t${gcc} %s -Wl,--base-file,%s -o %s %s %s\n",
737 gccOptions.c_str (),
738 base_tmp.c_str (),
739 junk_tmp.c_str (),
740 archiveFilename.c_str (),
741 importLibraryDependencies.c_str () );
742 fprintf ( fMakefile,
743 "\t${rm} %s\n",
744 junk_tmp.c_str () );
745 fprintf ( fMakefile,
746 "\t${dlltool} --dllname %s --base-file %s --output-exp %s --kill-at\n",
747 target.c_str (),
748 base_tmp.c_str (),
749 temp_exp.c_str ());
750 fprintf ( fMakefile,
751 "\t${rm} %s\n",
752 base_tmp.c_str () );
753 fprintf ( fMakefile,
754 "\t${gcc} %s -Wl,%s -o %s %s %s\n",
755 gccOptions.c_str (),
756 temp_exp.c_str (),
757 target.c_str (),
758 archiveFilename.c_str (),
759 importLibraryDependencies.c_str () );
760 fprintf ( fMakefile,
761 "\t${rm} %s\n\n",
762 temp_exp.c_str () );
763
764 GenerateMacrosAndTargetsTarget ( module );
765 }
766
767
768 static MingwStaticLibraryModuleHandler staticlibrary_handler;
769
770 MingwStaticLibraryModuleHandler::MingwStaticLibraryModuleHandler ()
771 : MingwModuleHandler ( StaticLibrary )
772 {
773 }
774
775 void
776 MingwStaticLibraryModuleHandler::Process ( const Module& module )
777 {
778 GeneratePreconditionDependencies ( module );
779 GenerateStaticLibraryModuleTarget ( module );
780 GenerateInvocations ( module );
781 }
782
783 void
784 MingwStaticLibraryModuleHandler::GenerateStaticLibraryModuleTarget ( const Module& module )
785 {
786 GenerateMacrosAndTargetsTarget ( module );
787 }
788
789
790 static MingwKernelModeDLLModuleHandler kernelmodedll_handler;
791
792 MingwKernelModeDLLModuleHandler::MingwKernelModeDLLModuleHandler ()
793 : MingwModuleHandler ( KernelModeDLL )
794 {
795 }
796
797 void
798 MingwKernelModeDLLModuleHandler::Process ( const Module& module )
799 {
800 GeneratePreconditionDependencies ( module );
801 GenerateKernelModeDLLModuleTarget ( module );
802 GenerateInvocations ( module );
803 }
804
805 void
806 MingwKernelModeDLLModuleHandler::GenerateKernelModeDLLModuleTarget ( const Module& module )
807 {
808 static string ros_junk ( "$(ROS_TEMPORARY)" );
809 string target ( FixupTargetFilename ( module.GetPath () ) );
810 string workingDirectory = GetWorkingDirectory ( );
811 string archiveFilename = GetModuleArchiveFilename ( module );
812 string importLibraryDependencies = GetImportLibraryDependencies ( module );
813
814 if (module.importLibrary != NULL)
815 {
816 fprintf ( fMakefile, "%s:\n",
817 module.GetDependencyPath ().c_str () );
818
819 fprintf ( fMakefile,
820 "\t${dlltool} --dllname %s --def %s --output-lib %s --kill-at\n\n",
821 module.GetTargetName ().c_str (),
822 FixupTargetFilename ( module.GetBasePath () + SSEP + module.importLibrary->definition ).c_str (),
823 FixupTargetFilename ( module.GetDependencyPath () ).c_str () );
824 }
825
826 if (module.files.size () > 0)
827 {
828 fprintf ( fMakefile, "%s: %s %s\n",
829 target.c_str (),
830 archiveFilename.c_str (),
831 importLibraryDependencies.c_str () );
832
833 fprintf ( fMakefile,
834 "\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",
835 target.c_str (),
836 archiveFilename.c_str (),
837 importLibraryDependencies.c_str () );
838
839 GenerateMacrosAndTargetsTarget ( module );
840 }
841 else
842 {
843 fprintf ( fMakefile, "%s:\n",
844 target.c_str ());
845 fprintf ( fMakefile, ".PHONY: %s\n\n",
846 target.c_str ());
847 }
848 }
849
850
851 static MingwNativeDLLModuleHandler nativedll_handler;
852
853 MingwNativeDLLModuleHandler::MingwNativeDLLModuleHandler ()
854 : MingwModuleHandler ( NativeDLL )
855 {
856 }
857
858 void
859 MingwNativeDLLModuleHandler::Process ( const Module& module )
860 {
861 GeneratePreconditionDependencies ( module );
862 GenerateNativeDLLModuleTarget ( module );
863 GenerateInvocations ( module );
864 }
865
866 void
867 MingwNativeDLLModuleHandler::GenerateNativeDLLModuleTarget ( const Module& module )
868 {
869 static string ros_junk ( "$(ROS_TEMPORARY)" );
870 string target ( FixupTargetFilename ( module.GetPath () ) );
871 string workingDirectory = GetWorkingDirectory ( );
872 string archiveFilename = GetModuleArchiveFilename ( module );
873 string importLibraryDependencies = GetImportLibraryDependencies ( module );
874
875 if (module.importLibrary != NULL)
876 {
877 fprintf ( fMakefile, "%s:\n",
878 module.GetDependencyPath ().c_str () );
879
880 fprintf ( fMakefile,
881 "\t${dlltool} --dllname %s --def %s --output-lib %s --kill-at\n\n",
882 module.GetTargetName ().c_str (),
883 FixupTargetFilename ( module.GetBasePath () + SSEP + module.importLibrary->definition ).c_str (),
884 FixupTargetFilename ( module.GetDependencyPath () ).c_str () );
885 }
886
887 if (module.files.size () > 0)
888 {
889 fprintf ( fMakefile, "%s: %s %s\n",
890 target.c_str (),
891 archiveFilename.c_str (),
892 importLibraryDependencies.c_str () );
893
894 fprintf ( fMakefile,
895 "\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",
896 target.c_str (),
897 archiveFilename.c_str (),
898 importLibraryDependencies.c_str () );
899
900 GenerateMacrosAndTargetsTarget ( module );
901 }
902 else
903 {
904 fprintf ( fMakefile, "%s:\n\n",
905 target.c_str ());
906 fprintf ( fMakefile, ".PHONY: %s\n\n",
907 target.c_str ());
908 }
909 }