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"
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
,
165 DependenciesType Dependencies
;
166 bool CheckDependenciesForModuleOnly
;
167 bool CompilationUnitsEnabled
;
168 bool PrecompiledHeadersEnabled
;
169 std::string CheckDependenciesForModuleOnlyModule
;
170 std::string VSProjectVersion
;
171 std::string VSConfigurationType
;
172 bool UseVSVersionInPath
;
173 bool UseConfigurationInPath
;
174 bool MakeHandlesInstallDirectories
;
175 bool GenerateProxyMakefilesInSourceTree
;
182 static std::string
GetVariable ( const std::string
& name
);
183 static std::string
GetArch ();
184 static std::string
GetIntermediatePath ();
185 static std::string
GetOutputPath ();
186 static std::string
GetCdOutputPath ();
187 static std::string
GetInstallPath ();
188 static std::string
GetAutomakeFile ( const std::string
& defaultFile
);
189 static std::string
GetEnvironmentVariablePathOrDefault ( const std::string
& name
,
190 const std::string
& defaultValue
);
194 class FileSupportCode
197 static void WriteIfChanged ( char* outbuf
,
198 const std::string
& filename
,
199 bool ignoreError
= false );
206 CompilationUnit
* compilationUnit
;
214 std::vector
<CompilationUnit
*> compilationUnits
;
215 std::vector
<File
*> files
;
216 std::vector
<Include
*> includes
;
217 std::vector
<Define
*> defines
;
218 std::vector
<Library
*> libraries
;
219 std::map
<std::string
, Property
*> properties
;
220 std::vector
<Module
*> modules
;
221 std::vector
<CompilerFlag
*> compilerFlags
;
222 int asmFiles
; // number of .asm files in compilationUnits
227 void ExtractModules( std::map
<std::string
, Module
*> &modules
);
233 XMLElement
*node
, *head
;
236 const Configuration
& configuration
;
238 std::string makefile
;
239 XMLIncludes xmlbuildfiles
;
240 std::vector
<LinkerFlag
*> linkerFlags
;
241 std::vector
<CDFile
*> cdfiles
;
242 std::vector
<InstallFile
*> installfiles
;
243 std::map
<std::string
, Module
*> modules
;
244 IfableData non_if_data
;
245 IfableData host_non_if_data
;
247 bool allowWarningsSet
;
249 Project ( const Configuration
& configuration
,
250 const std::string
& filename
,
251 const std::map
<std::string
, std::string
>* properties
= NULL
);
253 void SetBackend ( Backend
* backend
) { _backend
= backend
; }
254 Backend
& GetBackend() { return *_backend
; }
255 void ExecuteInvocations ();
257 void ProcessXML ( const std::string
& path
);
258 Module
* LocateModule ( const std::string
& name
);
259 const Module
* LocateModule ( const std::string
& name
) const;
260 const std::string
& GetProjectFilename () const;
261 std::string
ResolveProperties ( const std::string
& s
) const;
262 const Property
* LookupProperty ( const std::string
& name
) const;
264 std::string
ResolveNextProperty ( const std::string
& s
) const;
266 void ProcessXMLSubElement ( const XMLElement
& e
,
267 const std::string
& path
,
268 ParseContext
& parseContext
);
270 // disable copy semantics
271 Project ( const Project
& );
272 Project
& operator = ( const Project
& );
311 TypeDontCare
, // always at the end
324 CompilerTypeDontCare
,
332 DirectoryLocation directory
;
333 std::string relative_path
;
336 FileLocation ( const DirectoryLocation directory
,
337 const std::string
& relative_path
,
338 const std::string
& name
,
339 const XMLElement
*node
= NULL
);
341 FileLocation ( const FileLocation
& other
);
347 const Project
& project
;
348 const XMLElement
& node
;
349 std::string xmlbuildFile
;
352 std::string extension
;
353 std::string baseaddress
;
355 std::string buildtype
;
357 ImportLibrary
* importLibrary
;
359 Bootsector
* bootSector
;
361 bool underscoreSymbols
;
363 bool isDefaultEntryPoint
;
364 Bootstrap
* bootstrap
;
365 AutoRegister
* autoRegister
; // <autoregister> node
366 IfableData non_if_data
;
367 std::vector
<Invoke
*> invocations
;
368 std::vector
<Dependency
*> dependencies
;
369 std::vector
<CompilerFlag
*> compilerFlags
;
370 std::vector
<LinkerFlag
*> linkerFlags
;
371 std::vector
<StubbedComponent
*> stubbedComponents
;
372 LinkerScript
* linkerScript
;
376 std::string aliasedModuleName
;
383 FileLocation
*output
; // "path/foo.exe"
384 FileLocation
*dependency
; // "path/foo.exe" or "path/libfoo.a"
385 FileLocation
*install
;
386 std::string description
;
388 std::string layoutId
;
389 std::string layoutNameResId
;
391 Module ( const Project
& project
,
392 const XMLElement
& moduleNode
,
393 const std::string
& modulePath
);
395 ModuleType
GetModuleType ( const std::string
& location
,
396 const XMLAttribute
& attribute
);
397 bool HasImportLibrary () const;
399 std::string
GetPathWithPrefix ( const std::string
& prefix
) const; // "path/prefixfoo.exe"
400 std::string
GetPathToBaseDir() const; // "../" offset to rootdirectory
401 std::string
GetEntryPoint(bool leadingUnderscore
) const;
402 void GetTargets ( string_list
& ) const;
403 std::string
GetInvocationTarget ( const int index
) const;
404 bool HasFileWithExtension ( const IfableData
&, const std::string
& extension
) const;
405 void InvokeModule () const;
407 std::string
GetDllName() const;
409 void SetImportLibrary ( ImportLibrary
* importLibrary
);
410 DirectoryLocation
GetTargetDirectoryTree () const;
411 std::string
GetDefaultModuleExtension () const;
412 std::string
GetDefaultModuleEntrypoint () const;
413 std::string
GetDefaultModuleBaseaddress () const;
414 std::string
GetDefaultModuleCRT () const;
415 bool GetDefaultModuleIsCRT () const;
416 std::string entrypoint
;
417 void ProcessXMLSubElement ( const XMLElement
& e
,
418 DirectoryLocation directory
,
419 const std::string
& relative_path
,
420 ParseContext
& parseContext
);
421 bool GetBooleanAttribute ( const XMLElement
& moduleNode
,
423 bool default_value
= false );
430 FileLocation
*directory
;
432 Include ( const Project
& project
,
433 const XMLElement
* includeNode
);
434 Include ( const Project
& project
,
435 const XMLElement
* includeNode
,
436 const Module
* module
);
437 Include ( const Project
& project
,
438 DirectoryLocation directory
,
439 const std::string
& relative_path
);
443 const Project
& project
;
444 const XMLElement
* node
;
445 const Module
* module
;
446 DirectoryLocation
GetDefaultDirectoryTree ( const Module
* module
) const;
453 const Project
& project
;
454 const Module
* module
;
455 const XMLElement
* node
;
461 Define ( const Project
& project
,
462 const XMLElement
& defineNode
);
463 Define ( const Project
& project
,
464 const Module
* module
,
465 const XMLElement
& defineNode
);
466 Define ( const Project
& project
,
467 const Module
* module
,
468 const std::string
& name_
,
469 const std::string
& backend_
= "" );
482 std::string switches
;
483 bool isPreCompiledHeader
;
485 File ( DirectoryLocation directory
,
486 const std::string
& relative_path
,
487 const std::string
& name
,
489 const std::string
& _switches
,
490 bool _isPreCompiledHeader
);
493 std::string
GetFullPath () const;
499 const XMLElement
*node
;
501 const Module
& module
;
503 const Module
* importedModule
;
505 Library ( const XMLElement
& _node
,
506 const Module
& _module
,
507 const std::string
& _name
);
508 Library ( const Module
& _module
,
509 const std::string
& _name
);
518 const XMLElement
& node
;
519 const Module
& module
;
520 const Module
* invokeModule
;
521 std::vector
<InvokeFile
*> input
;
522 std::vector
<InvokeFile
*> output
;
524 Invoke ( const XMLElement
& _node
,
525 const Module
& _module
);
528 void GetTargets ( string_list
& targets
) const;
529 std::string
GetParameters () const;
531 void ProcessXMLSubElement ( const XMLElement
& e
);
532 void ProcessXMLSubElementInput ( const XMLElement
& e
);
533 void ProcessXMLSubElementOutput ( const XMLElement
& e
);
540 const XMLElement
& node
;
542 std::string switches
;
544 InvokeFile ( const XMLElement
& _node
,
545 const std::string
& _name
);
554 const XMLElement
& node
;
555 const Module
& module
;
556 const Module
* dependencyModule
;
558 Dependency ( const XMLElement
& _node
,
559 const Module
& _module
);
567 const XMLElement
& node
;
568 const Module
* module
;
569 const Module
* bootSectorModule
;
571 Bootsector ( const XMLElement
& _node
,
572 const Module
* _module
);
576 bool IsSupportedModuleType ( ModuleType type
);
582 const XMLElement
& node
;
583 const Module
& module
;
585 std::string description
;
587 std::string copyright
;
592 Metadata ( const XMLElement
& _node
,
593 const Module
& _module
);
598 class ImportLibrary
: public XmlNode
601 const Module
* module
;
603 FileLocation
*source
;
605 ImportLibrary ( const Project
& project
,
606 const XMLElement
& node
,
607 const Module
* module
);
615 const Project
& project
;
616 const Module
* module
;
617 const XMLElement
& node
;
619 CompilerType compiler
;
621 CompilerFlag ( const Project
& project
,
622 const XMLElement
& compilerFlagNode
);
623 CompilerFlag ( const Project
& project
,
624 const Module
* module
,
625 const XMLElement
& compilerFlagNode
);
636 const Project
& project
;
637 const Module
* module
;
638 const XMLElement
& node
;
641 LinkerFlag ( const Project
& project
,
642 const XMLElement
& linkerFlagNode
);
643 LinkerFlag ( const Project
& project
,
644 const Module
* module
,
645 const XMLElement
& linkerFlagNode
);
656 const XMLElement
& node
;
657 const Module
& module
;
658 const FileLocation
*file
;
660 LinkerScript ( const XMLElement
& node
,
661 const Module
& module
,
662 const FileLocation
*file
);
671 const Project
& project
;
672 const Module
* module
;
673 std::string name
, value
;
676 Property ( const XMLElement
& node_
,
677 const Project
& project_
,
678 const Module
* module_
);
680 Property ( const Project
& project_
,
681 const Module
* module_
,
682 const std::string
& name_
,
683 const std::string
& value_
);
689 class TestSupportCode
692 const Project
& project
;
694 TestSupportCode ( const Project
& project
);
696 void GenerateTestSupportCode ( bool verbose
);
698 bool IsTestModule ( const Module
& module
);
699 void GenerateTestSupportCodeForModule ( Module
& module
,
701 std::string
GetHooksFilename ( Module
& module
);
702 char* WriteStubbedSymbolToHooksFile ( char* buffer
,
703 const StubbedComponent
& component
,
704 const StubbedSymbol
& symbol
);
705 char* WriteStubbedComponentToHooksFile ( char* buffer
,
706 const StubbedComponent
& component
);
707 void WriteHooksFile ( Module
& module
);
708 std::string
GetStubsFilename ( Module
& module
);
709 char* WriteStubbedSymbolToStubsFile ( char* buffer
,
710 const StubbedComponent
& component
,
711 const StubbedSymbol
& symbol
,
713 char* WriteStubbedComponentToStubsFile ( char* buffer
,
714 const StubbedComponent
& component
,
716 void WriteStubsFile ( Module
& module
);
717 std::string
GetStartupFilename ( Module
& module
);
718 bool IsUnknownCharacter ( char ch
);
719 std::string
GetTestDispatcherName ( std::string filename
);
720 bool IsTestFile ( std::string
& filename
) const;
721 void GetSourceFilenames ( string_list
& list
,
722 Module
& module
) const;
723 char* WriteTestDispatcherPrototypesToStartupFile ( char* buffer
,
725 char* WriteRegisterTestsFunctionToStartupFile ( char* buffer
,
727 void WriteStartupFile ( Module
& module
);
734 SourceFile ( AutomaticDependency
* automaticDependency
,
735 const Module
& module
,
737 SourceFile
* parent
);
739 std::vector
<SourceFile
*> files
; /* List of files included in this file */
741 AutomaticDependency
* automaticDependency
;
742 const Module
& module
;
743 std::vector
<SourceFile
*> parents
; /* List of files, this file is included from */
744 time_t lastWriteTime
;
745 time_t youngestLastWriteTime
; /* Youngest last write time of this file and all children */
746 SourceFile
* youngestFile
;
750 void SkipWhitespace ();
751 bool ReadInclude ( std::string
& filename
,
752 bool& searchCurrentDirectory
,
754 bool IsIncludedFrom ( const File
& file
);
755 SourceFile
* ParseFile(const File
& file
);
756 bool CanProcessFile ( const File
& file
);
757 bool IsParentOf ( const SourceFile
* parent
,
758 const SourceFile
* child
);
765 class AutomaticDependency
767 friend class SourceFileTest
;
769 const Project
& project
;
771 AutomaticDependency ( const Project
& project
);
772 ~AutomaticDependency ();
773 bool LocateIncludedFile ( const FileLocation
& directory
,
774 const std::string
& includedFilename
);
775 bool LocateIncludedFile ( SourceFile
* sourceFile
,
776 const Module
& module
,
777 const std::string
& includedFilename
,
778 bool searchCurrentDirectory
,
780 File
& resolvedFile
);
781 SourceFile
* RetrieveFromCacheOrParse ( const Module
& module
,
783 SourceFile
* parentSourceFile
);
784 SourceFile
* RetrieveFromCache ( const File
& file
);
785 void CheckAutomaticDependencies ( bool verbose
);
786 void CheckAutomaticDependenciesForModule ( Module
& module
,
789 void GetModulesToCheck ( Module
& module
, std::vector
<const Module
*>& modules
);
790 void CheckAutomaticDependencies ( const Module
& module
,
792 void CheckAutomaticDependenciesForFile ( SourceFile
* sourceFile
);
793 void GetIncludeDirectories ( std::vector
<Include
*>& includes
,
794 const Module
& module
);
795 void GetModuleFiles ( const Module
& module
,
796 std::vector
<File
*>& files
) const;
798 void ParseFiles ( const Module
& module
);
799 void ParseFile ( const Module
& module
,
801 std::map
<std::string
, SourceFile
*> sourcefile_map
;
808 const Project
& project
;
809 const Module
* module
;
810 const XMLElement
& node
;
812 std::string nameoncd
;
814 Bootstrap ( const Project
& project
,
815 const Module
* module
,
816 const XMLElement
& bootstrapNode
);
820 bool IsSupportedModuleType ( ModuleType type
);
822 static std::string
ReplaceVariable ( const std::string
& name
,
823 const std::string
& value
,
828 class CDFile
: public XmlNode
831 FileLocation
*source
;
832 FileLocation
*target
;
834 CDFile ( const Project
& project
,
835 const XMLElement
& bootstrapNode
,
836 const std::string
& path
);
839 static std::string
ReplaceVariable ( const std::string
& name
,
840 const std::string
& value
,
845 class InstallFile
: public XmlNode
848 FileLocation
*source
;
849 FileLocation
*target
;
851 InstallFile ( const Project
& project
,
852 const XMLElement
& bootstrapNode
,
853 const std::string
& path
);
861 const XMLElement
& node
;
862 const Module
& module
;
863 const FileLocation
*file
;
866 const XMLElement
& node
,
867 const Module
& module
,
868 const FileLocation
*file
);
874 class StubbedComponent
877 const Module
* module
;
878 const XMLElement
& node
;
880 std::vector
<StubbedSymbol
*> symbols
;
882 StubbedComponent ( const Module
* module_
,
883 const XMLElement
& stubbedComponentNode
);
884 ~StubbedComponent ();
886 void ProcessXMLSubElement ( const XMLElement
& e
);
893 const XMLElement
& node
;
896 std::string strippedName
;
898 StubbedSymbol ( const XMLElement
& stubbedSymbolNode
);
902 std::string
StripSymbol ( std::string symbol
);
906 class CompilationUnit
911 CompilationUnit ( const File
* file
);
912 CompilationUnit ( const Project
* project
,
913 const Module
* module
,
914 const XMLElement
* node
);
917 bool IsGeneratedFile () const;
918 bool HasFileWithExtension ( const std::string
& extension
) const;
919 bool IsFirstFile () const;
920 const FileLocation
& GetFilename () const;
921 const std::string
& GetSwitches () const;
922 void AddFile ( const File
* file
);
923 const std::vector
<const File
*> GetFiles () const;
925 const Project
* project
;
926 const Module
* module
;
927 const XMLElement
* node
;
928 std::vector
<const File
*> files
;
929 FileLocation
*default_name
;
933 class CompilationUnitSupportCode
936 const Project
& project
;
938 CompilationUnitSupportCode ( const Project
& project
);
939 ~CompilationUnitSupportCode ();
940 void Generate ( bool verbose
);
942 void GenerateForModule ( Module
& module
,
944 std::string
GetCompilationUnitFilename ( Module
& module
,
945 CompilationUnit
& compilationUnit
);
946 void WriteCompilationUnitFile ( Module
& module
,
947 CompilationUnit
& compilationUnit
);
951 enum AutoRegisterType
958 class AutoRegister
: public XmlNode
961 const Module
* module
;
962 std::string infSection
;
963 AutoRegisterType type
;
964 AutoRegister ( const Project
& project_
,
965 const Module
* module_
,
966 const XMLElement
& node_
);
968 bool IsSupportedModuleType ( ModuleType type
);
969 AutoRegisterType
GetAutoRegisterType( const std::string
& type
);
974 class SysSetupGenerator
977 const Project
& project
;
978 SysSetupGenerator ( const Project
& project
);
979 ~SysSetupGenerator ();
982 std::string
GetDirectoryId ( const Module
& module
);
983 std::string
GetFlags ( const Module
& module
);
984 void Generate ( HINF inf
,
985 const Module
& module
);
990 InitializeEnvironment ();
993 Right ( const std::string
& s
, size_t n
);
996 Replace ( const std::string
& s
, const std::string
& find
, const std::string
& with
);
999 ChangeSeparator ( const std::string
& s
,
1000 const char fromSeparator
,
1001 const char toSeparator
);
1004 FixSeparator ( const std::string
& s
);
1007 FixSeparatorForSystemCommand ( const std::string
& s
);
1010 DosSeparator ( const std::string
& s
);
1014 const std::string
& filename
,
1015 const std::string
& newExtension
);
1019 const Project
& project
,
1020 const std::string
& location
,
1021 const std::string
& path
,
1022 const std::string
& att_value
);
1025 GetExtension ( const FileLocation
& file
);
1028 NormalizeFilename ( const std::string
& filename
);
1031 ToLower ( std::string filename
);
1033 #endif /* __RBUILD_H */