#include <liba.h>
+static OptionDescriptor_T* lookup_opt(int optc, OptionDescriptor_T* optv, char* flag)
+{
+ OptionDescriptor_T* opt = NULL;
+
+ for (int i = 0; i < optc; i++)
+ {
+ int match = (
+ (flag[1] && !strcmp(optv[i].lname, flag))
+ || (!flag[1] && flag[0] == optv[i].sname)
+ );
+ if (match)
+ {
+ opt = &optv[i];
+ break;
+ }
+ }
+
+ return opt;
+}
+
+static void parse_longopt(int optc, OptionDescriptor_T* optv, int* currp, char** argv)
+{
+ /* get the option and the arg if there is one */
+ char* flag = argv[*currp] + 2;
+ char* split = strchr(flag, '=');
+ char* arg = NULL;
+ if (split)
+ {
+ *split = '\0';
+ arg = split+1;
+ }
+
+ OptionDescriptor_T* od = lookup_opt(optc, optv, flag);
+ if (!od)
+ {
+ fatal("unknown option: '%s'", flag);
+ }
+ else if (arg && !od->hasarg)
+ {
+ fatal("unexpected argument to option: --%s=%s", flag, arg);
+ }
+ else
+ {
+ if (od->hasarg && !arg)
+ {
+ *currp += 1;
+ arg = argv[*currp];
+ if (!arg)
+ {
+ fatal("expected argument for option: '%s'", flag);
+ }
+ }
+ printf("handled option: '--%s=%s'\n", flag, arg);
+ }
+
+ /* now repair the split if we made one */
+ if (split && *split)
+ {
+ *split = '=';
+ }
+ *currp += 1;
+}
+
+static void parse_shortopt(int optc, OptionDescriptor_T* optv, int* currp, char** argv)
+{
+ (void)optc, (void)optv;
+ printf("short: %s\n", argv[*currp]);
+ *currp += 1;
+}
+
void opts_parse(int argc, char** argv)
{
- (void)argc;
- (void)argv;
+ /* parse the option descriptors for easy usage during parsing */
+ int capacity = 32;
+ int optc = 1;
+ OptionDescriptor_T* optv = malloc(capacity * sizeof(OptionDescriptor_T));
+ opts_parsespec("h,help", optv);
+ for (Option_T* curr = Options; curr->name; curr++)
+ {
+ if (optc == capacity)
+ {
+ capacity <<= 1;
+ optv = realloc(optv, capacity * sizeof(OptionDescriptor_T));
+ }
+ opts_parsespec(curr->name, &optv[optc]);
+ optc++;
+ }
+
+ /* Record the program name */
+ ARGV0 = argv[0];
+
+ /* now parse the arguments */
+ for (int i = 1; i < argc;)
+ {
+ if (argv[i][0] == '-' && argv[i][1] == '-')
+ {
+ parse_longopt(optc, optv, &i, argv);
+ }
+ else if (argv[i][0] == '-')
+ {
+ parse_shortopt(optc, optv, &i, argv);
+ }
+ else
+ {
+ printf("posarg: %s\n", argv[i]);
+ i++;
+ }
+ }
+
+ free(optv);
}
"-I/usr/X11/include/",
"-I/usr/X11/include/freetype2",
"-I/usr/include/freetype2/",
- "-c", "-o",
+ "-g", "-c", "-o",
/* output */
/* inputs */
NULL
"cc",
"-L./build/lib",
"-L/usr/X11/lib/",
- "-o",
+ "-g", "-o",
/* output */
/* inputs */
/* LIBS */
void object(Target* tgt)
{
- printf("Object %s\n", tgt->outputs[0]);
char** cmd = make_cmd((char**[]){ CCCMD, tgt->outputs, tgt->inputs, NULL });
print_cmd(cmd);
make_dirs(tgt->outputs);
void library(Target* tgt)
{
- printf("Library %s\n", tgt->outputs[0]);
char** cmd = make_cmd((char**[]){ ARCMD, tgt->outputs, tgt->inputs, NULL });
print_cmd(cmd);
exit(execvp(cmd[0], cmd));
void binary(Target* tgt)
{
- printf("Binary %s\n", tgt->outputs[0]);
char** cmd = make_cmd((char**[]){
LDCMD, tgt->outputs, tgt->inputs, LIBS, NULL });
print_cmd(cmd);
int pid = fork();
if (pid == 0)
{
- if (should_rebuild(targets->outputs, targets->inputs))
+ if (targets->build == binary || should_rebuild(targets->outputs, targets->inputs))
{
targets->build(targets);
}