Skip to content

Commit a02ac73

Browse files
avikchaudhurisgolemon
authored andcommitted
build limited non-Linux version of Hack
Stubbed out some Linux-specific dependencies in Hack (inotify,hh_shared). Note: this is not intended to be a general solution for running Hack on OSX. Reviewed By: @jwatzman Differential Revision: D1247209
1 parent 17c6f41 commit a02ac73

File tree

3 files changed

+47
-30
lines changed

3 files changed

+47
-30
lines changed

hphp/hack/src/Makefile

+19-4
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
TOP=$(shell pwd)
1818
TARGET=hack
1919

20+
OS=$(shell uname -s)
21+
2022
#------------------------------------------------------------------------------
2123
#package dependencies
2224
#------------------------------------------------------------------------------
@@ -39,14 +41,23 @@ endif
3941
#------------------------------------------------------------------------------
4042
# Program related variables
4143
#------------------------------------------------------------------------------
42-
PROGS=hh_single hh_server hh_client
44+
ifeq ($(OS), Linux)
45+
PROGS=hh_single hh_server hh_client
46+
else
47+
PROGS=hh_single
48+
endif
4349

4450
#------------------------------------------------------------------------------
4551
# Main variables
4652
#------------------------------------------------------------------------------
4753
SYSLIBS=nums.cma bigarray.cma str.cma unix.cma
4854

49-
LIBS=inotify/inotify.cma \
55+
ifeq ($(OS), Linux)
56+
INOTIFY=inotify
57+
INOTIFYLIB=inotify/inotify.cma
58+
endif
59+
60+
LIBS=$(INOTIFYLIB) \
5061
utils/lib.cma \
5162
avl/lib.cma \
5263
dfind/lib.cma \
@@ -58,7 +69,7 @@ LIBS=inotify/inotify.cma \
5869
$(BUILD_CMAS) \
5970
client/lib.cma
6071

61-
MAKESUBDIRS=inotify avl dfind \
72+
MAKESUBDIRS=$(INOTIFY) avl dfind \
6273
utils globals \
6374
heap procs \
6475
deps parsing naming typing \
@@ -75,7 +86,6 @@ INCLUDEDIRS=$(filter-out $(FILTER_FROM_INCLUDE),$(MAKESUBDIRS)) \
7586
##############################################################################
7687
-include $(TOP)/Makefile.common
7788

78-
7989
##############################################################################
8090
# Top rules
8191
##############################################################################
@@ -152,10 +162,15 @@ distclean:: clean
152162
##############################################################################
153163
# Install
154164
##############################################################################
165+
ifeq ($(OS), Linux)
155166
bin:
156167
cp -f hh_server.opt ../bin/hh_server
157168
cp hh_client.opt ../bin/hh_client
158169
cp hh_single.opt ../bin/hh_single_type_check
170+
else
171+
bin:
172+
cp hh_single.opt ../bin/hh_single_type_check
173+
endif
159174

160175
stamp:
161176
rm -f utils/build_id.ml

hphp/hack/src/Makefile.common

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# -*- makefile -*-
22

33
##############################################################################
4-
# Prelude
4+
# Prelude
55
##############################################################################
66

77
# This file assumes the "includer" will have set a few variables and then do a
@@ -25,8 +25,8 @@ OPTOBJS?= $(SRC:.ml=.cmx)
2525

2626
#-w +9 is for better warning on records, needs ocaml 3.12
2727
#-w +9
28-
#DO NOT USE -thread, the runtime is already chaotic enough to debug, we
29-
# don't want to add an extra layer of complexity. In fact, we are not
28+
#DO NOT USE -thread, the runtime is already chaotic enough to debug, we
29+
# don't want to add an extra layer of complexity. In fact, we are not
3030
# even sure it would work with -thread ...
3131
OCAMLCFLAGS?=-g -dtypes $(OCAMLCFLAGS_EXTRA) -w -21 -warn-error +a
3232

@@ -71,7 +71,7 @@ all::
7171
.ml.cmx:
7272
$(OCAMLOPT) -c $<
7373

74-
.ml.mldepend:
74+
.ml.mldepend:
7575
$(OCAMLC) -i $<
7676

7777
.c.o:

hphp/hack/src/heap/hh_shared.c

+24-22
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@
1616
* BUT ... YOU WERE GOING TO SAY BUT? BUT ...
1717
* THERE IS NO BUT! DONNY YOU'RE OUT OF YOUR ELEMENT!
1818
*
19-
* The lock-free data structures implemented here only work because of how
20-
* the Hack phases are synchronized.
19+
* The lock-free data structures implemented here only work because of how
20+
* the Hack phases are synchronized.
2121
*
2222
* There are 3 kinds of storage implemented in this file.
2323
* I) The global storage. Used by the master to efficiently transfer a blob
24-
* of data to the workers. This is used to share an environment in
24+
* of data to the workers. This is used to share an environment in
2525
* read-only mode with all the workers.
2626
* The master stores, the workers read.
2727
*
@@ -31,9 +31,9 @@
3131
*
3232
* II) The Hashtable.
3333
* The operations implemented, and their limitations:
34-
*
34+
*
3535
* -) Concurrent writes: SUPPORTED
36-
* As long as its not interleaved with any other operation
36+
* As long as its not interleaved with any other operation
3737
* (other than mem)!
3838
*
3939
* -) Concurrent reads: SUPPORTED
@@ -112,12 +112,12 @@ typedef struct {
112112
/* The location of the shared memory */
113113
static char* shared_mem;
114114

115-
/* ENCODING: The first element is the size stored in bytes, the rest is
115+
/* ENCODING: The first element is the size stored in bytes, the rest is
116116
* the data. The size is set to zero when the storage is empty.
117117
*/
118118
static value* global_storage;
119119

120-
/* ENCODING:
120+
/* ENCODING:
121121
* The highest 2 bits are unused.
122122
* The next 31 bits encode the key the lower 31 bits the value.
123123
*/
@@ -223,12 +223,14 @@ static void set_priorities() {
223223
// at all!)
224224
//
225225
// No need to check the return value, if we failed then whatever.
226+
#ifdef __linux__
226227
syscall(
227228
SYS_ioprio_set,
228229
IOPRIO_WHO_PROCESS,
229230
my_pid,
230231
IOPRIO_PRIO_VALUE(IOPRIO_CLASS_IDLE, 7)
231232
);
233+
#endif
232234

233235
// Don't slam the CPU either, though this has much less tendency to make the
234236
// system totally unresponsive so we don't need to lower all the way.
@@ -242,12 +244,12 @@ void hh_shared_init() {
242244
/* MAP_NORESERVE is because we want a lot more virtual memory than what
243245
* we are actually going to use.
244246
*/
245-
int flags = MAP_SHARED | MAP_ANONYMOUS | MAP_NORESERVE;
247+
int flags = MAP_SHARED | MAP_ANON | MAP_NORESERVE;
246248
int prot = PROT_READ | PROT_WRITE;
247249

248250
page_size = getpagesize();
249251

250-
shared_mem =
252+
shared_mem =
251253
(char*)mmap(NULL, page_size + SHARED_MEM_SIZE, prot, flags, 0, 0);
252254

253255
if(shared_mem == MAP_FAILED) {
@@ -353,7 +355,7 @@ void hh_shared_clear() {
353355
* modifying.
354356
* The table contains key/value bindings encoded in a word.
355357
* The higher bits represent the key, the lower ones the value.
356-
* Each key/value binding is unique, but a key can have multiple value
358+
* Each key/value binding is unique, but a key can have multiple value
357359
* bound to it.
358360
* Concretely, if you try to add a key/value pair that is already in the table
359361
* the data structure is left unmodified.
@@ -366,7 +368,7 @@ void hh_add_dep(value ocaml_dep) {
366368
unsigned long dep = Long_val(ocaml_dep);
367369
unsigned long hash = dep >> 31;
368370
unsigned long slot = hash & (DEP_SIZE - 1);
369-
371+
370372
while(1) {
371373
/* It considerably speeds things up to do a normal load before trying using
372374
* an atomic operation.
@@ -376,7 +378,7 @@ void hh_add_dep(value ocaml_dep) {
376378
// The binding exists, done!
377379
if(slot_val == dep)
378380
return;
379-
381+
380382
// The slot is free, let's try to take it.
381383
if(slot_val == 0) {
382384
// See comments in hh_add about its similar construction here.
@@ -400,7 +402,7 @@ value hh_get_dep(value dep) {
400402

401403
unsigned long hash = Long_val(dep);
402404
unsigned long slot = hash & (DEP_SIZE - 1);
403-
405+
404406
result = Val_int(0); // The empty list
405407

406408
while(1) {
@@ -443,7 +445,7 @@ void hh_call_after_init() {
443445
*/
444446
/*****************************************************************************/
445447
void hh_collect() {
446-
int flags = MAP_PRIVATE | MAP_ANONYMOUS | MAP_NORESERVE;
448+
int flags = MAP_PRIVATE | MAP_ANON | MAP_NORESERVE;
447449
int prot = PROT_READ | PROT_WRITE;
448450
char* dest;
449451
size_t mem_size = 0;
@@ -488,7 +490,7 @@ void hh_collect() {
488490

489491
if(munmap(tmp_heap, HEAP_SIZE) == -1) {
490492
printf("Error while collecting: %s\n", strerror(errno));
491-
exit(2);
493+
exit(2);
492494
}
493495
}
494496

@@ -509,8 +511,8 @@ static char* hh_alloc(size_t size) {
509511
}
510512

511513
/*****************************************************************************/
512-
/* Allocates an ocaml value in the shared heap.
513-
* The values can only be ocaml strings. It returns the address of the
514+
/* Allocates an ocaml value in the shared heap.
515+
* The values can only be ocaml strings. It returns the address of the
514516
* allocated chunk.
515517
*/
516518
/*****************************************************************************/
@@ -676,7 +678,7 @@ void hh_move(value key1, value key2) {
676678
/*****************************************************************************/
677679
void hh_remove(value key) {
678680
unsigned int slot = find_slot(key);
679-
681+
680682
assert(my_pid == master_pid);
681683
assert(hashtbl[slot].hash == get_hash(key));
682684
hashtbl[slot].addr = NULL;
@@ -685,7 +687,7 @@ void hh_remove(value key) {
685687
/*****************************************************************************/
686688
/* Returns a copy of the content of a file in an ocaml string.
687689
* This code should be very tolerant to failure. At any given time, the
688-
* file could be modified, when that happens, we don't want to fail, we
690+
* file could be modified, when that happens, we don't want to fail, we
689691
* return the empty string instead.
690692
*/
691693
/*****************************************************************************/
@@ -697,7 +699,7 @@ value hh_read_file(value filename) {
697699
int fd;
698700
struct stat sb;
699701
char* memblock;
700-
702+
701703
fd = open(String_val(filename), O_RDONLY);
702704
if(fd == -1) {
703705
result = caml_alloc_string(0);
@@ -706,7 +708,7 @@ value hh_read_file(value filename) {
706708
result = caml_alloc_string(0);
707709
close(fd);
708710
}
709-
else if((memblock =
711+
else if((memblock =
710712
(char*)mmap(NULL, sb.st_size, PROT_READ, MAP_PRIVATE, fd, 0))
711713
== MAP_FAILED) {
712714
result = caml_alloc_string(0);
@@ -718,6 +720,6 @@ value hh_read_file(value filename) {
718720
munmap(memblock, sb.st_size);
719721
close(fd);
720722
}
721-
723+
722724
CAMLreturn(result);
723725
}

0 commit comments

Comments
 (0)