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
.Dependencies
== 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
522 if ( configuration
.Dependencies
== FullDependencies
)
525 "ifeq ($(ROS_BUILDDEPS),)\n"
526 "ROS_BUILDDEPS:=%s\n"
533 compilerPrefix
.c_str () );
536 nasmCommand
.c_str () );
538 GenerateGlobalCFlagsAndProperties ( "=", ProjectNode
.non_if_data
);
539 GenerateProjectGccOptions ( "=", ProjectNode
.non_if_data
);
541 fprintf ( fMakefile
, "PROJECT_RCFLAGS := $(PROJECT_CINCLUDES) $(PROJECT_CDEFINES)\n" );
542 fprintf ( fMakefile
, "PROJECT_WIDLFLAGS := $(PROJECT_CINCLUDES) $(PROJECT_CDEFINES)\n" );
543 fprintf ( fMakefile
, "PROJECT_LFLAGS := '$(shell ${TARGET_CC} -print-libgcc-file-name)' %s\n", GenerateProjectLFLAGS ().c_str () );
544 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" );
545 fprintf ( fMakefile
, "PROJECT_GCCOPTIONS += -Wall\n" );
546 fprintf ( fMakefile
, "ifneq ($(OARCH),)\n" );
547 fprintf ( fMakefile
, "PROJECT_GCCOPTIONS += -march=$(OARCH)\n" );
548 fprintf ( fMakefile
, "endif\n" );
549 fprintf ( fMakefile
, "ifneq ($(TUNE),)\n" );
550 fprintf ( fMakefile
, "PROJECT_GCCOPTIONS += -mtune=$(TUNE)\n" );
551 fprintf ( fMakefile
, "endif\n" );
552 fprintf ( fMakefile
, "PROJECT_CFLAGS = $(PROJECT_GCCOPTIONS) $(PROJECT_GCC_CFLAGS)\n" );
553 fprintf ( fMakefile
, "PROJECT_CXXFLAGS = $(PROJECT_GCCOPTIONS) $(PROJECT_GCC_CXXFLAGS)\n" );
554 fprintf ( fMakefile
, "\n" );
558 MingwBackend::IncludeInAllTarget ( const Module
& module
) const
560 if ( MingwModuleHandler::ReferenceObjects ( module
) )
562 if ( module
.type
== BootSector
)
564 if ( module
.type
== Iso
)
566 if ( module
.type
== LiveIso
)
568 if ( module
.type
== IsoRegTest
)
570 if ( module
.type
== LiveIsoRegTest
)
572 if ( module
.type
== Test
)
574 if ( module
.type
== Alias
)
580 MingwBackend::GenerateAllTarget ( const vector
<MingwModuleHandler
*>& handlers
) const
582 fprintf ( fMakefile
, "all:" );
584 size_t iend
= handlers
.size ();
585 for ( size_t i
= 0; i
< iend
; i
++ )
587 const Module
& module
= handlers
[i
]->module
;
588 if ( IncludeInAllTarget ( module
) )
590 if ( wrap_count
++ == 5 )
591 fprintf ( fMakefile
, " \\\n\t\t" ), wrap_count
= 0;
594 GetTargetMacro(module
).c_str () );
597 fprintf ( fMakefile
, "\n\t\n\n" );
601 MingwBackend::GenerateRegTestsRunTarget () const
604 "REGTESTS_RUN_TARGET = regtests.dll\n" );
606 "$(REGTESTS_RUN_TARGET): $(REGTESTS_TARGET)\n" );
608 "\t$(cp) $(REGTESTS_TARGET) $(REGTESTS_RUN_TARGET)\n" );
609 fprintf ( fMakefile
, "\n" );
613 MingwBackend::GenerateXmlBuildFilesMacro() const
616 "XMLBUILDFILES = %s \\\n",
617 ProjectNode
.GetProjectFilename ().c_str () );
618 string xmlbuildFilenames
;
619 int numberOfExistingFiles
= 0;
621 time_t SystemTime
, lastWriteTime
;
623 for ( size_t i
= 0; i
< ProjectNode
.xmlbuildfiles
.size (); i
++ )
625 XMLInclude
& xmlbuildfile
= *ProjectNode
.xmlbuildfiles
[i
];
626 if ( !xmlbuildfile
.fileExists
)
628 numberOfExistingFiles
++;
629 if ( xmlbuildFilenames
.length () > 0 )
630 xmlbuildFilenames
+= " ";
632 FILE* f
= fopen ( xmlbuildfile
.topIncludeFilename
.c_str (), "rb" );
634 throw FileNotFoundException ( NormalizeFilename ( xmlbuildfile
.topIncludeFilename
) );
636 if ( fstat ( fileno ( f
), &statbuf
) != 0 )
639 throw AccessDeniedException ( NormalizeFilename ( xmlbuildfile
.topIncludeFilename
) );
642 lastWriteTime
= statbuf
.st_mtime
;
643 SystemTime
= time(NULL
);
645 if (SystemTime
!= -1)
647 if (difftime (lastWriteTime
, SystemTime
) > 0)
648 throw InvalidDateException ( NormalizeFilename ( xmlbuildfile
.topIncludeFilename
) );
653 xmlbuildFilenames
+= NormalizeFilename ( xmlbuildfile
.topIncludeFilename
);
654 if ( numberOfExistingFiles
% 5 == 4 || i
== ProjectNode
.xmlbuildfiles
.size () - 1 )
658 xmlbuildFilenames
.c_str ());
659 if ( i
== ProjectNode
.xmlbuildfiles
.size () - 1 )
661 fprintf ( fMakefile
, "\n" );
668 xmlbuildFilenames
.resize ( 0 );
670 numberOfExistingFiles
++;
672 fprintf ( fMakefile
, "\n" );
676 MingwBackend::GenerateTestSupportCode ()
678 printf ( "Generating test support code..." );
679 TestSupportCode
testSupportCode ( ProjectNode
);
680 testSupportCode
.GenerateTestSupportCode ( configuration
.Verbose
);
685 MingwBackend::GenerateCompilationUnitSupportCode ()
687 if ( configuration
.CompilationUnitsEnabled
)
689 printf ( "Generating compilation unit support code..." );
690 CompilationUnitSupportCode
compilationUnitSupportCode ( ProjectNode
);
691 compilationUnitSupportCode
.Generate ( configuration
.Verbose
);
697 MingwBackend::GenerateSysSetup ()
699 printf ( "Generating syssetup.inf..." );
700 SysSetupGenerator
sysSetupGenerator ( ProjectNode
);
701 sysSetupGenerator
.Generate ();
706 MingwBackend::GetProxyMakefileTree () const
708 if ( configuration
.GenerateProxyMakefilesInSourceTree
)
711 return Environment::GetOutputPath ();
715 MingwBackend::GenerateProxyMakefiles ()
717 printf ( "Generating proxy makefiles..." );
718 ProxyMakefile
proxyMakefile ( ProjectNode
);
719 proxyMakefile
.GenerateProxyMakefiles ( configuration
.Verbose
,
720 GetProxyMakefileTree () );
725 MingwBackend::CheckAutomaticDependencies ()
727 if ( configuration
.Dependencies
== AutomaticDependencies
)
729 printf ( "Checking automatic dependencies..." );
730 AutomaticDependency
automaticDependency ( ProjectNode
);
731 automaticDependency
.CheckAutomaticDependencies ( configuration
.Verbose
);
737 MingwBackend::GenerateDirectories ()
739 printf ( "Creating directories..." );
740 intermediateDirectory
->GenerateTree ( IntermediateDirectory
, configuration
.Verbose
);
741 outputDirectory
->GenerateTree ( OutputDirectory
, configuration
.Verbose
);
742 if ( !configuration
.MakeHandlesInstallDirectories
)
743 installDirectory
->GenerateTree ( InstallDirectory
, configuration
.Verbose
);
748 MingwBackend::TryToDetectThisCompiler ( const string
& compiler
)
750 string command
= ssprintf (
752 FixSeparatorForSystemCommand(compiler
).c_str (),
755 int exitcode
= system ( command
.c_str () );
756 return (bool) (exitcode
== 0);
760 MingwBackend::DetectCompiler ()
762 printf ( "Detecting compiler..." );
764 bool detectedCompiler
= false;
765 const string
& ROS_PREFIXValue
= Environment::GetVariable ( "ROS_PREFIX" );
766 if ( ROS_PREFIXValue
.length () > 0 )
768 compilerPrefix
= ROS_PREFIXValue
;
769 compilerCommand
= compilerPrefix
+ "-gcc";
770 detectedCompiler
= TryToDetectThisCompiler ( compilerCommand
);
773 if ( !detectedCompiler
)
776 compilerCommand
= "gcc";
777 detectedCompiler
= TryToDetectThisCompiler ( compilerCommand
);
780 if ( !detectedCompiler
)
782 compilerPrefix
= "mingw32";
783 compilerCommand
= compilerPrefix
+ "-gcc";
784 detectedCompiler
= TryToDetectThisCompiler ( compilerCommand
);
787 if ( detectedCompiler
)
789 string compilerVersion
= GetCompilerVersion ( compilerCommand
);
790 if ( IsSupportedCompilerVersion ( compilerVersion
) )
791 printf ( "detected (%s %s)\n", compilerCommand
.c_str (), compilerVersion
.c_str() );
794 printf ( "detected (%s), but with unsupported version (%s)\n",
795 compilerCommand
.c_str (),
796 compilerVersion
.c_str () );
797 throw UnsupportedBuildToolException ( compilerCommand
, compilerVersion
);
801 printf ( "not detected\n" );
806 MingwBackend::TryToDetectThisNetwideAssembler ( const string
& assembler
)
808 string command
= ssprintf (
810 FixSeparatorForSystemCommand(assembler
).c_str (),
813 int exitcode
= system ( command
.c_str () );
814 return (bool) (exitcode
== 0);
818 MingwBackend::GetVersionString ( const string
& versionCommand
)
825 fp
= popen ( versionCommand
.c_str () , "r" );
828 ( feof ( fp
) == 0 &&
829 ( ( ch
= fgetc( fp
) ) != -1 ) );
832 buffer
[i
] = (char) ch
;
837 char separators
[] = " ()";
839 char *prevtoken
= NULL
;
843 token
= strtok ( buffer
, separators
);
844 while ( token
!= NULL
)
847 version
= string( prevtoken
);
848 if ( (newline
= version
.find('\n')) != std::string::npos
)
849 version
.erase(newline
, 1);
850 if ( version
.find('.') != std::string::npos
)
852 token
= strtok ( NULL
, separators
);
858 MingwBackend::GetNetwideAssemblerVersion ( const string
& nasmCommand
)
860 string versionCommand
;
861 if ( nasmCommand
.find("yasm") != std::string::npos
)
863 versionCommand
= ssprintf ( "%s --version",
864 nasmCommand
.c_str (),
870 versionCommand
= ssprintf ( "%s -v",
871 nasmCommand
.c_str (),
875 return GetVersionString( versionCommand
);
879 MingwBackend::GetCompilerVersion ( const string
& compilerCommand
)
881 string versionCommand
= ssprintf ( "%s --version gcc",
882 compilerCommand
.c_str (),
885 return GetVersionString( versionCommand
);
889 MingwBackend::GetBinutilsVersion ( const string
& binutilsCommand
)
891 string versionCommand
= ssprintf ( "%s -v",
892 binutilsCommand
.c_str (),
895 return GetVersionString( versionCommand
);
899 MingwBackend::IsSupportedCompilerVersion ( const string
& compilerVersion
)
901 if ( strcmp ( compilerVersion
.c_str (), "3.4.2") < 0 )
908 MingwBackend::TryToDetectThisBinutils ( const string
& binutils
)
910 string command
= ssprintf (
912 FixSeparatorForSystemCommand(binutils
).c_str (),
915 int exitcode
= system ( command
.c_str () );
916 return (exitcode
== 0);
920 MingwBackend::GetBinutilsVersionDate ( const string
& binutilsCommand
)
926 string versionCommand
= ssprintf ( "%s -v",
927 binutilsCommand
.c_str (),
930 fp
= popen ( versionCommand
.c_str () , "r" );
933 ( feof ( fp
) == 0 &&
934 ( ( ch
= fgetc( fp
) ) != -1 ) );
937 buffer
[i
] = (char) ch
;
942 char separators
[] = " ";
944 char *prevtoken
= NULL
;
946 token
= strtok ( buffer
, separators
);
947 while ( token
!= NULL
)
950 token
= strtok ( NULL
, separators
);
952 string version
= string ( prevtoken
);
953 int lastDigit
= version
.find_last_not_of ( "\t\r\n" );
954 if ( lastDigit
!= -1 )
955 return string ( version
, 0, lastDigit
+1 );
961 MingwBackend::IsSupportedBinutilsVersion ( const string
& binutilsVersion
)
963 if ( manualBinutilsSetting
) return true;
966 if ( binutilsVersion
.find('.') != std::string::npos
)
968 /* TODO: blacklist versions on version number instead of date */
973 * - Binutils older than 2003/10/01 have broken windres which can't handle
974 * icons with alpha channel.
975 * - Binutils between 2004/09/02 and 2004/10/08 have broken handling of
976 * forward exports in dlltool.
978 if ( ( ( strcmp ( binutilsVersion
.c_str (), "20040902") >= 0 ) &&
979 ( strcmp ( binutilsVersion
.c_str (), "20041008") <= 0 ) ) ||
980 ( strcmp ( binutilsVersion
.c_str (), "20031001") < 0 ) )
987 MingwBackend::DetectBinutils ()
989 printf ( "Detecting binutils..." );
991 bool detectedBinutils
= false;
992 const string
& ROS_PREFIXValue
= Environment::GetVariable ( "ROS_PREFIX" );
994 if ( ROS_PREFIXValue
.length () > 0 )
996 binutilsPrefix
= ROS_PREFIXValue
;
997 binutilsCommand
= binutilsPrefix
+ "-ld";
998 manualBinutilsSetting
= true;
999 detectedBinutils
= true;
1002 if ( !detectedBinutils
)
1004 binutilsPrefix
= "";
1005 binutilsCommand
= "ld";
1006 detectedBinutils
= TryToDetectThisBinutils ( binutilsCommand
);
1009 if ( !detectedBinutils
)
1011 binutilsPrefix
= "mingw32";
1012 binutilsCommand
= binutilsPrefix
+ "-ld";
1013 detectedBinutils
= TryToDetectThisBinutils ( binutilsCommand
);
1015 if ( detectedBinutils
)
1017 string binutilsVersion
= GetBinutilsVersionDate ( binutilsCommand
);
1018 if ( IsSupportedBinutilsVersion ( binutilsVersion
) )
1019 printf ( "detected (%s %s)\n", binutilsCommand
.c_str (), GetBinutilsVersion( binutilsCommand
).c_str() );
1022 printf ( "detected (%s), but with unsupported version (%s)\n",
1023 binutilsCommand
.c_str (),
1024 binutilsVersion
.c_str () );
1025 throw UnsupportedBuildToolException ( binutilsCommand
, binutilsVersion
);
1029 printf ( "not detected\n" );
1034 MingwBackend::DetectNetwideAssembler ()
1036 printf ( "Detecting netwide assembler..." );
1038 nasmCommand
= "nasm";
1039 bool detectedNasm
= TryToDetectThisNetwideAssembler ( nasmCommand
);
1041 if ( !detectedNasm
)
1043 nasmCommand
= "nasmw";
1044 detectedNasm
= TryToDetectThisNetwideAssembler ( nasmCommand
);
1047 if ( !detectedNasm
)
1049 nasmCommand
= "yasm";
1050 detectedNasm
= TryToDetectThisNetwideAssembler ( nasmCommand
);
1053 printf ( "detected (%s %s)\n", nasmCommand
.c_str (), GetNetwideAssemblerVersion( nasmCommand
).c_str() );
1055 printf ( "not detected\n" );
1059 MingwBackend::DetectPipeSupport ()
1061 printf ( "Detecting compiler -pipe support..." );
1063 string pipe_detection
= "tools" + sSep
+ "rbuild" + sSep
+ "backend" + sSep
+ "mingw" + sSep
+ "pipe_detection.c";
1064 string pipe_detectionObjectFilename
= ReplaceExtension ( pipe_detection
,
1066 string command
= ssprintf (
1067 "%s -pipe -c %s -o %s 1>%s 2>%s",
1068 FixSeparatorForSystemCommand(compilerCommand
).c_str (),
1069 pipe_detection
.c_str (),
1070 pipe_detectionObjectFilename
.c_str (),
1073 int exitcode
= system ( command
.c_str () );
1074 FILE* f
= fopen ( pipe_detectionObjectFilename
.c_str (), "rb" );
1077 usePipe
= (exitcode
== 0);
1079 unlink ( pipe_detectionObjectFilename
.c_str () );
1085 printf ( "detected\n" );
1087 printf ( "not detected\n" );
1091 MingwBackend::DetectPCHSupport ()
1093 printf ( "Detecting compiler pre-compiled header support..." );
1095 if ( configuration
.PrecompiledHeadersEnabled
)
1097 string path
= "tools" + sSep
+ "rbuild" + sSep
+ "backend" + sSep
+ "mingw" + sSep
+ "pch_detection.h";
1098 string cmd
= ssprintf (
1099 "%s -c %s 1>%s 2>%s",
1100 FixSeparatorForSystemCommand(compilerCommand
).c_str (),
1104 system ( cmd
.c_str () );
1107 FILE* f
= fopen ( path
.c_str (), "rb" );
1112 unlink ( path
.c_str () );
1118 printf ( "detected\n" );
1120 printf ( "not detected\n" );
1125 printf ( "disabled\n" );
1130 MingwBackend::GetNonModuleInstallTargetFiles (
1131 vector
<FileLocation
>& out
) const
1133 for ( size_t i
= 0; i
< ProjectNode
.installfiles
.size (); i
++ )
1135 const InstallFile
& installfile
= *ProjectNode
.installfiles
[i
];
1136 out
.push_back ( *installfile
.target
);
1141 MingwBackend::GetModuleInstallTargetFiles (
1142 vector
<FileLocation
>& out
) const
1144 for ( std::map
<std::string
, Module
*>::const_iterator p
= ProjectNode
.modules
.begin (); p
!= ProjectNode
.modules
.end (); ++ p
)
1146 const Module
& module
= *p
->second
;
1147 if ( !module
.enabled
)
1149 if ( module
.install
)
1150 out
.push_back ( *module
.install
);
1155 MingwBackend::GetInstallTargetFiles (
1156 vector
<FileLocation
>& out
) const
1158 GetNonModuleInstallTargetFiles ( out
);
1159 GetModuleInstallTargetFiles ( out
);
1163 MingwBackend::OutputInstallTarget ( const FileLocation
& source
,
1164 const FileLocation
& target
)
1166 fprintf ( fMakefile
,
1168 GetFullName ( target
).c_str (),
1169 GetFullName ( source
).c_str (),
1170 GetFullPath ( target
).c_str () );
1171 fprintf ( fMakefile
,
1173 fprintf ( fMakefile
,
1174 "\t${cp} %s %s 1>$(NUL)\n",
1175 GetFullName ( source
).c_str (),
1176 GetFullName ( target
).c_str () );
1180 MingwBackend::OutputNonModuleInstallTargets ()
1182 for ( size_t i
= 0; i
< ProjectNode
.installfiles
.size (); i
++ )
1184 const InstallFile
& installfile
= *ProjectNode
.installfiles
[i
];
1185 OutputInstallTarget ( *installfile
.source
, *installfile
.target
);
1190 MingwBackend::GetAliasedModuleOrModule ( const Module
& module
) const
1192 if ( module
.aliasedModuleName
.size () > 0 )
1194 const Module
* aliasedModule
= ProjectNode
.LocateModule ( module
.aliasedModuleName
);
1195 assert ( aliasedModule
);
1196 return *aliasedModule
;
1203 MingwBackend::OutputModuleInstallTargets ()
1205 for ( std::map
<std::string
, Module
*>::const_iterator p
= ProjectNode
.modules
.begin (); p
!= ProjectNode
.modules
.end (); ++ p
)
1207 const Module
& module
= *p
->second
;
1208 if ( !module
.enabled
)
1210 if ( module
.install
)
1212 const Module
& aliasedModule
= GetAliasedModuleOrModule ( module
);
1213 OutputInstallTarget ( *aliasedModule
.output
, *module
.install
);
1219 MingwBackend::GetRegistrySourceFiles ()
1221 return "boot" + sSep
+ "bootdata" + sSep
+ "hivecls_" + Environment::GetArch() + ".inf "
1222 "boot" + sSep
+ "bootdata" + sSep
+ "hivedef_" + Environment::GetArch() + ".inf "
1223 "boot" + sSep
+ "bootdata" + sSep
+ "hiveinst_" + Environment::GetArch() + ".inf "
1224 "boot" + sSep
+ "bootdata" + sSep
+ "hivesft_" + Environment::GetArch() + ".inf "
1225 "boot" + sSep
+ "bootdata" + sSep
+ "hivesys_" + Environment::GetArch() + ".inf ";
1229 MingwBackend::GetRegistryTargetFiles ()
1231 string system32ConfigDirectory
= "system32" + sSep
+ "config";
1232 FileLocation
system32 ( InstallDirectory
, system32ConfigDirectory
, "" );
1234 vector
<FileLocation
> registry_files
;
1235 registry_files
.push_back ( FileLocation ( InstallDirectory
, system32ConfigDirectory
, "default" ) );
1236 registry_files
.push_back ( FileLocation ( InstallDirectory
, system32ConfigDirectory
, "sam" ) );
1237 registry_files
.push_back ( FileLocation ( InstallDirectory
, system32ConfigDirectory
, "security" ) );
1238 registry_files
.push_back ( FileLocation ( InstallDirectory
, system32ConfigDirectory
, "software" ) );
1239 registry_files
.push_back ( FileLocation ( InstallDirectory
, system32ConfigDirectory
, "system" ) );
1241 return v2s( this, registry_files
, 6 );
1245 MingwBackend::OutputRegistryInstallTarget ()
1247 FileLocation
system32 ( InstallDirectory
, "system32" + sSep
+ "config", "" );
1249 string registrySourceFiles
= GetRegistrySourceFiles ();
1250 string registryTargetFiles
= GetRegistryTargetFiles ();
1251 fprintf ( fMakefile
,
1252 "install_registry: %s\n",
1253 registryTargetFiles
.c_str () );
1254 fprintf ( fMakefile
,
1255 "%s: %s %s $(MKHIVE_TARGET)\n",
1256 registryTargetFiles
.c_str (),
1257 registrySourceFiles
.c_str (),
1258 GetFullPath ( system32
).c_str () );
1259 fprintf ( fMakefile
,
1260 "\t$(ECHO_MKHIVE)\n" );
1261 fprintf ( fMakefile
,
1262 "\t$(MKHIVE_TARGET) boot%cbootdata %s $(ARCH) boot%cbootdata%chiveinst_$(ARCH).inf\n",
1263 cSep
, GetFullPath ( system32
).c_str (),
1265 fprintf ( fMakefile
,
1270 MingwBackend::GenerateInstallTarget ()
1272 vector
<FileLocation
> vInstallTargetFiles
;
1273 GetInstallTargetFiles ( vInstallTargetFiles
);
1274 string installTargetFiles
= v2s ( this, vInstallTargetFiles
, 5 );
1275 string registryTargetFiles
= GetRegistryTargetFiles ();
1277 fprintf ( fMakefile
,
1279 installTargetFiles
.c_str (),
1280 registryTargetFiles
.c_str () );
1281 OutputNonModuleInstallTargets ();
1282 OutputModuleInstallTargets ();
1283 OutputRegistryInstallTarget ();
1284 fprintf ( fMakefile
,
1289 MingwBackend::GetModuleTestTargets (
1290 vector
<string
>& out
) const
1292 for ( std::map
<std::string
, Module
*>::const_iterator p
= ProjectNode
.modules
.begin (); p
!= ProjectNode
.modules
.end (); ++ p
)
1294 const Module
& module
= *p
->second
;
1295 if ( !module
.enabled
)
1297 if ( module
.type
== Test
)
1298 out
.push_back ( module
.name
);
1303 MingwBackend::GenerateTestTarget ()
1305 vector
<string
> vTestTargets
;
1306 GetModuleTestTargets ( vTestTargets
);
1307 string testTargets
= v2s ( vTestTargets
, 5 );
1309 fprintf ( fMakefile
,
1311 testTargets
.c_str () );
1312 fprintf ( fMakefile
,
1317 MingwBackend::GenerateDirectoryTargets ()
1319 intermediateDirectory
->CreateRule ( fMakefile
, "$(INTERMEDIATE)" );
1320 outputDirectory
->CreateRule ( fMakefile
, "$(OUTPUT)" );
1321 installDirectory
->CreateRule ( fMakefile
, "$(INSTALL)" );