vlang / v

Simple, fast, safe, compiled language for developing maintainable software. Compiles itself in <1s with zero library dependencies. Supports automatic C => V translation. https://vlang.io
MIT License
35.79k stars 2.16k forks source link

Failed to compile vweb on Apple M1 (arm) #14888

Closed ultd closed 1 year ago

ultd commented 2 years ago

V doctor:

OS: macos, macOS, 12.0, 21A344
Processor: 10 cpus, 64bit, little endian, Apple M1 Pro
CC version: Apple clang version 13.1.6 (clang-1316.0.21.2.3)

getwd: /Users/unlimited/syndica/syndica/v-example
vmodules: /Users/unlimited/.vmodules
vroot: /Users/unlimited/v
vexe: /Users/unlimited/v/v
vexe mtime: 2022-06-29 15:04:08
is vroot writable: true
is vmodules writable: true
V full version: V 0.2.4 9242390.f2be115

Git version: git version 2.32.0 (Apple Git-132)
Git vroot status: weekly.2022.26-11-gf2be115f
.git/config present: true
thirdparty/tcc status: thirdparty-macos-arm64 173c526e

What did you do? v -g -o vdbg cmd/v && vdbg v-example.v

module main

import vweb

struct App {
    vweb.Context
}

fn main() {
    vweb.run(&App{}, 8080)
}

What did you expect to see?

The app to run

What did you see instead?

==================
   ^~~~~~~~~~~~~~~
/tmp/v_501/v-example.6463870415103349028.tmp.c:25684:3: error: implicit declaration of function 'OPENSSL_init_ssl' is invalid in C99 [-Werror,-Wimplicit-function-declaration]
                OPENSSL_init_ssl(OPENSSL_INIT_LOAD_SSL_STRINGS, 0);
                ^
/tmp/v_501/v-example.6463870415103349028.tmp.c:25684:3: note: did you mean 'OPENSSL_init'?
/usr/local/Cellar/openssl@1.0/1.0.2t/include/openssl/crypto.h:600:6: note: 'OPENSSL_init' declared here
void OPENSSL_init(void);
     ^
/tmp/v_501/v-example.6463870415103349028.tmp.c:25684:20: error: use of undeclared identifier 'OPENSSL_INIT_LOAD_SSL_STRINGS'
                OPENSSL_init_ssl(OPENSSL_INIT_LOAD_SSL_STRINGS, 0);
                                 ^
/tmp/v_501/v-example.6463870415103349028.tmp.c:26004:4: warning: expression result unused [-Wunused-value]
...
==================
(Use `v -cg` to print the entire error message)

builder error: 
==================
C error. This should never happen.

This is a compiler bug, please report it using `v bug file.v`.

https://github.com/vlang/v/issues/new/choose

You can also use #help on Discord: https://discord.gg/vlang
Hunam6 commented 2 years ago

It works fine for me, the only difference is that I have a M1 not pro... Did you installed OpenSSL? If no your case isn't detected by V that's weird. Try installing it using brew install openssl. If yes I don't know.

JalonSolov commented 2 years ago

The C compiler found the openssl header, though they're from a really old release (1.0.2t, from the path). Maybe that's the only issue - need a newer version of openssl (and headers) to have that resolved.

jminor commented 2 years ago

I am also having trouble compiling/running any vweb examples on my M1 MacBookPro, though the symptom is different. V complains

builder error: Your V installation may be out-of-date. Try removing `thirdparty/tcc/` and running `make`

Even though I have a fresh clone & build from github. I can work around this by building v as x86_64 and setting VFLAGS="-d dynamic_boehm".

Side note: I have the latest brew install openssl per the advice in the thread above.

Steps to repro, with "Note:" annotations for clarity

(base) ~/git ⭐ git clone https://github.com/vlang/v v-repro
Cloning into 'v-repro'...
remote: Enumerating objects: 124239, done.
remote: Counting objects: 100% (124239/124239), done.
remote: Compressing objects: 100% (32776/32776), done.
remote: Total 124239 (delta 88772), reused 124051 (delta 88675), pack-reused 0
Receiving objects: 100% (124239/124239), 56.03 MiB | 6.55 MiB/s, done.
Resolving deltas: 100% (88772/88772), done.
(base) ~/git ⭐ cd v-repro 
(base) ~/git/v-repro ⭐ ls
CHANGELOG.md        Dockerfile.alpine   Makefile            cmd/                thirdparty/
CODE_OF_CONDUCT.md  Dockerfile.cross    README.md           doc/                tutorials/
CONTRIBUTING.md     GNUmakefile         ROADMAP.md          examples/           v.mod
Dockerfile          LICENSE             TESTS.md            make.bat            vlib/

# Note: my computer is an M1 MacBookPro running macOS 12.4
(base) ~/git/v-repro ⭐ uname -a
Darwin gemini.attlocal.net 21.5.0 Darwin Kernel Version 21.5.0: Tue Apr 26 21:08:37 PDT 2022; root:xnu-8020.121.3~4/RELEASE_ARM64_T6000 arm64

# Note: Build v as arm64 by default
(base) ~/git/v-repro ⭐ make
/Applications/Xcode.app/Contents/Developer/usr/bin/make fresh_vc
rm -rf ./vc
git clone --depth 1 --quiet --single-branch https://github.com/vlang/vc ./vc
cd ./vc && git clean -xf && git pull --quiet
/Applications/Xcode.app/Contents/Developer/usr/bin/make fresh_tcc
rm -rf ./thirdparty/tcc
git clone --depth 1 --quiet --single-branch --branch thirdparty-macos-arm64 https://github.com/vlang/tccbin ./thirdparty/tcc
cd ./thirdparty/tcc && git clean -xf && git pull --quiet
cc  -std=gnu99 -w -o v1.exe ./vc/v.c -lm -lpthread 
./v1.exe -no-parallel -o v2.exe  cmd/v
./v2.exe -o ./v  cmd/v
rm -rf v1.exe v2.exe
V has been successfully built
V 0.3.0 706a922
(base) ~/git/v-repro ⭐ file v
v: Mach-O 64-bit executable arm64
(base) ~/git/v-repro ⭐ ./v symlink
(base) ~/git/v-repro ⭐ ll `which v`
lrwxr-xr-x  1 jminor  admin    27B Jul 17 12:12 /usr/local/bin/v@ -> /Users/jminor/git/v-repro/v

# Note: Most examples work fine
(base) ~/git/v-repro ⭐ v run examples/cli.v 
Usage: cli [flags] [commands]

An example of the cli library.

Flags:
  -help               Prints help information.
  -version            Prints version information.
  -man                Prints the auto-generated manpage.

Commands:
  greet               Prints greeting in different languages.
  help                Prints help information.
  version             Prints version information.
  man                 Prints the auto-generated manpage.

# Note: vweb examples fail with builder error:
(base) ~/git/v-repro ⭐ v run examples/vweb/server_sent_events/server.v
builder error: Your V installation may be out-of-date. Try removing `thirdparty/tcc/` and running `make`

# Note: Try using different VFLAGS (per advice on Discord)
(base) ~/git/v-repro ⭐ export VFLAGS="-d dynamic_boehm"
(base) ~/git/v-repro ⭐ v run examples/vweb/server_sent_events/server.v
==================
ld: symbol(s) not found for architecture arm64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
...
==================
(Use `v -cg` to print the entire error message)

builder error: 
==================
C error. This should never happen.

This is a compiler bug, please report it using `v bug file.v`.

https://github.com/vlang/v/issues/new/choose

You can also use #help on Discord: https://discord.gg/vlang

(base) ~/git/v-repro ⭐ unset VFLAGS

# Note: Re-build v as x86_64
(base) ~/git/v-repro ⭐ make clean
rm -rf ./thirdparty/tcc
rm -rf ./vc
(base) ~/git/v-repro ⭐ arch -x86_64 make                                           
/Applications/Xcode.app/Contents/Developer/usr/bin/make fresh_vc
rm -rf ./vc
git clone --depth 1 --quiet --single-branch https://github.com/vlang/vc ./vc
cd ./vc && git clean -xf && git pull --quiet
/Applications/Xcode.app/Contents/Developer/usr/bin/make fresh_tcc
rm -rf ./thirdparty/tcc
git clone --depth 1 --quiet --single-branch --branch thirdparty-macos-amd64 https://github.com/vlang/tccbin ./thirdparty/tcc
cd ./thirdparty/tcc && git clean -xf && git pull --quiet
cc  -std=gnu99 -w -o v1.exe ./vc/v.c -lm -lpthread 
./v1.exe -no-parallel -o v2.exe -d dynamic_boehm cmd/v
./v2.exe -o ./v -d dynamic_boehm cmd/v
rm -rf v1.exe v2.exe
V has been successfully built
V 0.3.0 706a922
(base) ~/git/v-repro ⭐ file v
v: Mach-O 64-bit executable x86_64
(base) ~/git/v-repro ⭐ v version
V 0.3.0 706a922
(base) ~/git/v-repro ⭐ ll `which v`
lrwxr-xr-x  1 jminor  admin    27B Jul 17 12:12 /usr/local/bin/v@ -> /Users/jminor/git/v-repro/v

# Note: most examples work fine still
(base) ~/git/v-repro ⭐ v run examples/cli.v 
Usage: cli [flags] [commands]

An example of the cli library.

Flags:
  -help               Prints help information.
  -version            Prints version information.
  -man                Prints the auto-generated manpage.

Commands:
  greet               Prints greeting in different languages.
  help                Prints help information.
  version             Prints version information.
  man                 Prints the auto-generated manpage.                                                                                                                               (base) ~/git/v-repro ⭐ unset VFLAGS                                   

# Note: vweb examples still fail
(base) ~/git/v-repro ⭐ v run examples/vweb/server_sent_events/server.v
builder error: Your V installation may be out-of-date. Try removing `thirdparty/tcc/` and running `make`

# Note: Set VFLAGS...
(base) ~/git/v-repro ⭐ export VFLAGS="-d dynamic_boehm"

# Note: Now it works! Yay!
(base) ~/git/v-repro ⭐ v run examples/vweb/server_sent_events/server.v
[Vweb] Running app on http://localhost:8081/
> sent event: {"time": "2022-07-17 12:14:32", "random_id": "01G86SAVB0NC7VGBW7FQ88FV4B"}
> sent event: {"time": "2022-07-17 12:14:32", "random_id": "01G86SAVB2K197PEDW78GY4GPS"}
> sent event: {"time": "2022-07-17 12:14:33", "random_id": "01G86SAWADJ0VYPEFBS7B4M7WH"}
> sent event: {"time": "2022-07-17 12:14:33", "random_id": "01G86SAWAEZJS21AW68BV8NMX5"}
^C%                                                                                                                                   (base) ~/git/v-repro ⭐ 
jminor commented 2 years ago

Ah! I think I might have found my problem. V was getting confused since my system has two copies of openssl on it. One in Homebrew's arm64 area /opt/homebrew/opt/openssl and one in Homebrew's x86_64 area /usr/local/opt/openssl'. When building, V was finding both, but preferring the wrong one. I was able to fix it by editingvlib/net/openssl/c.v` and commenting out the undesirable location here:

// Brew
//#flag darwin -I/usr/local/opt/openssl/include
//#flag darwin -L/usr/local/opt/openssl/lib
// Brew arm64
#flag darwin -I /opt/homebrew/opt/openssl/include
#flag darwin -L /opt/homebrew/opt/openssl/lib

After rebuilding V (now native arm64) now vweb seems to work fine.

jminor commented 2 years ago

It seems that both the OP and my issue were related to finding the right openssl version & architecture.

medvednikov commented 2 years ago

openssl is going to be embedded soon in 0.4, so this will be fixed on its own

jminor commented 2 years ago

Glad to hear it :)

medvednikov commented 1 year ago

ssl is now embedded, should work fine