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
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, 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
, "", "", "" }, // Kernel
42 { HostFalse
, "", "", "" }, // KernelModeDLL
43 { HostFalse
, "-D__NTDRIVER__", "", "" }, // KernelModeDriver
44 { HostFalse
, "", "", "" }, // NativeDLL
45 { HostFalse
, "-D__NTAPP__", "", "" }, // NativeCUI
46 { HostFalse
, "", "", "" }, // Win32DLL
47 { HostFalse
, "", "", "" }, // Win32OCX
48 { HostFalse
, "", "", "" }, // Win32CUI
49 { HostFalse
, "", "", "" }, // Win32GUI
50 { HostFalse
, "", "", "-nostartfiles -nostdlib" }, // 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
, "", "", "-nostartfiles -nostdlib" }, // BootProgram
59 { HostFalse
, "", "", "" }, // Win32SCR
60 { HostFalse
, "", "", "" }, // IdlHeader
61 { HostFalse
, "", "", "" }, // IsoRegTest
62 { HostFalse
, "", "", "" }, // LiveIsoRegTest
63 { HostFalse
, "", "", "" }, // EmbeddedTypeLib
64 { HostFalse
, "", "", "" }, // ElfExecutable
65 { HostFalse
, "", "", "" }, // RpcProxy
66 { HostTrue
, "", "", "" }, // HostStaticLibrary
67 { HostFalse
, "", "", "" }, // Cabinet
68 { HostFalse
, "", "", "" }, // KeyboardLayout
69 { HostFalse
, "", "", "" }, // MessageHeader
73 MingwBackend::GetFullPath ( const FileLocation
& file
) const
75 MingwModuleHandler::PassThruCacheDirectory ( &file
);
78 switch ( file
.directory
)
83 case IntermediateDirectory
:
84 directory
= "$(INTERMEDIATE)";
87 directory
= "$(OUTPUT)";
89 case InstallDirectory
:
90 directory
= "$(INSTALL)";
92 case TemporaryDirectory
:
93 directory
= "$(TEMPORARY)";
96 throw InvalidOperationException ( __FILE__
,
98 "Invalid directory %d.",
102 if ( file
.relative_path
.length () > 0 )
104 if ( directory
.length () > 0 )
106 directory
+= file
.relative_path
;
112 MingwBackend::GetFullName ( const FileLocation
& file
) const
115 switch ( file
.directory
)
117 case SourceDirectory
:
120 case IntermediateDirectory
:
121 directory
= "$(INTERMEDIATE)";
123 case OutputDirectory
:
124 directory
= "$(OUTPUT)";
126 case InstallDirectory
:
127 directory
= "$(INSTALL)";
129 case TemporaryDirectory
:
130 directory
= "$(TEMPORARY)";
133 throw InvalidOperationException ( __FILE__
,
135 "Invalid directory %d.",
139 if ( file
.relative_path
.length () > 0 )
141 if ( directory
.length () > 0 )
143 directory
+= file
.relative_path
;
146 if ( directory
.length () > 0 )
149 return directory
+ file
.name
;
153 v2s ( const Backend
* backend
, const vector
<FileLocation
>& files
, int wrap_at
)
159 for ( size_t i
= 0; i
< files
.size(); i
++ )
161 const FileLocation
& file
= files
[i
];
162 if ( wrap_at
> 0 && wrap_count
++ == wrap_at
)
166 s
+= backend
->GetFullName ( file
);
173 v2s ( const string_list
& v
, int wrap_at
)
179 for ( size_t i
= 0; i
< v
.size(); i
++ )
183 if ( wrap_at
> 0 && wrap_count
++ == wrap_at
)
193 static class MingwFactory
: public Backend::Factory
196 MingwFactory() : Factory ( "mingw", "Minimalist GNU Win32" ) {}
197 Backend
* operator() ( Project
& project
,
198 Configuration
& configuration
)
200 return new MingwBackend ( project
,
206 MingwBackend::MingwBackend ( Project
& project
,
207 Configuration
& configuration
)
208 : Backend ( project
, configuration
),
209 manualBinutilsSetting( false ),
210 intermediateDirectory ( new Directory ( "" ) ),
211 outputDirectory ( new Directory ( "" ) ),
212 installDirectory ( new Directory ( "" ) )
217 MingwBackend::~MingwBackend()
219 delete intermediateDirectory
;
220 delete outputDirectory
;
221 delete installDirectory
;
225 MingwBackend::AddDirectoryTarget ( const string
& directory
,
226 Directory
* directoryTree
)
228 if ( directory
.length () > 0)
229 directoryTree
->Add ( directory
.c_str() );
230 return directoryTree
->name
;
234 MingwBackend::CanEnablePreCompiledHeaderSupportForModule ( const Module
& module
)
236 if ( !configuration
.CompilationUnitsEnabled
)
239 const vector
<CompilationUnit
*>& compilationUnits
= module
.non_if_data
.compilationUnits
;
241 for ( i
= 0; i
< compilationUnits
.size (); i
++ )
243 CompilationUnit
& compilationUnit
= *compilationUnits
[i
];
244 if ( compilationUnit
.GetFiles ().size () != 1 )
251 MingwBackend::ProcessModules ()
253 printf ( "Processing modules..." );
255 vector
<MingwModuleHandler
*> v
;
258 for ( std::map
<std::string
, Module
*>::iterator p
= ProjectNode
.modules
.begin (); p
!= ProjectNode
.modules
.end (); ++ p
)
260 Module
& module
= *p
->second
;
261 if ( !module
.enabled
)
263 MingwModuleHandler
* h
= MingwModuleHandler::InstanciateHandler (
266 h
->AddImplicitLibraries ( module
);
267 if ( use_pch
&& CanEnablePreCompiledHeaderSupportForModule ( module
) )
268 h
->EnablePreCompiledHeaderSupport ();
272 size_t iend
= v
.size ();
274 for ( i
= 0; i
< iend
; i
++ )
275 v
[i
]->GenerateSourceMacro();
276 for ( i
= 0; i
< iend
; i
++ )
277 v
[i
]->GenerateObjectMacro();
278 fprintf ( fMakefile
, "\n" );
279 for ( i
= 0; i
< iend
; i
++ )
280 v
[i
]->GenerateTargetMacro();
281 fprintf ( fMakefile
, "\n" );
283 GenerateAllTarget ( v
);
284 GenerateRegTestsRunTarget ();
286 for ( i
= 0; i
< iend
; i
++ )
287 v
[i
]->GenerateOtherMacros();
289 for ( i
= 0; i
< iend
; i
++ )
291 MingwModuleHandler
& h
= *v
[i
];
292 h
.GeneratePreconditionDependencies ();
294 h
.GenerateInvocations ();
295 h
.GenerateCleanTarget ();
296 h
.GenerateInstallTarget ();
297 h
.GenerateDependsTarget ();
305 MingwBackend::Process ()
307 if ( configuration
.CheckDependenciesForModuleOnly
)
308 CheckAutomaticDependenciesForModuleOnly ();
314 MingwBackend::CheckAutomaticDependenciesForModuleOnly ()
316 if ( configuration
.AutomaticDependencies
)
318 Module
* module
= ProjectNode
.LocateModule ( configuration
.CheckDependenciesForModuleOnlyModule
);
319 if ( module
== NULL
)
321 printf ( "Module '%s' does not exist\n",
322 configuration
.CheckDependenciesForModuleOnlyModule
.c_str () );
326 printf ( "Checking automatic dependencies for module '%s'...",
327 module
->name
.c_str () );
328 AutomaticDependency
automaticDependency ( ProjectNode
);
329 automaticDependency
.CheckAutomaticDependenciesForModule ( *module
,
330 configuration
.Verbose
);
336 MingwBackend::ProcessNormal ()
338 assert(sizeof(ModuleHandlerInformations
)/sizeof(ModuleHandlerInformations
[0]) == TypeDontCare
);
342 DetectNetwideAssembler ();
343 DetectPipeSupport ();
347 GenerateGlobalVariables ();
348 GenerateXmlBuildFilesMacro ();
350 GenerateInstallTarget ();
351 GenerateTestTarget ();
352 GenerateDirectoryTargets ();
353 GenerateDirectories ();
354 GenerateTestSupportCode ();
355 GenerateCompilationUnitSupportCode ();
357 GenerateProxyMakefiles ();
358 CheckAutomaticDependencies ();
363 MingwBackend::CreateMakefile ()
365 fMakefile
= fopen ( ProjectNode
.makefile
.c_str (), "w" );
367 throw AccessDeniedException ( ProjectNode
.makefile
);
368 MingwModuleHandler::SetBackend ( this );
369 MingwModuleHandler::SetMakefile ( fMakefile
);
373 MingwBackend::CloseMakefile () const
376 fclose ( fMakefile
);
380 MingwBackend::GenerateHeader () const
382 fprintf ( fMakefile
, "# THIS FILE IS AUTOMATICALLY GENERATED, EDIT '%s' INSTEAD\n\n",
383 ProjectNode
.GetProjectFilename ().c_str () );
387 MingwBackend::GenerateProjectCFlagsMacro ( const char* assignmentOperation
,
388 const IfableData
& data
) const
390 set
<string
> used_defs
;
392 if ( data
.includes
.size () > 0 )
395 "PROJECT_CINCLUDES %s %s\n",
397 MingwModuleHandler::GenerateGccIncludeParametersFromVector ( data
.includes
).c_str ());
399 if ( data
.defines
.size () > 0 )
402 "PROJECT_CDEFINES %s %s\n",
404 MingwModuleHandler::GenerateGccDefineParametersFromVector ( data
.defines
, used_defs
).c_str ());
408 MingwBackend::GenerateGlobalCFlagsAndProperties (
409 const char* assignmentOperation
,
410 const IfableData
& data
) const
412 for ( std::map
<std::string
, Property
*>::const_iterator p
= data
.properties
.begin(); p
!= data
.properties
.end(); ++ p
)
414 Property
& prop
= *p
->second
;
416 if (!prop
.isInternal
)
418 fprintf ( fMakefile
, "%s := %s\n",
420 prop
.value
.c_str() );
424 if ( data
.includes
.size() || data
.defines
.size() )
426 GenerateProjectCFlagsMacro ( assignmentOperation
,
432 MingwBackend::GenerateProjectGccOptionsMacro ( const char* assignmentOperation
,
433 IfableData
& data
) const
439 "PROJECT_GCCOPTIONS %s",
440 assignmentOperation
);
442 for ( i
= 0; i
< data
.compilerFlags
.size(); i
++ )
444 if ( data
.compilerFlags
[i
]->compiler
== CompilerTypeDontCare
)
449 data
.compilerFlags
[i
]->flag
.c_str() );
453 fputs ( "\n", fMakefile
);
455 // TODO: reference these from somewhere
458 "PROJECT_GCC_CFLAGS %s",
459 assignmentOperation
);
461 for ( i
= 0; i
< data
.compilerFlags
.size(); i
++ )
463 if ( data
.compilerFlags
[i
]->compiler
== CompilerTypeCC
)
468 data
.compilerFlags
[i
]->flag
.c_str() );
472 fputs ( "\n", fMakefile
);
476 "PROJECT_GCC_CXXFLAGS %s",
477 assignmentOperation
);
479 for ( i
= 0; i
< data
.compilerFlags
.size(); i
++ )
481 if ( data
.compilerFlags
[i
]->compiler
== CompilerTypeCPP
)
486 data
.compilerFlags
[i
]->flag
.c_str() );
490 fputs ( "\n", fMakefile
);
494 MingwBackend::GenerateProjectGccOptions (
495 const char* assignmentOperation
,
496 IfableData
& data
) const
498 if ( data
.compilerFlags
.size() )
500 GenerateProjectGccOptionsMacro ( assignmentOperation
,
506 MingwBackend::GenerateProjectLFLAGS () const
509 for ( size_t i
= 0; i
< ProjectNode
.linkerFlags
.size (); i
++ )
511 LinkerFlag
& linkerFlag
= *ProjectNode
.linkerFlags
[i
];
512 if ( lflags
.length () > 0 )
514 lflags
+= linkerFlag
.flag
;
520 MingwBackend::GenerateGlobalVariables () const
524 compilerPrefix
.c_str () );
527 nasmCommand
.c_str () );
529 GenerateGlobalCFlagsAndProperties ( "=", ProjectNode
.non_if_data
);
530 GenerateProjectGccOptions ( "=", ProjectNode
.non_if_data
);
532 fprintf ( fMakefile
, "PROJECT_RCFLAGS := $(PROJECT_CINCLUDES) $(PROJECT_CDEFINES)\n" );
533 fprintf ( fMakefile
, "PROJECT_WIDLFLAGS := $(PROJECT_CINCLUDES) $(PROJECT_CDEFINES)\n" );
534 fprintf ( fMakefile
, "PROJECT_LFLAGS := '$(shell ${TARGET_CC} -print-libgcc-file-name)' %s\n", GenerateProjectLFLAGS ().c_str () );
535 fprintf ( fMakefile
, "PROJECT_LPPFLAGS := '$(shell ${TARGET_CPP} -print-file-name=libstdc++.a)' '$(shell ${TARGET_CPP} -print-file-name=libgcc.a)' '$(shell ${TARGET_CPP} -print-file-name=libmingw32.a)' '$(shell ${TARGET_CPP} -print-file-name=libmingwex.a)' '$(shell ${TARGET_CPP} -print-file-name=libcoldname.a)'\n" );
536 /* hack to get libgcc_eh.a, should check mingw version or something */
537 fprintf ( fMakefile
, "ifeq ($(ARCH),amd64)\n" );
538 fprintf ( fMakefile
, "PROJECT_LPPFLAGS += '$(shell ${TARGET_CPP} -print-file-name=libgcc_eh.a)'\n" );
539 fprintf ( fMakefile
, "endif\n" );
540 fprintf ( fMakefile
, "PROJECT_GCCOPTIONS += -Wall\n" );
541 fprintf ( fMakefile
, "ifneq ($(OARCH),)\n" );
542 fprintf ( fMakefile
, "PROJECT_GCCOPTIONS += -march=$(OARCH)\n" );
543 fprintf ( fMakefile
, "endif\n" );
544 fprintf ( fMakefile
, "PROJECT_CFLAGS = $(PROJECT_GCCOPTIONS) $(PROJECT_GCC_CFLAGS)\n" );
545 fprintf ( fMakefile
, "PROJECT_CXXFLAGS = $(PROJECT_GCCOPTIONS) $(PROJECT_GCC_CXXFLAGS)\n" );
546 fprintf ( fMakefile
, "\n" );
550 MingwBackend::IncludeInAllTarget ( const Module
& module
) const
552 if ( MingwModuleHandler::ReferenceObjects ( module
) )
554 if ( module
.type
== BootSector
)
556 if ( module
.type
== Iso
)
558 if ( module
.type
== LiveIso
)
560 if ( module
.type
== IsoRegTest
)
562 if ( module
.type
== LiveIsoRegTest
)
564 if ( module
.type
== Test
)
566 if ( module
.type
== Alias
)
572 MingwBackend::GenerateAllTarget ( const vector
<MingwModuleHandler
*>& handlers
) const
574 fprintf ( fMakefile
, "all:" );
576 size_t iend
= handlers
.size ();
577 for ( size_t i
= 0; i
< iend
; i
++ )
579 const Module
& module
= handlers
[i
]->module
;
580 if ( IncludeInAllTarget ( module
) )
582 if ( wrap_count
++ == 5 )
583 fprintf ( fMakefile
, " \\\n\t\t" ), wrap_count
= 0;
586 GetTargetMacro(module
).c_str () );
589 fprintf ( fMakefile
, "\n\t\n\n" );
593 MingwBackend::GenerateRegTestsRunTarget () const
596 "REGTESTS_RUN_TARGET = regtests.dll\n" );
598 "$(REGTESTS_RUN_TARGET): $(REGTESTS_TARGET)\n" );
600 "\t$(cp) $(REGTESTS_TARGET) $(REGTESTS_RUN_TARGET)\n" );
601 fprintf ( fMakefile
, "\n" );
605 MingwBackend::GenerateXmlBuildFilesMacro() const
608 "XMLBUILDFILES = %s \\\n",
609 ProjectNode
.GetProjectFilename ().c_str () );
610 string xmlbuildFilenames
;
611 int numberOfExistingFiles
= 0;
613 time_t SystemTime
, lastWriteTime
;
615 for ( size_t i
= 0; i
< ProjectNode
.xmlbuildfiles
.size (); i
++ )
617 XMLInclude
& xmlbuildfile
= *ProjectNode
.xmlbuildfiles
[i
];
618 if ( !xmlbuildfile
.fileExists
)
620 numberOfExistingFiles
++;
621 if ( xmlbuildFilenames
.length () > 0 )
622 xmlbuildFilenames
+= " ";
624 FILE* f
= fopen ( xmlbuildfile
.topIncludeFilename
.c_str (), "rb" );
626 throw FileNotFoundException ( NormalizeFilename ( xmlbuildfile
.topIncludeFilename
) );
628 if ( fstat ( fileno ( f
), &statbuf
) != 0 )
631 throw AccessDeniedException ( NormalizeFilename ( xmlbuildfile
.topIncludeFilename
) );
634 lastWriteTime
= statbuf
.st_mtime
;
635 SystemTime
= time(NULL
);
637 if (SystemTime
!= -1)
639 if (difftime (lastWriteTime
, SystemTime
) > 0)
640 throw InvalidDateException ( NormalizeFilename ( xmlbuildfile
.topIncludeFilename
) );
645 xmlbuildFilenames
+= NormalizeFilename ( xmlbuildfile
.topIncludeFilename
);
646 if ( numberOfExistingFiles
% 5 == 4 || i
== ProjectNode
.xmlbuildfiles
.size () - 1 )
650 xmlbuildFilenames
.c_str ());
651 if ( i
== ProjectNode
.xmlbuildfiles
.size () - 1 )
653 fprintf ( fMakefile
, "\n" );
660 xmlbuildFilenames
.resize ( 0 );
662 numberOfExistingFiles
++;
664 fprintf ( fMakefile
, "\n" );
668 MingwBackend::GenerateTestSupportCode ()
670 printf ( "Generating test support code..." );
671 TestSupportCode
testSupportCode ( ProjectNode
);
672 testSupportCode
.GenerateTestSupportCode ( configuration
.Verbose
);
677 MingwBackend::GenerateCompilationUnitSupportCode ()
679 if ( configuration
.CompilationUnitsEnabled
)
681 printf ( "Generating compilation unit support code..." );
682 CompilationUnitSupportCode
compilationUnitSupportCode ( ProjectNode
);
683 compilationUnitSupportCode
.Generate ( configuration
.Verbose
);
689 MingwBackend::GenerateSysSetup ()
691 printf ( "Generating syssetup.inf..." );
692 SysSetupGenerator
sysSetupGenerator ( ProjectNode
);
693 sysSetupGenerator
.Generate ();
698 MingwBackend::GetProxyMakefileTree () const
700 if ( configuration
.GenerateProxyMakefilesInSourceTree
)
703 return Environment::GetOutputPath ();
707 MingwBackend::GenerateProxyMakefiles ()
709 printf ( "Generating proxy makefiles..." );
710 ProxyMakefile
proxyMakefile ( ProjectNode
);
711 proxyMakefile
.GenerateProxyMakefiles ( configuration
.Verbose
,
712 GetProxyMakefileTree () );
717 MingwBackend::CheckAutomaticDependencies ()
719 if ( configuration
.AutomaticDependencies
)
721 printf ( "Checking automatic dependencies..." );
722 AutomaticDependency
automaticDependency ( ProjectNode
);
723 automaticDependency
.CheckAutomaticDependencies ( configuration
.Verbose
);
729 MingwBackend::GenerateDirectories ()
731 printf ( "Creating directories..." );
732 intermediateDirectory
->GenerateTree ( IntermediateDirectory
, configuration
.Verbose
);
733 outputDirectory
->GenerateTree ( OutputDirectory
, configuration
.Verbose
);
734 if ( !configuration
.MakeHandlesInstallDirectories
)
735 installDirectory
->GenerateTree ( InstallDirectory
, configuration
.Verbose
);
740 MingwBackend::TryToDetectThisCompiler ( const string
& compiler
)
742 string command
= ssprintf (
744 FixSeparatorForSystemCommand(compiler
).c_str (),
747 int exitcode
= system ( command
.c_str () );
748 return (bool) (exitcode
== 0);
752 MingwBackend::DetectCompiler ()
754 printf ( "Detecting compiler..." );
756 bool detectedCompiler
= false;
757 const string
& ROS_PREFIXValue
= Environment::GetVariable ( "ROS_PREFIX" );
758 if ( ROS_PREFIXValue
.length () > 0 )
760 compilerPrefix
= ROS_PREFIXValue
;
761 compilerCommand
= compilerPrefix
+ "-gcc";
762 detectedCompiler
= TryToDetectThisCompiler ( compilerCommand
);
765 if ( !detectedCompiler
)
768 compilerCommand
= "gcc";
769 detectedCompiler
= TryToDetectThisCompiler ( compilerCommand
);
772 if ( !detectedCompiler
)
774 compilerPrefix
= "mingw32";
775 compilerCommand
= compilerPrefix
+ "-gcc";
776 detectedCompiler
= TryToDetectThisCompiler ( compilerCommand
);
779 if ( detectedCompiler
)
781 string compilerVersion
= GetCompilerVersion ( compilerCommand
);
782 if ( IsSupportedCompilerVersion ( compilerVersion
) )
783 printf ( "detected (%s %s)\n", compilerCommand
.c_str (), compilerVersion
.c_str() );
786 printf ( "detected (%s), but with unsupported version (%s)\n",
787 compilerCommand
.c_str (),
788 compilerVersion
.c_str () );
789 throw UnsupportedBuildToolException ( compilerCommand
, compilerVersion
);
793 printf ( "not detected\n" );
798 MingwBackend::TryToDetectThisNetwideAssembler ( const string
& assembler
)
800 string command
= ssprintf (
802 FixSeparatorForSystemCommand(assembler
).c_str (),
805 int exitcode
= system ( command
.c_str () );
806 return (bool) (exitcode
== 0);
810 MingwBackend::GetVersionString ( const string
& versionCommand
)
817 fp
= popen ( versionCommand
.c_str () , "r" );
820 ( feof ( fp
) == 0 &&
821 ( ( ch
= fgetc( fp
) ) != -1 ) );
824 buffer
[i
] = (char) ch
;
829 char separators
[] = " ()";
831 char *prevtoken
= NULL
;
835 token
= strtok ( buffer
, separators
);
836 while ( token
!= NULL
)
839 version
= string( prevtoken
);
840 if ( (newline
= version
.find('\n')) != std::string::npos
)
841 version
.erase(newline
, 1);
842 if ( version
.find('.') != std::string::npos
)
844 token
= strtok ( NULL
, separators
);
850 MingwBackend::GetNetwideAssemblerVersion ( const string
& nasmCommand
)
852 string versionCommand
;
853 if ( nasmCommand
.find("yasm") != std::string::npos
)
855 versionCommand
= ssprintf ( "%s --version",
856 nasmCommand
.c_str (),
862 versionCommand
= ssprintf ( "%s -v",
863 nasmCommand
.c_str (),
867 return GetVersionString( versionCommand
);
871 MingwBackend::GetCompilerVersion ( const string
& compilerCommand
)
873 string versionCommand
= ssprintf ( "%s --version gcc",
874 compilerCommand
.c_str (),
877 return GetVersionString( versionCommand
);
881 MingwBackend::GetBinutilsVersion ( const string
& binutilsCommand
)
883 string versionCommand
= ssprintf ( "%s -v",
884 binutilsCommand
.c_str (),
887 return GetVersionString( versionCommand
);
891 MingwBackend::IsSupportedCompilerVersion ( const string
& compilerVersion
)
893 if ( strcmp ( compilerVersion
.c_str (), "3.4.2") < 0 )
900 MingwBackend::TryToDetectThisBinutils ( const string
& binutils
)
902 string command
= ssprintf (
904 FixSeparatorForSystemCommand(binutils
).c_str (),
907 int exitcode
= system ( command
.c_str () );
908 return (exitcode
== 0);
912 MingwBackend::GetBinutilsVersionDate ( const string
& binutilsCommand
)
918 string versionCommand
= ssprintf ( "%s -v",
919 binutilsCommand
.c_str (),
922 fp
= popen ( versionCommand
.c_str () , "r" );
925 ( feof ( fp
) == 0 &&
926 ( ( ch
= fgetc( fp
) ) != -1 ) );
929 buffer
[i
] = (char) ch
;
934 char separators
[] = " ";
936 char *prevtoken
= NULL
;
938 token
= strtok ( buffer
, separators
);
939 while ( token
!= NULL
)
942 token
= strtok ( NULL
, separators
);
944 string version
= string ( prevtoken
);
945 int lastDigit
= version
.find_last_not_of ( "\t\r\n" );
946 if ( lastDigit
!= -1 )
947 return string ( version
, 0, lastDigit
+1 );
953 MingwBackend::IsSupportedBinutilsVersion ( const string
& binutilsVersion
)
955 if ( manualBinutilsSetting
) return true;
958 if ( binutilsVersion
.find('.') != std::string::npos
)
960 /* TODO: blacklist versions on version number instead of date */
965 * - Binutils older than 2003/10/01 have broken windres which can't handle
966 * icons with alpha channel.
967 * - Binutils between 2004/09/02 and 2004/10/08 have broken handling of
968 * forward exports in dlltool.
970 if ( ( ( strcmp ( binutilsVersion
.c_str (), "20040902") >= 0 ) &&
971 ( strcmp ( binutilsVersion
.c_str (), "20041008") <= 0 ) ) ||
972 ( strcmp ( binutilsVersion
.c_str (), "20031001") < 0 ) )
979 MingwBackend::DetectBinutils ()
981 printf ( "Detecting binutils..." );
983 bool detectedBinutils
= false;
984 const string
& ROS_PREFIXValue
= Environment::GetVariable ( "ROS_PREFIX" );
986 if ( ROS_PREFIXValue
.length () > 0 )
988 binutilsPrefix
= ROS_PREFIXValue
;
989 binutilsCommand
= binutilsPrefix
+ "-ld";
990 manualBinutilsSetting
= true;
991 detectedBinutils
= true;
994 if ( !detectedBinutils
)
997 binutilsCommand
= "ld";
998 detectedBinutils
= TryToDetectThisBinutils ( binutilsCommand
);
1001 if ( !detectedBinutils
)
1003 binutilsPrefix
= "mingw32";
1004 binutilsCommand
= binutilsPrefix
+ "-ld";
1005 detectedBinutils
= TryToDetectThisBinutils ( binutilsCommand
);
1007 if ( detectedBinutils
)
1009 string binutilsVersion
= GetBinutilsVersionDate ( binutilsCommand
);
1010 if ( IsSupportedBinutilsVersion ( binutilsVersion
) )
1011 printf ( "detected (%s %s)\n", binutilsCommand
.c_str (), GetBinutilsVersion( binutilsCommand
).c_str() );
1014 printf ( "detected (%s), but with unsupported version (%s)\n",
1015 binutilsCommand
.c_str (),
1016 binutilsVersion
.c_str () );
1017 throw UnsupportedBuildToolException ( binutilsCommand
, binutilsVersion
);
1021 printf ( "not detected\n" );
1026 MingwBackend::DetectNetwideAssembler ()
1028 printf ( "Detecting netwide assembler..." );
1030 nasmCommand
= "nasm";
1031 bool detectedNasm
= TryToDetectThisNetwideAssembler ( nasmCommand
);
1033 if ( !detectedNasm
)
1035 nasmCommand
= "nasmw";
1036 detectedNasm
= TryToDetectThisNetwideAssembler ( nasmCommand
);
1039 if ( !detectedNasm
)
1041 nasmCommand
= "yasm";
1042 detectedNasm
= TryToDetectThisNetwideAssembler ( nasmCommand
);
1045 printf ( "detected (%s %s)\n", nasmCommand
.c_str (), GetNetwideAssemblerVersion( nasmCommand
).c_str() );
1047 printf ( "not detected\n" );
1051 MingwBackend::DetectPipeSupport ()
1053 printf ( "Detecting compiler -pipe support..." );
1055 string pipe_detection
= "tools" + sSep
+ "rbuild" + sSep
+ "backend" + sSep
+ "mingw" + sSep
+ "pipe_detection.c";
1056 string pipe_detectionObjectFilename
= ReplaceExtension ( pipe_detection
,
1058 string command
= ssprintf (
1059 "%s -pipe -c %s -o %s 1>%s 2>%s",
1060 FixSeparatorForSystemCommand(compilerCommand
).c_str (),
1061 pipe_detection
.c_str (),
1062 pipe_detectionObjectFilename
.c_str (),
1065 int exitcode
= system ( command
.c_str () );
1066 FILE* f
= fopen ( pipe_detectionObjectFilename
.c_str (), "rb" );
1069 usePipe
= (exitcode
== 0);
1071 unlink ( pipe_detectionObjectFilename
.c_str () );
1077 printf ( "detected\n" );
1079 printf ( "not detected\n" );
1083 MingwBackend::DetectPCHSupport ()
1085 printf ( "Detecting compiler pre-compiled header support..." );
1087 if ( configuration
.PrecompiledHeadersEnabled
)
1089 string path
= "tools" + sSep
+ "rbuild" + sSep
+ "backend" + sSep
+ "mingw" + sSep
+ "pch_detection.h";
1090 string cmd
= ssprintf (
1091 "%s -c %s 1>%s 2>%s",
1092 FixSeparatorForSystemCommand(compilerCommand
).c_str (),
1096 system ( cmd
.c_str () );
1099 FILE* f
= fopen ( path
.c_str (), "rb" );
1104 unlink ( path
.c_str () );
1110 printf ( "detected\n" );
1112 printf ( "not detected\n" );
1117 printf ( "disabled\n" );
1122 MingwBackend::GetNonModuleInstallTargetFiles (
1123 vector
<FileLocation
>& out
) const
1125 for ( size_t i
= 0; i
< ProjectNode
.installfiles
.size (); i
++ )
1127 const InstallFile
& installfile
= *ProjectNode
.installfiles
[i
];
1128 out
.push_back ( *installfile
.target
);
1133 MingwBackend::GetModuleInstallTargetFiles (
1134 vector
<FileLocation
>& out
) const
1136 for ( std::map
<std::string
, Module
*>::const_iterator p
= ProjectNode
.modules
.begin (); p
!= ProjectNode
.modules
.end (); ++ p
)
1138 const Module
& module
= *p
->second
;
1139 if ( !module
.enabled
)
1141 if ( module
.install
)
1142 out
.push_back ( *module
.install
);
1147 MingwBackend::GetInstallTargetFiles (
1148 vector
<FileLocation
>& out
) const
1150 GetNonModuleInstallTargetFiles ( out
);
1151 GetModuleInstallTargetFiles ( out
);
1155 MingwBackend::OutputInstallTarget ( const FileLocation
& source
,
1156 const FileLocation
& target
)
1158 fprintf ( fMakefile
,
1160 GetFullName ( target
).c_str (),
1161 GetFullName ( source
).c_str (),
1162 GetFullPath ( target
).c_str () );
1163 fprintf ( fMakefile
,
1165 fprintf ( fMakefile
,
1166 "\t${cp} %s %s 1>$(NUL)\n",
1167 GetFullName ( source
).c_str (),
1168 GetFullName ( target
).c_str () );
1172 MingwBackend::OutputNonModuleInstallTargets ()
1174 for ( size_t i
= 0; i
< ProjectNode
.installfiles
.size (); i
++ )
1176 const InstallFile
& installfile
= *ProjectNode
.installfiles
[i
];
1177 OutputInstallTarget ( *installfile
.source
, *installfile
.target
);
1182 MingwBackend::GetAliasedModuleOrModule ( const Module
& module
) const
1184 if ( module
.aliasedModuleName
.size () > 0 )
1186 const Module
* aliasedModule
= ProjectNode
.LocateModule ( module
.aliasedModuleName
);
1187 assert ( aliasedModule
);
1188 return *aliasedModule
;
1195 MingwBackend::OutputModuleInstallTargets ()
1197 for ( std::map
<std::string
, Module
*>::const_iterator p
= ProjectNode
.modules
.begin (); p
!= ProjectNode
.modules
.end (); ++ p
)
1199 const Module
& module
= *p
->second
;
1200 if ( !module
.enabled
)
1202 if ( module
.install
)
1204 const Module
& aliasedModule
= GetAliasedModuleOrModule ( module
);
1205 OutputInstallTarget ( *aliasedModule
.output
, *module
.install
);
1211 MingwBackend::GetRegistrySourceFiles ()
1213 return "boot" + sSep
+ "bootdata" + sSep
+ "hivecls_" + Environment::GetArch() + ".inf "
1214 "boot" + sSep
+ "bootdata" + sSep
+ "hivedef_" + Environment::GetArch() + ".inf "
1215 "boot" + sSep
+ "bootdata" + sSep
+ "hiveinst_" + Environment::GetArch() + ".inf "
1216 "boot" + sSep
+ "bootdata" + sSep
+ "hivesft_" + Environment::GetArch() + ".inf "
1217 "boot" + sSep
+ "bootdata" + sSep
+ "hivesys_" + Environment::GetArch() + ".inf ";
1221 MingwBackend::GetRegistryTargetFiles ()
1223 string system32ConfigDirectory
= "system32" + sSep
+ "config";
1224 FileLocation
system32 ( InstallDirectory
, system32ConfigDirectory
, "" );
1226 vector
<FileLocation
> registry_files
;
1227 registry_files
.push_back ( FileLocation ( InstallDirectory
, system32ConfigDirectory
, "default" ) );
1228 registry_files
.push_back ( FileLocation ( InstallDirectory
, system32ConfigDirectory
, "sam" ) );
1229 registry_files
.push_back ( FileLocation ( InstallDirectory
, system32ConfigDirectory
, "security" ) );
1230 registry_files
.push_back ( FileLocation ( InstallDirectory
, system32ConfigDirectory
, "software" ) );
1231 registry_files
.push_back ( FileLocation ( InstallDirectory
, system32ConfigDirectory
, "system" ) );
1233 return v2s( this, registry_files
, 6 );
1237 MingwBackend::OutputRegistryInstallTarget ()
1239 FileLocation
system32 ( InstallDirectory
, "system32" + sSep
+ "config", "" );
1241 string registrySourceFiles
= GetRegistrySourceFiles ();
1242 string registryTargetFiles
= GetRegistryTargetFiles ();
1243 fprintf ( fMakefile
,
1244 "install_registry: %s\n",
1245 registryTargetFiles
.c_str () );
1246 fprintf ( fMakefile
,
1247 "%s: %s %s $(MKHIVE_TARGET)\n",
1248 registryTargetFiles
.c_str (),
1249 registrySourceFiles
.c_str (),
1250 GetFullPath ( system32
).c_str () );
1251 fprintf ( fMakefile
,
1252 "\t$(ECHO_MKHIVE)\n" );
1253 fprintf ( fMakefile
,
1254 "\t$(MKHIVE_TARGET) boot%cbootdata %s $(ARCH) boot%cbootdata%chiveinst_$(ARCH).inf\n",
1255 cSep
, GetFullPath ( system32
).c_str (),
1257 fprintf ( fMakefile
,
1262 MingwBackend::GenerateInstallTarget ()
1264 vector
<FileLocation
> vInstallTargetFiles
;
1265 GetInstallTargetFiles ( vInstallTargetFiles
);
1266 string installTargetFiles
= v2s ( this, vInstallTargetFiles
, 5 );
1267 string registryTargetFiles
= GetRegistryTargetFiles ();
1269 fprintf ( fMakefile
,
1271 installTargetFiles
.c_str (),
1272 registryTargetFiles
.c_str () );
1273 OutputNonModuleInstallTargets ();
1274 OutputModuleInstallTargets ();
1275 OutputRegistryInstallTarget ();
1276 fprintf ( fMakefile
,
1281 MingwBackend::GetModuleTestTargets (
1282 vector
<string
>& out
) const
1284 for ( std::map
<std::string
, Module
*>::const_iterator p
= ProjectNode
.modules
.begin (); p
!= ProjectNode
.modules
.end (); ++ p
)
1286 const Module
& module
= *p
->second
;
1287 if ( !module
.enabled
)
1289 if ( module
.type
== Test
)
1290 out
.push_back ( module
.name
);
1295 MingwBackend::GenerateTestTarget ()
1297 vector
<string
> vTestTargets
;
1298 GetModuleTestTargets ( vTestTargets
);
1299 string testTargets
= v2s ( vTestTargets
, 5 );
1301 fprintf ( fMakefile
,
1303 testTargets
.c_str () );
1304 fprintf ( fMakefile
,
1309 MingwBackend::GenerateDirectoryTargets ()
1311 intermediateDirectory
->CreateRule ( fMakefile
, "$(INTERMEDIATE)" );
1312 outputDirectory
->CreateRule ( fMakefile
, "$(OUTPUT)" );
1313 installDirectory
->CreateRule ( fMakefile
, "$(INSTALL)" );