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
;
92 class AutomaticDependency
;
97 class StubbedComponent
;
99 class CompilationUnit
;
103 class SourceFileTest
;
106 typedef std::map
<std::string
,Directory
*> directory_map
;
112 directory_map subdirs
;
113 Directory ( const std::string
& name
);
114 void Add ( const char* subdir
);
115 void GenerateTree ( const std::string
& parent
,
117 std::string
EscapeSpaces ( std::string path
);
118 void CreateRule ( FILE* f
,
119 const std::string
& parent
);
121 bool mkdir_p ( const char* path
);
122 std::string
ReplaceVariable ( std::string name
,
125 std::string
GetEnvironmentVariable ( const std::string
& name
);
126 void ResolveVariablesInPath ( char* buf
,
128 bool CreateDirectory ( std::string path
);
139 bool AutomaticDependencies
;
140 bool CheckDependenciesForModuleOnly
;
141 bool CompilationUnitsEnabled
;
142 std::string CheckDependenciesForModuleOnlyModule
;
143 std::string VSProjectVersion
;
144 std::string VSConfigurationType
;
145 bool UseVSVersionInPath
;
146 bool UseConfigurationInPath
;
147 bool MakeHandlesInstallDirectories
;
148 bool GenerateProxyMakefilesInSourceTree
;
155 static std::string
GetVariable ( const std::string
& name
);
156 static std::string
GetIntermediatePath ();
157 static std::string
GetOutputPath ();
158 static std::string
GetCdOutputPath ();
159 static std::string
GetInstallPath ();
160 static std::string
GetAutomakeFile ( const std::string
& defaultFile
);
161 static std::string
GetEnvironmentVariablePathOrDefault ( const std::string
& name
,
162 const std::string
& defaultValue
);
166 class FileSupportCode
169 static void WriteIfChanged ( char* outbuf
,
170 std::string filename
);
178 CompilationUnit
* compilationUnit
;
186 std::vector
<CompilationUnit
*> compilationUnits
;
187 std::vector
<File
*> files
;
188 std::vector
<Include
*> includes
;
189 std::vector
<Define
*> defines
;
190 std::vector
<Library
*> libraries
;
191 std::vector
<Property
*> properties
;
192 std::vector
<Module
*> modules
;
193 std::vector
<CompilerFlag
*> compilerFlags
;
194 std::vector
<If
*> ifs
;
198 void ExtractModules( std::vector
<Module
*> &modules
);
204 XMLElement
*node
, *head
;
207 const Configuration
& configuration
;
209 std::string makefile
;
210 XMLIncludes xmlbuildfiles
;
211 std::vector
<LinkerFlag
*> linkerFlags
;
212 std::vector
<CDFile
*> cdfiles
;
213 std::vector
<InstallFile
*> installfiles
;
214 std::vector
<Module
*> modules
;
215 IfableData non_if_data
;
217 Project ( const Configuration
& configuration
,
218 const std::string
& filename
);
220 void SetBackend ( Backend
* backend
) { _backend
= backend
; }
221 Backend
& GetBackend() { return *_backend
; }
222 void WriteConfigurationFile ();
223 void ExecuteInvocations ();
225 void ProcessXML ( const std::string
& path
);
226 Module
* LocateModule ( const std::string
& name
);
227 const Module
* LocateModule ( const std::string
& name
) const;
228 const std::string
& GetProjectFilename () const;
229 std::string
ResolveProperties ( const std::string
& s
) const;
231 std::string
ResolveNextProperty ( std::string
& s
) const;
232 const Property
* LookupProperty ( const std::string
& name
) const;
233 void SetConfigurationOption ( char* s
,
235 std::string
* alternativeName
);
236 void SetConfigurationOption ( char* s
,
239 void ProcessXMLSubElement ( const XMLElement
& e
,
240 const std::string
& path
,
241 ParseContext
& parseContext
);
243 // disable copy semantics
244 Project ( const Project
& );
245 Project
& operator = ( const Project
& );
256 KernelModeDriver
= 5,
288 const Project
& project
;
289 const XMLElement
& node
;
290 std::string xmlbuildFile
;
293 std::string extension
;
294 std::string baseaddress
;
298 ImportLibrary
* importLibrary
;
300 bool underscoreSymbols
;
302 bool isDefaultEntryPoint
;
303 Bootstrap
* bootstrap
;
304 AutoRegister
* autoRegister
;
305 IfableData non_if_data
;
306 std::vector
<Invoke
*> invocations
;
307 std::vector
<Dependency
*> dependencies
;
308 std::vector
<CompilerFlag
*> compilerFlags
;
309 std::vector
<LinkerFlag
*> linkerFlags
;
310 std::vector
<StubbedComponent
*> stubbedComponents
;
311 LinkerScript
* linkerScript
;
316 std::string installBase
;
317 std::string installName
;
318 std::string aliasedModuleName
;
325 Module ( const Project
& project
,
326 const XMLElement
& moduleNode
,
327 const std::string
& modulePath
);
329 ModuleType
GetModuleType ( const std::string
& location
,
330 const XMLAttribute
& attribute
);
331 bool HasImportLibrary () const;
333 bool GenerateInOutputTree () const;
334 std::string
GetTargetName () const; // "foo.exe"
335 std::string
GetDependencyPath () const; // "path/foo.exe" or "path/libfoo.a"
336 std::string
GetBasePath () const; // "path"
337 std::string
GetPath () const; // "path/foo.exe"
338 std::string
GetPathWithPrefix ( const std::string
& prefix
) const; // "path/prefixfoo.exe"
339 std::string
GetPathToBaseDir() const; // "../" offset to rootdirectory
340 std::string
GetEntryPoint(bool leadingUnderscore
) const;
341 void GetTargets ( string_list
& ) const;
342 std::string
GetInvocationTarget ( const int index
) const;
343 bool HasFileWithExtension ( const IfableData
&, const std::string
& extension
) const;
344 void InvokeModule () const;
346 void GetSourceFilenames ( string_list
& list
,
347 bool includeGeneratedFiles
) const;
349 std::string
GetDefaultModuleExtension () const;
350 std::string
GetDefaultModuleEntrypoint () const;
351 std::string
GetDefaultModuleBaseaddress () const;
352 std::string entrypoint
;
353 void ProcessXMLSubElement ( const XMLElement
& e
,
354 const std::string
& path
,
355 ParseContext
& parseContext
);
362 const Project
& project
;
363 const Module
* module
;
364 const XMLElement
* node
;
365 const Module
* baseModule
;
366 std::string directory
;
367 std::string basePath
;
369 Include ( const Project
& project
,
370 const XMLElement
* includeNode
);
371 Include ( const Project
& project
,
372 const Module
* module
,
373 const XMLElement
* includeNode
);
374 Include ( const Project
& project
,
375 std::string directory
,
376 std::string basePath
);
386 const Project
& project
;
387 const Module
* module
;
388 const XMLElement
* node
;
392 Define ( const Project
& project
,
393 const XMLElement
& defineNode
);
394 Define ( const Project
& project
,
395 const Module
* module
,
396 const XMLElement
& defineNode
);
397 Define ( const Project
& project
,
398 const Module
* module
,
399 const std::string name_
);
412 std::string switches
;
413 bool isPreCompiledHeader
;
415 File ( const std::string
& _name
,
417 std::string _switches
,
418 bool _isPreCompiledHeader
);
426 const XMLElement
*node
;
428 const Module
& module
;
430 const Module
* importedModule
;
432 Library ( const XMLElement
& _node
,
433 const Module
& _module
,
434 const std::string
& _name
);
435 Library ( const Module
& _module
,
436 const std::string
& _name
);
445 const XMLElement
& node
;
446 const Module
& module
;
447 const Module
* invokeModule
;
448 std::vector
<InvokeFile
*> input
;
449 std::vector
<InvokeFile
*> output
;
451 Invoke ( const XMLElement
& _node
,
452 const Module
& _module
);
455 void GetTargets ( string_list
& targets
) const;
456 std::string
GetParameters () const;
458 void ProcessXMLSubElement ( const XMLElement
& e
);
459 void ProcessXMLSubElementInput ( const XMLElement
& e
);
460 void ProcessXMLSubElementOutput ( const XMLElement
& e
);
467 const XMLElement
& node
;
469 std::string switches
;
471 InvokeFile ( const XMLElement
& _node
,
472 const std::string
& _name
);
481 const XMLElement
& node
;
482 const Module
& module
;
483 const Module
* dependencyModule
;
485 Dependency ( const XMLElement
& _node
,
486 const Module
& _module
);
495 const XMLElement
& node
;
496 const Module
& module
;
497 std::string basename
;
498 std::string definition
;
501 ImportLibrary ( const XMLElement
& _node
,
502 const Module
& module
);
511 const XMLElement
& node
;
512 const Project
& project
;
513 const Module
* module
;
515 std::string property
, value
;
518 If ( const XMLElement
& node_
,
519 const Project
& project_
,
520 const Module
* module_
,
521 const bool negated_
= false );
531 const Project
& project
;
532 const Module
* module
;
533 const XMLElement
& node
;
536 CompilerFlag ( const Project
& project
,
537 const XMLElement
& compilerFlagNode
);
538 CompilerFlag ( const Project
& project
,
539 const Module
* module
,
540 const XMLElement
& compilerFlagNode
);
551 const Project
& project
;
552 const Module
* module
;
553 const XMLElement
& node
;
556 LinkerFlag ( const Project
& project
,
557 const XMLElement
& linkerFlagNode
);
558 LinkerFlag ( const Project
& project
,
559 const Module
* module
,
560 const XMLElement
& linkerFlagNode
);
571 const Project
& project
;
572 const Module
* module
;
573 const XMLElement
& node
;
574 const Module
* baseModule
;
575 std::string directory
;
576 std::string basePath
;
578 LinkerScript ( const Project
& project
,
579 const Module
* module
,
580 const XMLElement
& node
);
589 const XMLElement
& node
;
590 const Project
& project
;
591 const Module
* module
;
592 std::string name
, value
;
594 Property ( const XMLElement
& node_
,
595 const Project
& project_
,
596 const Module
* module_
);
602 class TestSupportCode
605 const Project
& project
;
607 TestSupportCode ( const Project
& project
);
609 void GenerateTestSupportCode ( bool verbose
);
611 bool IsTestModule ( const Module
& module
);
612 void GenerateTestSupportCodeForModule ( Module
& module
,
614 std::string
GetHooksFilename ( Module
& module
);
615 char* WriteStubbedSymbolToHooksFile ( char* buffer
,
616 const StubbedComponent
& component
,
617 const StubbedSymbol
& symbol
);
618 char* WriteStubbedComponentToHooksFile ( char* buffer
,
619 const StubbedComponent
& component
);
620 void WriteHooksFile ( Module
& module
);
621 std::string
GetStubsFilename ( Module
& module
);
622 char* WriteStubbedSymbolToStubsFile ( char* buffer
,
623 const StubbedComponent
& component
,
624 const StubbedSymbol
& symbol
,
626 char* WriteStubbedComponentToStubsFile ( char* buffer
,
627 const StubbedComponent
& component
,
629 void WriteStubsFile ( Module
& module
);
630 std::string
GetStartupFilename ( Module
& module
);
631 bool IsUnknownCharacter ( char ch
);
632 std::string
GetTestDispatcherName ( std::string filename
);
633 bool IsTestFile ( std::string
& filename
) const;
634 void GetSourceFilenames ( string_list
& list
,
635 Module
& module
) const;
636 char* WriteTestDispatcherPrototypesToStartupFile ( char* buffer
,
638 char* WriteRegisterTestsFunctionToStartupFile ( char* buffer
,
640 void WriteStartupFile ( Module
& module
);
647 const Project
& project
;
650 WineResource ( const Project
& project
,
651 std::string bin2res
);
653 void UnpackResources ( bool verbose
);
655 bool IsSpecFile ( const File
& file
);
656 bool IsWineModule ( const Module
& module
);
657 bool IsResourceFile ( const File
& file
);
658 std::string
GetResourceFilename ( const Module
& module
);
659 void UnpackResourcesInModule ( Module
& module
,
667 SourceFile ( AutomaticDependency
* automaticDependency
,
668 const Module
& module
,
669 const std::string
& filename
,
671 bool isNonAutomaticDependency
);
672 SourceFile
* ParseFile ( const std::string
& normalizedFilename
);
674 std::string
Location () const;
675 std::vector
<SourceFile
*> files
;
676 AutomaticDependency
* automaticDependency
;
677 const Module
& module
;
678 std::string filename
;
679 std::string filenamePart
;
680 std::string directoryPart
;
681 std::vector
<SourceFile
*> parents
; /* List of files, this file is included from */
682 bool isNonAutomaticDependency
;
683 std::string cachedDependencies
;
684 time_t lastWriteTime
;
685 time_t youngestLastWriteTime
; /* Youngest last write time of this file and all children */
686 SourceFile
* youngestFile
;
688 void GetDirectoryAndFilenameParts ();
691 void SkipWhitespace ();
692 bool ReadInclude ( std::string
& filename
,
693 bool& searchCurrentDirectory
,
695 bool IsIncludedFrom ( const std::string
& normalizedFilename
);
696 SourceFile
* GetParentSourceFile ();
697 bool CanProcessFile ( const std::string
& extension
);
698 bool IsParentOf ( const SourceFile
* parent
,
699 const SourceFile
* child
);
706 class AutomaticDependency
708 friend class SourceFileTest
;
710 const Project
& project
;
712 AutomaticDependency ( const Project
& project
);
713 ~AutomaticDependency ();
714 std::string
GetFilename ( const std::string
& filename
);
715 bool LocateIncludedFile ( const std::string
& directory
,
716 const std::string
& includedFilename
,
717 std::string
& resolvedFilename
);
718 bool LocateIncludedFile ( SourceFile
* sourceFile
,
719 const Module
& module
,
720 const std::string
& includedFilename
,
721 bool searchCurrentDirectory
,
723 std::string
& resolvedFilename
);
724 SourceFile
* RetrieveFromCacheOrParse ( const Module
& module
,
725 const std::string
& filename
,
726 SourceFile
* parentSourceFile
);
727 SourceFile
* RetrieveFromCache ( const std::string
& filename
);
728 void CheckAutomaticDependencies ( bool verbose
);
729 void CheckAutomaticDependenciesForModule ( Module
& module
,
732 void GetModulesToCheck ( Module
& module
, std::vector
<const Module
*>& modules
);
733 void CheckAutomaticDependencies ( const Module
& module
,
735 void CheckAutomaticDependenciesForFile ( SourceFile
* sourceFile
);
736 void GetIncludeDirectories ( std::vector
<Include
*>& includes
,
737 const Module
& module
,
738 Include
& currentDirectory
,
739 bool searchCurrentDirectory
);
740 void GetModuleFiles ( const Module
& module
,
741 std::vector
<File
*>& files
) const;
743 void ParseFiles ( const Module
& module
);
744 void ParseFile ( const Module
& module
,
746 std::map
<std::string
, SourceFile
*> sourcefile_map
;
753 const Project
& project
;
754 const Module
* module
;
755 const XMLElement
& node
;
757 std::string nameoncd
;
759 Bootstrap ( const Project
& project
,
760 const Module
* module
,
761 const XMLElement
& bootstrapNode
);
765 bool IsSupportedModuleType ( ModuleType type
);
767 static std::string
ReplaceVariable ( const std::string
& name
,
768 const std::string
& value
,
776 const Project
& project
;
777 const XMLElement
& node
;
780 std::string nameoncd
;
783 CDFile ( const Project
& project
,
784 const XMLElement
& bootstrapNode
,
785 const std::string
& path
);
788 std::string
GetPath () const;
790 static std::string
ReplaceVariable ( const std::string
& name
,
791 const std::string
& value
,
799 const Project
& project
;
800 const XMLElement
& node
;
806 InstallFile ( const Project
& project
,
807 const XMLElement
& bootstrapNode
,
808 const std::string
& path
);
811 std::string
GetPath () const;
818 const XMLElement
& node
;
819 const Module
& module
;
823 const XMLElement
& node
,
824 const Module
& module
,
830 class StubbedComponent
833 const Module
* module
;
834 const XMLElement
& node
;
836 std::vector
<StubbedSymbol
*> symbols
;
838 StubbedComponent ( const Module
* module_
,
839 const XMLElement
& stubbedComponentNode
);
840 ~StubbedComponent ();
842 void ProcessXMLSubElement ( const XMLElement
& e
);
849 const XMLElement
& node
;
852 std::string strippedName
;
854 StubbedSymbol ( const XMLElement
& stubbedSymbolNode
);
858 std::string
StripSymbol ( std::string symbol
);
862 class CompilationUnit
865 const Project
* project
;
866 const Module
* module
;
867 const XMLElement
* node
;
869 std::vector
<File
*> files
;
871 CompilationUnit ( File
* file
);
872 CompilationUnit ( const Project
* project
,
873 const Module
* module
,
874 const XMLElement
* node
);
877 bool IsGeneratedFile () const;
878 bool HasFileWithExtension ( const std::string
& extension
) const;
879 bool IsFirstFile () const;
880 FileLocation
* GetFilename ( Directory
* intermediateDirectory
) const;
881 std::string
GetSwitches () const;
885 class CompilationUnitSupportCode
888 const Project
& project
;
890 CompilationUnitSupportCode ( const Project
& project
);
891 ~CompilationUnitSupportCode ();
892 void Generate ( bool verbose
);
894 void GenerateForModule ( Module
& module
,
896 std::string
GetCompilationUnitFilename ( Module
& module
,
897 CompilationUnit
& compilationUnit
);
898 void WriteCompilationUnitFile ( Module
& module
,
899 CompilationUnit
& compilationUnit
);
906 Directory
* directory
;
907 std::string filename
;
908 FileLocation ( Directory
* directory
,
909 std::string filename
);
913 enum AutoRegisterType
923 const Project
& project
;
924 const Module
* module
;
925 const XMLElement
& node
;
926 std::string infSection
;
927 AutoRegisterType type
;
928 AutoRegister ( const Project
& project_
,
929 const Module
* module_
,
930 const XMLElement
& node_
);
934 bool IsSupportedModuleType ( ModuleType type
);
935 AutoRegisterType
GetAutoRegisterType( std::string type
);
940 class SysSetupGenerator
943 const Project
& project
;
944 SysSetupGenerator ( const Project
& project
);
945 ~SysSetupGenerator ();
948 std::string
GetDirectoryId ( const Module
& module
);
949 std::string
GetFlags ( const Module
& module
);
950 void Generate ( HINF inf
,
951 const Module
& module
);
956 InitializeEnvironment ();
959 Right ( const std::string
& s
, size_t n
);
962 Replace ( const std::string
& s
, const std::string
& find
, const std::string
& with
);
965 ChangeSeparator ( const std::string
& s
,
966 const char fromSeparator
,
967 const char toSeparator
);
970 FixSeparator ( const std::string
& s
);
973 FixSeparatorForSystemCommand ( const std::string
& s
);
976 DosSeparator ( const std::string
& s
);
980 const std::string
& filename
,
981 const std::string
& newExtension
);
985 const std::string
& location
,
986 const std::string
& path
,
987 const std::string
& att_value
);
990 GetExtension ( const std::string
& filename
);
993 GetDirectory ( const std::string
& filename
);
996 GetFilename ( const std::string
& filename
);
999 NormalizeFilename ( const std::string
& filename
);
1002 ToLower ( std::string filename
);
1004 #endif /* __RBUILD_H */