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

Added OpenBSD support. #2823

Merged
merged 7 commits into from
Aug 22, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ else
OSTYPE = bsd
CXX = c++
endif

ifeq ($(UNAME_S),OpenBSD)
OSTYPE = bsd
CXX = c++
endif
endif

ifdef LTO_PLUGIN
Expand Down
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -684,6 +684,25 @@ gmake
./build/release/ponyc examples/helloworld
```

## Building on OpenBSD

OpenBSD has been tested on OpenBSD 6.4.

First, install the required dependencies:

```bash
doas pkg_add gmake libexecinfo llvm pcre2
```

This will build ponyc and compile helloworld:

```bash
gmake verbose=true default_pic=true bits=64
./build/release/ponyc examples/helloworld
```

If you are on a 32-bit platform (e.g., armv7), change `bits=64` to `bits=32`.

## Building on Mac OS X
[![Linux and OS X](https://travis-ci.org/ponylang/ponyc.svg?branch=master)](https://travis-ci.org/ponylang/ponyc)

Expand Down
2 changes: 2 additions & 0 deletions lib/gbenchmark/src/internal_macros.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@
#define BENCHMARK_OS_FREEBSD 1
#elif defined(__NetBSD__)
#define BENCHMARK_OS_NETBSD 1
#elif defined(__OpenBSD__)
#define BENCHMARK_OS_OPENBSD 1
#elif defined(__linux__)
#define BENCHMARK_OS_LINUX 1
#elif defined(__native_client__)
Expand Down
3 changes: 2 additions & 1 deletion lib/gbenchmark/src/sysinfo.cc
Original file line number Diff line number Diff line change
Expand Up @@ -486,7 +486,8 @@ double GetCPUCyclesPerSecond() {

#elif defined BENCHMARK_HAS_SYSCTL
constexpr auto* FreqStr =
#if defined(BENCHMARK_OS_FREEBSD) || defined(BENCHMARK_OS_NETBSD)
#if defined(BENCHMARK_OS_FREEBSD) || defined(BENCHMARK_OS_NETBSD) || \
defined(BENCHMARK_OS_OPENBSD)
"machdep.tsc_freq";
#else
"hw.cpufrequency";
Expand Down
3 changes: 2 additions & 1 deletion lib/gbenchmark/src/timers.cc
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@
#include <sys/time.h>
#include <sys/types.h> // this header must be included before 'sys/sysctl.h' to avoid compilation error on FreeBSD
#include <unistd.h>
#if defined BENCHMARK_OS_FREEBSD || defined BENCHMARK_OS_MACOSX
#if defined BENCHMARK_OS_FREEBSD || defined BENCHMARK_OS_MACOSX || \
defined BENCHMARK_OS_OPENBSD
#include <sys/sysctl.h>
#endif
#if defined(BENCHMARK_OS_MACOSX)
Expand Down
3 changes: 2 additions & 1 deletion packages/builtin/platform.pony
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
primitive Platform
fun bsd(): Bool => freebsd() or dragonfly()
fun bsd(): Bool => freebsd() or dragonfly() or openbsd()
fun freebsd(): Bool => compile_intrinsic
fun dragonfly(): Bool => compile_intrinsic
fun openbsd(): Bool => compile_intrinsic
fun linux(): Bool => compile_intrinsic
fun osx(): Bool => compile_intrinsic
fun posix(): Bool => bsd() or linux() or osx()
Expand Down
4 changes: 2 additions & 2 deletions src/common/paths.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ char* get_file_name(char* path);

char* remove_ext(const char* path, char dot, char sep, size_t* allocated_size);

bool get_compiler_exe_path(char* output_path);
bool get_compiler_exe_path(char* output_path, const char* argv0);

bool get_compiler_exe_directory(char* output_path);
bool get_compiler_exe_directory(char* output_path, const char* argv0);

#endif
3 changes: 3 additions & 0 deletions src/common/platform.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@
#elif defined(__DragonFly__)
# define PLATFORM_IS_BSD
# define PLATFORM_IS_DRAGONFLY
#elif defined(__OpenBSD__)
# define PLATFORM_IS_BSD
# define PLATFORM_IS_OPENBSD
#elif defined(_WIN32)
# define PLATFORM_IS_WINDOWS
# if defined(_MSC_VER)
Expand Down
5 changes: 5 additions & 0 deletions src/libponyc/codegen/genexe.c
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,12 @@ static bool link_exe(compile_t* c, ast_t* program,
// for backtrace reporting.
"-rdynamic "
#endif
#ifdef PLATFORM_IS_OPENBSD
// On OpenBSD, the unwind symbols are contained within libc++abi.
"%s %s %s %s %s -lpthread %s %s %s -lm -lc++abi %s",
#else
"%s %s %s %s %s -lpthread %s %s %s -lm %s",
#endif
linker, file_exe, arch, mcx16_arg, atomic, staticbin, fuseld, file_o,
lib_args, dtrace_args, ponyrt, ldl, lexecinfo
);
Expand Down
11 changes: 9 additions & 2 deletions src/libponyc/codegen/genopt.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1466,7 +1466,7 @@ bool target_is_bsd(char* t)
{
Triple triple = Triple(t);

return triple.isOSDragonFly() || triple.isOSFreeBSD();
return triple.isOSDragonFly() || triple.isOSFreeBSD() || triple.isOSOpenBSD();
}

bool target_is_freebsd(char* t)
Expand All @@ -1483,6 +1483,13 @@ bool target_is_dragonfly(char* t)
return triple.isOSDragonFly();
}

bool target_is_openbsd(char* t)
{
Triple triple = Triple(t);

return triple.isOSOpenBSD();
}

bool target_is_macosx(char* t)
{
Triple triple = Triple(t);
Expand All @@ -1502,7 +1509,7 @@ bool target_is_posix(char* t)
Triple triple = Triple(t);

return triple.isMacOSX() || triple.isOSFreeBSD() || triple.isOSLinux()
|| triple.isOSDragonFly();
|| triple.isOSDragonFly() || triple.isOSOpenBSD();
}

bool target_is_x86(char* t)
Expand Down
1 change: 1 addition & 0 deletions src/libponyc/codegen/genopt.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ bool target_is_linux(char* triple);
bool target_is_bsd(char* triple);
bool target_is_freebsd(char* triple);
bool target_is_dragonfly(char* triple);
bool target_is_openbsd(char* triple);
bool target_is_macosx(char* triple);
bool target_is_windows(char* triple);
bool target_is_posix(char* triple);
Expand Down
12 changes: 12 additions & 0 deletions src/libponyc/codegen/genprim.c
Original file line number Diff line number Diff line change
Expand Up @@ -1172,6 +1172,17 @@ static void platform_dragonfly(compile_t* c, reach_type_t* t, token_id cap)
codegen_finishfun(c);
}

static void platform_openbsd(compile_t* c, reach_type_t* t, token_id cap)
{
FIND_METHOD("openbsd", cap);
start_function(c, t, m, c->i1, &c_t->use_type, 1);

LLVMValueRef result =
LLVMConstInt(c->i1, target_is_openbsd(c->opt->triple), false);
LLVMBuildRet(c->builder, result);
codegen_finishfun(c);
}

static void platform_linux(compile_t* c, reach_type_t* t, token_id cap)
{
FIND_METHOD("linux", cap);
Expand Down Expand Up @@ -1307,6 +1318,7 @@ void genprim_platform_methods(compile_t* c, reach_type_t* t)
{
BOX_FUNCTION(platform_freebsd, t);
BOX_FUNCTION(platform_dragonfly, t);
BOX_FUNCTION(platform_openbsd, t);
BOX_FUNCTION(platform_linux, t);
BOX_FUNCTION(platform_osx, t);
BOX_FUNCTION(platform_windows, t);
Expand Down
1 change: 1 addition & 0 deletions src/libponyc/pass/pass.h
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,7 @@ typedef struct pass_opt_t
strlist_t* safe_packages;
magic_package_t* magic_packages;

const char* argv0;
const char* output;
const char* bin_name;
char* link_arch;
Expand Down
1 change: 1 addition & 0 deletions src/libponyc/pkg/buildflagset.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ static const char* _os_flags[] =
OS_BSD_NAME,
OS_FREEBSD_NAME,
OS_DRAGONFLY_NAME,
OS_OPENBSD_NAME,
OS_LINUX_NAME,
OS_MACOSX_NAME,
OS_WINDOWS_NAME,
Expand Down
2 changes: 1 addition & 1 deletion src/libponyc/pkg/package.c
Original file line number Diff line number Diff line change
Expand Up @@ -676,7 +676,7 @@ static bool add_safe(const char* path, pass_opt_t* opt)
static bool add_exec_dir(pass_opt_t* opt)
{
char path[FILENAME_MAX];
bool success = get_compiler_exe_directory(path);
bool success = get_compiler_exe_directory(path, opt->argv0);
errors_t* errors = opt->check.errors;

if(!success)
Expand Down
6 changes: 6 additions & 0 deletions src/libponyc/pkg/platformfuns.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ bool os_is_target(const char* attribute, bool release, bool* out_is_target, pass
return true;
}

if(!strcmp(attribute, OS_OPENBSD_NAME))
{
*out_is_target = target_is_openbsd(options->triple);
return true;
}

if(!strcmp(attribute, OS_LINUX_NAME))
{
*out_is_target = target_is_linux(options->triple);
Expand Down
1 change: 1 addition & 0 deletions src/libponyc/pkg/platformfuns.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ PONY_EXTERN_C_BEGIN
#define OS_BSD_NAME "bsd"
#define OS_FREEBSD_NAME "freebsd"
#define OS_DRAGONFLY_NAME "dragonfly"
#define OS_OPENBSD_NAME "openbsd"
#define OS_LINUX_NAME "linux"
#define OS_MACOSX_NAME "osx"
#define OS_WINDOWS_NAME "windows"
Expand Down
49 changes: 44 additions & 5 deletions src/libponyc/platform/paths.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <unistd.h>
#include <sys/types.h>
#include <sys/sysctl.h>
#include <sys/stat.h>
#endif

#ifdef PLATFORM_IS_WINDOWS
Expand Down Expand Up @@ -212,10 +213,11 @@ char* remove_ext(const char* path, char dot, char sep, size_t* allocated_size)
return retstr;
}

bool get_compiler_exe_path(char* output_path)
bool get_compiler_exe_path(char* output_path, const char* argv0)
{
bool success = false;
#ifdef PLATFORM_IS_WINDOWS
success = (argv0 == NULL) ? success : success; // hush compiler warning
#ifdef PLATFORM_IS_WINDOWS
// Specified size *includes* nul terminator
GetModuleFileName(NULL, output_path, FILENAME_MAX);
success = (GetLastError() == ERROR_SUCCESS);
Expand All @@ -226,6 +228,43 @@ bool get_compiler_exe_path(char* output_path)

if(success)
output_path[r] = '\0';
#elif defined PLATFORM_IS_OPENBSD
if (argv0 != NULL && (*argv0 == '/' || *argv0 == '.'))
{
if (pony_realpath(argv0, output_path) != NULL)
{
return true;
} else {
return false;
}
}
else {
char *env_path = getenv("PATH");
char *token, *string, *tofree;
char try_path[FILENAME_MAX];
struct stat sb;

if (env_path == NULL)
{
return false;
}
tofree = string = strdup(env_path);
while ((token = strsep(&string, ":")) != NULL)
{
snprintf(try_path, sizeof(try_path), "%s/%s", token, argv0);
if (access(try_path, X_OK) == 0 &&
stat(try_path, &sb) == 0 &&
(sb.st_mode & S_IFREG) == S_IFREG)
{
if (pony_realpath(try_path, output_path) != NULL)
{
success = true;
break;
}
}
}
free(tofree);
}
#elif defined PLATFORM_IS_BSD
int mib[4];
mib[0] = CTL_KERN;
Expand All @@ -252,9 +291,9 @@ bool get_compiler_exe_path(char* output_path)
return success;
}

bool get_compiler_exe_directory(char* output_path)
bool get_compiler_exe_directory(char* output_path, const char* argv0)
{
bool can_get_compiler_path = get_compiler_exe_path(output_path);
bool can_get_compiler_path = get_compiler_exe_path(output_path, argv0);
if (can_get_compiler_path)
{
char *p = strrchr(output_path, PATH_SLASH);
Expand All @@ -268,4 +307,4 @@ bool get_compiler_exe_directory(char* output_path)
} else {
return false;
}
}
}
7 changes: 6 additions & 1 deletion src/libponyrt/lang/socket.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,14 @@ typedef int SOCKET;
#include <linux/udp.h>
#endif
#ifdef PLATFORM_IS_BSD
#ifndef PLATFORM_IS_DRAGONFLY
#ifdef PLATFORM_IS_FREEBSD
#include <netinet/ip_mroute.h>
#include <netinet/sctp.h>
#elif defined(PLATFORM_IS_OPENBSD)
// Taken from FreeBSD
#define TCP_KEEPCNT 1024
#define TCP_KEEPIDLE 256
#define TCP_KEEPINTVL 512
#endif
#include <sys/socket.h>
#include <netinet/in.h>
Expand Down
3 changes: 3 additions & 0 deletions src/libponyrt/mem/alloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ void* ponyint_virt_alloc(size_t bytes)
#elif defined(PLATFORM_IS_DRAGONFLY)
p = mmap(0, bytes, PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANON, -1, 0);
#elif defined(PLATFORM_IS_OPENBSD)
p = mmap(0, bytes, PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANON, -1, 0);
#elif defined(PLATFORM_IS_BSD)
p = mmap(0, bytes, PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANON | MAP_ALIGNED_SUPER, -1, 0);
Expand Down
2 changes: 1 addition & 1 deletion src/libponyrt/platform/ponyassert.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ static PONY_ATOMIC(bool) assert_guard = false;

#ifdef PLATFORM_IS_POSIX_BASED

#ifdef PLATFORM_IS_BSD
#if defined(PLATFORM_IS_BSD) && !defined(PLATFORM_IS_OPENBSD)
typedef size_t stack_depth_t;
#else
typedef int stack_depth_t;
Expand Down
16 changes: 14 additions & 2 deletions src/libponyrt/sched/cpu.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ static uint32_t* avail_cpu_list;
static uint32_t avail_cpu_size;
#endif

#if defined(PLATFORM_IS_MACOSX) || defined(PLATFORM_IS_BSD)
#if defined(PLATFORM_IS_MACOSX) || (defined(PLATFORM_IS_BSD) && !defined(PLATFORM_IS_OPENBSD))

#include <sys/types.h>
#include <sys/sysctl.h>
Expand All @@ -40,6 +40,16 @@ static uint32_t property(const char* key)
}
#endif

#if defined(PLATFORM_IS_OPENBSD)

static uint32_t cpus_online(void)
{
int value = (int) sysconf(_SC_NPROCESSORS_ONLN);
return value;
}

#endif

static uint32_t hw_cpu_count;

#if defined(PLATFORM_IS_LINUX)
Expand Down Expand Up @@ -137,8 +147,10 @@ void ponyint_cpu_init()
i = cpu_add_mask_to_list(i, &hw_cpus);
i = cpu_add_mask_to_list(i, &ht_cpus);
}
#elif defined(PLATFORM_IS_BSD)
#elif defined(PLATFORM_IS_BSD) && !defined(PLATFORM_IS_OPENBSD)
hw_cpu_count = property("hw.ncpu");
#elif defined(PLATFORM_IS_OPENBSD)
hw_cpu_count = cpus_online();
#elif defined(PLATFORM_IS_MACOSX)
hw_cpu_count = property("hw.physicalcpu");
#elif defined(PLATFORM_IS_WINDOWS)
Expand Down
1 change: 1 addition & 0 deletions src/ponyc/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ int main(int argc, char* argv[])
opt.release = true;
opt.output = ".";
opt.ast_print_width = get_width();
opt.argv0 = argv[0];

ponyc_opt_process_t exit_code;
bool print_program_ast;
Expand Down