-
Notifications
You must be signed in to change notification settings - Fork 5
Add example project #3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
cf9b01d
Add example project
wojtekmach 3692ea2
Apply suggestions from code review
wojtekmach edda90e
up
wojtekmach 0b7ef4b
Update .github/workflows/ci.yml
wojtekmach 2e43a5a
up
wojtekmach 76190e0
up
wojtekmach 135a693
up
wojtekmach 0d8a7e5
up
wojtekmach File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
# Used by "mix format" | ||
[ | ||
inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"] | ||
] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# The directory Mix will write compiled artifacts to. | ||
/_build/ | ||
|
||
# If you run "mix test --cover", coverage assets end up here. | ||
/cover/ | ||
|
||
# The directory Mix downloads your dependencies sources to. | ||
/deps/ | ||
|
||
# Where third-party dependencies like ExDoc output generated docs. | ||
/doc/ | ||
|
||
# If the VM crashes, it generates a dump, let's ignore it too. | ||
erl_crash.dump | ||
|
||
# Also ignore archive artifacts (built via "mix archive.build"). | ||
*.ez | ||
|
||
# Ignore package tarball (built via "mix hex.build"). | ||
example-*.tar | ||
|
||
# Temporary files, for example, from tests. | ||
/tmp/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
PRIV_DIR := $(MIX_APP_PATH)/priv | ||
NIF_PATH := $(PRIV_DIR)/libexample.so | ||
C_SRC := $(shell pwd)/c_src | ||
|
||
CPPFLAGS := -shared -fPIC -std=c++17 -Wall -Wextra | ||
CPPFLAGS += -I$(ERTS_INCLUDE_DIR) -I$(FINE_INCLUDE_DIR) | ||
|
||
ifdef DEBUG | ||
CPPFLAGS += -g | ||
else | ||
CPPFLAGS += -O3 | ||
endif | ||
|
||
ifndef TARGET_ABI | ||
TARGET_ABI := $(shell uname -s | tr '[:upper:]' '[:lower:]') | ||
endif | ||
|
||
ifeq ($(TARGET_ABI),darwin) | ||
CPPFLAGS += -undefined dynamic_lookup -flat_namespace | ||
endif | ||
|
||
SOURCES := $(wildcard $(C_SRC)/*.cpp) | ||
|
||
all: $(NIF_PATH) | ||
@ echo > /dev/null # Dummy command to avoid the default output "Nothing to be done" | ||
|
||
$(NIF_PATH): $(SOURCES) | ||
@ mkdir -p $(PRIV_DIR) | ||
$(CXX) $(CPPFLAGS) $(SOURCES) -o $(NIF_PATH) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
PRIV_DIR=$(MIX_APP_PATH)\priv | ||
NIF_PATH=$(PRIV_DIR)\libexample.dll | ||
|
||
C_SRC=$(MAKEDIR)\c_src | ||
CPPFLAGS=/LD /std:c++17 /W4 /O2 /EHsc | ||
CPPFLAGS=$(CPPFLAGS) /I"$(ERTS_INCLUDE_DIR)" /I"$(FINE_INCLUDE_DIR)" | ||
|
||
SOURCES=$(C_SRC)\*.cpp | ||
|
||
all: $(NIF_PATH) | ||
|
||
$(NIF_PATH): $(SOURCES) | ||
@ if not exist "$(PRIV_DIR)" mkdir "$(PRIV_DIR)" | ||
cl $(CPPFLAGS) $(SOURCES) /Fe"$(NIF_PATH)" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
# Example | ||
|
||
Example project using Fine to implement NIFs. | ||
|
||
To run tests, execute: | ||
|
||
```shell | ||
$ mix deps.get | ||
$ mix test | ||
``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
#include <fine.hpp> | ||
|
||
int64_t add(ErlNifEnv *env, int64_t x, int64_t y) { | ||
return x + y; | ||
} | ||
|
||
FINE_NIF(add, 0); | ||
FINE_INIT("Elixir.Example"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
defmodule Example do | ||
@on_load :__on_load__ | ||
|
||
def __on_load__ do | ||
path = :filename.join(:code.priv_dir(:example), ~c"libexample") | ||
|
||
case :erlang.load_nif(path, 0) do | ||
:ok -> :ok | ||
{:error, reason} -> raise "failed to load NIF library, reason: #{inspect(reason)}" | ||
end | ||
end | ||
|
||
@doc """ | ||
Adds two numbers using NIF. | ||
|
||
## Examples | ||
|
||
iex> Example.add(1, 2) | ||
3 | ||
""" | ||
def add(_x, _y) do | ||
:erlang.nif_error("nif not loaded") | ||
end | ||
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
defmodule Example.MixProject do | ||
use Mix.Project | ||
|
||
def project do | ||
[ | ||
app: :example, | ||
version: "0.1.0", | ||
elixir: "~> 1.15", | ||
compilers: [:elixir_make] ++ Mix.compilers(), | ||
make_env: fn -> %{"FINE_INCLUDE_DIR" => Fine.include_dir()} end, | ||
deps: deps() | ||
] | ||
end | ||
|
||
def application do | ||
[ | ||
extra_applications: [:logger] | ||
] | ||
end | ||
|
||
defp deps do | ||
[ | ||
{:elixir_make, "~> 0.9.0"}, | ||
{:fine, path: ".."} | ||
] | ||
end | ||
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
%{ | ||
"elixir_make": {:hex, :elixir_make, "0.9.0", "6484b3cd8c0cee58f09f05ecaf1a140a8c97670671a6a0e7ab4dc326c3109726", [:mix], [], "hexpm", "db23d4fd8b757462ad02f8aa73431a426fe6671c80b200d9710caf3d1dd0ffdb"}, | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
defmodule ExampleTest do | ||
use ExUnit.Case, async: true | ||
doctest Example | ||
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
ExUnit.start() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this can also be the following, wdyt?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good, but according to
erlang:load_nif/2
typespec the path should be a charlist, so probably safer to stick to that.