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,
319 CompilerTypeDontCare
,
327 DirectoryLocation directory
;
328 std::string relative_path
;
331 FileLocation ( const DirectoryLocation directory
,
332 const std::string
& relative_path
,
333 const std::string
& name
,
334 const XMLElement
*node
= NULL
);
336 FileLocation ( const FileLocation
& other
);
342 const Project
& project
;
343 const XMLElement
& node
;
344 std::string xmlbuildFile
;
347 std::string extension
;
348 std::string baseaddress
;
350 std::string buildtype
;
352 ImportLibrary
* importLibrary
;
355 bool underscoreSymbols
;
357 bool isDefaultEntryPoint
;
358 Bootstrap
* bootstrap
;
359 AutoRegister
* autoRegister
; // <autoregister> node
360 IfableData non_if_data
;
361 std::vector
<Invoke
*> invocations
;
362 std::vector
<Dependency
*> dependencies
;
363 std::vector
<CompilerFlag
*> compilerFlags
;
364 std::vector
<LinkerFlag
*> linkerFlags
;
365 std::vector
<StubbedComponent
*> stubbedComponents
;
366 LinkerScript
* linkerScript
;
371 std::string aliasedModuleName
;
375 FileLocation
*output
; // "path/foo.exe"
376 FileLocation
*dependency
; // "path/foo.exe" or "path/libfoo.a"
377 FileLocation
*install
;
379 Module ( const Project
& project
,
380 const XMLElement
& moduleNode
,
381 const std::string
& modulePath
);
383 ModuleType
GetModuleType ( const std::string
& location
,
384 const XMLAttribute
& attribute
);
385 bool HasImportLibrary () const;
387 std::string
GetPathWithPrefix ( const std::string
& prefix
) const; // "path/prefixfoo.exe"
388 std::string
GetPathToBaseDir() const; // "../" offset to rootdirectory
389 std::string
GetEntryPoint(bool leadingUnderscore
) const;
390 void GetTargets ( string_list
& ) const;
391 std::string
GetInvocationTarget ( const int index
) const;
392 bool HasFileWithExtension ( const IfableData
&, const std::string
& extension
) const;
393 void InvokeModule () const;
396 void SetImportLibrary ( ImportLibrary
* importLibrary
);
397 DirectoryLocation
GetTargetDirectoryTree () const;
398 std::string
GetDefaultModuleExtension () const;
399 std::string
GetDefaultModuleEntrypoint () const;
400 std::string
GetDefaultModuleBaseaddress () const;
401 std::string entrypoint
;
402 void ProcessXMLSubElement ( const XMLElement
& e
,
403 DirectoryLocation directory
,
404 const std::string
& relative_path
,
405 ParseContext
& parseContext
);
412 FileLocation
*directory
;
414 Include ( const Project
& project
,
415 const XMLElement
* includeNode
);
416 Include ( const Project
& project
,
417 const XMLElement
* includeNode
,
418 const Module
* module
);
419 Include ( const Project
& project
,
420 DirectoryLocation directory
,
421 const std::string
& relative_path
);
425 const Project
& project
;
426 const XMLElement
* node
;
427 const Module
* module
;
428 DirectoryLocation
GetDefaultDirectoryTree ( const Module
* module
) const;
435 const Project
& project
;
436 const Module
* module
;
437 const XMLElement
* node
;
443 Define ( const Project
& project
,
444 const XMLElement
& defineNode
);
445 Define ( const Project
& project
,
446 const Module
* module
,
447 const XMLElement
& defineNode
);
448 Define ( const Project
& project
,
449 const Module
* module
,
450 const std::string
& name_
,
451 const std::string
& backend_
= "" );
464 std::string switches
;
465 bool isPreCompiledHeader
;
467 File ( DirectoryLocation directory
,
468 const std::string
& relative_path
,
469 const std::string
& name
,
471 const std::string
& _switches
,
472 bool _isPreCompiledHeader
);
475 std::string
GetFullPath () const;
481 const XMLElement
*node
;
483 const Module
& module
;
485 const Module
* importedModule
;
487 Library ( const XMLElement
& _node
,
488 const Module
& _module
,
489 const std::string
& _name
);
490 Library ( const Module
& _module
,
491 const std::string
& _name
);
500 const XMLElement
& node
;
501 const Module
& module
;
502 const Module
* invokeModule
;
503 std::vector
<InvokeFile
*> input
;
504 std::vector
<InvokeFile
*> output
;
506 Invoke ( const XMLElement
& _node
,
507 const Module
& _module
);
510 void GetTargets ( string_list
& targets
) const;
511 std::string
GetParameters () const;
513 void ProcessXMLSubElement ( const XMLElement
& e
);
514 void ProcessXMLSubElementInput ( const XMLElement
& e
);
515 void ProcessXMLSubElementOutput ( const XMLElement
& e
);
522 const XMLElement
& node
;
524 std::string switches
;
526 InvokeFile ( const XMLElement
& _node
,
527 const std::string
& _name
);
536 const XMLElement
& node
;
537 const Module
& module
;
538 const Module
* dependencyModule
;
540 Dependency ( const XMLElement
& _node
,
541 const Module
& _module
);
549 const XMLElement
& node
;
550 const Module
& module
;
552 std::string description
;
554 std::string copyright
;
559 Metadata ( const XMLElement
& _node
,
560 const Module
& _module
);
565 class ImportLibrary
: public XmlNode
568 const Module
& module
;
570 FileLocation
*source
;
572 ImportLibrary ( const Project
& project
,
573 const XMLElement
& node
,
574 const Module
& module
);
582 const XMLElement
& node
;
583 const Project
& project
;
584 const Module
* module
;
586 std::string property
, value
;
589 If ( const XMLElement
& node_
,
590 const Project
& project_
,
591 const Module
* module_
,
592 const bool negated_
= false );
602 const Project
& project
;
603 const Module
* module
;
604 const XMLElement
& node
;
606 CompilerType compiler
;
608 CompilerFlag ( const Project
& project
,
609 const XMLElement
& compilerFlagNode
);
610 CompilerFlag ( const Project
& project
,
611 const Module
* module
,
612 const XMLElement
& compilerFlagNode
);
623 const Project
& project
;
624 const Module
* module
;
625 const XMLElement
& node
;
628 LinkerFlag ( const Project
& project
,
629 const XMLElement
& linkerFlagNode
);
630 LinkerFlag ( const Project
& project
,
631 const Module
* module
,
632 const XMLElement
& linkerFlagNode
);
643 const XMLElement
& node
;
644 const Module
& module
;
645 const FileLocation
*file
;
647 LinkerScript ( const XMLElement
& node
,
648 const Module
& module
,
649 const FileLocation
*file
);
658 const Project
& project
;
659 const Module
* module
;
660 std::string name
, value
;
662 Property ( const XMLElement
& node_
,
663 const Project
& project_
,
664 const Module
* module_
);
666 Property ( const Project
& project_
,
667 const Module
* module_
,
668 const std::string
& name_
,
669 const std::string
& value_
);
675 class TestSupportCode
678 const Project
& project
;
680 TestSupportCode ( const Project
& project
);
682 void GenerateTestSupportCode ( bool verbose
);
684 bool IsTestModule ( const Module
& module
);
685 void GenerateTestSupportCodeForModule ( Module
& module
,
687 std::string
GetHooksFilename ( Module
& module
);
688 char* WriteStubbedSymbolToHooksFile ( char* buffer
,
689 const StubbedComponent
& component
,
690 const StubbedSymbol
& symbol
);
691 char* WriteStubbedComponentToHooksFile ( char* buffer
,
692 const StubbedComponent
& component
);
693 void WriteHooksFile ( Module
& module
);
694 std::string
GetStubsFilename ( Module
& module
);
695 char* WriteStubbedSymbolToStubsFile ( char* buffer
,
696 const StubbedComponent
& component
,
697 const StubbedSymbol
& symbol
,
699 char* WriteStubbedComponentToStubsFile ( char* buffer
,
700 const StubbedComponent
& component
,
702 void WriteStubsFile ( Module
& module
);
703 std::string
GetStartupFilename ( Module
& module
);
704 bool IsUnknownCharacter ( char ch
);
705 std::string
GetTestDispatcherName ( std::string filename
);
706 bool IsTestFile ( std::string
& filename
) const;
707 void GetSourceFilenames ( string_list
& list
,
708 Module
& module
) const;
709 char* WriteTestDispatcherPrototypesToStartupFile ( char* buffer
,
711 char* WriteRegisterTestsFunctionToStartupFile ( char* buffer
,
713 void WriteStartupFile ( Module
& module
);
720 SourceFile ( AutomaticDependency
* automaticDependency
,
721 const Module
& module
,
723 SourceFile
* parent
);
725 std::vector
<SourceFile
*> files
; /* List of files included in this file */
727 AutomaticDependency
* automaticDependency
;
728 const Module
& module
;
729 std::vector
<SourceFile
*> parents
; /* List of files, this file is included from */
730 time_t lastWriteTime
;
731 time_t youngestLastWriteTime
; /* Youngest last write time of this file and all children */
732 SourceFile
* youngestFile
;
736 void SkipWhitespace ();
737 bool ReadInclude ( std::string
& filename
,
738 bool& searchCurrentDirectory
,
740 bool IsIncludedFrom ( const File
& file
);
741 SourceFile
* ParseFile(const File
& file
);
742 bool CanProcessFile ( const File
& file
);
743 bool IsParentOf ( const SourceFile
* parent
,
744 const SourceFile
* child
);
751 class AutomaticDependency
753 friend class SourceFileTest
;
755 const Project
& project
;
757 AutomaticDependency ( const Project
& project
);
758 ~AutomaticDependency ();
759 bool LocateIncludedFile ( const FileLocation
& directory
,
760 const std::string
& includedFilename
);
761 bool LocateIncludedFile ( SourceFile
* sourceFile
,
762 const Module
& module
,
763 const std::string
& includedFilename
,
764 bool searchCurrentDirectory
,
766 File
& resolvedFile
);
767 SourceFile
* RetrieveFromCacheOrParse ( const Module
& module
,
769 SourceFile
* parentSourceFile
);
770 SourceFile
* RetrieveFromCache ( const File
& file
);
771 void CheckAutomaticDependencies ( bool verbose
);
772 void CheckAutomaticDependenciesForModule ( Module
& module
,
775 void GetModulesToCheck ( Module
& module
, std::vector
<const Module
*>& modules
);
776 void CheckAutomaticDependencies ( const Module
& module
,
778 void CheckAutomaticDependenciesForFile ( SourceFile
* sourceFile
);
779 void GetIncludeDirectories ( std::vector
<Include
*>& includes
,
780 const Module
& module
);
781 void GetModuleFiles ( const Module
& module
,
782 std::vector
<File
*>& files
) const;
784 void ParseFiles ( const Module
& module
);
785 void ParseFile ( const Module
& module
,
787 std::map
<std::string
, SourceFile
*> sourcefile_map
;
794 const Project
& project
;
795 const Module
* module
;
796 const XMLElement
& node
;
798 std::string nameoncd
;
800 Bootstrap ( const Project
& project
,
801 const Module
* module
,
802 const XMLElement
& bootstrapNode
);
806 bool IsSupportedModuleType ( ModuleType type
);
808 static std::string
ReplaceVariable ( const std::string
& name
,
809 const std::string
& value
,
814 class CDFile
: public XmlNode
817 FileLocation
*source
;
818 FileLocation
*target
;
820 CDFile ( const Project
& project
,
821 const XMLElement
& bootstrapNode
,
822 const std::string
& path
);
825 static std::string
ReplaceVariable ( const std::string
& name
,
826 const std::string
& value
,
831 class InstallFile
: public XmlNode
834 FileLocation
*source
;
835 FileLocation
*target
;
837 InstallFile ( const Project
& project
,
838 const XMLElement
& bootstrapNode
,
839 const std::string
& path
);
847 const XMLElement
& node
;
848 const Module
& module
;
849 const FileLocation
*file
;
852 const XMLElement
& node
,
853 const Module
& module
,
854 const FileLocation
*file
);
860 class StubbedComponent
863 const Module
* module
;
864 const XMLElement
& node
;
866 std::vector
<StubbedSymbol
*> symbols
;
868 StubbedComponent ( const Module
* module_
,
869 const XMLElement
& stubbedComponentNode
);
870 ~StubbedComponent ();
872 void ProcessXMLSubElement ( const XMLElement
& e
);
879 const XMLElement
& node
;
882 std::string strippedName
;
884 StubbedSymbol ( const XMLElement
& stubbedSymbolNode
);
888 std::string
StripSymbol ( std::string symbol
);
892 class CompilationUnit
897 CompilationUnit ( const File
* file
);
898 CompilationUnit ( const Project
* project
,
899 const Module
* module
,
900 const XMLElement
* node
);
903 bool IsGeneratedFile () const;
904 bool HasFileWithExtension ( const std::string
& extension
) const;
905 bool IsFirstFile () const;
906 const FileLocation
& GetFilename () const;
907 const std::string
& GetSwitches () const;
908 void AddFile ( const File
* file
);
909 const std::vector
<const File
*> GetFiles () const;
911 const Project
* project
;
912 const Module
* module
;
913 const XMLElement
* node
;
914 std::vector
<const File
*> files
;
915 FileLocation
*default_name
;
919 class CompilationUnitSupportCode
922 const Project
& project
;
924 CompilationUnitSupportCode ( const Project
& project
);
925 ~CompilationUnitSupportCode ();
926 void Generate ( bool verbose
);
928 void GenerateForModule ( Module
& module
,
930 std::string
GetCompilationUnitFilename ( Module
& module
,
931 CompilationUnit
& compilationUnit
);
932 void WriteCompilationUnitFile ( Module
& module
,
933 CompilationUnit
& compilationUnit
);
937 enum AutoRegisterType
944 class AutoRegister
: public XmlNode
947 const Module
* module
;
948 std::string infSection
;
949 AutoRegisterType type
;
950 AutoRegister ( const Project
& project_
,
951 const Module
* module_
,
952 const XMLElement
& node_
);
954 bool IsSupportedModuleType ( ModuleType type
);
955 AutoRegisterType
GetAutoRegisterType( const std::string
& type
);
960 class SysSetupGenerator
963 const Project
& project
;
964 SysSetupGenerator ( const Project
& project
);
965 ~SysSetupGenerator ();
968 std::string
GetDirectoryId ( const Module
& module
);
969 std::string
GetFlags ( const Module
& module
);
970 void Generate ( HINF inf
,
971 const Module
& module
);
976 InitializeEnvironment ();
979 Right ( const std::string
& s
, size_t n
);
982 Replace ( const std::string
& s
, const std::string
& find
, const std::string
& with
);
985 ChangeSeparator ( const std::string
& s
,
986 const char fromSeparator
,
987 const char toSeparator
);
990 FixSeparator ( const std::string
& s
);
993 FixSeparatorForSystemCommand ( const std::string
& s
);
996 DosSeparator ( const std::string
& s
);
1000 const std::string
& filename
,
1001 const std::string
& newExtension
);
1005 const Project
& project
,
1006 const std::string
& location
,
1007 const std::string
& path
,
1008 const std::string
& att_value
);
1011 GetExtension ( const FileLocation
& file
);
1014 NormalizeFilename ( const std::string
& filename
);
1017 ToLower ( std::string filename
);
1019 #endif /* __RBUILD_H */