Don't strip relocation information from DLLs
[reactos.git] / reactos / tools / rbuild / rbuild.h
1 #ifndef __RBUILD_H
2 #define __RBUILD_H
3
4 #include "pch.h"
5
6 #ifdef WIN32
7 #include <direct.h>
8 #include <io.h>
9 #endif/*WIN32*/
10 #include <sys/stat.h>
11 #include <time.h>
12 #ifdef _MSC_VER
13 #include <sys/utime.h>
14 #else/*_MSC_VER*/
15 #include <utime.h>
16 #ifdef WIN32
17 #include <process.h>
18 #endif/*WIN32*/
19 #endif/*_MSC_VER*/
20
21 #include "ssprintf.h"
22 #include "exception.h"
23 #include "XML.h"
24
25 typedef std::vector<std::string> string_list;
26
27 #ifdef WIN32
28 #define EXEPREFIX ""
29 #define EXEPOSTFIX ".exe"
30 #define CSEP '\\'
31 #define CBAD_SEP '/'
32 #define SSEP "\\"
33 #define SBAD_SEP "/"
34 #else
35 #define EXEPREFIX "./"
36 #define EXEPOSTFIX ""
37 #define CSEP '/'
38 #define CBAD_SEP '\\'
39 #define SSEP "/"
40 #define SBAD_SEP "\\"
41 #endif
42
43 class Project;
44 class IfableData;
45 class Module;
46 class Include;
47 class Define;
48 class File;
49 class Library;
50 class Invoke;
51 class InvokeFile;
52 class Dependency;
53 class ImportLibrary;
54 class If;
55 class CompilerFlag;
56 class LinkerFlag;
57 class Property;
58 class AutomaticDependency;
59 class Bootstrap;
60 class CDFile;
61 class InstallFile;
62 class PchFile;
63 class StubbedComponent;
64 class StubbedSymbol;
65
66 class SourceFileTest;
67
68 class IfableData
69 {
70 public:
71 std::vector<File*> files;
72 std::vector<Include*> includes;
73 std::vector<Define*> defines;
74 std::vector<Library*> libraries;
75 std::vector<Property*> properties;
76 std::vector<If*> ifs;
77
78 ~IfableData();
79 void ProcessXML();
80 };
81
82 class Project
83 {
84 std::string xmlfile;
85 XMLElement *node, *head;
86 public:
87 std::string name;
88 std::string makefile;
89 XMLIncludes xmlbuildfiles;
90 std::vector<Module*> modules;
91 std::vector<LinkerFlag*> linkerFlags;
92 std::vector<CDFile*> cdfiles;
93 std::vector<InstallFile*> installfiles;
94 IfableData non_if_data;
95
96 Project ( const std::string& filename );
97 ~Project ();
98 void WriteConfigurationFile ();
99 void ExecuteInvocations ();
100 void ProcessXML ( const std::string& path );
101 Module* LocateModule ( const std::string& name );
102 const Module* LocateModule ( const std::string& name ) const;
103 std::string GetProjectFilename () const;
104 private:
105 const Property* LookupProperty ( const std::string& name ) const;
106 void SetConfigurationOption ( char* s,
107 std::string name,
108 std::string* alternativeName );
109 void SetConfigurationOption ( char* s,
110 std::string name );
111 void WriteIfChanged ( char* outbuf,
112 std::string filename );
113 void ReadXml ();
114 void ProcessXMLSubElement ( const XMLElement& e,
115 const std::string& path,
116 If* pIf = NULL );
117
118 // disable copy semantics
119 Project ( const Project& );
120 Project& operator = ( const Project& );
121 };
122
123
124 enum ModuleType
125 {
126 BuildTool = 0,
127 StaticLibrary = 1,
128 ObjectLibrary = 2,
129 Kernel = 3,
130 KernelModeDLL = 4,
131 KernelModeDriver = 5,
132 NativeDLL = 6,
133 NativeCUI = 7,
134 Win32DLL = 8,
135 Win32CUI = 9,
136 Win32GUI = 10,
137 BootLoader = 11,
138 BootSector = 12,
139 Iso = 13,
140 LiveIso = 14,
141 Test = 15,
142 RpcServer = 16,
143 RpcClient = 17
144 };
145
146 enum HostType
147 {
148 HostFalse,
149 HostDefault,
150 HostTrue
151 };
152
153 class Module
154 {
155 public:
156 const Project& project;
157 const XMLElement& node;
158 std::string xmlbuildFile;
159 std::string name;
160 std::string extension;
161 std::string entrypoint;
162 std::string baseaddress;
163 std::string path;
164 ModuleType type;
165 ImportLibrary* importLibrary;
166 bool mangledSymbols;
167 Bootstrap* bootstrap;
168 IfableData non_if_data;
169 std::vector<Invoke*> invocations;
170 std::vector<Dependency*> dependencies;
171 std::vector<CompilerFlag*> compilerFlags;
172 std::vector<LinkerFlag*> linkerFlags;
173 std::vector<StubbedComponent*> stubbedComponents;
174 PchFile* pch;
175 bool cplusplus;
176 std::string prefix;
177 HostType host;
178 std::string installBase;
179 std::string installName;
180 bool useWRC;
181 bool enableWarnings;
182
183 Module ( const Project& project,
184 const XMLElement& moduleNode,
185 const std::string& modulePath );
186 ~Module ();
187 ModuleType GetModuleType ( const std::string& location,
188 const XMLAttribute& attribute );
189 bool HasImportLibrary () const;
190 bool IsDLL () const;
191 std::string GetTargetName () const;
192 std::string GetDependencyPath () const;
193 std::string GetBasePath () const;
194 std::string GetPath () const;
195 std::string GetPathWithPrefix ( const std::string& prefix ) const;
196 void GetTargets ( string_list& ) const;
197 std::string GetInvocationTarget ( const int index ) const;
198 bool HasFileWithExtension ( const IfableData&, const std::string& extension ) const;
199 void InvokeModule () const;
200 void ProcessXML ();
201 private:
202 std::string GetDefaultModuleExtension () const;
203 std::string GetDefaultModuleEntrypoint () const;
204 std::string GetDefaultModuleBaseaddress () const;
205 void ProcessXMLSubElement ( const XMLElement& e,
206 const std::string& path,
207 If* pIf = NULL );
208 };
209
210
211 class Include
212 {
213 public:
214 const Project& project;
215 const Module* module;
216 const XMLElement& node;
217 std::string directory;
218 std::string basePath;
219
220 Include ( const Project& project,
221 const XMLElement& includeNode );
222 Include ( const Project& project,
223 const Module* module,
224 const XMLElement& includeNode );
225 ~Include ();
226 void ProcessXML();
227 private:
228 void Initialize();
229 };
230
231
232 class Define
233 {
234 public:
235 const Project& project;
236 const Module* module;
237 const XMLElement& node;
238 std::string name;
239 std::string value;
240
241 Define ( const Project& project,
242 const XMLElement& defineNode );
243 Define ( const Project& project,
244 const Module* module,
245 const XMLElement& defineNode );
246 ~Define();
247 void ProcessXML();
248 private:
249 void Initialize();
250 };
251
252
253 class File
254 {
255 public:
256 std::string name;
257 bool first;
258 std::string switches;
259
260 File ( const std::string& _name,
261 bool _first,
262 std::string _switches );
263
264 void ProcessXML();
265 };
266
267
268 class Library
269 {
270 public:
271 const XMLElement& node;
272 const Module& module;
273 std::string name;
274 const Module* imported_module;
275
276 Library ( const XMLElement& _node,
277 const Module& _module,
278 const std::string& _name );
279
280 void ProcessXML();
281 };
282
283
284 class Invoke
285 {
286 public:
287 const XMLElement& node;
288 const Module& module;
289 const Module* invokeModule;
290 std::vector<InvokeFile*> input;
291 std::vector<InvokeFile*> output;
292
293 Invoke ( const XMLElement& _node,
294 const Module& _module );
295
296 void ProcessXML();
297 void GetTargets ( string_list& targets ) const;
298 std::string GetParameters () const;
299 private:
300 void ProcessXMLSubElement ( const XMLElement& e );
301 void ProcessXMLSubElementInput ( const XMLElement& e );
302 void ProcessXMLSubElementOutput ( const XMLElement& e );
303 };
304
305
306 class InvokeFile
307 {
308 public:
309 const XMLElement& node;
310 std::string name;
311 std::string switches;
312
313 InvokeFile ( const XMLElement& _node,
314 const std::string& _name );
315
316 void ProcessXML ();
317 };
318
319
320 class Dependency
321 {
322 public:
323 const XMLElement& node;
324 const Module& module;
325 const Module* dependencyModule;
326
327 Dependency ( const XMLElement& _node,
328 const Module& _module );
329
330 void ProcessXML();
331 };
332
333
334 class ImportLibrary
335 {
336 public:
337 const XMLElement& node;
338 const Module& module;
339 std::string basename;
340 std::string definition;
341
342 ImportLibrary ( const XMLElement& _node,
343 const Module& module );
344
345 void ProcessXML ();
346 };
347
348
349 class If
350 {
351 public:
352 const XMLElement& node;
353 const Project& project;
354 const Module* module;
355 std::string property, value;
356 IfableData data;
357
358 If ( const XMLElement& node_,
359 const Project& project_,
360 const Module* module_ );
361 ~If();
362
363 void ProcessXML();
364 };
365
366
367 class CompilerFlag
368 {
369 public:
370 const Project& project;
371 const Module* module;
372 const XMLElement& node;
373 std::string flag;
374
375 CompilerFlag ( const Project& project,
376 const XMLElement& compilerFlagNode );
377 CompilerFlag ( const Project& project,
378 const Module* module,
379 const XMLElement& compilerFlagNode );
380 ~CompilerFlag ();
381 void ProcessXML();
382 private:
383 void Initialize();
384 };
385
386
387 class LinkerFlag
388 {
389 public:
390 const Project& project;
391 const Module* module;
392 const XMLElement& node;
393 std::string flag;
394
395 LinkerFlag ( const Project& project,
396 const XMLElement& linkerFlagNode );
397 LinkerFlag ( const Project& project,
398 const Module* module,
399 const XMLElement& linkerFlagNode );
400 ~LinkerFlag ();
401 void ProcessXML();
402 private:
403 void Initialize();
404 };
405
406
407 class Property
408 {
409 public:
410 const XMLElement& node;
411 const Project& project;
412 const Module* module;
413 std::string name, value;
414
415 Property ( const XMLElement& node_,
416 const Project& project_,
417 const Module* module_ );
418
419 void ProcessXML();
420 };
421
422
423 class SourceFile
424 {
425 public:
426 SourceFile ( AutomaticDependency* automaticDependency,
427 Module& module,
428 const std::string& filename,
429 SourceFile* parent,
430 bool isNonAutomaticDependency );
431 SourceFile* ParseFile ( const std::string& normalizedFilename );
432 void Parse ();
433 std::string Location () const;
434 std::vector<SourceFile*> files;
435 AutomaticDependency* automaticDependency;
436 Module& module;
437 std::string filename;
438 std::string filenamePart;
439 std::string directoryPart;
440 std::vector<SourceFile*> parents; /* List of files, this file is included from */
441 bool isNonAutomaticDependency;
442 std::string cachedDependencies;
443 time_t lastWriteTime;
444 time_t youngestLastWriteTime; /* Youngest last write time of this file and all children */
445 SourceFile* youngestFile;
446 private:
447 void GetDirectoryAndFilenameParts ();
448 void Close ();
449 void Open ();
450 void SkipWhitespace ();
451 bool ReadInclude ( std::string& filename,
452 bool& includeNext );
453 bool IsIncludedFrom ( const std::string& normalizedFilename );
454 SourceFile* GetParentSourceFile ();
455 bool CanProcessFile ( const std::string& extension );
456 bool IsParentOf ( const SourceFile* parent,
457 const SourceFile* child );
458 std::string buf;
459 const char *p;
460 const char *end;
461 };
462
463
464 class AutomaticDependency
465 {
466 friend class SourceFileTest;
467 public:
468 const Project& project;
469
470 AutomaticDependency ( const Project& project );
471 ~AutomaticDependency ();
472 void Process ();
473 std::string GetFilename ( const std::string& filename );
474 bool LocateIncludedFile ( const std::string& directory,
475 const std::string& includedFilename,
476 std::string& resolvedFilename );
477 bool LocateIncludedFile ( SourceFile* sourceFile,
478 Module& module,
479 const std::string& includedFilename,
480 bool includeNext,
481 std::string& resolvedFilename );
482 SourceFile* RetrieveFromCacheOrParse ( Module& module,
483 const std::string& filename,
484 SourceFile* parentSourceFile );
485 SourceFile* RetrieveFromCache ( const std::string& filename );
486 void CheckAutomaticDependencies ( bool verbose );
487 void CheckAutomaticDependenciesForFile ( SourceFile* sourceFile );
488 private:
489 void ProcessModule ( Module& module );
490 void ProcessFile ( Module& module,
491 const File& file );
492 std::map<std::string, SourceFile*> sourcefile_map;
493 };
494
495
496 class Bootstrap
497 {
498 public:
499 const Project& project;
500 const Module* module;
501 const XMLElement& node;
502 std::string base;
503 std::string nameoncd;
504
505 Bootstrap ( const Project& project,
506 const Module* module,
507 const XMLElement& bootstrapNode );
508 ~Bootstrap ();
509 void ProcessXML();
510 private:
511 bool IsSupportedModuleType ( ModuleType type );
512 void Initialize();
513 };
514
515
516 class CDFile
517 {
518 public:
519 const Project& project;
520 const XMLElement& node;
521 std::string name;
522 std::string base;
523 std::string nameoncd;
524 std::string path;
525
526 CDFile ( const Project& project,
527 const XMLElement& bootstrapNode,
528 const std::string& path );
529 ~CDFile ();
530 void ProcessXML();
531 std::string GetPath () const;
532 };
533
534
535 class InstallFile
536 {
537 public:
538 const Project& project;
539 const XMLElement& node;
540 std::string name;
541 std::string base;
542 std::string newname;
543 std::string path;
544
545 InstallFile ( const Project& project,
546 const XMLElement& bootstrapNode,
547 const std::string& path );
548 ~InstallFile ();
549 void ProcessXML ();
550 std::string GetPath () const;
551 };
552
553
554 class PchFile
555 {
556 public:
557 const XMLElement& node;
558 const Module& module;
559 std::string header;
560
561 PchFile (
562 const XMLElement& node,
563 const Module& module,
564 const std::string& header );
565 void ProcessXML();
566 };
567
568
569 class StubbedComponent
570 {
571 public:
572 const Module* module;
573 const XMLElement& node;
574 std::string name;
575 std::vector<StubbedSymbol*> symbols;
576
577 StubbedComponent ( const Module* module_,
578 const XMLElement& stubbedComponentNode );
579 ~StubbedComponent ();
580 void ProcessXML ();
581 void ProcessXMLSubElement ( const XMLElement& e );
582 };
583
584
585 class StubbedSymbol
586 {
587 public:
588 const XMLElement& node;
589 std::string symbol;
590 std::string newname;
591
592 StubbedSymbol ( const XMLElement& stubbedSymbolNode );
593 ~StubbedSymbol ();
594 void ProcessXML();
595 };
596
597 extern std::string
598 FixSeparator ( const std::string& s );
599
600 extern std::string
601 ReplaceExtension (
602 const std::string& filename,
603 const std::string& newExtension );
604
605 extern std::string
606 GetSubPath (
607 const std::string& location,
608 const std::string& path,
609 const std::string& att_value );
610
611 extern std::string
612 GetExtension ( const std::string& filename );
613
614 extern std::string
615 GetDirectory ( const std::string& filename );
616
617 extern std::string
618 NormalizeFilename ( const std::string& filename );
619
620 #endif /* __RBUILD_H */