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 ();
283 GenerateRegTestsRunTarget ();
285 for ( i
= 0; i
< iend
; i
++ )
286 v
[i
]->GenerateOtherMacros();
288 for ( i
= 0; i
< iend
; i
++ )
290 MingwModuleHandler
& h
= *v
[i
];
291 h
.GeneratePreconditionDependencies ();
293 h
.GenerateInvocations ();
294 h
.GenerateCleanTarget ();
295 h
.GenerateInstallTarget ();
303 MingwBackend::Process ()
306 DetectPipeSupport ();
310 GenerateGlobalVariables ();
311 GenerateXmlBuildFilesMacro ();
313 GenerateInstallTarget ();
314 GenerateDirectoryTargets ();
315 GenerateDirectories ();
316 UnpackWineResources ();
317 GenerateTestSupportCode ();
318 GenerateProxyMakefiles ();
319 CheckAutomaticDependencies ();
324 MingwBackend::CreateMakefile ()
326 fMakefile
= fopen ( ProjectNode
.makefile
.c_str (), "w" );
328 throw AccessDeniedException ( ProjectNode
.makefile
);
329 MingwModuleHandler::SetBackend ( this );
330 MingwModuleHandler::SetMakefile ( fMakefile
);
331 MingwModuleHandler::SetUsePch ( use_pch
);
335 MingwBackend::CloseMakefile () const
338 fclose ( fMakefile
);
342 MingwBackend::GenerateHeader () const
344 fprintf ( fMakefile
, "# THIS FILE IS AUTOMATICALLY GENERATED, EDIT 'ReactOS.xml' INSTEAD\n\n" );
348 MingwBackend::GenerateIncludesAndDefines ( IfableData
& data
) const
350 string includeParameters
= MingwModuleHandler::GenerateGccIncludeParametersFromVector ( data
.includes
);
351 string defineParameters
= MingwModuleHandler::GenerateGccDefineParametersFromVector ( data
.defines
);
352 return includeParameters
+ " " + defineParameters
;
356 MingwBackend::GenerateProjectCFlagsMacro ( const char* assignmentOperation
,
357 IfableData
& data
) const
362 assignmentOperation
);
366 GenerateIncludesAndDefines ( data
).c_str() );
368 fprintf ( fMakefile
, "\n" );
372 MingwBackend::GenerateGlobalCFlagsAndProperties (
373 const char* assignmentOperation
,
374 IfableData
& data
) const
378 for ( i
= 0; i
< data
.properties
.size(); i
++ )
380 Property
& prop
= *data
.properties
[i
];
381 fprintf ( fMakefile
, "%s := %s\n",
383 prop
.value
.c_str() );
386 if ( data
.includes
.size() || data
.defines
.size() )
388 GenerateProjectCFlagsMacro ( assignmentOperation
,
392 for ( i
= 0; i
< data
.ifs
.size(); i
++ )
394 If
& rIf
= *data
.ifs
[i
];
395 if ( rIf
.data
.defines
.size()
396 || rIf
.data
.includes
.size()
397 || rIf
.data
.ifs
.size() )
401 "ifeq (\"$(%s)\",\"%s\")\n",
402 rIf
.property
.c_str(),
404 GenerateGlobalCFlagsAndProperties (
415 MingwBackend::GenerateProjectGccOptionsMacro ( const char* assignmentOperation
,
416 IfableData
& data
) const
422 "PROJECT_GCCOPTIONS %s",
423 assignmentOperation
);
425 for ( i
= 0; i
< data
.compilerFlags
.size(); i
++ )
430 data
.compilerFlags
[i
]->flag
.c_str() );
433 fprintf ( fMakefile
, "\n" );
437 MingwBackend::GenerateProjectGccOptions (
438 const char* assignmentOperation
,
439 IfableData
& data
) const
443 if ( data
.compilerFlags
.size() )
445 GenerateProjectGccOptionsMacro ( assignmentOperation
,
449 for ( i
= 0; i
< data
.ifs
.size(); i
++ )
451 If
& rIf
= *data
.ifs
[i
];
452 if ( rIf
.data
.compilerFlags
.size()
453 || rIf
.data
.ifs
.size() )
457 "ifeq (\"$(%s)\",\"%s\")\n",
458 rIf
.property
.c_str(),
460 GenerateProjectGccOptions (
471 MingwBackend::GenerateProjectLFLAGS () const
474 for ( size_t i
= 0; i
< ProjectNode
.linkerFlags
.size (); i
++ )
476 LinkerFlag
& linkerFlag
= *ProjectNode
.linkerFlags
[i
];
477 if ( lflags
.length () > 0 )
479 lflags
+= linkerFlag
.flag
;
485 MingwBackend::GenerateGlobalVariables () const
487 GenerateGlobalCFlagsAndProperties ( "=", ProjectNode
.non_if_data
);
488 GenerateProjectGccOptions ( "=", ProjectNode
.non_if_data
);
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" );
500 MingwBackend::IncludeInAllTarget ( const Module
& module
) const
502 if ( MingwModuleHandler::ReferenceObjects ( module
) )
504 if ( module
.type
== BootSector
)
506 if ( module
.type
== Iso
)
508 if ( module
.type
== LiveIso
)
510 if ( module
.type
== Test
)
516 MingwBackend::GenerateAllTarget ( const vector
<MingwModuleHandler
*>& handlers
) const
518 fprintf ( fMakefile
, "all:" );
520 size_t iend
= handlers
.size ();
521 for ( size_t i
= 0; i
< iend
; i
++ )
523 const Module
& module
= handlers
[i
]->module
;
524 if ( IncludeInAllTarget ( module
) )
526 if ( wrap_count
++ == 5 )
527 fprintf ( fMakefile
, " \\\n\t\t" ), wrap_count
= 0;
530 GetTargetMacro(module
).c_str () );
533 fprintf ( fMakefile
, "\n\t\n\n" );
537 MingwBackend::GetBuildToolDependencies () const
540 for ( size_t i
= 0; i
< ProjectNode
.modules
.size (); i
++ )
542 Module
& module
= *ProjectNode
.modules
[i
];
543 if ( module
.type
== BuildTool
)
545 if ( dependencies
.length () > 0 )
547 dependencies
+= module
.GetDependencyPath ();
554 MingwBackend::GenerateInitTarget () const
558 GetBuildToolDependencies ().c_str () );
559 fprintf ( fMakefile
, "\n" );
563 MingwBackend::GenerateRegTestsRunTarget () const
566 "REGTESTS_RUN_TARGET = regtests.dll\n" );
568 "$(REGTESTS_RUN_TARGET):\n" );
570 "\t$(cp) $(REGTESTS_TARGET) $(REGTESTS_RUN_TARGET)\n" );
571 fprintf ( fMakefile
, "\n" );
575 MingwBackend::GenerateXmlBuildFilesMacro() const
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
++ )
584 XMLInclude
& xmlbuildfile
= *ProjectNode
.xmlbuildfiles
[i
];
585 if ( !xmlbuildfile
.fileExists
)
587 numberOfExistingFiles
++;
588 if ( xmlbuildFilenames
.length () > 0 )
589 xmlbuildFilenames
+= " ";
590 xmlbuildFilenames
+= NormalizeFilename ( xmlbuildfile
.topIncludeFilename
);
591 if ( numberOfExistingFiles
% 5 == 4 || i
== ProjectNode
.xmlbuildfiles
.size () - 1 )
595 xmlbuildFilenames
.c_str ());
596 if ( i
== ProjectNode
.xmlbuildfiles
.size () - 1 )
598 fprintf ( fMakefile
, "\n" );
604 xmlbuildFilenames
.c_str () );
606 xmlbuildFilenames
.resize ( 0 );
608 numberOfExistingFiles
++;
610 fprintf ( fMakefile
, "\n" );
614 MingwBackend::GetBin2ResExecutable ()
616 return NormalizeFilename ( Environment::GetOutputPath () + SSEP
+ "tools/bin2res/bin2res" + EXEPOSTFIX
);
620 MingwBackend::UnpackWineResources ()
622 printf ( "Unpacking WINE resources..." );
623 WineResource
wineResource ( ProjectNode
,
624 GetBin2ResExecutable () );
625 wineResource
.UnpackResources ( verbose
);
630 MingwBackend::GenerateTestSupportCode ()
632 printf ( "Generating test support code..." );
633 TestSupportCode
testSupportCode ( ProjectNode
);
634 testSupportCode
.GenerateTestSupportCode ( verbose
);
639 MingwBackend::GenerateProxyMakefiles ()
641 printf ( "Generating proxy makefiles..." );
642 ProxyMakefile
proxyMakefile ( ProjectNode
);
643 proxyMakefile
.GenerateProxyMakefiles ( verbose
);
648 MingwBackend::CheckAutomaticDependencies ()
650 printf ( "Checking automatic dependencies..." );
651 AutomaticDependency
automaticDependency ( ProjectNode
);
652 automaticDependency
.Process ();
653 automaticDependency
.CheckAutomaticDependencies ( verbose
);
658 MingwBackend::IncludeDirectoryTarget ( const string
& directory
) const
660 if ( directory
== "$(INTERMEDIATE)" SSEP
"tools")
667 MingwBackend::GenerateDirectories ()
669 printf ( "Creating directories..." );
670 intermediateDirectory
->GenerateTree ( "", verbose
);
671 outputDirectory
->GenerateTree ( "", verbose
);
672 installDirectory
->GenerateTree ( "", verbose
);
677 MingwBackend::TryToDetectThisCompiler ( const string
& compiler
)
679 string command
= ssprintf (
683 int exitcode
= system ( command
.c_str () );
684 return (exitcode
== 0);
688 MingwBackend::DetectCompiler ()
690 printf ( "Detecting compiler..." );
692 bool detectedCompiler
= false;
693 const string
& ROS_PREFIXValue
= Environment::GetVariable ( "ROS_PREFIX" );
694 if ( ROS_PREFIXValue
.length () > 0 )
696 compilerCommand
= ROS_PREFIXValue
+ "-gcc";
697 detectedCompiler
= TryToDetectThisCompiler ( compilerCommand
);
700 if ( !detectedCompiler
)
702 compilerCommand
= "gcc";
703 detectedCompiler
= TryToDetectThisCompiler ( compilerCommand
);
706 if ( !detectedCompiler
)
708 compilerCommand
= "mingw32-gcc";
709 detectedCompiler
= TryToDetectThisCompiler ( compilerCommand
);
711 if ( detectedCompiler
)
712 printf ( "detected (%s)\n", compilerCommand
.c_str () );
714 printf ( "not detected\n" );
718 MingwBackend::DetectPipeSupport ()
720 printf ( "Detecting compiler -pipe support..." );
722 string pipe_detection
= "tools" SSEP
"rbuild" SSEP
"backend" SSEP
"mingw" SSEP
"pipe_detection.c";
723 string pipe_detectionObjectFilename
= ReplaceExtension ( pipe_detection
,
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 (),
731 int exitcode
= system ( command
.c_str () );
732 FILE* f
= fopen ( pipe_detectionObjectFilename
.c_str (), "rb" );
735 usePipe
= (exitcode
== 0);
737 unlink ( pipe_detectionObjectFilename
.c_str () );
743 printf ( "detected\n" );
745 printf ( "not detected\n" );
749 MingwBackend::DetectPCHSupport ()
751 printf ( "Detecting compiler pre-compiled header support..." );
753 string path
= "tools" SSEP
"rbuild" SSEP
"backend" SSEP
"mingw" SSEP
"pch_detection.h";
754 string cmd
= ssprintf (
756 compilerCommand
.c_str (),
759 system ( cmd
.c_str () );
762 FILE* f
= fopen ( path
.c_str (), "rb" );
767 unlink ( path
.c_str () );
773 printf ( "detected\n" );
775 printf ( "not detected\n" );
779 MingwBackend::GetNonModuleInstallTargetFiles (
780 vector
<string
>& out
) const
782 for ( size_t i
= 0; i
< ProjectNode
.installfiles
.size (); i
++ )
784 const InstallFile
& installfile
= *ProjectNode
.installfiles
[i
];
785 string targetFilenameNoFixup
= installfile
.base
+ SSEP
+ installfile
.newname
;
786 string targetFilename
= MingwModuleHandler::PassThruCacheDirectory (
787 NormalizeFilename ( targetFilenameNoFixup
),
789 out
.push_back ( targetFilename
);
794 MingwBackend::GetModuleInstallTargetFiles (
795 vector
<string
>& out
) const
797 for ( size_t i
= 0; i
< ProjectNode
.modules
.size (); i
++ )
799 const Module
& module
= *ProjectNode
.modules
[i
];
800 if ( module
.installName
.length () > 0 )
802 string targetFilenameNoFixup
;
803 if ( module
.installBase
.length () > 0 )
804 targetFilenameNoFixup
= module
.installBase
+ SSEP
+ module
.installName
;
806 targetFilenameNoFixup
= module
.installName
;
807 string targetFilename
= MingwModuleHandler::PassThruCacheDirectory (
808 NormalizeFilename ( targetFilenameNoFixup
),
810 out
.push_back ( targetFilename
);
816 MingwBackend::GetInstallTargetFiles (
817 vector
<string
>& out
) const
819 GetNonModuleInstallTargetFiles ( out
);
820 GetModuleInstallTargetFiles ( out
);
824 MingwBackend::OutputInstallTarget ( const string
& sourceFilename
,
825 const string
& targetFilename
,
826 const string
& targetDirectory
)
828 string fullTargetFilename
;
829 if ( targetDirectory
.length () > 0)
830 fullTargetFilename
= targetDirectory
+ SSEP
+ targetFilename
;
832 fullTargetFilename
= targetFilename
;
833 string normalizedTargetFilename
= MingwModuleHandler::PassThruCacheDirectory (
834 NormalizeFilename ( fullTargetFilename
),
836 string normalizedTargetDirectory
= MingwModuleHandler::PassThruCacheDirectory (
837 NormalizeFilename ( targetDirectory
),
841 normalizedTargetFilename
.c_str (),
842 sourceFilename
.c_str (),
843 normalizedTargetDirectory
.c_str () );
847 "\t${cp} %s %s 1>$(NUL)\n",
848 sourceFilename
.c_str (),
849 normalizedTargetFilename
.c_str () );
853 MingwBackend::OutputNonModuleInstallTargets ()
855 for ( size_t i
= 0; i
< ProjectNode
.installfiles
.size (); i
++ )
857 const InstallFile
& installfile
= *ProjectNode
.installfiles
[i
];
858 OutputInstallTarget ( installfile
.GetPath (),
865 MingwBackend::OutputModuleInstallTargets ()
867 for ( size_t i
= 0; i
< ProjectNode
.modules
.size (); i
++ )
869 const Module
& module
= *ProjectNode
.modules
[i
];
870 if ( module
.installName
.length () > 0 )
872 string sourceFilename
= MingwModuleHandler::PassThruCacheDirectory (
873 NormalizeFilename ( module
.GetPath () ),
875 OutputInstallTarget ( sourceFilename
,
877 module
.installBase
);
883 MingwBackend::GetRegistrySourceFiles ()
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";
893 MingwBackend::GetRegistryTargetFiles ()
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";
907 MingwBackend::OutputRegistryInstallTarget ()
909 string system32ConfigDirectory
= NormalizeFilename (
910 MingwModuleHandler::PassThruCacheDirectory (
911 "system32" SSEP
"config" SSEP
,
912 installDirectory
) );
914 string registrySourceFiles
= GetRegistrySourceFiles ();
915 string registryTargetFiles
= GetRegistryTargetFiles ();
917 "install_registry: %s\n",
918 registryTargetFiles
.c_str () );
920 "%s: %s %s $(MKHIVE_TARGET)\n",
921 registryTargetFiles
.c_str (),
922 registrySourceFiles
.c_str (),
923 system32ConfigDirectory
.c_str () );
925 "\t$(ECHO_MKHIVE)\n" );
927 "\t$(MKHIVE_TARGET) bootdata %s bootdata" SSEP
"hiveinst.inf\n",
928 system32ConfigDirectory
.c_str () );
934 MingwBackend::GenerateInstallTarget ()
936 vector
<string
> vInstallTargetFiles
;
937 GetInstallTargetFiles ( vInstallTargetFiles
);
938 string installTargetFiles
= v2s ( vInstallTargetFiles
, 5 );
939 string registryTargetFiles
= GetRegistryTargetFiles ();
943 installTargetFiles
.c_str (),
944 registryTargetFiles
.c_str () );
945 OutputNonModuleInstallTargets ();
946 OutputModuleInstallTargets ();
947 OutputRegistryInstallTarget ();
953 MingwBackend::GenerateDirectoryTargets ()
955 intermediateDirectory
->CreateRule ( fMakefile
, "" );
956 outputDirectory
->CreateRule ( fMakefile
, "" );
957 installDirectory
->CreateRule ( fMakefile
, "" );