dl: add a complete tested shared library generation/usage example
							parent
							
								
									e3a1756b11
								
							
						
					
					
						commit
						3a9034a0d0
					
				|  | @ -516,22 +516,13 @@ jobs: | ||||||
|   windows-tcc: |   windows-tcc: | ||||||
|     runs-on: windows-2019 |     runs-on: windows-2019 | ||||||
|     timeout-minutes: 30 |     timeout-minutes: 30 | ||||||
|     # We are simulating a user with no cc installed. |     env: | ||||||
|     # This way, v's cc detection on Windows is also tested. |          VFLAGS: -cc tcc | ||||||
|     # env: |  | ||||||
|     #   VFLAGS: -cc tcc |  | ||||||
|     steps: |     steps: | ||||||
|     - uses: actions/checkout@v2 |     - uses: actions/checkout@v2 | ||||||
|     #- uses: actions/setup-node@v1 |     - name: Build with make.bat -tcc | ||||||
|     #  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 |  | ||||||
|       run: | |       run: | | ||||||
|         'for /f "usebackq tokens=*" %i in (`where gcc.exe`) do move /Y "%i" "%i.old"'     | cmd |         .\make.bat -tcc | ||||||
|         '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 |  | ||||||
|     - name: Test new v.c |     - name: Test new v.c | ||||||
|       run: .\v.exe -o v.c cmd/v && .\thirdparty\tcc\tcc.exe -Werror -w -ladvapi32 -bt10 v.c |       run: .\v.exe -o v.c cmd/v && .\thirdparty\tcc\tcc.exe -Werror -w -ladvapi32 -bt10 v.c | ||||||
|     - name: Install dependencies |     - name: Install dependencies | ||||||
|  |  | ||||||
|  | @ -1,11 +1,10 @@ | ||||||
| module dl | module dl | ||||||
| 
 | 
 | ||||||
| #include <dlfcn.h> | #include <dlfcn.h> | ||||||
| 
 |  | ||||||
| pub const ( | pub const ( | ||||||
| 	rtld_now = C.RTLD_NOW | 	rtld_now  = C.RTLD_NOW | ||||||
| 	rtld_lazy = C.RTLD_LAZY | 	rtld_lazy = C.RTLD_LAZY | ||||||
| 	dl_ext = '.so' | 	dl_ext    = get_shared_library_extension() | ||||||
| ) | ) | ||||||
| 
 | 
 | ||||||
| fn C.dlopen(filename charptr, flags int) voidptr | 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 { | pub fn sym(handle voidptr, symbol string) voidptr { | ||||||
| 	return C.dlsym(handle, symbol.str) | 	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 | ||||||
|  | } | ||||||
|  |  | ||||||
|  | @ -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 | ||||||
|  | } | ||||||
|  | @ -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') | ||||||
|  | } | ||||||
|  | @ -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 | ||||||
|  | } | ||||||
|  | @ -267,15 +267,17 @@ fn (mut v Builder) cc() { | ||||||
| 		// linux_host := os.user_os() == 'linux'
 | 		// linux_host := os.user_os() == 'linux'
 | ||||||
| 		v.log('cc() isprod=$v.pref.is_prod outname=$v.pref.out_name') | 		v.log('cc() isprod=$v.pref.is_prod outname=$v.pref.out_name') | ||||||
| 		if v.pref.is_shared { | 		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' | 			linker_flags << '-shared' | ||||||
| 			args << '-fPIC' // -Wl,-z,defs'
 | 			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 { | 		if v.pref.is_bare { | ||||||
| 			args << '-fno-stack-protector' | 			args << '-fno-stack-protector' | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue