Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions ada/.gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
obj/
result
lib/
23 changes: 23 additions & 0 deletions ada/alire.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name = "erlang_nifs"
description = "Shiny new project"
version = "0.1.0-dev"

authors = ["Andy Arvanitis"]
maintainers = ["Andy Arvanitis <andyarvanitis@gmail.com>"]
maintainers-logins = ["andyarvanitis"]

[build-switches]
"*".style_checks = "No"
"*".ada_version = "Ada2022"

[environment]
RUNTIME_DIR.append = "dirname $(gcc --print-libgcc)"
RUNTIME_LIB.append = "/adalib/libgnat_pic.a"
ERLANG_LIB_BUNDLE.append = "erl -noshell -eval 'io:fwrite(code:root_dir())' -eval 'init:stop()'"
OPTIONS_FILE.append = "/ada_nifs_linker_options.in"

[[actions]]
type = "pre-build"
command = [ "sh", "-c", """ DIR=$(eval ${RUNTIME_DIR}); \
echo -L ${DIR} ${DIR}/${RUNTIME_LIB} \
> ${TMPDIR}${OPTIONS_FILE} """ ]
6 changes: 6 additions & 0 deletions ada/alire/alire.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# THIS FILE IS GENERATED. DO NOT EDIT.

[solution]
[solution.context]
solved = true

8 changes: 8 additions & 0 deletions ada/alire/build_hash_inputs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
environment:ERLANG_LIB_BUNDLE=erl -noshell -eval 'io:fwrite(code:root_dir())' -eval 'init:stop()'
environment:OPTIONS_FILE=/ada_nifs_linker_options.in
environment:RUNTIME_DIR=dirname $(gcc --print-libgcc)
environment:RUNTIME_LIB=/adalib/libgnat_pic.a
external:ERLANG_NIFS_LIBRARY_TYPE=default
external:LIBRARY_TYPE=default
profile:erlang_nifs=DEVELOPMENT
switches:erlang_nifs=-Og,-fdata-sections,-ffunction-sections,-g,-gnat2022,-gnatVa,-gnatW8,-gnatw.X,-gnatwa
Empty file added ada/alire/flags/post_fetch_done
Empty file.
2 changes: 2 additions & 0 deletions ada/alire/settings.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
last_build_profile = "erlang_nifs=DEVELOPMENT"

28 changes: 22 additions & 6 deletions ada/build.nix
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{ stdenv, gnat, gprbuild, glibc }:
{ stdenv, gnat, gprbuild, glibc, alire, erlang, git, gcc }:

stdenv.mkDerivation {
name = "ada-static-hello";
Expand All @@ -8,33 +8,49 @@ stdenv.mkDerivation {
nativeBuildInputs = [
gprbuild
gnat
alire
git
erlang
];

buildInputs = [
glibc.dev
glibc.static
];

NIX_LDFLAGS = "-L${glibc}/lib -L${glibc.static}/lib -L${gcc.cc.lib}/lib";

dontConfigure = true;

buildPhase = ''
runHook preBuild

gprbuild
export ERLANG_INCLUDE_DIR="-I${erlang}/lib/erlang/usr/include"
export GLIBC_INCLUDE_DIR="-I${glibc.dev}/include"
export GCC_INCLUDE_DIR="-I${gcc.cc}/include"

# alr build
gprbuild -P erlang_nifs.gpr -XLIBRARY_TYPE=relocatable

runHook postBuild
'';

installPhase = ''
runHook preInstall

mkdir -p $out/bin

# Only install what we need to run the binary.
gprinstall --prefix=$out hello.gpr \
gprinstall --prefix=$out erlang_nifs.gpr \
--no-project \
--no-manifest \
--mode=usage

cp -r ./lib $out

# for dir in lib; do
# if [ -f "$dir/Erlang_Nifs.so.0.1.0-dev" ]; then
# cp "$dir/Erlang_Nifs.so.0.1.0-dev" "$out/Erlang_Nifs.so"
# fi
# done

runHook postInstall
'';
}
20 changes: 20 additions & 0 deletions ada/config/erlang_nifs_config.ads
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
-- Configuration for erlang_nifs generated by Alire
pragma Restrictions (No_Elaboration_Code);
pragma Style_Checks (Off);

package Erlang_Nifs_Config is
pragma Pure;

Crate_Version : constant String := "0.1.0-dev";
Crate_Name : constant String := "erlang_nifs";

Alire_Host_OS : constant String := "linux";

Alire_Host_Arch : constant String := "x86_64";

Alire_Host_Distro : constant String := "distribution_unknown";

type Build_Profile_Kind is (release, validation, development);
Build_Profile : constant Build_Profile_Kind := development;

end Erlang_Nifs_Config;
28 changes: 28 additions & 0 deletions ada/config/erlang_nifs_config.gpr
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
-- Configuration for erlang_nifs generated by Alire
abstract project Erlang_Nifs_Config is
Crate_Version := "0.1.0-dev";
Crate_Name := "erlang_nifs";

Alire_Host_OS := "linux";

Alire_Host_Arch := "x86_64";

Alire_Host_Distro := "distribution_unknown";
Ada_Compiler_Switches := External_As_List ("ADAFLAGS", " ");
Ada_Compiler_Switches := Ada_Compiler_Switches &
(
"-Og" -- Optimize for debug
,"-ffunction-sections" -- Separate ELF section for each function
,"-fdata-sections" -- Separate ELF section for each variable
,"-g" -- Generate debug info
,"-gnatwa" -- Enable all warnings
,"-gnatw.X" -- Disable warnings for No_Exception_Propagation
,"-gnatVa" -- All validity checks
,"-gnat2022" -- Ada 2022 Mode
,"-gnatW8" -- UTF-8 encoding for wide characters
);

type Build_Profile_Kind is ("release", "validation", "development");
Build_Profile : Build_Profile_Kind := "development";

end Erlang_Nifs_Config;
20 changes: 20 additions & 0 deletions ada/config/erlang_nifs_config.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/* Configuration for erlang_nifs generated by Alire */
#ifndef ERLANG_NIFS_CONFIG_H
#define ERLANG_NIFS_CONFIG_H

#define CRATE_VERSION "0.1.0-dev"
#define CRATE_NAME "erlang_nifs"

#define ALIRE_HOST_OS "linux"

#define ALIRE_HOST_ARCH "x86_64"

#define ALIRE_HOST_DISTRO "distribution_unknown"

#define BUILD_PROFILE_RELEASE 1
#define BUILD_PROFILE_VALIDATION 2
#define BUILD_PROFILE_DEVELOPMENT 3

#define BUILD_PROFILE 3

#endif
58 changes: 58 additions & 0 deletions ada/erlang_nifs.gpr
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
with "config/erlang_nifs_config.gpr";
project Erlang_Nifs is

-- Erlang_Include_Dir : External ("ERLANG_INCLUDE_DIR", "");

for Languages use ("ada", "C++");

for Library_Name use "Erlang_Nifs";
for Library_Version use Project'Library_Name & ".so." & Erlang_Nifs_Config.Crate_Version;

for Library_Interface use ("examples");
for Library_Auto_Init use "true";
for Library_Dir use "lib";

for Source_Dirs use ("src/", "config/");
for Object_Dir use "obj/" & Erlang_Nifs_Config.Build_Profile;
for Create_Missing_Dirs use "True";

type Library_Type_Type is ("relocatable", "static", "static-pic");
Library_Type : Library_Type_Type :=
external ("ERLANG_NIFS_LIBRARY_TYPE", external ("LIBRARY_TYPE", "static-pic"));
for Library_Kind use Library_Type;

package Compiler is
for Default_Switches ("Ada") use Erlang_Nifs_Config.Ada_Compiler_Switches & ("-gnata", "-gnatW8");
for Default_Switches ("C++") use (External ("ERLANG_INCLUDE_DIR", ""), External ("GLIBC_INCLUDE_DIR", ""), External ("GCC_INCLUDE_DIR", ""));
end Compiler;

-- for Library_Builder use "gcc -shared";
for Archive_Builder use ("true"); -- no-op
for Archive_Indexer use ("true"); -- no-op

Options_File := external ("OPTIONS_FILE", "unknown");
Temp_Dir := external ("TMPDIR", "unknown");

case Library_Type is
when "static-pic" =>
for Library_Options use ("-lc",
"-lstdc++",
-- "@" & Temp_Dir & Options_File, -- NOTE: this is from alr environment (alire.toml)
"-o" & Project'Library_Name & ".so");
when others =>
null;
end case;

package Binder is
for Switches ("Ada") use ("-Es"); -- Symbolic traceback
end Binder;

package Install is
for Artifacts (".") use ("share");
end Install;

package Clean is
for Artifacts_In_Object_Dir use Clean'Artifacts_In_Object_Dir & (Project'Library_Name & ".so");
end Clean;

end Erlang_Nifs;
8 changes: 8 additions & 0 deletions ada/flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,18 @@
gnat
gprbuild
gdb
alire
erlang
glibc.dev
glibc.static
git
];
shellHook = ''
echo "Available commands: gnat, gprbuild, gdb"

export ERLANG_INCLUDE_DIR="-I${pkgs.erlang}/lib/erlang/usr/include"
export GLIBC_INCLUDE_DIR="-I${pkgs.glibc.dev}/include"
export GCC_INCLUDE_DIR="-I${pkgs.gcc.cc}/include"
'';
};
});
Expand Down
12 changes: 0 additions & 12 deletions ada/hello.gpr

This file was deleted.

23 changes: 23 additions & 0 deletions ada/src/erlang_nifs-arity_1.adb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@

package body erlang_nifs.arity_1 is

package args is new get_values(argument_type); use args;
package rets is new make_values(return_type); use rets;

function nif_wrapper(env: not null access erl_nif_env_t;
argc: C.int with unreferenced;
argv: erl_nif_terms_t)
return erl_nif_term_t is
begin
return make_value(env, ada_function(get_value(env, argv(0))));

exception
when error: others =>
return raise_erlang_exception(env, error);

end nif_wrapper;

begin
nif_functions.append(nif_info);

end erlang_nifs.arity_1;
24 changes: 24 additions & 0 deletions ada/src/erlang_nifs-arity_1.ads
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@

generic
erlang_name: string;
with package argument_type is new nif_supported_types (<>);
with package return_type is new nif_supported_types (<>);
with function ada_function(x: in argument_type.t) return return_type.t;
package erlang_nifs.arity_1 is
pragma elaborate_body;
pragma Assertion_Policy(Check);

private
function nif_wrapper(env: not null access erl_nif_env_t;
argc: C.int;
argv: erl_nif_terms_t)
return erl_nif_term_t
with convention => C,
pre => argc = 1;

nif_info : enif_func_t := (name => C.strings.new_string(erlang_name),
arity => 1,
fptr => nif_wrapper'access,
flags => 0);

end erlang_nifs.arity_1;
23 changes: 23 additions & 0 deletions ada/src/erlang_nifs-arity_2.adb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package body erlang_nifs.arity_2 is

package args1 is new get_values(argument_type1); use args1;
package args2 is new get_values(argument_type2); use args2;
package rets is new make_values(return_type); use rets;

function nif_wrapper(env: not null access erl_nif_env_t;
argc: C.int with unreferenced;
argv: erl_nif_terms_t)
return erl_nif_term_t is
begin
return make_value(env, ada_function(get_value(env, argv(0)), get_value(env, argv(1))));

exception
when error: others =>
return raise_erlang_exception(env, error);

end nif_wrapper;

begin
nif_functions.append(nif_info);

end erlang_nifs.arity_2;
24 changes: 24 additions & 0 deletions ada/src/erlang_nifs-arity_2.ads
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
generic
erlang_name: string;
with package argument_type1 is new nif_supported_types (<>);
with package argument_type2 is new nif_supported_types (<>);
with package return_type is new nif_supported_types (<>);
with function ada_function(x: in argument_type1.t; y: in argument_type2.t) return return_type.t;
package erlang_nifs.arity_2 is
pragma elaborate_body;
pragma Assertion_Policy(Check);

private
function nif_wrapper(env: not null access erl_nif_env_t;
argc: C.int;
argv: erl_nif_terms_t)
return erl_nif_term_t
with convention => C,
pre => argc = 2;

nif_info : enif_func_t := (name => C.strings.new_string(erlang_name),
arity => 2,
fptr => nif_wrapper'access,
flags => 0);

end erlang_nifs.arity_2;
13 changes: 13 additions & 0 deletions ada/src/erlang_nifs-c_apis.adb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package body erlang_nifs.c_apis is

procedure populate_nif_array(arr: in out nif_funcs_array_t;
count: C.int with unreferenced) is
i : integer := 0;
begin
for nif of nif_functions loop
arr(i):= nif;
i := i + 1;
end loop;
end populate_nif_array;

end erlang_nifs.c_apis;
Loading