Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@wojtekmach
Copy link
Contributor

@wojtekmach wojtekmach commented Nov 29, 2021

Based on #5284, thanks @dominicletz!

Below [1] is a script to cross-compile OpenSSL & OTP for aarch64-darwin. I executed it from an Intel Mac.

A couple of notes however:

  1. Wx cannot be cross-compiled yet:

    *********************************************************************
    *********************************************************************
    **********************  APPLICATIONS INFORMATION  *******************
    *********************************************************************
    
    wx             : Cross compilation of the wx driver is not supported yet, wx will NOT be usable
    
  2. Erlang shell successfully starts:

    $ uname -sm
    Darwin arm64
    $ ./bin/erl
    Erlang/OTP 24 [erts-12.1.5] [source-df1294f108] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1]
    
    Eshell V12.1.5  (abort with ^G)
    1>
    
  3. But crypto is not working yet:

    $ ./bin/erl
    Erlang/OTP 24 [erts-12.1.5] [source-df1294f108] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1]
    
    Eshell V12.1.5  (abort with ^G)
    1> crypto:start().
    =ERROR REPORT==== 29-Nov-2021::14:50:14.218776 ===
    Unable to load crypto library. Failed with error:
    "load_failed, Failed to load NIF library: 'dlopen(/Users/wojtek/Downloads/otp-24.1.7-aarch64-darwin/lib/crypto-5.0.4/priv/lib/crypto.so, 2): no suitable image found.  Did find:
            /Users/wojtek/Downloads/otp-24.1.7-aarch64-darwin/lib/crypto-5.0.4/priv/lib/crypto.so: mach-o, but wrong filetype
            /Users/wojtek/Downloads/otp-24.1.7-aarch64-darwin/lib/crypto-5.0.4/priv/lib/crypto.so: stat() failed with errno=35'"
    OpenSSL might not be installed on this system.
    
    =WARNING REPORT==== 29-Nov-2021::14:50:14.222040 ===
    The on_load function for module crypto returned:
    {error,{load_failed,"Failed to load NIF library: 'dlopen(/Users/wojtek/Downloads/otp-24.1.7-aarch64-darwin/lib/crypto-5.0.4/priv/lib/crypto.so, 2): no suitable image found.  Did find:\n\t/Users/wojtek/Downloads/otp-24.1.7-aarch64-darwin/lib/crypto-5.0.4/priv/lib/crypto.so: mach-o, but wrong filetype\n\t/Users/wojtek/Downloads/otp-24.1.7-aarch64-darwin/lib/crypto-5.0.4/priv/lib/crypto.so: stat() failed with errno=35'"}}
    
    ** exception error: undefined function crypto:start/0
    

Any guidance with how to make crypto and later wx work would be very appreciated.

[1] the script:

set -e

main() {
  export MAKEFLAGS=-j$(getconf _NPROCESSORS_ONLN)

  openssl_vsn=1.1.1l
  otp_vsn=$(cat OTP_VERSION)
  target=aarch64-darwin

  openssl_dir="$PWD/tmp/openssl-$openssl_vsn-$target"
  otp_dir="$PWD/tmp/otp-$otp_vsn-$target"

  mkdir -p tmp

  if [ ! -d "$openssl_dir" ]; then
    build_openssl $openssl_dir $openssl_vsn $target
  fi

  export PATH=$openssl_dir/bin:$PATH
  echo "openssl"
  file `which openssl`
  echo

  if [ ! -d $otp_dir ]; then
    build_otp $otp_dir $otp_vsn $target $openssl_dir
  fi

  (cd $otp_dir && ./Install -sasl $PWD)
  export PATH=$otp_dir/bin:$PATH
  echo "otp"
  file `which erlc`
  file $otp_dir/lib/crypto-*/priv/lib/crypto.so
  # erl +V
  # erl -noshell -eval 'ok = crypto:start(), io:format("crypto ok~n"), halt().'
  echo
}

build_openssl() {
  dir=$1
  vsn=$2
  target=$3

  if [ "$target" = "x86_64-darwin" ]; then
    openssl_target=darwin64-x86_64-cc
  fi
  if [ "$target" = "aarch64-darwin" ]; then
    openssl_target=darwin64-arm64-cc
  fi

  cd tmp

  if [ ! -d openssl-$vsn-src ]; then
    url=https://www.openssl.org/source/openssl-$vsn.tar.gz
      echo "downloading $url"
    curl --fail -LO $url
    tar -xf openssl-$vsn.tar.gz
    mv openssl-$vsn openssl-$vsn-src
  fi

  cd openssl-$vsn-src
  ./Configure \
    $openssl_target \
    --prefix=$dir
  make
  make install_sw
  cd ../..
}

build_otp() {
  dir=$1
  vsn=$2
  target=$3
  openssl_dir=$4
  cwd=$PWD

  # path to OTP git checkout. Comment-out if you want to download it.
  otp_src_dir=$HOME/src/otp

  if [ ! -d "$otp_src_dir" ]; then
    cd tmp
    url="https://github.com/erlang/otp/releases/download/OTP-${vsn}/otp_src_${vsn}.tar.gz"
    echo "downloading $url"
    curl --fail -LO $url
    tar -xf otp_src_${vsn}.tar.gz
    cd otp_src_${vsn}
  fi

  export ERL_TOP=`pwd`

  ./otp_build configure \
    --disable-dynamic-ssl-lib \
    --with-ssl=$openssl_dir \
    --xcomp-conf=xcomp/erl-xcomp-$target.conf

  ./otp_build boot -a
  ./otp_build release -a $dir
  cd $cwd
}

main

Below [1] is a script to cross-compile OpenSSL & OTP for aarch64-darwin.

A couple of notes however:

1. Wx cannot be cross-compiled yet:

   > ```
   > *********************************************************************
   > *********************************************************************
   > **********************  APPLICATIONS INFORMATION  *******************
   > *********************************************************************
   >
   > wx             : Cross compilation of the wx driver is not supported yet, wx will NOT be usable
   > ```

2. Erlang shell successfully starts:

       $ uname -sm
       Darwin arm64
       $ ./bin/erl
       Erlang/OTP 24 [erts-12.1.5] [source-df1294f108] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1]

       Eshell V12.1.5  (abort with ^G)
       1>

3. But crypto is not working yet:

       ./bin/erl
       Erlang/OTP 24 [erts-12.1.5] [source-df1294f108] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1]

       Eshell V12.1.5  (abort with ^G)
       1> crypto:start().
       =ERROR REPORT==== 29-Nov-2021::14:50:14.218776 ===
       Unable to load crypto library. Failed with error:
       "load_failed, Failed to load NIF library: 'dlopen(/Users/wojtek/Downloads/otp-24.1.7-aarch64-darwin/lib/crypto-5.0.4/priv/lib/crypto.so, 2): no suitable image found.  Did find:
               /Users/wojtek/Downloads/otp-24.1.7-aarch64-darwin/lib/crypto-5.0.4/priv/lib/crypto.so: mach-o, but wrong filetype
               /Users/wojtek/Downloads/otp-24.1.7-aarch64-darwin/lib/crypto-5.0.4/priv/lib/crypto.so: stat() failed with errno=35'"
       OpenSSL might not be installed on this system.

       =WARNING REPORT==== 29-Nov-2021::14:50:14.222040 ===
       The on_load function for module crypto returned:
       {error,{load_failed,"Failed to load NIF library: 'dlopen(/Users/wojtek/Downloads/otp-24.1.7-aarch64-darwin/lib/crypto-5.0.4/priv/lib/crypto.so, 2): no suitable image found.  Did find:\n\t/Users/wojtek/Downloads/otp-24.1.7-aarch64-darwin/lib/crypto-5.0.4/priv/lib/crypto.so: mach-o, but wrong filetype\n\t/Users/wojtek/Downloads/otp-24.1.7-aarch64-darwin/lib/crypto-5.0.4/priv/lib/crypto.so: stat() failed with errno=35'"}}

       ** exception error: undefined function crypto:start/0

[1] the script:

```
set -e

main() {
  export MAKEFLAGS=-j$(getconf _NPROCESSORS_ONLN)

  openssl_vsn=1.1.1l
  otp_vsn=$(cat OTP_VERSION)
  target=aarch64-darwin

  openssl_dir="$PWD/tmp/openssl-$openssl_vsn-$target"
  otp_dir="$PWD/tmp/otp-$otp_vsn-$target"

  mkdir -p tmp

  if [ ! -d "$openssl_dir" ]; then
    build_openssl $openssl_dir $openssl_vsn $target
  fi

  export PATH=$openssl_dir/bin:$PATH
  echo "openssl"
  file `which openssl`
  echo

  if [ ! -d $otp_dir ]; then
    build_otp $otp_dir $otp_vsn $target $openssl_dir
  fi

  (cd $otp_dir && ./Install -sasl $PWD)
  export PATH=$otp_dir/bin:$PATH
  echo "otp"
  file `which erlc`
  file $otp_dir/lib/crypto-*/priv/lib/crypto.so
  # erl +V
  # erl -noshell -eval 'ok = crypto:start(), io:format("crypto ok~n"), halt().'
  echo
}

build_openssl() {
  dir=$1
  vsn=$2
  target=$3

  if [ "$target" = "x86_64-darwin" ]; then
    openssl_target=darwin64-x86_64-cc
  fi
  if [ "$target" = "aarch64-darwin" ]; then
    openssl_target=darwin64-arm64-cc
  fi

  cd tmp

  if [ ! -d openssl-$vsn-src ]; then
    url=https://www.openssl.org/source/openssl-$vsn.tar.gz
      echo "downloading $url"
    curl --fail -LO $url
    tar -xf openssl-$vsn.tar.gz
    mv openssl-$vsn openssl-$vsn-src
  fi

  cd openssl-$vsn-src
  ./Configure \
    $openssl_target \
    --prefix=$dir
  make
  make install_sw
  cd ../..
}

build_otp() {
  dir=$1
  vsn=$2
  target=$3
  openssl_dir=$4
  cwd=$PWD

  # path to OTP git checkout. Comment-out if you want to download it.
  otp_src_dir=$HOME/src/otp

  if [ ! -d "$otp_src_dir" ]; then
    cd tmp
    url="https://github.com/erlang/otp/releases/download/OTP-${vsn}/otp_src_${vsn}.tar.gz"
    echo "downloading $url"
    curl --fail -LO $url
    tar -xf otp_src_${vsn}.tar.gz
    cd otp_src_${vsn}
  fi

  export ERL_TOP=`pwd`

  ./otp_build configure \
    --disable-dynamic-ssl-lib \
    --with-ssl=$openssl_dir \
    --xcomp-conf=xcomp/erl-xcomp-$target.conf

  ./otp_build boot -a
  ./otp_build release -a $dir
  cd $cwd
}

main
```
@wojtekmach
Copy link
Contributor Author

wojtekmach commented Nov 29, 2021

Oh, worth mentioning that applying this patch on master works too, including JIT!

Erlang/OTP 25 [DEVELOPMENT] [erts-12.1.5] [source-df1c481bd4] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] [jit]

Eshell V12.1.5  (abort with ^G)
1> erlang:system_info(emu_flavor).
jit

@bjorng bjorng added the team:VM Assigned to OTP team VM label Nov 30, 2021
@rickard-green rickard-green self-assigned this Dec 6, 2021
@rickard-green rickard-green merged commit a484d2c into erlang:maint Jan 25, 2022
@wojtekmach wojtekmach deleted the wm-xcomp-darwin branch January 25, 2022 13:10
@rickard-green
Copy link
Contributor

Merged this after removing my name as author (which was copied from the template).

Note that we do not test this xcomp configuration so we don't know how good it works now and in the future.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

team:VM Assigned to OTP team VM

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants