VKCOM / kphp

KPHP — a PHP compiler
https://vkcom.github.io/kphp/
GNU General Public License v3.0
1.36k stars 92 forks source link

Linking fails - libcurl #1056

Closed henrywood closed 3 months ago

henrywood commented 3 months ago

I am trying to compile this code

<?php

//-------------------------------------------------------------

//namespace App\Enums;

class EmulatedEnumString {

        protected string $value;

        protected function __construct(string $value) {

                $this->value = $value;
        }

        public function __toString() : string {
                return (string)$this->value;
        }
}

//-------------------------------------------------------------

//namespace App\Enums;

class EmulatedEnumInt {

        protected int $value;

        protected function __construct(int $value) {

                $this->value = $value;
        }

        public function __toString() : string {
                return (string) $this->value;
        }
}

//-------------------------------------------------------------

#ifndef KPHP
enum BackendAuthType : string {
case NONE = 'NONE';
case JWT = 'JWT';
case DIGEST = 'DIGEST';
case LOGGEDIN = 'LOGGED_IN';
}
if (false) {
        #endif
        final class BackendAuthType extends EmulatedEnumString
        {
                const NONE = 'NONE';
                const JWT = 'JWT';
                const DIGEST = 'DIGEST';
                const LOGGEDIN = 'LOGGED_IN';

                // GENERATED
                public static function NONE() : static {

                        $x = new static('NONE');
                        return $x;
                }
                public static function JWT() : static {

                        $x = new static('JWT');
                        return $x;
                }
                public static function DIGEST() : static {

                        $x = new static('DIGEST');
                        return $x;
                }
                public static function LOGGEDIN() : static {

                        $x = new static('LOGGEDIN');
                        return $x;
                }
        }
        #ifndef KPHP
}
#endif

//-------------------------------------------------------------

#ifndef KPHP
enum Reply : int {
case OK = 200;
case NOT_FOUND = 404;
case ERROR = 500;
case UNAUTHORIZED = 401;
case UNAUTHORIZED_TOKEN_EXPIRED = 410;
case BAD_REQUEST = 400;
case METHOD_NOT_ALLOWED = 405;
case INVALID_API_KEY = 403;
}
if (false) {
        #endif
        final class Reply extends EmulatedEnumInt
        {
                const OK = 200;
                const NOT_FOUND = 404;
                const ERROR = 500;
                const UNAUTHORIZED = 401;
                const UNAUTHORIZED_TOKEN_EXPIRED = 410;
                const BAD_REQUEST = 400;
                const METHOD_NOT_ALLOWED = 405;
                const INVALID_API_KEY = 403;

                // GENERATED
                public static function OK() : static {

                        $x = new static(200);
                        return $x;
                }

                public static function NOT_FOUND() : static {

                        $x = new static(400);
                        return $x;
                }

                public static function ERROR() : static {

                        $x = new static(500);
                        return $x;
                }

                public static function UNAUTHORIZED() : static {

                        $x = new static(401);
                        return $x;
                }

                public static function UNAUTHORIZED_TOKEN_EXPIRED() : static {

                        $x = new static(410);
                        return $x;
                }

                public static function BAD_REQUEST() : static {

                        $x = new static(400);
                        return $x;
                }

                public static function METHOD_NOT_ALLOWED() : static {

                        $x = new static(405);
                        return $x;
                }
                public static function INVALID_API_KEY() : static {

                        $x = new static(403);
                        return $x;
                }
        }
        #ifndef KPHP
}
#endif

///////////////////////////////////////////////////////////////////////////

#ifndef KPHP
$OK = Reply::tryFrom(200)->value;
if (FALSE) {
#endif
$OK = Reply::OK();
#ifndef KPHP
}
#endif

echo $OK.PHP_EOL;

and I get this:

henrik@HSLAPTOP-ASUS:/tmp$ kphp --mode cli enum.php -o ./enum
Starting php to cpp transpiling...

Starting make...
Compiling pch stage started...
objs cnt = 11
objs cnt = 10
Compiling stage started...
  0% [total jobs 15] [left jobs 15] [running jobs 0] [waiting jobs 4]
  6% [total jobs 15] [left jobs 14] [running jobs 7] [waiting jobs 4]
 13% [total jobs 15] [left jobs 13] [running jobs 7] [waiting jobs 3]
 20% [total jobs 15] [left jobs 12] [running jobs 7] [waiting jobs 3]
 26% [total jobs 15] [left jobs 11] [running jobs 7] [waiting jobs 3]
 33% [total jobs 15] [left jobs 10] [running jobs 7] [waiting jobs 3]
 40% [total jobs 15] [left jobs 9] [running jobs 6] [waiting jobs 2]
 46% [total jobs 15] [left jobs 8] [running jobs 6] [waiting jobs 2]
 53% [total jobs 15] [left jobs 7] [running jobs 5] [waiting jobs 2]
 60% [total jobs 15] [left jobs 6] [running jobs 4] [waiting jobs 2]
 66% [total jobs 15] [left jobs 5] [running jobs 3] [waiting jobs 2]
 73% [total jobs 15] [left jobs 4] [running jobs 2] [waiting jobs 1]
 80% [total jobs 15] [left jobs 3] [running jobs 2] [waiting jobs 1]
 86% [total jobs 15] [left jobs 2] [running jobs 1] [waiting jobs 1]
 93% [total jobs 15] [left jobs 1] [running jobs 0] [waiting jobs 0]
100% [total jobs 15] [left jobs 0] [running jobs 0] [waiting jobs 0]
Linking stage started...
/usr/bin/ld: /opt/curl7600/lib/libcurl.a(libcurl_la-openssl.o): in function `servercert':
openssl.c:(.text+0x1fa8): undefined reference to `SSL_get1_peer_certificate'
/usr/bin/ld: openssl.c:(.text+0x2a12): undefined reference to `EVP_PKEY_get_id'
/usr/bin/ld: /opt/curl7600/lib/libcurl.a(libcurl_la-openssl.o): in function `ossl_connect_step1':
openssl.c:(.text+0x4aef): undefined reference to `EVP_PKEY_get_id'
collect2: error: ld returned 1 exit status
pid [302263] failed or terminated : return code 1
Failed [g++  -o /tmp/kphp_out/cli -Wl,--whole-archive -Wl,--start-group /tmp/kphp_out/objs/_shape_keys.o /tmp/kphp_out/objs/main.o /tmp/kphp_out/objs/_tagger.o /tmp/kphp_out/objs/init_php_scripts.o /tmp/kphp_out/objs/vars.o /tmp/kphp_out/objs/vars_reset.src_enumad40eed0d473dbde.o /tmp/kphp_out/objs/o_28_6d21bd6a94f0de1d.o /tmp/kphp_out/objs/o_57_5497b566a8c8cf7f.o /tmp/kphp_out/objs/o_vars_0_f0d514d0f1758740.o /tmp/kphp_out/objs/o_vars_reset_1147a6bb47cb6082.o /usr/share/vkontakte/kphp_source/objs/libkphp-full-runtime.a  -Wl,--end-group -Wl,--no-whole-archive -L/usr/share/vkontakte/kphp_source//objs/flex -ggdb -fno-lto -no-pie /opt/curl7600/lib/libcurl.a -L /usr/local/lib -L /usr/local/lib -l:libpcre.a -l:libre2.a -l:libyaml-cpp.a -l:libh3.a -l:libz.a -l:libzstd.a -l:libnghttp2.a -l:libkphp-timelib.a -l:libssl.a -l:libcrypto.a -l:libnuma.a -l:libvk-flex-data.a -lpthread -lm -ldl -lrt -rdynamic]
Make failed. Waiting for 0 children

How can I fix this ?

Hint: I have PHP8 on the machine where I try to compile.
Also trying a PHP7.4 machine and there it compiles OK Unfortunately, it is not an option to downgrade to PHP 7.4

I installed KPHP from your repo if I remember correctly.

This is the KPHP version:

henrik@HSLAPTOP-ASUS:/tmp$ kphp --version
kphp2cpp compiled at Feb  9 2024 18:56:41 CET by gcc 9.4.0 64-bit after commit 82f3514c53acde7a1baa50f98d5f2cd31ca02a6e build 2889

Please help !

mkornaukhov03 commented 3 months ago

Hi! Could you also provide versions of openssl lib on your machines? And which OS you tried on?

henrywood commented 3 months ago

Ubuntu 20.04: (Compilation / linking fails here)

root@HSLAPTOP-ASUS:/tmp/binutils-2.38# dpkg -l | grep openssl
ii  libcurl4-openssl-dev:amd64            7.68.0-1ubuntu2.22                                                   amd64        development files and documentation for libcurl (OpenSSL flavour)
ii  libgnutls-openssl27:amd64             3.6.13-2ubuntu1.11                                                   amd64        GNU TLS library - OpenSSL wrapper
ii  libxmlsec1-openssl:amd64              1.2.28-2                                                             amd64        Openssl engine for the XML security library
ii  openssl                               1.1.1f-1ubuntu2.23                                                   amd64        Secure Sockets Layer toolkit - cryptographic utility
ii  python3-openssl                       19.0.0-1build1                                                       all          Python 3 wrapper around the OpenSSL library
root@HSLAPTOP-ASUS:/tmp/binutils-2.38# cat /etc/lsb-release
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=20.04
DISTRIB_CODENAME=focal
DISTRIB_DESCRIPTION="Ubuntu 20.04.5 LTS"

Ubuntu 22.04: (Compilation / linking succeeds here)

henrik@HSLAPTOP-ASUS2:~/procname2$ dpkg -l | grep openssl
ii  openssl                           3.0.2-0ubuntu1.6                             amd64        Secure Sockets Layer toolkit - cryptographic utility

On the Ubuntu 20.04 machine it is not even possible to compile KPHP from sources (see #1057) (as linking fails)

henrywood commented 3 months ago

Any news on this issue ?

henrywood commented 3 months ago

Upgrading the Ubuntu 20.04 to Ubuntu 22.04 and re-compiling KPHP solved the linking problem