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_CFLAGS %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 fprintf ( fMakefile
, "\n" );
457 MingwBackend::GenerateProjectGccOptions (
458 const char* assignmentOperation
,
459 IfableData
& data
) const
461 if ( data
.compilerFlags
.size() )
463 GenerateProjectGccOptionsMacro ( assignmentOperation
,
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
487 compilerPrefix
.c_str () );
490 nasmCommand
.c_str () );
492 GenerateGlobalCFlagsAndProperties ( "=", ProjectNode
.non_if_data
);
493 GenerateProjectGccOptions ( "=", ProjectNode
.non_if_data
);
495 fprintf ( fMakefile
, "PROJECT_RCFLAGS := $(PROJECT_CFLAGS) $(PROJECT_CDEFINES)\n" );
496 fprintf ( fMakefile
, "PROJECT_WIDLFLAGS := $(PROJECT_CFLAGS) $(PROJECT_CDEFINES)\n" );
497 fprintf ( fMakefile
, "PROJECT_LFLAGS := '$(shell ${TARGET_CC} -print-libgcc-file-name)' %s\n", GenerateProjectLFLAGS ().c_str () );
498 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)'\n" );
499 fprintf ( fMakefile
, "PROJECT_CFLAGS += -Wall\n" );
500 fprintf ( fMakefile
, "ifneq ($(OARCH),)\n" );
501 fprintf ( fMakefile
, "PROJECT_CFLAGS += -march=$(OARCH)\n" );
502 fprintf ( fMakefile
, "endif\n" );
503 fprintf ( fMakefile
, "PROJECT_CFLAGS += $(PROJECT_GCCOPTIONS)\n" );
504 fprintf ( fMakefile
, "\n" );
508 MingwBackend::IncludeInAllTarget ( const Module
& module
) const
510 if ( MingwModuleHandler::ReferenceObjects ( module
) )
512 if ( module
.type
== BootSector
)
514 if ( module
.type
== Iso
)
516 if ( module
.type
== LiveIso
)
518 if ( module
.type
== IsoRegTest
)
520 if ( module
.type
== LiveIsoRegTest
)
522 if ( module
.type
== Test
)
524 if ( module
.type
== Alias
)
530 MingwBackend::GenerateAllTarget ( const vector
<MingwModuleHandler
*>& handlers
) const
532 fprintf ( fMakefile
, "all:" );
534 size_t iend
= handlers
.size ();
535 for ( size_t i
= 0; i
< iend
; i
++ )
537 const Module
& module
= handlers
[i
]->module
;
538 if ( IncludeInAllTarget ( module
) )
540 if ( wrap_count
++ == 5 )
541 fprintf ( fMakefile
, " \\\n\t\t" ), wrap_count
= 0;
544 GetTargetMacro(module
).c_str () );
547 fprintf ( fMakefile
, "\n\t\n\n" );
551 MingwBackend::GenerateRegTestsRunTarget () const
554 "REGTESTS_RUN_TARGET = regtests.dll\n" );
556 "$(REGTESTS_RUN_TARGET): $(REGTESTS_TARGET)\n" );
558 "\t$(cp) $(REGTESTS_TARGET) $(REGTESTS_RUN_TARGET)\n" );
559 fprintf ( fMakefile
, "\n" );
563 MingwBackend::GenerateXmlBuildFilesMacro() const
566 "XMLBUILDFILES = %s \\\n",
567 ProjectNode
.GetProjectFilename ().c_str () );
568 string xmlbuildFilenames
;
569 int numberOfExistingFiles
= 0;
571 time_t SystemTime
, lastWriteTime
;
573 for ( size_t i
= 0; i
< ProjectNode
.xmlbuildfiles
.size (); i
++ )
575 XMLInclude
& xmlbuildfile
= *ProjectNode
.xmlbuildfiles
[i
];
576 if ( !xmlbuildfile
.fileExists
)
578 numberOfExistingFiles
++;
579 if ( xmlbuildFilenames
.length () > 0 )
580 xmlbuildFilenames
+= " ";
582 FILE* f
= fopen ( xmlbuildfile
.topIncludeFilename
.c_str (), "rb" );
584 throw FileNotFoundException ( NormalizeFilename ( xmlbuildfile
.topIncludeFilename
) );
586 if ( fstat ( fileno ( f
), &statbuf
) != 0 )
589 throw AccessDeniedException ( NormalizeFilename ( xmlbuildfile
.topIncludeFilename
) );
592 lastWriteTime
= statbuf
.st_mtime
;
593 SystemTime
= time(NULL
);
595 if (SystemTime
!= -1)
597 if (difftime (lastWriteTime
, SystemTime
) > 0)
598 throw InvalidDateException ( NormalizeFilename ( xmlbuildfile
.topIncludeFilename
) );
603 xmlbuildFilenames
+= NormalizeFilename ( xmlbuildfile
.topIncludeFilename
);
604 if ( numberOfExistingFiles
% 5 == 4 || i
== ProjectNode
.xmlbuildfiles
.size () - 1 )
608 xmlbuildFilenames
.c_str ());
609 if ( i
== ProjectNode
.xmlbuildfiles
.size () - 1 )
611 fprintf ( fMakefile
, "\n" );
618 xmlbuildFilenames
.resize ( 0 );
620 numberOfExistingFiles
++;
622 fprintf ( fMakefile
, "\n" );
626 MingwBackend::GenerateTestSupportCode ()
628 printf ( "Generating test support code..." );
629 TestSupportCode
testSupportCode ( ProjectNode
);
630 testSupportCode
.GenerateTestSupportCode ( configuration
.Verbose
);
635 MingwBackend::GenerateCompilationUnitSupportCode ()
637 if ( configuration
.CompilationUnitsEnabled
)
639 printf ( "Generating compilation unit support code..." );
640 CompilationUnitSupportCode
compilationUnitSupportCode ( ProjectNode
);
641 compilationUnitSupportCode
.Generate ( configuration
.Verbose
);
647 MingwBackend::GenerateSysSetup ()
649 printf ( "Generating syssetup.inf..." );
650 SysSetupGenerator
sysSetupGenerator ( ProjectNode
);
651 sysSetupGenerator
.Generate ();
656 MingwBackend::GetProxyMakefileTree () const
658 if ( configuration
.GenerateProxyMakefilesInSourceTree
)
661 return Environment::GetOutputPath ();
665 MingwBackend::GenerateProxyMakefiles ()
667 printf ( "Generating proxy makefiles..." );
668 ProxyMakefile
proxyMakefile ( ProjectNode
);
669 proxyMakefile
.GenerateProxyMakefiles ( configuration
.Verbose
,
670 GetProxyMakefileTree () );
675 MingwBackend::CheckAutomaticDependencies ()
677 if ( configuration
.AutomaticDependencies
)
679 printf ( "Checking automatic dependencies..." );
680 AutomaticDependency
automaticDependency ( ProjectNode
);
681 automaticDependency
.CheckAutomaticDependencies ( configuration
.Verbose
);
687 MingwBackend::GenerateDirectories ()
689 printf ( "Creating directories..." );
690 intermediateDirectory
->GenerateTree ( IntermediateDirectory
, configuration
.Verbose
);
691 outputDirectory
->GenerateTree ( OutputDirectory
, configuration
.Verbose
);
692 if ( !configuration
.MakeHandlesInstallDirectories
)
693 installDirectory
->GenerateTree ( InstallDirectory
, configuration
.Verbose
);
698 MingwBackend::TryToDetectThisCompiler ( const string
& compiler
)
700 string command
= ssprintf (
702 FixSeparatorForSystemCommand(compiler
).c_str (),
705 int exitcode
= system ( command
.c_str () );
706 return (bool) (exitcode
== 0);
710 MingwBackend::DetectCompiler ()
712 printf ( "Detecting compiler..." );
714 bool detectedCompiler
= false;
715 const string
& ROS_PREFIXValue
= Environment::GetVariable ( "ROS_PREFIX" );
716 if ( ROS_PREFIXValue
.length () > 0 )
718 compilerPrefix
= ROS_PREFIXValue
;
719 compilerCommand
= compilerPrefix
+ "-gcc";
720 detectedCompiler
= TryToDetectThisCompiler ( compilerCommand
);
723 if ( !detectedCompiler
)
726 compilerCommand
= "gcc";
727 detectedCompiler
= TryToDetectThisCompiler ( compilerCommand
);
730 if ( !detectedCompiler
)
732 compilerPrefix
= "mingw32";
733 compilerCommand
= compilerPrefix
+ "-gcc";
734 detectedCompiler
= TryToDetectThisCompiler ( compilerCommand
);
737 if ( detectedCompiler
)
739 string compilerVersion
= GetCompilerVersion ( compilerCommand
);
740 if ( IsSupportedCompilerVersion ( compilerVersion
) )
741 printf ( "detected (%s %s)\n", compilerCommand
.c_str (), compilerVersion
.c_str() );
744 printf ( "detected (%s), but with unsupported version (%s)\n",
745 compilerCommand
.c_str (),
746 compilerVersion
.c_str () );
747 throw UnsupportedBuildToolException ( compilerCommand
, compilerVersion
);
751 printf ( "not detected\n" );
756 MingwBackend::TryToDetectThisNetwideAssembler ( const string
& assembler
)
758 string command
= ssprintf (
760 FixSeparatorForSystemCommand(assembler
).c_str (),
763 int exitcode
= system ( command
.c_str () );
764 return (bool) (exitcode
== 0);
768 MingwBackend::GetVersionString ( const string
& versionCommand
)
775 fp
= popen ( versionCommand
.c_str () , "r" );
778 ( feof ( fp
) == 0 &&
779 ( ( ch
= fgetc( fp
) ) != -1 ) );
782 buffer
[i
] = (char) ch
;
787 char separators
[] = " ";
789 char *prevtoken
= NULL
;
793 token
= strtok ( buffer
, separators
);
794 while ( token
!= NULL
)
797 version
= string( prevtoken
);
798 if ( (newline
= version
.find('\n')) != std::string::npos
)
799 version
.erase(newline
, 1);
800 if ( version
.find('.') != std::string::npos
)
802 token
= strtok ( NULL
, separators
);
808 MingwBackend::GetNetwideAssemblerVersion ( const string
& nasmCommand
)
810 string versionCommand
;
811 if ( nasmCommand
.find("yasm") != std::string::npos
)
813 versionCommand
= ssprintf ( "%s --version",
814 nasmCommand
.c_str (),
820 versionCommand
= ssprintf ( "%s -v",
821 nasmCommand
.c_str (),
825 return GetVersionString( versionCommand
);
829 MingwBackend::GetCompilerVersion ( const string
& compilerCommand
)
831 string versionCommand
= ssprintf ( "%s --version gcc",
832 compilerCommand
.c_str (),
835 return GetVersionString( versionCommand
);
839 MingwBackend::GetBinutilsVersion ( const string
& binutilsCommand
)
841 string versionCommand
= ssprintf ( "%s -v",
842 binutilsCommand
.c_str (),
845 return GetVersionString( versionCommand
);
849 MingwBackend::IsSupportedCompilerVersion ( const string
& compilerVersion
)
851 if ( strcmp ( compilerVersion
.c_str (), "3.4.2") < 0 )
858 MingwBackend::TryToDetectThisBinutils ( const string
& binutils
)
860 string command
= ssprintf (
862 FixSeparatorForSystemCommand(binutils
).c_str (),
865 int exitcode
= system ( command
.c_str () );
866 return (exitcode
== 0);
870 MingwBackend::GetBinutilsVersionDate ( const string
& binutilsCommand
)
876 string versionCommand
= ssprintf ( "%s -v",
877 binutilsCommand
.c_str (),
880 fp
= popen ( versionCommand
.c_str () , "r" );
883 ( feof ( fp
) == 0 &&
884 ( ( ch
= fgetc( fp
) ) != -1 ) );
887 buffer
[i
] = (char) ch
;
892 char separators
[] = " ";
894 char *prevtoken
= NULL
;
896 token
= strtok ( buffer
, separators
);
897 while ( token
!= NULL
)
900 token
= strtok ( NULL
, separators
);
902 string version
= string ( prevtoken
);
903 int lastDigit
= version
.find_last_not_of ( "\t\r\n" );
904 if ( lastDigit
!= -1 )
905 return string ( version
, 0, lastDigit
+1 );
911 MingwBackend::IsSupportedBinutilsVersion ( const string
& binutilsVersion
)
913 if ( manualBinutilsSetting
) return true;
916 if ( binutilsVersion
.find('.') != std::string::npos
)
918 /* TODO: blacklist versions on version number instead of date */
923 * - Binutils older than 2003/10/01 have broken windres which can't handle
924 * icons with alpha channel.
925 * - Binutils between 2004/09/02 and 2004/10/08 have broken handling of
926 * forward exports in dlltool.
928 if ( ( ( strcmp ( binutilsVersion
.c_str (), "20040902") >= 0 ) &&
929 ( strcmp ( binutilsVersion
.c_str (), "20041008") <= 0 ) ) ||
930 ( strcmp ( binutilsVersion
.c_str (), "20031001") < 0 ) )
937 MingwBackend::DetectBinutils ()
939 printf ( "Detecting binutils..." );
941 bool detectedBinutils
= false;
942 const string
& ROS_PREFIXValue
= Environment::GetVariable ( "ROS_PREFIX" );
944 if ( ROS_PREFIXValue
.length () > 0 )
946 binutilsPrefix
= ROS_PREFIXValue
;
947 binutilsCommand
= binutilsPrefix
+ "-ld";
948 manualBinutilsSetting
= true;
949 detectedBinutils
= true;
952 if ( !detectedBinutils
)
955 binutilsCommand
= "ld";
956 detectedBinutils
= TryToDetectThisBinutils ( binutilsCommand
);
959 if ( !detectedBinutils
)
961 binutilsPrefix
= "mingw32";
962 binutilsCommand
= binutilsPrefix
+ "-ld";
963 detectedBinutils
= TryToDetectThisBinutils ( binutilsCommand
);
965 if ( detectedBinutils
)
967 string binutilsVersion
= GetBinutilsVersionDate ( binutilsCommand
);
968 if ( IsSupportedBinutilsVersion ( binutilsVersion
) )
969 printf ( "detected (%s %s)\n", binutilsCommand
.c_str (), GetBinutilsVersion( binutilsCommand
).c_str() );
972 printf ( "detected (%s), but with unsupported version (%s)\n",
973 binutilsCommand
.c_str (),
974 binutilsVersion
.c_str () );
975 throw UnsupportedBuildToolException ( binutilsCommand
, binutilsVersion
);
979 printf ( "not detected\n" );
984 MingwBackend::DetectNetwideAssembler ()
986 printf ( "Detecting netwide assembler..." );
988 nasmCommand
= "nasm";
989 bool detectedNasm
= TryToDetectThisNetwideAssembler ( nasmCommand
);
993 nasmCommand
= "nasmw";
994 detectedNasm
= TryToDetectThisNetwideAssembler ( nasmCommand
);
999 nasmCommand
= "yasm";
1000 detectedNasm
= TryToDetectThisNetwideAssembler ( nasmCommand
);
1003 printf ( "detected (%s %s)\n", nasmCommand
.c_str (), GetNetwideAssemblerVersion( nasmCommand
).c_str() );
1005 printf ( "not detected\n" );
1009 MingwBackend::DetectPipeSupport ()
1011 printf ( "Detecting compiler -pipe support..." );
1013 string pipe_detection
= "tools" + sSep
+ "rbuild" + sSep
+ "backend" + sSep
+ "mingw" + sSep
+ "pipe_detection.c";
1014 string pipe_detectionObjectFilename
= ReplaceExtension ( pipe_detection
,
1016 string command
= ssprintf (
1017 "%s -pipe -c %s -o %s 1>%s 2>%s",
1018 FixSeparatorForSystemCommand(compilerCommand
).c_str (),
1019 pipe_detection
.c_str (),
1020 pipe_detectionObjectFilename
.c_str (),
1023 int exitcode
= system ( command
.c_str () );
1024 FILE* f
= fopen ( pipe_detectionObjectFilename
.c_str (), "rb" );
1027 usePipe
= (exitcode
== 0);
1029 unlink ( pipe_detectionObjectFilename
.c_str () );
1035 printf ( "detected\n" );
1037 printf ( "not detected\n" );
1041 MingwBackend::DetectPCHSupport ()
1043 printf ( "Detecting compiler pre-compiled header support..." );
1045 if ( configuration
.PrecompiledHeadersEnabled
)
1047 string path
= "tools" + sSep
+ "rbuild" + sSep
+ "backend" + sSep
+ "mingw" + sSep
+ "pch_detection.h";
1048 string cmd
= ssprintf (
1049 "%s -c %s 1>%s 2>%s",
1050 FixSeparatorForSystemCommand(compilerCommand
).c_str (),
1054 system ( cmd
.c_str () );
1057 FILE* f
= fopen ( path
.c_str (), "rb" );
1062 unlink ( path
.c_str () );
1068 printf ( "detected\n" );
1070 printf ( "not detected\n" );
1075 printf ( "disabled\n" );
1080 MingwBackend::GetNonModuleInstallTargetFiles (
1081 vector
<FileLocation
>& out
) const
1083 for ( size_t i
= 0; i
< ProjectNode
.installfiles
.size (); i
++ )
1085 const InstallFile
& installfile
= *ProjectNode
.installfiles
[i
];
1086 out
.push_back ( *installfile
.target
);
1091 MingwBackend::GetModuleInstallTargetFiles (
1092 vector
<FileLocation
>& out
) const
1094 for ( std::map
<std::string
, Module
*>::const_iterator p
= ProjectNode
.modules
.begin (); p
!= ProjectNode
.modules
.end (); ++ p
)
1096 const Module
& module
= *p
->second
;
1097 if ( !module
.enabled
)
1099 if ( module
.install
)
1100 out
.push_back ( *module
.install
);
1105 MingwBackend::GetInstallTargetFiles (
1106 vector
<FileLocation
>& out
) const
1108 GetNonModuleInstallTargetFiles ( out
);
1109 GetModuleInstallTargetFiles ( out
);
1113 MingwBackend::OutputInstallTarget ( const FileLocation
& source
,
1114 const FileLocation
& target
)
1116 fprintf ( fMakefile
,
1118 GetFullName ( target
).c_str (),
1119 GetFullName ( source
).c_str (),
1120 GetFullPath ( target
).c_str () );
1121 fprintf ( fMakefile
,
1123 fprintf ( fMakefile
,
1124 "\t${cp} %s %s 1>$(NUL)\n",
1125 GetFullName ( source
).c_str (),
1126 GetFullName ( target
).c_str () );
1130 MingwBackend::OutputNonModuleInstallTargets ()
1132 for ( size_t i
= 0; i
< ProjectNode
.installfiles
.size (); i
++ )
1134 const InstallFile
& installfile
= *ProjectNode
.installfiles
[i
];
1135 OutputInstallTarget ( *installfile
.source
, *installfile
.target
);
1140 MingwBackend::GetAliasedModuleOrModule ( const Module
& module
) const
1142 if ( module
.aliasedModuleName
.size () > 0 )
1144 const Module
* aliasedModule
= ProjectNode
.LocateModule ( module
.aliasedModuleName
);
1145 assert ( aliasedModule
);
1146 return *aliasedModule
;
1153 MingwBackend::OutputModuleInstallTargets ()
1155 for ( std::map
<std::string
, Module
*>::const_iterator p
= ProjectNode
.modules
.begin (); p
!= ProjectNode
.modules
.end (); ++ p
)
1157 const Module
& module
= *p
->second
;
1158 if ( !module
.enabled
)
1160 if ( module
.install
)
1162 const Module
& aliasedModule
= GetAliasedModuleOrModule ( module
);
1163 OutputInstallTarget ( *aliasedModule
.output
, *module
.install
);
1169 MingwBackend::GetRegistrySourceFiles ()
1171 return "boot" + sSep
+ "bootdata" + sSep
+ "hivecls_" + Environment::GetArch() + ".inf "
1172 "boot" + sSep
+ "bootdata" + sSep
+ "hivedef_" + Environment::GetArch() + ".inf "
1173 "boot" + sSep
+ "bootdata" + sSep
+ "hiveinst_" + Environment::GetArch() + ".inf "
1174 "boot" + sSep
+ "bootdata" + sSep
+ "hivesft_" + Environment::GetArch() + ".inf "
1175 "boot" + sSep
+ "bootdata" + sSep
+ "hivesys_" + Environment::GetArch() + ".inf ";
1179 MingwBackend::GetRegistryTargetFiles ()
1181 string system32ConfigDirectory
= "system32" + sSep
+ "config";
1182 FileLocation
system32 ( InstallDirectory
, system32ConfigDirectory
, "" );
1184 vector
<FileLocation
> registry_files
;
1185 registry_files
.push_back ( FileLocation ( InstallDirectory
, system32ConfigDirectory
, "default" ) );
1186 registry_files
.push_back ( FileLocation ( InstallDirectory
, system32ConfigDirectory
, "sam" ) );
1187 registry_files
.push_back ( FileLocation ( InstallDirectory
, system32ConfigDirectory
, "security" ) );
1188 registry_files
.push_back ( FileLocation ( InstallDirectory
, system32ConfigDirectory
, "software" ) );
1189 registry_files
.push_back ( FileLocation ( InstallDirectory
, system32ConfigDirectory
, "system" ) );
1191 return v2s( this, registry_files
, 6 );
1195 MingwBackend::OutputRegistryInstallTarget ()
1197 FileLocation
system32 ( InstallDirectory
, "system32" + sSep
+ "config", "" );
1199 string registrySourceFiles
= GetRegistrySourceFiles ();
1200 string registryTargetFiles
= GetRegistryTargetFiles ();
1201 fprintf ( fMakefile
,
1202 "install_registry: %s\n",
1203 registryTargetFiles
.c_str () );
1204 fprintf ( fMakefile
,
1205 "%s: %s %s $(MKHIVE_TARGET)\n",
1206 registryTargetFiles
.c_str (),
1207 registrySourceFiles
.c_str (),
1208 GetFullPath ( system32
).c_str () );
1209 fprintf ( fMakefile
,
1210 "\t$(ECHO_MKHIVE)\n" );
1211 fprintf ( fMakefile
,
1212 "\t$(MKHIVE_TARGET) boot%cbootdata %s $(ARCH) boot%cbootdata%chiveinst_$(ARCH).inf\n",
1213 cSep
, GetFullPath ( system32
).c_str (),
1215 fprintf ( fMakefile
,
1220 MingwBackend::GenerateInstallTarget ()
1222 vector
<FileLocation
> vInstallTargetFiles
;
1223 GetInstallTargetFiles ( vInstallTargetFiles
);
1224 string installTargetFiles
= v2s ( this, vInstallTargetFiles
, 5 );
1225 string registryTargetFiles
= GetRegistryTargetFiles ();
1227 fprintf ( fMakefile
,
1229 installTargetFiles
.c_str (),
1230 registryTargetFiles
.c_str () );
1231 OutputNonModuleInstallTargets ();
1232 OutputModuleInstallTargets ();
1233 OutputRegistryInstallTarget ();
1234 fprintf ( fMakefile
,
1239 MingwBackend::GetModuleTestTargets (
1240 vector
<string
>& out
) const
1242 for ( std::map
<std::string
, Module
*>::const_iterator p
= ProjectNode
.modules
.begin (); p
!= ProjectNode
.modules
.end (); ++ p
)
1244 const Module
& module
= *p
->second
;
1245 if ( !module
.enabled
)
1247 if ( module
.type
== Test
)
1248 out
.push_back ( module
.name
);
1253 MingwBackend::GenerateTestTarget ()
1255 vector
<string
> vTestTargets
;
1256 GetModuleTestTargets ( vTestTargets
);
1257 string testTargets
= v2s ( vTestTargets
, 5 );
1259 fprintf ( fMakefile
,
1261 testTargets
.c_str () );
1262 fprintf ( fMakefile
,
1267 MingwBackend::GenerateDirectoryTargets ()
1269 intermediateDirectory
->CreateRule ( fMakefile
, "$(INTERMEDIATE)" );
1270 outputDirectory
->CreateRule ( fMakefile
, "$(OUTPUT)" );
1271 installDirectory
->CreateRule ( fMakefile
, "$(INSTALL)" );