kit

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

hosted.c (4908B)


      1 #include "os/hosted_internal.h"
      2 
      3 static const char* freebsd_version_str(uint8_t major) {
      4   static const char* const tab[21] = {"15", "1",  "2",  "3",  "4",  "5",  "6",
      5                                       "7",  "8",  "9",  "10", "11", "12", "13",
      6                                       "14", "15", "16", "17", "18", "19", "20"};
      7   return (major < 21) ? tab[major] : "15";
      8 }
      9 
     10 static int hosted_add_freebsd_defines(KitOsHostedPlan* plan,
     11                                       uint8_t version_major) {
     12   if (kit_os_hosted_add_clang_compat_defines(plan) != 0 ||
     13       kit_os_hosted_add_define(plan, "__FreeBSD__",
     14                                freebsd_version_str(version_major)) != 0 ||
     15       kit_os_hosted_add_define(plan, "__ELF__", "1") != 0 ||
     16       kit_os_hosted_add_define(plan, "__unix__", "1") != 0 ||
     17       kit_os_hosted_add_define(plan, "__unix", "1") != 0 ||
     18       kit_os_hosted_add_define(plan, "unix", "1") != 0)
     19     return 1;
     20   return 0;
     21 }
     22 
     23 static int hosted_resolve_freebsd(const KitOsHostedRequest* req,
     24                                   const KitOsHostedDirs* dirs,
     25                                   KitOsHostedPlan* plan) {
     26   int static_link;
     27   if (dirs->nlibdirs == 0) {
     28     kit_os_hosted_errf(req->host, req->host_user, req->tool,
     29                        "FreeBSD hosted profile requires --sysroot or "
     30                        "KIT_SYSROOT");
     31     return 1;
     32   }
     33   static_link = req->static_link &&
     34                 kit_os_hosted_libdir_has(req->host, req->host_user, dirs,
     35                                          "libc.a");
     36   plan->profile_name = static_link ? "freebsd-static" : "freebsd-dynamic";
     37   if (!static_link) plan->interp_path = "/libexec/ld-elf.so.1";
     38   if (hosted_add_freebsd_defines(plan, req->target.os_version_major) != 0 ||
     39       kit_os_hosted_add_incdirs(plan, req->host, req->host_user, dirs) != 0) {
     40     kit_os_hosted_errf(req->host, req->host_user, req->tool, "out of memory");
     41     return 1;
     42   }
     43   if (!req->link_inputs) return 0;
     44   if (kit_os_hosted_add_required_search(
     45           plan->before, &plan->nbefore, KIT_OS_HOSTED_MAX_BEFORE, req, dirs,
     46           static_link ? "crt1.o" : "Scrt1.o",
     47           KIT_OS_HOSTED_INPUT_OBJECT) != 0 ||
     48       kit_os_hosted_add_required_search(
     49           plan->before, &plan->nbefore, KIT_OS_HOSTED_MAX_BEFORE, req, dirs,
     50           "crti.o", KIT_OS_HOSTED_INPUT_OBJECT) != 0)
     51     return 1;
     52   if (static_link) {
     53     int has_libsys = kit_os_hosted_libdir_has(req->host, req->host_user, dirs,
     54                                               "libsys.a");
     55     int has_crt = kit_os_hosted_libdir_has(req->host, req->host_user, dirs,
     56                                            "libcompiler_rt.a");
     57     if (kit_os_hosted_add_required_search(
     58             plan->after, &plan->nafter, KIT_OS_HOSTED_MAX_AFTER, req, dirs,
     59             "libc.a", KIT_OS_HOSTED_INPUT_ARCHIVE) != 0)
     60       return 1;
     61     if (has_libsys &&
     62         kit_os_hosted_add_required_search(
     63             plan->after, &plan->nafter, KIT_OS_HOSTED_MAX_AFTER, req, dirs,
     64             "libsys.a", KIT_OS_HOSTED_INPUT_ARCHIVE) != 0)
     65       return 1;
     66     if (has_crt &&
     67         kit_os_hosted_add_required_search(
     68             plan->after, &plan->nafter, KIT_OS_HOSTED_MAX_AFTER, req, dirs,
     69             "libcompiler_rt.a", KIT_OS_HOSTED_INPUT_ARCHIVE) != 0)
     70       return 1;
     71     if ((has_libsys || has_crt) &&
     72         kit_os_hosted_add_required_search(
     73             plan->after, &plan->nafter, KIT_OS_HOSTED_MAX_AFTER, req, dirs,
     74             "libc.a", KIT_OS_HOSTED_INPUT_ARCHIVE) != 0)
     75       return 1;
     76   } else {
     77     if (kit_os_hosted_add_required_search(
     78             plan->after, &plan->nafter, KIT_OS_HOSTED_MAX_AFTER, req, dirs,
     79             "libc.so.7", KIT_OS_HOSTED_INPUT_DSO) != 0)
     80       return 1;
     81     if (kit_os_hosted_libdir_has(req->host, req->host_user, dirs,
     82                                  "libsys.so.7") &&
     83         kit_os_hosted_add_required_search(
     84             plan->after, &plan->nafter, KIT_OS_HOSTED_MAX_AFTER, req, dirs,
     85             "libsys.so.7", KIT_OS_HOSTED_INPUT_DSO) != 0)
     86       return 1;
     87   }
     88   if (kit_os_hosted_add_required_search(
     89           plan->final, &plan->nfinal, KIT_OS_HOSTED_MAX_FINAL, req, dirs,
     90           "crtn.o", KIT_OS_HOSTED_INPUT_OBJECT) != 0)
     91     return 1;
     92   return 0;
     93 }
     94 
     95 static int hosted_sysroot_layout_freebsd(KitOsHostedDirs* dirs,
     96                                          KitTargetSpec target,
     97                                          const char* sysroot) {
     98   (void)target;
     99   if (kit_os_hosted_dirs_add_inc_join(dirs, sysroot, "usr/include") != 0 ||
    100       kit_os_hosted_dirs_add_lib_join(dirs, sysroot, "usr/lib") != 0 ||
    101       kit_os_hosted_dirs_add_lib_join(dirs, sysroot, "lib") != 0)
    102     return 1;
    103   return 0;
    104 }
    105 
    106 const KitOsHostedOps kit_os_freebsd_hosted_ops = {
    107     .obj = KIT_OBJ_ELF,
    108     .default_profile = 0,
    109     .needs_sysroot_libdir = 0,
    110     .resolve = hosted_resolve_freebsd,
    111     .sysroot_layout = hosted_sysroot_layout_freebsd,
    112 };