Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Project fetch missing libraries #1469

Merged
merged 14 commits into from
Sep 21, 2024
4 changes: 3 additions & 1 deletion src/build/build.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ typedef enum
{
SUBCOMMAND_MISSING = 0,
SUBCOMMAND_VIEW,
SUBCOMMAND_ADD
SUBCOMMAND_ADD,
SUBCOMMAND_FETCH
} ProjectSubcommand;

typedef enum
Expand Down Expand Up @@ -733,3 +734,4 @@ void create_library(BuildOptions *build_options);
void resolve_libraries(BuildTarget *build_target);
void view_project(BuildOptions *build_options);
void add_target_project(BuildOptions *build_options);
void fetch_project(BuildOptions* options);
7 changes: 7 additions & 0 deletions src/build/build_options.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#include "../utils/whereami.h"
#include "build.h"
#include "project.h"
#include "build_internal.h"
#include "git_hash.h"

Expand Down Expand Up @@ -187,6 +188,7 @@ static void project_usage()
PRINTF("Project Subcommands:");
PRINTF(" view view the current projects structure");
PRINTF(" add-target <name> <target_type> add a new target to the project");
PRINTF(" fetch fetch missing project libraries");
brian-sinquin marked this conversation as resolved.
Show resolved Hide resolved
}

static void parse_project_subcommand(BuildOptions *options)
Expand All @@ -208,6 +210,11 @@ static void parse_project_subcommand(BuildOptions *options)

return;
}
if (arg_match("fetch"))
{
options->project_options.command = SUBCOMMAND_FETCH;
return;
}

PROJECT_FAIL_WITH_ERR("Cannot process the unknown subcommand \"%s\".", current_arg);
}
Expand Down
8 changes: 7 additions & 1 deletion src/build/project.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,10 @@

const char** get_project_dependency_directories();

void add_libraries_to_project_file(const char** libs, const char* target_name);
const char** get_project_dependencies();

static void print_vec(const char *header, const char **vec, bool opt);

void add_libraries_to_project_file(const char** libs, const char* target_name);

const char* vendor_fetch_single(const char* lib, const char* path);
104 changes: 104 additions & 0 deletions src/build/project_manipulation.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,21 @@ static void print_vec(const char *header, const char **vec, bool opt)
PRINTFN("");
}

const char** get_project_dependencies()
{
const char *filename;
const char** dependencies = NULL;

JSONObject *project_json = read_project(&filename);
JSONObject *dependencies_json = json_map_get(project_json, "dependencies");

FOREACH(JSONObject *, element, dependencies_json->elements)
{
vec_add(dependencies, element->str);
}
return dependencies;
}

static void print_opt_str(const char *header, const char *str)
{
if (!str) return;
Expand Down Expand Up @@ -252,6 +267,95 @@ static void view_target(const char *filename, const char *name, JSONObject *targ
TARGET_VIEW_BOOL("Return structs on the stack", "x86-stack-struct-return");
}



#if FETCH_AVAILABLE
void fetch_project(BuildOptions* options)
{
if (!file_exists(PROJECT_JSON5) && !file_exists(PROJECT_JSON))
{
error_exit("Failed: no project file found.");
}

if (str_eq(options->path, DEFAULT_PATH))
{
{
const char** deps_dirs = get_project_dependency_directories();
int num_lib = vec_size(deps_dirs);
if (num_lib > 0) options->vendor_download_path = deps_dirs[0];

}

}

const char** libdirs = get_project_dependency_directories();
const char** deps = get_project_dependencies();
const char *filename;
JSONObject *project_json = read_project(&filename);

JSONObject *targets_json = json_map_get(project_json, "targets");

if (targets_json && targets_json->type == J_OBJECT)
{

FOREACH_IDX(i, JSONObject *, target, targets_json->members)
{
const char *key = targets_json->keys[i];
if (target->type != J_OBJECT)
{
error_exit("Invalid data in target '%s'", key);
}

const char** target_deps = get_optional_string_array(filename, key, target, "dependencies");

FOREACH(const char*, dep, target_deps) {
vec_add(deps, dep);
}
}

}

// dependency check tree
while (vec_size(deps) > 0)
{

FOREACH(const char*, dir, libdirs)
{

const char* dep = deps[vec_size(deps)-1];
if(file_exists(file_append_path(dir, str_printf("%s.c3l", dep))))
brian-sinquin marked this conversation as resolved.
Show resolved Hide resolved
{
vec_pop(deps);
break;
};

printf("Fetching missing library '%s'...", dep);
fflush(stdout);

const char* error = vendor_fetch_single(dep, options->vendor_download_path);

if (!error)
{
puts(" finished.");
}
else
{
printf("Failed: '%s'\n", error);
}

vec_pop(deps);
break;
}
}
}
#else
void fetch_project(BuildOptions* options)
{
error_exit("Error: project fetch only available when compiled with cURL.");
}
#endif


void add_libraries_to_project_file(const char** libs, const char* target_name) {

if (!file_exists(PROJECT_JSON5) && !file_exists(PROJECT_JSON)) return;
Expand Down
3 changes: 3 additions & 0 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,9 @@ int main_real(int argc, const char *argv[])
case SUBCOMMAND_ADD:
add_target_project(&build_options);
break;
case SUBCOMMAND_FETCH:
fetch_project(&build_options);
break;
case SUBCOMMAND_MISSING:
break;
}
Expand Down
Loading