42876579cc9843bbbcf2fe7b55ae6dc2ce68fb2c
[reactos.git] / reactos / tools / rbuild / backend / mingw / mingw.cpp
1 /*
2 * Copyright (C) 2005 Casper S. Hornstrup
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17 */
18 #include "../../pch.h"
19
20 #include "mingw.h"
21 #include <assert.h>
22 #include "modulehandler.h"
23
24 using std::string;
25 using std::vector;
26 using std::set;
27 using std::map;
28
29 typedef set<string> set_string;
30
31
32 string
33 v2s ( const string_list& v, int wrap_at )
34 {
35 if ( !v.size() )
36 return "";
37 string s;
38 int wrap_count = 0;
39 for ( size_t i = 0; i < v.size(); i++ )
40 {
41 if ( !v[i].size() )
42 continue;
43 if ( wrap_at > 0 && wrap_count++ == wrap_at )
44 s += " \\\n\t\t";
45 else if ( s.size() )
46 s += " ";
47 s += v[i];
48 }
49 return s;
50 }
51
52
53 static class MingwFactory : public Backend::Factory
54 {
55 public:
56 MingwFactory() : Factory ( "mingw" ) {}
57 Backend* operator() ( Project& project,
58 Configuration& configuration )
59 {
60 return new MingwBackend ( project,
61 configuration );
62 }
63 } factory;
64
65
66 MingwBackend::MingwBackend ( Project& project,
67 Configuration& configuration )
68 : Backend ( project, configuration ),
69 intermediateDirectory ( new Directory ("$(INTERMEDIATE)" ) ),
70 outputDirectory ( new Directory ( "$(OUTPUT)" ) ),
71 installDirectory ( new Directory ( "$(INSTALL)" ) )
72 {
73 compilerPrefix = "";
74 }
75
76 MingwBackend::~MingwBackend()
77 {
78 delete intermediateDirectory;
79 delete outputDirectory;
80 delete installDirectory;
81 }
82
83 string
84 MingwBackend::AddDirectoryTarget ( const string& directory,
85 Directory* directoryTree )
86 {
87 if ( directory.length () > 0)
88 directoryTree->Add ( directory.c_str() );
89 return directoryTree->name;
90 }
91
92 void
93 MingwBackend::ProcessModules ()
94 {
95 printf ( "Processing modules..." );
96
97 vector<MingwModuleHandler*> v;
98 size_t i;
99 for ( i = 0; i < ProjectNode.modules.size (); i++ )
100 {
101 Module& module = *ProjectNode.modules[i];
102 if ( !module.enabled )
103 continue;
104 MingwModuleHandler* h = MingwModuleHandler::InstanciateHandler (
105 module,
106 this );
107 if ( module.host == HostDefault )
108 {
109 module.host = h->DefaultHost();
110 assert ( module.host != HostDefault );
111 }
112 v.push_back ( h );
113 }
114
115 size_t iend = v.size ();
116
117 for ( i = 0; i < iend; i++ )
118 v[i]->GenerateObjectMacro();
119 fprintf ( fMakefile, "\n" );
120 for ( i = 0; i < iend; i++ )
121 v[i]->GenerateTargetMacro();
122 fprintf ( fMakefile, "\n" );
123
124 GenerateAllTarget ( v );
125 GenerateInitTarget ();
126 GenerateRegTestsRunTarget ();
127
128 for ( i = 0; i < iend; i++ )
129 v[i]->GenerateOtherMacros();
130
131 for ( i = 0; i < iend; i++ )
132 {
133 MingwModuleHandler& h = *v[i];
134 h.GeneratePreconditionDependencies ();
135 h.Process ();
136 h.GenerateInvocations ();
137 h.GenerateCleanTarget ();
138 h.GenerateInstallTarget ();
139 h.GenerateDependsTarget ();
140 delete v[i];
141 }
142
143 printf ( "done\n" );
144 }
145
146 void
147 MingwBackend::Process ()
148 {
149 if ( configuration.CheckDependenciesForModuleOnly )
150 CheckAutomaticDependenciesForModuleOnly ();
151 else
152 ProcessNormal ();
153 }
154
155 void
156 MingwBackend::CheckAutomaticDependenciesForModuleOnly ()
157 {
158 if ( configuration.AutomaticDependencies )
159 {
160 Module* module = ProjectNode.LocateModule ( configuration.CheckDependenciesForModuleOnlyModule );
161 if ( module == NULL )
162 {
163 printf ( "Module '%s' does not exist\n",
164 configuration.CheckDependenciesForModuleOnlyModule.c_str () );
165 return;
166 }
167
168 printf ( "Checking automatic dependencies for module '%s'...",
169 module->name.c_str () );
170 AutomaticDependency automaticDependency ( ProjectNode );
171 automaticDependency.CheckAutomaticDependenciesForModule ( *module,
172 configuration.Verbose );
173 printf ( "done\n" );
174 }
175 }
176
177 void
178 MingwBackend::ProcessNormal ()
179 {
180 DetectCompiler ();
181 DetectBinutils ();
182 DetectNetwideAssembler ();
183 DetectPipeSupport ();
184 DetectPCHSupport ();
185 CreateMakefile ();
186 GenerateHeader ();
187 GenerateGlobalVariables ();
188 GenerateXmlBuildFilesMacro ();
189 ProcessModules ();
190 GenerateInstallTarget ();
191 GenerateTestTarget ();
192 GenerateDirectoryTargets ();
193 GenerateDirectories ();
194 UnpackWineResources ();
195 GenerateTestSupportCode ();
196 GenerateCompilationUnitSupportCode ();
197 GenerateProxyMakefiles ();
198 CheckAutomaticDependencies ();
199 CloseMakefile ();
200 }
201
202 void
203 MingwBackend::CreateMakefile ()
204 {
205 fMakefile = fopen ( ProjectNode.makefile.c_str (), "w" );
206 if ( !fMakefile )
207 throw AccessDeniedException ( ProjectNode.makefile );
208 MingwModuleHandler::SetBackend ( this );
209 MingwModuleHandler::SetMakefile ( fMakefile );
210 MingwModuleHandler::SetUsePch ( use_pch );
211 }
212
213 void
214 MingwBackend::CloseMakefile () const
215 {
216 if (fMakefile)
217 fclose ( fMakefile );
218 }
219
220 void
221 MingwBackend::GenerateHeader () const
222 {
223 fprintf ( fMakefile, "# THIS FILE IS AUTOMATICALLY GENERATED, EDIT 'ReactOS.xml' INSTEAD\n\n" );
224 }
225
226 string
227 MingwBackend::GenerateIncludesAndDefines ( IfableData& data ) const
228 {
229 string includeParameters = MingwModuleHandler::GenerateGccIncludeParametersFromVector ( data.includes );
230 string defineParameters = MingwModuleHandler::GenerateGccDefineParametersFromVector ( data.defines );
231 return includeParameters + " " + defineParameters;
232 }
233
234 void
235 MingwBackend::GenerateProjectCFlagsMacro ( const char* assignmentOperation,
236 IfableData& data ) const
237 {
238 fprintf (
239 fMakefile,
240 "PROJECT_CFLAGS %s",
241 assignmentOperation );
242
243 fprintf ( fMakefile,
244 " %s",
245 GenerateIncludesAndDefines ( data ).c_str() );
246
247 fprintf ( fMakefile, "\n" );
248 }
249
250 void
251 MingwBackend::GenerateGlobalCFlagsAndProperties (
252 const char* assignmentOperation,
253 IfableData& data ) const
254 {
255 size_t i;
256
257 for ( i = 0; i < data.properties.size(); i++ )
258 {
259 Property& prop = *data.properties[i];
260 fprintf ( fMakefile, "%s := %s\n",
261 prop.name.c_str(),
262 prop.value.c_str() );
263 }
264
265 if ( data.includes.size() || data.defines.size() )
266 {
267 GenerateProjectCFlagsMacro ( assignmentOperation,
268 data );
269 }
270
271 for ( i = 0; i < data.ifs.size(); i++ )
272 {
273 If& rIf = *data.ifs[i];
274 if ( rIf.data.defines.size()
275 || rIf.data.includes.size()
276 || rIf.data.ifs.size() )
277 {
278 fprintf (
279 fMakefile,
280 "ifeq (\"$(%s)\",\"%s\")\n",
281 rIf.property.c_str(),
282 rIf.value.c_str() );
283 GenerateGlobalCFlagsAndProperties (
284 "+=",
285 rIf.data );
286 fprintf (
287 fMakefile,
288 "endif\n\n" );
289 }
290 }
291 }
292
293 void
294 MingwBackend::GenerateProjectGccOptionsMacro ( const char* assignmentOperation,
295 IfableData& data ) const
296 {
297 size_t i;
298
299 fprintf (
300 fMakefile,
301 "PROJECT_GCCOPTIONS %s",
302 assignmentOperation );
303
304 for ( i = 0; i < data.compilerFlags.size(); i++ )
305 {
306 fprintf (
307 fMakefile,
308 " %s",
309 data.compilerFlags[i]->flag.c_str() );
310 }
311
312 fprintf ( fMakefile, "\n" );
313 }
314
315 void
316 MingwBackend::GenerateProjectGccOptions (
317 const char* assignmentOperation,
318 IfableData& data ) const
319 {
320 size_t i;
321
322 if ( data.compilerFlags.size() )
323 {
324 GenerateProjectGccOptionsMacro ( assignmentOperation,
325 data );
326 }
327
328 for ( i = 0; i < data.ifs.size(); i++ )
329 {
330 If& rIf = *data.ifs[i];
331 if ( rIf.data.compilerFlags.size()
332 || rIf.data.ifs.size() )
333 {
334 fprintf (
335 fMakefile,
336 "ifeq (\"$(%s)\",\"%s\")\n",
337 rIf.property.c_str(),
338 rIf.value.c_str() );
339 GenerateProjectGccOptions (
340 "+=",
341 rIf.data );
342 fprintf (
343 fMakefile,
344 "endif\n\n" );
345 }
346 }
347 }
348
349 string
350 MingwBackend::GenerateProjectLFLAGS () const
351 {
352 string lflags;
353 for ( size_t i = 0; i < ProjectNode.linkerFlags.size (); i++ )
354 {
355 LinkerFlag& linkerFlag = *ProjectNode.linkerFlags[i];
356 if ( lflags.length () > 0 )
357 lflags += " ";
358 lflags += linkerFlag.flag;
359 }
360 return lflags;
361 }
362
363 void
364 MingwBackend::GenerateGlobalVariables () const
365 {
366 fprintf ( fMakefile,
367 "PREFIX := %s\n",
368 compilerPrefix.c_str () );
369 fprintf ( fMakefile,
370 "nasm := %s\n",
371 nasmCommand.c_str () );
372
373 GenerateGlobalCFlagsAndProperties ( "=", ProjectNode.non_if_data );
374 GenerateProjectGccOptions ( "=", ProjectNode.non_if_data );
375
376 fprintf ( fMakefile, "PROJECT_RCFLAGS := $(PROJECT_CFLAGS)\n" );
377 fprintf ( fMakefile, "PROJECT_WIDLFLAGS := $(PROJECT_CFLAGS)\n" );
378 fprintf ( fMakefile, "PROJECT_LFLAGS := %s\n",
379 GenerateProjectLFLAGS ().c_str () );
380 fprintf ( fMakefile, "PROJECT_CFLAGS += -Wall\n" );
381 fprintf ( fMakefile, "PROJECT_CFLAGS += $(PROJECT_GCCOPTIONS)\n" );
382 fprintf ( fMakefile, "\n" );
383 }
384
385 bool
386 MingwBackend::IncludeInAllTarget ( const Module& module ) const
387 {
388 if ( MingwModuleHandler::ReferenceObjects ( module ) )
389 return false;
390 if ( module.type == BootSector )
391 return false;
392 if ( module.type == Iso )
393 return false;
394 if ( module.type == LiveIso )
395 return false;
396 if ( module.type == Test )
397 return false;
398 if ( module.type == Alias )
399 return false;
400 return true;
401 }
402
403 void
404 MingwBackend::GenerateAllTarget ( const vector<MingwModuleHandler*>& handlers ) const
405 {
406 fprintf ( fMakefile, "all:" );
407 int wrap_count = 0;
408 size_t iend = handlers.size ();
409 for ( size_t i = 0; i < iend; i++ )
410 {
411 const Module& module = handlers[i]->module;
412 if ( IncludeInAllTarget ( module ) )
413 {
414 if ( wrap_count++ == 5 )
415 fprintf ( fMakefile, " \\\n\t\t" ), wrap_count = 0;
416 fprintf ( fMakefile,
417 " %s",
418 GetTargetMacro(module).c_str () );
419 }
420 }
421 fprintf ( fMakefile, "\n\t\n\n" );
422 }
423
424 string
425 MingwBackend::GetBuildToolDependencies () const
426 {
427 string dependencies;
428 for ( size_t i = 0; i < ProjectNode.modules.size (); i++ )
429 {
430 Module& module = *ProjectNode.modules[i];
431 if ( !module.enabled )
432 continue;
433 if ( module.type == BuildTool )
434 {
435 if ( dependencies.length () > 0 )
436 dependencies += " ";
437 dependencies += module.GetDependencyPath ();
438 }
439 }
440 return dependencies;
441 }
442
443 void
444 MingwBackend::GenerateInitTarget () const
445 {
446 fprintf ( fMakefile,
447 "INIT = %s\n",
448 GetBuildToolDependencies ().c_str () );
449 fprintf ( fMakefile, "\n" );
450 }
451
452 void
453 MingwBackend::GenerateRegTestsRunTarget () const
454 {
455 fprintf ( fMakefile,
456 "REGTESTS_RUN_TARGET = regtests.dll\n" );
457 fprintf ( fMakefile,
458 "$(REGTESTS_RUN_TARGET): $(REGTESTS_TARGET)\n" );
459 fprintf ( fMakefile,
460 "\t$(cp) $(REGTESTS_TARGET) $(REGTESTS_RUN_TARGET)\n" );
461 fprintf ( fMakefile, "\n" );
462 }
463
464 void
465 MingwBackend::GenerateXmlBuildFilesMacro() const
466 {
467 fprintf ( fMakefile,
468 "XMLBUILDFILES = %s \\\n",
469 ProjectNode.GetProjectFilename ().c_str () );
470 string xmlbuildFilenames;
471 int numberOfExistingFiles = 0;
472 for ( size_t i = 0; i < ProjectNode.xmlbuildfiles.size (); i++ )
473 {
474 XMLInclude& xmlbuildfile = *ProjectNode.xmlbuildfiles[i];
475 if ( !xmlbuildfile.fileExists )
476 continue;
477 numberOfExistingFiles++;
478 if ( xmlbuildFilenames.length () > 0 )
479 xmlbuildFilenames += " ";
480 xmlbuildFilenames += NormalizeFilename ( xmlbuildfile.topIncludeFilename );
481 if ( numberOfExistingFiles % 5 == 4 || i == ProjectNode.xmlbuildfiles.size () - 1 )
482 {
483 fprintf ( fMakefile,
484 "\t%s",
485 xmlbuildFilenames.c_str ());
486 if ( i == ProjectNode.xmlbuildfiles.size () - 1 )
487 {
488 fprintf ( fMakefile, "\n" );
489 }
490 else
491 {
492 fprintf ( fMakefile,
493 " \\\n" );
494 }
495 xmlbuildFilenames.resize ( 0 );
496 }
497 numberOfExistingFiles++;
498 }
499 fprintf ( fMakefile, "\n" );
500 }
501
502 string
503 MingwBackend::GetBin2ResExecutable ()
504 {
505 return NormalizeFilename ( Environment::GetOutputPath () + sSep + "tools/bin2res/bin2res" + ExePostfix );
506 }
507
508 void
509 MingwBackend::UnpackWineResources ()
510 {
511 printf ( "Unpacking WINE resources..." );
512 WineResource wineResource ( ProjectNode,
513 GetBin2ResExecutable () );
514 wineResource.UnpackResources ( configuration.Verbose );
515 printf ( "done\n" );
516 }
517
518 void
519 MingwBackend::GenerateTestSupportCode ()
520 {
521 printf ( "Generating test support code..." );
522 TestSupportCode testSupportCode ( ProjectNode );
523 testSupportCode.GenerateTestSupportCode ( configuration.Verbose );
524 printf ( "done\n" );
525 }
526
527 void
528 MingwBackend::GenerateCompilationUnitSupportCode ()
529 {
530 printf ( "Generating compilation unit support code..." );
531 CompilationUnitSupportCode compilationUnitSupportCode ( ProjectNode );
532 compilationUnitSupportCode.Generate ( configuration.Verbose );
533 printf ( "done\n" );
534 }
535
536 string
537 MingwBackend::GetProxyMakefileTree () const
538 {
539 if ( configuration.GenerateProxyMakefilesInSourceTree )
540 return "";
541 else
542 return Environment::GetOutputPath ();
543 }
544
545 void
546 MingwBackend::GenerateProxyMakefiles ()
547 {
548 printf ( "Generating proxy makefiles..." );
549 ProxyMakefile proxyMakefile ( ProjectNode );
550 proxyMakefile.GenerateProxyMakefiles ( configuration.Verbose,
551 GetProxyMakefileTree () );
552 printf ( "done\n" );
553 }
554
555 void
556 MingwBackend::CheckAutomaticDependencies ()
557 {
558 if ( configuration.AutomaticDependencies )
559 {
560 printf ( "Checking automatic dependencies..." );
561 AutomaticDependency automaticDependency ( ProjectNode );
562 automaticDependency.CheckAutomaticDependencies ( configuration.Verbose );
563 printf ( "done\n" );
564 }
565 }
566
567 bool
568 MingwBackend::IncludeDirectoryTarget ( const string& directory ) const
569 {
570 if ( directory == "$(INTERMEDIATE)" + sSep + "tools")
571 return false;
572 else
573 return true;
574 }
575
576 void
577 MingwBackend::GenerateDirectories ()
578 {
579 printf ( "Creating directories..." );
580 intermediateDirectory->GenerateTree ( "", configuration.Verbose );
581 outputDirectory->GenerateTree ( "", configuration.Verbose );
582 if ( !configuration.MakeHandlesInstallDirectories )
583 installDirectory->GenerateTree ( "", configuration.Verbose );
584 printf ( "done\n" );
585 }
586
587 bool
588 MingwBackend::TryToDetectThisCompiler ( const string& compiler )
589 {
590 string command = ssprintf (
591 "%s -v 1>%s 2>%s",
592 FixSeparatorForSystemCommand(compiler).c_str (),
593 NUL,
594 NUL );
595 int exitcode = system ( command.c_str () );
596 return (exitcode == 0);
597 }
598
599 void
600 MingwBackend::DetectCompiler ()
601 {
602 printf ( "Detecting compiler..." );
603
604 bool detectedCompiler = false;
605 const string& ROS_PREFIXValue = Environment::GetVariable ( "ROS_PREFIX" );
606 if ( ROS_PREFIXValue.length () > 0 )
607 {
608 compilerPrefix = ROS_PREFIXValue;
609 compilerCommand = compilerPrefix + "-gcc";
610 detectedCompiler = TryToDetectThisCompiler ( compilerCommand );
611 }
612 #if defined(WIN32)
613 if ( !detectedCompiler )
614 {
615 compilerPrefix = "";
616 compilerCommand = "gcc";
617 detectedCompiler = TryToDetectThisCompiler ( compilerCommand );
618 }
619 #endif
620 if ( !detectedCompiler )
621 {
622 compilerPrefix = "mingw32";
623 compilerCommand = compilerPrefix + "-gcc";
624 detectedCompiler = TryToDetectThisCompiler ( compilerCommand );
625 }
626 if ( detectedCompiler )
627 printf ( "detected (%s)\n", compilerCommand.c_str () );
628 else
629 printf ( "not detected\n" );
630 }
631
632 bool
633 MingwBackend::TryToDetectThisNetwideAssembler ( const string& assembler )
634 {
635 string command = ssprintf (
636 "%s -h 1>%s 2>%s",
637 FixSeparatorForSystemCommand(assembler).c_str (),
638 NUL,
639 NUL );
640 int exitcode = system ( command.c_str () );
641 return (exitcode == 0);
642 }
643
644 bool
645 MingwBackend::TryToDetectThisBinutils ( const string& binutils )
646 {
647 string command = ssprintf (
648 "%s -v 1>%s",
649 FixSeparatorForSystemCommand(binutils).c_str (),
650 NUL,
651 NUL );
652 int exitcode = system ( command.c_str () );
653 return (exitcode == 0);
654 }
655
656 string
657 MingwBackend::GetBinutilsVersion ( const string& binutilsCommand )
658 {
659 FILE *fp;
660 int ch, i;
661 char buffer[81];
662
663 string versionCommand = ssprintf ( "%s -v",
664 binutilsCommand.c_str (),
665 NUL,
666 NUL );
667 fp = popen ( versionCommand.c_str () , "r" );
668 for( i = 0;
669 ( i < 80 ) &&
670 ( feof ( fp ) == 0 &&
671 ( ( ch = fgetc( fp ) ) != -1 ) );
672 i++ )
673 {
674 buffer[i] = (char) ch;
675 }
676 buffer[i] = '\0';
677 pclose ( fp );
678
679 char separators[] = " ";
680 char *token;
681 char *prevtoken = NULL;
682
683 token = strtok ( buffer, separators );
684 while ( token != NULL )
685 {
686 prevtoken = token;
687 token = strtok ( NULL, separators );
688 }
689 string version = string ( prevtoken );
690 int lastDigit = version.find_last_not_of ( "\t\r\n" );
691 if ( lastDigit != -1 )
692 return string ( version, 0, lastDigit+1 );
693 else
694 return version;
695 }
696
697 bool
698 MingwBackend::IsSupportedBinutilsVersion ( const string& binutilsVersion )
699 {
700 if ( ( ( strcmp ( binutilsVersion.c_str (), "20040902") >= 0 ) &&
701 ( strcmp ( binutilsVersion.c_str (), "20041008") <= 0 ) ) ||
702 ( strcmp ( binutilsVersion.c_str (), "20031001") < 0 ) )
703 return false;
704 else
705 return true;
706 }
707
708 void
709 MingwBackend::DetectBinutils ()
710 {
711 printf ( "Detecting binutils..." );
712
713 bool detectedBinutils = false;
714 const string& ROS_PREFIXValue = Environment::GetVariable ( "ROS_PREFIX" );
715 if ( ROS_PREFIXValue.length () > 0 )
716 {
717 binutilsPrefix = ROS_PREFIXValue;
718 binutilsCommand = binutilsPrefix + "-ld";
719 detectedBinutils = TryToDetectThisBinutils ( binutilsCommand );
720 }
721 #if defined(WIN32)
722 if ( !detectedBinutils )
723 {
724 binutilsPrefix = "";
725 binutilsCommand = "ld";
726 detectedBinutils = TryToDetectThisBinutils ( binutilsCommand );
727 }
728 #endif
729 if ( !detectedBinutils )
730 {
731 binutilsPrefix = "mingw32";
732 binutilsCommand = binutilsPrefix + "-ld";
733 detectedBinutils = TryToDetectThisBinutils ( binutilsCommand );
734 }
735 if ( detectedBinutils )
736 {
737 string binutilsVersion = GetBinutilsVersion ( binutilsCommand );
738 if ( IsSupportedBinutilsVersion ( binutilsVersion ) )
739 printf ( "detected (%s)\n", binutilsCommand.c_str () );
740 else
741 {
742 printf ( "detected (%s), but with unsupported version (%s)\n",
743 binutilsCommand.c_str (),
744 binutilsVersion.c_str () );
745 throw UnsupportedBuildToolException ( binutilsCommand, binutilsVersion );
746 }
747 }
748 else
749 printf ( "not detected\n" );
750 }
751
752 void
753 MingwBackend::DetectNetwideAssembler ()
754 {
755 printf ( "Detecting netwide assembler..." );
756
757 nasmCommand = "nasm";
758 bool detectedNasm = TryToDetectThisNetwideAssembler ( nasmCommand );
759 #if defined(WIN32)
760 if ( !detectedNasm )
761 {
762 nasmCommand = "nasmw";
763 detectedNasm = TryToDetectThisNetwideAssembler ( nasmCommand );
764 }
765 #endif
766 if ( !detectedNasm )
767 {
768 nasmCommand = "yasm";
769 detectedNasm = TryToDetectThisNetwideAssembler ( nasmCommand );
770 }
771 if ( detectedNasm )
772 printf ( "detected (%s)\n", nasmCommand.c_str () );
773 else
774 printf ( "not detected\n" );
775 }
776
777 void
778 MingwBackend::DetectPipeSupport ()
779 {
780 printf ( "Detecting compiler -pipe support..." );
781
782 string pipe_detection = "tools" + sSep + "rbuild" + sSep + "backend" + sSep + "mingw" + sSep + "pipe_detection.c";
783 string pipe_detectionObjectFilename = ReplaceExtension ( pipe_detection,
784 ".o" );
785 string command = ssprintf (
786 "%s -pipe -c %s -o %s 1>%s 2>%s",
787 FixSeparatorForSystemCommand(compilerCommand).c_str (),
788 pipe_detection.c_str (),
789 pipe_detectionObjectFilename.c_str (),
790 NUL,
791 NUL );
792 int exitcode = system ( command.c_str () );
793 FILE* f = fopen ( pipe_detectionObjectFilename.c_str (), "rb" );
794 if ( f )
795 {
796 usePipe = (exitcode == 0);
797 fclose ( f );
798 unlink ( pipe_detectionObjectFilename.c_str () );
799 }
800 else
801 usePipe = false;
802
803 if ( usePipe )
804 printf ( "detected\n" );
805 else
806 printf ( "not detected\n" );
807 }
808
809 void
810 MingwBackend::DetectPCHSupport ()
811 {
812 printf ( "Detecting compiler pre-compiled header support..." );
813
814 string path = "tools" + sSep + "rbuild" + sSep + "backend" + sSep + "mingw" + sSep + "pch_detection.h";
815 string cmd = ssprintf (
816 "%s -c %s 1>%s 2>%s",
817 FixSeparatorForSystemCommand(compilerCommand).c_str (),
818 path.c_str (),
819 NUL,
820 NUL );
821 system ( cmd.c_str () );
822 path += ".gch";
823
824 FILE* f = fopen ( path.c_str (), "rb" );
825 if ( f )
826 {
827 use_pch = true;
828 fclose ( f );
829 unlink ( path.c_str () );
830 }
831 else
832 use_pch = false;
833
834 if ( use_pch )
835 printf ( "detected\n" );
836 else
837 printf ( "not detected\n" );
838 }
839
840 void
841 MingwBackend::GetNonModuleInstallTargetFiles (
842 vector<string>& out ) const
843 {
844 for ( size_t i = 0; i < ProjectNode.installfiles.size (); i++ )
845 {
846 const InstallFile& installfile = *ProjectNode.installfiles[i];
847 string targetFilenameNoFixup = installfile.base + sSep + installfile.newname;
848 string targetFilename = MingwModuleHandler::PassThruCacheDirectory (
849 NormalizeFilename ( targetFilenameNoFixup ),
850 installDirectory );
851 out.push_back ( targetFilename );
852 }
853 }
854
855 void
856 MingwBackend::GetModuleInstallTargetFiles (
857 vector<string>& out ) const
858 {
859 for ( size_t i = 0; i < ProjectNode.modules.size (); i++ )
860 {
861 const Module& module = *ProjectNode.modules[i];
862 if ( !module.enabled )
863 continue;
864 if ( module.installName.length () > 0 )
865 {
866 string targetFilenameNoFixup;
867 if ( module.installBase.length () > 0 )
868 targetFilenameNoFixup = module.installBase + sSep + module.installName;
869 else
870 targetFilenameNoFixup = module.installName;
871 string targetFilename = MingwModuleHandler::PassThruCacheDirectory (
872 NormalizeFilename ( targetFilenameNoFixup ),
873 installDirectory );
874 out.push_back ( targetFilename );
875 }
876 }
877 }
878
879 void
880 MingwBackend::GetInstallTargetFiles (
881 vector<string>& out ) const
882 {
883 GetNonModuleInstallTargetFiles ( out );
884 GetModuleInstallTargetFiles ( out );
885 }
886
887 void
888 MingwBackend::OutputInstallTarget ( const string& sourceFilename,
889 const string& targetFilename,
890 const string& targetDirectory )
891 {
892 string fullTargetFilename;
893 if ( targetDirectory.length () > 0)
894 fullTargetFilename = targetDirectory + sSep + targetFilename;
895 else
896 fullTargetFilename = targetFilename;
897 string normalizedTargetFilename = MingwModuleHandler::PassThruCacheDirectory (
898 NormalizeFilename ( fullTargetFilename ),
899 installDirectory );
900 string normalizedTargetDirectory = MingwModuleHandler::PassThruCacheDirectory (
901 NormalizeFilename ( targetDirectory ),
902 installDirectory );
903 fprintf ( fMakefile,
904 "%s: %s | %s\n",
905 normalizedTargetFilename.c_str (),
906 sourceFilename.c_str (),
907 normalizedTargetDirectory.c_str () );
908 fprintf ( fMakefile,
909 "\t$(ECHO_CP)\n" );
910 fprintf ( fMakefile,
911 "\t${cp} %s %s 1>$(NUL)\n",
912 sourceFilename.c_str (),
913 normalizedTargetFilename.c_str () );
914 }
915
916 void
917 MingwBackend::OutputNonModuleInstallTargets ()
918 {
919 for ( size_t i = 0; i < ProjectNode.installfiles.size (); i++ )
920 {
921 const InstallFile& installfile = *ProjectNode.installfiles[i];
922 OutputInstallTarget ( installfile.GetPath (),
923 installfile.newname,
924 installfile.base );
925 }
926 }
927
928 const Module&
929 MingwBackend::GetAliasedModuleOrModule ( const Module& module ) const
930 {
931 if ( module.aliasedModuleName.size () > 0 )
932 {
933 const Module* aliasedModule = ProjectNode.LocateModule ( module.aliasedModuleName );
934 assert ( aliasedModule );
935 return *aliasedModule;
936 }
937 else
938 return module;
939 }
940
941 void
942 MingwBackend::OutputModuleInstallTargets ()
943 {
944 for ( size_t i = 0; i < ProjectNode.modules.size (); i++ )
945 {
946 const Module& module = *ProjectNode.modules[i];
947 if ( !module.enabled )
948 continue;
949 if ( module.installName.length () > 0 )
950 {
951 const Module& aliasedModule = GetAliasedModuleOrModule ( module );
952 string sourceFilename = MingwModuleHandler::PassThruCacheDirectory (
953 NormalizeFilename ( aliasedModule.GetPath () ),
954 outputDirectory );
955 OutputInstallTarget ( sourceFilename,
956 module.installName,
957 module.installBase );
958 }
959 }
960 }
961
962 string
963 MingwBackend::GetRegistrySourceFiles ()
964 {
965 return "bootdata" + sSep + "hivecls.inf "
966 "bootdata" + sSep + "hivedef.inf "
967 "bootdata" + sSep + "hiveinst.inf "
968 "bootdata" + sSep + "hivesft.inf "
969 "bootdata" + sSep + "hivesys.inf";
970 }
971
972 string
973 MingwBackend::GetRegistryTargetFiles ()
974 {
975 string system32ConfigDirectory = NormalizeFilename (
976 MingwModuleHandler::PassThruCacheDirectory (
977 "system32" + sSep + "config" + sSep,
978 installDirectory ) );
979 return system32ConfigDirectory + sSep + "default " +
980 system32ConfigDirectory + sSep + "sam " +
981 system32ConfigDirectory + sSep + "security " +
982 system32ConfigDirectory + sSep + "software " +
983 system32ConfigDirectory + sSep + "system";
984 }
985
986 void
987 MingwBackend::OutputRegistryInstallTarget ()
988 {
989 string system32ConfigDirectory = NormalizeFilename (
990 MingwModuleHandler::PassThruCacheDirectory (
991 "system32" + sSep + "config" + sSep,
992 installDirectory ) );
993
994 string registrySourceFiles = GetRegistrySourceFiles ();
995 string registryTargetFiles = GetRegistryTargetFiles ();
996 fprintf ( fMakefile,
997 "install_registry: %s\n",
998 registryTargetFiles.c_str () );
999 fprintf ( fMakefile,
1000 "%s: %s %s $(MKHIVE_TARGET)\n",
1001 registryTargetFiles.c_str (),
1002 registrySourceFiles.c_str (),
1003 system32ConfigDirectory.c_str () );
1004 fprintf ( fMakefile,
1005 "\t$(ECHO_MKHIVE)\n" );
1006 fprintf ( fMakefile,
1007 "\t$(MKHIVE_TARGET) bootdata %s bootdata%chiveinst.inf\n",
1008 system32ConfigDirectory.c_str (),
1009 cSep );
1010 fprintf ( fMakefile,
1011 "\n" );
1012 }
1013
1014 void
1015 MingwBackend::GenerateInstallTarget ()
1016 {
1017 vector<string> vInstallTargetFiles;
1018 GetInstallTargetFiles ( vInstallTargetFiles );
1019 string installTargetFiles = v2s ( vInstallTargetFiles, 5 );
1020 string registryTargetFiles = GetRegistryTargetFiles ();
1021
1022 fprintf ( fMakefile,
1023 "install: %s %s\n",
1024 installTargetFiles.c_str (),
1025 registryTargetFiles.c_str () );
1026 OutputNonModuleInstallTargets ();
1027 OutputModuleInstallTargets ();
1028 OutputRegistryInstallTarget ();
1029 fprintf ( fMakefile,
1030 "\n" );
1031 }
1032
1033 void
1034 MingwBackend::GetModuleTestTargets (
1035 vector<string>& out ) const
1036 {
1037 for ( size_t i = 0; i < ProjectNode.modules.size (); i++ )
1038 {
1039 const Module& module = *ProjectNode.modules[i];
1040 if ( !module.enabled )
1041 continue;
1042 if ( module.type == Test )
1043 out.push_back ( module.name );
1044 }
1045 }
1046
1047 void
1048 MingwBackend::GenerateTestTarget ()
1049 {
1050 vector<string> vTestTargets;
1051 GetModuleTestTargets ( vTestTargets );
1052 string testTargets = v2s ( vTestTargets, 5 );
1053
1054 fprintf ( fMakefile,
1055 "test: %s\n",
1056 testTargets.c_str () );
1057 fprintf ( fMakefile,
1058 "\n" );
1059 }
1060
1061 void
1062 MingwBackend::GenerateDirectoryTargets ()
1063 {
1064 intermediateDirectory->CreateRule ( fMakefile, "" );
1065 outputDirectory->CreateRule ( fMakefile, "" );
1066 installDirectory->CreateRule ( fMakefile, "" );
1067 }