dl: add a complete tested shared library generation/usage example

pull/7351/head
Delyan Angelov 2020-12-15 18:22:07 +02:00
parent e3a1756b11
commit 3a9034a0d0
No known key found for this signature in database
GPG Key ID: 66886C0F12D595ED
6 changed files with 105 additions and 23 deletions

View File

@ -516,22 +516,13 @@ jobs:
windows-tcc:
runs-on: windows-2019
timeout-minutes: 30
# We are simulating a user with no cc installed.
# This way, v's cc detection on Windows is also tested.
# env:
# VFLAGS: -cc tcc
env:
VFLAGS: -cc tcc
steps:
- uses: actions/checkout@v2
#- uses: actions/setup-node@v1
# with:
# node-version: 12.x
- name: Build
# We need to move gcc and msvc, so that V can't find an existing C compiler and downloads tcc
- name: Build with make.bat -tcc
run: |
'for /f "usebackq tokens=*" %i in (`where gcc.exe`) do move /Y "%i" "%i.old"' | cmd
'for /f "usebackq tokens=*" %i in (`where vswhere.exe`) do move /Y "%i" "%i.old"' | cmd
move "${env:ProgramFiles(x86)}\Microsoft Visual Studio\Installer\vswhere.exe" "${env:ProgramFiles(x86)}\Microsoft Visual Studio\Installer\vswhere.exe.old"
.\make.bat
.\make.bat -tcc
- name: Test new v.c
run: .\v.exe -o v.c cmd/v && .\thirdparty\tcc\tcc.exe -Werror -w -ladvapi32 -bt10 v.c
- name: Install dependencies

View File

@ -1,11 +1,10 @@
module dl
#include <dlfcn.h>
pub const (
rtld_now = C.RTLD_NOW
rtld_now = C.RTLD_NOW
rtld_lazy = C.RTLD_LAZY
dl_ext = '.so'
dl_ext = get_shared_library_extension()
)
fn C.dlopen(filename charptr, flags int) voidptr
@ -28,3 +27,14 @@ pub fn close(handle voidptr) bool {
pub fn sym(handle voidptr, symbol string) voidptr {
return C.dlsym(handle, symbol.str)
}
pub fn get_shared_library_extension() string {
mut res := '.so'
$if macos {
res = '.dylib'
}
$if windows {
res = '.dll'
}
return res
}

View File

@ -0,0 +1,10 @@
module library
[export: 'add_1']
pub fn add_1(x int, y int) int {
return my_private_function(x + y)
}
fn my_private_function(x int) int {
return 1 + x
}

View File

@ -0,0 +1,17 @@
module main
import os
import dl
type FNAdder = fn (int, int) int
fn main() {
library_file_path := os.join_path(os.getwd(), 'library${dl.dl_ext}')
handle := dl.open(library_file_path, dl.rtld_lazy)
eprintln('handle: ${ptr_str(handle)}')
mut f := &FNAdder(0)
f = dl.sym(handle, 'add_1')
eprintln('f: ${ptr_str(f)}')
res := f(1, 2)
eprintln('res: $res')
}

View File

@ -0,0 +1,52 @@
module main
import os
import dl
const (
vexe = os.real_path(os.getenv('VEXE'))
cfolder = os.dir(@FILE)
so_ext = dl.dl_ext
library_file_name = os.join_path(cfolder, 'library$so_ext')
)
fn test_vexe() {
eprintln('vexe: $vexe')
assert vexe != ''
eprintln('os.executable: ' + os.executable())
eprintln('@FILE: ' + @FILE)
eprintln('cfolder: $cfolder')
eprintln('so_ext: $so_ext')
eprintln('library_file_name: $library_file_name')
}
fn test_can_compile_library() {
os.chdir(cfolder)
os.rm(library_file_name)
res := v_compile('-d no_backtrace -o library -shared library.v')
eprintln('res: $res')
assert os.is_file(library_file_name)
}
fn test_can_compile_main_program() {
os.chdir(cfolder)
assert os.is_file(library_file_name)
result := v_compile('run use.v')
eprintln('result: $result')
assert result.output.contains('res: 4')
os.rm(library_file_name)
}
fn v_compile(vopts string) os.Result {
cmd := '"$vexe" -showcc $vopts'
eprintln('>>> v_compile cmd: $cmd')
res := os.exec(cmd) or { panic(err) }
eprintln('>>> v_compile res: $res')
// assert res.exit_code == 0
$if !windows {
os.system('dir $cfolder -a -l')
} $else {
os.system('dir $cfolder /a')
}
return res
}

View File

@ -267,15 +267,17 @@ fn (mut v Builder) cc() {
// linux_host := os.user_os() == 'linux'
v.log('cc() isprod=$v.pref.is_prod outname=$v.pref.out_name')
if v.pref.is_shared {
mut shared_postfix := '.so'
$if macos {
shared_postfix = '.dylib'
} $else $if windows {
shared_postfix = '.dll'
}
if !v.pref.out_name.ends_with(shared_postfix) {
v.pref.out_name += shared_postfix
}
linker_flags << '-shared'
args << '-fPIC' // -Wl,-z,defs'
$if macos {
v.pref.out_name += '.dylib'
} $else $if windows {
v.pref.out_name += '.dll'
} $else {
v.pref.out_name += '.so'
}
}
if v.pref.is_bare {
args << '-fno-stack-protector'