Commit 704cbb60 authored by Kirill Smelkov's avatar Kirill Smelkov

*: Centralize detection of OS and compiler in golang/runtime/platform.h

Up until now we had scattered ifdef __linux__, ifdef __APPLE__, ifdef
_MSC_VER etc. And in the future we will need to detect more things with
more involved conditions.

-> Factor the code, that detects OS and compiler, into one place as a
preparatory step for that.
parent d2c36212
...@@ -2,6 +2,7 @@ include COPYING README.rst CHANGELOG.rst tox.ini pyproject.toml trun .lsan-ignor ...@@ -2,6 +2,7 @@ include COPYING README.rst CHANGELOG.rst tox.ini pyproject.toml trun .lsan-ignor
include golang/libgolang.h include golang/libgolang.h
include golang/runtime/libgolang.cpp include golang/runtime/libgolang.cpp
include golang/runtime/libpyxruntime.cpp include golang/runtime/libpyxruntime.cpp
include golang/runtime/platform.h
include golang/pyx/runtime.h include golang/pyx/runtime.h
include golang/pyx/testprog/golang_dso_user/dsouser/dso.h include golang/pyx/testprog/golang_dso_user/dsouser/dso.h
include golang/pyx/testprog/golang_dso_user/dsouser/dso.cpp include golang/pyx/testprog/golang_dso_user/dsouser/dso.cpp
......
#ifndef _NXD_LIBGOLANG_FMT_H #ifndef _NXD_LIBGOLANG_FMT_H
#define _NXD_LIBGOLANG_FMT_H #define _NXD_LIBGOLANG_FMT_H
// Copyright (C) 2019-2023 Nexedi SA and Contributors. // Copyright (C) 2019-2024 Nexedi SA and Contributors.
// Kirill Smelkov <kirr@nexedi.com> // Kirill Smelkov <kirr@nexedi.com>
// //
// This program is free software: you can Use, Study, Modify and Redistribute // This program is free software: you can Use, Study, Modify and Redistribute
...@@ -111,7 +111,7 @@ inline error errorf(const string& format, Argv... argv) { ...@@ -111,7 +111,7 @@ inline error errorf(const string& format, Argv... argv) {
// `const char *` overloads just to catch format mistakes as // `const char *` overloads just to catch format mistakes as
// __attribute__(format) does not work with std::string. // __attribute__(format) does not work with std::string.
LIBGOLANG_API string sprintf(const char *format, ...) LIBGOLANG_API string sprintf(const char *format, ...)
#ifndef _MSC_VER #ifndef LIBGOLANG_CC_msc
__attribute__ ((format (printf, 1, 2))) __attribute__ ((format (printf, 1, 2)))
#endif #endif
; ;
......
...@@ -169,6 +169,8 @@ ...@@ -169,6 +169,8 @@
// [1] Libtask: a Coroutine Library for C and Unix. https://swtch.com/libtask. // [1] Libtask: a Coroutine Library for C and Unix. https://swtch.com/libtask.
// [2] http://9p.io/magic/man2html/2/thread. // [2] http://9p.io/magic/man2html/2/thread.
#include "golang/runtime/platform.h"
#include <stdbool.h> #include <stdbool.h>
#include <stddef.h> #include <stddef.h>
#include <stdint.h> #include <stdint.h>
...@@ -177,21 +179,18 @@ ...@@ -177,21 +179,18 @@
#include <sys/stat.h> #include <sys/stat.h>
#include <fcntl.h> #include <fcntl.h>
#ifdef _MSC_VER // no mode_t on msvc #ifdef LIBGOLANG_CC_msc // no mode_t on msvc
typedef int mode_t; typedef int mode_t;
#endif #endif
// DSO symbols visibility (based on https://gcc.gnu.org/wiki/Visibility) // DSO symbols visibility (based on https://gcc.gnu.org/wiki/Visibility)
#if defined _WIN32 || defined __CYGWIN__ #ifdef LIBGOLANG_OS_windows
#define LIBGOLANG_DSO_EXPORT __declspec(dllexport) #define LIBGOLANG_DSO_EXPORT __declspec(dllexport)
#define LIBGOLANG_DSO_IMPORT __declspec(dllimport) #define LIBGOLANG_DSO_IMPORT __declspec(dllimport)
#elif __GNUC__ >= 4 #else
#define LIBGOLANG_DSO_EXPORT __attribute__ ((visibility ("default"))) #define LIBGOLANG_DSO_EXPORT __attribute__ ((visibility ("default")))
#define LIBGOLANG_DSO_IMPORT __attribute__ ((visibility ("default"))) #define LIBGOLANG_DSO_IMPORT __attribute__ ((visibility ("default")))
#else
#define LIBGOLANG_DSO_EXPORT
#define LIBGOLANG_DSO_IMPORT
#endif #endif
#if BUILDING_LIBGOLANG #if BUILDING_LIBGOLANG
......
// Copyright (C) 2019-2023 Nexedi SA and Contributors. // Copyright (C) 2019-2024 Nexedi SA and Contributors.
// Kirill Smelkov <kirr@nexedi.com> // Kirill Smelkov <kirr@nexedi.com>
// //
// This program is free software: you can Use, Study, Modify and Redistribute // This program is free software: you can Use, Study, Modify and Redistribute
...@@ -38,7 +38,7 @@ ...@@ -38,7 +38,7 @@
// cut this short // cut this short
// (on darwing sys_siglist declaration is normally provided) // (on darwing sys_siglist declaration is normally provided)
// (on windows sys_siglist is not available at all) // (on windows sys_siglist is not available at all)
#if !(defined(__APPLE__) || defined(_WIN32)) #if !(defined(LIBGOLANG_OS_darwin) || defined(LIBGOLANG_OS_windows))
extern "C" { extern "C" {
extern const char * const sys_siglist[]; extern const char * const sys_siglist[];
} }
...@@ -287,7 +287,7 @@ string Signal::String() const { ...@@ -287,7 +287,7 @@ string Signal::String() const {
const Signal& sig = *this; const Signal& sig = *this;
const char *sigstr = nil; const char *sigstr = nil;
#ifdef _WIN32 #ifdef LIBGOLANG_OS_windows
switch (sig.signo) { switch (sig.signo) {
case SIGABRT: return "Aborted"; case SIGABRT: return "Aborted";
case SIGBREAK: return "Break"; case SIGBREAK: return "Break";
......
#ifndef _NXD_LIBGOLANG_OS_H #ifndef _NXD_LIBGOLANG_OS_H
#define _NXD_LIBGOLANG_OS_H #define _NXD_LIBGOLANG_OS_H
// //
// Copyright (C) 2019-2023 Nexedi SA and Contributors. // Copyright (C) 2019-2024 Nexedi SA and Contributors.
// Kirill Smelkov <kirr@nexedi.com> // Kirill Smelkov <kirr@nexedi.com>
// //
// This program is free software: you can Use, Study, Modify and Redistribute // This program is free software: you can Use, Study, Modify and Redistribute
...@@ -96,7 +96,7 @@ private: ...@@ -96,7 +96,7 @@ private:
// Open opens file @path. // Open opens file @path.
LIBGOLANG_API std::tuple<File, error> Open(const string &path, int flags = O_RDONLY, LIBGOLANG_API std::tuple<File, error> Open(const string &path, int flags = O_RDONLY,
mode_t mode = mode_t mode =
#if !defined(_MSC_VER) #if !defined(LIBGOLANG_CC_msc)
S_IRUSR | S_IWUSR | S_IXUSR | S_IRUSR | S_IWUSR | S_IXUSR |
S_IRGRP | S_IWGRP | S_IXGRP | S_IRGRP | S_IWGRP | S_IXGRP |
S_IROTH | S_IWOTH | S_IXOTH S_IROTH | S_IWOTH | S_IXOTH
......
// Copyright (C) 2021-2023 Nexedi SA and Contributors. // Copyright (C) 2021-2024 Nexedi SA and Contributors.
// Kirill Smelkov <kirr@nexedi.com> // Kirill Smelkov <kirr@nexedi.com>
// //
// This program is free software: you can Use, Study, Modify and Redistribute // This program is free software: you can Use, Study, Modify and Redistribute
...@@ -89,7 +89,7 @@ ...@@ -89,7 +89,7 @@
#include <atomic> #include <atomic>
#include <tuple> #include <tuple>
#if defined(_WIN32) #if defined(LIBGOLANG_OS_windows)
# include <windows.h> # include <windows.h>
#endif #endif
...@@ -101,7 +101,7 @@ ...@@ -101,7 +101,7 @@
# define debugf(format, ...) do {} while (0) # define debugf(format, ...) do {} while (0)
#endif #endif
#if defined(_MSC_VER) #ifdef LIBGOLANG_CC_msc
# define HAVE_SIGACTION 0 # define HAVE_SIGACTION 0
#else #else
# define HAVE_SIGACTION 1 # define HAVE_SIGACTION 1
...@@ -194,7 +194,7 @@ void _init() { ...@@ -194,7 +194,7 @@ void _init() {
if (err != nil) if (err != nil)
panic("os::newFile(_wakerx"); panic("os::newFile(_wakerx");
_waketx = vfd[1]; _waketx = vfd[1];
#ifndef _WIN32 #ifndef LIBGOLANG_OS_windows
if (sys::Fcntl(_waketx, F_SETFL, O_NONBLOCK) < 0) if (sys::Fcntl(_waketx, F_SETFL, O_NONBLOCK) < 0)
panic("fcntl(_waketx, O_NONBLOCK)"); // TODO +syserr panic("fcntl(_waketx, O_NONBLOCK)"); // TODO +syserr
#else #else
......
# Copyright (C) 2019-2023 Nexedi SA and Contributors. # Copyright (C) 2019-2024 Nexedi SA and Contributors.
# Kirill Smelkov <kirr@nexedi.com> # Kirill Smelkov <kirr@nexedi.com>
# #
# This program is free software: you can Use, Study, Modify and Redistribute # This program is free software: you can Use, Study, Modify and Redistribute
...@@ -215,6 +215,7 @@ def _with_build_defaults(name, kw): # -> (pygo, kw') ...@@ -215,6 +215,7 @@ def _with_build_defaults(name, kw): # -> (pygo, kw')
'runtime/internal.h', 'runtime/internal.h',
'runtime/internal/atomic.h', 'runtime/internal/atomic.h',
'runtime/internal/syscall.h', 'runtime/internal/syscall.h',
'runtime/platform.h',
'context.h', 'context.h',
'cxx.h', 'cxx.h',
'errors.h', 'errors.h',
......
// Copyright (C) 2022-2023 Nexedi SA and Contributors. // Copyright (C) 2022-2024 Nexedi SA and Contributors.
// Kirill Smelkov <kirr@nexedi.com> // Kirill Smelkov <kirr@nexedi.com>
// //
// This program is free software: you can Use, Study, Modify and Redistribute // This program is free software: you can Use, Study, Modify and Redistribute
...@@ -20,7 +20,7 @@ ...@@ -20,7 +20,7 @@
#include "golang/runtime/internal/atomic.h" #include "golang/runtime/internal/atomic.h"
#include "golang/libgolang.h" #include "golang/libgolang.h"
#ifndef _WIN32 #ifndef LIBGOLANG_OS_windows
#include <pthread.h> #include <pthread.h>
#endif #endif
...@@ -44,7 +44,7 @@ static void _forkNewEpoch() { ...@@ -44,7 +44,7 @@ static void _forkNewEpoch() {
void _init() { void _init() {
// there is no fork on windows // there is no fork on windows
#ifndef _WIN32 #ifndef LIBGOLANG_OS_windows
int e = pthread_atfork(/*prepare*/nil, /*inparent*/nil, /*inchild*/_forkNewEpoch); int e = pthread_atfork(/*prepare*/nil, /*inparent*/nil, /*inchild*/_forkNewEpoch);
if (e != 0) if (e != 0)
panic("pthread_atfork failed"); panic("pthread_atfork failed");
......
// Copyright (C) 2021-2023 Nexedi SA and Contributors. // Copyright (C) 2021-2024 Nexedi SA and Contributors.
// Kirill Smelkov <kirr@nexedi.com> // Kirill Smelkov <kirr@nexedi.com>
// //
// This program is free software: you can Use, Study, Modify and Redistribute // This program is free software: you can Use, Study, Modify and Redistribute
...@@ -58,9 +58,9 @@ string _Errno::Error() { ...@@ -58,9 +58,9 @@ string _Errno::Error() {
char ebuf[128]; char ebuf[128];
bool ok; bool ok;
#if __APPLE__ #ifdef LIBGOLANG_OS_darwin
ok = (::strerror_r(-e.syserr, ebuf, sizeof(ebuf)) == 0); ok = (::strerror_r(-e.syserr, ebuf, sizeof(ebuf)) == 0);
#elif defined(_WIN32) #elif defined(LIBGOLANG_OS_windows)
ok = (::strerror_s(ebuf, sizeof(ebuf), -e.syserr) == 0); ok = (::strerror_s(ebuf, sizeof(ebuf), -e.syserr) == 0);
#else #else
char *estr = ::strerror_r(-e.syserr, ebuf, sizeof(ebuf)); char *estr = ::strerror_r(-e.syserr, ebuf, sizeof(ebuf));
...@@ -102,7 +102,7 @@ __Errno Close(int fd) { ...@@ -102,7 +102,7 @@ __Errno Close(int fd) {
return err; return err;
} }
#ifndef _WIN32 #ifndef LIBGOLANG_OS_windows
__Errno Fcntl(int fd, int cmd, int arg) { __Errno Fcntl(int fd, int cmd, int arg) {
int save_errno = errno; int save_errno = errno;
int err = ::fcntl(fd, cmd, arg); int err = ::fcntl(fd, cmd, arg);
...@@ -124,7 +124,7 @@ __Errno Fstat(int fd, struct ::stat *out_st) { ...@@ -124,7 +124,7 @@ __Errno Fstat(int fd, struct ::stat *out_st) {
int Open(const char *path, int flags, mode_t mode) { int Open(const char *path, int flags, mode_t mode) {
int save_errno = errno; int save_errno = errno;
#ifdef _WIN32 // default to open files in binary mode #ifdef LIBGOLANG_OS_windows // default to open files in binary mode
if ((flags & (_O_TEXT | _O_BINARY)) == 0) if ((flags & (_O_TEXT | _O_BINARY)) == 0)
flags |= _O_BINARY; flags |= _O_BINARY;
#endif #endif
...@@ -141,9 +141,9 @@ __Errno Pipe(int vfd[2], int flags) { ...@@ -141,9 +141,9 @@ __Errno Pipe(int vfd[2], int flags) {
return -EINVAL; return -EINVAL;
int save_errno = errno; int save_errno = errno;
int err; int err;
#ifdef __linux__ #ifdef LIBGOLANG_OS_linux
err = ::pipe2(vfd, flags); err = ::pipe2(vfd, flags);
#elif defined(_WIN32) #elif defined(LIBGOLANG_OS_windows)
err = ::_pipe(vfd, 4096, flags | _O_BINARY); err = ::_pipe(vfd, 4096, flags | _O_BINARY);
#else #else
err = ::pipe(vfd); err = ::pipe(vfd);
...@@ -167,7 +167,7 @@ out: ...@@ -167,7 +167,7 @@ out:
return err; return err;
} }
#ifndef _WIN32 #ifndef LIBGOLANG_OS_windows
__Errno Sigaction(int signo, const struct ::sigaction *act, struct ::sigaction *oldact) { __Errno Sigaction(int signo, const struct ::sigaction *act, struct ::sigaction *oldact) {
int save_errno = errno; int save_errno = errno;
int err = ::sigaction(signo, act, oldact); int err = ::sigaction(signo, act, oldact);
......
#ifndef _NXD_LIBGOLANG_RUNTIME_INTERNAL_SYSCALL_H #ifndef _NXD_LIBGOLANG_RUNTIME_INTERNAL_SYSCALL_H
#define _NXD_LIBGOLANG_RUNTIME_INTERNAL_SYSCALL_H #define _NXD_LIBGOLANG_RUNTIME_INTERNAL_SYSCALL_H
// Copyright (C) 2021-2023 Nexedi SA and Contributors. // Copyright (C) 2021-2024 Nexedi SA and Contributors.
// Kirill Smelkov <kirr@nexedi.com> // Kirill Smelkov <kirr@nexedi.com>
// //
// This program is free software: you can Use, Study, Modify and Redistribute // This program is free software: you can Use, Study, Modify and Redistribute
...@@ -63,13 +63,13 @@ LIBGOLANG_API int/*n|err*/ Read(int fd, void *buf, size_t count); ...@@ -63,13 +63,13 @@ LIBGOLANG_API int/*n|err*/ Read(int fd, void *buf, size_t count);
LIBGOLANG_API int/*n|err*/ Write(int fd, const void *buf, size_t count); LIBGOLANG_API int/*n|err*/ Write(int fd, const void *buf, size_t count);
LIBGOLANG_API __Errno Close(int fd); LIBGOLANG_API __Errno Close(int fd);
#ifndef _WIN32 #ifndef LIBGOLANG_OS_windows
LIBGOLANG_API __Errno Fcntl(int fd, int cmd, int arg); LIBGOLANG_API __Errno Fcntl(int fd, int cmd, int arg);
#endif #endif
LIBGOLANG_API __Errno Fstat(int fd, struct ::stat *out_st); LIBGOLANG_API __Errno Fstat(int fd, struct ::stat *out_st);
LIBGOLANG_API int/*fd|err*/ Open(const char *path, int flags, mode_t mode); LIBGOLANG_API int/*fd|err*/ Open(const char *path, int flags, mode_t mode);
LIBGOLANG_API __Errno Pipe(int vfd[2], int flags); LIBGOLANG_API __Errno Pipe(int vfd[2], int flags);
#ifndef _WIN32 #ifndef LIBGOLANG_OS_windows
LIBGOLANG_API __Errno Sigaction(int signo, const struct ::sigaction *act, struct ::sigaction *oldact); LIBGOLANG_API __Errno Sigaction(int signo, const struct ::sigaction *act, struct ::sigaction *oldact);
#endif #endif
typedef void (*sighandler_t)(int); typedef void (*sighandler_t)(int);
......
...@@ -52,7 +52,7 @@ ...@@ -52,7 +52,7 @@
#include <linux/list.h> #include <linux/list.h>
// MSVC does not support statement expressions and typeof // MSVC does not support statement expressions and typeof
// -> redo list_entry via C++ lambda. // -> redo list_entry via C++ lambda.
#ifdef _MSC_VER #ifdef LIBGOLANG_CC_msc
# undef list_entry # undef list_entry
# define list_entry(ptr, type, member) [&]() { \ # define list_entry(ptr, type, member) [&]() { \
const decltype( ((type *)0)->member ) *__mptr = (ptr); \ const decltype( ((type *)0)->member ) *__mptr = (ptr); \
......
#ifndef _NXD_LIBGOLANG_RUNTIME_PLATFORM_H
#define _NXD_LIBGOLANG_RUNTIME_PLATFORM_H
// Copyright (C) 2023-2024 Nexedi SA and Contributors.
// Kirill Smelkov <kirr@nexedi.com>
//
// This program is free software: you can Use, Study, Modify and Redistribute
// it under the terms of the GNU General Public License version 3, or (at your
// option) any later version, as published by the Free Software Foundation.
//
// You can also Link and Combine this program with other software covered by
// the terms of any of the Free Software licenses or any of the Open Source
// Initiative approved licenses and Convey the resulting work. Corresponding
// source of such a combination shall include the source code for all other
// software used.
//
// This program is distributed WITHOUT ANY WARRANTY; without even the implied
// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
//
// See COPYING file for full licensing terms.
// See https://www.nexedi.com/licensing for rationale and options.
// Header platform.h provides preprocessor defines that describe target platform.
// LIBGOLANG_OS_<X> is defined on operating system X.
//
// List of supported operating systems: linux, darwin, windows.
#ifdef __linux__
# define LIBGOLANG_OS_linux 1
#elif defined(__APPLE__)
# define LIBGOLANG_OS_darwin 1
#elif defined(_WIN32) || defined(__CYGWIN__)
# define LIBGOLANG_OS_windows 1
#else
# error "unsupported operating system"
#endif
// LIBGOLANG_CC_<X> is defined on C/C++ compiler X.
//
// List of supported compilers: gcc, clang, msc.
#ifdef __clang__
# define LIBGOLANG_CC_clang 1
#elif defined(_MSC_VER)
# define LIBGOLANG_CC_msc 1
// NOTE gcc comes last because e.g. clang and icc define __GNUC__ as well
#elif __GNUC__
# define LIBGOLANG_CC_gcc 1
#else
# error "unsupported compiler"
#endif
#endif // _NXD_LIBGOLANG_RUNTIME_PLATFORM_H
...@@ -241,6 +241,7 @@ setup( ...@@ -241,6 +241,7 @@ setup(
'golang/runtime/internal.h', 'golang/runtime/internal.h',
'golang/runtime/internal/atomic.h', 'golang/runtime/internal/atomic.h',
'golang/runtime/internal/syscall.h', 'golang/runtime/internal/syscall.h',
'golang/runtime/platform.h',
'golang/context.h', 'golang/context.h',
'golang/cxx.h', 'golang/cxx.h',
'golang/errors.h', 'golang/errors.h',
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment