]> git.kernelconcepts.de Git - karo-tx-redboot.git/blob - tools/src/tools/configtool/common/common/build.cxx
Initial revision
[karo-tx-redboot.git] / tools / src / tools / configtool / common / common / build.cxx
1 //####COPYRIGHTBEGIN####
2 //                                                                          
3 // ----------------------------------------------------------------------------
4 // Copyright (C) 1998, 1999, 2000, 2002 Red Hat, Inc.
5 //
6 // This program is part of the eCos host tools.
7 //
8 // This program is free software; you can redistribute it and/or modify it 
9 // under the terms of the GNU General Public License as published by the Free 
10 // Software Foundation; either version 2 of the License, or (at your option) 
11 // any later version.
12 // 
13 // This program is distributed in the hope that it will be useful, but WITHOUT 
14 // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 
15 // FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for 
16 // more details.
17 // 
18 // You should have received a copy of the GNU General Public License along with
19 // this program; if not, write to the Free Software Foundation, Inc., 
20 // 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
21 //
22 // ----------------------------------------------------------------------------
23 //                                                                          
24 //####COPYRIGHTEND####
25 //==========================================================================
26 //
27 //      build.cxx
28 //
29 //      The implementation of build tree and makefile generation using
30 //      CDL data
31 //
32 //==========================================================================
33 //==========================================================================
34 //#####DESCRIPTIONBEGIN####                                             
35 //
36 // Author(s):           jld
37 // Date:                1999-11-08
38 //
39 //####DESCRIPTIONEND####
40 //==========================================================================
41
42 #if defined(_WIN32) || defined(__CYGWIN__)
43         #include <windows.h> /* for GetShortPathNameA() */
44 #endif
45 #ifdef __CYGWIN__
46         #include <sys/cygwin.h> /* for cygwin_conv_to_posix_path() */
47 #endif
48 #ifdef __WXMSW__
49 // We take advantage of wxWindows' recursive wxFileName::Mkdir function
50 // to workaround a bug in Tcl on Windows 9x
51 #include "wx/filename.h"
52 #  ifdef new
53 #    undef new
54 #  endif
55 #endif
56 #include "flags.hxx"
57 #include "build.hxx"
58
59 // Two methods of generating Cygwin filenames
60 // ECOS_USE_CYGDRIVE = 0: use e.g. //c/, but this is deprecated in new versions of Cygwin
61 // ECOS_USE_CYGDRIVE = 1: use e.g. /cygdrive/c/
62 // ECOS_USE_CYGDRIVE = 2: use e.g. c:/ notation
63 // ECOS_USE_CYGDRIVE = 3: use e.g. /ecos-x notation where x is a drive name.
64 #define ECOS_USE_CYGDRIVE 3
65
66 // Use registry functions to find out location of /cygdrive
67 #define ECOS_USE_REGISTRY 1
68
69 // Don't use Reg... functions in command-line version until we know how
70 // to add advapi32.lib
71 #if defined(_WIN32) && !defined(__WXMSW__) && !defined(ECOS_CT)
72 #undef ECOS_USE_REGISTRY
73 #define ECOS_USE_REGISTRY 0
74 #endif
75
76 std::string makefile_header = "# eCos makefile\n\n# This is a generated file - do not edit\n\n";
77
78 // This code seems to crash Tcl under Windows ME and Linux, so
79 // disable for now. What should the criterion be? TCL version?
80 #define SET_STDOUT_TO_NULL 0
81
82 bool eval_tcl_command (const std::string command) {
83         Tcl_Interp * interp = Tcl_CreateInterp ();
84 #if SET_STDOUT_TO_NULL
85         Tcl_Channel outchan = Tcl_OpenFileChannel (interp, "nul", "a+", 777);
86         Tcl_SetStdChannel (outchan, TCL_STDOUT); // direct standard output to the null device
87 #endif
88         int nStatus = Tcl_Eval (interp, CDL_TCL_CONST_CAST(char*, command.c_str()));
89 #if SET_STDOUT_TO_NULL
90         Tcl_SetStdChannel (NULL, TCL_STDOUT);
91         Tcl_UnregisterChannel (interp, outchan);
92 #endif
93         Tcl_DeleteInterp (interp);
94         return (TCL_OK == nStatus);
95 }
96
97 // generate a copy of a string where each occurance of a specified char is replaced with another char
98 std::string replace_char (const std::string input, const char old_char, const char new_char) {
99         std::string output;
100         for (unsigned int n = 0; n < input.size (); n++) { // for each char
101                 output += (old_char == input [n]) ? new_char : input [n]; // convert during copy
102         }
103         return output;
104 }
105
106 // convert a filepath into a vector of path components
107 static void path_to_vector (std::string input, std::vector <std::string> & output) {
108         std::string component;
109         output.clear ();
110
111         for (unsigned int n = 0; n < input.size (); n++) { // for each char in the path
112                 if (('/' == input [n]) || ('\\' == input [n])) { // if char is a directory separator
113                         output.push_back (component); // add path component to output vector
114                         component.erase (); // clear path component string
115                 } else { // char is not a separator
116                         component += input [n]; // add char to path component string
117                 }
118         }
119         output.push_back (component); // add final path component to output vector
120 }
121
122 // eliminate spaces from a DOS filepath by substituting the
123 // short form of path components containing spaces
124 #if defined(_WIN32) || defined(__CYGWIN__)
125 std::string nospace_path (const std::string input) {
126         // split the path into a vector of path components
127         std::vector <std::string> long_path_vector;
128         path_to_vector (input, long_path_vector);
129
130         // convert the path to its short form and split
131         // the result into a vector of path components
132         char buffer [MAX_PATH + 1];
133         GetShortPathNameA (input.c_str (), buffer, sizeof (buffer));
134         std::vector <std::string> short_path_vector;
135         path_to_vector (buffer, short_path_vector);
136
137         // append the short or long form of each path component to the output string as appropriate
138         std::string output;
139         for (unsigned int n = 0; n < long_path_vector.size (); n++) { // for each component of the path
140                 if (long_path_vector [n].end () != std::find (long_path_vector [n].begin (), long_path_vector [n].end (), ' ')) { // if there is a space in the path component
141                         output += short_path_vector [n]; // add the short form of the path component
142                 } else { // there is no space in the path component
143                         output += long_path_vector [n]; // add the long form of the path component
144                 }
145                 output += '\\'; // add a directory separator
146         }
147         output.resize (output.size () - 1); // remove the trailing separator
148
149         return output;
150 }
151 #endif
152
153 // convert a DOS filepath to a Cygwin filepath
154 std::string cygpath (const std::string input) {
155 #if defined(_WIN32) || defined(__CYGWIN__)
156         // remove spaces from the DOS filepath
157         const std::string path = nospace_path (input);
158         std::string output;
159
160         // convert the DOS filepath to Cygwin notation - using Cygwin if available
161     // 2001-10-15: should now do the same thing under Cygwin as under VC++, namely
162     // use the /ecos-x form.
163 #if 0 // def __CYGWIN__
164         char buffer [MAX_PATH + 1];
165         cygwin_conv_to_posix_path (path.c_str (), buffer);
166         output = buffer;
167 #else
168
169 #if ECOS_USE_CYGDRIVE == 1
170     std::string strCygdrive("/cygdrive");
171
172 #if ECOS_USE_REGISTRY
173     HKEY hKey = 0;
174     if (ERROR_SUCCESS == RegOpenKeyEx(HKEY_CURRENT_USER, "Software\\Cygnus Solutions\\Cygwin\\mounts v2",
175         0, KEY_READ, &hKey))
176     {
177         DWORD type;
178         BYTE value[256];
179         DWORD sz;
180         if (ERROR_SUCCESS == RegQueryValueEx(hKey, "cygdrive prefix", NULL, & type, value, & sz))
181         {
182             strCygdrive = (const char*) value;
183         }
184
185         RegCloseKey(hKey);
186     }
187 #endif
188     strCygdrive = strCygdrive + "/";
189
190         for (unsigned int n = 0; n < path.size (); n++) { // for each char
191                 if ((1 == n) && (':' == path [n])) { // if a DOS logical drive letter is present
192                         output = strCygdrive + output; // convert to Cygwin notation
193                 } else {
194                         output += ('\\' == path [n]) ? '/' : path [n]; // convert backslash to slash
195                 }
196         }
197 #elif ECOS_USE_CYGDRIVE == 2
198     // Convert to c:/foo/bar notation
199         for (unsigned int n = 0; n < path.size (); n++) { // for each char
200                         output += ('\\' == path [n]) ? '/' : path [n]; // convert backslash to slash
201         }
202 #elif ECOS_USE_CYGDRIVE == 3
203     // Convert to /ecos-x notation, assuming that this mount point will be created
204     // by the application.
205
206     std::string output1;
207
208     if (path.size() > 1 && path[1] == ':')
209     {
210         output1 = "/ecos-";
211         output1 += tolower(path[0]);
212         output1 += "/";
213
214         // Append the rest of the path
215         if (path.size() > 2)
216         {
217             unsigned int n = 2;
218             unsigned int i;
219
220             if (path[n] == '\\' || path[n] == '/')
221                 n ++;
222             
223             for (i = n; i < path.size(); i++)
224                 output1 += path[i];
225         }
226     }
227     else
228         output1 = path;
229
230     for (unsigned int n = 0; n < output1.size (); n++) { // for each char
231         output += ('\\' == output1 [n]) ? '/' : output1 [n]; // convert backslash to slash
232         }
233 #else
234         for (unsigned int n = 0; n < path.size (); n++) { // for each char
235                 if ((1 == n) && (':' == path [n])) { // if a DOS logical drive letter is present
236                         output = "//" + output; // convert to Cygwin notation
237                 } else {
238                         output += ('\\' == path [n]) ? '/' : path [n]; // convert backslash to slash
239                 }
240         }
241 #endif
242     // ECOS_USE_CYGDRIVE
243 #endif
244         return output;
245 #else
246         return input;
247 #endif
248 }
249
250 // create a directory
251 bool create_directory (const std::string directory) {
252 // We take advantage of wxWindows' recursive wxFileName::Mkdir function
253 // to workaround a bug in Tcl on Windows 9x
254 #if defined(__WXMSW__)
255     if (wxDirExists(directory.c_str()))
256         return TRUE;
257     return wxFileName::Mkdir(directory.c_str(), 0777, TRUE);
258 #else
259     return eval_tcl_command ("file mkdir \"" + directory + "\"");
260 #endif
261 }
262
263 // copy a file
264 bool copy_file (const std::string file, const std::string destination) {
265         return eval_tcl_command ("file copy \"" + file + "\" \"" + destination + "\"");
266         return true;
267 }
268
269 // returns the directory of the specified file
270 std::string file_to_directory (const std::string file) {
271         for (unsigned int n = file.size (); n >= 0; n--) {
272                 if ('/' == file [n]) {
273                         std::string directory = file;
274                         directory.resize (n);
275                         return directory;
276                 }
277         }
278         return "";
279 }
280
281 std::string tab_indent (const std::string input) {
282         std::string output;
283         bool indent = true;
284         for (unsigned int n = 0; n < input.size (); n++) {
285                 if (indent) {
286                         output += '\t';
287                         indent = false;
288                 } else {
289                         indent = ('\n' == input [n]);
290                 }
291                 output += input [n];
292         }
293         return output;
294 }
295
296 // return the tests of the specified loadable
297 std::string get_tests (const CdlConfiguration config, const CdlBuildInfo_Loadable & build_info) {
298         CdlValuable tests = dynamic_cast <CdlValuable> (config->lookup (build_info.name + "_TESTS"));
299     // check if there are tests active and enabled
300         if (tests && tests->is_active() && tests->is_enabled()) {
301                 return tests->get_value ();
302         } else { // there are no tests
303                 return "";
304         }
305 }
306
307 // replaces all occurances of a substring with a new substring
308 std::string replace_substr (const std::string input, const std::string old_substring, const std::string new_substring) {
309         std::string output = input;
310         std::string::size_type index = 0;
311         while (index = output.find (old_substring, index), std::string::npos != index) {
312                 output.replace (index, old_substring.size (), new_substring);
313         }
314         return output;
315 }
316
317 // resolve tokens in custom make rule targets and dependencies
318 std::string resolve_tokens (const std::string input) {
319         std::string output = input;
320         output = replace_substr (output, "<PREFIX>", "$(PREFIX)");
321         output = replace_substr (output, "<PACKAGE>", "$(REPOSITORY)/$(PACKAGE)");
322         return output;
323 }
324
325 // create the makefile for a loadable
326 bool generate_makefile (const CdlConfiguration config, const CdlBuildInfo_Loadable & info, const std::string install_tree, const std::string filename) {
327         unsigned int count;
328         unsigned int library;
329
330         // obtain the command prefix
331         std::string command_prefix = get_flags (config, NULL, "COMMAND_PREFIX");
332         if (! command_prefix.empty ()) { // if there is a command prefix
333                 command_prefix += '-'; // add a trailing hyphen
334         }
335
336         // generate the prefix for archived objects
337         unsigned int final_separator = 0; // the index of the last directory separator
338         std::string object_prefix = info.directory; // start with the loadable directory
339         for (count = 0; count < object_prefix.size (); count++) { // for each char
340                 if ('/' == object_prefix [count]) { // if the char is a directory separator
341                         object_prefix [count] = '_'; // replace the char with an underscore
342                         final_separator = count;
343                 }
344         }
345         object_prefix.resize (final_separator); // remove the version directory
346         
347         // open the makefile
348         FILE * stream = fopen (filename.c_str (), "wt");
349         if (stream == NULL) // if the file could not be opened
350                 return false;
351
352         // generate the header
353         fprintf (stream, makefile_header.c_str ());
354
355         // generate the global variables
356         fprintf (stream, "export REPOSITORY := %s\n", cygpath (config->get_database ()->get_component_repository ()).c_str ());
357         fprintf (stream, "export PREFIX := %s\n", cygpath (install_tree).c_str ());
358         fprintf (stream, "export COMMAND_PREFIX := %s\n", command_prefix.c_str ());
359         fprintf (stream, "export CC := $(COMMAND_PREFIX)gcc\n");
360         fprintf (stream, "export OBJCOPY := $(COMMAND_PREFIX)objcopy\n");
361 #if defined(_WIN32) || defined(__CYGWIN__)
362     fprintf (stream, "export HOST := CYGWIN\n");
363 #else
364     fprintf (stream, "export HOST := UNIX\n");
365 #endif
366         fprintf (stream, "export AR := $(COMMAND_PREFIX)ar\n\n");
367
368         // generate the package variables
369         fprintf (stream, "PACKAGE := %s\n", info.directory.c_str ());
370         fprintf (stream, "OBJECT_PREFIX := %s\n", object_prefix.c_str ());
371         fprintf (stream, "CFLAGS := %s\n", get_flags (config, &info, "CFLAGS").c_str ());
372         fprintf (stream, "LDFLAGS := %s\n", get_flags (config, &info, "LDFLAGS").c_str ());
373         fprintf (stream, "VPATH := $(REPOSITORY)/$(PACKAGE)\n");
374         fprintf (stream, "INCLUDE_PATH := $(INCLUDE_PATH) -I$(PREFIX)/include $(foreach dir,$(VPATH),-I$(dir) -I$(dir)/src -I$(dir)/tests) -I.\n");
375         fprintf (stream, "MLT := $(wildcard $(REPOSITORY)/$(PACKAGE)/include/pkgconf/mlt*.ldi $(REPOSITORY)/$(PACKAGE)/include/pkgconf/mlt*.h)\n");
376         fprintf (stream, "TESTS := %s\n\n", get_tests (config, info).c_str ());
377
378         // create a vector of libraries
379         std::vector <std::string> library_vector;
380         for (count = 0; count < info.compiles.size (); count++) { // for each compilable file
381                 const std::string & library = info.compiles [count].library;
382                 if (library_vector.end () == std::find (library_vector.begin (), library_vector.end (), library)) { // if a new library
383                         library_vector.push_back (library); // add the library to the vector
384                 }
385         }
386         for (count = 0; count < info.objects.size (); count++) { // for each object file
387                 const std::string & library = info.objects [count].library;
388                 if (library_vector.end () == std::find (library_vector.begin (), library_vector.end (), library)) { // if a new library
389                         library_vector.push_back (library); // add the library to the vector
390                 }
391         }
392         for (count = 0; count < info.make_objects.size (); count++) { // for each make object
393                 const std::string & library = info.make_objects [count].library;
394                 if (library_vector.end () == std::find (library_vector.begin (), library_vector.end (), library)) { // if a new library
395                         library_vector.push_back (library); // add the library to the vector
396                 }
397         }
398
399         // generate the default rule
400         fprintf (stream, "build: headers");
401         for (library = 0; library < library_vector.size (); library++) { // for each library
402                 fprintf (stream, " %s.stamp", library_vector [library].c_str ());
403         }
404         fprintf (stream, "\n\n");
405
406         // generate library rules
407         for (library = 0; library < library_vector.size (); library++) { // for each library
408                 fprintf (stream, "LIBRARY := %s\n", library_vector [library].c_str ());
409                 fprintf (stream, "COMPILE :=");
410                 for (count = 0; count < info.compiles.size (); count++) { // for each compilable file
411                         if (library_vector [library] == info.compiles [count].library) { // if the file and library are related
412                                 fprintf (stream, " %s", info.compiles [count].source.c_str ());
413                         }
414                 }
415                 for (count = 0; count < info.objects.size (); count++) { // for each object file
416                         if (library_vector [library] == info.objects [count].library) { // if the file and library are related
417                                 fprintf (stream, " %s", info.objects [count].object.c_str ());
418                         }
419                 }
420                 for (count = 0; count < info.make_objects.size (); count++) { // for each make object
421                         if (library_vector [library] == info.make_objects [count].library) { // if the object and library are related
422                                 fprintf (stream, " %s", info.make_objects [count].object.c_str ());
423                         }
424                 }
425
426                 fprintf (stream, "\nOBJECTS := $(COMPILE:.cxx=.o.d)\n");
427                 fprintf (stream, "OBJECTS := $(OBJECTS:.c=.o.d)\n");
428                 fprintf (stream, "OBJECTS := $(OBJECTS:.S=.o.d)\n\n");
429                 fprintf (stream, "$(LIBRARY).stamp: $(OBJECTS)\n");
430                 fprintf (stream, "\t$(AR) rcs $(PREFIX)/lib/$(@:.stamp=) $(foreach obj,$?,$(dir $(obj))$(OBJECT_PREFIX)_$(notdir $(obj:.o.d=.o)))\n");
431                 fprintf (stream, "\t@cat $^ > $(@:.stamp=.deps)\n");
432                 fprintf (stream, "\t@touch $@\n\n");
433         }       
434
435         // generate make objects rules
436         for (count = 0; count < info.make_objects.size (); count++) { // for each make object
437                 fprintf (stream, "%s: %s\n%s\n", info.make_objects [count].object.c_str (), info.make_objects [count].deps.c_str (), tab_indent (info.make_objects [count].rules).c_str ());
438         }
439
440         // generate makes rules
441         for (count = 0; count < info.makes.size (); count++) { // for each make
442                 fprintf (stream, "%s: $(wildcard %s)\n%s\n", resolve_tokens (info.makes [count].target).c_str (), resolve_tokens (info.makes [count].deps).c_str (), tab_indent (info.makes [count].rules).c_str ());
443         }
444
445         // generate header rules
446         fprintf (stream, "headers: mlt_headers");
447         for (count = 0; count < info.headers.size (); count++) { // for each header
448                 fprintf (stream, " $(PREFIX)/include/%s", info.headers [count].destination.c_str ());
449         }
450         fprintf (stream, "\n\n");
451         for (count = 0; count < info.headers.size (); count++) { // for each header
452                 fprintf (stream, "$(PREFIX)/include/%s: $(REPOSITORY)/$(PACKAGE)/%s\n", info.headers [count].destination.c_str (), info.headers [count].source.c_str ());
453 #if (defined(_WIN32) || defined(__CYGWIN__)) && (ECOS_USE_CYGDRIVE > 0)
454         fprintf (stream, "ifeq ($(HOST),CYGWIN)\n");
455             fprintf (stream, "\t@mkdir -p `cygpath -w \"$(dir $@)\" | sed \"s@\\\\\\\\\\\\\\\\@/@g\"`\n");
456         fprintf (stream, "else\n");
457             fprintf (stream, "\t@mkdir -p $(dir $@)\n");
458         fprintf (stream, "endif\n");
459 #else
460         // This prevents older versions of mkdir failing
461         fprintf (stream, "\t@mkdir -p $(dir $@)\n");
462 #endif
463
464                 fprintf (stream, "\t@cp $< $@\n");
465                 fprintf (stream, "\t@chmod u+w $@\n\n");
466         }
467
468         // include default rules
469         fprintf (stream, "include $(REPOSITORY)/pkgconf/rules.mak\n\n");
470
471         // close the makefile
472         return (0 == fclose (stream));
473 }
474
475 // a structure and operator for sorting custom make rules by priority
476 struct info_make {
477         std::vector <CdlBuildInfo_Loadable>::const_iterator loadable;
478         unsigned int make;
479 };
480 bool operator < (info_make op1, info_make op2) {
481         return op1.loadable->makes [op1.make].priority < op2.loadable->makes [op2.make].priority;
482 }
483
484 // create the top-level makefile
485 bool generate_toplevel_makefile (const CdlConfiguration config, const std::string install_tree, const std::string filename) {
486         unsigned int loadable;
487         unsigned int make;
488
489         // obtain the command prefix
490         std::string command_prefix = get_flags (config, NULL, "COMMAND_PREFIX");
491         if (! command_prefix.empty ()) { // if there is a command prefix
492                 command_prefix += '-'; // add a trailing hyphen
493         }
494
495         // obtain build information from the specified configuration
496         CdlBuildInfo build_info;
497         config->get_build_info (build_info);
498         std::vector <CdlBuildInfo_Loadable> info_vector = build_info.entries;
499
500         // create a vector of makes and sort them by priority
501         std::vector <info_make> info_make_vector;
502         for (std::vector <CdlBuildInfo_Loadable>::iterator info = info_vector.begin (); info != info_vector.end (); info++) { // for each buildable loaded package
503                 for (unsigned int count = 0; count < info->makes.size (); count++) { // for each make
504                         info_make make_info = {info, count};
505                         info_make_vector.push_back (make_info);
506                 }
507         }
508         std::sort (info_make_vector.begin (), info_make_vector.end ());
509
510         // open the makefile
511         FILE * stream = fopen (filename.c_str (), "wt");
512         if (stream == NULL) // if the file could not be opened
513                 return false;
514
515         // generate the header
516         fprintf (stream, makefile_header.c_str ());
517
518         // generate the variables
519         fprintf (stream, "export REPOSITORY := %s\n", cygpath (config->get_database ()->get_component_repository ()).c_str ());
520 #if defined(_WIN32) || defined(__CYGWIN__)
521     fprintf (stream, "export HOST := CYGWIN\n");
522 #else
523     fprintf (stream, "export HOST := UNIX\n");
524 #endif
525         fprintf (stream, "export PREFIX := %s\n", cygpath (install_tree).c_str ());
526         fprintf (stream, "export COMMAND_PREFIX := %s\n", command_prefix.c_str ());
527         fprintf (stream, "export CC := $(COMMAND_PREFIX)gcc\n");
528         fprintf (stream, "export OBJCOPY := $(COMMAND_PREFIX)objcopy\n");
529         fprintf (stream, "export AR := $(COMMAND_PREFIX)ar\n\n");
530
531     // generate the makefile contents
532         fprintf (stream, ".PHONY: default build clean tests headers\n\n");
533
534         fprintf (stream, "build: headers $(PREFIX)/include/pkgconf/ecos.mak\n");
535         for (make = 0; make < info_make_vector.size (); make++) { // for each make
536                 if (info_make_vector [make].loadable->makes [info_make_vector [make].make].priority < 100) { // if priority higher than default complilation
537                         fprintf (stream, "\t$(MAKE) -r -C %s %s\n", info_make_vector [make].loadable->directory.c_str (), resolve_tokens (info_make_vector [make].loadable->makes [info_make_vector [make].make].target).c_str ());
538                 }
539         }
540         for (loadable = 0; loadable < info_vector.size (); loadable++) { // for each buildable loaded package
541                 const std::string source_path = info_vector [loadable].directory;
542                 fprintf (stream, "\t$(MAKE) -r -C %s $@\n", source_path.c_str ());
543         }
544         for (make = 0; make < info_make_vector.size (); make++) { // for each make
545                 if (info_make_vector [make].loadable->makes [info_make_vector [make].make].priority >= 100) { // if priority lower than or equal to default complilation
546                         fprintf (stream, "\t$(MAKE) -r -C %s %s\n", info_make_vector [make].loadable->directory.c_str (), resolve_tokens (info_make_vector [make].loadable->makes [info_make_vector [make].make].target).c_str ());
547                 }
548         }
549         fprintf (stream, "\t@echo $@ finished\n\n");
550
551         fprintf (stream, "clean:\n");
552         for (loadable = 0; loadable < info_vector.size (); loadable++) { // for each buildable loaded package
553                 const std::string source_path = info_vector [loadable].directory;
554                 fprintf (stream, "\t$(MAKE) -r -C %s $@\n", source_path.c_str ());
555         }
556         fprintf (stream, "\t@echo $@ finished\n\n");
557
558         fprintf (stream, "tests: build\n");
559         for (loadable = 0; loadable < info_vector.size (); loadable++) { // for each buildable loaded package
560                 const std::string source_path = info_vector [loadable].directory;
561                 fprintf (stream, "\t$(MAKE) -r -C %s $@\n", source_path.c_str ());
562         }
563         fprintf (stream, "\t@echo $@ finished\n\n");
564
565         fprintf (stream, "headers:\n");
566         for (loadable = 0; loadable < info_vector.size (); loadable++) { // for each buildable loaded package
567                 const std::string source_path = info_vector [loadable].directory;
568                 fprintf (stream, "\t$(MAKE) -r -C %s $@\n", source_path.c_str ());
569         }
570         fprintf (stream, "\t@echo $@ finished\n\n");
571
572         fprintf (stream, "$(PREFIX)/include/pkgconf/ecos.mak: makefile\n");
573         fprintf (stream, "\t@echo 'ECOS_GLOBAL_CFLAGS = %s' > $@\n", get_flags (config, NULL, "CFLAGS").c_str ());
574         fprintf (stream, "\t@echo 'ECOS_GLOBAL_LDFLAGS = %s' >> $@\n", get_flags (config, NULL, "LDFLAGS").c_str ());
575         fprintf (stream, "\t@echo 'ECOS_COMMAND_PREFIX = $(COMMAND_PREFIX)' >> $@\n\n");
576
577         // close the makefile
578         return (0 == fclose (stream));
579 }
580
581 // generates the directory structure for the build and install trees
582 bool generate_build_tree (const CdlConfiguration config, const std::string build_tree, const std::string install_tree /* = "" */ ) {
583 #if defined(_WIN32) || defined(__CYGWIN__)
584         // convert backslash directory separators to forward slashes under Win32
585         const std::string build_dir = replace_char (build_tree, '\\', '/');
586         const std::string install_dir = install_tree.empty () ? build_dir + "/install" : replace_char (install_tree, '\\', '/');
587 #else
588         const std::string build_dir = build_tree;
589         const std::string install_dir = install_tree.empty () ? build_dir + "/install" : install_tree;
590 #endif
591
592         // create build and install directories to ensure they are in writable locations
593         if (! create_directory (build_dir))
594                 return false;
595         if (! create_directory (install_dir + "/lib"))
596                 return false;
597         if (! create_directory (install_dir + "/include/pkgconf"))
598                 return false;
599
600         // obtain build information from the specified configuration
601         CdlBuildInfo build_info;
602         config->get_build_info (build_info);
603         std::vector <CdlBuildInfo_Loadable> info_vector = build_info.entries;
604
605         for (unsigned int loadable = 0; loadable < info_vector.size (); loadable++) { // for each buildable loaded package
606                 const std::string build_dir_loadable = build_dir + "/" + info_vector [loadable].directory;
607
608                 // create loadable directory in build tree
609                 if (! create_directory (build_dir_loadable))
610                         return false;
611
612                 // generate makefile
613                 generate_makefile (config, info_vector [loadable], install_dir, build_dir_loadable + "/makefile");
614         }
615
616         generate_toplevel_makefile (config, install_dir, build_dir + "/makefile");
617
618         return true;
619 }