f933cc1bc5f21eec2b17bbe26bdad15daf3befdf
[tpg/acess2.git] / UDI / Tools / udibuild_src / main.c
1 /*
2  * udibuild - UDI Compilation Utility
3  * - By John Hodge (thePowersGang)
4  *
5  * main.c
6  * - Core
7  */
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <unistd.h>     // getopt
11 #include <getopt.h>
12 #include <string.h>     // strrchr
13 #include <assert.h>
14 #include "include/common.h"
15 #include "include/build.h"
16 #include "include/inifile.h"
17 #include "include/udiprops.h"
18
19 #define CONFIG_FILENAME "udibuild.ini"
20 #ifdef __ACESS__
21 #define RUNTIME_DIR     "/Acess/Conf/UDI"
22 #else
23 #define RUNTIME_DIR     "/etc/udi"
24 #endif
25
26 // === PROTOTYPES ===
27  int    main(int argc, char *argv[]);
28  int    ParseArguments(int argc, char *argv[]);
29 void    Usage(const char *progname);
30
31 // === GLOBALS ===
32 bool    gbTraceEnabled = false;
33 const char *gsRuntimeDir = RUNTIME_DIR;
34 const char *gsOpt_ConfigFile;
35 const char *gsOpt_WorkingDir;
36 const char *gsOpt_UdipropsFile;
37 const char *gsOpt_ABIName = "ia32";
38 tIniFile        *gpOptions;
39 tUdiprops       *gpUdipropsBuild;
40
41 // === CODE ===
42 int main(int argc, char *argv[])
43 {
44         if( ParseArguments(argc, argv) ) {
45                 return 1;
46         }
47
48         // Locate udibuild.ini
49         // 1. Check CWD
50         if( !gsOpt_ConfigFile ) {
51                 //if( file_exists("./udibuild.ini") )
52                 //{
53                 //      gsOpt_ConfigFile = "udibuild.ini";
54                 //}
55         }
56         // 2. Check program dir (if not invoked from PATH)
57         if( !gsOpt_ConfigFile && (argv[0][0] == '.' || argv[0][0] == '/') ) {
58                 char *last_slash = strrchr(argv[0], '/');
59                 if( last_slash ) {
60                         gsOpt_ConfigFile = mkstr("%.*s/%s",
61                                 last_slash-argv[0], argv[0], CONFIG_FILENAME);
62                 }
63                 //if( !file_exists(gsOpt_ConfigFile) ) {
64                 //      free(gsOpt_ConfigFile);
65                 //      gsOpt_ConfigFile = NULL;
66                 //}
67         }
68         // 3. Check ~/.config/udi/udibuild.ini
69         // 4. Check RUNTIME_DIR/udibuild.ini
70
71         // #. Oh well   
72         if( !gsOpt_ConfigFile ) {
73                 fprintf(stderr, "Can't locate "CONFIG_FILENAME" file, please specify using '-c'\n");
74                 exit(2);
75         }
76         
77         // Load udibuild.ini
78         gpOptions = IniFile_Load(gsOpt_ConfigFile);
79         assert(gpOptions);
80
81         // Change to working directory (-C <dir>)
82         if( gsOpt_WorkingDir )
83         {
84                 chdir(gsOpt_WorkingDir);
85         }
86
87         // Load udiprops
88         gpUdipropsBuild = Udiprops_LoadBuild( gsOpt_UdipropsFile ? gsOpt_UdipropsFile : "udiprops.txt" );
89         assert(gpUdipropsBuild);
90         assert(gpUdipropsBuild->SourceFiles);
91
92         // Do build
93         for( int i = 0; i < gpUdipropsBuild->nSourceFiles; i ++ )
94         {
95                 int rv = Build_CompileFile(gpOptions, gsOpt_ABIName, gpUdipropsBuild,
96                         gpUdipropsBuild->SourceFiles[i]);
97                 if( rv ) {
98                         fprintf(stderr, "*** Exit status: %i\n", rv);
99                         return rv;
100                 }
101         }
102         // Create file with `.udiprops` section
103         // - udimkpkg's job
104         //Build_CreateUdiprops(gpOptions, gsOpt_ABIName, gpUdipropsBuild);
105         // Link
106         Build_LinkObjects(gpOptions, gsOpt_ABIName, gpUdipropsBuild);
107
108         return 0;
109 }
110
111 int ParseArguments(int argc, char *argv[])
112 {
113          int    opt;
114         while( (opt = getopt(argc, argv, "hC:c:f:a:")) != -1 )
115         {
116                 switch(opt)
117                 {
118                 case 'h':
119                         Usage(argv[0]);
120                         exit(0);
121                 case 'C':
122                         gsOpt_WorkingDir = optarg;
123                         break;
124                 case 'c':
125                         gsOpt_ConfigFile = optarg;
126                         break;
127                 case 'f':
128                         gsOpt_UdipropsFile = optarg;
129                         break;
130                 case 'a':
131                         gsOpt_ABIName = optarg;
132                         break;
133                 case '?':
134                         Usage(argv[0]);
135                         return 1;
136                 default:
137                         fprintf(stderr, "BUG: Unhandled optarg %i '%c'\n", opt, opt);
138                         break;
139                 }
140         }
141         return 0;
142 }
143
144 void Usage(const char *progname)
145 {
146         fprintf(stderr, "Usage: %s [-C workingdir] [-c udibuild.ini] [-f udiprops.txt] [-a abiname]\n",
147                 progname);
148         fprintf(stderr, "\n"
149                 "-C workingdir   : Change to the specified directory before looking for udiprops.txt\n"
150                 "-c udibuild.ini : Override the default udibuild config file\n"
151                 "-f udiprops.txt : Override the default udiprops file\n"
152                 "-a abiname      : Select a different ABI\n"
153                 "\n");
154 }
155
156 char *mkstr(const char *fmt, ...)
157 {
158         va_list args;
159         va_start(args, fmt);
160         size_t len = vsnprintf(NULL, 0, fmt, args);
161         va_end(args);
162         va_start(args, fmt);
163         char *ret = malloc(len+1);
164         vsnprintf(ret, len+1, fmt, args);
165         va_end(args);
166         return ret;
167 }
168

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