File tree Expand file tree Collapse file tree 3 files changed +32
-20
lines changed Expand file tree Collapse file tree 3 files changed +32
-20
lines changed Original file line number Diff line number Diff line change 1
- SRC := c_src/example_nif.cpp
2
- TARGET := $(MIX_BUILD_DIR ) /example_nif.so
1
+ PRIV_DIR := $(MIX_APP_PATH ) /priv
2
+ NIF_PATH := $(PRIV_DIR ) /libexample.so
3
+ C_SRC := $(shell pwd) /c_src
3
4
4
- CPPFLAGS := -std=c++17 -fvisibility=hidden -fPIC -I$(ERL_INCLUDE_DIR ) -I$(FINE_INCLUDE_DIR )
5
+ CPPFLAGS := -shared -fPIC -std=c++17 -Wall -Wextra
6
+ CPPFLAGS += -I$(ERTS_INCLUDE_DIR ) -I$(FINE_INCLUDE_DIR )
5
7
6
- ifeq ( $( shell uname -s) ,Darwin)
7
- LDFLAGS : = -dynamiclib -undefined dynamic_lookup
8
+ ifdef DEBUG
9
+ CPPFLAGS + = -g
8
10
else
9
- LDFLAGS : = -shared
11
+ CPPFLAGS + = -O3
10
12
endif
11
13
12
- all : $(TARGET )
14
+ ifndef TARGET_ABI
15
+ TARGET_ABI := $(shell uname -s | tr '[:upper:]' '[:lower:]')
16
+ endif
17
+
18
+ ifeq ($(TARGET_ABI ) ,darwin)
19
+ CPPFLAGS += -undefined dynamic_lookup -flat_namespace
20
+ endif
21
+
22
+ SOURCES := $(wildcard $(C_SRC ) /* .cpp)
23
+
24
+ all : $(NIF_PATH )
25
+ @ echo > /dev/null # Dummy command to avoid the default output "Nothing to be done"
13
26
14
- $(TARGET ) : $(SRC )
15
- $(CXX ) $(CPPFLAGS ) $(LDFLAGS ) -o $@ $^
27
+ $(NIF_PATH ) : $(SOURCES )
28
+ @ mkdir -p $(PRIV_DIR )
29
+ $(CXX ) $(CPPFLAGS ) $(SOURCES ) -o $(NIF_PATH )
Original file line number Diff line number Diff line change 1
1
defmodule Example do
2
- @ on_load :load_nif
3
- @ mix_build_dir Mix.Project . build_path ( )
2
+ @ on_load :__on_load__
4
3
5
- defp load_nif do
6
- :erlang . load_nif ( ~c" #{ @ mix_build_dir } /example_nif" , 0 )
4
+ def __on_load__ do
5
+ path = :filename . join ( :code . priv_dir ( :example ) , ~c" libexample" )
6
+
7
+ case :erlang . load_nif ( path , 0 ) do
8
+ :ok -> :ok
9
+ { :error , reason } -> raise "failed to load NIF library, reason: #{ inspect ( reason ) } "
10
+ end
7
11
end
8
12
9
13
@ doc """
Original file line number Diff line number Diff line change @@ -7,13 +7,7 @@ defmodule Example.MixProject do
7
7
version: "0.1.0" ,
8
8
elixir: "~> 1.15" ,
9
9
compilers: [ :elixir_make ] ++ Mix . compilers ( ) ,
10
- make_env: fn ->
11
- % {
12
- "MIX_BUILD_DIR" => Mix.Project . build_path ( ) ,
13
- "ERL_INCLUDE_DIR" => "#{ :code . root_dir ( ) } /usr/include" ,
14
- "FINE_INCLUDE_DIR" => Fine . include_dir ( )
15
- }
16
- end ,
10
+ make_env: fn -> % { "FINE_INCLUDE_DIR" => Fine . include_dir ( ) } end ,
17
11
deps: deps ( )
18
12
]
19
13
end
You can’t perform that action at this time.
0 commit comments