mirror of
https://github.com/odin-lang/Odin.git
synced 2026-04-19 04:50:29 +00:00
Merge pull request #1557 from semarie/openbsd-support
initial OpenBSD support
This commit is contained in:
@@ -17,6 +17,11 @@
|
||||
#include <sys/sysctl.h>
|
||||
#endif
|
||||
|
||||
#if defined(GB_SYSTEM_OPENBSD)
|
||||
#include <sys/sysctl.h>
|
||||
#include <sys/utsname.h>
|
||||
#endif
|
||||
|
||||
/*
|
||||
NOTE(Jeroen): This prints the Windows product edition only, to be called from `print_platform_details`.
|
||||
*/
|
||||
@@ -242,6 +247,14 @@ void report_ram_info() {
|
||||
if (sysctl(sysctls, 2, &ram_amount, &val_size, NULL, 0) != -1) {
|
||||
gb_printf("%lld MiB\n", ram_amount / gb_megabytes(1));
|
||||
}
|
||||
#elif defined(GB_SYSTEM_OPENBSD)
|
||||
uint64_t ram_amount;
|
||||
size_t val_size = sizeof(ram_amount);
|
||||
|
||||
int sysctls[] = { CTL_HW, HW_PHYSMEM64 };
|
||||
if (sysctl(sysctls, 2, &ram_amount, &val_size, NULL, 0) != -1) {
|
||||
gb_printf("%lld MiB\n", ram_amount / gb_megabytes(1));
|
||||
}
|
||||
#else
|
||||
gb_printf("Unknown.\n");
|
||||
#endif
|
||||
@@ -643,6 +656,14 @@ void print_bug_report_help() {
|
||||
} else {
|
||||
gb_printf("macOS: Unknown\n");
|
||||
}
|
||||
#elif defined(GB_SYSTEM_OPENBSD)
|
||||
struct utsname un;
|
||||
|
||||
if (uname(&un) != -1) {
|
||||
gb_printf("%s %s %s %s\n", un.sysname, un.release, un.version, un.machine);
|
||||
} else {
|
||||
gb_printf("OpenBSD: Unknown\n");
|
||||
}
|
||||
#else
|
||||
gb_printf("Unknown\n");
|
||||
|
||||
@@ -657,4 +678,4 @@ void print_bug_report_help() {
|
||||
And RAM info.
|
||||
*/
|
||||
report_ram_info();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
#if defined(GB_SYSTEM_FREEBSD)
|
||||
#if defined(GB_SYSTEM_FREEBSD) || defined(GB_SYSTEM_OPENBSD)
|
||||
#include <sys/types.h>
|
||||
#include <sys/sysctl.h>
|
||||
#endif
|
||||
@@ -16,6 +16,7 @@ enum TargetOsKind {
|
||||
TargetOs_linux,
|
||||
TargetOs_essence,
|
||||
TargetOs_freebsd,
|
||||
TargetOs_openbsd,
|
||||
|
||||
TargetOs_wasi,
|
||||
TargetOs_js,
|
||||
@@ -53,6 +54,7 @@ String target_os_names[TargetOs_COUNT] = {
|
||||
str_lit("linux"),
|
||||
str_lit("essence"),
|
||||
str_lit("freebsd"),
|
||||
str_lit("openbsd"),
|
||||
|
||||
str_lit("wasi"),
|
||||
str_lit("js"),
|
||||
@@ -354,6 +356,15 @@ gb_global TargetMetrics target_freebsd_amd64 = {
|
||||
str_lit("e-m:w-i64:64-f80:128-n8:16:32:64-S128"),
|
||||
};
|
||||
|
||||
gb_global TargetMetrics target_openbsd_amd64 = {
|
||||
TargetOs_openbsd,
|
||||
TargetArch_amd64,
|
||||
8,
|
||||
16,
|
||||
str_lit("x86_64-unknown-openbsd-elf"),
|
||||
str_lit("e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"),
|
||||
};
|
||||
|
||||
gb_global TargetMetrics target_essence_amd64 = {
|
||||
TargetOs_essence,
|
||||
TargetArch_amd64,
|
||||
@@ -417,6 +428,7 @@ gb_global NamedTargetMetrics named_targets[] = {
|
||||
{ str_lit("windows_amd64"), &target_windows_amd64 },
|
||||
{ str_lit("freebsd_386"), &target_freebsd_386 },
|
||||
{ str_lit("freebsd_amd64"), &target_freebsd_amd64 },
|
||||
{ str_lit("openbsd_amd64"), &target_openbsd_amd64 },
|
||||
{ str_lit("freestanding_wasm32"), &target_freestanding_wasm32 },
|
||||
{ str_lit("wasi_wasm32"), &target_wasi_wasm32 },
|
||||
{ str_lit("js_wasm32"), &target_js_wasm32 },
|
||||
@@ -722,10 +734,38 @@ String internal_odin_root_dir(void) {
|
||||
len = readlink("/proc/curproc/exe", &path_buf[0], path_buf.count);
|
||||
#elif defined(GB_SYSTEM_DRAGONFLYBSD)
|
||||
len = readlink("/proc/curproc/file", &path_buf[0], path_buf.count);
|
||||
#else
|
||||
#elif defined(GB_SYSTEM_LINUX)
|
||||
len = readlink("/proc/self/exe", &path_buf[0], path_buf.count);
|
||||
#elif defined(GB_SYSTEM_OPENBSD)
|
||||
int error;
|
||||
int mib[] = {
|
||||
CTL_KERN,
|
||||
KERN_PROC_ARGS,
|
||||
getpid(),
|
||||
KERN_PROC_ARGV,
|
||||
};
|
||||
// get argv size
|
||||
error = sysctl(mib, 4, NULL, (size_t *) &len, NULL, 0);
|
||||
if (error == -1) {
|
||||
// sysctl error
|
||||
return make_string(nullptr, 0);
|
||||
}
|
||||
// get argv
|
||||
char **argv = (char **)gb_malloc(len);
|
||||
error = sysctl(mib, 4, argv, (size_t *) &len, NULL, 0);
|
||||
if (error == -1) {
|
||||
// sysctl error
|
||||
gb_mfree(argv);
|
||||
return make_string(nullptr, 0);
|
||||
}
|
||||
// copy argv[0] to path_buf
|
||||
len = gb_strlen(argv[0]);
|
||||
if(len < path_buf.count) {
|
||||
gb_memmove(&path_buf[0], argv[0], len);
|
||||
}
|
||||
gb_mfree(argv);
|
||||
#endif
|
||||
if(len == 0) {
|
||||
if(len == 0 || len == -1) {
|
||||
return make_string(nullptr, 0);
|
||||
}
|
||||
if (len < path_buf.count) {
|
||||
@@ -922,6 +962,8 @@ void init_build_context(TargetMetrics *cross_target) {
|
||||
#endif
|
||||
#elif defined(GB_SYSTEM_FREEBSD)
|
||||
metrics = &target_freebsd_amd64;
|
||||
#elif defined(GB_SYSTEM_OPENBSD)
|
||||
metrics = &target_openbsd_amd64;
|
||||
#elif defined(GB_CPU_ARM)
|
||||
metrics = &target_linux_arm64;
|
||||
#else
|
||||
@@ -980,6 +1022,9 @@ void init_build_context(TargetMetrics *cross_target) {
|
||||
case TargetOs_freebsd:
|
||||
bc->link_flags = str_lit("-arch x86-64 ");
|
||||
break;
|
||||
case TargetOs_openbsd:
|
||||
bc->link_flags = str_lit("-arch x86-64 ");
|
||||
break;
|
||||
}
|
||||
} else if (bc->metrics.arch == TargetArch_i386) {
|
||||
switch (bc->metrics.os) {
|
||||
|
||||
@@ -3506,6 +3506,7 @@ bool check_builtin_procedure(CheckerContext *c, Operand *operand, Ast *call, i32
|
||||
case TargetOs_linux:
|
||||
case TargetOs_essence:
|
||||
case TargetOs_freebsd:
|
||||
case TargetOs_openbsd:
|
||||
switch (build_context.metrics.arch) {
|
||||
case TargetArch_i386:
|
||||
case TargetArch_amd64:
|
||||
|
||||
@@ -906,6 +906,7 @@ void init_universal(void) {
|
||||
{"Linux", TargetOs_linux},
|
||||
{"Essence", TargetOs_essence},
|
||||
{"FreeBSD", TargetOs_freebsd},
|
||||
{"OpenBSD", TargetOs_openbsd},
|
||||
{"WASI", TargetOs_wasi},
|
||||
{"JS", TargetOs_js},
|
||||
{"Freestanding", TargetOs_freestanding},
|
||||
|
||||
@@ -848,7 +848,7 @@ ReadDirectoryError read_directory(String path, Array<FileInfo> *fi) {
|
||||
|
||||
return ReadDirectory_None;
|
||||
}
|
||||
#elif defined(GB_SYSTEM_LINUX) || defined(GB_SYSTEM_OSX) || defined(GB_SYSTEM_FREEBSD)
|
||||
#elif defined(GB_SYSTEM_LINUX) || defined(GB_SYSTEM_OSX) || defined(GB_SYSTEM_FREEBSD) || defined(GB_SYSTEM_OPENBSD)
|
||||
|
||||
#include <dirent.h>
|
||||
|
||||
|
||||
88
src/gb/gb.h
88
src/gb/gb.h
@@ -79,6 +79,10 @@ extern "C" {
|
||||
#ifndef GB_SYSTEM_FREEBSD
|
||||
#define GB_SYSTEM_FREEBSD 1
|
||||
#endif
|
||||
#elif defined(__OpenBSD__)
|
||||
#ifndef GB_SYSTEM_OPENBSD
|
||||
#define GB_SYSTEM_OPENBSD 1
|
||||
#endif
|
||||
#else
|
||||
#error This UNIX operating system is not supported
|
||||
#endif
|
||||
@@ -199,7 +203,7 @@ extern "C" {
|
||||
#endif
|
||||
#include <stdlib.h> // NOTE(bill): malloc on linux
|
||||
#include <sys/mman.h>
|
||||
#if !defined(GB_SYSTEM_OSX) && !defined(__FreeBSD__)
|
||||
#if !defined(GB_SYSTEM_OSX) && !defined(__FreeBSD__) && !defined(__OpenBSD__)
|
||||
#include <sys/sendfile.h>
|
||||
#endif
|
||||
#include <sys/stat.h>
|
||||
@@ -235,6 +239,12 @@ extern "C" {
|
||||
#define sendfile(out, in, offset, count) sendfile(out, in, offset, count, NULL, NULL, 0)
|
||||
#endif
|
||||
|
||||
#if defined(GB_SYSTEM_OPENBSD)
|
||||
#include <stdio.h>
|
||||
#include <pthread_np.h>
|
||||
#define lseek64 lseek
|
||||
#endif
|
||||
|
||||
#if defined(GB_SYSTEM_UNIX)
|
||||
#include <semaphore.h>
|
||||
#endif
|
||||
@@ -783,6 +793,13 @@ typedef struct gbAffinity {
|
||||
isize thread_count;
|
||||
isize threads_per_core;
|
||||
} gbAffinity;
|
||||
#elif defined(GB_SYSTEM_OPENBSD)
|
||||
typedef struct gbAffinity {
|
||||
b32 is_accurate;
|
||||
isize core_count;
|
||||
isize thread_count;
|
||||
isize threads_per_core;
|
||||
} gbAffinity;
|
||||
#else
|
||||
#error TODO(bill): Unknown system
|
||||
#endif
|
||||
@@ -3678,6 +3695,30 @@ b32 gb_affinity_set(gbAffinity *a, isize core, isize thread_index) {
|
||||
return true;
|
||||
}
|
||||
|
||||
isize gb_affinity_thread_count_for_core(gbAffinity *a, isize core) {
|
||||
GB_ASSERT(0 <= core && core < a->core_count);
|
||||
return a->threads_per_core;
|
||||
}
|
||||
|
||||
#elif defined(GB_SYSTEM_OPENBSD)
|
||||
#include <unistd.h>
|
||||
|
||||
void gb_affinity_init(gbAffinity *a) {
|
||||
a->core_count = sysconf(_SC_NPROCESSORS_ONLN);
|
||||
a->threads_per_core = 1;
|
||||
a->is_accurate = a->core_count > 0;
|
||||
a->core_count = a->is_accurate ? a->core_count : 1;
|
||||
a->thread_count = a->core_count;
|
||||
}
|
||||
|
||||
void gb_affinity_destroy(gbAffinity *a) {
|
||||
gb_unused(a);
|
||||
}
|
||||
|
||||
b32 gb_affinity_set(gbAffinity *a, isize core, isize thread_index) {
|
||||
return true;
|
||||
}
|
||||
|
||||
isize gb_affinity_thread_count_for_core(gbAffinity *a, isize core) {
|
||||
GB_ASSERT(0 <= core && core < a->core_count);
|
||||
return a->threads_per_core;
|
||||
@@ -6025,7 +6066,7 @@ gbFileTime gb_file_last_write_time(char const *filepath) {
|
||||
gb_inline b32 gb_file_copy(char const *existing_filename, char const *new_filename, b32 fail_if_exists) {
|
||||
#if defined(GB_SYSTEM_OSX)
|
||||
return copyfile(existing_filename, new_filename, NULL, COPYFILE_DATA) == 0;
|
||||
#else
|
||||
#elif defined(GB_SYSTEM_LINUX) || defined(GB_SYSTEM_FREEBSD)
|
||||
isize size;
|
||||
int existing_fd = open(existing_filename, O_RDONLY, 0);
|
||||
int new_fd = open(new_filename, O_WRONLY|O_CREAT, 0666);
|
||||
@@ -6041,6 +6082,49 @@ gb_inline b32 gb_file_copy(char const *existing_filename, char const *new_filena
|
||||
close(new_fd);
|
||||
close(existing_fd);
|
||||
|
||||
return size == stat_existing.st_size;
|
||||
#else
|
||||
int new_flags = O_WRONLY | O_CREAT;
|
||||
if (fail_if_exists) {
|
||||
new_flags |= O_EXCL;
|
||||
}
|
||||
int existing_fd = open(existing_filename, O_RDONLY, 0);
|
||||
int new_fd = open(new_filename, new_flags, 0666);
|
||||
|
||||
struct stat stat_existing;
|
||||
if (fstat(existing_fd, &stat_existing) == -1) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
size_t bsize = stat_existing.st_blksize > BUFSIZ ? stat_existing.st_blksize : BUFSIZ;
|
||||
char *buf = (char *)malloc(bsize);
|
||||
if (buf == NULL) {
|
||||
close(new_fd);
|
||||
close(existing_fd);
|
||||
return 0;
|
||||
}
|
||||
|
||||
isize size = 0;
|
||||
ssize_t nread, nwrite, offset;
|
||||
while ((nread = read(existing_fd, buf, bsize)) != -1 && nread != 0) {
|
||||
for (offset = 0; nread; nread -= nwrite, offset += nwrite) {
|
||||
if ((nwrite = write(new_fd, buf + offset, nread)) == -1 || nwrite == 0) {
|
||||
free(buf);
|
||||
close(new_fd);
|
||||
close(existing_fd);
|
||||
return 0;
|
||||
}
|
||||
size += nwrite;
|
||||
}
|
||||
}
|
||||
|
||||
free(buf);
|
||||
close(new_fd);
|
||||
close(existing_fd);
|
||||
|
||||
if (nread == -1) {
|
||||
return 0;
|
||||
}
|
||||
return size == stat_existing.st_size;
|
||||
#endif
|
||||
}
|
||||
|
||||
@@ -486,7 +486,7 @@ void thread_set_name(Thread *t, char const *name) {
|
||||
#elif defined(GB_SYSTEM_OSX)
|
||||
// TODO(bill): Test if this works
|
||||
pthread_setname_np(name);
|
||||
#elif defined(GB_SYSTEM_FREEBSD)
|
||||
#elif defined(GB_SYSTEM_FREEBSD) || defined(GB_SYSTEM_OPENBSD)
|
||||
pthread_set_name_np(t->posix_handle, name);
|
||||
#else
|
||||
// TODO(bill): Test if this works
|
||||
|
||||
Reference in New Issue
Block a user