2 * Copyright (C) 2005 Casper S. Hornstrup
3 * 2006 Christoph von Wittich
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 #include "../../pch.h"
23 #include "modulehandler.h"
27 #define pclose _pclose
35 typedef set
<string
> set_string
;
37 const struct ModuleHandlerInformations ModuleHandlerInformations
[] = {
38 { HostTrue
, "", "", "" }, // BuildTool
39 { HostFalse
, "", "", "" }, // StaticLibrary
40 { HostFalse
, "", "", "" }, // ObjectLibrary
41 { HostFalse
, "", "", "$(LDFLAG_DRIVER)" }, // Kernel
42 { HostFalse
, "", "", "$(LDFLAG_DRIVER)" }, // KernelModeDLL
43 { HostFalse
, "-D__NTDRIVER__", "", "$(LDFLAG_DRIVER)" }, // KernelModeDriver
44 { HostFalse
, "", "", "$(LDFLAG_DLL)" }, // NativeDLL
45 { HostFalse
, "-D__NTAPP__", "", "$(LDFLAG_NATIVE)" }, // NativeCUI
46 { HostFalse
, "", "", "$(LDFLAG_DLL)" }, // Win32DLL
47 { HostFalse
, "", "", "$(LDFLAG_DLL)" }, // Win32OCX
48 { HostFalse
, "", "", "$(LDFLAG_CONSOLE)" }, // Win32CUI
49 { HostFalse
, "", "", "$(LDFLAG_WINDOWS)" }, // Win32GUI
50 { HostFalse
, "", "", "" }, // BootLoader
51 { HostFalse
, "", "-f bin", "" }, // BootSector
52 { HostFalse
, "", "", "" }, // Iso
53 { HostFalse
, "", "", "" }, // LiveIso
54 { HostFalse
, "", "", "" }, // Test
55 { HostFalse
, "", "", "" }, // RpcServer
56 { HostFalse
, "", "", "" }, // RpcClient
57 { HostFalse
, "", "", "" }, // Alias
58 { HostFalse
, "", "", "" }, // BootProgram
59 { HostFalse
, "", "", "$(LDFLAG_WINDOWS)" }, // Win32SCR
60 { HostFalse
, "", "", "" }, // IdlHeader
61 { HostFalse
, "", "", "" }, // IdlInterface
62 { HostFalse
, "", "", "" }, // EmbeddedTypeLib
63 { HostFalse
, "", "", "" }, // ElfExecutable
64 { HostFalse
, "", "", "" }, // RpcProxy
65 { HostTrue
, "", "", "" }, // HostStaticLibrary
66 { HostFalse
, "", "", "" }, // Cabinet
67 { HostFalse
, "", "", "$(LDFLAG_DLL)" }, // KeyboardLayout
68 { HostFalse
, "", "", "" }, // MessageHeader
71 static std::string mscPath
;
72 static std::string mslinkPath
;
75 MingwBackend::GetFullPath ( const FileLocation
& file
) const
77 MingwModuleHandler::PassThruCacheDirectory ( &file
);
80 switch ( file
.directory
)
85 case IntermediateDirectory
:
86 directory
= "$(INTERMEDIATE)";
89 directory
= "$(OUTPUT)";
91 case InstallDirectory
:
92 directory
= "$(INSTALL)";
94 case TemporaryDirectory
:
95 directory
= "$(TEMPORARY)";
98 throw InvalidOperationException ( __FILE__
,
100 "Invalid directory %d.",
104 if ( file
.relative_path
.length () > 0 )
106 if ( directory
.length () > 0 )
108 directory
+= file
.relative_path
;
114 MingwBackend::GetFullName ( const FileLocation
& file
) const
117 switch ( file
.directory
)
119 case SourceDirectory
:
122 case IntermediateDirectory
:
123 directory
= "$(INTERMEDIATE)";
125 case OutputDirectory
:
126 directory
= "$(OUTPUT)";
128 case InstallDirectory
:
129 directory
= "$(INSTALL)";
131 case TemporaryDirectory
:
132 directory
= "$(TEMPORARY)";
135 throw InvalidOperationException ( __FILE__
,
137 "Invalid directory %d.",
141 if ( file
.relative_path
.length () > 0 )
143 if ( directory
.length () > 0 )
145 directory
+= file
.relative_path
;
148 if ( directory
.length () > 0 )
151 return directory
+ file
.name
;
155 v2s ( const Backend
* backend
, const vector
<FileLocation
>& files
, int wrap_at
)
161 for ( size_t i
= 0; i
< files
.size(); i
++ )
163 const FileLocation
& file
= files
[i
];
164 if ( wrap_at
> 0 && wrap_count
++ == wrap_at
)
171 s
+= backend
->GetFullName ( file
);
178 v2s ( const string_list
& v
, int wrap_at
)
184 for ( size_t i
= 0; i
< v
.size(); i
++ )
188 if ( wrap_at
> 0 && wrap_count
++ == wrap_at
)
201 static class MingwFactory
: public Backend::Factory
204 MingwFactory() : Factory ( "mingw", "Minimalist GNU Win32" ) {}
205 Backend
* operator() ( Project
& project
,
206 Configuration
& configuration
)
208 return new MingwBackend ( project
,
214 MingwBackend::MingwBackend ( Project
& project
,
215 Configuration
& configuration
)
216 : Backend ( project
, configuration
),
217 manualBinutilsSetting( false ),
218 intermediateDirectory ( new Directory ( "" ) ),
219 outputDirectory ( new Directory ( "" ) ),
220 installDirectory ( new Directory ( "" ) )
225 MingwBackend::~MingwBackend()
227 delete intermediateDirectory
;
228 delete outputDirectory
;
229 delete installDirectory
;
233 MingwBackend::AddDirectoryTarget ( const string
& directory
,
234 Directory
* directoryTree
)
236 if ( directory
.length () > 0)
237 directoryTree
->Add ( directory
.c_str() );
238 return directoryTree
->name
;
242 MingwBackend::CanEnablePreCompiledHeaderSupportForModule ( const Module
& module
)
244 if ( !configuration
.CompilationUnitsEnabled
)
247 const vector
<CompilationUnit
*>& compilationUnits
= module
.non_if_data
.compilationUnits
;
249 for ( i
= 0; i
< compilationUnits
.size (); i
++ )
251 CompilationUnit
& compilationUnit
= *compilationUnits
[i
];
252 if ( compilationUnit
.GetFiles ().size () != 1 )
259 MingwBackend::ProcessModules ()
261 printf ( "Processing modules..." );
263 vector
<MingwModuleHandler
*> v
;
266 for ( std::map
<std::string
, Module
*>::iterator p
= ProjectNode
.modules
.begin (); p
!= ProjectNode
.modules
.end (); ++ p
)
267 fprintf ( fMakefile
, "%s_TYPE:=%u\n", p
->second
->name
.c_str(), p
->second
->type
);
269 for ( std::map
<std::string
, Module
*>::iterator p
= ProjectNode
.modules
.begin (); p
!= ProjectNode
.modules
.end (); ++ p
)
271 Module
& module
= *p
->second
;
272 if ( !module
.enabled
)
274 MingwModuleHandler
* h
= MingwModuleHandler::InstanciateHandler (
277 h
->AddImplicitLibraries ( module
);
278 if ( use_pch
&& CanEnablePreCompiledHeaderSupportForModule ( module
) )
279 h
->EnablePreCompiledHeaderSupport ();
283 size_t iend
= v
.size ();
285 for ( i
= 0; i
< iend
; i
++ )
286 v
[i
]->GenerateSourceMacro();
287 for ( i
= 0; i
< iend
; i
++ )
288 v
[i
]->GenerateObjectMacro();
289 fprintf ( fMakefile
, "\n" );
290 for ( i
= 0; i
< iend
; i
++ )
291 v
[i
]->GenerateTargetMacro();
292 fprintf ( fMakefile
, "\n" );
294 GenerateAllTarget ( v
);
295 GenerateRegTestsRunTarget ();
297 for ( i
= 0; i
< iend
; i
++ )
298 v
[i
]->GenerateOtherMacros();
300 for ( i
= 0; i
< iend
; i
++ )
302 MingwModuleHandler
& h
= *v
[i
];
303 h
.GeneratePreconditionDependencies ();
305 h
.GenerateInvocations ();
306 h
.GenerateCleanTarget ();
307 h
.GenerateInstallTarget ();
308 h
.GenerateDependsTarget ();
316 MingwBackend::Process ()
318 if ( configuration
.CheckDependenciesForModuleOnly
)
319 CheckAutomaticDependenciesForModuleOnly ();
325 MingwBackend::CheckAutomaticDependenciesForModuleOnly ()
327 if ( configuration
.Dependencies
== AutomaticDependencies
)
329 Module
* module
= ProjectNode
.LocateModule ( configuration
.CheckDependenciesForModuleOnlyModule
);
330 if ( module
== NULL
)
332 printf ( "Module '%s' does not exist\n",
333 configuration
.CheckDependenciesForModuleOnlyModule
.c_str () );
337 printf ( "Checking automatic dependencies for module '%s'...",
338 module
->name
.c_str () );
339 AutomaticDependency
automaticDependency ( ProjectNode
);
340 automaticDependency
.CheckAutomaticDependenciesForModule ( *module
,
341 configuration
.Verbose
);
347 MingwBackend::ProcessNormal ()
349 assert(sizeof(ModuleHandlerInformations
)/sizeof(ModuleHandlerInformations
[0]) == TypeDontCare
);
353 DetectNetwideAssembler ();
354 DetectPipeSupport ();
358 GenerateGlobalVariables ();
359 GenerateXmlBuildFilesMacro ();
361 GenerateInstallTarget ();
362 GenerateTestTarget ();
363 GenerateDirectoryTargets ();
364 GenerateDirectories ();
365 GenerateTestSupportCode ();
366 GenerateCompilationUnitSupportCode ();
368 GenerateInstallerFileList();
369 GenerateProxyMakefiles ();
370 CheckAutomaticDependencies ();
375 MingwBackend::GenerateInstallerFileList()
377 this->ProjectNode
.GenerateInstallerFileList();
381 MingwBackend::CreateMakefile ()
383 fMakefile
= fopen ( ProjectNode
.makefile
.c_str (), "w" );
385 throw AccessDeniedException ( ProjectNode
.makefile
);
386 MingwModuleHandler::SetBackend ( this );
387 MingwModuleHandler::SetMakefile ( fMakefile
);
391 MingwBackend::CloseMakefile () const
394 fclose ( fMakefile
);
398 MingwBackend::GenerateHeader () const
400 fprintf ( fMakefile
, "# THIS FILE IS AUTOMATICALLY GENERATED, EDIT '%s' INSTEAD\n\n",
401 ProjectNode
.GetProjectFilename ().c_str () );
405 MingwBackend::GenerateGlobalProperties (
406 const char* assignmentOperation
,
407 const IfableData
& data
) const
409 for ( std::map
<std::string
, Property
*>::const_iterator p
= data
.properties
.begin(); p
!= data
.properties
.end(); ++ p
)
411 Property
& prop
= *p
->second
;
413 if (!prop
.isInternal
)
415 fprintf ( fMakefile
, "%s := %s\n",
417 prop
.value
.c_str() );
423 MingwBackend::GenerateProjectLDFLAGS () const
426 for ( size_t i
= 0; i
< ProjectNode
.linkerFlags
.size (); i
++ )
428 LinkerFlag
& linkerFlag
= *ProjectNode
.linkerFlags
[i
];
429 if ( ldflags
.length () > 0 )
431 ldflags
+= linkerFlag
.flag
;
437 MingwBackend::GenerateGlobalVariables () const
439 fputs ( "include tools$(SEP)rbuild$(SEP)backend$(SEP)mingw$(SEP)rules.mak\n", fMakefile
);
440 fprintf ( fMakefile
, "include tools$(SEP)rbuild$(SEP)backend$(SEP)mingw$(SEP)linkers$(SEP)%s.mak\n", ProjectNode
.GetLinkerSet ().c_str () );
441 fprintf ( fMakefile
, "include tools$(SEP)rbuild$(SEP)backend$(SEP)mingw$(SEP)compilers$(SEP)%s.mak\n", ProjectNode
.GetCompilerSet ().c_str () );
443 if ( mscPath
.length() )
444 fprintf ( fMakefile
, "export RBUILD_CL_PATH=%s\n", mscPath
.c_str () );
446 if ( mslinkPath
.length() )
447 fprintf ( fMakefile
, "export RBUILD_LINK_PATH=%s\n", mslinkPath
.c_str () );
449 if ( configuration
.Dependencies
== FullDependencies
)
452 "ifeq ($(ROS_BUILDDEPS),)\n"
453 "ROS_BUILDDEPS:=%s\n"
460 compilerPrefix
.c_str () );
463 nasmCommand
.c_str () );
465 GenerateGlobalProperties ( "=", ProjectNode
.non_if_data
);
467 if ( ProjectNode
.configuration
.Compiler
== GnuGcc
)
469 fprintf ( fMakefile
, "ifneq ($(OARCH),)\n" );
470 fprintf ( fMakefile
, "PROJECT_ASFLAGS += -march=$(OARCH)\n" );
471 fprintf ( fMakefile
, "PROJECT_CFLAGS += -march=$(OARCH)\n" );
472 fprintf ( fMakefile
, "PROJECT_CXXFLAGS += -march=$(OARCH)\n" );
473 fprintf ( fMakefile
, "endif\n" );
474 fprintf ( fMakefile
, "ifneq ($(TUNE),)\n" );
475 fprintf ( fMakefile
, "PROJECT_CFLAGS += -mtune=$(TUNE)\n" );
476 fprintf ( fMakefile
, "PROJECT_CXXFLAGS += -mtune=$(TUNE)\n" );
477 fprintf ( fMakefile
, "endif\n" );
481 fprintf ( fMakefile
, "PROJECT_CFLAGS += -pipe\n" );
482 fprintf ( fMakefile
, "PROJECT_CXXFLAGS += -pipe\n" );
483 fprintf ( fMakefile
, "PROJECT_ASFLAGS += -pipe\n" );
486 // Would be nice to have our own C++ runtime
487 fputs ( "BUILTIN_CXXINCLUDES+= $(TARGET_CPPFLAGS)\n", fMakefile
);
489 fprintf ( fMakefile
, "PROJECT_CCLIBS := \"$(shell ${TARGET_CC} -print-libgcc-file-name)\"\n" );
490 fprintf ( fMakefile
, "PROJECT_CXXLIBS := \"$(shell ${TARGET_CPP} -print-file-name=libstdc++.a)\" \"$(shell ${TARGET_CPP} -print-libgcc-file-name)\" \"$(shell ${TARGET_CPP} -print-file-name=libmingw32.a)\" \"$(shell ${TARGET_CPP} -print-file-name=libmingwex.a)\" " );
492 /* hack to get libgcc_eh.a, should check mingw version or something */
493 if (Environment::GetArch() == "amd64")
494 fprintf ( fMakefile
, " \"$(shell ${TARGET_CPP} -print-file-name=libgcc_eh.a)\"" );
495 /* hack to get _get_output_format, needed by libmingwex */
496 else if (Environment::GetArch() == "i386")
497 fprintf ( fMakefile
, "\"$(shell ${TARGET_CPP} -print-file-name=ofmt_stub.a)\"");
498 fprintf ( fMakefile
,"\n");
500 MingwModuleHandler::GenerateParameters ( "PROJECT", "+=", ProjectNode
.non_if_data
);
501 MingwModuleHandler::GenerateParameters ( "PROJECT_HOST", "+=", ProjectNode
.host_non_if_data
);
503 fprintf ( fMakefile
, "PROJECT_LDFLAGS := %s\n", GenerateProjectLDFLAGS ().c_str () );
505 // TODO: use symbolic names for module types
506 for ( size_t i
= 0; i
< sizeof(ModuleHandlerInformations
) / sizeof(ModuleHandlerInformations
[0]); ++ i
)
508 if ( ModuleHandlerInformations
[i
].cflags
&& ModuleHandlerInformations
[i
].cflags
[0] )
511 "MODULETYPE%d_%sFLAGS:=%s\n",
514 ModuleHandlerInformations
[i
].cflags
);
517 if ( ModuleHandlerInformations
[i
].nasmflags
&& ModuleHandlerInformations
[i
].nasmflags
[0] )
520 "MODULETYPE%d_%sFLAGS:=%s\n",
523 ModuleHandlerInformations
[i
].nasmflags
);
526 if ( ModuleHandlerInformations
[i
].linkerflags
&& ModuleHandlerInformations
[i
].linkerflags
[0] )
529 "MODULETYPE%d_%sFLAGS:=%s\n",
532 ModuleHandlerInformations
[i
].linkerflags
);
537 "MODULETYPE%d_KMODE:=yes\n",
541 "MODULETYPE%d_KMODE:=yes\n",
542 (int)KernelModeDLL
);
545 "MODULETYPE%d_KMODE:=yes\n",
546 (int)KernelModeDriver
);
548 fprintf ( fMakefile
, "\n" );
552 MingwBackend::IncludeInAllTarget ( const Module
& module
) const
554 if ( MingwModuleHandler::ReferenceObjects ( module
) )
556 if ( module
.type
== BootSector
)
558 if ( module
.type
== Iso
)
560 if ( module
.type
== LiveIso
)
562 if ( module
.type
== Test
)
564 if ( module
.type
== Alias
)
570 MingwBackend::GenerateAllTarget ( const vector
<MingwModuleHandler
*>& handlers
) const
572 fprintf ( fMakefile
, "all:" );
574 size_t iend
= handlers
.size ();
575 for ( size_t i
= 0; i
< iend
; i
++ )
577 const Module
& module
= handlers
[i
]->module
;
578 if ( IncludeInAllTarget ( module
) )
580 if ( wrap_count
++ == 5 )
581 fprintf ( fMakefile
, " \\\n\t\t" ), wrap_count
= 0;
584 GetTargetMacro(module
).c_str () );
587 fprintf ( fMakefile
, "\n\t\n\n" );
591 MingwBackend::GenerateRegTestsRunTarget () const
594 "REGTESTS_RUN_TARGET = regtests.dll\n" );
596 "$(REGTESTS_RUN_TARGET): $(REGTESTS_TARGET)\n" );
598 "\t$(cp) $(REGTESTS_TARGET) $(REGTESTS_RUN_TARGET)\n" );
599 fprintf ( fMakefile
, "\n" );
603 MingwBackend::GenerateXmlBuildFilesMacro() const
606 "XMLBUILDFILES = %s \\\n",
607 ProjectNode
.GetProjectFilename ().c_str () );
608 string xmlbuildFilenames
;
609 int numberOfExistingFiles
= 0;
611 time_t SystemTime
, lastWriteTime
;
613 for ( size_t i
= 0; i
< ProjectNode
.xmlbuildfiles
.size (); i
++ )
615 XMLInclude
& xmlbuildfile
= *ProjectNode
.xmlbuildfiles
[i
];
616 if ( !xmlbuildfile
.fileExists
)
618 numberOfExistingFiles
++;
619 if ( xmlbuildFilenames
.length () > 0 )
620 xmlbuildFilenames
+= " ";
622 FILE* f
= fopen ( xmlbuildfile
.topIncludeFilename
.c_str (), "rb" );
624 throw FileNotFoundException ( NormalizeFilename ( xmlbuildfile
.topIncludeFilename
) );
626 if ( fstat ( fileno ( f
), &statbuf
) != 0 )
629 throw AccessDeniedException ( NormalizeFilename ( xmlbuildfile
.topIncludeFilename
) );
632 lastWriteTime
= statbuf
.st_mtime
;
633 SystemTime
= time(NULL
);
635 if (SystemTime
!= -1)
637 if (difftime (lastWriteTime
, SystemTime
) > 0)
638 throw InvalidDateException ( NormalizeFilename ( xmlbuildfile
.topIncludeFilename
) );
643 xmlbuildFilenames
+= NormalizeFilename ( xmlbuildfile
.topIncludeFilename
);
644 if ( numberOfExistingFiles
% 5 == 4 || i
== ProjectNode
.xmlbuildfiles
.size () - 1 )
648 xmlbuildFilenames
.c_str ());
649 if ( i
== ProjectNode
.xmlbuildfiles
.size () - 1 )
651 fprintf ( fMakefile
, "\n" );
658 xmlbuildFilenames
.resize ( 0 );
660 numberOfExistingFiles
++;
662 fprintf ( fMakefile
, "\n" );
666 MingwBackend::GenerateTestSupportCode ()
668 printf ( "Generating test support code..." );
669 TestSupportCode
testSupportCode ( ProjectNode
);
670 testSupportCode
.GenerateTestSupportCode ( configuration
.Verbose
);
675 MingwBackend::GenerateCompilationUnitSupportCode ()
677 if ( configuration
.CompilationUnitsEnabled
)
679 printf ( "Generating compilation unit support code..." );
680 CompilationUnitSupportCode
compilationUnitSupportCode ( ProjectNode
);
681 compilationUnitSupportCode
.Generate ( configuration
.Verbose
);
687 MingwBackend::GenerateSysSetup ()
689 printf ( "Generating syssetup.inf..." );
690 SysSetupGenerator
sysSetupGenerator ( ProjectNode
);
691 sysSetupGenerator
.Generate ();
696 MingwBackend::GetProxyMakefileTree () const
698 if ( configuration
.GenerateProxyMakefilesInSourceTree
)
701 return Environment::GetOutputPath ();
705 MingwBackend::GenerateProxyMakefiles ()
707 printf ( "Generating proxy makefiles..." );
708 ProxyMakefile
proxyMakefile ( ProjectNode
);
709 proxyMakefile
.GenerateProxyMakefiles ( configuration
.Verbose
,
710 GetProxyMakefileTree () );
715 MingwBackend::CheckAutomaticDependencies ()
717 if ( configuration
.Dependencies
== AutomaticDependencies
)
719 printf ( "Checking automatic dependencies..." );
720 AutomaticDependency
automaticDependency ( ProjectNode
);
721 automaticDependency
.CheckAutomaticDependencies ( configuration
.Verbose
);
727 MingwBackend::GenerateDirectories ()
729 printf ( "Creating directories..." );
730 intermediateDirectory
->GenerateTree ( IntermediateDirectory
, configuration
.Verbose
);
731 outputDirectory
->GenerateTree ( OutputDirectory
, configuration
.Verbose
);
732 if ( !configuration
.MakeHandlesInstallDirectories
)
733 installDirectory
->GenerateTree ( InstallDirectory
, configuration
.Verbose
);
738 MingwBackend::TryToDetectThisCompiler ( const string
& compiler
)
740 string command
= ssprintf (
742 FixSeparatorForSystemCommand(compiler
).c_str (),
745 int exitcode
= system ( command
.c_str () );
746 return (bool) (exitcode
== 0);
750 MingwBackend::DetectCompiler ()
752 printf ( "Detecting compiler..." );
754 bool detectedCompiler
= false;
755 bool supportedCompiler
= false;
756 string compilerVersion
;
758 if ( ProjectNode
.configuration
.Compiler
== GnuGcc
)
760 const string
& TARGET_CCValue
= Environment::GetVariable ( "TARGET_CC" );
761 const string
& ROS_PREFIXValue
= Environment::GetVariable ( "ROS_PREFIX" );
763 if ( TARGET_CCValue
.length () > 0 )
766 compilerCommand
= TARGET_CCValue
;
767 detectedCompiler
= TryToDetectThisCompiler ( compilerCommand
);
770 if ( !detectedCompiler
)
772 if ( ROS_PREFIXValue
.length () > 0 )
774 compilerPrefix
= ROS_PREFIXValue
;
775 compilerCommand
= compilerPrefix
+ "-gcc";
776 detectedCompiler
= TryToDetectThisCompiler ( compilerCommand
);
780 if ( !detectedCompiler
)
783 compilerCommand
= "gcc";
784 detectedCompiler
= TryToDetectThisCompiler ( compilerCommand
);
787 if ( !detectedCompiler
)
789 compilerPrefix
= "mingw32";
790 compilerCommand
= compilerPrefix
+ "-gcc";
791 detectedCompiler
= TryToDetectThisCompiler ( compilerCommand
);
794 if ( detectedCompiler
)
795 compilerVersion
= GetCompilerVersion ( compilerCommand
);
797 supportedCompiler
= IsSupportedCompilerVersion ( compilerVersion
);
799 else if ( ProjectNode
.configuration
.Compiler
== MicrosoftC
)
801 compilerCommand
= "cl";
802 detectedCompiler
= DetectMicrosoftCompiler ( compilerVersion
, mscPath
);
803 supportedCompiler
= true; // TODO
806 if ( detectedCompiler
)
808 if ( supportedCompiler
)
809 printf ( "detected (%s %s)\n", compilerCommand
.c_str (), compilerVersion
.c_str() );
812 printf ( "detected (%s), but with unsupported version (%s)\n",
813 compilerCommand
.c_str (),
814 compilerVersion
.c_str () );
815 throw UnsupportedBuildToolException ( compilerCommand
, compilerVersion
);
819 printf ( "not detected\n" );
824 MingwBackend::TryToDetectThisNetwideAssembler ( const string
& assembler
)
826 string command
= ssprintf (
828 FixSeparatorForSystemCommand(assembler
).c_str (),
831 int exitcode
= system ( command
.c_str () );
832 return (bool) (exitcode
== 0);
836 MingwBackend::GetVersionString ( const string
& versionCommand
)
843 fp
= popen ( versionCommand
.c_str () , "r" );
846 ( feof ( fp
) == 0 &&
847 ( ( ch
= fgetc( fp
) ) != -1 ) );
850 buffer
[i
] = (char) ch
;
855 char separators
[] = " ()\n";
857 char *prevtoken
= NULL
;
861 token
= strtok ( buffer
, separators
);
862 while ( token
!= NULL
)
865 version
= string( prevtoken
);
866 if ( (newline
= version
.find('\n')) != std::string::npos
)
867 version
.erase(newline
, 1);
868 if ( version
.find('.') != std::string::npos
)
870 token
= strtok ( NULL
, separators
);
876 MingwBackend::GetNetwideAssemblerVersion ( const string
& nasmCommand
)
878 string versionCommand
;
879 if ( nasmCommand
.find("yasm") != std::string::npos
)
881 versionCommand
= ssprintf ( "%s --version",
882 nasmCommand
.c_str (),
888 versionCommand
= ssprintf ( "%s -v",
889 nasmCommand
.c_str (),
893 return GetVersionString( versionCommand
);
897 MingwBackend::GetCompilerVersion ( const string
& compilerCommand
)
899 string versionCommand
= ssprintf ( "%s --version",
900 compilerCommand
.c_str (),
903 return GetVersionString( versionCommand
);
907 MingwBackend::GetBinutilsVersion ( const string
& binutilsCommand
)
909 string versionCommand
= ssprintf ( "%s -v",
910 binutilsCommand
.c_str (),
913 return GetVersionString( versionCommand
);
917 MingwBackend::IsSupportedCompilerVersion ( const string
& compilerVersion
)
919 if ( strcmp ( compilerVersion
.c_str (), "4.4.2") < 0 )
926 MingwBackend::TryToDetectThisBinutils ( const string
& binutils
)
928 string command
= ssprintf (
930 FixSeparatorForSystemCommand(binutils
).c_str (),
933 int exitcode
= system ( command
.c_str () );
934 return (exitcode
== 0);
938 MingwBackend::GetBinutilsVersionDate ( const string
& binutilsCommand
)
944 string versionCommand
= ssprintf ( "%s -v",
945 binutilsCommand
.c_str (),
948 fp
= popen ( versionCommand
.c_str () , "r" );
951 ( feof ( fp
) == 0 &&
952 ( ( ch
= fgetc( fp
) ) != -1 ) );
955 buffer
[i
] = (char) ch
;
960 char separators
[] = " ";
962 char *prevtoken
= NULL
;
964 token
= strtok ( buffer
, separators
);
965 while ( token
!= NULL
)
968 token
= strtok ( NULL
, separators
);
970 string version
= string ( prevtoken
);
971 int lastDigit
= version
.find_last_not_of ( "\t\r\n" );
972 if ( lastDigit
!= -1 )
973 return string ( version
, 0, lastDigit
+1 );
979 MingwBackend::IsSupportedBinutilsVersion ( const string
& binutilsVersion
)
981 int digit
= binutilsVersion
.find_last_of(".");
984 printf("Unable to detect binutils version!\n");
988 string date
= string(binutilsVersion
, digit
+ 1);
989 if(date
.length() == 8)
991 /* This is a real date in the format YYYYMMDD.
992 Check whether we have at least Binutils 20091017 (older versions
993 don't support the -exclude-all-symbols option we use). */
994 if(strcmp(date
.c_str(), "20091119") < 0)
999 /* This is no date, so binutilsVersion should just contain the version
1001 Binutils 2.20 will hopefully contain the required features. */
1002 if(strcmp(binutilsVersion
.c_str(), "2.20") < 0)
1010 MingwBackend::DetectBinutils ()
1012 printf ( "Detecting binutils..." );
1014 bool detectedBinutils
= false;
1015 bool supportedBinutils
= false;
1016 string binutilsVersion
;
1018 if ( ProjectNode
.configuration
.Linker
== GnuLd
)
1020 const string
& ROS_PREFIXValue
= Environment::GetVariable ( "ROS_PREFIX" );
1022 if ( ROS_PREFIXValue
.length () > 0 )
1024 binutilsPrefix
= ROS_PREFIXValue
;
1025 binutilsCommand
= binutilsPrefix
+ "-ld";
1026 manualBinutilsSetting
= true;
1027 detectedBinutils
= true;
1030 if ( !detectedBinutils
)
1032 binutilsPrefix
= "";
1033 binutilsCommand
= "ld";
1034 detectedBinutils
= TryToDetectThisBinutils ( binutilsCommand
);
1037 if ( !detectedBinutils
)
1039 binutilsPrefix
= "mingw32";
1040 binutilsCommand
= binutilsPrefix
+ "-ld";
1041 detectedBinutils
= TryToDetectThisBinutils ( binutilsCommand
);
1043 if ( detectedBinutils
)
1045 binutilsVersion
= GetBinutilsVersionDate ( binutilsCommand
);
1046 supportedBinutils
= IsSupportedBinutilsVersion ( binutilsVersion
);
1049 else if ( ProjectNode
.configuration
.Linker
== MicrosoftLink
)
1051 compilerCommand
= "link";
1052 detectedBinutils
= DetectMicrosoftLinker ( binutilsVersion
, mslinkPath
);
1053 supportedBinutils
= true; // TODO
1056 if ( detectedBinutils
)
1058 if ( supportedBinutils
)
1059 printf ( "detected (%s %s)\n", binutilsCommand
.c_str (), binutilsVersion
.c_str() );
1062 printf ( "detected (%s), but with unsupported version (%s)\n",
1063 binutilsCommand
.c_str (),
1064 binutilsVersion
.c_str () );
1065 throw UnsupportedBuildToolException ( binutilsCommand
, binutilsVersion
);
1069 printf ( "not detected\n" );
1074 MingwBackend::DetectNetwideAssembler ()
1076 printf ( "Detecting netwide assembler..." );
1078 nasmCommand
= "nasm";
1079 bool detectedNasm
= TryToDetectThisNetwideAssembler ( nasmCommand
);
1081 if ( !detectedNasm
)
1083 nasmCommand
= "nasmw";
1084 detectedNasm
= TryToDetectThisNetwideAssembler ( nasmCommand
);
1087 if ( !detectedNasm
)
1089 nasmCommand
= "yasm";
1090 detectedNasm
= TryToDetectThisNetwideAssembler ( nasmCommand
);
1093 printf ( "detected (%s %s)\n", nasmCommand
.c_str (), GetNetwideAssemblerVersion( nasmCommand
).c_str() );
1095 printf ( "not detected\n" );
1099 MingwBackend::DetectPipeSupport ()
1101 if ( ProjectNode
.configuration
.Compiler
== GnuGcc
)
1103 printf ( "Detecting compiler -pipe support..." );
1105 string pipe_detection
= "tools" + sSep
+ "rbuild" + sSep
+ "backend" + sSep
+ "mingw" + sSep
+ "pipe_detection.c";
1106 string pipe_detectionObjectFilename
= ReplaceExtension ( pipe_detection
,
1108 string command
= ssprintf (
1109 "%s -pipe -c %s -o %s 1>%s 2>%s",
1110 FixSeparatorForSystemCommand(compilerCommand
).c_str (),
1111 pipe_detection
.c_str (),
1112 pipe_detectionObjectFilename
.c_str (),
1115 int exitcode
= system ( command
.c_str () );
1116 FILE* f
= fopen ( pipe_detectionObjectFilename
.c_str (), "rb" );
1119 usePipe
= (exitcode
== 0);
1121 unlink ( pipe_detectionObjectFilename
.c_str () );
1127 printf ( "detected\n" );
1129 printf ( "not detected\n" );
1136 MingwBackend::DetectPCHSupport ()
1138 printf ( "Detecting compiler pre-compiled header support..." );
1140 if ( configuration
.PrecompiledHeadersEnabled
&& ProjectNode
.configuration
.Compiler
== GnuGcc
)
1142 string path
= "tools" + sSep
+ "rbuild" + sSep
+ "backend" + sSep
+ "mingw" + sSep
+ "pch_detection.h";
1143 string cmd
= ssprintf (
1144 "%s -c %s 1>%s 2>%s",
1145 FixSeparatorForSystemCommand(compilerCommand
).c_str (),
1149 system ( cmd
.c_str () );
1152 FILE* f
= fopen ( path
.c_str (), "rb" );
1157 unlink ( path
.c_str () );
1163 printf ( "detected\n" );
1165 printf ( "not detected\n" );
1170 printf ( "disabled\n" );
1175 MingwBackend::GetNonModuleInstallTargetFiles (
1176 vector
<FileLocation
>& out
) const
1178 for ( size_t i
= 0; i
< ProjectNode
.installfiles
.size (); i
++ )
1180 const InstallFile
& installfile
= *ProjectNode
.installfiles
[i
];
1181 out
.push_back ( *installfile
.target
);
1186 MingwBackend::GetModuleInstallTargetFiles (
1187 vector
<FileLocation
>& out
) const
1189 for ( std::map
<std::string
, Module
*>::const_iterator p
= ProjectNode
.modules
.begin (); p
!= ProjectNode
.modules
.end (); ++ p
)
1191 const Module
& module
= *p
->second
;
1192 if ( !module
.enabled
)
1194 if ( module
.install
)
1195 out
.push_back ( *module
.install
);
1200 MingwBackend::GetInstallTargetFiles (
1201 vector
<FileLocation
>& out
) const
1203 GetNonModuleInstallTargetFiles ( out
);
1204 GetModuleInstallTargetFiles ( out
);
1208 MingwBackend::OutputInstallTarget ( const FileLocation
& source
,
1209 const FileLocation
& target
)
1211 fprintf ( fMakefile
,
1213 GetFullName ( target
).c_str (),
1214 GetFullName ( source
).c_str (),
1215 GetFullPath ( target
).c_str () );
1216 fprintf ( fMakefile
,
1218 fprintf ( fMakefile
,
1219 "\t${cp} %s %s 1>$(NUL)\n",
1220 GetFullName ( source
).c_str (),
1221 GetFullName ( target
).c_str () );
1225 MingwBackend::OutputNonModuleInstallTargets ()
1227 for ( size_t i
= 0; i
< ProjectNode
.installfiles
.size (); i
++ )
1229 const InstallFile
& installfile
= *ProjectNode
.installfiles
[i
];
1230 OutputInstallTarget ( *installfile
.source
, *installfile
.target
);
1235 MingwBackend::GetAliasedModuleOrModule ( const Module
& module
) const
1237 if ( module
.aliasedModuleName
.size () > 0 )
1239 const Module
* aliasedModule
= ProjectNode
.LocateModule ( module
.aliasedModuleName
);
1240 assert ( aliasedModule
);
1241 return *aliasedModule
;
1248 MingwBackend::OutputModuleInstallTargets ()
1250 for ( std::map
<std::string
, Module
*>::const_iterator p
= ProjectNode
.modules
.begin (); p
!= ProjectNode
.modules
.end (); ++ p
)
1252 const Module
& module
= *p
->second
;
1253 if ( !module
.enabled
)
1255 if ( module
.install
)
1257 const Module
& aliasedModule
= GetAliasedModuleOrModule ( module
);
1258 OutputInstallTarget ( *aliasedModule
.output
, *module
.install
);
1264 MingwBackend::GetRegistrySourceFiles ()
1266 return "boot" + sSep
+ "bootdata" + sSep
+ "hivecls_" + Environment::GetArch() + ".inf "
1267 "boot" + sSep
+ "bootdata" + sSep
+ "hivedef_" + Environment::GetArch() + ".inf "
1268 "boot" + sSep
+ "bootdata" + sSep
+ "hiveinst_" + Environment::GetArch() + ".inf "
1269 "boot" + sSep
+ "bootdata" + sSep
+ "hivesft_" + Environment::GetArch() + ".inf "
1270 "boot" + sSep
+ "bootdata" + sSep
+ "hivesys_" + Environment::GetArch() + ".inf ";
1274 MingwBackend::GetRegistryTargetFiles ()
1276 string system32ConfigDirectory
= "system32" + sSep
+ "config";
1277 FileLocation
system32 ( InstallDirectory
, system32ConfigDirectory
, "" );
1279 vector
<FileLocation
> registry_files
;
1280 registry_files
.push_back ( FileLocation ( InstallDirectory
, system32ConfigDirectory
, "default" ) );
1281 registry_files
.push_back ( FileLocation ( InstallDirectory
, system32ConfigDirectory
, "sam" ) );
1282 registry_files
.push_back ( FileLocation ( InstallDirectory
, system32ConfigDirectory
, "security" ) );
1283 registry_files
.push_back ( FileLocation ( InstallDirectory
, system32ConfigDirectory
, "software" ) );
1284 registry_files
.push_back ( FileLocation ( InstallDirectory
, system32ConfigDirectory
, "system" ) );
1286 return v2s( this, registry_files
, 6 );
1290 MingwBackend::OutputRegistryInstallTarget ()
1292 FileLocation
system32 ( InstallDirectory
, "system32" + sSep
+ "config", "" );
1294 string registrySourceFiles
= GetRegistrySourceFiles ();
1295 string registryTargetFiles
= GetRegistryTargetFiles ();
1296 fprintf ( fMakefile
,
1297 "install_registry: %s\n",
1298 registryTargetFiles
.c_str () );
1299 fprintf ( fMakefile
,
1300 "%s: %s %s $(mkhive_TARGET)\n",
1301 registryTargetFiles
.c_str (),
1302 registrySourceFiles
.c_str (),
1303 GetFullPath ( system32
).c_str () );
1304 fprintf ( fMakefile
,
1305 "\t$(ECHO_MKHIVE)\n" );
1306 fprintf ( fMakefile
,
1307 "\t$(mkhive_TARGET) boot%cbootdata %s $(ARCH) boot%cbootdata%chiveinst_$(ARCH).inf\n",
1308 cSep
, GetFullPath ( system32
).c_str (),
1310 fprintf ( fMakefile
,
1315 MingwBackend::GenerateInstallTarget ()
1317 vector
<FileLocation
> vInstallTargetFiles
;
1318 GetInstallTargetFiles ( vInstallTargetFiles
);
1319 string installTargetFiles
= v2s ( this, vInstallTargetFiles
, 5 );
1320 string registryTargetFiles
= GetRegistryTargetFiles ();
1322 fprintf ( fMakefile
,
1324 installTargetFiles
.c_str (),
1325 registryTargetFiles
.c_str () );
1326 OutputNonModuleInstallTargets ();
1327 OutputModuleInstallTargets ();
1328 OutputRegistryInstallTarget ();
1329 fprintf ( fMakefile
,
1334 MingwBackend::GetModuleTestTargets (
1335 vector
<string
>& out
) const
1337 for ( std::map
<std::string
, Module
*>::const_iterator p
= ProjectNode
.modules
.begin (); p
!= ProjectNode
.modules
.end (); ++ p
)
1339 const Module
& module
= *p
->second
;
1340 if ( !module
.enabled
)
1342 if ( module
.type
== Test
)
1343 out
.push_back ( module
.name
);
1348 MingwBackend::GenerateTestTarget ()
1350 vector
<string
> vTestTargets
;
1351 GetModuleTestTargets ( vTestTargets
);
1352 string testTargets
= v2s ( vTestTargets
, 5 );
1354 fprintf ( fMakefile
,
1356 testTargets
.c_str () );
1357 fprintf ( fMakefile
,
1362 MingwBackend::GenerateDirectoryTargets ()
1364 intermediateDirectory
->CreateRule ( fMakefile
, "$(INTERMEDIATE)" );
1365 outputDirectory
->CreateRule ( fMakefile
, "$(OUTPUT)" );
1366 installDirectory
->CreateRule ( fMakefile
, "$(INSTALL)" );