Skip to content

Commit

Permalink
Make builds reliable across OSes
Browse files Browse the repository at this point in the history
  • Loading branch information
codeadict committed Feb 21, 2023
1 parent eec032f commit ea45107
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 41 deletions.
1 change: 0 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ cover: test
$(REBAR) cover

test: compile
$(MAKE) -C c_src test
$(REBAR) as test do eunit

dialyzer:
Expand Down
28 changes: 19 additions & 9 deletions c_src/Makefile
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
LEVELDB_VSN ?= "2.0.38"
BASEDIR = $(shell pwd)

LDFLAGS := $(LDFLAGS) -L$(BASEDIR)/system/lib
CFLAGS += -I $(BASEDIR)/leveldb/include -fPIC
CXXFLAGS += -I $(BASEDIR)/leveldb/include -fPIC
C_SRC_DIR := $(shell pwd)
PROJECT_DIR := $(abspath $(C_SRC_DIR)/..)
TARGET_DIR := $(PROJECT_DIR)/priv/

# LevelDB tools
TOOLS = \
leveldb_repair \
perf_dump \
sst_rewrite \
sst_scan

LDFLAGS := $(LDFLAGS) -lpthread
CFLAGS := $(CFLAGS) -I. -I $(C_SRC_DIRC_SRC_DIR)/leveldb/include -fPIC
CXXFLAGS := $(CXXFLAGS) -I. -I $(C_SRC_DIR)/leveldb/include -fPIC
TEST_CXXFLAGS := $(CXXFLAGS) -Wno-narrowing

ifeq ($(shell uname -s), Darwin)
Expand Down Expand Up @@ -33,15 +42,16 @@ get-deps:
fi

compile: get-deps ldb
cp leveldb/perf_dump leveldb/sst_rewrite leveldb/sst_scan leveldb/leveldb_repair ../priv
for tool in $(TOOLS); do cp leveldb/$$tool $(TARGET_DIR); done

ldb:
@echo "Building LevelDB..."
$(MAKE) LDFLAGS="$(LDFLAGS) -lsnappy -lpthread" LD_LIBRARY_PATH="$(LD_LIBRARY_PATH)" -C leveldb all
$(MAKE) LDFLAGS="$(LDFLAGS) -lsnappy -lpthread" LD_LIBRARY_PATH="$(LD_LIBRARY_PATH)" -C leveldb tools
$(MAKE) LDFLAGS="" -C leveldb all
$(MAKE) LDFLAGS="$(LDFLAGS)" -C leveldb tools

clean:
$(MAKE) -C leveldb clean
for tool in $(TOOLS); do rm -f $(TARGET_DIR)$$tool; done

test: compile
$(MAKE) PLATFORM_CXXFLAGS="$(TEST_CXXFLAGS)" LDFLAGS="$(LDFLAGS) -lsnappy -lpthread" LD_LIBRARY_PATH="$(LD_LIBRARY_PATH)" -C leveldb test
$(MAKE) LDFLAGS="$(LDFLAGS)" CXXFLAGS="$(CXXFLAGS)" CFLAGS="$(CFLAGS)" -C leveldb test
6 changes: 0 additions & 6 deletions make

This file was deleted.

54 changes: 31 additions & 23 deletions rebar.config
Original file line number Diff line number Diff line change
Expand Up @@ -5,39 +5,47 @@

{plugins, [{eqc_rebar, {git, "https://github.com/Quviq/eqc-rebar", {branch, "master"}}}, pc]}.

{provider_hooks,
[
{post,
[
{compile, {pc, compile}},
{clean, {pc, clean}}
]
}
]
}.

{xref_checks,[undefined_function_calls,undefined_functions,locals_not_used,
deprecated_function_calls, deprecated_functions]}.
{provider_hooks, [
{post, [
{compile, {pc, compile}},
{clean, {pc, clean}}
]}
]}.

{xref_checks, [
undefined_function_calls,
undefined_functions,
locals_not_used,
deprecated_function_calls,
deprecated_functions
]}.

{erl_opts, [warnings_as_errors, debug_info]}.

{profiles, [
{gha, [{erl_opts, [{d, 'GITHUBEXCLUDE'}]}]}
]}.

{port_specs, [{ "priv/eleveldb.so", ["c_src/*.cc"] }]}.
{port_specs, [{"priv/eleveldb.so", ["c_src/*.cc"]}]}.

{artifacts, ["priv/eleveldb.so"]}.

{port_env, [
%% Make sure to set -fPIC when compiling leveldb
{"CFLAGS", "$CFLAGS -Wall -O3 -fPIC"},
{"CXXFLAGS", "$CXXFLAGS -Wall -O3 -fPIC"},
{"DRV_CFLAGS", "$DRV_CFLAGS -O3 -Wall -I c_src/leveldb/include -I c_src/leveldb"},
{"DRV_LDFLAGS", "$DRV_LDFLAGS c_src/leveldb/libleveldb.a -lstdc++"}
]}.
%% Make sure to set -fPIC when compiling leveldb
{"CXXFLAGS", "$CXXFLAGS -Wall -O3 -fPIC -I c_src/leveldb/include -I c_src/leveldb"},
{"LDFLAGS", "$LDFLAGS c_src/leveldb/libleveldb.a -lsnappy -lstdc++"}
]}.

{pre_hooks, [{'get-deps', "./make -C c_src get-deps"},
{compile, "./make -C c_src compile"}]}.
{pre_hooks, [
{"(linux|darwin|solaris)", 'get-deps', "./make -C c_src get-deps"},
{"(freebsd|netbsd|openbsd)", 'get-deps', "gmake -C c_src get-deps"},
{"(linux|darwin|solaris)", compile, "make -C c_src compile"},
{"(freebsd|netbsd|openbsd)", compile, "gmake -C c_src compile"},
{"(linux|darwin|solaris)", eunit, "make -C c_src test"},
{"(freebsd|netbsd|openbsd)", eunit, "gmake -C c_src test"}
]}.

{post_hooks, [{clean, "./make -C c_src clean"}]}.
{post_hooks, [
{"(linux|darwin|solaris)", clean, "make -C c_src clean"},
{"(freebsd|netbsd|openbsd)", clean, "gmake -C c_src clean"}
]}.
11 changes: 9 additions & 2 deletions rebar.config.script
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,19 @@
%% actually running.
case os:type() of
{unix,darwin} ->
Opt = " -mmacosx-version-min=10.8 -stdlib=libc++",
DefaultOpt = " -mmacosx-version-min=10.8 -stdlib=libc++",
Opt = case os:find_executable("brew") of
false ->
DefaultOpt;
_Brew ->
HomebrewPrefix = string:trim(os:cmd("brew --prefix")),
DefaultOpt ++ " -L" ++ HomebrewPrefix ++ "/lib"
end,
[Mjr|_] = string:tokens(os:cmd("/usr/bin/uname -r"), "."),
Major = list_to_integer(Mjr),
if
Major >= 13 ->
Flags = ["CFLAGS", "CXXFLAGS", "DRV_CFLAGS", "DRV_LDFLAGS"],
Flags = ["CFLAGS", "CXXFLAGS", "LDFLAGS"],
{port_env, Vals} = lists:keyfind(port_env, 1, CONFIG),
Fold = fun({Flag,Val}, Acc) ->
case lists:member(Flag, Flags) of
Expand Down

0 comments on commit ea45107

Please sign in to comment.