OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [ecos-3.0/] [host/] [tools/] [configtool/] [common/] [common/] [build.cxx] - Blame information for rev 786

Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.