7 #include "modulehandler.h"
10 #define MKDIR(s) mkdir(s)
12 #define MKDIR(s) mkdir(s, 0755)
20 typedef set
<string
> set_string
;
24 v2s ( const string_list
& v
, int wrap_at
)
30 for ( size_t i
= 0; i
< v
.size(); i
++ )
34 if ( wrap_at
> 0 && wrap_count
++ == wrap_at
)
44 Directory::Directory ( const string
& name_
)
50 Directory::Add ( const char* subdir
)
53 string s1
= string ( subdir
);
54 if ( ( i
= s1
.find ( '$' ) ) != string::npos
)
56 throw InvalidOperationException ( __FILE__
,
58 "No environment variables can be used here. Path was %s",
62 const char* p
= strpbrk ( subdir
, "/\\" );
64 p
= subdir
+ strlen(subdir
);
65 string
s ( subdir
, p
-subdir
);
66 if ( subdirs
.find(s
) == subdirs
.end() )
67 subdirs
[s
] = new Directory(s
);
69 subdirs
[s
]->Add ( p
);
73 Directory::mkdir_p ( const char* path
)
76 directory
= opendir ( path
);
77 if ( directory
!= NULL
)
79 closedir ( directory
);
83 if ( MKDIR ( path
) != 0 )
84 throw AccessDeniedException ( string ( path
) );
89 Directory::CreateDirectory ( string path
)
93 if ( isalpha ( path
[0] ) && path
[1] == ':' && path
[2] == CSEP
)
95 nextIndex
= path
.find ( CSEP
, 3);
98 nextIndex
= path
.find ( CSEP
);
100 bool directoryWasCreated
= false;
101 while ( nextIndex
!= string::npos
)
103 nextIndex
= path
.find ( CSEP
, index
+ 1 );
104 directoryWasCreated
= mkdir_p ( path
.substr ( 0, nextIndex
).c_str () );
107 return directoryWasCreated
;
111 Directory::ReplaceVariable ( string name
,
115 size_t i
= path
.find ( name
);
116 if ( i
!= string::npos
)
117 return path
.replace ( i
, name
.length (), value
);
123 Directory::ResolveVariablesInPath ( char* buf
,
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 () );
133 Directory::GenerateTree ( const string
& parent
,
138 if ( parent
.size () > 0 )
142 path
= parent
+ SSEP
+ name
;
143 ResolveVariablesInPath ( buf
, path
);
144 if ( CreateDirectory ( buf
) && verbose
)
145 printf ( "Created %s\n", buf
);
150 for ( directory_map::iterator i
= subdirs
.begin ();
154 i
->second
->GenerateTree ( path
, verbose
);
159 Directory::EscapeSpaces ( string path
)
166 newpath
= newpath
+ "\\ ";
168 newpath
= newpath
+ *p
;
175 Directory::CreateRule ( FILE* f
,
176 const string
& parent
)
180 if ( parent
.size() > 0 )
182 string escapedParent
= EscapeSpaces ( parent
);
185 escapedParent
.c_str (),
187 EscapeSpaces ( name
).c_str (),
188 escapedParent
.c_str () );
191 "\t$(ECHO_MKDIR)\n" );
196 path
= parent
+ SSEP
+ name
;
201 for ( directory_map::iterator i
= subdirs
.begin();
205 i
->second
->CreateRule ( f
, path
);
210 static class MingwFactory
: public Backend::Factory
213 MingwFactory() : Factory ( "mingw" ) {}
214 Backend
* operator() ( Project
& project
,
218 return new MingwBackend ( project
,
225 MingwBackend::MingwBackend ( Project
& project
,
228 : Backend ( project
, verbose
, cleanAsYouGo
),
229 intermediateDirectory ( new Directory ("$(INTERMEDIATE)" ) ),
230 outputDirectory ( new Directory ( "$(OUTPUT)" ) ),
231 installDirectory ( new Directory ( "$(INSTALL)" ) )
235 MingwBackend::~MingwBackend()
237 delete intermediateDirectory
;
238 delete outputDirectory
;
239 delete installDirectory
;
243 MingwBackend::AddDirectoryTarget ( const string
& directory
,
244 Directory
* directoryTree
)
246 if ( directory
.length () > 0)
247 directoryTree
->Add ( directory
.c_str() );
248 return directoryTree
->name
;
252 MingwBackend::ProcessModules ()
254 printf ( "Processing modules..." );
256 vector
<MingwModuleHandler
*> v
;
258 for ( i
= 0; i
< ProjectNode
.modules
.size (); i
++ )
260 Module
& module
= *ProjectNode
.modules
[i
];
261 MingwModuleHandler
* h
= MingwModuleHandler::InstanciateHandler (
264 if ( module
.host
== HostDefault
)
266 module
.host
= h
->DefaultHost();
267 assert ( module
.host
!= HostDefault
);
272 size_t iend
= v
.size ();
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" );
281 GenerateAllTarget ( v
);
282 GenerateInitTarget ();
284 for ( i
= 0; i
< iend
; i
++ )
285 v
[i
]->GenerateOtherMacros();
287 for ( i
= 0; i
< iend
; i
++ )
289 MingwModuleHandler
& h
= *v
[i
];
290 h
.GeneratePreconditionDependencies ();
292 h
.GenerateInvocations ();
293 h
.GenerateCleanTarget ();
294 h
.GenerateInstallTarget ();
302 MingwBackend::Process ()
305 DetectPipeSupport ();
309 GenerateGlobalVariables ();
310 GenerateXmlBuildFilesMacro ();
312 GenerateInstallTarget ();
313 GenerateDirectoryTargets ();
314 GenerateDirectories ();
315 UnpackWineResources ();
316 GenerateTestSupportCode ();
317 CheckAutomaticDependencies ();
322 MingwBackend::CreateMakefile ()
324 fMakefile
= fopen ( ProjectNode
.makefile
.c_str (), "w" );
326 throw AccessDeniedException ( ProjectNode
.makefile
);
327 MingwModuleHandler::SetBackend ( this );
328 MingwModuleHandler::SetMakefile ( fMakefile
);
329 MingwModuleHandler::SetUsePch ( use_pch
);
333 MingwBackend::CloseMakefile () const
336 fclose ( fMakefile
);
340 MingwBackend::GenerateHeader () const
342 fprintf ( fMakefile
, "# THIS FILE IS AUTOMATICALLY GENERATED, EDIT 'ReactOS.xml' INSTEAD\n\n" );
346 MingwBackend::GenerateIncludesAndDefines ( IfableData
& data
) const
348 string includeParameters
= MingwModuleHandler::GenerateGccIncludeParametersFromVector ( data
.includes
);
349 string defineParameters
= MingwModuleHandler::GenerateGccDefineParametersFromVector ( data
.defines
);
350 return includeParameters
+ " " + defineParameters
;
354 MingwBackend::GenerateProjectCFlagsMacro ( const char* assignmentOperation
,
355 IfableData
& data
) const
360 assignmentOperation
);
364 GenerateIncludesAndDefines ( data
).c_str() );
366 fprintf ( fMakefile
, "\n" );
370 MingwBackend::GenerateGlobalCFlagsAndProperties (
371 const char* assignmentOperation
,
372 IfableData
& data
) const
376 for ( i
= 0; i
< data
.properties
.size(); i
++ )
378 Property
& prop
= *data
.properties
[i
];
379 fprintf ( fMakefile
, "%s := %s\n",
381 prop
.value
.c_str() );
384 if ( data
.includes
.size() || data
.defines
.size() )
386 GenerateProjectCFlagsMacro ( assignmentOperation
,
390 for ( i
= 0; i
< data
.ifs
.size(); i
++ )
392 If
& rIf
= *data
.ifs
[i
];
393 if ( rIf
.data
.defines
.size()
394 || rIf
.data
.includes
.size()
395 || rIf
.data
.ifs
.size() )
399 "ifeq (\"$(%s)\",\"%s\")\n",
400 rIf
.property
.c_str(),
402 GenerateGlobalCFlagsAndProperties (
413 MingwBackend::GenerateProjectGccOptionsMacro ( const char* assignmentOperation
,
414 IfableData
& data
) const
420 "PROJECT_GCCOPTIONS %s",
421 assignmentOperation
);
423 for ( i
= 0; i
< data
.compilerFlags
.size(); i
++ )
428 data
.compilerFlags
[i
]->flag
.c_str() );
431 fprintf ( fMakefile
, "\n" );
435 MingwBackend::GenerateProjectGccOptions (
436 const char* assignmentOperation
,
437 IfableData
& data
) const
441 if ( data
.compilerFlags
.size() )
443 GenerateProjectGccOptionsMacro ( assignmentOperation
,
447 for ( i
= 0; i
< data
.ifs
.size(); i
++ )
449 If
& rIf
= *data
.ifs
[i
];
450 if ( rIf
.data
.compilerFlags
.size()
451 || rIf
.data
.ifs
.size() )
455 "ifeq (\"$(%s)\",\"%s\")\n",
456 rIf
.property
.c_str(),
458 GenerateProjectGccOptions (
469 MingwBackend::GenerateProjectLFLAGS () const
472 for ( size_t i
= 0; i
< ProjectNode
.linkerFlags
.size (); i
++ )
474 LinkerFlag
& linkerFlag
= *ProjectNode
.linkerFlags
[i
];
475 if ( lflags
.length () > 0 )
477 lflags
+= linkerFlag
.flag
;
483 MingwBackend::GenerateGlobalVariables () const
485 GenerateGlobalCFlagsAndProperties ( "=", ProjectNode
.non_if_data
);
486 GenerateProjectGccOptions ( "=", ProjectNode
.non_if_data
);
488 fprintf ( fMakefile
, "PROJECT_RCFLAGS := $(PROJECT_CFLAGS)\n" );
489 fprintf ( fMakefile
, "PROJECT_WIDLFLAGS := $(PROJECT_CFLAGS)\n" );
490 fprintf ( fMakefile
, "PROJECT_LFLAGS := %s\n",
491 GenerateProjectLFLAGS ().c_str () );
492 fprintf ( fMakefile
, "PROJECT_CFLAGS += $(PROJECT_GCCOPTIONS)\n" );
493 fprintf ( fMakefile
, "PROJECT_CFLAGS += -Wall\n" );
494 fprintf ( fMakefile
, "\n" );
498 MingwBackend::IncludeInAllTarget ( const Module
& module
) const
500 if ( MingwModuleHandler::ReferenceObjects ( module
) )
502 if ( module
.type
== BootSector
)
504 if ( module
.type
== Iso
)
506 if ( module
.type
== LiveIso
)
508 if ( module
.type
== Test
)
514 MingwBackend::GenerateAllTarget ( const vector
<MingwModuleHandler
*>& handlers
) const
516 fprintf ( fMakefile
, "all:" );
518 size_t iend
= handlers
.size ();
519 for ( size_t i
= 0; i
< iend
; i
++ )
521 const Module
& module
= handlers
[i
]->module
;
522 if ( IncludeInAllTarget ( module
) )
524 if ( wrap_count
++ == 5 )
525 fprintf ( fMakefile
, " \\\n\t\t" ), wrap_count
= 0;
528 GetTargetMacro(module
).c_str () );
531 fprintf ( fMakefile
, "\n\t\n\n" );
535 MingwBackend::GetBuildToolDependencies () const
538 for ( size_t i
= 0; i
< ProjectNode
.modules
.size (); i
++ )
540 Module
& module
= *ProjectNode
.modules
[i
];
541 if ( module
.type
== BuildTool
)
543 if ( dependencies
.length () > 0 )
545 dependencies
+= module
.GetDependencyPath ();
552 MingwBackend::GenerateInitTarget () const
556 GetBuildToolDependencies ().c_str () );
557 fprintf ( fMakefile
, "\n" );
561 MingwBackend::GenerateXmlBuildFilesMacro() const
564 "XMLBUILDFILES = %s \\\n",
565 ProjectNode
.GetProjectFilename ().c_str () );
566 string xmlbuildFilenames
;
567 int numberOfExistingFiles
= 0;
568 for ( size_t i
= 0; i
< ProjectNode
.xmlbuildfiles
.size (); i
++ )
570 XMLInclude
& xmlbuildfile
= *ProjectNode
.xmlbuildfiles
[i
];
571 if ( !xmlbuildfile
.fileExists
)
573 numberOfExistingFiles
++;
574 if ( xmlbuildFilenames
.length () > 0 )
575 xmlbuildFilenames
+= " ";
576 xmlbuildFilenames
+= NormalizeFilename ( xmlbuildfile
.topIncludeFilename
);
577 if ( numberOfExistingFiles
% 5 == 4 || i
== ProjectNode
.xmlbuildfiles
.size () - 1 )
581 xmlbuildFilenames
.c_str ());
582 if ( i
== ProjectNode
.xmlbuildfiles
.size () - 1 )
584 fprintf ( fMakefile
, "\n" );
590 xmlbuildFilenames
.c_str () );
592 xmlbuildFilenames
.resize ( 0 );
594 numberOfExistingFiles
++;
596 fprintf ( fMakefile
, "\n" );
600 MingwBackend::GetBin2ResExecutable ()
602 return NormalizeFilename ( Environment::GetOutputPath () + SSEP
+ "tools/bin2res/bin2res" + EXEPOSTFIX
);
606 MingwBackend::UnpackWineResources ()
608 printf ( "Unpacking WINE resources..." );
609 WineResource
wineResource ( ProjectNode
,
610 GetBin2ResExecutable () );
611 wineResource
.UnpackResources ( verbose
);
616 MingwBackend::GenerateTestSupportCode ()
618 printf ( "Generating test support code..." );
619 TestSupportCode
testSupportCode ( ProjectNode
);
620 testSupportCode
.GenerateTestSupportCode ( verbose
);
625 MingwBackend::CheckAutomaticDependencies ()
627 printf ( "Checking automatic dependencies..." );
628 AutomaticDependency
automaticDependency ( ProjectNode
);
629 automaticDependency
.Process ();
630 automaticDependency
.CheckAutomaticDependencies ( verbose
);
635 MingwBackend::IncludeDirectoryTarget ( const string
& directory
) const
637 if ( directory
== "$(INTERMEDIATE)" SSEP
"tools")
644 MingwBackend::GenerateDirectories ()
646 printf ( "Creating directories..." );
647 intermediateDirectory
->GenerateTree ( "", verbose
);
648 outputDirectory
->GenerateTree ( "", verbose
);
649 installDirectory
->GenerateTree ( "", verbose
);
654 MingwBackend::TryToDetectThisCompiler ( const string
& compiler
)
656 string command
= ssprintf (
660 int exitcode
= system ( command
.c_str () );
661 return (exitcode
== 0);
665 MingwBackend::DetectCompiler ()
667 printf ( "Detecting compiler..." );
669 bool detectedCompiler
= false;
670 const string
& ROS_PREFIXValue
= Environment::GetVariable ( "ROS_PREFIX" );
671 if ( ROS_PREFIXValue
.length () > 0 )
673 compilerCommand
= ROS_PREFIXValue
+ "-gcc";
674 detectedCompiler
= TryToDetectThisCompiler ( compilerCommand
);
677 if ( !detectedCompiler
)
679 compilerCommand
= "gcc";
680 detectedCompiler
= TryToDetectThisCompiler ( compilerCommand
);
683 if ( !detectedCompiler
)
685 compilerCommand
= "mingw32-gcc";
686 detectedCompiler
= TryToDetectThisCompiler ( compilerCommand
);
688 if ( detectedCompiler
)
689 printf ( "detected (%s)\n", compilerCommand
.c_str () );
691 printf ( "not detected\n" );
695 MingwBackend::DetectPipeSupport ()
697 printf ( "Detecting compiler -pipe support..." );
699 string pipe_detection
= "tools" SSEP
"rbuild" SSEP
"backend" SSEP
"mingw" SSEP
"pipe_detection.c";
700 string pipe_detectionObjectFilename
= ReplaceExtension ( pipe_detection
,
702 string command
= ssprintf (
703 "%s -pipe -c %s -o %s 2>%s",
704 compilerCommand
.c_str (),
705 pipe_detection
.c_str (),
706 pipe_detectionObjectFilename
.c_str (),
708 int exitcode
= system ( command
.c_str () );
709 FILE* f
= fopen ( pipe_detectionObjectFilename
.c_str (), "rb" );
712 usePipe
= (exitcode
== 0);
714 unlink ( pipe_detectionObjectFilename
.c_str () );
720 printf ( "detected\n" );
722 printf ( "not detected\n" );
726 MingwBackend::DetectPCHSupport ()
728 printf ( "Detecting compiler pre-compiled header support..." );
730 string path
= "tools" SSEP
"rbuild" SSEP
"backend" SSEP
"mingw" SSEP
"pch_detection.h";
731 string cmd
= ssprintf (
733 compilerCommand
.c_str (),
736 system ( cmd
.c_str () );
739 FILE* f
= fopen ( path
.c_str (), "rb" );
744 unlink ( path
.c_str () );
750 printf ( "detected\n" );
752 printf ( "not detected\n" );
756 MingwBackend::GetNonModuleInstallTargetFiles (
757 vector
<string
>& out
) const
759 for ( size_t i
= 0; i
< ProjectNode
.installfiles
.size (); i
++ )
761 const InstallFile
& installfile
= *ProjectNode
.installfiles
[i
];
762 string targetFilenameNoFixup
= installfile
.base
+ SSEP
+ installfile
.newname
;
763 string targetFilename
= MingwModuleHandler::PassThruCacheDirectory (
764 NormalizeFilename ( targetFilenameNoFixup
),
766 out
.push_back ( targetFilename
);
771 MingwBackend::GetModuleInstallTargetFiles (
772 vector
<string
>& out
) const
774 for ( size_t i
= 0; i
< ProjectNode
.modules
.size (); i
++ )
776 const Module
& module
= *ProjectNode
.modules
[i
];
777 if ( module
.installName
.length () > 0 )
779 string targetFilenameNoFixup
;
780 if ( module
.installBase
.length () > 0 )
781 targetFilenameNoFixup
= module
.installBase
+ SSEP
+ module
.installName
;
783 targetFilenameNoFixup
= module
.installName
;
784 string targetFilename
= MingwModuleHandler::PassThruCacheDirectory (
785 NormalizeFilename ( targetFilenameNoFixup
),
787 out
.push_back ( targetFilename
);
793 MingwBackend::GetInstallTargetFiles (
794 vector
<string
>& out
) const
796 GetNonModuleInstallTargetFiles ( out
);
797 GetModuleInstallTargetFiles ( out
);
801 MingwBackend::OutputInstallTarget ( const string
& sourceFilename
,
802 const string
& targetFilename
,
803 const string
& targetDirectory
)
805 string fullTargetFilename
;
806 if ( targetDirectory
.length () > 0)
807 fullTargetFilename
= targetDirectory
+ SSEP
+ targetFilename
;
809 fullTargetFilename
= targetFilename
;
810 string normalizedTargetFilename
= MingwModuleHandler::PassThruCacheDirectory (
811 NormalizeFilename ( fullTargetFilename
),
813 string normalizedTargetDirectory
= MingwModuleHandler::PassThruCacheDirectory (
814 NormalizeFilename ( targetDirectory
),
818 normalizedTargetFilename
.c_str (),
819 sourceFilename
.c_str (),
820 normalizedTargetDirectory
.c_str () );
824 "\t${cp} %s %s 1>$(NUL)\n",
825 sourceFilename
.c_str (),
826 normalizedTargetFilename
.c_str () );
830 MingwBackend::OutputNonModuleInstallTargets ()
832 for ( size_t i
= 0; i
< ProjectNode
.installfiles
.size (); i
++ )
834 const InstallFile
& installfile
= *ProjectNode
.installfiles
[i
];
835 OutputInstallTarget ( installfile
.GetPath (),
842 MingwBackend::OutputModuleInstallTargets ()
844 for ( size_t i
= 0; i
< ProjectNode
.modules
.size (); i
++ )
846 const Module
& module
= *ProjectNode
.modules
[i
];
847 if ( module
.installName
.length () > 0 )
849 string sourceFilename
= MingwModuleHandler::PassThruCacheDirectory (
850 NormalizeFilename ( module
.GetPath () ),
852 OutputInstallTarget ( sourceFilename
,
854 module
.installBase
);
860 MingwBackend::GetRegistrySourceFiles ()
862 return "bootdata" SSEP
"hivecls.inf "
863 "bootdata" SSEP
"hivedef.inf "
864 "bootdata" SSEP
"hiveinst.inf "
865 "bootdata" SSEP
"hivesft.inf "
866 "bootdata" SSEP
"hivesys.inf";
870 MingwBackend::GetRegistryTargetFiles ()
872 string system32ConfigDirectory
= NormalizeFilename (
873 MingwModuleHandler::PassThruCacheDirectory (
874 "system32" SSEP
"config" SSEP
,
875 installDirectory
) );
876 return system32ConfigDirectory
+ SSEP
"default " +
877 system32ConfigDirectory
+ SSEP
"sam " +
878 system32ConfigDirectory
+ SSEP
"security " +
879 system32ConfigDirectory
+ SSEP
"software " +
880 system32ConfigDirectory
+ SSEP
"system";
884 MingwBackend::OutputRegistryInstallTarget ()
886 string system32ConfigDirectory
= NormalizeFilename (
887 MingwModuleHandler::PassThruCacheDirectory (
888 "system32" SSEP
"config" SSEP
,
889 installDirectory
) );
891 string registrySourceFiles
= GetRegistrySourceFiles ();
892 string registryTargetFiles
= GetRegistryTargetFiles ();
894 "install_registry: %s\n",
895 registryTargetFiles
.c_str () );
897 "%s: %s %s $(MKHIVE_TARGET)\n",
898 registryTargetFiles
.c_str (),
899 registrySourceFiles
.c_str (),
900 system32ConfigDirectory
.c_str () );
902 "\t$(ECHO_MKHIVE)\n" );
904 "\t$(MKHIVE_TARGET) bootdata %s bootdata" SSEP
"hiveinst.inf\n",
905 system32ConfigDirectory
.c_str () );
911 MingwBackend::GenerateInstallTarget ()
913 vector
<string
> vInstallTargetFiles
;
914 GetInstallTargetFiles ( vInstallTargetFiles
);
915 string installTargetFiles
= v2s ( vInstallTargetFiles
, 5 );
916 string registryTargetFiles
= GetRegistryTargetFiles ();
920 installTargetFiles
.c_str (),
921 registryTargetFiles
.c_str () );
922 OutputNonModuleInstallTargets ();
923 OutputModuleInstallTargets ();
924 OutputRegistryInstallTarget ();
930 MingwBackend::GenerateDirectoryTargets ()
932 intermediateDirectory
->CreateRule ( fMakefile
, "" );
933 outputDirectory
->CreateRule ( fMakefile
, "" );
934 installDirectory
->CreateRule ( fMakefile
, "" );