Build and run tests
[reactos.git] / reactos / tools / rbuild / backend / mingw / mingw.cpp
1
2 #include "../../pch.h"
3
4 #include "mingw.h"
5 #include <assert.h>
6 #include <dirent.h>
7 #include "modulehandler.h"
8
9 #ifdef WIN32
10 #define MKDIR(s) mkdir(s)
11 #else
12 #define MKDIR(s) mkdir(s, 0755)
13 #endif
14
15 using std::string;
16 using std::vector;
17 using std::set;
18 using std::map;
19
20 typedef set<string> set_string;
21
22
23 string
24 v2s ( const string_list& v, int wrap_at )
25 {
26 if ( !v.size() )
27 return "";
28 string s;
29 int wrap_count = 0;
30 for ( size_t i = 0; i < v.size(); i++ )
31 {
32 if ( !v[i].size() )
33 continue;
34 if ( wrap_at > 0 && wrap_count++ == wrap_at )
35 s += " \\\n\t\t";
36 else if ( s.size() )
37 s += " ";
38 s += v[i];
39 }
40 return s;
41 }
42
43
44 Directory::Directory ( const string& name_ )
45 : name(name_)
46 {
47 }
48
49 void
50 Directory::Add ( const char* subdir )
51 {
52 size_t i;
53 string s1 = string ( subdir );
54 if ( ( i = s1.find ( '$' ) ) != string::npos )
55 {
56 throw InvalidOperationException ( __FILE__,
57 __LINE__,
58 "No environment variables can be used here. Path was %s",
59 subdir );
60 }
61
62 const char* p = strpbrk ( subdir, "/\\" );
63 if ( !p )
64 p = subdir + strlen(subdir);
65 string s ( subdir, p-subdir );
66 if ( subdirs.find(s) == subdirs.end() )
67 subdirs[s] = new Directory(s);
68 if ( *p && *++p )
69 subdirs[s]->Add ( p );
70 }
71
72 bool
73 Directory::mkdir_p ( const char* path )
74 {
75 DIR *directory;
76 directory = opendir ( path );
77 if ( directory != NULL )
78 {
79 closedir ( directory );
80 return false;
81 }
82
83 if ( MKDIR ( path ) != 0 )
84 throw AccessDeniedException ( string ( path ) );
85 return true;
86 }
87
88 bool
89 Directory::CreateDirectory ( string path )
90 {
91 size_t index = 0;
92 size_t nextIndex;
93 if ( isalpha ( path[0] ) && path[1] == ':' && path[2] == CSEP )
94 {
95 nextIndex = path.find ( CSEP, 3);
96 }
97 else
98 nextIndex = path.find ( CSEP );
99
100 bool directoryWasCreated = false;
101 while ( nextIndex != string::npos )
102 {
103 nextIndex = path.find ( CSEP, index + 1 );
104 directoryWasCreated = mkdir_p ( path.substr ( 0, nextIndex ).c_str () );
105 index = nextIndex;
106 }
107 return directoryWasCreated;
108 }
109
110 string
111 Directory::ReplaceVariable ( string name,
112 string value,
113 string path )
114 {
115 size_t i = path.find ( name );
116 if ( i != string::npos )
117 return path.replace ( i, name.length (), value );
118 else
119 return path;
120 }
121
122 void
123 Directory::ResolveVariablesInPath ( char* buf,
124 string path )
125 {
126 string s = ReplaceVariable ( "$(INTERMEDIATE)", Environment::GetIntermediatePath (), path );
127 s = ReplaceVariable ( "$(OUTPUT)", Environment::GetOutputPath (), s );
128 s = ReplaceVariable ( "$(INSTALL)", Environment::GetInstallPath (), s );
129 strcpy ( buf, s.c_str () );
130 }
131
132 void
133 Directory::GenerateTree ( const string& parent,
134 bool verbose )
135 {
136 string path;
137
138 if ( parent.size () > 0 )
139 {
140 char buf[256];
141
142 path = parent + SSEP + name;
143 ResolveVariablesInPath ( buf, path );
144 if ( CreateDirectory ( buf ) && verbose )
145 printf ( "Created %s\n", buf );
146 }
147 else
148 path = name;
149
150 for ( directory_map::iterator i = subdirs.begin ();
151 i != subdirs.end ();
152 ++i )
153 {
154 i->second->GenerateTree ( path, verbose );
155 }
156 }
157
158 string
159 Directory::EscapeSpaces ( string path )
160 {
161 string newpath;
162 char* p = &path[0];
163 while ( *p != 0 )
164 {
165 if ( *p == ' ' )
166 newpath = newpath + "\\ ";
167 else
168 newpath = newpath + *p;
169 *p++;
170 }
171 return newpath;
172 }
173
174 void
175 Directory::CreateRule ( FILE* f,
176 const string& parent )
177 {
178 string path;
179
180 if ( parent.size() > 0 )
181 {
182 string escapedParent = EscapeSpaces ( parent );
183 fprintf ( f,
184 "%s%c%s: | %s\n",
185 escapedParent.c_str (),
186 CSEP,
187 EscapeSpaces ( name ).c_str (),
188 escapedParent.c_str () );
189
190 fprintf ( f,
191 "\t$(ECHO_MKDIR)\n" );
192
193 fprintf ( f,
194 "\t${mkdir} $@\n" );
195
196 path = parent + SSEP + name;
197 }
198 else
199 path = name;
200
201 for ( directory_map::iterator i = subdirs.begin();
202 i != subdirs.end();
203 ++i )
204 {
205 i->second->CreateRule ( f, path );
206 }
207 }
208
209
210 static class MingwFactory : public Backend::Factory
211 {
212 public:
213 MingwFactory() : Factory ( "mingw" ) {}
214 Backend* operator() ( Project& project,
215 bool verbose,
216 bool cleanAsYouGo )
217 {
218 return new MingwBackend ( project,
219 verbose,
220 cleanAsYouGo );
221 }
222 } factory;
223
224
225 MingwBackend::MingwBackend ( Project& project,
226 bool verbose,
227 bool cleanAsYouGo )
228 : Backend ( project, verbose, cleanAsYouGo ),
229 intermediateDirectory ( new Directory ("$(INTERMEDIATE)" ) ),
230 outputDirectory ( new Directory ( "$(OUTPUT)" ) ),
231 installDirectory ( new Directory ( "$(INSTALL)" ) )
232 {
233 }
234
235 MingwBackend::~MingwBackend()
236 {
237 delete intermediateDirectory;
238 delete outputDirectory;
239 delete installDirectory;
240 }
241
242 string
243 MingwBackend::AddDirectoryTarget ( const string& directory,
244 Directory* directoryTree )
245 {
246 if ( directory.length () > 0)
247 directoryTree->Add ( directory.c_str() );
248 return directoryTree->name;
249 }
250
251 void
252 MingwBackend::ProcessModules ()
253 {
254 printf ( "Processing modules..." );
255
256 vector<MingwModuleHandler*> v;
257 size_t i;
258 for ( i = 0; i < ProjectNode.modules.size (); i++ )
259 {
260 Module& module = *ProjectNode.modules[i];
261 MingwModuleHandler* h = MingwModuleHandler::InstanciateHandler (
262 module,
263 this );
264 if ( module.host == HostDefault )
265 {
266 module.host = h->DefaultHost();
267 assert ( module.host != HostDefault );
268 }
269 v.push_back ( h );
270 }
271
272 size_t iend = v.size ();
273
274 for ( i = 0; i < iend; i++ )
275 v[i]->GenerateObjectMacro();
276 fprintf ( fMakefile, "\n" );
277 for ( i = 0; i < iend; i++ )
278 v[i]->GenerateTargetMacro();
279 fprintf ( fMakefile, "\n" );
280
281 GenerateAllTarget ( v );
282 GenerateInitTarget ();
283 GenerateRegTestsRunTarget ();
284
285 for ( i = 0; i < iend; i++ )
286 v[i]->GenerateOtherMacros();
287
288 for ( i = 0; i < iend; i++ )
289 {
290 MingwModuleHandler& h = *v[i];
291 h.GeneratePreconditionDependencies ();
292 h.Process ();
293 h.GenerateInvocations ();
294 h.GenerateCleanTarget ();
295 h.GenerateInstallTarget ();
296 delete v[i];
297 }
298
299 printf ( "done\n" );
300 }
301
302 void
303 MingwBackend::Process ()
304 {
305 DetectCompiler ();
306 DetectPipeSupport ();
307 DetectPCHSupport ();
308 CreateMakefile ();
309 GenerateHeader ();
310 GenerateGlobalVariables ();
311 GenerateXmlBuildFilesMacro ();
312 ProcessModules ();
313 GenerateInstallTarget ();
314 GenerateDirectoryTargets ();
315 GenerateDirectories ();
316 UnpackWineResources ();
317 GenerateTestSupportCode ();
318 GenerateProxyMakefiles ();
319 CheckAutomaticDependencies ();
320 CloseMakefile ();
321 }
322
323 void
324 MingwBackend::CreateMakefile ()
325 {
326 fMakefile = fopen ( ProjectNode.makefile.c_str (), "w" );
327 if ( !fMakefile )
328 throw AccessDeniedException ( ProjectNode.makefile );
329 MingwModuleHandler::SetBackend ( this );
330 MingwModuleHandler::SetMakefile ( fMakefile );
331 MingwModuleHandler::SetUsePch ( use_pch );
332 }
333
334 void
335 MingwBackend::CloseMakefile () const
336 {
337 if (fMakefile)
338 fclose ( fMakefile );
339 }
340
341 void
342 MingwBackend::GenerateHeader () const
343 {
344 fprintf ( fMakefile, "# THIS FILE IS AUTOMATICALLY GENERATED, EDIT 'ReactOS.xml' INSTEAD\n\n" );
345 }
346
347 string
348 MingwBackend::GenerateIncludesAndDefines ( IfableData& data ) const
349 {
350 string includeParameters = MingwModuleHandler::GenerateGccIncludeParametersFromVector ( data.includes );
351 string defineParameters = MingwModuleHandler::GenerateGccDefineParametersFromVector ( data.defines );
352 return includeParameters + " " + defineParameters;
353 }
354
355 void
356 MingwBackend::GenerateProjectCFlagsMacro ( const char* assignmentOperation,
357 IfableData& data ) const
358 {
359 fprintf (
360 fMakefile,
361 "PROJECT_CFLAGS %s",
362 assignmentOperation );
363
364 fprintf ( fMakefile,
365 " %s",
366 GenerateIncludesAndDefines ( data ).c_str() );
367
368 fprintf ( fMakefile, "\n" );
369 }
370
371 void
372 MingwBackend::GenerateGlobalCFlagsAndProperties (
373 const char* assignmentOperation,
374 IfableData& data ) const
375 {
376 size_t i;
377
378 for ( i = 0; i < data.properties.size(); i++ )
379 {
380 Property& prop = *data.properties[i];
381 fprintf ( fMakefile, "%s := %s\n",
382 prop.name.c_str(),
383 prop.value.c_str() );
384 }
385
386 if ( data.includes.size() || data.defines.size() )
387 {
388 GenerateProjectCFlagsMacro ( assignmentOperation,
389 data );
390 }
391
392 for ( i = 0; i < data.ifs.size(); i++ )
393 {
394 If& rIf = *data.ifs[i];
395 if ( rIf.data.defines.size()
396 || rIf.data.includes.size()
397 || rIf.data.ifs.size() )
398 {
399 fprintf (
400 fMakefile,
401 "ifeq (\"$(%s)\",\"%s\")\n",
402 rIf.property.c_str(),
403 rIf.value.c_str() );
404 GenerateGlobalCFlagsAndProperties (
405 "+=",
406 rIf.data );
407 fprintf (
408 fMakefile,
409 "endif\n\n" );
410 }
411 }
412 }
413
414 void
415 MingwBackend::GenerateProjectGccOptionsMacro ( const char* assignmentOperation,
416 IfableData& data ) const
417 {
418 size_t i;
419
420 fprintf (
421 fMakefile,
422 "PROJECT_GCCOPTIONS %s",
423 assignmentOperation );
424
425 for ( i = 0; i < data.compilerFlags.size(); i++ )
426 {
427 fprintf (
428 fMakefile,
429 " %s",
430 data.compilerFlags[i]->flag.c_str() );
431 }
432
433 fprintf ( fMakefile, "\n" );
434 }
435
436 void
437 MingwBackend::GenerateProjectGccOptions (
438 const char* assignmentOperation,
439 IfableData& data ) const
440 {
441 size_t i;
442
443 if ( data.compilerFlags.size() )
444 {
445 GenerateProjectGccOptionsMacro ( assignmentOperation,
446 data );
447 }
448
449 for ( i = 0; i < data.ifs.size(); i++ )
450 {
451 If& rIf = *data.ifs[i];
452 if ( rIf.data.compilerFlags.size()
453 || rIf.data.ifs.size() )
454 {
455 fprintf (
456 fMakefile,
457 "ifeq (\"$(%s)\",\"%s\")\n",
458 rIf.property.c_str(),
459 rIf.value.c_str() );
460 GenerateProjectGccOptions (
461 "+=",
462 rIf.data );
463 fprintf (
464 fMakefile,
465 "endif\n\n" );
466 }
467 }
468 }
469
470 string
471 MingwBackend::GenerateProjectLFLAGS () const
472 {
473 string lflags;
474 for ( size_t i = 0; i < ProjectNode.linkerFlags.size (); i++ )
475 {
476 LinkerFlag& linkerFlag = *ProjectNode.linkerFlags[i];
477 if ( lflags.length () > 0 )
478 lflags += " ";
479 lflags += linkerFlag.flag;
480 }
481 return lflags;
482 }
483
484 void
485 MingwBackend::GenerateGlobalVariables () const
486 {
487 GenerateGlobalCFlagsAndProperties ( "=", ProjectNode.non_if_data );
488 GenerateProjectGccOptions ( "=", ProjectNode.non_if_data );
489
490 fprintf ( fMakefile, "PROJECT_RCFLAGS := $(PROJECT_CFLAGS)\n" );
491 fprintf ( fMakefile, "PROJECT_WIDLFLAGS := $(PROJECT_CFLAGS)\n" );
492 fprintf ( fMakefile, "PROJECT_LFLAGS := %s\n",
493 GenerateProjectLFLAGS ().c_str () );
494 fprintf ( fMakefile, "PROJECT_CFLAGS += $(PROJECT_GCCOPTIONS)\n" );
495 fprintf ( fMakefile, "PROJECT_CFLAGS += -Wall\n" );
496 fprintf ( fMakefile, "\n" );
497 }
498
499 bool
500 MingwBackend::IncludeInAllTarget ( const Module& module ) const
501 {
502 if ( MingwModuleHandler::ReferenceObjects ( module ) )
503 return false;
504 if ( module.type == BootSector )
505 return false;
506 if ( module.type == Iso )
507 return false;
508 if ( module.type == LiveIso )
509 return false;
510 if ( module.type == Test )
511 return false;
512 return true;
513 }
514
515 void
516 MingwBackend::GenerateAllTarget ( const vector<MingwModuleHandler*>& handlers ) const
517 {
518 fprintf ( fMakefile, "all:" );
519 int wrap_count = 0;
520 size_t iend = handlers.size ();
521 for ( size_t i = 0; i < iend; i++ )
522 {
523 const Module& module = handlers[i]->module;
524 if ( IncludeInAllTarget ( module ) )
525 {
526 if ( wrap_count++ == 5 )
527 fprintf ( fMakefile, " \\\n\t\t" ), wrap_count = 0;
528 fprintf ( fMakefile,
529 " %s",
530 GetTargetMacro(module).c_str () );
531 }
532 }
533 fprintf ( fMakefile, "\n\t\n\n" );
534 }
535
536 string
537 MingwBackend::GetBuildToolDependencies () const
538 {
539 string dependencies;
540 for ( size_t i = 0; i < ProjectNode.modules.size (); i++ )
541 {
542 Module& module = *ProjectNode.modules[i];
543 if ( module.type == BuildTool )
544 {
545 if ( dependencies.length () > 0 )
546 dependencies += " ";
547 dependencies += module.GetDependencyPath ();
548 }
549 }
550 return dependencies;
551 }
552
553 void
554 MingwBackend::GenerateInitTarget () const
555 {
556 fprintf ( fMakefile,
557 "INIT = %s\n",
558 GetBuildToolDependencies ().c_str () );
559 fprintf ( fMakefile, "\n" );
560 }
561
562 void
563 MingwBackend::GenerateRegTestsRunTarget () const
564 {
565 fprintf ( fMakefile,
566 "REGTESTS_RUN_TARGET = regtests.dll\n" );
567 fprintf ( fMakefile,
568 "$(REGTESTS_RUN_TARGET):\n" );
569 fprintf ( fMakefile,
570 "\t$(cp) $(REGTESTS_TARGET) $(REGTESTS_RUN_TARGET)\n" );
571 fprintf ( fMakefile, "\n" );
572 }
573
574 void
575 MingwBackend::GenerateXmlBuildFilesMacro() const
576 {
577 fprintf ( fMakefile,
578 "XMLBUILDFILES = %s \\\n",
579 ProjectNode.GetProjectFilename ().c_str () );
580 string xmlbuildFilenames;
581 int numberOfExistingFiles = 0;
582 for ( size_t i = 0; i < ProjectNode.xmlbuildfiles.size (); i++ )
583 {
584 XMLInclude& xmlbuildfile = *ProjectNode.xmlbuildfiles[i];
585 if ( !xmlbuildfile.fileExists )
586 continue;
587 numberOfExistingFiles++;
588 if ( xmlbuildFilenames.length () > 0 )
589 xmlbuildFilenames += " ";
590 xmlbuildFilenames += NormalizeFilename ( xmlbuildfile.topIncludeFilename );
591 if ( numberOfExistingFiles % 5 == 4 || i == ProjectNode.xmlbuildfiles.size () - 1 )
592 {
593 fprintf ( fMakefile,
594 "\t%s",
595 xmlbuildFilenames.c_str ());
596 if ( i == ProjectNode.xmlbuildfiles.size () - 1 )
597 {
598 fprintf ( fMakefile, "\n" );
599 }
600 else
601 {
602 fprintf ( fMakefile,
603 " \\\n",
604 xmlbuildFilenames.c_str () );
605 }
606 xmlbuildFilenames.resize ( 0 );
607 }
608 numberOfExistingFiles++;
609 }
610 fprintf ( fMakefile, "\n" );
611 }
612
613 string
614 MingwBackend::GetBin2ResExecutable ()
615 {
616 return NormalizeFilename ( Environment::GetOutputPath () + SSEP + "tools/bin2res/bin2res" + EXEPOSTFIX );
617 }
618
619 void
620 MingwBackend::UnpackWineResources ()
621 {
622 printf ( "Unpacking WINE resources..." );
623 WineResource wineResource ( ProjectNode,
624 GetBin2ResExecutable () );
625 wineResource.UnpackResources ( verbose );
626 printf ( "done\n" );
627 }
628
629 void
630 MingwBackend::GenerateTestSupportCode ()
631 {
632 printf ( "Generating test support code..." );
633 TestSupportCode testSupportCode ( ProjectNode );
634 testSupportCode.GenerateTestSupportCode ( verbose );
635 printf ( "done\n" );
636 }
637
638 void
639 MingwBackend::GenerateProxyMakefiles ()
640 {
641 printf ( "Generating proxy makefiles..." );
642 ProxyMakefile proxyMakefile ( ProjectNode );
643 proxyMakefile.GenerateProxyMakefiles ( verbose );
644 printf ( "done\n" );
645 }
646
647 void
648 MingwBackend::CheckAutomaticDependencies ()
649 {
650 printf ( "Checking automatic dependencies..." );
651 AutomaticDependency automaticDependency ( ProjectNode );
652 automaticDependency.Process ();
653 automaticDependency.CheckAutomaticDependencies ( verbose );
654 printf ( "done\n" );
655 }
656
657 bool
658 MingwBackend::IncludeDirectoryTarget ( const string& directory ) const
659 {
660 if ( directory == "$(INTERMEDIATE)" SSEP "tools")
661 return false;
662 else
663 return true;
664 }
665
666 void
667 MingwBackend::GenerateDirectories ()
668 {
669 printf ( "Creating directories..." );
670 intermediateDirectory->GenerateTree ( "", verbose );
671 outputDirectory->GenerateTree ( "", verbose );
672 installDirectory->GenerateTree ( "", verbose );
673 printf ( "done\n" );
674 }
675
676 bool
677 MingwBackend::TryToDetectThisCompiler ( const string& compiler )
678 {
679 string command = ssprintf (
680 "%s -v 2>%s",
681 compiler.c_str (),
682 NUL );
683 int exitcode = system ( command.c_str () );
684 return (exitcode == 0);
685 }
686
687 void
688 MingwBackend::DetectCompiler ()
689 {
690 printf ( "Detecting compiler..." );
691
692 bool detectedCompiler = false;
693 const string& ROS_PREFIXValue = Environment::GetVariable ( "ROS_PREFIX" );
694 if ( ROS_PREFIXValue.length () > 0 )
695 {
696 compilerCommand = ROS_PREFIXValue + "-gcc";
697 detectedCompiler = TryToDetectThisCompiler ( compilerCommand );
698 }
699 #if defined(WIN32)
700 if ( !detectedCompiler )
701 {
702 compilerCommand = "gcc";
703 detectedCompiler = TryToDetectThisCompiler ( compilerCommand );
704 }
705 #endif
706 if ( !detectedCompiler )
707 {
708 compilerCommand = "mingw32-gcc";
709 detectedCompiler = TryToDetectThisCompiler ( compilerCommand );
710 }
711 if ( detectedCompiler )
712 printf ( "detected (%s)\n", compilerCommand.c_str () );
713 else
714 printf ( "not detected\n" );
715 }
716
717 void
718 MingwBackend::DetectPipeSupport ()
719 {
720 printf ( "Detecting compiler -pipe support..." );
721
722 string pipe_detection = "tools" SSEP "rbuild" SSEP "backend" SSEP "mingw" SSEP "pipe_detection.c";
723 string pipe_detectionObjectFilename = ReplaceExtension ( pipe_detection,
724 ".o" );
725 string command = ssprintf (
726 "%s -pipe -c %s -o %s 2>%s",
727 compilerCommand.c_str (),
728 pipe_detection.c_str (),
729 pipe_detectionObjectFilename.c_str (),
730 NUL );
731 int exitcode = system ( command.c_str () );
732 FILE* f = fopen ( pipe_detectionObjectFilename.c_str (), "rb" );
733 if ( f )
734 {
735 usePipe = (exitcode == 0);
736 fclose ( f );
737 unlink ( pipe_detectionObjectFilename.c_str () );
738 }
739 else
740 usePipe = false;
741
742 if ( usePipe )
743 printf ( "detected\n" );
744 else
745 printf ( "not detected\n" );
746 }
747
748 void
749 MingwBackend::DetectPCHSupport ()
750 {
751 printf ( "Detecting compiler pre-compiled header support..." );
752
753 string path = "tools" SSEP "rbuild" SSEP "backend" SSEP "mingw" SSEP "pch_detection.h";
754 string cmd = ssprintf (
755 "%s -c %s 2>%s",
756 compilerCommand.c_str (),
757 path.c_str (),
758 NUL );
759 system ( cmd.c_str () );
760 path += ".gch";
761
762 FILE* f = fopen ( path.c_str (), "rb" );
763 if ( f )
764 {
765 use_pch = true;
766 fclose ( f );
767 unlink ( path.c_str () );
768 }
769 else
770 use_pch = false;
771
772 if ( use_pch )
773 printf ( "detected\n" );
774 else
775 printf ( "not detected\n" );
776 }
777
778 void
779 MingwBackend::GetNonModuleInstallTargetFiles (
780 vector<string>& out ) const
781 {
782 for ( size_t i = 0; i < ProjectNode.installfiles.size (); i++ )
783 {
784 const InstallFile& installfile = *ProjectNode.installfiles[i];
785 string targetFilenameNoFixup = installfile.base + SSEP + installfile.newname;
786 string targetFilename = MingwModuleHandler::PassThruCacheDirectory (
787 NormalizeFilename ( targetFilenameNoFixup ),
788 installDirectory );
789 out.push_back ( targetFilename );
790 }
791 }
792
793 void
794 MingwBackend::GetModuleInstallTargetFiles (
795 vector<string>& out ) const
796 {
797 for ( size_t i = 0; i < ProjectNode.modules.size (); i++ )
798 {
799 const Module& module = *ProjectNode.modules[i];
800 if ( module.installName.length () > 0 )
801 {
802 string targetFilenameNoFixup;
803 if ( module.installBase.length () > 0 )
804 targetFilenameNoFixup = module.installBase + SSEP + module.installName;
805 else
806 targetFilenameNoFixup = module.installName;
807 string targetFilename = MingwModuleHandler::PassThruCacheDirectory (
808 NormalizeFilename ( targetFilenameNoFixup ),
809 installDirectory );
810 out.push_back ( targetFilename );
811 }
812 }
813 }
814
815 void
816 MingwBackend::GetInstallTargetFiles (
817 vector<string>& out ) const
818 {
819 GetNonModuleInstallTargetFiles ( out );
820 GetModuleInstallTargetFiles ( out );
821 }
822
823 void
824 MingwBackend::OutputInstallTarget ( const string& sourceFilename,
825 const string& targetFilename,
826 const string& targetDirectory )
827 {
828 string fullTargetFilename;
829 if ( targetDirectory.length () > 0)
830 fullTargetFilename = targetDirectory + SSEP + targetFilename;
831 else
832 fullTargetFilename = targetFilename;
833 string normalizedTargetFilename = MingwModuleHandler::PassThruCacheDirectory (
834 NormalizeFilename ( fullTargetFilename ),
835 installDirectory );
836 string normalizedTargetDirectory = MingwModuleHandler::PassThruCacheDirectory (
837 NormalizeFilename ( targetDirectory ),
838 installDirectory );
839 fprintf ( fMakefile,
840 "%s: %s | %s\n",
841 normalizedTargetFilename.c_str (),
842 sourceFilename.c_str (),
843 normalizedTargetDirectory.c_str () );
844 fprintf ( fMakefile,
845 "\t$(ECHO_CP)\n" );
846 fprintf ( fMakefile,
847 "\t${cp} %s %s 1>$(NUL)\n",
848 sourceFilename.c_str (),
849 normalizedTargetFilename.c_str () );
850 }
851
852 void
853 MingwBackend::OutputNonModuleInstallTargets ()
854 {
855 for ( size_t i = 0; i < ProjectNode.installfiles.size (); i++ )
856 {
857 const InstallFile& installfile = *ProjectNode.installfiles[i];
858 OutputInstallTarget ( installfile.GetPath (),
859 installfile.newname,
860 installfile.base );
861 }
862 }
863
864 void
865 MingwBackend::OutputModuleInstallTargets ()
866 {
867 for ( size_t i = 0; i < ProjectNode.modules.size (); i++ )
868 {
869 const Module& module = *ProjectNode.modules[i];
870 if ( module.installName.length () > 0 )
871 {
872 string sourceFilename = MingwModuleHandler::PassThruCacheDirectory (
873 NormalizeFilename ( module.GetPath () ),
874 outputDirectory );
875 OutputInstallTarget ( sourceFilename,
876 module.installName,
877 module.installBase );
878 }
879 }
880 }
881
882 string
883 MingwBackend::GetRegistrySourceFiles ()
884 {
885 return "bootdata" SSEP "hivecls.inf "
886 "bootdata" SSEP "hivedef.inf "
887 "bootdata" SSEP "hiveinst.inf "
888 "bootdata" SSEP "hivesft.inf "
889 "bootdata" SSEP "hivesys.inf";
890 }
891
892 string
893 MingwBackend::GetRegistryTargetFiles ()
894 {
895 string system32ConfigDirectory = NormalizeFilename (
896 MingwModuleHandler::PassThruCacheDirectory (
897 "system32" SSEP "config" SSEP,
898 installDirectory ) );
899 return system32ConfigDirectory + SSEP "default " +
900 system32ConfigDirectory + SSEP "sam " +
901 system32ConfigDirectory + SSEP "security " +
902 system32ConfigDirectory + SSEP "software " +
903 system32ConfigDirectory + SSEP "system";
904 }
905
906 void
907 MingwBackend::OutputRegistryInstallTarget ()
908 {
909 string system32ConfigDirectory = NormalizeFilename (
910 MingwModuleHandler::PassThruCacheDirectory (
911 "system32" SSEP "config" SSEP,
912 installDirectory ) );
913
914 string registrySourceFiles = GetRegistrySourceFiles ();
915 string registryTargetFiles = GetRegistryTargetFiles ();
916 fprintf ( fMakefile,
917 "install_registry: %s\n",
918 registryTargetFiles.c_str () );
919 fprintf ( fMakefile,
920 "%s: %s %s $(MKHIVE_TARGET)\n",
921 registryTargetFiles.c_str (),
922 registrySourceFiles.c_str (),
923 system32ConfigDirectory.c_str () );
924 fprintf ( fMakefile,
925 "\t$(ECHO_MKHIVE)\n" );
926 fprintf ( fMakefile,
927 "\t$(MKHIVE_TARGET) bootdata %s bootdata" SSEP "hiveinst.inf\n",
928 system32ConfigDirectory.c_str () );
929 fprintf ( fMakefile,
930 "\n" );
931 }
932
933 void
934 MingwBackend::GenerateInstallTarget ()
935 {
936 vector<string> vInstallTargetFiles;
937 GetInstallTargetFiles ( vInstallTargetFiles );
938 string installTargetFiles = v2s ( vInstallTargetFiles, 5 );
939 string registryTargetFiles = GetRegistryTargetFiles ();
940
941 fprintf ( fMakefile,
942 "install: %s %s\n",
943 installTargetFiles.c_str (),
944 registryTargetFiles.c_str () );
945 OutputNonModuleInstallTargets ();
946 OutputModuleInstallTargets ();
947 OutputRegistryInstallTarget ();
948 fprintf ( fMakefile,
949 "\n" );
950 }
951
952 void
953 MingwBackend::GenerateDirectoryTargets ()
954 {
955 intermediateDirectory->CreateRule ( fMakefile, "" );
956 outputDirectory->CreateRule ( fMakefile, "" );
957 installDirectory->CreateRule ( fMakefile, "" );
958 }