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"
91 class TestSupportCode
;
92 class AutomaticDependency
;
97 class StubbedComponent
;
99 class CompilationUnit
;
103 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
,
159 bool AutomaticDependencies
;
160 bool CheckDependenciesForModuleOnly
;
161 bool CompilationUnitsEnabled
;
162 std::string CheckDependenciesForModuleOnlyModule
;
163 std::string VSProjectVersion
;
164 std::string VSConfigurationType
;
165 bool UseVSVersionInPath
;
166 bool UseConfigurationInPath
;
167 bool MakeHandlesInstallDirectories
;
168 bool GenerateProxyMakefilesInSourceTree
;
175 static std::string
GetVariable ( const std::string
& name
);
176 static std::string
GetArch ();
177 static std::string
GetIntermediatePath ();
178 static std::string
GetOutputPath ();
179 static std::string
GetCdOutputPath ();
180 static std::string
GetInstallPath ();
181 static std::string
GetAutomakeFile ( const std::string
& defaultFile
);
182 static std::string
GetEnvironmentVariablePathOrDefault ( const std::string
& name
,
183 const std::string
& defaultValue
);
187 class FileSupportCode
190 static void WriteIfChanged ( char* outbuf
,
191 const std::string
& filename
,
192 bool ignoreError
= false );
200 CompilationUnit
* compilationUnit
;
208 std::vector
<CompilationUnit
*> compilationUnits
;
209 std::vector
<File
*> files
;
210 std::vector
<Include
*> includes
;
211 std::vector
<Define
*> defines
;
212 std::vector
<Library
*> libraries
;
213 std::vector
<Property
*> properties
;
214 std::vector
<Module
*> modules
;
215 std::vector
<CompilerFlag
*> compilerFlags
;
216 std::vector
<If
*> ifs
;
217 int asmFiles
; // number of .asm files in compilationUnits
222 void ExtractModules( std::vector
<Module
*> &modules
);
228 XMLElement
*node
, *head
;
231 const Configuration
& configuration
;
233 std::string makefile
;
234 XMLIncludes xmlbuildfiles
;
235 std::vector
<LinkerFlag
*> linkerFlags
;
236 std::vector
<CDFile
*> cdfiles
;
237 std::vector
<InstallFile
*> installfiles
;
238 std::vector
<Module
*> modules
;
239 IfableData non_if_data
;
241 Project ( const Configuration
& configuration
,
242 const std::string
& filename
,
243 const std::map
<std::string
, std::string
>* properties
= NULL
);
245 void SetBackend ( Backend
* backend
) { _backend
= backend
; }
246 Backend
& GetBackend() { return *_backend
; }
247 void WriteConfigurationFile ();
248 void ExecuteInvocations ();
250 void ProcessXML ( const std::string
& path
);
251 Module
* LocateModule ( const std::string
& name
);
252 const Module
* LocateModule ( const std::string
& name
) const;
253 const std::string
& GetProjectFilename () const;
254 std::string
ResolveProperties ( const std::string
& s
) const;
256 std::string
ResolveNextProperty ( const std::string
& s
) const;
257 const Property
* LookupProperty ( const std::string
& name
) const;
258 void SetConfigurationOption ( char* s
,
260 std::string alternativeName
);
261 void SetConfigurationOption ( char* s
,
264 void ProcessXMLSubElement ( const XMLElement
& e
,
265 const std::string
& path
,
266 ParseContext
& parseContext
);
268 // disable copy semantics
269 Project ( const Project
& );
270 Project
& operator = ( const Project
& );
281 KernelModeDriver
= 5,
301 EmbeddedTypeLib
= 26,
320 CompilerTypeDontCare
,
328 DirectoryLocation directory
;
329 std::string relative_path
;
332 FileLocation ( const DirectoryLocation directory
,
333 const std::string
& relative_path
,
334 const std::string
& name
,
335 const XMLElement
*node
= NULL
);
337 FileLocation ( const FileLocation
& other
);
343 const Project
& project
;
344 const XMLElement
& node
;
345 std::string xmlbuildFile
;
348 std::string extension
;
349 std::string baseaddress
;
351 std::string buildtype
;
353 ImportLibrary
* importLibrary
;
356 bool underscoreSymbols
;
358 bool isDefaultEntryPoint
;
359 Bootstrap
* bootstrap
;
360 AutoRegister
* autoRegister
; // <autoregister> node
361 IfableData non_if_data
;
362 std::vector
<Invoke
*> invocations
;
363 std::vector
<Dependency
*> dependencies
;
364 std::vector
<CompilerFlag
*> compilerFlags
;
365 std::vector
<LinkerFlag
*> linkerFlags
;
366 std::vector
<StubbedComponent
*> stubbedComponents
;
367 LinkerScript
* linkerScript
;
372 std::string aliasedModuleName
;
376 FileLocation
*output
; // "path/foo.exe"
377 FileLocation
*dependency
; // "path/foo.exe" or "path/libfoo.a"
378 FileLocation
*install
;
379 std::string description
;
382 Module ( const Project
& project
,
383 const XMLElement
& moduleNode
,
384 const std::string
& modulePath
);
386 ModuleType
GetModuleType ( const std::string
& location
,
387 const XMLAttribute
& attribute
);
388 bool HasImportLibrary () const;
390 std::string
GetPathWithPrefix ( const std::string
& prefix
) const; // "path/prefixfoo.exe"
391 std::string
GetPathToBaseDir() const; // "../" offset to rootdirectory
392 std::string
GetEntryPoint(bool leadingUnderscore
) const;
393 void GetTargets ( string_list
& ) const;
394 std::string
GetInvocationTarget ( const int index
) const;
395 bool HasFileWithExtension ( const IfableData
&, const std::string
& extension
) const;
396 void InvokeModule () const;
399 void SetImportLibrary ( ImportLibrary
* importLibrary
);
400 DirectoryLocation
GetTargetDirectoryTree () const;
401 std::string
GetDefaultModuleExtension () const;
402 std::string
GetDefaultModuleEntrypoint () const;
403 std::string
GetDefaultModuleBaseaddress () const;
404 std::string entrypoint
;
405 void ProcessXMLSubElement ( const XMLElement
& e
,
406 DirectoryLocation directory
,
407 const std::string
& relative_path
,
408 ParseContext
& parseContext
);
415 FileLocation
*directory
;
417 Include ( const Project
& project
,
418 const XMLElement
* includeNode
);
419 Include ( const Project
& project
,
420 const XMLElement
* includeNode
,
421 const Module
* module
);
422 Include ( const Project
& project
,
423 DirectoryLocation directory
,
424 const std::string
& relative_path
);
428 const Project
& project
;
429 const XMLElement
* node
;
430 const Module
* module
;
431 DirectoryLocation
GetDefaultDirectoryTree ( const Module
* module
) const;
438 const Project
& project
;
439 const Module
* module
;
440 const XMLElement
* node
;
446 Define ( const Project
& project
,
447 const XMLElement
& defineNode
);
448 Define ( const Project
& project
,
449 const Module
* module
,
450 const XMLElement
& defineNode
);
451 Define ( const Project
& project
,
452 const Module
* module
,
453 const std::string
& name_
,
454 const std::string
& backend_
= "" );
467 std::string switches
;
468 bool isPreCompiledHeader
;
470 File ( DirectoryLocation directory
,
471 const std::string
& relative_path
,
472 const std::string
& name
,
474 const std::string
& _switches
,
475 bool _isPreCompiledHeader
);
478 std::string
GetFullPath () const;
484 const XMLElement
*node
;
486 const Module
& module
;
488 const Module
* importedModule
;
490 Library ( const XMLElement
& _node
,
491 const Module
& _module
,
492 const std::string
& _name
);
493 Library ( const Module
& _module
,
494 const std::string
& _name
);
503 const XMLElement
& node
;
504 const Module
& module
;
505 const Module
* invokeModule
;
506 std::vector
<InvokeFile
*> input
;
507 std::vector
<InvokeFile
*> output
;
509 Invoke ( const XMLElement
& _node
,
510 const Module
& _module
);
513 void GetTargets ( string_list
& targets
) const;
514 std::string
GetParameters () const;
516 void ProcessXMLSubElement ( const XMLElement
& e
);
517 void ProcessXMLSubElementInput ( const XMLElement
& e
);
518 void ProcessXMLSubElementOutput ( const XMLElement
& e
);
525 const XMLElement
& node
;
527 std::string switches
;
529 InvokeFile ( const XMLElement
& _node
,
530 const std::string
& _name
);
539 const XMLElement
& node
;
540 const Module
& module
;
541 const Module
* dependencyModule
;
543 Dependency ( const XMLElement
& _node
,
544 const Module
& _module
);
552 const XMLElement
& node
;
553 const Module
& module
;
555 std::string description
;
557 std::string copyright
;
562 Metadata ( const XMLElement
& _node
,
563 const Module
& _module
);
568 class ImportLibrary
: public XmlNode
571 const Module
& module
;
573 FileLocation
*source
;
575 ImportLibrary ( const Project
& project
,
576 const XMLElement
& node
,
577 const Module
& module
);
585 const XMLElement
& node
;
586 const Project
& project
;
587 const Module
* module
;
589 std::string property
, value
;
592 If ( const XMLElement
& node_
,
593 const Project
& project_
,
594 const Module
* module_
,
595 const bool negated_
= false );
605 const Project
& project
;
606 const Module
* module
;
607 const XMLElement
& node
;
609 CompilerType compiler
;
611 CompilerFlag ( const Project
& project
,
612 const XMLElement
& compilerFlagNode
);
613 CompilerFlag ( const Project
& project
,
614 const Module
* module
,
615 const XMLElement
& compilerFlagNode
);
626 const Project
& project
;
627 const Module
* module
;
628 const XMLElement
& node
;
631 LinkerFlag ( const Project
& project
,
632 const XMLElement
& linkerFlagNode
);
633 LinkerFlag ( const Project
& project
,
634 const Module
* module
,
635 const XMLElement
& linkerFlagNode
);
646 const XMLElement
& node
;
647 const Module
& module
;
648 const FileLocation
*file
;
650 LinkerScript ( const XMLElement
& node
,
651 const Module
& module
,
652 const FileLocation
*file
);
661 const Project
& project
;
662 const Module
* module
;
663 std::string name
, value
;
666 Property ( const XMLElement
& node_
,
667 const Project
& project_
,
668 const Module
* module_
);
670 Property ( const Project
& project_
,
671 const Module
* module_
,
672 const std::string
& name_
,
673 const std::string
& value_
);
679 class TestSupportCode
682 const Project
& project
;
684 TestSupportCode ( const Project
& project
);
686 void GenerateTestSupportCode ( bool verbose
);
688 bool IsTestModule ( const Module
& module
);
689 void GenerateTestSupportCodeForModule ( Module
& module
,
691 std::string
GetHooksFilename ( Module
& module
);
692 char* WriteStubbedSymbolToHooksFile ( char* buffer
,
693 const StubbedComponent
& component
,
694 const StubbedSymbol
& symbol
);
695 char* WriteStubbedComponentToHooksFile ( char* buffer
,
696 const StubbedComponent
& component
);
697 void WriteHooksFile ( Module
& module
);
698 std::string
GetStubsFilename ( Module
& module
);
699 char* WriteStubbedSymbolToStubsFile ( char* buffer
,
700 const StubbedComponent
& component
,
701 const StubbedSymbol
& symbol
,
703 char* WriteStubbedComponentToStubsFile ( char* buffer
,
704 const StubbedComponent
& component
,
706 void WriteStubsFile ( Module
& module
);
707 std::string
GetStartupFilename ( Module
& module
);
708 bool IsUnknownCharacter ( char ch
);
709 std::string
GetTestDispatcherName ( std::string filename
);
710 bool IsTestFile ( std::string
& filename
) const;
711 void GetSourceFilenames ( string_list
& list
,
712 Module
& module
) const;
713 char* WriteTestDispatcherPrototypesToStartupFile ( char* buffer
,
715 char* WriteRegisterTestsFunctionToStartupFile ( char* buffer
,
717 void WriteStartupFile ( Module
& module
);
724 SourceFile ( AutomaticDependency
* automaticDependency
,
725 const Module
& module
,
727 SourceFile
* parent
);
729 std::vector
<SourceFile
*> files
; /* List of files included in this file */
731 AutomaticDependency
* automaticDependency
;
732 const Module
& module
;
733 std::vector
<SourceFile
*> parents
; /* List of files, this file is included from */
734 time_t lastWriteTime
;
735 time_t youngestLastWriteTime
; /* Youngest last write time of this file and all children */
736 SourceFile
* youngestFile
;
740 void SkipWhitespace ();
741 bool ReadInclude ( std::string
& filename
,
742 bool& searchCurrentDirectory
,
744 bool IsIncludedFrom ( const File
& file
);
745 SourceFile
* ParseFile(const File
& file
);
746 bool CanProcessFile ( const File
& file
);
747 bool IsParentOf ( const SourceFile
* parent
,
748 const SourceFile
* child
);
755 class AutomaticDependency
757 friend class SourceFileTest
;
759 const Project
& project
;
761 AutomaticDependency ( const Project
& project
);
762 ~AutomaticDependency ();
763 bool LocateIncludedFile ( const FileLocation
& directory
,
764 const std::string
& includedFilename
);
765 bool LocateIncludedFile ( SourceFile
* sourceFile
,
766 const Module
& module
,
767 const std::string
& includedFilename
,
768 bool searchCurrentDirectory
,
770 File
& resolvedFile
);
771 SourceFile
* RetrieveFromCacheOrParse ( const Module
& module
,
773 SourceFile
* parentSourceFile
);
774 SourceFile
* RetrieveFromCache ( const File
& file
);
775 void CheckAutomaticDependencies ( bool verbose
);
776 void CheckAutomaticDependenciesForModule ( Module
& module
,
779 void GetModulesToCheck ( Module
& module
, std::vector
<const Module
*>& modules
);
780 void CheckAutomaticDependencies ( const Module
& module
,
782 void CheckAutomaticDependenciesForFile ( SourceFile
* sourceFile
);
783 void GetIncludeDirectories ( std::vector
<Include
*>& includes
,
784 const Module
& module
);
785 void GetModuleFiles ( const Module
& module
,
786 std::vector
<File
*>& files
) const;
788 void ParseFiles ( const Module
& module
);
789 void ParseFile ( const Module
& module
,
791 std::map
<std::string
, SourceFile
*> sourcefile_map
;
798 const Project
& project
;
799 const Module
* module
;
800 const XMLElement
& node
;
802 std::string nameoncd
;
804 Bootstrap ( const Project
& project
,
805 const Module
* module
,
806 const XMLElement
& bootstrapNode
);
810 bool IsSupportedModuleType ( ModuleType type
);
812 static std::string
ReplaceVariable ( const std::string
& name
,
813 const std::string
& value
,
818 class CDFile
: public XmlNode
821 FileLocation
*source
;
822 FileLocation
*target
;
824 CDFile ( const Project
& project
,
825 const XMLElement
& bootstrapNode
,
826 const std::string
& path
);
829 static std::string
ReplaceVariable ( const std::string
& name
,
830 const std::string
& value
,
835 class InstallFile
: public XmlNode
838 FileLocation
*source
;
839 FileLocation
*target
;
841 InstallFile ( const Project
& project
,
842 const XMLElement
& bootstrapNode
,
843 const std::string
& path
);
851 const XMLElement
& node
;
852 const Module
& module
;
853 const FileLocation
*file
;
856 const XMLElement
& node
,
857 const Module
& module
,
858 const FileLocation
*file
);
864 class StubbedComponent
867 const Module
* module
;
868 const XMLElement
& node
;
870 std::vector
<StubbedSymbol
*> symbols
;
872 StubbedComponent ( const Module
* module_
,
873 const XMLElement
& stubbedComponentNode
);
874 ~StubbedComponent ();
876 void ProcessXMLSubElement ( const XMLElement
& e
);
883 const XMLElement
& node
;
886 std::string strippedName
;
888 StubbedSymbol ( const XMLElement
& stubbedSymbolNode
);
892 std::string
StripSymbol ( std::string symbol
);
896 class CompilationUnit
901 CompilationUnit ( const File
* file
);
902 CompilationUnit ( const Project
* project
,
903 const Module
* module
,
904 const XMLElement
* node
);
907 bool IsGeneratedFile () const;
908 bool HasFileWithExtension ( const std::string
& extension
) const;
909 bool IsFirstFile () const;
910 const FileLocation
& GetFilename () const;
911 const std::string
& GetSwitches () const;
912 void AddFile ( const File
* file
);
913 const std::vector
<const File
*> GetFiles () const;
915 const Project
* project
;
916 const Module
* module
;
917 const XMLElement
* node
;
918 std::vector
<const File
*> files
;
919 FileLocation
*default_name
;
923 class CompilationUnitSupportCode
926 const Project
& project
;
928 CompilationUnitSupportCode ( const Project
& project
);
929 ~CompilationUnitSupportCode ();
930 void Generate ( bool verbose
);
932 void GenerateForModule ( Module
& module
,
934 std::string
GetCompilationUnitFilename ( Module
& module
,
935 CompilationUnit
& compilationUnit
);
936 void WriteCompilationUnitFile ( Module
& module
,
937 CompilationUnit
& compilationUnit
);
941 enum AutoRegisterType
948 class AutoRegister
: public XmlNode
951 const Module
* module
;
952 std::string infSection
;
953 AutoRegisterType type
;
954 AutoRegister ( const Project
& project_
,
955 const Module
* module_
,
956 const XMLElement
& node_
);
958 bool IsSupportedModuleType ( ModuleType type
);
959 AutoRegisterType
GetAutoRegisterType( const std::string
& type
);
964 class SysSetupGenerator
967 const Project
& project
;
968 SysSetupGenerator ( const Project
& project
);
969 ~SysSetupGenerator ();
972 std::string
GetDirectoryId ( const Module
& module
);
973 std::string
GetFlags ( const Module
& module
);
974 void Generate ( HINF inf
,
975 const Module
& module
);
980 InitializeEnvironment ();
983 Right ( const std::string
& s
, size_t n
);
986 Replace ( const std::string
& s
, const std::string
& find
, const std::string
& with
);
989 ChangeSeparator ( const std::string
& s
,
990 const char fromSeparator
,
991 const char toSeparator
);
994 FixSeparator ( const std::string
& s
);
997 FixSeparatorForSystemCommand ( const std::string
& s
);
1000 DosSeparator ( const std::string
& s
);
1004 const std::string
& filename
,
1005 const std::string
& newExtension
);
1009 const Project
& project
,
1010 const std::string
& location
,
1011 const std::string
& path
,
1012 const std::string
& att_value
);
1015 GetExtension ( const FileLocation
& file
);
1018 NormalizeFilename ( const std::string
& filename
);
1021 ToLower ( std::string filename
);
1023 #endif /* __RBUILD_H */