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
, "", "", "" }, // IdlInterface
62 { HostFalse
, "", "", "" }, // EmbeddedTypeLib
63 { HostFalse
, "", "", "" }, // ElfExecutable
64 { HostFalse
, "", "", "" }, // RpcProxy
65 { HostTrue
, "", "", "" }, // HostStaticLibrary
66 { HostFalse
, "", "", "" }, // Cabinet
67 { HostFalse
, "", "", "" }, // KeyboardLayout
68 { HostFalse
, "", "", "" }, // MessageHeader
71 static std::string mscPath
;
72 static std::string mslinkPath
;
75 MingwBackend::GetFullPath ( const FileLocation
& file
) const
77 MingwModuleHandler::PassThruCacheDirectory ( &file
);
80 switch ( file
.directory
)
85 case IntermediateDirectory
:
86 directory
= "$(INTERMEDIATE)";
89 directory
= "$(OUTPUT)";
91 case InstallDirectory
:
92 directory
= "$(INSTALL)";
94 case TemporaryDirectory
:
95 directory
= "$(TEMPORARY)";
98 throw InvalidOperationException ( __FILE__
,
100 "Invalid directory %d.",
104 if ( file
.relative_path
.length () > 0 )
106 if ( directory
.length () > 0 )
108 directory
+= file
.relative_path
;
114 MingwBackend::GetFullName ( const FileLocation
& file
) const
117 switch ( file
.directory
)
119 case SourceDirectory
:
122 case IntermediateDirectory
:
123 directory
= "$(INTERMEDIATE)";
125 case OutputDirectory
:
126 directory
= "$(OUTPUT)";
128 case InstallDirectory
:
129 directory
= "$(INSTALL)";
131 case TemporaryDirectory
:
132 directory
= "$(TEMPORARY)";
135 throw InvalidOperationException ( __FILE__
,
137 "Invalid directory %d.",
141 if ( file
.relative_path
.length () > 0 )
143 if ( directory
.length () > 0 )
145 directory
+= file
.relative_path
;
148 if ( directory
.length () > 0 )
151 return directory
+ file
.name
;
155 v2s ( const Backend
* backend
, const vector
<FileLocation
>& files
, int wrap_at
)
161 for ( size_t i
= 0; i
< files
.size(); i
++ )
163 const FileLocation
& file
= files
[i
];
164 if ( wrap_at
> 0 && wrap_count
++ == wrap_at
)
171 s
+= backend
->GetFullName ( file
);
178 v2s ( const string_list
& v
, int wrap_at
)
184 for ( size_t i
= 0; i
< v
.size(); i
++ )
188 if ( wrap_at
> 0 && wrap_count
++ == wrap_at
)
201 static class MingwFactory
: public Backend::Factory
204 MingwFactory() : Factory ( "mingw", "Minimalist GNU Win32" ) {}
205 Backend
* operator() ( Project
& project
,
206 Configuration
& configuration
)
208 return new MingwBackend ( project
,
214 MingwBackend::MingwBackend ( Project
& project
,
215 Configuration
& configuration
)
216 : Backend ( project
, configuration
),
217 manualBinutilsSetting( false ),
218 intermediateDirectory ( new Directory ( "" ) ),
219 outputDirectory ( new Directory ( "" ) ),
220 installDirectory ( new Directory ( "" ) )
225 MingwBackend::~MingwBackend()
227 delete intermediateDirectory
;
228 delete outputDirectory
;
229 delete installDirectory
;
233 MingwBackend::AddDirectoryTarget ( const string
& directory
,
234 Directory
* directoryTree
)
236 if ( directory
.length () > 0)
237 directoryTree
->Add ( directory
.c_str() );
238 return directoryTree
->name
;
242 MingwBackend::CanEnablePreCompiledHeaderSupportForModule ( const Module
& module
)
244 if ( !configuration
.CompilationUnitsEnabled
)
247 const vector
<CompilationUnit
*>& compilationUnits
= module
.non_if_data
.compilationUnits
;
249 for ( i
= 0; i
< compilationUnits
.size (); i
++ )
251 CompilationUnit
& compilationUnit
= *compilationUnits
[i
];
252 if ( compilationUnit
.GetFiles ().size () != 1 )
259 MingwBackend::ProcessModules ()
261 printf ( "Processing modules..." );
263 vector
<MingwModuleHandler
*> v
;
266 for ( std::map
<std::string
, Module
*>::iterator p
= ProjectNode
.modules
.begin (); p
!= ProjectNode
.modules
.end (); ++ p
)
267 fprintf ( fMakefile
, "%s_TYPE:=%u\n", p
->second
->name
.c_str(), p
->second
->type
);
269 for ( std::map
<std::string
, Module
*>::iterator p
= ProjectNode
.modules
.begin (); p
!= ProjectNode
.modules
.end (); ++ p
)
271 Module
& module
= *p
->second
;
272 if ( !module
.enabled
)
274 MingwModuleHandler
* h
= MingwModuleHandler::InstanciateHandler (
277 h
->AddImplicitLibraries ( module
);
278 if ( use_pch
&& CanEnablePreCompiledHeaderSupportForModule ( module
) )
279 h
->EnablePreCompiledHeaderSupport ();
283 size_t iend
= v
.size ();
285 for ( i
= 0; i
< iend
; i
++ )
286 v
[i
]->GenerateSourceMacro();
287 for ( i
= 0; i
< iend
; i
++ )
288 v
[i
]->GenerateObjectMacro();
289 fprintf ( fMakefile
, "\n" );
290 for ( i
= 0; i
< iend
; i
++ )
291 v
[i
]->GenerateTargetMacro();
292 fprintf ( fMakefile
, "\n" );
294 GenerateAllTarget ( v
);
295 GenerateRegTestsRunTarget ();
297 for ( i
= 0; i
< iend
; i
++ )
298 v
[i
]->GenerateOtherMacros();
300 for ( i
= 0; i
< iend
; i
++ )
302 MingwModuleHandler
& h
= *v
[i
];
303 h
.GeneratePreconditionDependencies ();
305 h
.GenerateInvocations ();
306 h
.GenerateCleanTarget ();
307 h
.GenerateInstallTarget ();
308 h
.GenerateDependsTarget ();
316 MingwBackend::Process ()
318 if ( configuration
.CheckDependenciesForModuleOnly
)
319 CheckAutomaticDependenciesForModuleOnly ();
325 MingwBackend::CheckAutomaticDependenciesForModuleOnly ()
327 if ( configuration
.Dependencies
== AutomaticDependencies
)
329 Module
* module
= ProjectNode
.LocateModule ( configuration
.CheckDependenciesForModuleOnlyModule
);
330 if ( module
== NULL
)
332 printf ( "Module '%s' does not exist\n",
333 configuration
.CheckDependenciesForModuleOnlyModule
.c_str () );
337 printf ( "Checking automatic dependencies for module '%s'...",
338 module
->name
.c_str () );
339 AutomaticDependency
automaticDependency ( ProjectNode
);
340 automaticDependency
.CheckAutomaticDependenciesForModule ( *module
,
341 configuration
.Verbose
);
347 MingwBackend::ProcessNormal ()
349 assert(sizeof(ModuleHandlerInformations
)/sizeof(ModuleHandlerInformations
[0]) == TypeDontCare
);
353 DetectNetwideAssembler ();
354 DetectPipeSupport ();
358 GenerateGlobalVariables ();
359 GenerateXmlBuildFilesMacro ();
361 GenerateInstallTarget ();
362 GenerateTestTarget ();
363 GenerateDirectoryTargets ();
364 GenerateDirectories ();
365 GenerateTestSupportCode ();
366 GenerateCompilationUnitSupportCode ();
368 GenerateProxyMakefiles ();
369 CheckAutomaticDependencies ();
374 MingwBackend::CreateMakefile ()
376 fMakefile
= fopen ( ProjectNode
.makefile
.c_str (), "w" );
378 throw AccessDeniedException ( ProjectNode
.makefile
);
379 MingwModuleHandler::SetBackend ( this );
380 MingwModuleHandler::SetMakefile ( fMakefile
);
384 MingwBackend::CloseMakefile () const
387 fclose ( fMakefile
);
391 MingwBackend::GenerateHeader () const
393 fprintf ( fMakefile
, "# THIS FILE IS AUTOMATICALLY GENERATED, EDIT '%s' INSTEAD\n\n",
394 ProjectNode
.GetProjectFilename ().c_str () );
398 MingwBackend::GenerateGlobalProperties (
399 const char* assignmentOperation
,
400 const IfableData
& data
) const
402 for ( std::map
<std::string
, Property
*>::const_iterator p
= data
.properties
.begin(); p
!= data
.properties
.end(); ++ p
)
404 Property
& prop
= *p
->second
;
406 if (!prop
.isInternal
)
408 fprintf ( fMakefile
, "%s := %s\n",
410 prop
.value
.c_str() );
416 MingwBackend::GenerateProjectLFLAGS () const
419 for ( size_t i
= 0; i
< ProjectNode
.linkerFlags
.size (); i
++ )
421 LinkerFlag
& linkerFlag
= *ProjectNode
.linkerFlags
[i
];
422 if ( lflags
.length () > 0 )
424 lflags
+= linkerFlag
.flag
;
430 MingwBackend::GenerateGlobalVariables () const
432 fputs ( "include tools$(SEP)rbuild$(SEP)backend$(SEP)mingw$(SEP)rules.mak\n", fMakefile
);
433 fprintf ( fMakefile
, "include tools$(SEP)rbuild$(SEP)backend$(SEP)mingw$(SEP)linkers$(SEP)%s.mak\n", ProjectNode
.GetLinkerSet ().c_str () );
434 fprintf ( fMakefile
, "include tools$(SEP)rbuild$(SEP)backend$(SEP)mingw$(SEP)compilers$(SEP)%s.mak\n", ProjectNode
.GetCompilerSet ().c_str () );
436 if ( mscPath
.length() )
437 fprintf ( fMakefile
, "export RBUILD_CL_PATH=%s\n", mscPath
.c_str () );
439 if ( mslinkPath
.length() )
440 fprintf ( fMakefile
, "export RBUILD_LINK_PATH=%s\n", mslinkPath
.c_str () );
442 if ( configuration
.Dependencies
== FullDependencies
)
445 "ifeq ($(ROS_BUILDDEPS),)\n"
446 "ROS_BUILDDEPS:=%s\n"
453 compilerPrefix
.c_str () );
456 nasmCommand
.c_str () );
458 GenerateGlobalProperties ( "=", ProjectNode
.non_if_data
);
460 if ( ProjectNode
.configuration
.Compiler
== GnuGcc
)
462 fprintf ( fMakefile
, "ifneq ($(OARCH),)\n" );
463 fprintf ( fMakefile
, "PROJECT_ASFLAGS += -march=$(OARCH)\n" );
464 fprintf ( fMakefile
, "PROJECT_CFLAGS += -march=$(OARCH)\n" );
465 fprintf ( fMakefile
, "PROJECT_CXXFLAGS += -march=$(OARCH)\n" );
466 fprintf ( fMakefile
, "endif\n" );
467 fprintf ( fMakefile
, "ifneq ($(TUNE),)\n" );
468 fprintf ( fMakefile
, "PROJECT_CFLAGS += -mtune=$(TUNE)\n" );
469 fprintf ( fMakefile
, "PROJECT_CXXFLAGS += -mtune=$(TUNE)\n" );
470 fprintf ( fMakefile
, "endif\n" );
474 fprintf ( fMakefile
, "PROJECT_CFLAGS += -pipe\n" );
475 fprintf ( fMakefile
, "PROJECT_CXXFLAGS += -pipe\n" );
476 fprintf ( fMakefile
, "PROJECT_ASFLAGS += -pipe\n" );
479 // Would be nice to have our own C++ runtime
480 fputs ( "BUILTIN_CXXINCLUDES+= $(TARGET_CPPFLAGS)\n", fMakefile
);
483 // Because RosBE gcc is built to suck
484 fputs ( "BUILTIN_HOST_CINCLUDES+= $(HOST_CFLAGS)\n", fMakefile
);
485 fputs ( "BUILTIN_HOST_CPPINCLUDES+= $(HOST_CFLAGS)\n", fMakefile
);
486 fputs ( "BUILTIN_HOST_CXXINCLUDES+= $(HOST_CPPFLAGS)\n", fMakefile
);
488 MingwModuleHandler::GenerateParameters ( "PROJECT", "+=", ProjectNode
.non_if_data
);
489 MingwModuleHandler::GenerateParameters ( "PROJECT_HOST", "+=", ProjectNode
.host_non_if_data
);
491 // TODO: linker flags
492 fprintf ( fMakefile
, "PROJECT_LFLAGS := '$(shell ${TARGET_CC} -print-libgcc-file-name)' %s\n", GenerateProjectLFLAGS ().c_str () );
493 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" );
494 /* hack to get libgcc_eh.a, should check mingw version or something */
495 if (Environment::GetArch() == "amd64")
497 fprintf ( fMakefile
, "PROJECT_LPPFLAGS += '$(shell ${TARGET_CPP} -print-file-name=libgcc_eh.a)'\n" );
500 // TODO: use symbolic names for module types
501 for ( size_t i
= 0; i
< sizeof(ModuleHandlerInformations
) / sizeof(ModuleHandlerInformations
[0]); ++ i
)
503 if ( ModuleHandlerInformations
[i
].cflags
&& ModuleHandlerInformations
[i
].cflags
[0] )
506 "MODULETYPE%d_%sFLAGS:=%s\n",
509 ModuleHandlerInformations
[i
].cflags
);
512 if ( ModuleHandlerInformations
[i
].cflags
&& ModuleHandlerInformations
[i
].cflags
[0] )
515 "MODULETYPE%d_%sFLAGS:=%s\n",
518 ModuleHandlerInformations
[i
].cflags
);
521 if ( ModuleHandlerInformations
[i
].nasmflags
&& ModuleHandlerInformations
[i
].nasmflags
[0] )
524 "MODULETYPE%d_%sFLAGS:=%s\n",
527 ModuleHandlerInformations
[i
].nasmflags
);
531 fprintf ( fMakefile
, "\n" );
535 MingwBackend::IncludeInAllTarget ( const Module
& module
) const
537 if ( MingwModuleHandler::ReferenceObjects ( module
) )
539 if ( module
.type
== BootSector
)
541 if ( module
.type
== Iso
)
543 if ( module
.type
== LiveIso
)
545 if ( module
.type
== Test
)
547 if ( module
.type
== Alias
)
553 MingwBackend::GenerateAllTarget ( const vector
<MingwModuleHandler
*>& handlers
) const
555 fprintf ( fMakefile
, "all:" );
557 size_t iend
= handlers
.size ();
558 for ( size_t i
= 0; i
< iend
; i
++ )
560 const Module
& module
= handlers
[i
]->module
;
561 if ( IncludeInAllTarget ( module
) )
563 if ( wrap_count
++ == 5 )
564 fprintf ( fMakefile
, " \\\n\t\t" ), wrap_count
= 0;
567 GetTargetMacro(module
).c_str () );
570 fprintf ( fMakefile
, "\n\t\n\n" );
574 MingwBackend::GenerateRegTestsRunTarget () const
577 "REGTESTS_RUN_TARGET = regtests.dll\n" );
579 "$(REGTESTS_RUN_TARGET): $(REGTESTS_TARGET)\n" );
581 "\t$(cp) $(REGTESTS_TARGET) $(REGTESTS_RUN_TARGET)\n" );
582 fprintf ( fMakefile
, "\n" );
586 MingwBackend::GenerateXmlBuildFilesMacro() const
589 "XMLBUILDFILES = %s \\\n",
590 ProjectNode
.GetProjectFilename ().c_str () );
591 string xmlbuildFilenames
;
592 int numberOfExistingFiles
= 0;
594 time_t SystemTime
, lastWriteTime
;
596 for ( size_t i
= 0; i
< ProjectNode
.xmlbuildfiles
.size (); i
++ )
598 XMLInclude
& xmlbuildfile
= *ProjectNode
.xmlbuildfiles
[i
];
599 if ( !xmlbuildfile
.fileExists
)
601 numberOfExistingFiles
++;
602 if ( xmlbuildFilenames
.length () > 0 )
603 xmlbuildFilenames
+= " ";
605 FILE* f
= fopen ( xmlbuildfile
.topIncludeFilename
.c_str (), "rb" );
607 throw FileNotFoundException ( NormalizeFilename ( xmlbuildfile
.topIncludeFilename
) );
609 if ( fstat ( fileno ( f
), &statbuf
) != 0 )
612 throw AccessDeniedException ( NormalizeFilename ( xmlbuildfile
.topIncludeFilename
) );
615 lastWriteTime
= statbuf
.st_mtime
;
616 SystemTime
= time(NULL
);
618 if (SystemTime
!= -1)
620 if (difftime (lastWriteTime
, SystemTime
) > 0)
621 throw InvalidDateException ( NormalizeFilename ( xmlbuildfile
.topIncludeFilename
) );
626 xmlbuildFilenames
+= NormalizeFilename ( xmlbuildfile
.topIncludeFilename
);
627 if ( numberOfExistingFiles
% 5 == 4 || i
== ProjectNode
.xmlbuildfiles
.size () - 1 )
631 xmlbuildFilenames
.c_str ());
632 if ( i
== ProjectNode
.xmlbuildfiles
.size () - 1 )
634 fprintf ( fMakefile
, "\n" );
641 xmlbuildFilenames
.resize ( 0 );
643 numberOfExistingFiles
++;
645 fprintf ( fMakefile
, "\n" );
649 MingwBackend::GenerateTestSupportCode ()
651 printf ( "Generating test support code..." );
652 TestSupportCode
testSupportCode ( ProjectNode
);
653 testSupportCode
.GenerateTestSupportCode ( configuration
.Verbose
);
658 MingwBackend::GenerateCompilationUnitSupportCode ()
660 if ( configuration
.CompilationUnitsEnabled
)
662 printf ( "Generating compilation unit support code..." );
663 CompilationUnitSupportCode
compilationUnitSupportCode ( ProjectNode
);
664 compilationUnitSupportCode
.Generate ( configuration
.Verbose
);
670 MingwBackend::GenerateSysSetup ()
672 printf ( "Generating syssetup.inf..." );
673 SysSetupGenerator
sysSetupGenerator ( ProjectNode
);
674 sysSetupGenerator
.Generate ();
679 MingwBackend::GetProxyMakefileTree () const
681 if ( configuration
.GenerateProxyMakefilesInSourceTree
)
684 return Environment::GetOutputPath ();
688 MingwBackend::GenerateProxyMakefiles ()
690 printf ( "Generating proxy makefiles..." );
691 ProxyMakefile
proxyMakefile ( ProjectNode
);
692 proxyMakefile
.GenerateProxyMakefiles ( configuration
.Verbose
,
693 GetProxyMakefileTree () );
698 MingwBackend::CheckAutomaticDependencies ()
700 if ( configuration
.Dependencies
== AutomaticDependencies
)
702 printf ( "Checking automatic dependencies..." );
703 AutomaticDependency
automaticDependency ( ProjectNode
);
704 automaticDependency
.CheckAutomaticDependencies ( configuration
.Verbose
);
710 MingwBackend::GenerateDirectories ()
712 printf ( "Creating directories..." );
713 intermediateDirectory
->GenerateTree ( IntermediateDirectory
, configuration
.Verbose
);
714 outputDirectory
->GenerateTree ( OutputDirectory
, configuration
.Verbose
);
715 if ( !configuration
.MakeHandlesInstallDirectories
)
716 installDirectory
->GenerateTree ( InstallDirectory
, configuration
.Verbose
);
721 MingwBackend::TryToDetectThisCompiler ( const string
& compiler
)
723 string command
= ssprintf (
725 FixSeparatorForSystemCommand(compiler
).c_str (),
728 int exitcode
= system ( command
.c_str () );
729 return (bool) (exitcode
== 0);
733 MingwBackend::DetectCompiler ()
735 printf ( "Detecting compiler..." );
737 bool detectedCompiler
= false;
738 bool supportedCompiler
= false;
739 string compilerVersion
;
741 if ( ProjectNode
.configuration
.Compiler
== GnuGcc
)
743 const string
& ROS_PREFIXValue
= Environment::GetVariable ( "ROS_PREFIX" );
744 if ( ROS_PREFIXValue
.length () > 0 )
746 compilerPrefix
= ROS_PREFIXValue
;
747 compilerCommand
= compilerPrefix
+ "-gcc";
748 detectedCompiler
= TryToDetectThisCompiler ( compilerCommand
);
751 if ( !detectedCompiler
)
754 compilerCommand
= "gcc";
755 detectedCompiler
= TryToDetectThisCompiler ( compilerCommand
);
758 if ( !detectedCompiler
)
760 compilerPrefix
= "mingw32";
761 compilerCommand
= compilerPrefix
+ "-gcc";
762 detectedCompiler
= TryToDetectThisCompiler ( compilerCommand
);
765 if ( detectedCompiler
)
766 compilerVersion
= GetCompilerVersion ( compilerCommand
);
768 supportedCompiler
= IsSupportedCompilerVersion ( compilerVersion
);
770 else if ( ProjectNode
.configuration
.Compiler
== MicrosoftC
)
772 compilerCommand
= "cl";
773 detectedCompiler
= DetectMicrosoftCompiler ( compilerVersion
, mscPath
);
774 supportedCompiler
= true; // TODO
777 if ( detectedCompiler
)
779 if ( supportedCompiler
)
780 printf ( "detected (%s %s)\n", compilerCommand
.c_str (), compilerVersion
.c_str() );
783 printf ( "detected (%s), but with unsupported version (%s)\n",
784 compilerCommand
.c_str (),
785 compilerVersion
.c_str () );
786 throw UnsupportedBuildToolException ( compilerCommand
, compilerVersion
);
790 printf ( "not detected\n" );
795 MingwBackend::TryToDetectThisNetwideAssembler ( const string
& assembler
)
797 string command
= ssprintf (
799 FixSeparatorForSystemCommand(assembler
).c_str (),
802 int exitcode
= system ( command
.c_str () );
803 return (bool) (exitcode
== 0);
807 MingwBackend::GetVersionString ( const string
& versionCommand
)
814 fp
= popen ( versionCommand
.c_str () , "r" );
817 ( feof ( fp
) == 0 &&
818 ( ( ch
= fgetc( fp
) ) != -1 ) );
821 buffer
[i
] = (char) ch
;
826 char separators
[] = " ()";
828 char *prevtoken
= NULL
;
832 token
= strtok ( buffer
, separators
);
833 while ( token
!= NULL
)
836 version
= string( prevtoken
);
837 if ( (newline
= version
.find('\n')) != std::string::npos
)
838 version
.erase(newline
, 1);
839 if ( version
.find('.') != std::string::npos
)
841 token
= strtok ( NULL
, separators
);
847 MingwBackend::GetNetwideAssemblerVersion ( const string
& nasmCommand
)
849 string versionCommand
;
850 if ( nasmCommand
.find("yasm") != std::string::npos
)
852 versionCommand
= ssprintf ( "%s --version",
853 nasmCommand
.c_str (),
859 versionCommand
= ssprintf ( "%s -v",
860 nasmCommand
.c_str (),
864 return GetVersionString( versionCommand
);
868 MingwBackend::GetCompilerVersion ( const string
& compilerCommand
)
870 string versionCommand
= ssprintf ( "%s --version gcc",
871 compilerCommand
.c_str (),
874 return GetVersionString( versionCommand
);
878 MingwBackend::GetBinutilsVersion ( const string
& binutilsCommand
)
880 string versionCommand
= ssprintf ( "%s -v",
881 binutilsCommand
.c_str (),
884 return GetVersionString( versionCommand
);
888 MingwBackend::IsSupportedCompilerVersion ( const string
& compilerVersion
)
890 if ( strcmp ( compilerVersion
.c_str (), "3.4.2") < 0 )
897 MingwBackend::TryToDetectThisBinutils ( const string
& binutils
)
899 string command
= ssprintf (
901 FixSeparatorForSystemCommand(binutils
).c_str (),
904 int exitcode
= system ( command
.c_str () );
905 return (exitcode
== 0);
909 MingwBackend::GetBinutilsVersionDate ( const string
& binutilsCommand
)
915 string versionCommand
= ssprintf ( "%s -v",
916 binutilsCommand
.c_str (),
919 fp
= popen ( versionCommand
.c_str () , "r" );
922 ( feof ( fp
) == 0 &&
923 ( ( ch
= fgetc( fp
) ) != -1 ) );
926 buffer
[i
] = (char) ch
;
931 char separators
[] = " ";
933 char *prevtoken
= NULL
;
935 token
= strtok ( buffer
, separators
);
936 while ( token
!= NULL
)
939 token
= strtok ( NULL
, separators
);
941 string version
= string ( prevtoken
);
942 int lastDigit
= version
.find_last_not_of ( "\t\r\n" );
943 if ( lastDigit
!= -1 )
944 return string ( version
, 0, lastDigit
+1 );
950 MingwBackend::IsSupportedBinutilsVersion ( const string
& binutilsVersion
)
952 if ( manualBinutilsSetting
) return true;
955 if ( binutilsVersion
.find('.') != std::string::npos
)
957 /* TODO: blacklist versions on version number instead of date */
962 * - Binutils older than 2003/10/01 have broken windres which can't handle
963 * icons with alpha channel.
964 * - Binutils between 2004/09/02 and 2004/10/08 have broken handling of
965 * forward exports in dlltool.
967 if ( ( ( strcmp ( binutilsVersion
.c_str (), "20040902") >= 0 ) &&
968 ( strcmp ( binutilsVersion
.c_str (), "20041008") <= 0 ) ) ||
969 ( strcmp ( binutilsVersion
.c_str (), "20031001") < 0 ) )
976 MingwBackend::DetectBinutils ()
978 printf ( "Detecting binutils..." );
980 bool detectedBinutils
= false;
981 bool supportedBinutils
= false;
982 string binutilsVersion
;
984 if ( ProjectNode
.configuration
.Linker
== GnuLd
)
986 const string
& ROS_PREFIXValue
= Environment::GetVariable ( "ROS_PREFIX" );
988 if ( ROS_PREFIXValue
.length () > 0 )
990 binutilsPrefix
= ROS_PREFIXValue
;
991 binutilsCommand
= binutilsPrefix
+ "-ld";
992 manualBinutilsSetting
= true;
993 detectedBinutils
= true;
996 if ( !detectedBinutils
)
999 binutilsCommand
= "ld";
1000 detectedBinutils
= TryToDetectThisBinutils ( binutilsCommand
);
1003 if ( !detectedBinutils
)
1005 binutilsPrefix
= "mingw32";
1006 binutilsCommand
= binutilsPrefix
+ "-ld";
1007 detectedBinutils
= TryToDetectThisBinutils ( binutilsCommand
);
1009 if ( detectedBinutils
)
1011 binutilsVersion
= GetBinutilsVersionDate ( binutilsCommand
);
1012 supportedBinutils
= IsSupportedBinutilsVersion ( binutilsVersion
);
1015 else if ( ProjectNode
.configuration
.Linker
== MicrosoftLink
)
1017 compilerCommand
= "link";
1018 detectedBinutils
= DetectMicrosoftLinker ( binutilsVersion
, mslinkPath
);
1019 supportedBinutils
= true; // TODO
1022 if ( detectedBinutils
)
1024 if ( supportedBinutils
)
1025 printf ( "detected (%s %s)\n", binutilsCommand
.c_str (), binutilsVersion
.c_str() );
1028 printf ( "detected (%s), but with unsupported version (%s)\n",
1029 binutilsCommand
.c_str (),
1030 binutilsVersion
.c_str () );
1031 throw UnsupportedBuildToolException ( binutilsCommand
, binutilsVersion
);
1035 printf ( "not detected\n" );
1040 MingwBackend::DetectNetwideAssembler ()
1042 printf ( "Detecting netwide assembler..." );
1044 nasmCommand
= "nasm";
1045 bool detectedNasm
= TryToDetectThisNetwideAssembler ( nasmCommand
);
1047 if ( !detectedNasm
)
1049 nasmCommand
= "nasmw";
1050 detectedNasm
= TryToDetectThisNetwideAssembler ( nasmCommand
);
1053 if ( !detectedNasm
)
1055 nasmCommand
= "yasm";
1056 detectedNasm
= TryToDetectThisNetwideAssembler ( nasmCommand
);
1059 printf ( "detected (%s %s)\n", nasmCommand
.c_str (), GetNetwideAssemblerVersion( nasmCommand
).c_str() );
1061 printf ( "not detected\n" );
1065 MingwBackend::DetectPipeSupport ()
1067 if ( ProjectNode
.configuration
.Compiler
== GnuGcc
)
1069 printf ( "Detecting compiler -pipe support..." );
1071 string pipe_detection
= "tools" + sSep
+ "rbuild" + sSep
+ "backend" + sSep
+ "mingw" + sSep
+ "pipe_detection.c";
1072 string pipe_detectionObjectFilename
= ReplaceExtension ( pipe_detection
,
1074 string command
= ssprintf (
1075 "%s -pipe -c %s -o %s 1>%s 2>%s",
1076 FixSeparatorForSystemCommand(compilerCommand
).c_str (),
1077 pipe_detection
.c_str (),
1078 pipe_detectionObjectFilename
.c_str (),
1081 int exitcode
= system ( command
.c_str () );
1082 FILE* f
= fopen ( pipe_detectionObjectFilename
.c_str (), "rb" );
1085 usePipe
= (exitcode
== 0);
1087 unlink ( pipe_detectionObjectFilename
.c_str () );
1093 printf ( "detected\n" );
1095 printf ( "not detected\n" );
1102 MingwBackend::DetectPCHSupport ()
1104 printf ( "Detecting compiler pre-compiled header support..." );
1106 if ( configuration
.PrecompiledHeadersEnabled
&& ProjectNode
.configuration
.Compiler
== GnuGcc
)
1108 string path
= "tools" + sSep
+ "rbuild" + sSep
+ "backend" + sSep
+ "mingw" + sSep
+ "pch_detection.h";
1109 string cmd
= ssprintf (
1110 "%s -c %s 1>%s 2>%s",
1111 FixSeparatorForSystemCommand(compilerCommand
).c_str (),
1115 system ( cmd
.c_str () );
1118 FILE* f
= fopen ( path
.c_str (), "rb" );
1123 unlink ( path
.c_str () );
1129 printf ( "detected\n" );
1131 printf ( "not detected\n" );
1136 printf ( "disabled\n" );
1141 MingwBackend::GetNonModuleInstallTargetFiles (
1142 vector
<FileLocation
>& out
) const
1144 for ( size_t i
= 0; i
< ProjectNode
.installfiles
.size (); i
++ )
1146 const InstallFile
& installfile
= *ProjectNode
.installfiles
[i
];
1147 out
.push_back ( *installfile
.target
);
1152 MingwBackend::GetModuleInstallTargetFiles (
1153 vector
<FileLocation
>& out
) const
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
)
1161 out
.push_back ( *module
.install
);
1166 MingwBackend::GetInstallTargetFiles (
1167 vector
<FileLocation
>& out
) const
1169 GetNonModuleInstallTargetFiles ( out
);
1170 GetModuleInstallTargetFiles ( out
);
1174 MingwBackend::OutputInstallTarget ( const FileLocation
& source
,
1175 const FileLocation
& target
)
1177 fprintf ( fMakefile
,
1179 GetFullName ( target
).c_str (),
1180 GetFullName ( source
).c_str (),
1181 GetFullPath ( target
).c_str () );
1182 fprintf ( fMakefile
,
1184 fprintf ( fMakefile
,
1185 "\t${cp} %s %s 1>$(NUL)\n",
1186 GetFullName ( source
).c_str (),
1187 GetFullName ( target
).c_str () );
1191 MingwBackend::OutputNonModuleInstallTargets ()
1193 for ( size_t i
= 0; i
< ProjectNode
.installfiles
.size (); i
++ )
1195 const InstallFile
& installfile
= *ProjectNode
.installfiles
[i
];
1196 OutputInstallTarget ( *installfile
.source
, *installfile
.target
);
1201 MingwBackend::GetAliasedModuleOrModule ( const Module
& module
) const
1203 if ( module
.aliasedModuleName
.size () > 0 )
1205 const Module
* aliasedModule
= ProjectNode
.LocateModule ( module
.aliasedModuleName
);
1206 assert ( aliasedModule
);
1207 return *aliasedModule
;
1214 MingwBackend::OutputModuleInstallTargets ()
1216 for ( std::map
<std::string
, Module
*>::const_iterator p
= ProjectNode
.modules
.begin (); p
!= ProjectNode
.modules
.end (); ++ p
)
1218 const Module
& module
= *p
->second
;
1219 if ( !module
.enabled
)
1221 if ( module
.install
)
1223 const Module
& aliasedModule
= GetAliasedModuleOrModule ( module
);
1224 OutputInstallTarget ( *aliasedModule
.output
, *module
.install
);
1230 MingwBackend::GetRegistrySourceFiles ()
1232 return "boot" + sSep
+ "bootdata" + sSep
+ "hivecls_" + Environment::GetArch() + ".inf "
1233 "boot" + sSep
+ "bootdata" + sSep
+ "hivedef_" + Environment::GetArch() + ".inf "
1234 "boot" + sSep
+ "bootdata" + sSep
+ "hiveinst_" + Environment::GetArch() + ".inf "
1235 "boot" + sSep
+ "bootdata" + sSep
+ "hivesft_" + Environment::GetArch() + ".inf "
1236 "boot" + sSep
+ "bootdata" + sSep
+ "hivesys_" + Environment::GetArch() + ".inf ";
1240 MingwBackend::GetRegistryTargetFiles ()
1242 string system32ConfigDirectory
= "system32" + sSep
+ "config";
1243 FileLocation
system32 ( InstallDirectory
, system32ConfigDirectory
, "" );
1245 vector
<FileLocation
> registry_files
;
1246 registry_files
.push_back ( FileLocation ( InstallDirectory
, system32ConfigDirectory
, "default" ) );
1247 registry_files
.push_back ( FileLocation ( InstallDirectory
, system32ConfigDirectory
, "sam" ) );
1248 registry_files
.push_back ( FileLocation ( InstallDirectory
, system32ConfigDirectory
, "security" ) );
1249 registry_files
.push_back ( FileLocation ( InstallDirectory
, system32ConfigDirectory
, "software" ) );
1250 registry_files
.push_back ( FileLocation ( InstallDirectory
, system32ConfigDirectory
, "system" ) );
1252 return v2s( this, registry_files
, 6 );
1256 MingwBackend::OutputRegistryInstallTarget ()
1258 FileLocation
system32 ( InstallDirectory
, "system32" + sSep
+ "config", "" );
1260 string registrySourceFiles
= GetRegistrySourceFiles ();
1261 string registryTargetFiles
= GetRegistryTargetFiles ();
1262 fprintf ( fMakefile
,
1263 "install_registry: %s\n",
1264 registryTargetFiles
.c_str () );
1265 fprintf ( fMakefile
,
1266 "%s: %s %s $(MKHIVE_TARGET)\n",
1267 registryTargetFiles
.c_str (),
1268 registrySourceFiles
.c_str (),
1269 GetFullPath ( system32
).c_str () );
1270 fprintf ( fMakefile
,
1271 "\t$(ECHO_MKHIVE)\n" );
1272 fprintf ( fMakefile
,
1273 "\t$(MKHIVE_TARGET) boot%cbootdata %s $(ARCH) boot%cbootdata%chiveinst_$(ARCH).inf\n",
1274 cSep
, GetFullPath ( system32
).c_str (),
1276 fprintf ( fMakefile
,
1281 MingwBackend::GenerateInstallTarget ()
1283 vector
<FileLocation
> vInstallTargetFiles
;
1284 GetInstallTargetFiles ( vInstallTargetFiles
);
1285 string installTargetFiles
= v2s ( this, vInstallTargetFiles
, 5 );
1286 string registryTargetFiles
= GetRegistryTargetFiles ();
1288 fprintf ( fMakefile
,
1290 installTargetFiles
.c_str (),
1291 registryTargetFiles
.c_str () );
1292 OutputNonModuleInstallTargets ();
1293 OutputModuleInstallTargets ();
1294 OutputRegistryInstallTarget ();
1295 fprintf ( fMakefile
,
1300 MingwBackend::GetModuleTestTargets (
1301 vector
<string
>& out
) const
1303 for ( std::map
<std::string
, Module
*>::const_iterator p
= ProjectNode
.modules
.begin (); p
!= ProjectNode
.modules
.end (); ++ p
)
1305 const Module
& module
= *p
->second
;
1306 if ( !module
.enabled
)
1308 if ( module
.type
== Test
)
1309 out
.push_back ( module
.name
);
1314 MingwBackend::GenerateTestTarget ()
1316 vector
<string
> vTestTargets
;
1317 GetModuleTestTargets ( vTestTargets
);
1318 string testTargets
= v2s ( vTestTargets
, 5 );
1320 fprintf ( fMakefile
,
1322 testTargets
.c_str () );
1323 fprintf ( fMakefile
,
1328 MingwBackend::GenerateDirectoryTargets ()
1330 intermediateDirectory
->CreateRule ( fMakefile
, "$(INTERMEDIATE)" );
1331 outputDirectory
->CreateRule ( fMakefile
, "$(OUTPUT)" );
1332 installDirectory
->CreateRule ( fMakefile
, "$(INSTALL)" );