Skip to content

Commit

Permalink
* fix: Possibility to compile on Mac OS platform
Browse files Browse the repository at this point in the history
  • Loading branch information
Dawid 'nCore' Opis committed Sep 16, 2015
1 parent 83790e5 commit 1997879
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 2 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,7 @@
*.i*86
*.x86_64
*.hex

# tuntox related, not needed in repo
tuntox
gitversion.h
15 changes: 14 additions & 1 deletion BUILD.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,17 @@
* cd tuntox
* make

The makefile creates a static binary by default. If you're not a fan of static binaries, remove "-static" from LDFLAGS. One reason to do so may be if you'd like to resolve hostnames on the tuntox server (invoke client with -L 80:reddit.com:80 instead of -L 80:198.41.208.138:80). Static linking breaks hostname resolution, but IMHO the pros overweight the cons.
The makefile creates a static binary by default. If you're not a fan of static binaries, remove `-static` from `LDFLAGS`.

One reason to do so may be if you'd like to resolve hostnames on the tuntox server (invoke client with `-L 80:reddit.com:80` instead of `-L 80:198.41.208.138:80`).

Static linking breaks hostname resolution, but IMHO the pros overweight the cons.

## MacOS build
Basically the same as above but:

* static compiling is removed - you can't do this on MacOS platform (no, just don't)
* because of removed `-static` you can't resolve hostnames (you can always put it into `hosts` file in your system)

If you'd like to build on Mac do: `make -f Makefile.mac`

28 changes: 28 additions & 0 deletions Makefile.mac
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
SOURCES = $(wildcard *.c)
DEPS=libtoxcore
CC=gcc
CFLAGS=-g #-std=c99
CFLAGS += $(shell pkg-config --cflags $(DEPS))
LDFLAGS=-g -lm
LDFLAGS += $(shell pkg-config --libs $(DEPS))
OBJECTS=$(SOURCES:.c=.o)
INCLUDES = $(wildcard *.h)

all: cscope.out tuntox

gitversion.h: .git/HEAD .git/index
echo "#define GITVERSION \"$(shell git rev-parse HEAD)\"" > $@

gitversion.c: gitversion.h

.c.o: $(INCLUDES)
$(CC) $(CFLAGS) $< -c -o $@

tuntox: $(OBJECTS) $(INCLUDES)
$(CC) -o $@ $(OBJECTS) -ltoxcore $(LDFLAGS) /usr/local/lib/libsodium.a /usr/local/lib/libtoxcore.a

cscope.out:
cscope -bv ./*.[ch]

clean:
rm -rf *.o tuntox gitversion.h
6 changes: 6 additions & 0 deletions client.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
#include <time.h>

/* MacOS related */
#ifdef __MACH__
#include "mach.h"
#endif

#include "log.h"
#include "main.h"
#include "client.h"
Expand Down
20 changes: 20 additions & 0 deletions mach.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#include "mach.h"

/* there is no clock_gettime on MacOS platform */
int clock_gettime(int clk_id, struct timespec *t)
{
mach_timebase_info_data_t timebase;
mach_timebase_info(&timebase);

uint64_t time;

time = mach_absolute_time();

double nseconds = ((double)time * (double)timebase.numer) / ((double)timebase.denom);
double seconds = ((double)time * (double)timebase.numer) / ((double)timebase.denom * 1e9);

t->tv_sec = seconds;
t->tv_nsec = nseconds;

return 0;
}
14 changes: 14 additions & 0 deletions mach.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#ifndef _MACH_H
#define _MACH_H

#include <time.h>
#include <mach/mach_time.h>

// there is no CLOCK_REALTIME or CLOCK_MONOTONIC on MacOS platform
#define CLOCK_REALTIME 0
#define CLOCK_MONOTONIC 0

// MacOS doesn't support the flag MSG_NOSIGNAL
#define MSG_NOSIGNAL SO_NOSIGPIPE

#endif
6 changes: 5 additions & 1 deletion main.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
#include "tox_bootstrap.h"
#include "log.h"

#ifdef __MACH__
#include "mach.h"
#endif

static struct Tox_Options tox_options;
Tox *tox;
int client_socket = 0;
Expand Down Expand Up @@ -1108,7 +1112,7 @@ int main(int argc, char *argv[])
do_daemonize();
}

on_exit(cleanup, NULL);
atexit(cleanup);

print_version();

Expand Down

0 comments on commit 1997879

Please sign in to comment.