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
);
137 void Add ( const char* subdir
);
138 void GenerateTree ( DirectoryLocation root
,
140 void CreateRule ( FILE* f
,
141 const std::string
& parent
);
143 bool mkdir_p ( const char* path
);
144 bool CreateDirectory ( const std::string
& path
);
145 std::string
EscapeSpaces ( const std::string
& path
);
146 void GenerateTree ( const std::string
& parent
,
158 bool AutomaticDependencies
;
159 bool CheckDependenciesForModuleOnly
;
160 bool CompilationUnitsEnabled
;
161 std::string CheckDependenciesForModuleOnlyModule
;
162 std::string VSProjectVersion
;
163 std::string VSConfigurationType
;
164 bool UseVSVersionInPath
;
165 bool UseConfigurationInPath
;
166 bool MakeHandlesInstallDirectories
;
167 bool GenerateProxyMakefilesInSourceTree
;
174 static std::string
GetVariable ( const std::string
& name
);
175 static std::string
GetArch ();
176 static std::string
GetIntermediatePath ();
177 static std::string
GetOutputPath ();
178 static std::string
GetCdOutputPath ();
179 static std::string
GetInstallPath ();
180 static std::string
GetAutomakeFile ( const std::string
& defaultFile
);
181 static std::string
GetEnvironmentVariablePathOrDefault ( const std::string
& name
,
182 const std::string
& defaultValue
);
186 class FileSupportCode
189 static void WriteIfChanged ( char* outbuf
,
190 const std::string
& filename
,
191 bool ignoreError
= false );
199 CompilationUnit
* compilationUnit
;
207 std::vector
<CompilationUnit
*> compilationUnits
;
208 std::vector
<File
*> files
;
209 std::vector
<Include
*> includes
;
210 std::vector
<Define
*> defines
;
211 std::vector
<Library
*> libraries
;
212 std::vector
<Property
*> properties
;
213 std::vector
<Module
*> modules
;
214 std::vector
<CompilerFlag
*> compilerFlags
;
215 std::vector
<If
*> ifs
;
216 int asmFiles
; // number of .asm files in compilationUnits
221 void ExtractModules( std::vector
<Module
*> &modules
);
227 XMLElement
*node
, *head
;
230 const Configuration
& configuration
;
232 std::string makefile
;
233 XMLIncludes xmlbuildfiles
;
234 std::vector
<LinkerFlag
*> linkerFlags
;
235 std::vector
<CDFile
*> cdfiles
;
236 std::vector
<InstallFile
*> installfiles
;
237 std::vector
<Module
*> modules
;
238 IfableData non_if_data
;
240 Project ( const Configuration
& configuration
,
241 const std::string
& filename
,
242 const std::map
<std::string
, std::string
>* properties
= NULL
);
244 void SetBackend ( Backend
* backend
) { _backend
= backend
; }
245 Backend
& GetBackend() { return *_backend
; }
246 void WriteConfigurationFile ();
247 void ExecuteInvocations ();
249 void ProcessXML ( const std::string
& path
);
250 Module
* LocateModule ( const std::string
& name
);
251 const Module
* LocateModule ( const std::string
& name
) const;
252 const std::string
& GetProjectFilename () const;
253 std::string
ResolveProperties ( const std::string
& s
) const;
255 std::string
ResolveNextProperty ( const std::string
& s
) const;
256 const Property
* LookupProperty ( const std::string
& name
) const;
257 void SetConfigurationOption ( char* s
,
259 std::string
* alternativeName
);
260 void SetConfigurationOption ( char* s
,
263 void ProcessXMLSubElement ( const XMLElement
& e
,
264 const std::string
& path
,
265 ParseContext
& parseContext
);
267 // disable copy semantics
268 Project ( const Project
& );
269 Project
& operator = ( const Project
& );
280 KernelModeDriver
= 5,
300 EmbeddedTypeLib
= 26,
315 DirectoryLocation directory
;
316 std::string relative_path
;
319 FileLocation ( const DirectoryLocation directory
,
320 const std::string
& relative_path
,
321 const std::string
& name
,
322 const XMLElement
*node
= NULL
);
324 FileLocation ( const FileLocation
& other
);
330 const Project
& project
;
331 const XMLElement
& node
;
332 std::string xmlbuildFile
;
335 std::string extension
;
336 std::string baseaddress
;
338 std::string buildtype
;
340 ImportLibrary
* importLibrary
;
343 bool underscoreSymbols
;
345 bool isDefaultEntryPoint
;
346 Bootstrap
* bootstrap
;
347 AutoRegister
* autoRegister
; // <autoregister> node
348 IfableData non_if_data
;
349 std::vector
<Invoke
*> invocations
;
350 std::vector
<Dependency
*> dependencies
;
351 std::vector
<CompilerFlag
*> compilerFlags
;
352 std::vector
<LinkerFlag
*> linkerFlags
;
353 std::vector
<StubbedComponent
*> stubbedComponents
;
354 LinkerScript
* linkerScript
;
359 std::string aliasedModuleName
;
363 FileLocation
*output
; // "path/foo.exe"
364 FileLocation
*dependency
; // "path/foo.exe" or "path/libfoo.a"
365 FileLocation
*install
;
367 Module ( const Project
& project
,
368 const XMLElement
& moduleNode
,
369 const std::string
& modulePath
);
371 ModuleType
GetModuleType ( const std::string
& location
,
372 const XMLAttribute
& attribute
);
373 bool HasImportLibrary () const;
375 std::string
GetPathWithPrefix ( const std::string
& prefix
) const; // "path/prefixfoo.exe"
376 std::string
GetPathToBaseDir() const; // "../" offset to rootdirectory
377 std::string
GetEntryPoint(bool leadingUnderscore
) const;
378 void GetTargets ( string_list
& ) const;
379 std::string
GetInvocationTarget ( const int index
) const;
380 bool HasFileWithExtension ( const IfableData
&, const std::string
& extension
) const;
381 void InvokeModule () const;
384 void SetImportLibrary ( ImportLibrary
* importLibrary
);
385 DirectoryLocation
GetTargetDirectoryTree () const;
386 std::string
GetDefaultModuleExtension () const;
387 std::string
GetDefaultModuleEntrypoint () const;
388 std::string
GetDefaultModuleBaseaddress () const;
389 std::string entrypoint
;
390 void ProcessXMLSubElement ( const XMLElement
& e
,
391 DirectoryLocation directory
,
392 const std::string
& relative_path
,
393 ParseContext
& parseContext
);
400 FileLocation
*directory
;
402 Include ( const Project
& project
,
403 const XMLElement
* includeNode
);
404 Include ( const Project
& project
,
405 const XMLElement
* includeNode
,
406 const Module
* module
);
407 Include ( const Project
& project
,
408 DirectoryLocation directory
,
409 const std::string
& relative_path
);
413 const Project
& project
;
414 const XMLElement
* node
;
415 const Module
* module
;
416 DirectoryLocation
GetDefaultDirectoryTree ( const Module
* module
) const;
423 const Project
& project
;
424 const Module
* module
;
425 const XMLElement
* node
;
431 Define ( const Project
& project
,
432 const XMLElement
& defineNode
);
433 Define ( const Project
& project
,
434 const Module
* module
,
435 const XMLElement
& defineNode
);
436 Define ( const Project
& project
,
437 const Module
* module
,
438 const std::string
& name_
,
439 const std::string
& backend_
= "" );
452 std::string switches
;
453 bool isPreCompiledHeader
;
455 File ( DirectoryLocation directory
,
456 const std::string
& relative_path
,
457 const std::string
& name
,
459 const std::string
& _switches
,
460 bool _isPreCompiledHeader
);
463 std::string
GetFullPath () const;
469 const XMLElement
*node
;
471 const Module
& module
;
473 const Module
* importedModule
;
475 Library ( const XMLElement
& _node
,
476 const Module
& _module
,
477 const std::string
& _name
);
478 Library ( const Module
& _module
,
479 const std::string
& _name
);
488 const XMLElement
& node
;
489 const Module
& module
;
490 const Module
* invokeModule
;
491 std::vector
<InvokeFile
*> input
;
492 std::vector
<InvokeFile
*> output
;
494 Invoke ( const XMLElement
& _node
,
495 const Module
& _module
);
498 void GetTargets ( string_list
& targets
) const;
499 std::string
GetParameters () const;
501 void ProcessXMLSubElement ( const XMLElement
& e
);
502 void ProcessXMLSubElementInput ( const XMLElement
& e
);
503 void ProcessXMLSubElementOutput ( const XMLElement
& e
);
510 const XMLElement
& node
;
512 std::string switches
;
514 InvokeFile ( const XMLElement
& _node
,
515 const std::string
& _name
);
524 const XMLElement
& node
;
525 const Module
& module
;
526 const Module
* dependencyModule
;
528 Dependency ( const XMLElement
& _node
,
529 const Module
& _module
);
537 const XMLElement
& node
;
538 const Module
& module
;
540 std::string description
;
542 std::string copyright
;
547 Metadata ( const XMLElement
& _node
,
548 const Module
& _module
);
553 class ImportLibrary
: public XmlNode
556 const Module
& module
;
558 FileLocation
*source
;
560 ImportLibrary ( const Project
& project
,
561 const XMLElement
& node
,
562 const Module
& module
);
569 const XMLElement
& node
;
570 const Project
& project
;
571 const Module
* module
;
573 std::string property
, value
;
576 If ( const XMLElement
& node_
,
577 const Project
& project_
,
578 const Module
* module_
,
579 const bool negated_
= false );
589 const Project
& project
;
590 const Module
* module
;
591 const XMLElement
& node
;
594 CompilerFlag ( const Project
& project
,
595 const XMLElement
& compilerFlagNode
);
596 CompilerFlag ( const Project
& project
,
597 const Module
* module
,
598 const XMLElement
& compilerFlagNode
);
609 const Project
& project
;
610 const Module
* module
;
611 const XMLElement
& node
;
614 LinkerFlag ( const Project
& project
,
615 const XMLElement
& linkerFlagNode
);
616 LinkerFlag ( const Project
& project
,
617 const Module
* module
,
618 const XMLElement
& linkerFlagNode
);
629 const XMLElement
& node
;
630 const Module
& module
;
633 LinkerScript ( const XMLElement
& node
,
634 const Module
& module
,
635 const FileLocation
& file
);
644 const Project
& project
;
645 const Module
* module
;
646 std::string name
, value
;
648 Property ( const XMLElement
& node_
,
649 const Project
& project_
,
650 const Module
* module_
);
652 Property ( const Project
& project_
,
653 const Module
* module_
,
654 const std::string
& name_
,
655 const std::string
& value_
);
661 class TestSupportCode
664 const Project
& project
;
666 TestSupportCode ( const Project
& project
);
668 void GenerateTestSupportCode ( bool verbose
);
670 bool IsTestModule ( const Module
& module
);
671 void GenerateTestSupportCodeForModule ( Module
& module
,
673 std::string
GetHooksFilename ( Module
& module
);
674 char* WriteStubbedSymbolToHooksFile ( char* buffer
,
675 const StubbedComponent
& component
,
676 const StubbedSymbol
& symbol
);
677 char* WriteStubbedComponentToHooksFile ( char* buffer
,
678 const StubbedComponent
& component
);
679 void WriteHooksFile ( Module
& module
);
680 std::string
GetStubsFilename ( Module
& module
);
681 char* WriteStubbedSymbolToStubsFile ( char* buffer
,
682 const StubbedComponent
& component
,
683 const StubbedSymbol
& symbol
,
685 char* WriteStubbedComponentToStubsFile ( char* buffer
,
686 const StubbedComponent
& component
,
688 void WriteStubsFile ( Module
& module
);
689 std::string
GetStartupFilename ( Module
& module
);
690 bool IsUnknownCharacter ( char ch
);
691 std::string
GetTestDispatcherName ( std::string filename
);
692 bool IsTestFile ( std::string
& filename
) const;
693 void GetSourceFilenames ( string_list
& list
,
694 Module
& module
) const;
695 char* WriteTestDispatcherPrototypesToStartupFile ( char* buffer
,
697 char* WriteRegisterTestsFunctionToStartupFile ( char* buffer
,
699 void WriteStartupFile ( Module
& module
);
706 SourceFile ( AutomaticDependency
* automaticDependency
,
707 const Module
& module
,
709 SourceFile
* parent
);
711 std::vector
<SourceFile
*> files
; /* List of files included in this file */
713 AutomaticDependency
* automaticDependency
;
714 const Module
& module
;
715 std::vector
<SourceFile
*> parents
; /* List of files, this file is included from */
716 time_t lastWriteTime
;
717 time_t youngestLastWriteTime
; /* Youngest last write time of this file and all children */
718 SourceFile
* youngestFile
;
722 void SkipWhitespace ();
723 bool ReadInclude ( std::string
& filename
,
724 bool& searchCurrentDirectory
,
726 bool IsIncludedFrom ( const File
& file
);
727 SourceFile
* ParseFile(const File
& file
);
728 bool CanProcessFile ( const File
& file
);
729 bool IsParentOf ( const SourceFile
* parent
,
730 const SourceFile
* child
);
737 class AutomaticDependency
739 friend class SourceFileTest
;
741 const Project
& project
;
743 AutomaticDependency ( const Project
& project
);
744 ~AutomaticDependency ();
745 bool LocateIncludedFile ( const FileLocation
& directory
,
746 const std::string
& includedFilename
);
747 bool LocateIncludedFile ( SourceFile
* sourceFile
,
748 const Module
& module
,
749 const std::string
& includedFilename
,
750 bool searchCurrentDirectory
,
752 File
& resolvedFile
);
753 SourceFile
* RetrieveFromCacheOrParse ( const Module
& module
,
755 SourceFile
* parentSourceFile
);
756 SourceFile
* RetrieveFromCache ( const File
& file
);
757 void CheckAutomaticDependencies ( bool verbose
);
758 void CheckAutomaticDependenciesForModule ( Module
& module
,
761 void GetModulesToCheck ( Module
& module
, std::vector
<const Module
*>& modules
);
762 void CheckAutomaticDependencies ( const Module
& module
,
764 void CheckAutomaticDependenciesForFile ( SourceFile
* sourceFile
);
765 void GetIncludeDirectories ( std::vector
<Include
*>& includes
,
766 const Module
& module
);
767 void GetModuleFiles ( const Module
& module
,
768 std::vector
<File
*>& files
) const;
770 void ParseFiles ( const Module
& module
);
771 void ParseFile ( const Module
& module
,
773 std::map
<std::string
, SourceFile
*> sourcefile_map
;
780 const Project
& project
;
781 const Module
* module
;
782 const XMLElement
& node
;
784 std::string nameoncd
;
786 Bootstrap ( const Project
& project
,
787 const Module
* module
,
788 const XMLElement
& bootstrapNode
);
792 bool IsSupportedModuleType ( ModuleType type
);
794 static std::string
ReplaceVariable ( const std::string
& name
,
795 const std::string
& value
,
800 class CDFile
: public XmlNode
803 FileLocation
*source
;
804 FileLocation
*target
;
806 CDFile ( const Project
& project
,
807 const XMLElement
& bootstrapNode
,
808 const std::string
& path
);
810 static std::string
ReplaceVariable ( const std::string
& name
,
811 const std::string
& value
,
816 class InstallFile
: public XmlNode
819 FileLocation
*source
;
820 FileLocation
*target
;
822 InstallFile ( const Project
& project
,
823 const XMLElement
& bootstrapNode
,
824 const std::string
& path
);
831 const XMLElement
& node
;
832 const Module
& module
;
836 const XMLElement
& node
,
837 const Module
& module
,
838 const FileLocation
& file
);
843 class StubbedComponent
846 const Module
* module
;
847 const XMLElement
& node
;
849 std::vector
<StubbedSymbol
*> symbols
;
851 StubbedComponent ( const Module
* module_
,
852 const XMLElement
& stubbedComponentNode
);
853 ~StubbedComponent ();
855 void ProcessXMLSubElement ( const XMLElement
& e
);
862 const XMLElement
& node
;
865 std::string strippedName
;
867 StubbedSymbol ( const XMLElement
& stubbedSymbolNode
);
871 std::string
StripSymbol ( std::string symbol
);
875 class CompilationUnit
880 CompilationUnit ( const File
* file
);
881 CompilationUnit ( const Project
* project
,
882 const Module
* module
,
883 const XMLElement
* node
);
886 bool IsGeneratedFile () const;
887 bool HasFileWithExtension ( const std::string
& extension
) const;
888 bool IsFirstFile () const;
889 const FileLocation
& GetFilename () const;
890 const std::string
& GetSwitches () const;
891 void AddFile ( const File
* file
);
892 const std::vector
<const File
*> GetFiles () const;
894 const Project
* project
;
895 const Module
* module
;
896 const XMLElement
* node
;
897 std::vector
<const File
*> files
;
898 FileLocation
*default_name
;
902 class CompilationUnitSupportCode
905 const Project
& project
;
907 CompilationUnitSupportCode ( const Project
& project
);
908 ~CompilationUnitSupportCode ();
909 void Generate ( bool verbose
);
911 void GenerateForModule ( Module
& module
,
913 std::string
GetCompilationUnitFilename ( Module
& module
,
914 CompilationUnit
& compilationUnit
);
915 void WriteCompilationUnitFile ( Module
& module
,
916 CompilationUnit
& compilationUnit
);
920 enum AutoRegisterType
927 class AutoRegister
: public XmlNode
930 const Module
* module
;
931 std::string infSection
;
932 AutoRegisterType type
;
933 AutoRegister ( const Project
& project_
,
934 const Module
* module_
,
935 const XMLElement
& node_
);
937 bool IsSupportedModuleType ( ModuleType type
);
938 AutoRegisterType
GetAutoRegisterType( const std::string
& type
);
943 class SysSetupGenerator
946 const Project
& project
;
947 SysSetupGenerator ( const Project
& project
);
948 ~SysSetupGenerator ();
951 std::string
GetDirectoryId ( const Module
& module
);
952 std::string
GetFlags ( const Module
& module
);
953 void Generate ( HINF inf
,
954 const Module
& module
);
959 InitializeEnvironment ();
962 Right ( const std::string
& s
, size_t n
);
965 Replace ( const std::string
& s
, const std::string
& find
, const std::string
& with
);
968 ChangeSeparator ( const std::string
& s
,
969 const char fromSeparator
,
970 const char toSeparator
);
973 FixSeparator ( const std::string
& s
);
976 FixSeparatorForSystemCommand ( const std::string
& s
);
979 DosSeparator ( const std::string
& s
);
983 const std::string
& filename
,
984 const std::string
& newExtension
);
988 const Project
& project
,
989 const std::string
& location
,
990 const std::string
& path
,
991 const std::string
& att_value
);
994 GetExtension ( const FileLocation
& file
);
997 NormalizeFilename ( const std::string
& filename
);
1000 ToLower ( std::string filename
);
1002 #endif /* __RBUILD_H */