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 along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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 "9.00"
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
& );
325 TypeDontCare
, // always at the end
352 DirectoryLocation directory
;
353 std::string relative_path
;
356 FileLocation ( const DirectoryLocation directory
,
357 const std::string
& relative_path
,
358 const std::string
& name
,
359 const XMLElement
*node
= NULL
);
361 FileLocation ( const FileLocation
& other
);
367 const Project
& project
;
368 const XMLElement
& node
;
369 std::string xmlbuildFile
;
372 std::string extension
;
373 std::string baseaddress
;
375 std::string buildtype
;
377 ImportLibrary
* importLibrary
;
378 ImportLibrary
* delayImportLibrary
;
380 Bootsector
* bootSector
;
382 bool isDefaultEntryPoint
;
383 Bootstrap
* bootstrap
;
384 AutoRegister
* autoRegister
; // <autoregister> node
385 IfableData non_if_data
;
386 std::vector
<Invoke
*> invocations
;
387 std::vector
<Dependency
*> dependencies
;
388 std::vector
<CompilerFlag
*> compilerFlags
;
389 std::vector
<LinkerFlag
*> linkerFlags
;
390 std::vector
<StubbedComponent
*> stubbedComponents
;
391 std::vector
<CDFile
*> cdfiles
;
392 LinkerScript
* linkerScript
;
396 std::string aliasedModuleName
;
403 FileLocation
*output
; // "path/foo.exe"
404 FileLocation
*dependency
; // "path/foo.exe" or "path/libfoo.a"
405 FileLocation
*install
;
406 std::string description
;
408 std::string layoutId
;
409 std::string layoutNameResId
;
411 Module ( const Project
& project
,
412 const XMLElement
& moduleNode
,
413 const std::string
& modulePath
);
415 ModuleType
GetModuleType ( const std::string
& location
,
416 const XMLAttribute
& attribute
);
417 bool HasImportLibrary () const;
419 std::string
GetPathWithPrefix ( const std::string
& prefix
) const; // "path/prefixfoo.exe"
420 std::string
GetPathToBaseDir() const; // "../" offset to rootdirectory
421 std::string
GetEntryPoint() const;
422 void GetTargets ( string_list
& ) const;
423 std::string
GetInvocationTarget ( const int index
) const;
424 bool HasFileWithExtension ( const IfableData
&, const std::string
& extension
) const;
425 void InvokeModule () const;
427 std::string
GetDllName() const;
429 void SetImportLibrary ( ImportLibrary
* importLibrary
);
430 void SetDelayImportLibrary ( ImportLibrary
* importLibrary
);
431 DirectoryLocation
GetTargetDirectoryTree () const;
432 std::string
GetDefaultModuleExtension () const;
433 std::string
GetDefaultModuleEntrypoint () const;
434 std::string
GetDefaultModuleBaseaddress () const;
435 std::string
GetDefaultModuleCRT () const;
436 bool GetDefaultModuleIsCRT () const;
437 std::string entrypoint
;
438 void ProcessXMLSubElement ( const XMLElement
& e
,
439 DirectoryLocation directory
,
440 const std::string
& relative_path
,
441 ParseContext
& parseContext
);
442 bool GetBooleanAttribute ( const XMLElement
& moduleNode
,
444 bool default_value
= false );
447 class ToolsetDirective
453 void ParseToolsets ( const Project
& project
, const XMLElement
& node
);
456 virtual ~ToolsetDirective() { }
457 bool IsEnabled () const;
460 class CompilerDirective
463 std::bitset
<CompilerTypesCount
> compilersSet
;
467 void ParseCompilers ( const XMLElement
& node
, const std::string
& defaultValue
);
470 CompilerDirective (): enabled ( true ) { }
471 virtual ~CompilerDirective() { }
472 void SetCompiler ( CompilerType compiler
);
473 void UnsetCompiler ( CompilerType compiler
);
474 void SetAllCompilers ();
475 void UnsetAllCompilers ();
476 bool IsCompilerSet ( CompilerType compiler
) const;
479 class Include
: public CompilerDirective
, public ToolsetDirective
482 FileLocation
*directory
;
484 Include ( const Project
& project
,
485 const XMLElement
* includeNode
);
486 Include ( const Project
& project
,
487 const XMLElement
* includeNode
,
488 const Module
* module
);
489 Include ( const Project
& project
,
490 DirectoryLocation directory
,
491 const std::string
& relative_path
);
495 const Project
& project
;
496 const XMLElement
* node
;
497 const Module
* module
;
498 DirectoryLocation
GetDefaultDirectoryTree ( const Module
* module
) const;
503 class Define
: public CompilerDirective
, public ToolsetDirective
506 const Project
& project
;
507 const Module
* module
;
508 const XMLElement
* node
;
510 std::string arguments
;
515 Define ( const Project
& project
,
516 const XMLElement
& defineNode
);
517 Define ( const Project
& project
,
518 const Module
* module
,
519 const XMLElement
& defineNode
);
520 Define ( const Project
& project
,
521 const Module
* module
,
522 const std::string
& name_
,
523 const std::string
& backend_
= "",
524 bool redefine_
= false );
537 std::string switches
;
538 bool isPreCompiledHeader
;
540 File ( DirectoryLocation directory
,
541 const std::string
& relative_path
,
542 const std::string
& name
,
544 const std::string
& _switches
,
545 bool _isPreCompiledHeader
);
548 std::string
GetFullPath () const;
554 const XMLElement
*node
;
556 const Module
& module
;
558 const Module
* importedModule
;
561 Library ( const XMLElement
& _node
,
562 const Module
& _module
,
563 const std::string
& _name
);
564 Library ( const Module
& _module
,
565 const std::string
& _name
);
574 const XMLElement
& node
;
575 const Module
& module
;
576 const Module
* invokeModule
;
577 std::vector
<InvokeFile
*> input
;
578 std::vector
<InvokeFile
*> output
;
580 Invoke ( const XMLElement
& _node
,
581 const Module
& _module
);
584 void GetTargets ( string_list
& targets
) const;
585 std::string
GetParameters () const;
587 void ProcessXMLSubElement ( const XMLElement
& e
);
588 void ProcessXMLSubElementInput ( const XMLElement
& e
);
589 void ProcessXMLSubElementOutput ( const XMLElement
& e
);
596 const XMLElement
& node
;
598 std::string switches
;
600 InvokeFile ( const XMLElement
& _node
,
601 const std::string
& _name
);
610 const XMLElement
& node
;
611 const Module
& module
;
612 const Module
* dependencyModule
;
614 Dependency ( const XMLElement
& _node
,
615 const Module
& _module
);
623 const XMLElement
& node
;
624 const Module
* module
;
625 const Module
* bootSectorModule
;
627 Bootsector ( const XMLElement
& _node
,
628 const Module
* _module
);
632 bool IsSupportedModuleType ( ModuleType type
);
638 const XMLElement
& node
;
639 const Module
& module
;
641 std::string description
;
643 std::string copyright
;
648 Metadata ( const XMLElement
& _node
,
649 const Module
& _module
);
654 class ImportLibrary
: public XmlNode
657 const Module
* module
;
659 FileLocation
*source
;
660 FileLocation
*target
;
662 ImportLibrary ( const Project
& project
,
663 const XMLElement
& node
,
664 const Module
* module
,
670 class CompilerFlag
: public CompilerDirective
, public ToolsetDirective
673 const Project
& project
;
674 const Module
* module
;
675 const XMLElement
& node
;
678 CompilerFlag ( const Project
& project
,
679 const XMLElement
& compilerFlagNode
);
680 CompilerFlag ( const Project
& project
,
681 const Module
* module
,
682 const XMLElement
& compilerFlagNode
);
690 class LinkerFlag
: public ToolsetDirective
693 const Project
& project
;
694 const Module
* module
;
695 const XMLElement
& node
;
698 LinkerFlag ( const Project
& project
,
699 const XMLElement
& linkerFlagNode
);
700 LinkerFlag ( const Project
& project
,
701 const Module
* module
,
702 const XMLElement
& linkerFlagNode
);
713 const XMLElement
& node
;
714 const Module
& module
;
715 const FileLocation
*file
;
717 LinkerScript ( const XMLElement
& node
,
718 const Module
& module
,
719 const FileLocation
*file
);
728 const Project
& project
;
729 const Module
* module
;
730 std::string name
, value
;
733 Property ( const XMLElement
& node_
,
734 const Project
& project_
,
735 const Module
* module_
);
737 Property ( const Project
& project_
,
738 const Module
* module_
,
739 const std::string
& name_
,
740 const std::string
& value_
);
746 class TestSupportCode
749 const Project
& project
;
751 TestSupportCode ( const Project
& project
);
753 void GenerateTestSupportCode ( bool verbose
);
755 bool IsTestModule ( const Module
& module
);
756 void GenerateTestSupportCodeForModule ( Module
& module
,
758 std::string
GetHooksFilename ( Module
& module
);
759 char* WriteStubbedSymbolToHooksFile ( char* buffer
,
760 const StubbedComponent
& component
,
761 const StubbedSymbol
& symbol
);
762 char* WriteStubbedComponentToHooksFile ( char* buffer
,
763 const StubbedComponent
& component
);
764 void WriteHooksFile ( Module
& module
);
765 std::string
GetStubsFilename ( Module
& module
);
766 char* WriteStubbedSymbolToStubsFile ( char* buffer
,
767 const StubbedComponent
& component
,
768 const StubbedSymbol
& symbol
,
770 char* WriteStubbedComponentToStubsFile ( char* buffer
,
771 const StubbedComponent
& component
,
773 void WriteStubsFile ( Module
& module
);
774 std::string
GetStartupFilename ( Module
& module
);
775 bool IsUnknownCharacter ( char ch
);
776 std::string
GetTestDispatcherName ( std::string filename
);
777 bool IsTestFile ( std::string
& filename
) const;
778 void GetSourceFilenames ( string_list
& list
,
779 Module
& module
) const;
780 char* WriteTestDispatcherPrototypesToStartupFile ( char* buffer
,
782 char* WriteRegisterTestsFunctionToStartupFile ( char* buffer
,
784 void WriteStartupFile ( Module
& module
);
791 SourceFile ( AutomaticDependency
* automaticDependency
,
792 const Module
& module
,
794 SourceFile
* parent
);
796 std::vector
<SourceFile
*> files
; /* List of files included in this file */
798 AutomaticDependency
* automaticDependency
;
799 const Module
& module
;
800 std::vector
<SourceFile
*> parents
; /* List of files, this file is included from */
801 time_t lastWriteTime
;
802 time_t youngestLastWriteTime
; /* Youngest last write time of this file and all children */
803 SourceFile
* youngestFile
;
807 void SkipWhitespace ();
808 bool ReadInclude ( std::string
& filename
,
809 bool& searchCurrentDirectory
,
811 bool IsIncludedFrom ( const File
& file
);
812 SourceFile
* ParseFile(const File
& file
);
813 bool CanProcessFile ( const File
& file
);
814 bool IsParentOf ( const SourceFile
* parent
,
815 const SourceFile
* child
);
822 class AutomaticDependency
824 friend class SourceFileTest
;
826 const Project
& project
;
828 AutomaticDependency ( const Project
& project
);
829 ~AutomaticDependency ();
830 bool LocateIncludedFile ( const FileLocation
& directory
,
831 const std::string
& includedFilename
);
832 bool LocateIncludedFile ( SourceFile
* sourceFile
,
833 const Module
& module
,
834 const std::string
& includedFilename
,
835 bool searchCurrentDirectory
,
837 File
& resolvedFile
);
838 SourceFile
* RetrieveFromCacheOrParse ( const Module
& module
,
840 SourceFile
* parentSourceFile
);
841 SourceFile
* RetrieveFromCache ( const File
& file
);
842 void CheckAutomaticDependencies ( bool verbose
);
843 void CheckAutomaticDependenciesForModule ( Module
& module
,
846 void GetModulesToCheck ( Module
& module
, std::vector
<const Module
*>& modules
);
847 void CheckAutomaticDependencies ( const Module
& module
,
849 void CheckAutomaticDependenciesForFile ( SourceFile
* sourceFile
);
850 void GetIncludeDirectories ( std::vector
<Include
*>& includes
,
851 const Module
& module
);
852 void GetModuleFiles ( const Module
& module
,
853 std::vector
<File
*>& files
) const;
855 void ParseFiles ( const Module
& module
);
856 void ParseFile ( const Module
& module
,
858 std::map
<std::string
, SourceFile
*> sourcefile_map
;
865 const Project
& project
;
866 const Module
* module
;
867 const XMLElement
& node
;
869 std::string nameoncd
;
871 Bootstrap ( const Project
& project
,
872 const Module
* module
,
873 const XMLElement
& bootstrapNode
);
877 bool IsSupportedModuleType ( ModuleType type
);
879 static std::string
ReplaceVariable ( const std::string
& name
,
880 const std::string
& value
,
885 class CDFile
: public XmlNode
888 FileLocation
*source
;
889 FileLocation
*target
;
891 CDFile ( const Project
& project
,
892 const XMLElement
& bootstrapNode
,
893 const std::string
& path
);
896 static std::string
ReplaceVariable ( const std::string
& name
,
897 const std::string
& value
,
902 class InstallFile
: public XmlNode
905 FileLocation
*source
;
906 FileLocation
*target
;
908 InstallFile ( const Project
& project
,
909 const XMLElement
& bootstrapNode
,
910 const std::string
& path
);
918 const XMLElement
& node
;
919 const Module
& module
;
920 const FileLocation
*file
;
923 const XMLElement
& node
,
924 const Module
& module
,
925 const FileLocation
*file
);
931 class StubbedComponent
934 const Module
* module
;
935 const XMLElement
& node
;
937 std::vector
<StubbedSymbol
*> symbols
;
939 StubbedComponent ( const Module
* module_
,
940 const XMLElement
& stubbedComponentNode
);
941 ~StubbedComponent ();
943 void ProcessXMLSubElement ( const XMLElement
& e
);
950 const XMLElement
& node
;
953 std::string strippedName
;
955 StubbedSymbol ( const XMLElement
& stubbedSymbolNode
);
959 std::string
StripSymbol ( std::string symbol
);
963 class CompilationUnit
968 CompilationUnit ( const File
* file
);
969 CompilationUnit ( const Project
* project
,
970 const Module
* module
,
971 const XMLElement
* node
);
974 bool IsGeneratedFile () const;
975 bool HasFileWithExtension ( const std::string
& extension
) const;
976 bool IsFirstFile () const;
977 const FileLocation
& GetFilename () const;
978 const std::string
& GetSwitches () const;
979 void AddFile ( const File
* file
);
980 const std::vector
<const File
*> GetFiles () const;
982 const Project
* project
;
983 const Module
* module
;
984 const XMLElement
* node
;
985 std::vector
<const File
*> files
;
986 FileLocation
*default_name
;
990 class CompilationUnitSupportCode
993 const Project
& project
;
995 CompilationUnitSupportCode ( const Project
& project
);
996 ~CompilationUnitSupportCode ();
997 void Generate ( bool verbose
);
999 void GenerateForModule ( Module
& module
,
1001 std::string
GetCompilationUnitFilename ( Module
& module
,
1002 CompilationUnit
& compilationUnit
);
1003 void WriteCompilationUnitFile ( Module
& module
,
1004 CompilationUnit
& compilationUnit
);
1008 enum AutoRegisterType
1015 class AutoRegister
: public XmlNode
1018 const Module
* module
;
1019 std::string infSection
;
1020 AutoRegisterType type
;
1021 AutoRegister ( const Project
& project_
,
1022 const Module
* module_
,
1023 const XMLElement
& node_
);
1025 bool IsSupportedModuleType ( ModuleType type
);
1026 AutoRegisterType
GetAutoRegisterType( const std::string
& type
);
1031 class SysSetupGenerator
1034 const Project
& project
;
1035 SysSetupGenerator ( const Project
& project
);
1036 ~SysSetupGenerator ();
1039 std::string
GetDirectoryId ( const Module
& module
);
1040 std::string
GetFlags ( const Module
& module
);
1041 void Generate ( HINF inf
,
1042 const Module
& module
);
1047 InitializeEnvironment ();
1050 Right ( const std::string
& s
, size_t n
);
1053 Replace ( const std::string
& s
, const std::string
& find
, const std::string
& with
);
1056 ChangeSeparator ( const std::string
& s
,
1057 const char fromSeparator
,
1058 const char toSeparator
);
1061 FixSeparator ( const std::string
& s
);
1064 FixSeparatorForSystemCommand ( const std::string
& s
);
1067 DosSeparator ( const std::string
& s
);
1071 const std::string
& filename
,
1072 const std::string
& newExtension
);
1076 const Project
& project
,
1077 const std::string
& location
,
1078 const std::string
& path
,
1079 const std::string
& att_value
);
1082 GetExtension ( const FileLocation
& file
);
1085 NormalizeFilename ( const std::string
& filename
);
1088 ToLower ( std::string filename
);
1090 #endif /* __RBUILD_H */