Tools/udibuild - Implemented linking
[tpg/acess2.git] / Tools / udibuild / src / build.c
1 /*
2  * udibuild - UDI Compilation Utility
3  * - By John Hodge (thePowersGang)
4  *
5  * build.c
6  * - Compilation functions
7  */
8 #include <stdlib.h>
9 #include <stdio.h>
10 #include <string.h>
11 #include <sys/stat.h>   // mkdir
12 #include "include/build.h"
13 #include "include/common.h"
14
15 // === PROTOTYPES ===
16 char    *get_objfile(tIniFile *opts, const char *abi, const char *srcfile);
17
18 // === CODE ===
19 int Build_CompileFile(tIniFile *opts, const char *abi, tUdiprops *udiprops, tUdiprops_Srcfile *srcfile)
20 {
21         // Select compiler from opts [abi]
22         const char *cc_prog = IniFile_Get(opts, abi, "CC", NULL);
23         if( !cc_prog ) {
24                 fprintf(stderr, "No 'CC' defined for ABI %s\n", abi);
25                 return -1;
26         }
27         
28         // Build up compiler's command line
29         // - Include CFLAGS from .ini file
30         // - defines from udiprops
31         // - Object file is srcfile with .o appended
32         //  > Place in 'obj/' dir?
33         char *objfile = get_objfile(opts, abi, srcfile->Filename);
34         char *cmd = mkstr("%s -DUDI_ABI_is_%s %s %s -c %s -o %s",
35                 cc_prog,
36                 abi,
37                 IniFile_Get(opts, abi, "CFLAGS", ""),
38                 srcfile->CompileOpts ? srcfile->CompileOpts : "",
39                 srcfile->Filename, objfile);
40         printf("--- Compiling: %s\n", srcfile->Filename);
41          int rv = system(cmd);
42         free(cmd);
43         free(objfile);
44         
45         return rv;
46 }
47
48 int Build_LinkObjects(tIniFile *opts, const char *abi, tUdiprops *udiprops)
49 {
50         const char *linker = IniFile_Get(opts, abi, "LD", NULL);
51         if( !linker ) {
52                 fprintf(stderr, "No 'LD' defined for ABI %s\n", abi);
53                 return -1;
54         }
55
56         char    *objfiles[udiprops->nSourceFiles];
57         size_t  objfiles_len = 0;
58         for( int i = 0; i < udiprops->nSourceFiles; i ++ ) {
59                 objfiles[i] = get_objfile(opts, abi, udiprops->SourceFiles[i]->Filename);
60                 objfiles_len += strlen(objfiles[i])+1;
61         }
62         
63         // Create command string
64         char *objfiles_str = malloc(objfiles_len);
65         objfiles_len = 0;
66         for( int i = 0; i < udiprops->nSourceFiles; i ++ ) {
67                 strcpy(objfiles_str + objfiles_len, objfiles[i]);
68                 objfiles_len += strlen(objfiles[i])+1;
69                 objfiles_str[objfiles_len-1] = ' ';
70                 free( objfiles[i] );
71         }
72         objfiles_str[objfiles_len-1] = '\0';
73
74         mkdir("bin", 0755);
75         char *abidir = mkstr("bin/%s", abi);
76         mkdir(abidir, 0755);
77         free(abidir);
78         
79         char *cmd = mkstr("%s %s -o bin/%s/%s -r %s",
80                 linker, IniFile_Get(opts, abi, "LDFLAGS", ""),
81                 abi, udiprops->ModuleName, objfiles_str);
82         printf("--- Linking: bin/%s/%s\n", abi, udiprops->ModuleName);
83         int rv = system(cmd);
84         free(cmd);
85         free(objfiles_str);
86
87         return rv;
88 }
89
90 char *get_objfile(tIniFile *opts, const char *abi, const char *srcfile)
91 {
92         return mkstr("%s.o", srcfile);
93 }
94

UCC git Repository :: git.ucc.asn.au