kit

kit
git clone https://git.ryansepassi.com/git/kit.git
Log | Files | Refs | README

hosted.c (4670B)


      1 #include "hosted.h"
      2 
      3 #include <stdarg.h>
      4 #include <string.h>
      5 
      6 static char* driver_hosted_path_join(void* user, const char* a, const char* b,
      7                                      size_t* out_size) {
      8   return driver_path_join((DriverEnv*)user, a, b, out_size);
      9 }
     10 
     11 static int driver_hosted_path_exists(void* user, const char* path) {
     12   (void)user;
     13   return driver_path_exists(path);
     14 }
     15 
     16 static void* driver_hosted_alloc(void* user, size_t size) {
     17   return driver_alloc((DriverEnv*)user, size);
     18 }
     19 
     20 static void driver_hosted_free(void* user, void* ptr, size_t size) {
     21   driver_free((DriverEnv*)user, ptr, size);
     22 }
     23 
     24 static void driver_hosted_vdiagf(void* user, const char* tool,
     25                                  const char* fmt, va_list ap) {
     26   (void)user;
     27   driver_verrf(tool, fmt, ap);
     28 }
     29 
     30 static const KitOsHostedHost driver_hosted_host = {
     31     .path_join = driver_hosted_path_join,
     32     .path_exists = driver_hosted_path_exists,
     33     .alloc = driver_hosted_alloc,
     34     .free = driver_hosted_free,
     35     .vdiagf = driver_hosted_vdiagf,
     36 };
     37 
     38 static const KitOsHostedOps* hosted_ops_for(KitTargetSpec target) {
     39   const KitOsImpl* os = kit_os_lookup(target.os);
     40   if (!os || !os->hosted || os->hosted->obj != target.obj) return NULL;
     41   return os->hosted;
     42 }
     43 
     44 static const KitOsHostedOps* hosted_ops_for_os(KitOSKind os_kind) {
     45   const KitOsImpl* os = kit_os_lookup(os_kind);
     46   return os ? os->hosted : NULL;
     47 }
     48 
     49 static int hosted_dirs_from_sysroot(DriverHostedDirs* dirs,
     50                                     KitTargetSpec target, const char* sysroot) {
     51   const KitOsHostedOps* ops = hosted_ops_for_os(target.os);
     52   dirs->root = sysroot;
     53   if (!ops) return 0;
     54   return ops->sysroot_layout(dirs, target, sysroot);
     55 }
     56 
     57 int driver_hosted_dirs_resolve(const DriverHostedRequest* req,
     58                                DriverHostedDirs* out) {
     59   const char* sysroot;
     60   if (!req || !req->env || !out) return 1;
     61   memset(out, 0, sizeof(*out));
     62   out->host = &driver_hosted_host;
     63   out->host_user = req->env;
     64 
     65   sysroot = (req->sysroot && req->sysroot[0]) ? req->sysroot : NULL;
     66   if (!sysroot) {
     67     const char* env_sysroot = driver_getenv("KIT_SYSROOT");
     68     if (env_sysroot && env_sysroot[0]) sysroot = env_sysroot;
     69   }
     70   if (sysroot) {
     71     if (!driver_path_exists(sysroot)) {
     72       driver_errf(req->tool, "sysroot not found: %.*s",
     73                   KIT_SLICE_ARG(kit_slice_cstr(sysroot)));
     74       return 1;
     75     }
     76     if (hosted_dirs_from_sysroot(out, req->target, sysroot) != 0) {
     77       driver_hosted_dirs_fini(out);
     78       return 1;
     79     }
     80   } else {
     81     KitTargetSpec host = driver_host_target();
     82     if (host.os == KIT_OS_MACOS && req->target.os == KIT_OS_MACOS)
     83       /* One installed macOS SDK serves both supported Darwin architectures;
     84        * allow native cross-arch x86_64<->aarch64 without xcrun. */
     85       (void)driver_default_hosted_dirs(req->env, req->target, out);
     86     else if (req->target.os == host.os && req->target.arch == host.arch)
     87       (void)driver_default_hosted_dirs(req->env, req->target, out);
     88     else if (host.os == KIT_OS_MACOS &&
     89              (req->target.os == KIT_OS_IOS ||
     90               req->target.os == KIT_OS_IOS_SIMULATOR))
     91       (void)driver_default_hosted_dirs(req->env, req->target, out);
     92   }
     93   return 0;
     94 }
     95 
     96 int driver_hosted_resolve(const DriverHostedRequest* req,
     97                           DriverHostedPlan* out) {
     98   DriverHostedPlan zero = {0};
     99   DriverHostedDirs dirs;
    100   KitOsHostedRequest os_req;
    101   const KitOsHostedOps* ops;
    102   int rc;
    103   if (!req || !out || !req->env || !req->tool) return 1;
    104   *out = zero;
    105   if (driver_hosted_dirs_resolve(req, &dirs) != 0) {
    106     return 1;
    107   }
    108   ops = hosted_ops_for(req->target);
    109   if (!ops) {
    110     driver_errf(req->tool, "no hosted libc profile for target");
    111     driver_hosted_dirs_fini(&dirs);
    112     return 1;
    113   }
    114   memset(&os_req, 0, sizeof(os_req));
    115   os_req.host = &driver_hosted_host;
    116   os_req.host_user = req->env;
    117   os_req.tool = req->tool;
    118   os_req.target = req->target;
    119   os_req.static_link = req->static_link;
    120   os_req.shared_link = req->shared_link;
    121   os_req.link_inputs = req->link_inputs;
    122 
    123   rc = ops->resolve(&os_req, &dirs, out);
    124   if (rc == 0) {
    125     uint32_t j;
    126     for (j = 0; j < dirs.nlibdirs && j < DRIVER_HOSTED_MAX_LIB_SEARCH_DIRS;
    127          ++j) {
    128       out->lib_search_dirs[j] = dirs.libdirs[j];
    129       out->lib_search_dir_sizes[j] = dirs.libdir_sizes[j];
    130       dirs.libdirs[j] = NULL;
    131       dirs.libdir_sizes[j] = 0;
    132     }
    133     out->nlib_search_dirs = j;
    134   }
    135   driver_hosted_dirs_fini(&dirs);
    136   if (rc != 0) driver_hosted_plan_fini(req->env, out);
    137   return rc;
    138 }
    139 
    140 void driver_hosted_plan_fini(DriverEnv* env, DriverHostedPlan* plan) {
    141   kit_os_hosted_plan_fini(&driver_hosted_host, env, plan);
    142 }