Tools/udibuild - Working (but hacky) udibuild tool
[tpg/acess2.git] / 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 <assert.h>
13 #include "include/build.h"
14 #include "include/inifile.h"
15 #include "include/udiprops.h"
16
17 #ifdef __ACESS__
18 #define CONFIG_FILE     "/Acess/Conf/UDI/udibuild.ini"
19 #else
20 #define CONFIG_FILE     "/etc/udi/udibuild.ini"
21 #endif
22
23 // === PROTOTYPES ===
24  int    main(int argc, char *argv[]);
25  int    ParseArguments(int argc, char *argv[]);
26 void    Usage(const char *progname);
27
28 // === GLOBALS ===
29 const char *gsOpt_ConfigFile;
30 const char *gsOpt_WorkingDir;
31 const char *gsOpt_UdipropsFile;
32 const char *gsOpt_ABIName = "ia32";
33 tIniFile        *gpOptions;
34 tUdiprops       *gpUdipropsBuild;
35
36 // === CODE ===
37 int main(int argc, char *argv[])
38 {
39         if( ParseArguments(argc, argv) ) {
40                 return 1;
41         }
42
43         // Locate udibuild.ini
44         if( NULL == gsOpt_ConfigFile )
45         {
46                 // 1. Check CWD
47                 //if( file_exists("./udibuild.ini") )
48                 //{
49                 //      gsOpt_ConfigFile = "udibuild.ini";
50                 //}
51                 // 2. Check program dir (if not invoked from PATH)
52                 // 3. Check ~/.config/udi/udibuild.ini
53                 // 4. Check CONFIGNAME
54                 
55                 exit(2);
56         }
57         gpOptions = IniFile_Load(gsOpt_ConfigFile);
58         assert(gpOptions);
59
60         // Change to working directory (-C <dir>)
61         if( gsOpt_WorkingDir )
62         {
63                 chdir(gsOpt_WorkingDir);
64         }
65
66         // Load udiprops
67         gpUdipropsBuild = Udiprops_LoadBuild( gsOpt_UdipropsFile ? gsOpt_UdipropsFile : "udiprops.txt" );
68         assert(gpUdipropsBuild);
69         assert(gpUdipropsBuild->SourceFiles);
70
71         // Do build
72         for( int i = 0; gpUdipropsBuild->SourceFiles[i]; i ++ )
73         {
74                 int rv = Build_CompileFile(gpOptions, gsOpt_ABIName, gpUdipropsBuild,
75                         gpUdipropsBuild->SourceFiles[i]);
76                 if( rv ) {
77                         fprintf(stderr, "*** Exit status: %i\n", rv);
78                         return rv;
79                 }
80         }
81         Build_LinkObjects(gpOptions, gsOpt_ABIName, gpUdipropsBuild);
82
83         return 0;
84 }
85
86 int ParseArguments(int argc, char *argv[])
87 {
88          int    opt;
89         while( (opt = getopt(argc, argv, "hC:c:f:a:")) != -1 )
90         {
91                 switch(opt)
92                 {
93                 case 'h':
94                         Usage(argv[0]);
95                         exit(0);
96                 case 'C':
97                         gsOpt_WorkingDir = optarg;
98                         break;
99                 case 'c':
100                         gsOpt_ConfigFile = optarg;
101                         break;
102                 case 'f':
103                         gsOpt_UdipropsFile = optarg;
104                         break;
105                 case 'a':
106                         gsOpt_ABIName = optarg;
107                         break;
108                 case '?':
109                         Usage(argv[0]);
110                         return 1;
111                 default:
112                         fprintf(stderr, "BUG: Unhandled optarg %i '%c'\n", opt, opt);
113                         break;
114                 }
115         }
116         return 0;
117 }
118
119 void Usage(const char *progname)
120 {
121         fprintf(stderr, "Usage: %s [-C workingdir] [-c udibuild.ini] [-f udiprops.txt] [-a abiname]\n",
122                 progname);
123         fprintf(stderr, "\n"
124                 "-C workingdir   : Change to the specified directory before looking for udiprops.txt\n"
125                 "-c udibuild.ini : Override the default udibuild config file\n"
126                 "-f udiprops.txt : Override the default udiprops file\n"
127                 "-a abiname      : Select a different ABI\n"
128                 "\n");
129 }
130

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