2 * Copyright (C) 2005 Casper S. Hornstrup
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
30 #include <sys/utime.h>
41 #include "exception.h"
44 class Backend
; // forward declaration
46 typedef std::vector
<std::string
> string_list
;
48 extern std::string ExePrefix
;
49 extern std::string ExePostfix
;
50 extern std::string sSep
;
51 extern std::string sBadSep
;
56 #define DEF_EXEPREFIX ""
57 #define DEF_EXEPOSTFIX ".exe"
59 #define DEF_CBAD_SEP '/'
61 #define DEF_SBAD_SEP "/"
63 #define DEF_EXEPREFIX "./"
64 #define DEF_EXEPOSTFIX ""
66 #define DEF_CBAD_SEP '\\'
68 #define DEF_SBAD_SEP "\\"
71 #define MS_VS_DEF_VERSION "7.10"
90 class TestSupportCode
;
91 class AutomaticDependency
;
96 class StubbedComponent
;
98 class CompilationUnit
;
102 class SourceFileTest
;
106 typedef std::map
<std::string
,Directory
*> directory_map
;
111 const Project
& project
;
112 const XMLElement
& node
;
114 XmlNode ( const Project
& project_
,
115 const XMLElement
& node_
);
119 virtual void ProcessXML();
122 enum DirectoryLocation
125 IntermediateDirectory
,
135 directory_map subdirs
;
136 Directory ( const std::string
& name
);
138 void Add ( const char* subdir
);
139 void GenerateTree ( DirectoryLocation root
,
141 void CreateRule ( FILE* f
,
142 const std::string
& parent
);
144 bool mkdir_p ( const char* path
);
145 bool CreateDirectory ( const std::string
& path
);
146 std::string
EscapeSpaces ( const std::string
& path
);
147 void GenerateTree ( const std::string
& parent
,
151 enum DependenciesType
154 AutomaticDependencies
,
177 DependenciesType Dependencies
;
178 bool CheckDependenciesForModuleOnly
;
179 bool CompilationUnitsEnabled
;
180 bool PrecompiledHeadersEnabled
;
181 std::string CheckDependenciesForModuleOnlyModule
;
182 std::string VSProjectVersion
;
183 std::string VSConfigurationType
;
184 bool UseVSVersionInPath
;
185 bool UseConfigurationInPath
;
186 bool MakeHandlesInstallDirectories
;
187 bool GenerateProxyMakefilesInSourceTree
;
189 CompilerSet Compiler
;
196 static std::string
GetVariable ( const std::string
& name
);
197 static std::string
GetArch ();
198 static std::string
GetIntermediatePath ();
199 static std::string
GetOutputPath ();
200 static std::string
GetCdOutputPath ();
201 static std::string
GetInstallPath ();
202 static std::string
GetAutomakeFile ( const std::string
& defaultFile
);
203 static std::string
GetEnvironmentVariablePathOrDefault ( const std::string
& name
,
204 const std::string
& defaultValue
);
208 class FileSupportCode
211 static void WriteIfChanged ( char* outbuf
,
212 const std::string
& filename
,
213 bool ignoreError
= false );
220 CompilationUnit
* compilationUnit
;
228 std::vector
<CompilationUnit
*> compilationUnits
;
229 std::vector
<File
*> files
;
230 std::vector
<Include
*> includes
;
231 std::vector
<Define
*> defines
;
232 std::vector
<Library
*> libraries
;
233 std::map
<std::string
, Property
*> properties
;
234 std::vector
<Module
*> modules
;
235 std::vector
<CompilerFlag
*> compilerFlags
;
236 int asmFiles
; // number of .asm files in compilationUnits
241 void ExtractModules( std::map
<std::string
, Module
*> &modules
);
247 XMLElement
*node
, *head
;
250 const Configuration
& configuration
;
252 std::string makefile
;
253 XMLIncludes xmlbuildfiles
;
254 std::vector
<LinkerFlag
*> linkerFlags
;
255 std::vector
<CDFile
*> cdfiles
;
256 std::vector
<InstallFile
*> installfiles
;
257 std::map
<std::string
, Module
*> modules
;
258 IfableData non_if_data
;
259 IfableData host_non_if_data
;
261 bool allowWarningsSet
;
263 Project ( const Configuration
& configuration
,
264 const std::string
& filename
,
265 const std::map
<std::string
, std::string
>* properties
= NULL
);
267 void SetBackend ( Backend
* backend
) { _backend
= backend
; }
268 Backend
& GetBackend() { return *_backend
; }
269 void ExecuteInvocations ();
271 void ProcessXML ( const std::string
& path
);
272 Module
* LocateModule ( const std::string
& name
);
273 const Module
* LocateModule ( const std::string
& name
) const;
274 const std::string
& GetProjectFilename () const;
275 std::string
ResolveProperties ( const std::string
& s
) const;
276 const Property
* LookupProperty ( const std::string
& name
) const;
277 std::string
GetCompilerSet () const;
278 std::string
GetLinkerSet () const;
280 std::string
ResolveNextProperty ( const std::string
& s
) const;
282 void ProcessXMLSubElement ( const XMLElement
& e
,
283 const std::string
& path
,
284 ParseContext
& parseContext
);
286 // disable copy semantics
287 Project ( const Project
& );
288 Project
& operator = ( const Project
& );
327 TypeDontCare
, // always at the end
354 DirectoryLocation directory
;
355 std::string relative_path
;
358 FileLocation ( const DirectoryLocation directory
,
359 const std::string
& relative_path
,
360 const std::string
& name
,
361 const XMLElement
*node
= NULL
);
363 FileLocation ( const FileLocation
& other
);
369 const Project
& project
;
370 const XMLElement
& node
;
371 std::string xmlbuildFile
;
374 std::string extension
;
375 std::string baseaddress
;
377 std::string buildtype
;
379 ImportLibrary
* importLibrary
;
380 ImportLibrary
* delayImportLibrary
;
382 Bootsector
* bootSector
;
384 bool underscoreSymbols
;
386 bool isDefaultEntryPoint
;
387 Bootstrap
* bootstrap
;
388 AutoRegister
* autoRegister
; // <autoregister> node
389 IfableData non_if_data
;
390 std::vector
<Invoke
*> invocations
;
391 std::vector
<Dependency
*> dependencies
;
392 std::vector
<CompilerFlag
*> compilerFlags
;
393 std::vector
<LinkerFlag
*> linkerFlags
;
394 std::vector
<StubbedComponent
*> stubbedComponents
;
395 LinkerScript
* linkerScript
;
399 std::string aliasedModuleName
;
406 FileLocation
*output
; // "path/foo.exe"
407 FileLocation
*dependency
; // "path/foo.exe" or "path/libfoo.a"
408 FileLocation
*install
;
409 std::string description
;
411 std::string layoutId
;
412 std::string layoutNameResId
;
414 Module ( const Project
& project
,
415 const XMLElement
& moduleNode
,
416 const std::string
& modulePath
);
418 ModuleType
GetModuleType ( const std::string
& location
,
419 const XMLAttribute
& attribute
);
420 bool HasImportLibrary () const;
422 std::string
GetPathWithPrefix ( const std::string
& prefix
) const; // "path/prefixfoo.exe"
423 std::string
GetPathToBaseDir() const; // "../" offset to rootdirectory
424 std::string
GetEntryPoint(bool leadingUnderscore
) const;
425 void GetTargets ( string_list
& ) const;
426 std::string
GetInvocationTarget ( const int index
) const;
427 bool HasFileWithExtension ( const IfableData
&, const std::string
& extension
) const;
428 void InvokeModule () const;
430 std::string
GetDllName() const;
432 void SetImportLibrary ( ImportLibrary
* importLibrary
);
433 void SetDelayImportLibrary ( ImportLibrary
* importLibrary
);
434 DirectoryLocation
GetTargetDirectoryTree () const;
435 std::string
GetDefaultModuleExtension () const;
436 std::string
GetDefaultModuleEntrypoint () const;
437 std::string
GetDefaultModuleBaseaddress () const;
438 std::string
GetDefaultModuleCRT () const;
439 bool GetDefaultModuleIsCRT () const;
440 std::string entrypoint
;
441 void ProcessXMLSubElement ( const XMLElement
& e
,
442 DirectoryLocation directory
,
443 const std::string
& relative_path
,
444 ParseContext
& parseContext
);
445 bool GetBooleanAttribute ( const XMLElement
& moduleNode
,
447 bool default_value
= false );
450 class ToolsetDirective
456 void ParseToolsets ( const Project
& project
, const XMLElement
& node
);
459 bool IsEnabled () const;
462 class CompilerDirective
465 std::bitset
<CompilerTypesCount
> compilersSet
;
469 void ParseCompilers ( const XMLElement
& node
, const std::string
& defaultValue
);
472 CompilerDirective (): enabled ( true ) { }
473 void SetCompiler ( CompilerType compiler
);
474 void UnsetCompiler ( CompilerType compiler
);
475 void SetAllCompilers ();
476 void UnsetAllCompilers ();
477 bool IsCompilerSet ( CompilerType compiler
) const;
480 class Include
: public CompilerDirective
, public ToolsetDirective
483 FileLocation
*directory
;
485 Include ( const Project
& project
,
486 const XMLElement
* includeNode
);
487 Include ( const Project
& project
,
488 const XMLElement
* includeNode
,
489 const Module
* module
);
490 Include ( const Project
& project
,
491 DirectoryLocation directory
,
492 const std::string
& relative_path
);
496 const Project
& project
;
497 const XMLElement
* node
;
498 const Module
* module
;
499 DirectoryLocation
GetDefaultDirectoryTree ( const Module
* module
) const;
504 class Define
: public CompilerDirective
, public ToolsetDirective
507 const Project
& project
;
508 const Module
* module
;
509 const XMLElement
* node
;
511 std::string arguments
;
516 Define ( const Project
& project
,
517 const XMLElement
& defineNode
);
518 Define ( const Project
& project
,
519 const Module
* module
,
520 const XMLElement
& defineNode
);
521 Define ( const Project
& project
,
522 const Module
* module
,
523 const std::string
& name_
,
524 const std::string
& backend_
= "",
525 bool redefine_
= false );
538 std::string switches
;
539 bool isPreCompiledHeader
;
541 File ( DirectoryLocation directory
,
542 const std::string
& relative_path
,
543 const std::string
& name
,
545 const std::string
& _switches
,
546 bool _isPreCompiledHeader
);
549 std::string
GetFullPath () const;
555 const XMLElement
*node
;
557 const Module
& module
;
559 const Module
* importedModule
;
562 Library ( const XMLElement
& _node
,
563 const Module
& _module
,
564 const std::string
& _name
);
565 Library ( const Module
& _module
,
566 const std::string
& _name
);
575 const XMLElement
& node
;
576 const Module
& module
;
577 const Module
* invokeModule
;
578 std::vector
<InvokeFile
*> input
;
579 std::vector
<InvokeFile
*> output
;
581 Invoke ( const XMLElement
& _node
,
582 const Module
& _module
);
585 void GetTargets ( string_list
& targets
) const;
586 std::string
GetParameters () const;
588 void ProcessXMLSubElement ( const XMLElement
& e
);
589 void ProcessXMLSubElementInput ( const XMLElement
& e
);
590 void ProcessXMLSubElementOutput ( const XMLElement
& e
);
597 const XMLElement
& node
;
599 std::string switches
;
601 InvokeFile ( const XMLElement
& _node
,
602 const std::string
& _name
);
611 const XMLElement
& node
;
612 const Module
& module
;
613 const Module
* dependencyModule
;
615 Dependency ( const XMLElement
& _node
,
616 const Module
& _module
);
624 const XMLElement
& node
;
625 const Module
* module
;
626 const Module
* bootSectorModule
;
628 Bootsector ( const XMLElement
& _node
,
629 const Module
* _module
);
633 bool IsSupportedModuleType ( ModuleType type
);
639 const XMLElement
& node
;
640 const Module
& module
;
642 std::string description
;
644 std::string copyright
;
649 Metadata ( const XMLElement
& _node
,
650 const Module
& _module
);
655 class ImportLibrary
: public XmlNode
658 const Module
* module
;
660 FileLocation
*source
;
661 FileLocation
*target
;
663 ImportLibrary ( const Project
& project
,
664 const XMLElement
& node
,
665 const Module
* module
,
671 class CompilerFlag
: public CompilerDirective
, public ToolsetDirective
674 const Project
& project
;
675 const Module
* module
;
676 const XMLElement
& node
;
679 CompilerFlag ( const Project
& project
,
680 const XMLElement
& compilerFlagNode
);
681 CompilerFlag ( const Project
& project
,
682 const Module
* module
,
683 const XMLElement
& compilerFlagNode
);
691 class LinkerFlag
: public ToolsetDirective
694 const Project
& project
;
695 const Module
* module
;
696 const XMLElement
& node
;
699 LinkerFlag ( const Project
& project
,
700 const XMLElement
& linkerFlagNode
);
701 LinkerFlag ( const Project
& project
,
702 const Module
* module
,
703 const XMLElement
& linkerFlagNode
);
714 const XMLElement
& node
;
715 const Module
& module
;
716 const FileLocation
*file
;
718 LinkerScript ( const XMLElement
& node
,
719 const Module
& module
,
720 const FileLocation
*file
);
729 const Project
& project
;
730 const Module
* module
;
731 std::string name
, value
;
734 Property ( const XMLElement
& node_
,
735 const Project
& project_
,
736 const Module
* module_
);
738 Property ( const Project
& project_
,
739 const Module
* module_
,
740 const std::string
& name_
,
741 const std::string
& value_
);
747 class TestSupportCode
750 const Project
& project
;
752 TestSupportCode ( const Project
& project
);
754 void GenerateTestSupportCode ( bool verbose
);
756 bool IsTestModule ( const Module
& module
);
757 void GenerateTestSupportCodeForModule ( Module
& module
,
759 std::string
GetHooksFilename ( Module
& module
);
760 char* WriteStubbedSymbolToHooksFile ( char* buffer
,
761 const StubbedComponent
& component
,
762 const StubbedSymbol
& symbol
);
763 char* WriteStubbedComponentToHooksFile ( char* buffer
,
764 const StubbedComponent
& component
);
765 void WriteHooksFile ( Module
& module
);
766 std::string
GetStubsFilename ( Module
& module
);
767 char* WriteStubbedSymbolToStubsFile ( char* buffer
,
768 const StubbedComponent
& component
,
769 const StubbedSymbol
& symbol
,
771 char* WriteStubbedComponentToStubsFile ( char* buffer
,
772 const StubbedComponent
& component
,
774 void WriteStubsFile ( Module
& module
);
775 std::string
GetStartupFilename ( Module
& module
);
776 bool IsUnknownCharacter ( char ch
);
777 std::string
GetTestDispatcherName ( std::string filename
);
778 bool IsTestFile ( std::string
& filename
) const;
779 void GetSourceFilenames ( string_list
& list
,
780 Module
& module
) const;
781 char* WriteTestDispatcherPrototypesToStartupFile ( char* buffer
,
783 char* WriteRegisterTestsFunctionToStartupFile ( char* buffer
,
785 void WriteStartupFile ( Module
& module
);
792 SourceFile ( AutomaticDependency
* automaticDependency
,
793 const Module
& module
,
795 SourceFile
* parent
);
797 std::vector
<SourceFile
*> files
; /* List of files included in this file */
799 AutomaticDependency
* automaticDependency
;
800 const Module
& module
;
801 std::vector
<SourceFile
*> parents
; /* List of files, this file is included from */
802 time_t lastWriteTime
;
803 time_t youngestLastWriteTime
; /* Youngest last write time of this file and all children */
804 SourceFile
* youngestFile
;
808 void SkipWhitespace ();
809 bool ReadInclude ( std::string
& filename
,
810 bool& searchCurrentDirectory
,
812 bool IsIncludedFrom ( const File
& file
);
813 SourceFile
* ParseFile(const File
& file
);
814 bool CanProcessFile ( const File
& file
);
815 bool IsParentOf ( const SourceFile
* parent
,
816 const SourceFile
* child
);
823 class AutomaticDependency
825 friend class SourceFileTest
;
827 const Project
& project
;
829 AutomaticDependency ( const Project
& project
);
830 ~AutomaticDependency ();
831 bool LocateIncludedFile ( const FileLocation
& directory
,
832 const std::string
& includedFilename
);
833 bool LocateIncludedFile ( SourceFile
* sourceFile
,
834 const Module
& module
,
835 const std::string
& includedFilename
,
836 bool searchCurrentDirectory
,
838 File
& resolvedFile
);
839 SourceFile
* RetrieveFromCacheOrParse ( const Module
& module
,
841 SourceFile
* parentSourceFile
);
842 SourceFile
* RetrieveFromCache ( const File
& file
);
843 void CheckAutomaticDependencies ( bool verbose
);
844 void CheckAutomaticDependenciesForModule ( Module
& module
,
847 void GetModulesToCheck ( Module
& module
, std::vector
<const Module
*>& modules
);
848 void CheckAutomaticDependencies ( const Module
& module
,
850 void CheckAutomaticDependenciesForFile ( SourceFile
* sourceFile
);
851 void GetIncludeDirectories ( std::vector
<Include
*>& includes
,
852 const Module
& module
);
853 void GetModuleFiles ( const Module
& module
,
854 std::vector
<File
*>& files
) const;
856 void ParseFiles ( const Module
& module
);
857 void ParseFile ( const Module
& module
,
859 std::map
<std::string
, SourceFile
*> sourcefile_map
;
866 const Project
& project
;
867 const Module
* module
;
868 const XMLElement
& node
;
870 std::string nameoncd
;
872 Bootstrap ( const Project
& project
,
873 const Module
* module
,
874 const XMLElement
& bootstrapNode
);
878 bool IsSupportedModuleType ( ModuleType type
);
880 static std::string
ReplaceVariable ( const std::string
& name
,
881 const std::string
& value
,
886 class CDFile
: public XmlNode
889 FileLocation
*source
;
890 FileLocation
*target
;
892 CDFile ( const Project
& project
,
893 const XMLElement
& bootstrapNode
,
894 const std::string
& path
);
897 static std::string
ReplaceVariable ( const std::string
& name
,
898 const std::string
& value
,
903 class InstallFile
: public XmlNode
906 FileLocation
*source
;
907 FileLocation
*target
;
909 InstallFile ( const Project
& project
,
910 const XMLElement
& bootstrapNode
,
911 const std::string
& path
);
919 const XMLElement
& node
;
920 const Module
& module
;
921 const FileLocation
*file
;
924 const XMLElement
& node
,
925 const Module
& module
,
926 const FileLocation
*file
);
932 class StubbedComponent
935 const Module
* module
;
936 const XMLElement
& node
;
938 std::vector
<StubbedSymbol
*> symbols
;
940 StubbedComponent ( const Module
* module_
,
941 const XMLElement
& stubbedComponentNode
);
942 ~StubbedComponent ();
944 void ProcessXMLSubElement ( const XMLElement
& e
);
951 const XMLElement
& node
;
954 std::string strippedName
;
956 StubbedSymbol ( const XMLElement
& stubbedSymbolNode
);
960 std::string
StripSymbol ( std::string symbol
);
964 class CompilationUnit
969 CompilationUnit ( const File
* file
);
970 CompilationUnit ( const Project
* project
,
971 const Module
* module
,
972 const XMLElement
* node
);
975 bool IsGeneratedFile () const;
976 bool HasFileWithExtension ( const std::string
& extension
) const;
977 bool IsFirstFile () const;
978 const FileLocation
& GetFilename () const;
979 const std::string
& GetSwitches () const;
980 void AddFile ( const File
* file
);
981 const std::vector
<const File
*> GetFiles () const;
983 const Project
* project
;
984 const Module
* module
;
985 const XMLElement
* node
;
986 std::vector
<const File
*> files
;
987 FileLocation
*default_name
;
991 class CompilationUnitSupportCode
994 const Project
& project
;
996 CompilationUnitSupportCode ( const Project
& project
);
997 ~CompilationUnitSupportCode ();
998 void Generate ( bool verbose
);
1000 void GenerateForModule ( Module
& module
,
1002 std::string
GetCompilationUnitFilename ( Module
& module
,
1003 CompilationUnit
& compilationUnit
);
1004 void WriteCompilationUnitFile ( Module
& module
,
1005 CompilationUnit
& compilationUnit
);
1009 enum AutoRegisterType
1016 class AutoRegister
: public XmlNode
1019 const Module
* module
;
1020 std::string infSection
;
1021 AutoRegisterType type
;
1022 AutoRegister ( const Project
& project_
,
1023 const Module
* module_
,
1024 const XMLElement
& node_
);
1026 bool IsSupportedModuleType ( ModuleType type
);
1027 AutoRegisterType
GetAutoRegisterType( const std::string
& type
);
1032 class SysSetupGenerator
1035 const Project
& project
;
1036 SysSetupGenerator ( const Project
& project
);
1037 ~SysSetupGenerator ();
1040 std::string
GetDirectoryId ( const Module
& module
);
1041 std::string
GetFlags ( const Module
& module
);
1042 void Generate ( HINF inf
,
1043 const Module
& module
);
1048 InitializeEnvironment ();
1051 Right ( const std::string
& s
, size_t n
);
1054 Replace ( const std::string
& s
, const std::string
& find
, const std::string
& with
);
1057 ChangeSeparator ( const std::string
& s
,
1058 const char fromSeparator
,
1059 const char toSeparator
);
1062 FixSeparator ( const std::string
& s
);
1065 FixSeparatorForSystemCommand ( const std::string
& s
);
1068 DosSeparator ( const std::string
& s
);
1072 const std::string
& filename
,
1073 const std::string
& newExtension
);
1077 const Project
& project
,
1078 const std::string
& location
,
1079 const std::string
& path
,
1080 const std::string
& att_value
);
1083 GetExtension ( const FileLocation
& file
);
1086 NormalizeFilename ( const std::string
& filename
);
1089 ToLower ( std::string filename
);
1091 #endif /* __RBUILD_H */