Skip to content

Commit

Permalink
lots of little improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
rsms committed Mar 7, 2023
1 parent f8d278b commit a2d6fd6
Show file tree
Hide file tree
Showing 16 changed files with 311 additions and 87 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@ a.out
/build
/etc/bitset
/lib/sysinc
/lib/*.sysroot
11 changes: 11 additions & 0 deletions etc/dist.sh
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,17 @@ for f in lib/*; do
_copy $f $DESTDIR/$(basename $f)
done

# create symlinks
for name in \
cc \
ld.lld \
ld64.lld \
wasm-ld \
lld-link \
;do
_symlink "$DESTDIR/$name" compis
done

echo "creating $DESTDIR.tar.xz"
_create_tar_xz_from_dir $DESTDIR $DESTDIR.tar.xz

Expand Down
1 change: 1 addition & 0 deletions src/build.c
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ int main_build(int argc, char* argv[]) {
if (optind < 0)
return 1;

coverbose |= opt_verbose;
#if DEBUG
// --co-trace turns on all trace flags
opt_trace_parse |= opt_trace_all;
Expand Down
75 changes: 60 additions & 15 deletions src/build_syslibs.c
Original file line number Diff line number Diff line change
Expand Up @@ -314,33 +314,78 @@ static err_t build_librt(compiler_t* c) {
}


static err_t create_exe_symlinks(compiler_t* c) {
usize dstpathcap = strlen(coexefile) + 16;
char* dstpath = alloca(dstpathcap);
usize dstpathlen = path_dir(dstpath, dstpathcap, coexefile);
const char* coexename = path_base(coexefile);
int r;

#define CREATE_EXE_SYMLINK(name) { \
memcpy(&dstpath[dstpathlen], \
PATH_SEPARATOR_STR name, strlen(PATH_SEPARATOR_STR name) + 1); \
r = symlink(coexename, dstpath); \
if (r != 0 && errno != EEXIST) \
return err_errno(); \
if (r == 0) dlog("symlink %s -> %s", dstpath, coexename); \
}

CREATE_EXE_SYMLINK("cc");
CREATE_EXE_SYMLINK("ld.lld");
CREATE_EXE_SYMLINK("ld64.lld");
CREATE_EXE_SYMLINK("wasm-ld");
CREATE_EXE_SYMLINK("lld-link");

#undef CREATE_EXE_SYMLINK
return 0;
}


err_t build_syslibs_if_needed(compiler_t* c) {
err_t err = 0;

if (must_build_libc(c) || must_build_librt(c)) {
lockfile_t lockfile;
char* lockfile_path = path_join_alloca(c->sysroot, "syslibs-build.lock");
long lockee_pid;
if (( err = lockfile_trylock(&lockfile, lockfile_path, &lockee_pid) )) {
if (err != ErrExists)
return err;
log("waiting for compis (pid %ld) to finish...", lockee_pid);
if (( err = lockfile_lock(&lockfile, lockfile_path) ))
return err;
}
// note: must check again in case another process won and build the libs
if (!err && must_build_libc(c)) err = build_libc(c);
if (!err && must_build_librt(c)) err = build_librt(c);
lockfile_unlock(&lockfile);
}

if (!err)
err = create_exe_symlinks(c);

return err;
}


/*err_t build_syslibs_if_needed(compiler_t* c) {
if (!must_build_libc(c) && !must_build_librt(c))
return 0;
err_t err;
err_t err = 0;
lockfile_t lockfile;
char* lockfile_path = path_join_alloca(c->sysroot, "syslibs-build.lock");

long lockee_pid;
err = lockfile_trylock(&lockfile, lockfile_path, &lockee_pid);
if (err) {
if (( err = lockfile_trylock(&lockfile, lockfile_path, &lockee_pid) )) {
if (err != ErrExists)
return err;
log("waiting for compis (pid %ld) to finish...", lockee_pid);
err = lockfile_lock(&lockfile, lockfile_path);
if (err)
if (( err = lockfile_lock(&lockfile, lockfile_path) ))
return err;
}

// note: must check again in case another process won and build the libs

if (must_build_libc(c) && (err = build_libc(c)))
goto end;
if (must_build_librt(c) && (err = build_librt(c)))
goto end;

end:
if (!err && must_build_libc(c)) err = build_libc(c);
if (!err && must_build_librt(c)) err = build_librt(c);
lockfile_unlock(&lockfile);
return err;
}
}*/
31 changes: 12 additions & 19 deletions src/cc.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
#include "llvm/llvm.h"
#include "clang/Basic/Version.inc" // CLANG_VERSION_STRING

#include <unistd.h> // symlink


static void diaghandler(const diag_t* d, void* nullable userdata) {
// unused
Expand All @@ -35,15 +33,22 @@ int cc_main(int user_argc, char* user_argv[]) {
const char* arg = user_argv[i];
if (streq(arg, "-nostdlib") || streq(arg, "-nolibc")) {
nostdlib = true;
} if (streq(arg, "-nostdinc") ||
} else if (streq(arg, "-nostdinc") ||
streq(arg, "--no-standard-includes") ||
streq(arg, "-nostdlibinc"))
{
nostdinc = true;
} if (streq(arg, "-ffreestanding")) {
} else if (streq(arg, "-ffreestanding")) {
freestanding = true;
} if (streq(arg, "-c") || streq(arg, "-S") || streq(arg, "-E")) {
} else if (streq(arg, "-c") || streq(arg, "-S") || streq(arg, "-E")) {
nolink = true;
} else if (streq(arg, "-v") || streq(arg, "--verbose")) {
c.opt_verbose = true;
coverbose = true;
} else if (streq(arg, "-O0")) {
c.buildmode = BUILDMODE_DEBUG;
} else if (str_startswith(arg, "-O")) {
c.buildmode = BUILDMODE_OPT;
} else if (streq(arg, "-target")) {
panic("TODO -target");
} else if (str_startswith(arg, "--target=")) {
Expand Down Expand Up @@ -90,20 +95,8 @@ int cc_main(int user_argc, char* user_argv[]) {

// linker flags
if (!nolink) {
// create lld symlink
// TODO: move to build_syslibs
const char* ldexefile;
switch ((enum target_sys)target->sys) {
case SYS_linux: ldexefile = "ld.lld"; break;
case SYS_macos: ldexefile = "ld64.lld"; break;
// case SYS_wasm: case SYS_wasi: ldexefile = "wasm-ld"; break;
// case SYS_win32: ldexefile = "lld-link"; break;
case SYS_COUNT: case SYS_none: safefail("bad sys"); break;
}
ldexefile = path_join_alloca(c.sysroot, ldexefile);
symlink(coexefile, ldexefile);
strlist_addf(&args, "-fuse-ld=%s", ldexefile);

char* bindir = path_dir_alloca(coexefile);
strlist_addf(&args, "-fuse-ld=%s/%s", bindir, c.ldname);
if (target->sys == SYS_macos && !nolink) {
char macosver[16];
if (streq(target->sysver, "10")) {
Expand Down
11 changes: 11 additions & 0 deletions src/colib.h
Original file line number Diff line number Diff line change
Expand Up @@ -635,6 +635,9 @@ enum err_ {
ErrNoMem = -12, // cannot allocate memory
ErrMFault = -13, // bad memory address
ErrOverflow = -14, // value too large
ErrReadOnly = -15, // read-only
ErrIO = -16, // I/O error
ErrNotDir = -17, // not a directory
};

EXTERN_C err_t err_errno(); // current errno value
Expand Down Expand Up @@ -1009,6 +1012,7 @@ u64 microsleep(u64 microseconds);
//—————————————————————————————————————————————————————————————————————————————————————
// system info
u32 sys_ncpu(); // number of available logical CPUs, logs error on failure and returns 1
const char* sys_homedir();

//—————————————————————————————————————————————————————————————————————————————————————
// files
Expand Down Expand Up @@ -1055,6 +1059,13 @@ extern const char* coexefile;
// Can be overridden with env var COROOT
extern const char* coroot;

// cocachedir: directory of compis cache (~/.cache/compis)
// Can be overridden with env var COCACHE
extern const char* cocachedir;

// coverbose: set to true if -v was passed on the command line
extern bool coverbose;

// comaxproc: max thread concurrency (defaults to sys_ncpu, can be set with -j)
extern u32 comaxproc;

Expand Down
60 changes: 47 additions & 13 deletions src/compiler.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,13 @@
#include "llvm/llvm.h"
#include "clang/Basic/Version.inc" // CLANG_VERSION_STRING

#include <err.h>
#include <errno.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdlib.h>
#include <err.h>

#include <string.h> // XXX


static void set_cstr(memalloc_t ma, char*nullable* dst, slice_t src) {
Expand Down Expand Up @@ -94,6 +97,15 @@ static void configure_target(compiler_t* c) {
c->inttype = type_i64;
}
set_secondary_pointer_types(c);

// set ldname
switch ((enum target_sys)c->target.sys) {
case SYS_linux: c->ldname = "ld.lld"; break;
case SYS_macos: c->ldname = "ld64.lld"; break;
// case SYS_wasm: case SYS_wasi: c->ldname = "wasm-ld"; break;
// case SYS_win32: c->ldname = "lld-link"; break;
case SYS_COUNT: case SYS_none: safefail("sys"); break;
}
}


Expand All @@ -106,10 +118,38 @@ static const char* buildmode_name(buildmode_t m) {
}


static err_t configure_buildroot(compiler_t* c, const char* buildroot) {
static err_t configure_sysroot(compiler_t* c) {
// sysroot = {cocachedir}/{target}[-debug]

char target[80];
usize targetlen = target_fmt(&c->target, target, sizeof(target));
if (c->buildmode == BUILDMODE_DEBUG) {
usize n = strlen("-debug");
memcpy(&target[targetlen], "-debug", n + 1);
targetlen += n;
}

err_t err;
char sysroot[PATH_MAX];
int n = snprintf(sysroot, sizeof(sysroot),
"%s%c%s", cocachedir, PATH_SEPARATOR, target);
safecheck(n > 0 && (usize)n < sizeof(sysroot));
if (( err = fs_mkdirs(sysroot, 0755) ))
return err;

if (c->opt_verbose)
log("using sysroot '%s'", sysroot);

mem_freecstr(c->ma, c->sysroot);
if (( c->sysroot = mem_strdup(c->ma, slice_cstr(sysroot), 0) ) == NULL)
return ErrNoMem;
return 0;
}


static err_t configure_builddir(compiler_t* c, const char* buildroot) {
// builddir = {buildroot}/{mode}-{target}
// pkgbuilddir = {builddir}/{pkgname}.pkg
// sysroot = {builddir}/sysroot

if (!( c->buildroot = path_abs(c->ma, buildroot) ))
return ErrNoMem;
Expand Down Expand Up @@ -158,12 +198,6 @@ static err_t configure_buildroot(compiler_t* c, const char* buildroot) {
APPEND(suffix);
*p = 0;

// sysroot
mem_freecstr(c->ma, c->sysroot);
c->sysroot = path_join_m(c->ma, c->builddir, "sysroot");
if (!c->sysroot)
return ErrNoMem;

return 0;

#undef APPEND
Expand Down Expand Up @@ -247,9 +281,9 @@ static err_t configure_cflags(compiler_t* c) {
err_t compiler_configure(compiler_t* c, const target_t* target, const char* buildroot) {
c->target = *target;
configure_target(c);
err_t err = configure_buildroot(c, buildroot);
if (!err)
err = configure_cflags(c);
err_t err = configure_builddir(c, buildroot);
if (!err) err = configure_sysroot(c);
if (!err) err = configure_cflags(c);
return err;
}

Expand Down
1 change: 1 addition & 0 deletions src/compiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -540,6 +540,7 @@ typedef struct compiler {
slice_t sflags_common; // flags used for all assembly objects (slice of cflags)
slice_t cflags_common; // cflags used for all objects (slice of cflags)
slice_t cflags_sysinc; // cflags with -isystemPATH for current target
const char* ldname; // name of linker for target, relative to sysroot

// diagnostics
diaghandler_t diaghandler; // called when errors are encountered
Expand Down
14 changes: 10 additions & 4 deletions src/err.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ const char* err_str(err_t e) {
case ErrNoMem: return "cannot allocate memory";
case ErrMFault: return "bad memory address";
case ErrOverflow: return "value too large";
case ErrReadOnly: return "read-only";
case ErrIO: return "I/O error";
case ErrNotDir: return "not a directory";
}
return "(unknown error)";
}
Expand All @@ -28,10 +31,13 @@ const char* err_str(err_t e) {
err_t err_errnox(int e) {
switch (e) {
case 0: return 0;
case EACCES: return ErrAccess;
case EEXIST: return ErrExists;
case ENOENT: return ErrNotFound;
case EBADF: return ErrBadfd;
case EACCES: return ErrAccess;
case EEXIST: return ErrExists;
case ENOENT: return ErrNotFound;
case EBADF: return ErrBadfd;
case EROFS: return ErrReadOnly;
case EIO: return ErrIO;
case ENOTDIR: return ErrNotDir;
case ENOTSUP:
case ENOSYS:
return ErrNotSupported;
Expand Down
Loading

0 comments on commit a2d6fd6

Please sign in to comment.