10f4638d51473d8028e124945e62fd36620043de
[reactos.git] / reactos / tools / rbuild / backend / mingw / proxymakefile.cpp
1 /*
2 * Copyright (C) 2005 Casper S. Hornstrup
3 *
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.
8 *
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.
13 *
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.
17 */
18 #include "../../pch.h"
19
20 #include "mingw.h"
21 #include <assert.h>
22
23 using std::string;
24 using std::vector;
25
26 ProxyMakefile::ProxyMakefile ( const Project& project )
27 : project ( project )
28 {
29 }
30
31 ProxyMakefile::~ProxyMakefile ()
32 {
33 }
34
35 bool
36 ProxyMakefile::GenerateProxyMakefile ( Module& module )
37 {
38 return module.GenerateInOutputTree ();
39 }
40
41 void
42 ProxyMakefile::GenerateProxyMakefiles ( bool verbose,
43 string outputTree )
44 {
45 for ( size_t i = 0; i < project.modules.size (); i++ )
46 {
47 Module& module = *project.modules[i];
48 if ( !module.enabled )
49 continue;
50 if ( !GenerateProxyMakefile ( module ) )
51 continue;
52 GenerateProxyMakefileForModule ( module,
53 verbose,
54 outputTree );
55 }
56 }
57
58 string
59 ProxyMakefile::GeneratePathToParentDirectory ( int numberOfParentDirectories )
60 {
61 string path = "";
62 for ( int i = 0; i < numberOfParentDirectories; i++ )
63 {
64 if ( path != "" )
65 path += SSEP;
66 path += "..";
67 }
68 return path;
69 }
70
71 string
72 ProxyMakefile::GetPathToTopDirectory ( Module& module )
73 {
74 int numberOfDirectories = 1;
75 string basePath = NormalizeFilename ( module.GetBasePath () );
76 for ( size_t i = 0; i < basePath.length (); i++ )
77 {
78 if ( basePath[i] == CSEP )
79 numberOfDirectories++;
80 }
81 return GeneratePathToParentDirectory ( numberOfDirectories );
82 }
83
84 void
85 ProxyMakefile::GenerateProxyMakefileForModule ( Module& module,
86 bool verbose,
87 string outputTree )
88 {
89 char* buf;
90 char* s;
91
92 if ( verbose )
93 {
94 printf ( "\nGenerating proxy makefile for %s",
95 module.name.c_str () );
96 }
97
98 string base;
99 string pathToTopDirectory;
100 if ( outputTree.length () > 0 )
101 {
102 base = outputTree + SSEP + module.GetBasePath ();
103 Path path;
104 pathToTopDirectory = working_directory;
105 }
106 else
107 {
108 base = module.GetBasePath ();
109 pathToTopDirectory = GetPathToTopDirectory ( module );
110 }
111 string proxyMakefile = NormalizeFilename ( base + SSEP "makefile" );
112 string defaultTarget = module.name;
113
114 buf = (char*) malloc ( 10*1024 );
115 if ( buf == NULL )
116 throw OutOfMemoryException ();
117
118 s = buf;
119 s = s + sprintf ( s, "# This file is automatically generated.\n" );
120 s = s + sprintf ( s, "\n" );
121 s = s + sprintf ( s, "TOP = %s\n", pathToTopDirectory.c_str () );
122 s = s + sprintf ( s, "DEFAULT = %s\n", defaultTarget.c_str () );
123 s = s + sprintf ( s, "include $(TOP)/proxy.mak\n" );
124
125 FileSupportCode::WriteIfChanged ( buf, proxyMakefile );
126
127 free ( buf );
128 }