2 * udibuild - UDI Compilation Utility
3 * - By John Hodge (thePowersGang)
6 * - Compilation functions
11 #include <sys/stat.h> // mkdir
12 #include <unistd.h> // unlink
13 #include "include/build.h"
14 #include "include/common.h"
17 char *get_objfile(tIniFile *opts, const char *abi, const char *srcfile);
18 char *get_udipropsfile(tIniFile *opts, const char *abi);
21 int Build_CompileFile(tIniFile *opts, const char *abi, tUdiprops *udiprops, tUdiprops_Srcfile *srcfile)
23 // Select compiler from opts [abi]
24 const char *cc_prog = IniFile_Get(opts, abi, "CC", NULL);
26 fprintf(stderr, "No 'CC' defined for ABI %s\n", abi);
30 // Build up compiler's command line
31 // - Include CFLAGS from .ini file
32 // - defines from udiprops
33 // - Object file is srcfile with .o appended
34 // > Place in 'obj/' dir?
35 char *objfile = get_objfile(opts, abi, srcfile->Filename);
36 char *cmd = mkstr("%s -DUDI_ABI_is_%s %s %s -c %s -o %s",
39 IniFile_Get(opts, abi, "CFLAGS", ""),
40 srcfile->CompileOpts ? srcfile->CompileOpts : "",
41 srcfile->Filename, objfile);
42 printf("--- Compiling: %s\n", srcfile->Filename);
50 int Build_CreateUdiprops(tIniFile *opts, const char *abi, tUdiprops *udiprops)
52 const char *cc_prog = IniFile_Get(opts, abi, "CC", NULL);
54 char *filename = get_udipropsfile(opts, abi);
55 FILE *fp = fopen(filename, "w");
56 fprintf(fp, "char udiprops[] __attribute__((section(\".udiprops\"))) = \n");
57 for( int i = 0; i < udiprops->nLines; i ++ ) {
58 // TODO: Escape " in string
59 fprintf(fp, " \"%s\"\n", udiprops->Lines[i]);
64 char *cmd = mkstr("%s %s -c %s -o %s.o",
65 cc_prog, IniFile_Get(opts, abi, "CFLAGS", ""),
76 int Build_LinkObjects(tIniFile *opts, const char *abi, tUdiprops *udiprops)
78 const char *linker = IniFile_Get(opts, abi, "LD", NULL);
80 fprintf(stderr, "No 'LD' defined for ABI %s\n", abi);
84 char *objfiles[udiprops->nSourceFiles];
85 size_t objfiles_len = 0;
86 for( int i = 0; i < udiprops->nSourceFiles; i ++ ) {
87 objfiles[i] = get_objfile(opts, abi, udiprops->SourceFiles[i]->Filename);
88 objfiles_len += strlen(objfiles[i])+1;
91 // Create command string
92 char *objfiles_str = malloc(objfiles_len);
94 for( int i = 0; i < udiprops->nSourceFiles; i ++ ) {
95 strcpy(objfiles_str + objfiles_len, objfiles[i]);
96 objfiles_len += strlen(objfiles[i])+1;
97 objfiles_str[objfiles_len-1] = ' ';
100 objfiles_str[objfiles_len-1] = '\0';
103 char *abidir = mkstr("bin/%s", abi);
107 char *udiprops_c = get_udipropsfile(opts, abi);
108 char *cmd = mkstr("%s -r %s -o bin/%s/%s -s %s",// %s.o",
109 linker, IniFile_Get(opts, abi, "LDFLAGS", ""),
110 abi, udiprops->ModuleName, objfiles_str, udiprops_c
112 printf("--- Linking: bin/%s/%s\n", abi, udiprops->ModuleName);
114 int rv = system(cmd);
122 char *get_objfile(tIniFile *opts, const char *abi, const char *srcfile)
124 return mkstr("%s.o", srcfile);
127 char *get_udipropsfile(tIniFile *opts, const char *abi)
129 return mkstr(".udiprops.c");