Skip to content

Commit

Permalink
Enable cross-compilation
Browse files Browse the repository at this point in the history
  • Loading branch information
JayKickliter committed Feb 18, 2021
1 parent 66a4903 commit cdc3a10
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 97 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ need `cmake` installed to build the required h3 core package.

To run the tests run `make test`.

## Cross compilation

Cross-compilation requires the environment variable `ERTS_INCLUDE_DIR`
defined as the target directory containing `erl_nif.h`,
e.g. `ERTS_INCLUDE_DIR=target/usr/lib/erlang/erts-<VERSION>/include`.

Usage
-----
Expand Down
130 changes: 33 additions & 97 deletions c_src/cmake/FindErlang.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -15,126 +15,62 @@ This module provides the following imported targets, if found:
``Erlang::Erlang``
Header only interface library suitible for compiling NIFs.
``Erlang::EI``
Erlang interface library.
``Erlang::ERTS``
Erlang runtime system library.
Result Variables
^^^^^^^^^^^^^^^^
This will define the following variables:
``Erlang_FOUND``
True if the system has the Erlang library.
``Erlang_RUNTIME``
The path to the Erlang runtime.
``Erlang_COMPILE``
The path to the Erlang compiler.
``Erlang_EI_PATH``
The path to the Erlang erl_interface path.
``Erlang_ERTS_PATH``
The path to the Erlang erts path.
``Erlang_EI_INCLUDE_DIRS``
/include appended to Erlang_EI_PATH.
``Erlang_EI_LIBRARY_PATH``
/lib appended to Erlang_EI_PATH.
``Erlang_ERTS_INCLUDE_DIRS``
/include appended to Erlang_ERTS_PATH.
``Erlang_ERTS_LIBRARY_PATH``
/lib appended to Erlang_ERTS_PATH.
#]=======================================================================]
include(FindPackageHandleStandardArgs)

SET(Erlang_BIN_PATH
$ENV{ERLANG_HOME}/bin
/opt/bin
/sw/bin
/usr/bin
/usr/local/bin
/opt/local/bin
)
FIND_PROGRAM(Erlang_RUNTIME
NAMES erl
PATHS ${Erlang_BIN_PATH}
)

FIND_PROGRAM(Erlang_COMPILE
NAMES erlc
PATHS ${Erlang_BIN_PATH}
)
Cache Variables
^^^^^^^^^^^^^^^
EXECUTE_PROCESS(
COMMAND erl -noshell -eval "io:format(\"~s\", [code:lib_dir()])" -s erlang halt
OUTPUT_VARIABLE Erlang_OTP_LIB_DIR
)
The following cache variables may also be set:
EXECUTE_PROCESS(
COMMAND erl -noshell -eval "io:format(\"~s\", [code:root_dir()])" -s erlang halt
OUTPUT_VARIABLE Erlang_OTP_ROOT_DIR
)

EXECUTE_PROCESS(
COMMAND erl -noshell -eval "io:format(\"~s\",[filename:basename(code:lib_dir('erl_interface'))])" -s erlang halt
OUTPUT_VARIABLE Erlang_EI_DIR
)
``Erlang_ERTS_INCLUDE_DIR``
Path to Erlang Runtime System headers.
EXECUTE_PROCESS(
COMMAND erl -noshell -eval "io:format(\"~s\",[filename:basename(code:lib_dir('erts'))])" -s erlang halt
OUTPUT_VARIABLE Erlang_ERTS_DIR
)

SET(Erlang_EI_PATH ${Erlang_OTP_LIB_DIR}/${Erlang_EI_DIR})
SET(Erlang_EI_INCLUDE_DIRS ${Erlang_OTP_LIB_DIR}/${Erlang_EI_DIR}/include)
SET(Erlang_EI_LIBRARY_PATH ${Erlang_OTP_LIB_DIR}/${Erlang_EI_DIR}/lib)
#]=======================================================================]
include(FindPackageHandleStandardArgs)

SET(Erlang_ERTS_PATH ${Erlang_OTP_ROOT_DIR}/${Erlang_ERTS_DIR})
SET(Erlang_ERTS_INCLUDE_DIRS ${Erlang_OTP_ROOT_DIR}/${Erlang_ERTS_DIR}/include)
SET(Erlang_ERTS_LIBRARY_PATH ${Erlang_OTP_ROOT_DIR}/${Erlang_ERTS_DIR}/lib)
if(DEFINED ENV{ERTS_INCLUDE_DIR})
SET(Erlang_ERTS_INCLUDE_DIR $ENV{ERTS_INCLUDE_DIR})
else()
SET(Erlang_BIN_PATH
$ENV{ERLANG_HOME}/bin
/opt/bin
/sw/bin
/usr/bin
/usr/local/bin
/opt/local/bin
)

EXECUTE_PROCESS(
COMMAND erl -noshell -eval "io:format(\"~s\", [code:root_dir()])" -s erlang halt
OUTPUT_VARIABLE Erlang_OTP_ROOT_DIR
)

EXECUTE_PROCESS(
COMMAND erl -noshell -eval "io:format(\"~s\",[filename:basename(code:lib_dir('erts'))])" -s erlang halt
OUTPUT_VARIABLE Erlang_ERTS_DIR
)

SET(Erlang_ERTS_INCLUDE_DIR ${Erlang_OTP_ROOT_DIR}/${Erlang_ERTS_DIR}/include)
endif()

FIND_PACKAGE_HANDLE_STANDARD_ARGS(
Erlang
DEFAULT_MSG
Erlang_RUNTIME
Erlang_COMPILE
Erlang_OTP_LIB_DIR
Erlang_OTP_ROOT_DIR
Erlang_EI_DIR
Erlang_ERTS_DIR
Erlang_ERTS_INCLUDE_DIR
)

if(Erlang_FOUND)
if(NOT TARGET Erlang::Erlang)
add_library(Erlang::Erlang INTERFACE IMPORTED)
set_target_properties(Erlang::Erlang
PROPERTIES
INTERFACE_INCLUDE_DIRECTORIES ${Erlang_OTP_ROOT_DIR}/usr/include
)
endif()

if(NOT TARGET Erlang::ERTS)
add_library(Erlang::ERTS STATIC IMPORTED)
set_target_properties(Erlang::ERTS
PROPERTIES
INTERFACE_INCLUDE_DIRECTORIES ${Erlang_ERTS_INCLUDE_DIRS}
IMPORTED_LOCATION ${Erlang_ERTS_LIBRARY_PATH}/liberts.a
)
endif()

if(NOT TARGET Erlang::EI)
add_library(erlang_ei STATIC IMPORTED)
set_property(TARGET erlang_ei PROPERTY
IMPORTED_LOCATION ${Erlang_EI_LIBRARY_PATH}/libei.a
)
add_library(Erlang::EI INTERFACE IMPORTED)
set_property(TARGET Erlang::EI PROPERTY
INTERFACE_INCLUDE_DIRECTORIES ${Erlang_EI_INCLUDE_DIRS}
)
set_property(TARGET Erlang::EI PROPERTY
INTERFACE_LINK_LIBRARIES erlang_ei
INTERFACE_INCLUDE_DIRECTORIES ${Erlang_ERTS_INCLUDE_DIR}
)
endif()
endif(Erlang_FOUND)

0 comments on commit cdc3a10

Please sign in to comment.