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
;
663 Property ( const XMLElement
& node_
,
664 const Project
& project_
,
665 const Module
* module_
);
667 Property ( const Project
& project_
,
668 const Module
* module_
,
669 const std::string
& name_
,
670 const std::string
& value_
);
676 class TestSupportCode
679 const Project
& project
;
681 TestSupportCode ( const Project
& project
);
683 void GenerateTestSupportCode ( bool verbose
);
685 bool IsTestModule ( const Module
& module
);
686 void GenerateTestSupportCodeForModule ( Module
& module
,
688 std::string
GetHooksFilename ( Module
& module
);
689 char* WriteStubbedSymbolToHooksFile ( char* buffer
,
690 const StubbedComponent
& component
,
691 const StubbedSymbol
& symbol
);
692 char* WriteStubbedComponentToHooksFile ( char* buffer
,
693 const StubbedComponent
& component
);
694 void WriteHooksFile ( Module
& module
);
695 std::string
GetStubsFilename ( Module
& module
);
696 char* WriteStubbedSymbolToStubsFile ( char* buffer
,
697 const StubbedComponent
& component
,
698 const StubbedSymbol
& symbol
,
700 char* WriteStubbedComponentToStubsFile ( char* buffer
,
701 const StubbedComponent
& component
,
703 void WriteStubsFile ( Module
& module
);
704 std::string
GetStartupFilename ( Module
& module
);
705 bool IsUnknownCharacter ( char ch
);
706 std::string
GetTestDispatcherName ( std::string filename
);
707 bool IsTestFile ( std::string
& filename
) const;
708 void GetSourceFilenames ( string_list
& list
,
709 Module
& module
) const;
710 char* WriteTestDispatcherPrototypesToStartupFile ( char* buffer
,
712 char* WriteRegisterTestsFunctionToStartupFile ( char* buffer
,
714 void WriteStartupFile ( Module
& module
);
721 SourceFile ( AutomaticDependency
* automaticDependency
,
722 const Module
& module
,
724 SourceFile
* parent
);
726 std::vector
<SourceFile
*> files
; /* List of files included in this file */
728 AutomaticDependency
* automaticDependency
;
729 const Module
& module
;
730 std::vector
<SourceFile
*> parents
; /* List of files, this file is included from */
731 time_t lastWriteTime
;
732 time_t youngestLastWriteTime
; /* Youngest last write time of this file and all children */
733 SourceFile
* youngestFile
;
737 void SkipWhitespace ();
738 bool ReadInclude ( std::string
& filename
,
739 bool& searchCurrentDirectory
,
741 bool IsIncludedFrom ( const File
& file
);
742 SourceFile
* ParseFile(const File
& file
);
743 bool CanProcessFile ( const File
& file
);
744 bool IsParentOf ( const SourceFile
* parent
,
745 const SourceFile
* child
);
752 class AutomaticDependency
754 friend class SourceFileTest
;
756 const Project
& project
;
758 AutomaticDependency ( const Project
& project
);
759 ~AutomaticDependency ();
760 bool LocateIncludedFile ( const FileLocation
& directory
,
761 const std::string
& includedFilename
);
762 bool LocateIncludedFile ( SourceFile
* sourceFile
,
763 const Module
& module
,
764 const std::string
& includedFilename
,
765 bool searchCurrentDirectory
,
767 File
& resolvedFile
);
768 SourceFile
* RetrieveFromCacheOrParse ( const Module
& module
,
770 SourceFile
* parentSourceFile
);
771 SourceFile
* RetrieveFromCache ( const File
& file
);
772 void CheckAutomaticDependencies ( bool verbose
);
773 void CheckAutomaticDependenciesForModule ( Module
& module
,
776 void GetModulesToCheck ( Module
& module
, std::vector
<const Module
*>& modules
);
777 void CheckAutomaticDependencies ( const Module
& module
,
779 void CheckAutomaticDependenciesForFile ( SourceFile
* sourceFile
);
780 void GetIncludeDirectories ( std::vector
<Include
*>& includes
,
781 const Module
& module
);
782 void GetModuleFiles ( const Module
& module
,
783 std::vector
<File
*>& files
) const;
785 void ParseFiles ( const Module
& module
);
786 void ParseFile ( const Module
& module
,
788 std::map
<std::string
, SourceFile
*> sourcefile_map
;
795 const Project
& project
;
796 const Module
* module
;
797 const XMLElement
& node
;
799 std::string nameoncd
;
801 Bootstrap ( const Project
& project
,
802 const Module
* module
,
803 const XMLElement
& bootstrapNode
);
807 bool IsSupportedModuleType ( ModuleType type
);
809 static std::string
ReplaceVariable ( const std::string
& name
,
810 const std::string
& value
,
815 class CDFile
: public XmlNode
818 FileLocation
*source
;
819 FileLocation
*target
;
821 CDFile ( const Project
& project
,
822 const XMLElement
& bootstrapNode
,
823 const std::string
& path
);
826 static std::string
ReplaceVariable ( const std::string
& name
,
827 const std::string
& value
,
832 class InstallFile
: public XmlNode
835 FileLocation
*source
;
836 FileLocation
*target
;
838 InstallFile ( const Project
& project
,
839 const XMLElement
& bootstrapNode
,
840 const std::string
& path
);
848 const XMLElement
& node
;
849 const Module
& module
;
850 const FileLocation
*file
;
853 const XMLElement
& node
,
854 const Module
& module
,
855 const FileLocation
*file
);
861 class StubbedComponent
864 const Module
* module
;
865 const XMLElement
& node
;
867 std::vector
<StubbedSymbol
*> symbols
;
869 StubbedComponent ( const Module
* module_
,
870 const XMLElement
& stubbedComponentNode
);
871 ~StubbedComponent ();
873 void ProcessXMLSubElement ( const XMLElement
& e
);
880 const XMLElement
& node
;
883 std::string strippedName
;
885 StubbedSymbol ( const XMLElement
& stubbedSymbolNode
);
889 std::string
StripSymbol ( std::string symbol
);
893 class CompilationUnit
898 CompilationUnit ( const File
* file
);
899 CompilationUnit ( const Project
* project
,
900 const Module
* module
,
901 const XMLElement
* node
);
904 bool IsGeneratedFile () const;
905 bool HasFileWithExtension ( const std::string
& extension
) const;
906 bool IsFirstFile () const;
907 const FileLocation
& GetFilename () const;
908 const std::string
& GetSwitches () const;
909 void AddFile ( const File
* file
);
910 const std::vector
<const File
*> GetFiles () const;
912 const Project
* project
;
913 const Module
* module
;
914 const XMLElement
* node
;
915 std::vector
<const File
*> files
;
916 FileLocation
*default_name
;
920 class CompilationUnitSupportCode
923 const Project
& project
;
925 CompilationUnitSupportCode ( const Project
& project
);
926 ~CompilationUnitSupportCode ();
927 void Generate ( bool verbose
);
929 void GenerateForModule ( Module
& module
,
931 std::string
GetCompilationUnitFilename ( Module
& module
,
932 CompilationUnit
& compilationUnit
);
933 void WriteCompilationUnitFile ( Module
& module
,
934 CompilationUnit
& compilationUnit
);
938 enum AutoRegisterType
945 class AutoRegister
: public XmlNode
948 const Module
* module
;
949 std::string infSection
;
950 AutoRegisterType type
;
951 AutoRegister ( const Project
& project_
,
952 const Module
* module_
,
953 const XMLElement
& node_
);
955 bool IsSupportedModuleType ( ModuleType type
);
956 AutoRegisterType
GetAutoRegisterType( const std::string
& type
);
961 class SysSetupGenerator
964 const Project
& project
;
965 SysSetupGenerator ( const Project
& project
);
966 ~SysSetupGenerator ();
969 std::string
GetDirectoryId ( const Module
& module
);
970 std::string
GetFlags ( const Module
& module
);
971 void Generate ( HINF inf
,
972 const Module
& module
);
977 InitializeEnvironment ();
980 Right ( const std::string
& s
, size_t n
);
983 Replace ( const std::string
& s
, const std::string
& find
, const std::string
& with
);
986 ChangeSeparator ( const std::string
& s
,
987 const char fromSeparator
,
988 const char toSeparator
);
991 FixSeparator ( const std::string
& s
);
994 FixSeparatorForSystemCommand ( const std::string
& s
);
997 DosSeparator ( const std::string
& s
);
1001 const std::string
& filename
,
1002 const std::string
& newExtension
);
1006 const Project
& project
,
1007 const std::string
& location
,
1008 const std::string
& path
,
1009 const std::string
& att_value
);
1012 GetExtension ( const FileLocation
& file
);
1015 NormalizeFilename ( const std::string
& filename
);
1018 ToLower ( std::string filename
);
1020 #endif /* __RBUILD_H */