targets are now read in
This commit is contained in:
parent
68ca76cb45
commit
76b011511a
110
src/cfg/opt.c
110
src/cfg/opt.c
|
@ -5,6 +5,8 @@
|
||||||
#include <cfg/opt.h>
|
#include <cfg/opt.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
|
#define MAX_TARGETS_PER_PROJECT 100
|
||||||
|
|
||||||
TargetConfig default_target_config() {
|
TargetConfig default_target_config() {
|
||||||
TargetConfig config;
|
TargetConfig config;
|
||||||
|
|
||||||
|
@ -64,26 +66,45 @@ void print_help(void) {
|
||||||
#define PROJECT_TOML_ERR 1
|
#define PROJECT_TOML_ERR 1
|
||||||
#define PROJECT_SEMANTIC_ERR 2
|
#define PROJECT_SEMANTIC_ERR 2
|
||||||
|
|
||||||
|
static void get_bool(bool* boolean, toml_table_t *table, const char* name) {
|
||||||
|
toml_datum_t datum = toml_bool_in(table, name);
|
||||||
|
|
||||||
|
if (datum.ok) {
|
||||||
|
*boolean = datum.u.b;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void get_str(char** string, toml_table_t *table, const char* name) {
|
||||||
|
toml_datum_t datum = toml_string_in(table, name);
|
||||||
|
|
||||||
|
if (datum.ok) {
|
||||||
|
*string = datum.u.s;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void get_int(int* integer, toml_table_t *table, const char* name) {
|
||||||
|
toml_datum_t datum = toml_int_in(table, name);
|
||||||
|
|
||||||
|
if (datum.ok) {
|
||||||
|
*integer = (int) datum.u.i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
static int parse_project_table(ProjectConfig *config, toml_table_t *project_table) {
|
static int parse_project_table(ProjectConfig *config, toml_table_t *project_table) {
|
||||||
// project name
|
// project name
|
||||||
toml_datum_t name = toml_string_in(project_table, "name");
|
get_str(&config->name, project_table, "version");
|
||||||
if (!name.ok) {
|
if (config->name == NULL) {
|
||||||
printf("Invalid project configuration: project must have a name\n\n");
|
printf("Invalid project configuration: project must have a name\n\n");
|
||||||
return PROJECT_SEMANTIC_ERR;
|
return PROJECT_SEMANTIC_ERR;
|
||||||
}
|
}
|
||||||
|
|
||||||
config->name = name.u.s;
|
|
||||||
|
|
||||||
// project version
|
// project version
|
||||||
toml_datum_t version = toml_string_in(project_table, "version");
|
get_str(&config->name, project_table, "version");
|
||||||
if (version.ok) {
|
|
||||||
config->name = version.u.s;
|
|
||||||
}
|
|
||||||
|
|
||||||
// author names
|
// author names
|
||||||
toml_array_t *authors = toml_array_in(project_table, "authors");
|
toml_array_t *authors = toml_array_in(project_table, "authors");
|
||||||
if (authors) {
|
if (authors) {
|
||||||
config->authors = g_array_new(FALSE, FALSE, sizeof(char*));
|
config->authors = g_array_new(FALSE, FALSE, sizeof(char *));
|
||||||
|
|
||||||
for (int i = 0;; i++) {
|
for (int i = 0;; i++) {
|
||||||
toml_datum_t author = toml_string_at(authors, i);
|
toml_datum_t author = toml_string_at(authors, i);
|
||||||
|
@ -95,15 +116,63 @@ static int parse_project_table(ProjectConfig *config, toml_table_t *project_tabl
|
||||||
}
|
}
|
||||||
|
|
||||||
// project description
|
// project description
|
||||||
toml_datum_t description = toml_string_in(project_table, "description");
|
get_str(&config->desc, project_table, "description");
|
||||||
if (description.ok) {
|
// project license
|
||||||
config->desc = description.u.s;
|
get_str(&config->license, project_table, "license");
|
||||||
|
|
||||||
|
return PROJECT_OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int get_mode_from_str(TargetCompilationMode* mode, const char* name) {
|
||||||
|
if (strcmp(name, "application") == 0) {
|
||||||
|
*mode = Application;
|
||||||
|
return PROJECT_OK;
|
||||||
|
} else if (strcmp(name, "library") == 0) {
|
||||||
|
*mode = Library;
|
||||||
|
return PROJECT_OK;
|
||||||
|
}
|
||||||
|
printf("Invalid project configuration, mode is invalid: %s\n\n", name);
|
||||||
|
return PROJECT_SEMANTIC_ERR;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int parse_target(ProjectConfig *config, toml_table_t *target_table, const char* name) {
|
||||||
|
TargetConfig target_config = default_target_config();
|
||||||
|
|
||||||
|
target_config.name = (char*) name;
|
||||||
|
|
||||||
|
get_bool(&target_config.print_ast, target_table, "print_ast");
|
||||||
|
get_bool(&target_config.print_asm, target_table, "print_asm");
|
||||||
|
get_bool(&target_config.print_ir, target_table, "print_ir");
|
||||||
|
|
||||||
|
get_str(&target_config.root_module, target_table, "root");
|
||||||
|
get_str(&target_config.output_directory, target_table, "output");
|
||||||
|
get_str(&target_config.archive_directory, target_table, "archive");
|
||||||
|
|
||||||
|
get_int(&target_config.optimization_level, target_table, "opt");
|
||||||
|
|
||||||
|
char* mode = NULL;
|
||||||
|
get_str(&mode, target_table, "mode");
|
||||||
|
int err = get_mode_from_str(&target_config.mode, mode);
|
||||||
|
if (err != PROJECT_OK) {
|
||||||
|
return err;
|
||||||
}
|
}
|
||||||
|
|
||||||
// project license
|
g_array_append_val(config->targets, target_config);
|
||||||
toml_datum_t license = toml_string_in(project_table, "license");
|
|
||||||
if (license.ok) {
|
return PROJECT_OK;
|
||||||
config->license = license.u.s;
|
}
|
||||||
|
|
||||||
|
static int parse_targets(ProjectConfig *config, toml_table_t *root) {
|
||||||
|
toml_table_t *targets = toml_table_in(root, "target");
|
||||||
|
|
||||||
|
for (int i = 0; i < MAX_TARGETS_PER_PROJECT; i++) {
|
||||||
|
const char *key = toml_key_in(targets, i);
|
||||||
|
|
||||||
|
if (!key)
|
||||||
|
break;
|
||||||
|
|
||||||
|
toml_table_t *target = toml_table_in(targets, key);
|
||||||
|
parse_target(config, target, key);
|
||||||
}
|
}
|
||||||
|
|
||||||
return PROJECT_OK;
|
return PROJECT_OK;
|
||||||
|
@ -127,11 +196,12 @@ int load_project_config(ProjectConfig *config) {
|
||||||
|
|
||||||
toml_table_t *project = toml_table_in(conf, "project");
|
toml_table_t *project = toml_table_in(conf, "project");
|
||||||
if (project) {
|
if (project) {
|
||||||
parse_project_table(config, project);
|
if (parse_project_table(config, project) == PROJECT_OK) {
|
||||||
|
return parse_targets(config, project);
|
||||||
return PROJECT_OK;
|
}
|
||||||
|
} else {
|
||||||
|
printf("Invalid project configuration: missing project table\n\n");
|
||||||
}
|
}
|
||||||
printf("Invalid project configuration: missing project table\n\n");
|
|
||||||
|
|
||||||
toml_free(conf);
|
toml_free(conf);
|
||||||
return PROJECT_SEMANTIC_ERR;
|
return PROJECT_SEMANTIC_ERR;
|
||||||
|
|
|
@ -42,6 +42,7 @@ typedef struct ProjectConfig_t {
|
||||||
char* license;
|
char* license;
|
||||||
// list of authors
|
// list of authors
|
||||||
GArray* authors;
|
GArray* authors;
|
||||||
|
GArray* targets;
|
||||||
} ProjectConfig;
|
} ProjectConfig;
|
||||||
|
|
||||||
TargetConfig default_target_config();
|
TargetConfig default_target_config();
|
||||||
|
|
Loading…
Reference in New Issue