diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 89a7dcc920..f2db42c3dd 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -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 diff --git a/vlib/dl/dl_nix.c.v b/vlib/dl/dl_nix.c.v index 82d9d98f91..58553255fe 100644 --- a/vlib/dl/dl_nix.c.v +++ b/vlib/dl/dl_nix.c.v @@ -1,11 +1,10 @@ module dl #include - 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 +} diff --git a/vlib/dl/example/library.v b/vlib/dl/example/library.v new file mode 100644 index 0000000000..9be3480298 --- /dev/null +++ b/vlib/dl/example/library.v @@ -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 +} diff --git a/vlib/dl/example/use.v b/vlib/dl/example/use.v new file mode 100644 index 0000000000..e5f5580191 --- /dev/null +++ b/vlib/dl/example/use.v @@ -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') +} diff --git a/vlib/dl/example/use_test.v b/vlib/dl/example/use_test.v new file mode 100644 index 0000000000..dd5d0dec48 --- /dev/null +++ b/vlib/dl/example/use_test.v @@ -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 +} diff --git a/vlib/v/builder/cc.v b/vlib/v/builder/cc.v index 310d39b547..7886e962bb 100644 --- a/vlib/v/builder/cc.v +++ b/vlib/v/builder/cc.v @@ -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'