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

Some fixes for macOS #23

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
9 changes: 6 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ project(phosg)

set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED True)
if (MSVC)
if(MSVC)
# Disabled warnings:
# 4458 = declaration of '%s' hides class member (I use this->x for members)
add_compile_options(/W4 /WX /wd4458)
elseif(APPLE AND CMAKE_OSX_ARCHITECTURES MATCHES "ppc|ppc64")
add_compile_options(-fpermissive -Wall -Wextra -Wno-strict-aliasing -Wno-unused-result -Wno-overflow)
else()
add_compile_options(-Wall -Wextra -Werror -Wno-strict-aliasing -Wno-unused-result -Wno-overflow)
endif()
Expand All @@ -31,10 +33,11 @@ add_library(phosg src/Encoding.cc src/Filesystem.cc src/Hash.cc src/Image.cc src
target_link_libraries(phosg pthread z)

# It seems that on some Linux variants (e.g. Raspbian) we also need -latomic,
# but this library does not exist on others (e.g. Ubuntu) nor on macOS
# but this library does not exist on others (e.g. Ubuntu).
# Linking to libatomic is also needed on macOS ppc32. Use CMAKE_OSX_ARCHITECTURES to take care of Rosetta case.
message(STATUS "Target architecture is ${CMAKE_HOST_SYSTEM_PROCESSOR}")
string(FIND ${CMAKE_HOST_SYSTEM_PROCESSOR} "armv" IS_LINUX_ARMV)
if(${CMAKE_HOST_SYSTEM_PROCESSOR} STREQUAL "aarch64" OR ${IS_LINUX_ARMV} GREATER_EQUAL 0)
if(${CMAKE_HOST_SYSTEM_PROCESSOR} STREQUAL "aarch64" OR ${IS_LINUX_ARMV} GREATER_EQUAL 0 OR ${CMAKE_OSX_ARCHITECTURES} STREQUAL "ppc")
target_link_libraries(phosg atomic)
endif()

Expand Down
1 change: 1 addition & 0 deletions src/Encoding.hh
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once

#define __STDC_FORMAT_MACROS
#include <stdint.h>
#include <inttypes.h>

Expand Down
94 changes: 94 additions & 0 deletions src/Filesystem.cc
Original file line number Diff line number Diff line change
Expand Up @@ -678,3 +678,97 @@ unordered_map<int, short> Poll::poll(int timeout_ms) {
}
return ret;
}

#ifdef __APPLE__
#include <AvailabilityMacros.h>
#if MAC_OS_X_VERSION_MAX_ALLOWED < 101300
// https://github.com/NimbusKit/memorymapping/blob/master/src/fmemopen.c

#include <sys/mman.h>

struct fmem {
size_t pos;
size_t size;
char *buffer;
};
typedef struct fmem fmem_t;

static int readfn(void *handler, char *buf, int size) {
fmem_t *mem = handler;
size_t available = mem->size - mem->pos;

if (size > available) {
size = available;
}
memcpy(buf, mem->buffer + mem->pos, sizeof(char) * size);
mem->pos += size;

return size;
}

static int writefn(void *handler, const char *buf, int size) {
fmem_t *mem = handler;
size_t available = mem->size - mem->pos;

if (size > available) {
size = available;
}
memcpy(mem->buffer + mem->pos, buf, sizeof(char) * size);
mem->pos += size;

return size;
}

static fpos_t seekfn(void *handler, fpos_t offset, int whence) {
size_t pos;
fmem_t *mem = handler;

switch (whence) {
case SEEK_SET: {
if (offset >= 0) {
pos = (size_t)offset;
} else {
pos = 0;
}
break;
}
case SEEK_CUR: {
if (offset >= 0 || (size_t)(-offset) <= mem->pos) {
pos = mem->pos + (size_t)offset;
} else {
pos = 0;
}
break;
}
case SEEK_END: pos = mem->size + (size_t)offset; break;
default: return -1;
}

if (pos > mem->size) {
return -1;
}

mem->pos = pos;
return (fpos_t)pos;
}

static int closefn(void *handler) {
free(handler);
return 0;
}

FILE *fmemopen(void *buf, size_t size, const char *mode) {
// This data is released on fclose.
fmem_t* mem = (fmem_t *) malloc(sizeof(fmem_t));

// Zero-out the structure.
memset(mem, 0, sizeof(fmem_t));

mem->size = size;
mem->buffer = buf;

// funopen's man page: https://developer.apple.com/library/mac/#documentation/Darwin/Reference/ManPages/man3/funopen.3.html
return funopen(mem, readfn, writefn, seekfn, closefn);
}
#endif
#endif
13 changes: 13 additions & 0 deletions src/Filesystem.hh
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,19 @@
#include <utility>
#include <vector>

#ifdef __APPLE__
#include <AvailabilityMacros.h>
#if MAC_OS_X_VERSION_MAX_ALLOWED < 101300
// https://github.com/NimbusKit/memorymapping/blob/master/src/fmemopen.h
#if defined __cplusplus
extern "C" {
#endif
FILE *fmemopen(void *buf, size_t size, const char *mode);
#ifdef __cplusplus
}
#endif
#endif
#endif


std::string basename(const std::string& filename);
Expand Down
3 changes: 3 additions & 0 deletions src/Image.hh
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@

#include "Platform.hh"

#ifdef __APPLE__
typedef __darwin_ssize_t ssize_t;
#endif

// an Image represents a drawing canvas. this class is fairly simple; it
// supports reading/writing individual pixels, drawing lines, and saving the
Expand Down
1 change: 1 addition & 0 deletions src/Network.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

#include "Filesystem.hh"
#include "Strings.hh"
#include <cstring>

using namespace std;

Expand Down