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 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_CFLAGS) $(PROJECT_CDEFINES)\n" );
533 fprintf ( fMakefile
, "PROJECT_WIDLFLAGS := $(PROJECT_CFLAGS) $(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)'\n" );
536 fprintf ( fMakefile
, "PROJECT_CFLAGS += -Wall\n" );
537 fprintf ( fMakefile
, "ifneq ($(OARCH),)\n" );
538 fprintf ( fMakefile
, "PROJECT_CFLAGS += -march=$(OARCH)\n" );
539 fprintf ( fMakefile
, "endif\n" );
540 fprintf ( fMakefile
, "PROJECT_CFLAGS += $(PROJECT_GCCOPTIONS)\n" );
541 fprintf ( fMakefile
, "\n" );
545 MingwBackend::IncludeInAllTarget ( const Module
& module
) const
547 if ( MingwModuleHandler::ReferenceObjects ( module
) )
549 if ( module
.type
== BootSector
)
551 if ( module
.type
== Iso
)
553 if ( module
.type
== LiveIso
)
555 if ( module
.type
== IsoRegTest
)
557 if ( module
.type
== LiveIsoRegTest
)
559 if ( module
.type
== Test
)
561 if ( module
.type
== Alias
)
567 MingwBackend::GenerateAllTarget ( const vector
<MingwModuleHandler
*>& handlers
) const
569 fprintf ( fMakefile
, "all:" );
571 size_t iend
= handlers
.size ();
572 for ( size_t i
= 0; i
< iend
; i
++ )
574 const Module
& module
= handlers
[i
]->module
;
575 if ( IncludeInAllTarget ( module
) )
577 if ( wrap_count
++ == 5 )
578 fprintf ( fMakefile
, " \\\n\t\t" ), wrap_count
= 0;
581 GetTargetMacro(module
).c_str () );
584 fprintf ( fMakefile
, "\n\t\n\n" );
588 MingwBackend::GenerateRegTestsRunTarget () const
591 "REGTESTS_RUN_TARGET = regtests.dll\n" );
593 "$(REGTESTS_RUN_TARGET): $(REGTESTS_TARGET)\n" );
595 "\t$(cp) $(REGTESTS_TARGET) $(REGTESTS_RUN_TARGET)\n" );
596 fprintf ( fMakefile
, "\n" );
600 MingwBackend::GenerateXmlBuildFilesMacro() const
603 "XMLBUILDFILES = %s \\\n",
604 ProjectNode
.GetProjectFilename ().c_str () );
605 string xmlbuildFilenames
;
606 int numberOfExistingFiles
= 0;
608 time_t SystemTime
, lastWriteTime
;
610 for ( size_t i
= 0; i
< ProjectNode
.xmlbuildfiles
.size (); i
++ )
612 XMLInclude
& xmlbuildfile
= *ProjectNode
.xmlbuildfiles
[i
];
613 if ( !xmlbuildfile
.fileExists
)
615 numberOfExistingFiles
++;
616 if ( xmlbuildFilenames
.length () > 0 )
617 xmlbuildFilenames
+= " ";
619 FILE* f
= fopen ( xmlbuildfile
.topIncludeFilename
.c_str (), "rb" );
621 throw FileNotFoundException ( NormalizeFilename ( xmlbuildfile
.topIncludeFilename
) );
623 if ( fstat ( fileno ( f
), &statbuf
) != 0 )
626 throw AccessDeniedException ( NormalizeFilename ( xmlbuildfile
.topIncludeFilename
) );
629 lastWriteTime
= statbuf
.st_mtime
;
630 SystemTime
= time(NULL
);
632 if (SystemTime
!= -1)
634 if (difftime (lastWriteTime
, SystemTime
) > 0)
635 throw InvalidDateException ( NormalizeFilename ( xmlbuildfile
.topIncludeFilename
) );
640 xmlbuildFilenames
+= NormalizeFilename ( xmlbuildfile
.topIncludeFilename
);
641 if ( numberOfExistingFiles
% 5 == 4 || i
== ProjectNode
.xmlbuildfiles
.size () - 1 )
645 xmlbuildFilenames
.c_str ());
646 if ( i
== ProjectNode
.xmlbuildfiles
.size () - 1 )
648 fprintf ( fMakefile
, "\n" );
655 xmlbuildFilenames
.resize ( 0 );
657 numberOfExistingFiles
++;
659 fprintf ( fMakefile
, "\n" );
663 MingwBackend::GenerateTestSupportCode ()
665 printf ( "Generating test support code..." );
666 TestSupportCode
testSupportCode ( ProjectNode
);
667 testSupportCode
.GenerateTestSupportCode ( configuration
.Verbose
);
672 MingwBackend::GenerateCompilationUnitSupportCode ()
674 if ( configuration
.CompilationUnitsEnabled
)
676 printf ( "Generating compilation unit support code..." );
677 CompilationUnitSupportCode
compilationUnitSupportCode ( ProjectNode
);
678 compilationUnitSupportCode
.Generate ( configuration
.Verbose
);
684 MingwBackend::GenerateSysSetup ()
686 printf ( "Generating syssetup.inf..." );
687 SysSetupGenerator
sysSetupGenerator ( ProjectNode
);
688 sysSetupGenerator
.Generate ();
693 MingwBackend::GetProxyMakefileTree () const
695 if ( configuration
.GenerateProxyMakefilesInSourceTree
)
698 return Environment::GetOutputPath ();
702 MingwBackend::GenerateProxyMakefiles ()
704 printf ( "Generating proxy makefiles..." );
705 ProxyMakefile
proxyMakefile ( ProjectNode
);
706 proxyMakefile
.GenerateProxyMakefiles ( configuration
.Verbose
,
707 GetProxyMakefileTree () );
712 MingwBackend::CheckAutomaticDependencies ()
714 if ( configuration
.AutomaticDependencies
)
716 printf ( "Checking automatic dependencies..." );
717 AutomaticDependency
automaticDependency ( ProjectNode
);
718 automaticDependency
.CheckAutomaticDependencies ( configuration
.Verbose
);
724 MingwBackend::GenerateDirectories ()
726 printf ( "Creating directories..." );
727 intermediateDirectory
->GenerateTree ( IntermediateDirectory
, configuration
.Verbose
);
728 outputDirectory
->GenerateTree ( OutputDirectory
, configuration
.Verbose
);
729 if ( !configuration
.MakeHandlesInstallDirectories
)
730 installDirectory
->GenerateTree ( InstallDirectory
, configuration
.Verbose
);
735 MingwBackend::TryToDetectThisCompiler ( const string
& compiler
)
737 string command
= ssprintf (
739 FixSeparatorForSystemCommand(compiler
).c_str (),
742 int exitcode
= system ( command
.c_str () );
743 return (bool) (exitcode
== 0);
747 MingwBackend::DetectCompiler ()
749 printf ( "Detecting compiler..." );
751 bool detectedCompiler
= false;
752 const string
& ROS_PREFIXValue
= Environment::GetVariable ( "ROS_PREFIX" );
753 if ( ROS_PREFIXValue
.length () > 0 )
755 compilerPrefix
= ROS_PREFIXValue
;
756 compilerCommand
= compilerPrefix
+ "-gcc";
757 detectedCompiler
= TryToDetectThisCompiler ( compilerCommand
);
760 if ( !detectedCompiler
)
763 compilerCommand
= "gcc";
764 detectedCompiler
= TryToDetectThisCompiler ( compilerCommand
);
767 if ( !detectedCompiler
)
769 compilerPrefix
= "mingw32";
770 compilerCommand
= compilerPrefix
+ "-gcc";
771 detectedCompiler
= TryToDetectThisCompiler ( compilerCommand
);
774 if ( detectedCompiler
)
776 string compilerVersion
= GetCompilerVersion ( compilerCommand
);
777 if ( IsSupportedCompilerVersion ( compilerVersion
) )
778 printf ( "detected (%s %s)\n", compilerCommand
.c_str (), compilerVersion
.c_str() );
781 printf ( "detected (%s), but with unsupported version (%s)\n",
782 compilerCommand
.c_str (),
783 compilerVersion
.c_str () );
784 throw UnsupportedBuildToolException ( compilerCommand
, compilerVersion
);
788 printf ( "not detected\n" );
793 MingwBackend::TryToDetectThisNetwideAssembler ( const string
& assembler
)
795 string command
= ssprintf (
797 FixSeparatorForSystemCommand(assembler
).c_str (),
800 int exitcode
= system ( command
.c_str () );
801 return (bool) (exitcode
== 0);
805 MingwBackend::GetVersionString ( const string
& versionCommand
)
812 fp
= popen ( versionCommand
.c_str () , "r" );
815 ( feof ( fp
) == 0 &&
816 ( ( ch
= fgetc( fp
) ) != -1 ) );
819 buffer
[i
] = (char) ch
;
824 char separators
[] = " ";
826 char *prevtoken
= NULL
;
830 token
= strtok ( buffer
, separators
);
831 while ( token
!= NULL
)
834 version
= string( prevtoken
);
835 if ( (newline
= version
.find('\n')) != std::string::npos
)
836 version
.erase(newline
, 1);
837 if ( version
.find('.') != std::string::npos
)
839 token
= strtok ( NULL
, separators
);
845 MingwBackend::GetNetwideAssemblerVersion ( const string
& nasmCommand
)
847 string versionCommand
;
848 if ( nasmCommand
.find("yasm") != std::string::npos
)
850 versionCommand
= ssprintf ( "%s --version",
851 nasmCommand
.c_str (),
857 versionCommand
= ssprintf ( "%s -v",
858 nasmCommand
.c_str (),
862 return GetVersionString( versionCommand
);
866 MingwBackend::GetCompilerVersion ( const string
& compilerCommand
)
868 string versionCommand
= ssprintf ( "%s --version gcc",
869 compilerCommand
.c_str (),
872 return GetVersionString( versionCommand
);
876 MingwBackend::GetBinutilsVersion ( const string
& binutilsCommand
)
878 string versionCommand
= ssprintf ( "%s -v",
879 binutilsCommand
.c_str (),
882 return GetVersionString( versionCommand
);
886 MingwBackend::IsSupportedCompilerVersion ( const string
& compilerVersion
)
888 if ( strcmp ( compilerVersion
.c_str (), "3.4.2") < 0 )
895 MingwBackend::TryToDetectThisBinutils ( const string
& binutils
)
897 string command
= ssprintf (
899 FixSeparatorForSystemCommand(binutils
).c_str (),
902 int exitcode
= system ( command
.c_str () );
903 return (exitcode
== 0);
907 MingwBackend::GetBinutilsVersionDate ( const string
& binutilsCommand
)
913 string versionCommand
= ssprintf ( "%s -v",
914 binutilsCommand
.c_str (),
917 fp
= popen ( versionCommand
.c_str () , "r" );
920 ( feof ( fp
) == 0 &&
921 ( ( ch
= fgetc( fp
) ) != -1 ) );
924 buffer
[i
] = (char) ch
;
929 char separators
[] = " ";
931 char *prevtoken
= NULL
;
933 token
= strtok ( buffer
, separators
);
934 while ( token
!= NULL
)
937 token
= strtok ( NULL
, separators
);
939 string version
= string ( prevtoken
);
940 int lastDigit
= version
.find_last_not_of ( "\t\r\n" );
941 if ( lastDigit
!= -1 )
942 return string ( version
, 0, lastDigit
+1 );
948 MingwBackend::IsSupportedBinutilsVersion ( const string
& binutilsVersion
)
950 if ( manualBinutilsSetting
) return true;
953 if ( binutilsVersion
.find('.') != std::string::npos
)
955 /* TODO: blacklist versions on version number instead of date */
960 * - Binutils older than 2003/10/01 have broken windres which can't handle
961 * icons with alpha channel.
962 * - Binutils between 2004/09/02 and 2004/10/08 have broken handling of
963 * forward exports in dlltool.
965 if ( ( ( strcmp ( binutilsVersion
.c_str (), "20040902") >= 0 ) &&
966 ( strcmp ( binutilsVersion
.c_str (), "20041008") <= 0 ) ) ||
967 ( strcmp ( binutilsVersion
.c_str (), "20031001") < 0 ) )
974 MingwBackend::DetectBinutils ()
976 printf ( "Detecting binutils..." );
978 bool detectedBinutils
= false;
979 const string
& ROS_PREFIXValue
= Environment::GetVariable ( "ROS_PREFIX" );
981 if ( ROS_PREFIXValue
.length () > 0 )
983 binutilsPrefix
= ROS_PREFIXValue
;
984 binutilsCommand
= binutilsPrefix
+ "-ld";
985 manualBinutilsSetting
= true;
986 detectedBinutils
= true;
989 if ( !detectedBinutils
)
992 binutilsCommand
= "ld";
993 detectedBinutils
= TryToDetectThisBinutils ( binutilsCommand
);
996 if ( !detectedBinutils
)
998 binutilsPrefix
= "mingw32";
999 binutilsCommand
= binutilsPrefix
+ "-ld";
1000 detectedBinutils
= TryToDetectThisBinutils ( binutilsCommand
);
1002 if ( detectedBinutils
)
1004 string binutilsVersion
= GetBinutilsVersionDate ( binutilsCommand
);
1005 if ( IsSupportedBinutilsVersion ( binutilsVersion
) )
1006 printf ( "detected (%s %s)\n", binutilsCommand
.c_str (), GetBinutilsVersion( binutilsCommand
).c_str() );
1009 printf ( "detected (%s), but with unsupported version (%s)\n",
1010 binutilsCommand
.c_str (),
1011 binutilsVersion
.c_str () );
1012 throw UnsupportedBuildToolException ( binutilsCommand
, binutilsVersion
);
1016 printf ( "not detected\n" );
1021 MingwBackend::DetectNetwideAssembler ()
1023 printf ( "Detecting netwide assembler..." );
1025 nasmCommand
= "nasm";
1026 bool detectedNasm
= TryToDetectThisNetwideAssembler ( nasmCommand
);
1028 if ( !detectedNasm
)
1030 nasmCommand
= "nasmw";
1031 detectedNasm
= TryToDetectThisNetwideAssembler ( nasmCommand
);
1034 if ( !detectedNasm
)
1036 nasmCommand
= "yasm";
1037 detectedNasm
= TryToDetectThisNetwideAssembler ( nasmCommand
);
1040 printf ( "detected (%s %s)\n", nasmCommand
.c_str (), GetNetwideAssemblerVersion( nasmCommand
).c_str() );
1042 printf ( "not detected\n" );
1046 MingwBackend::DetectPipeSupport ()
1048 printf ( "Detecting compiler -pipe support..." );
1050 string pipe_detection
= "tools" + sSep
+ "rbuild" + sSep
+ "backend" + sSep
+ "mingw" + sSep
+ "pipe_detection.c";
1051 string pipe_detectionObjectFilename
= ReplaceExtension ( pipe_detection
,
1053 string command
= ssprintf (
1054 "%s -pipe -c %s -o %s 1>%s 2>%s",
1055 FixSeparatorForSystemCommand(compilerCommand
).c_str (),
1056 pipe_detection
.c_str (),
1057 pipe_detectionObjectFilename
.c_str (),
1060 int exitcode
= system ( command
.c_str () );
1061 FILE* f
= fopen ( pipe_detectionObjectFilename
.c_str (), "rb" );
1064 usePipe
= (exitcode
== 0);
1066 unlink ( pipe_detectionObjectFilename
.c_str () );
1072 printf ( "detected\n" );
1074 printf ( "not detected\n" );
1078 MingwBackend::DetectPCHSupport ()
1080 printf ( "Detecting compiler pre-compiled header support..." );
1082 if ( configuration
.PrecompiledHeadersEnabled
)
1084 string path
= "tools" + sSep
+ "rbuild" + sSep
+ "backend" + sSep
+ "mingw" + sSep
+ "pch_detection.h";
1085 string cmd
= ssprintf (
1086 "%s -c %s 1>%s 2>%s",
1087 FixSeparatorForSystemCommand(compilerCommand
).c_str (),
1091 system ( cmd
.c_str () );
1094 FILE* f
= fopen ( path
.c_str (), "rb" );
1099 unlink ( path
.c_str () );
1105 printf ( "detected\n" );
1107 printf ( "not detected\n" );
1112 printf ( "disabled\n" );
1117 MingwBackend::GetNonModuleInstallTargetFiles (
1118 vector
<FileLocation
>& out
) const
1120 for ( size_t i
= 0; i
< ProjectNode
.installfiles
.size (); i
++ )
1122 const InstallFile
& installfile
= *ProjectNode
.installfiles
[i
];
1123 out
.push_back ( *installfile
.target
);
1128 MingwBackend::GetModuleInstallTargetFiles (
1129 vector
<FileLocation
>& out
) const
1131 for ( std::map
<std::string
, Module
*>::const_iterator p
= ProjectNode
.modules
.begin (); p
!= ProjectNode
.modules
.end (); ++ p
)
1133 const Module
& module
= *p
->second
;
1134 if ( !module
.enabled
)
1136 if ( module
.install
)
1137 out
.push_back ( *module
.install
);
1142 MingwBackend::GetInstallTargetFiles (
1143 vector
<FileLocation
>& out
) const
1145 GetNonModuleInstallTargetFiles ( out
);
1146 GetModuleInstallTargetFiles ( out
);
1150 MingwBackend::OutputInstallTarget ( const FileLocation
& source
,
1151 const FileLocation
& target
)
1153 fprintf ( fMakefile
,
1155 GetFullName ( target
).c_str (),
1156 GetFullName ( source
).c_str (),
1157 GetFullPath ( target
).c_str () );
1158 fprintf ( fMakefile
,
1160 fprintf ( fMakefile
,
1161 "\t${cp} %s %s 1>$(NUL)\n",
1162 GetFullName ( source
).c_str (),
1163 GetFullName ( target
).c_str () );
1167 MingwBackend::OutputNonModuleInstallTargets ()
1169 for ( size_t i
= 0; i
< ProjectNode
.installfiles
.size (); i
++ )
1171 const InstallFile
& installfile
= *ProjectNode
.installfiles
[i
];
1172 OutputInstallTarget ( *installfile
.source
, *installfile
.target
);
1177 MingwBackend::GetAliasedModuleOrModule ( const Module
& module
) const
1179 if ( module
.aliasedModuleName
.size () > 0 )
1181 const Module
* aliasedModule
= ProjectNode
.LocateModule ( module
.aliasedModuleName
);
1182 assert ( aliasedModule
);
1183 return *aliasedModule
;
1190 MingwBackend::OutputModuleInstallTargets ()
1192 for ( std::map
<std::string
, Module
*>::const_iterator p
= ProjectNode
.modules
.begin (); p
!= ProjectNode
.modules
.end (); ++ p
)
1194 const Module
& module
= *p
->second
;
1195 if ( !module
.enabled
)
1197 if ( module
.install
)
1199 const Module
& aliasedModule
= GetAliasedModuleOrModule ( module
);
1200 OutputInstallTarget ( *aliasedModule
.output
, *module
.install
);
1206 MingwBackend::GetRegistrySourceFiles ()
1208 return "boot" + sSep
+ "bootdata" + sSep
+ "hivecls_" + Environment::GetArch() + ".inf "
1209 "boot" + sSep
+ "bootdata" + sSep
+ "hivedef_" + Environment::GetArch() + ".inf "
1210 "boot" + sSep
+ "bootdata" + sSep
+ "hiveinst_" + Environment::GetArch() + ".inf "
1211 "boot" + sSep
+ "bootdata" + sSep
+ "hivesft_" + Environment::GetArch() + ".inf "
1212 "boot" + sSep
+ "bootdata" + sSep
+ "hivesys_" + Environment::GetArch() + ".inf ";
1216 MingwBackend::GetRegistryTargetFiles ()
1218 string system32ConfigDirectory
= "system32" + sSep
+ "config";
1219 FileLocation
system32 ( InstallDirectory
, system32ConfigDirectory
, "" );
1221 vector
<FileLocation
> registry_files
;
1222 registry_files
.push_back ( FileLocation ( InstallDirectory
, system32ConfigDirectory
, "default" ) );
1223 registry_files
.push_back ( FileLocation ( InstallDirectory
, system32ConfigDirectory
, "sam" ) );
1224 registry_files
.push_back ( FileLocation ( InstallDirectory
, system32ConfigDirectory
, "security" ) );
1225 registry_files
.push_back ( FileLocation ( InstallDirectory
, system32ConfigDirectory
, "software" ) );
1226 registry_files
.push_back ( FileLocation ( InstallDirectory
, system32ConfigDirectory
, "system" ) );
1228 return v2s( this, registry_files
, 6 );
1232 MingwBackend::OutputRegistryInstallTarget ()
1234 FileLocation
system32 ( InstallDirectory
, "system32" + sSep
+ "config", "" );
1236 string registrySourceFiles
= GetRegistrySourceFiles ();
1237 string registryTargetFiles
= GetRegistryTargetFiles ();
1238 fprintf ( fMakefile
,
1239 "install_registry: %s\n",
1240 registryTargetFiles
.c_str () );
1241 fprintf ( fMakefile
,
1242 "%s: %s %s $(MKHIVE_TARGET)\n",
1243 registryTargetFiles
.c_str (),
1244 registrySourceFiles
.c_str (),
1245 GetFullPath ( system32
).c_str () );
1246 fprintf ( fMakefile
,
1247 "\t$(ECHO_MKHIVE)\n" );
1248 fprintf ( fMakefile
,
1249 "\t$(MKHIVE_TARGET) boot%cbootdata %s $(ARCH) boot%cbootdata%chiveinst_$(ARCH).inf\n",
1250 cSep
, GetFullPath ( system32
).c_str (),
1252 fprintf ( fMakefile
,
1257 MingwBackend::GenerateInstallTarget ()
1259 vector
<FileLocation
> vInstallTargetFiles
;
1260 GetInstallTargetFiles ( vInstallTargetFiles
);
1261 string installTargetFiles
= v2s ( this, vInstallTargetFiles
, 5 );
1262 string registryTargetFiles
= GetRegistryTargetFiles ();
1264 fprintf ( fMakefile
,
1266 installTargetFiles
.c_str (),
1267 registryTargetFiles
.c_str () );
1268 OutputNonModuleInstallTargets ();
1269 OutputModuleInstallTargets ();
1270 OutputRegistryInstallTarget ();
1271 fprintf ( fMakefile
,
1276 MingwBackend::GetModuleTestTargets (
1277 vector
<string
>& out
) const
1279 for ( std::map
<std::string
, Module
*>::const_iterator p
= ProjectNode
.modules
.begin (); p
!= ProjectNode
.modules
.end (); ++ p
)
1281 const Module
& module
= *p
->second
;
1282 if ( !module
.enabled
)
1284 if ( module
.type
== Test
)
1285 out
.push_back ( module
.name
);
1290 MingwBackend::GenerateTestTarget ()
1292 vector
<string
> vTestTargets
;
1293 GetModuleTestTargets ( vTestTargets
);
1294 string testTargets
= v2s ( vTestTargets
, 5 );
1296 fprintf ( fMakefile
,
1298 testTargets
.c_str () );
1299 fprintf ( fMakefile
,
1304 MingwBackend::GenerateDirectoryTargets ()
1306 intermediateDirectory
->CreateRule ( fMakefile
, "$(INTERMEDIATE)" );
1307 outputDirectory
->CreateRule ( fMakefile
, "$(OUTPUT)" );
1308 installDirectory
->CreateRule ( fMakefile
, "$(INSTALL)" );