Skip to content

Commit f4affb0

Browse files
committed
Merge pull request xapi-project#7 from djs55/portable-osx
Fix build on OS X
2 parents aac0657 + 734cf4a commit f4affb0

File tree

8 files changed

+212
-82
lines changed

8 files changed

+212
-82
lines changed

_oasis

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ Plugins: DevFiles (0.3), META (0.3)
1111
Library stdext
1212
Path: lib
1313
Modules: Arrayext, Backtrace, Base64, Bigbuffer, Config, Date, Either, Encodings, ExtentlistSet, Filenameext, Fring, Fun, Hashtblext, Int64ext, LazyList, Listext, Mapext, Monad, Opt, Pervasiveext, Qring, Range, Ring, Xstringext, Threadext, Trie, Unixext, VIO, Zerocheck
14-
CSources: unixext_open_stubs.c, unixext_stubs.c, unixext_write_stubs.c, zerocheck_stub.c
14+
CSources: blkgetsize_stubs.c, unixext_open_stubs.c, unixext_stubs.c, unixext_write_stubs.c, zerocheck_stub.c
1515
BuildDepends: threads, uuidm, unix, fd-send-recv, bigarray
1616

1717

_tags

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# OASIS_START
2-
# DO NOT EDIT (digest: 97b078caaf9aa5877a841adb67d7deb6)
2+
# DO NOT EDIT (digest: ad1a1bbbb71d472f0711580d2cc59333)
33
# Ignore VCS directories, you can use the same kind of rule outside
44
# OASIS_START/STOP if you want to exclude directories that contains
55
# useless stuff for the build process
@@ -21,6 +21,11 @@
2121
<lib/*.ml{,i}>: pkg_threads
2222
<lib/*.ml{,i}>: pkg_unix
2323
<lib/*.ml{,i}>: pkg_uuidm
24+
"lib/blkgetsize_stubs.c": pkg_bigarray
25+
"lib/blkgetsize_stubs.c": pkg_fd-send-recv
26+
"lib/blkgetsize_stubs.c": pkg_threads
27+
"lib/blkgetsize_stubs.c": pkg_unix
28+
"lib/blkgetsize_stubs.c": pkg_uuidm
2429
"lib/unixext_open_stubs.c": pkg_bigarray
2530
"lib/unixext_open_stubs.c": pkg_fd-send-recv
2631
"lib/unixext_open_stubs.c": pkg_threads

lib/blkgetsize_stubs.c

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
/*
2+
* Copyright (C) 2012-2013 Citrix Inc
3+
*
4+
* Permission to use, copy, modify, and distribute this software for any
5+
* purpose with or without fee is hereby granted, provided that the above
6+
* copyright notice and this permission notice appear in all copies.
7+
*
8+
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9+
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10+
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11+
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12+
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13+
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14+
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15+
*/
16+
17+
#include <sys/types.h>
18+
#include <sys/stat.h>
19+
#include <sys/ioctl.h>
20+
21+
#include <fcntl.h>
22+
#include <string.h>
23+
#include <unistd.h>
24+
#include <stdint.h>
25+
26+
#include <caml/alloc.h>
27+
#include <caml/memory.h>
28+
#include <caml/signals.h>
29+
#include <caml/fail.h>
30+
#include <caml/callback.h>
31+
#include <caml/bigarray.h>
32+
33+
#ifdef __linux__
34+
#include <linux/fs.h>
35+
36+
int blkgetsize(int fd, uint64_t *psize)
37+
{
38+
#ifdef BLKGETSIZE64
39+
int ret = ioctl(fd, BLKGETSIZE64, psize);
40+
#elif BLKGETSIZE
41+
unsigned long sectors = 0;
42+
int ret = ioctl(fd, BLKGETSIZE, &sectors);
43+
*psize = sectors * 512ULL;
44+
#else
45+
# error "Linux configuration error (blkgetsize)"
46+
#endif
47+
return ret;
48+
}
49+
50+
#elif defined(__APPLE__)
51+
#include <sys/disk.h>
52+
53+
int blkgetsize(int fd, uint64_t *psize)
54+
{
55+
unsigned long blocksize = 0;
56+
int ret = ioctl(fd, DKIOCGETBLOCKSIZE, &blocksize);
57+
if (!ret) {
58+
unsigned long nblocks;
59+
ret = ioctl(fd, DKIOCGETBLOCKCOUNT, &nblocks);
60+
if (!ret)
61+
*psize = (uint64_t)nblocks * blocksize;
62+
}
63+
return ret;
64+
}
65+
66+
#elif defined(__FreeBSD__)
67+
#include <sys/disk.h>
68+
69+
int blkgetsize(int fd, uint64_t *psize)
70+
{
71+
int ret = ioctl(fd, DIOCGMEDIASIZE, psize);
72+
return ret;
73+
}
74+
75+
#else
76+
# error "Unable to query block device size: unsupported platform, please report."
77+
#endif
78+
79+
/* ocaml/ocaml/unixsupport.c */
80+
extern void uerror(char *cmdname, value cmdarg);
81+
#define Nothing ((value) 0)
82+
83+
CAMLprim value stub_blkgetsize(value filename){
84+
CAMLparam1(filename);
85+
CAMLlocal1(result);
86+
uint64_t size_in_bytes;
87+
int fd;
88+
int success = -1;
89+
90+
const char *filename_c = strdup(String_val(filename));
91+
92+
enter_blocking_section();
93+
fd = open(filename_c, O_RDONLY, 0);
94+
if (blkgetsize(fd, &size_in_bytes) == 0)
95+
success = 0;
96+
close(fd);
97+
leave_blocking_section();
98+
99+
free((void*)filename_c);
100+
101+
if (fd == -1) uerror("open", filename);
102+
if (success == -1) uerror("BLKGETSIZE", filename);
103+
104+
result = caml_copy_int64(size_in_bytes);
105+
CAMLreturn(result);
106+
}

lib/libstdext_stubs.clib

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# OASIS_START
2-
# DO NOT EDIT (digest: 6a87487702231dfad360de7126120d50)
2+
# DO NOT EDIT (digest: b4d5196086bdd5d02be639885c7103de)
3+
blkgetsize_stubs.o
34
unixext_open_stubs.o
45
unixext_stubs.o
56
unixext_write_stubs.o

lib/unixext_open_stubs.c

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,17 +44,27 @@ static int open_flag_table[] = {
4444
CAMLprim value stub_stdext_unix_open_direct(value path, value flags, value perm)
4545
{
4646
CAMLparam3(path, flags, perm);
47-
int ret, cv_flags;
47+
int fd, ret, cv_flags;
4848
char * p;
4949

50-
cv_flags = convert_flag_list(flags, open_flag_table) | O_DIRECT;
50+
cv_flags = convert_flag_list(flags, open_flag_table);
51+
52+
#ifdef O_DIRECT
53+
cv_flags |= O_DIRECT;
54+
#endif
5155
p = stat_alloc(string_length(path) + 1);
5256
strcpy(p, String_val(path));
5357
/* open on a named FIFO can block (PR#1533) */
5458
enter_blocking_section();
55-
ret = open(p, cv_flags, Int_val(perm));
59+
fd = open(p, cv_flags, Int_val(perm));
60+
#ifndef O_DIRECT
61+
if (fd != -1)
62+
ret = fcntl(fd, F_NOCACHE);
63+
#endif
5664
leave_blocking_section();
5765
stat_free(p);
58-
if (ret == -1) uerror("open", path);
59-
CAMLreturn (Val_int(ret));
66+
if (fd == -1) uerror("open", path);
67+
if (ret == -1) uerror("fcntl", path);
68+
69+
CAMLreturn (Val_int(fd));
6070
}

lib/unixext_stubs.c

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@
2222
#include <stdio.h> /* snprintf */
2323
#include <sys/ioctl.h>
2424
#include <sys/statvfs.h>
25-
#include <linux/fs.h>
25+
#if defined(__linux__)
26+
# include <linux/fs.h>
27+
#endif
2628

2729
#include <caml/mlvalues.h>
2830
#include <caml/memory.h>
@@ -53,12 +55,15 @@ CAMLprim value stub_unixext_fsync (value fd)
5355
CAMLreturn(Val_unit);
5456
}
5557

58+
extern uint64_t blkgetsize(int fd, uint64_t *psize);
59+
5660
CAMLprim value stub_unixext_blkgetsize64(value fd)
5761
{
5862
CAMLparam1(fd);
5963
uint64_t size;
6064
int c_fd = Int_val(fd);
61-
if(ioctl(c_fd,BLKGETSIZE64,&size)) {
65+
/* mirage-block-unix binding: */
66+
if (blkgetsize(c_fd, &size)) {
6267
uerror("ioctl(BLKGETSIZE64)", Nothing);
6368
}
6469
CAMLreturn(caml_copy_int64(size));
@@ -72,6 +77,14 @@ CAMLprim value stub_unixext_get_max_fd (value unit)
7277
CAMLreturn(Val_int(maxfd));
7378
}
7479

80+
#if defined(__linux__)
81+
# define TCP_LEVEL SOL_TCP
82+
#elif defined(__APPLE__)
83+
# define TCP_LEVEL IPPROTO_TCP
84+
#else
85+
# error "Don't know how to use setsockopt on this platform"
86+
#endif
87+
7588
CAMLprim value stub_unixext_set_sock_keepalives(value fd, value count, value idle, value interval)
7689
{
7790
CAMLparam4(fd, count, idle, interval);
@@ -81,17 +94,17 @@ CAMLprim value stub_unixext_set_sock_keepalives(value fd, value count, value idl
8194
socklen_t optlen=sizeof(optval);
8295

8396
optval = Int_val(count);
84-
if(setsockopt(c_fd, SOL_TCP, TCP_KEEPCNT, &optval, optlen) < 0) {
97+
if(setsockopt(c_fd, TCP_LEVEL, TCP_KEEPCNT, &optval, optlen) < 0) {
8598
uerror("setsockopt(TCP_KEEPCNT)", Nothing);
8699
}
87-
100+
#if defined(__linux__)
88101
optval = Int_val(idle);
89-
if(setsockopt(c_fd, SOL_TCP, TCP_KEEPIDLE, &optval, optlen) < 0) {
102+
if(setsockopt(c_fd, TCP_LEVEL, TCP_KEEPIDLE, &optval, optlen) < 0) {
90103
uerror("setsockopt(TCP_KEEPIDLE)", Nothing);
91104
}
92-
105+
#endif
93106
optval = Int_val(interval);
94-
if(setsockopt(c_fd, SOL_TCP, TCP_KEEPINTVL, &optval, optlen) < 0) {
107+
if(setsockopt(c_fd, TCP_LEVEL, TCP_KEEPINTVL, &optval, optlen) < 0) {
95108
uerror("setsockopt(TCP_KEEPINTVL)", Nothing);
96109
}
97110

@@ -299,12 +312,6 @@ CAMLprim value stub_fdset_is_empty(value set)
299312
CAMLreturn(Bool_val(ret == 0));
300313
}
301314

302-
static int msg_flag_table[] = {
303-
MSG_OOB, MSG_DONTROUTE, MSG_PEEK
304-
};
305-
306-
#define UNIX_BUFFER_SIZE 16384
307-
308315
CAMLprim value stub_statvfs(value filename)
309316
{
310317
CAMLparam1(filename);

myocamlbuild.ml

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
(* OASIS_START *)
2-
(* DO NOT EDIT (digest: fe2b018630989841943e3a87fe69634e) *)
2+
(* DO NOT EDIT (digest: b8faf3da52e902fb96e77b14b83140c9) *)
33
module OASISGettext = struct
44
(* # 22 "src/oasis/OASISGettext.ml" *)
55

@@ -39,10 +39,10 @@ module OASISExpr = struct
3939
open OASISGettext
4040

4141

42-
type test = string
42+
type test = string
4343

4444

45-
type flag = string
45+
type flag = string
4646

4747

4848
type t =
@@ -52,10 +52,10 @@ module OASISExpr = struct
5252
| EOr of t * t
5353
| EFlag of flag
5454
| ETest of test * string
55-
5655

5756

58-
type 'a choices = (t * 'a) list
57+
58+
type 'a choices = (t * 'a) list
5959

6060

6161
let eval var_get t =
@@ -430,10 +430,10 @@ module MyOCamlbuildBase = struct
430430
module OC = Ocamlbuild_pack.Ocaml_compiler
431431

432432

433-
type dir = string
434-
type file = string
435-
type name = string
436-
type tag = string
433+
type dir = string
434+
type file = string
435+
type name = string
436+
type tag = string
437437

438438

439439
(* # 62 "src/plugins/ocamlbuild/MyOCamlbuildBase.ml" *)
@@ -448,7 +448,7 @@ module MyOCamlbuildBase = struct
448448
* directory.
449449
*)
450450
includes: (dir * dir list) list;
451-
}
451+
}
452452

453453

454454
let env_filename =

0 commit comments

Comments
 (0)