Add dependency management.
This commit is contained in:
parent
76f022e7ea
commit
4624522e15
2
build.sh
2
build.sh
@ -1,4 +1,4 @@
|
|||||||
cd tomlc99
|
cd tomlc99
|
||||||
make
|
make
|
||||||
cd ..
|
cd ..
|
||||||
cc tomlc99/toml.o main.c -o tbuild
|
cc tomlc99/toml.o main.c -o tbuild -O0 -ggdb
|
||||||
|
80
main.c
80
main.c
@ -201,19 +201,33 @@ char* find_project_root(void)
|
|||||||
"\n" \
|
"\n" \
|
||||||
"[Dependencies]\n"
|
"[Dependencies]\n"
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
char *name, *uri;
|
||||||
|
} dependency;
|
||||||
|
|
||||||
|
#define MAX_DEP 20
|
||||||
typedef struct project_manifest {
|
typedef struct project_manifest {
|
||||||
char *name, *author, *version;
|
char *name, *author, *version;
|
||||||
// FIXME: Add dependencies.
|
dependency dependencies[MAX_DEP];
|
||||||
|
size_t dependencies_amount;
|
||||||
} project_manifest;
|
} project_manifest;
|
||||||
|
|
||||||
void free_manifest(project_manifest *manifest) {
|
void free_manifest(project_manifest *manifest) {
|
||||||
|
int i;
|
||||||
|
|
||||||
free(manifest->name);
|
free(manifest->name);
|
||||||
free(manifest->author);
|
free(manifest->author);
|
||||||
free(manifest->version);
|
free(manifest->version);
|
||||||
|
|
||||||
|
for (i = 0; i < manifest->dependencies_amount; i++) {
|
||||||
|
free(manifest->dependencies[i].name);
|
||||||
|
free(manifest->dependencies[i].uri);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
project_manifest* load_manifest(char const *path) {
|
project_manifest* load_manifest(char const *path) {
|
||||||
FILE* fp;
|
FILE* fp;
|
||||||
|
int i;
|
||||||
char errbuf[200];
|
char errbuf[200];
|
||||||
|
|
||||||
fp = fopen(path, "r");
|
fp = fopen(path, "r");
|
||||||
@ -259,12 +273,34 @@ project_manifest* load_manifest(char const *path) {
|
|||||||
new->author = author.u.s;
|
new->author = author.u.s;
|
||||||
new->version = version.u.s;
|
new->version = version.u.s;
|
||||||
|
|
||||||
|
new->dependencies_amount = 0;
|
||||||
|
|
||||||
// FIXME: Add dependencies.
|
// FIXME: Add dependencies.
|
||||||
|
toml_table_t *dependencies = toml_table_in(conf, "Dependencies");
|
||||||
|
if (!dependencies) {
|
||||||
|
fprintf(stderr, "Error: Cannot load manifest file: Cannot find [Dependencies] table.\n");
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (i = 0; ; i++) {
|
||||||
|
char const *key = toml_key_in(dependencies, i);
|
||||||
|
if (!key) break;
|
||||||
|
|
||||||
|
toml_datum_t dep_uri = toml_string_in(dependencies, key);
|
||||||
|
new->dependencies_amount++;
|
||||||
|
char *name = calloc(1, sizeof(char) * (strlen(key) + 1));
|
||||||
|
strcpy(name, key);
|
||||||
|
char *url = calloc(1, sizeof(char) * (strlen(dep_uri.u.s) + 1));
|
||||||
|
strcpy(url, dep_uri.u.s);
|
||||||
|
|
||||||
|
dependency dep = {
|
||||||
|
name, url
|
||||||
|
};
|
||||||
|
|
||||||
|
new->dependencies[i] = dep;
|
||||||
|
}
|
||||||
|
|
||||||
toml_free(conf);
|
toml_free(conf);
|
||||||
|
|
||||||
printf("%s, %s, %s\n", new->name, new->author, new->version);
|
|
||||||
|
|
||||||
return new;
|
return new;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -341,7 +377,7 @@ void print_help(char **argv) {
|
|||||||
printf("Usage: %s [command]\n", argv[0]);
|
printf("Usage: %s [command]\n", argv[0]);
|
||||||
fputs("\nCommands:\n", stderr);
|
fputs("\nCommands:\n", stderr);
|
||||||
fputs(" * init|i [path=.] - Setup a new project.\n", stderr);
|
fputs(" * init|i [path=.] - Setup a new project.\n", stderr);
|
||||||
fputs(" * build|. - Build project in current working directory.\n", stderr);
|
fputs(" * build|. [--zeal|-z|-Z] - Build project in current working directory.\n", stderr);
|
||||||
fputs(" * clean - Clean output code in current working directory.\n", stderr);
|
fputs(" * clean - Clean output code in current working directory.\n", stderr);
|
||||||
fputs("\nTo see manifest file usage, check out man tbuild(1)\n", stderr);
|
fputs("\nTo see manifest file usage, check out man tbuild(1)\n", stderr);
|
||||||
}
|
}
|
||||||
@ -642,6 +678,8 @@ int main(int argc, char **argv) {
|
|||||||
if (argc > 2)
|
if (argc > 2)
|
||||||
free(project_path);
|
free(project_path);
|
||||||
} else if (cmd == 'b' || cmd == '.') {
|
} else if (cmd == 'b' || cmd == '.') {
|
||||||
|
int i;
|
||||||
|
|
||||||
struct {
|
struct {
|
||||||
bool zeal_build;
|
bool zeal_build;
|
||||||
} options = { 0 };
|
} options = { 0 };
|
||||||
@ -681,9 +719,35 @@ int main(int argc, char **argv) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
project_manifest* manifest = load_manifest(text_format("%s/" MANIFEST_FNAME, project_path));
|
project_manifest* manifest = load_manifest(text_format("%s/" MANIFEST_FNAME, project_path));
|
||||||
|
if (!manifest) {
|
||||||
|
fputs("Error: Cannot build project: Failed to open manifest.\n", stderr);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
puts(buffer_text_format);
|
puts(buffer_text_format);
|
||||||
|
|
||||||
// TODO: Validate dependencies.
|
text_format("%s/lib", project_path);
|
||||||
|
char *lib_dir = malloc((strlen(buffer_text_format)+1)*sizeof(char));
|
||||||
|
strcpy(lib_dir, buffer_text_format);
|
||||||
|
if (!file_exists(lib_dir)) {
|
||||||
|
bool status = makedir(lib_dir);
|
||||||
|
if (!status) {
|
||||||
|
fputs("Error: Cannot build project: Cannot create lib directory.\n", stderr);
|
||||||
|
free_manifest(manifest);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for (i = 0; i < manifest->dependencies_amount; i++) {
|
||||||
|
dependency dep = manifest->dependencies[i];
|
||||||
|
printf("%s -> %s (%i)\n", dep.name, dep.uri, i);
|
||||||
|
|
||||||
|
puts(text_format("%s/%s", lib_dir, dep.name));
|
||||||
|
if (!file_exists(text_format("%s/%s", lib_dir, dep.name)))
|
||||||
|
system(text_format("git clone --depth 1 --recursive --shallow-submodules '%s' '%s/%s'", dep.uri, lib_dir, dep.name));
|
||||||
|
else
|
||||||
|
// FIXME: Make this cross platform.
|
||||||
|
system(text_format("cd '%s/%s' && git pull", lib_dir, dep.name));
|
||||||
|
}
|
||||||
|
|
||||||
// Create build directory
|
// Create build directory
|
||||||
text_format("%s/build", project_path);
|
text_format("%s/build", project_path);
|
||||||
@ -705,8 +769,8 @@ int main(int argc, char **argv) {
|
|||||||
puts("Populating build directory...");
|
puts("Populating build directory...");
|
||||||
clear_directory(build_dir);
|
clear_directory(build_dir);
|
||||||
copy_directory(src_dir, build_dir);
|
copy_directory(src_dir, build_dir);
|
||||||
if (file_exists(text_format("%s/libs", project_path)))
|
if (file_exists(lib_dir))
|
||||||
copy_directory(src_dir, buffer_text_format);
|
copy_directory(lib_dir, build_dir);
|
||||||
|
|
||||||
// Run scripts (if any)
|
// Run scripts (if any)
|
||||||
puts("Running scripts...");
|
puts("Running scripts...");
|
||||||
|
Loading…
x
Reference in New Issue
Block a user