mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
add MSVC C backend support; fix live code reloading on Windows; other Windows fixes
This commit is contained in:

committed by
Alexander Medvednikov

parent
3cf8e18cf6
commit
e25ea7f9dd
@ -254,6 +254,13 @@ fn (p mut Parser) fn_decl() {
|
||||
typ = 'int'
|
||||
str_args = 'int argc, char** argv'
|
||||
}
|
||||
|
||||
mut dll_export_linkage := ''
|
||||
|
||||
if p.os == .msvc && p.attr == 'live' && p.pref.is_so {
|
||||
dll_export_linkage = '__declspec(dllexport) '
|
||||
}
|
||||
|
||||
// Only in C code generate User_register() instead of register()
|
||||
// Internally it's still stored as "register" in type User
|
||||
// mut fn_name_cgen := f.name
|
||||
@ -269,7 +276,7 @@ fn (p mut Parser) fn_decl() {
|
||||
if p.pref.obfuscate {
|
||||
p.genln('; // $f.name')
|
||||
}
|
||||
p.genln('$typ $fn_name_cgen($str_args) {')
|
||||
p.genln('$dll_export_linkage$typ $fn_name_cgen($str_args) {')
|
||||
}
|
||||
if is_fn_header {
|
||||
p.genln('$typ $fn_name_cgen($str_args);')
|
||||
@ -330,7 +337,7 @@ fn (p mut Parser) fn_decl() {
|
||||
fn_name_cgen = '(* $fn_name_cgen )'
|
||||
}
|
||||
// Actual fn declaration!
|
||||
mut fn_decl := '$typ $fn_name_cgen($str_args)'
|
||||
mut fn_decl := '$dll_export_linkage$typ $fn_name_cgen($str_args)'
|
||||
if p.pref.obfuscate {
|
||||
fn_decl += '; // ${f.name}'
|
||||
}
|
||||
@ -363,11 +370,21 @@ fn (p mut Parser) fn_decl() {
|
||||
// We are in live code reload mode, call the .so loader in bg
|
||||
if p.pref.is_live {
|
||||
file_base := p.file_path.replace('.v', '')
|
||||
so_name := file_base + '.so'
|
||||
p.genln('
|
||||
if p.os != .windows && p.os != .msvc {
|
||||
so_name := file_base + '.so'
|
||||
p.genln('
|
||||
load_so("$so_name");
|
||||
pthread_t _thread_so;
|
||||
pthread_create(&_thread_so , NULL, &reload_so, NULL); ')
|
||||
} else {
|
||||
so_name := file_base + if p.os == .msvc {'.dll'} else {'.so'}
|
||||
p.genln('
|
||||
live_fn_mutex = CreateMutexA(0, 0, 0);
|
||||
load_so("$so_name");
|
||||
unsigned long _thread_so;
|
||||
_thread_so = CreateThread(0, 0, (LPTHREAD_START_ROUTINE)&reload_so, 0, 0, 0);
|
||||
')
|
||||
}
|
||||
}
|
||||
if p.pref.is_test && !p.scanner.file_path.contains('/volt') {
|
||||
p.error('tests cannot have function `main`')
|
||||
@ -454,6 +471,7 @@ fn (p mut Parser) async_fn_call(f Fn, method_ph int, receiver_var, receiver_type
|
||||
// str_args contains the args for the wrapper function:
|
||||
// wrapper(arg_struct * arg) { fn("arg->a, arg->b"); }
|
||||
mut str_args := ''
|
||||
mut did_gen_something := false
|
||||
for i, arg in f.args {
|
||||
arg_struct += '$arg.typ $arg.name ;'// Add another field (arg) to the tmp struct definition
|
||||
str_args += 'arg->$arg.name'
|
||||
@ -472,7 +490,14 @@ fn (p mut Parser) async_fn_call(f Fn, method_ph int, receiver_var, receiver_type
|
||||
p.check(.comma)
|
||||
str_args += ','
|
||||
}
|
||||
did_gen_something = true
|
||||
}
|
||||
|
||||
if p.os == .msvc && !did_gen_something {
|
||||
// Msvc doesnt like empty struct
|
||||
arg_struct += 'void *____dummy_variable;'
|
||||
}
|
||||
|
||||
arg_struct += '} $arg_struct_name ;'
|
||||
// Also register the wrapper, so we can use the original function without modifying it
|
||||
fn_name = p.table.cgen_name(f)
|
||||
@ -482,7 +507,7 @@ fn (p mut Parser) async_fn_call(f Fn, method_ph int, receiver_var, receiver_type
|
||||
// Create thread object
|
||||
tmp_nr := p.get_tmp_counter()
|
||||
thread_name = '_thread$tmp_nr'
|
||||
if p.os != .windows {
|
||||
if p.os != .windows && p.os != .msvc {
|
||||
p.genln('pthread_t $thread_name;')
|
||||
}
|
||||
tmp2 := p.get_tmp()
|
||||
@ -491,7 +516,7 @@ fn (p mut Parser) async_fn_call(f Fn, method_ph int, receiver_var, receiver_type
|
||||
parg = ' $tmp_struct'
|
||||
}
|
||||
// Call the wrapper
|
||||
if p.os == .windows {
|
||||
if p.os == .windows || p.os == .msvc {
|
||||
p.genln(' CreateThread(0,0, $wrapper_name, $parg, 0,0);')
|
||||
}
|
||||
else {
|
||||
|
123
compiler/main.v
123
compiler/main.v
@ -29,7 +29,7 @@ fn modules_path() string {
|
||||
}
|
||||
|
||||
const (
|
||||
SupportedPlatforms = ['windows', 'mac', 'linux', 'freebsd', 'openbsd', 'netbsd', 'dragonfly']
|
||||
SupportedPlatforms = ['windows', 'mac', 'linux', 'freebsd', 'openbsd', 'netbsd', 'dragonfly', 'msvc']
|
||||
ModPath = modules_path()
|
||||
)
|
||||
|
||||
@ -41,6 +41,7 @@ enum OS {
|
||||
openbsd
|
||||
netbsd
|
||||
dragonfly
|
||||
msvc
|
||||
}
|
||||
|
||||
enum Pass {
|
||||
@ -189,9 +190,22 @@ fn (v mut V) compile() {
|
||||
#include <inttypes.h> // int64_t etc
|
||||
|
||||
|
||||
#ifdef _WIN32
|
||||
#ifdef _WIN32
|
||||
#define WIN32_LEAN_AND_MEAN
|
||||
#include <windows.h>
|
||||
//#include <WinSock2.h>
|
||||
//#include <WinSock2.h>
|
||||
#ifdef _MSC_VER
|
||||
// On MSVC these are the same (as long as /volatile:ms is passed)
|
||||
#define _Atomic volatile
|
||||
#endif
|
||||
|
||||
void pthread_mutex_lock(HANDLE *m) {
|
||||
WaitForSingleObject(*m, INFINITE);
|
||||
}
|
||||
|
||||
void pthread_mutex_unlock(HANDLE *m) {
|
||||
ReleaseMutex(*m);
|
||||
}
|
||||
#else
|
||||
#include <pthread.h>
|
||||
#endif
|
||||
@ -246,12 +260,22 @@ int load_so(byteptr);
|
||||
void reload_so();
|
||||
void init_consts();')
|
||||
|
||||
if v.pref.is_so {
|
||||
cgen.genln('pthread_mutex_t live_fn_mutex;')
|
||||
}
|
||||
if v.pref.is_live {
|
||||
cgen.genln('pthread_mutex_t live_fn_mutex = PTHREAD_MUTEX_INITIALIZER;')
|
||||
if v.os != .windows && v.os != .msvc {
|
||||
if v.pref.is_so {
|
||||
cgen.genln('pthread_mutex_t live_fn_mutex;')
|
||||
}
|
||||
if v.pref.is_live {
|
||||
cgen.genln('pthread_mutex_t live_fn_mutex = PTHREAD_MUTEX_INITIALIZER;')
|
||||
}
|
||||
} else {
|
||||
if v.pref.is_so {
|
||||
cgen.genln('HANDLE live_fn_mutex;')
|
||||
}
|
||||
if v.pref.is_live {
|
||||
cgen.genln('HANDLE live_fn_mutex = 0;')
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
imports_json := v.table.imports.contains('json')
|
||||
// TODO remove global UI hack
|
||||
@ -386,8 +410,24 @@ string _STR_TMP(const char *fmt, ...) {
|
||||
so_name := file_base + '.so'
|
||||
// Need to build .so file before building the live application
|
||||
// The live app needs to load this .so file on initialization.
|
||||
vexe := os.args[0]
|
||||
os.system('$vexe -o $file_base -shared $file')
|
||||
mut vexe := os.args[0]
|
||||
|
||||
if os.user_os() == 'windows' {
|
||||
vexe = vexe.replace('\\', '\\\\')
|
||||
}
|
||||
|
||||
mut msvc := ''
|
||||
if v.os == .msvc {
|
||||
msvc = '-os msvc'
|
||||
}
|
||||
|
||||
mut debug := ''
|
||||
|
||||
if v.pref.is_debug {
|
||||
debug = '-debug'
|
||||
}
|
||||
|
||||
os.system('$vexe $msvc $debug -o $file_base -shared $file')
|
||||
cgen.genln('
|
||||
|
||||
void lfnmutex_print(char *s){
|
||||
@ -397,7 +437,10 @@ void lfnmutex_print(char *s){
|
||||
fflush(stderr);
|
||||
}
|
||||
}
|
||||
')
|
||||
|
||||
if v.os != .windows && v.os != .msvc {
|
||||
cgen.genln('
|
||||
#include <dlfcn.h>
|
||||
void* live_lib=0;
|
||||
int load_so(byteptr path) {
|
||||
@ -412,8 +455,28 @@ int load_so(byteptr path) {
|
||||
return 0;
|
||||
}
|
||||
')
|
||||
for so_fn in cgen.so_fns {
|
||||
cgen.genln('$so_fn = dlsym(live_lib, "$so_fn"); ')
|
||||
for so_fn in cgen.so_fns {
|
||||
cgen.genln('$so_fn = dlsym(live_lib, "$so_fn"); ')
|
||||
}
|
||||
}
|
||||
else {
|
||||
cgen.genln('
|
||||
void* live_lib=0;
|
||||
int load_so(byteptr path) {
|
||||
char cpath[1024];
|
||||
sprintf(cpath, "./%s", path);
|
||||
if (live_lib) FreeLibrary(live_lib);
|
||||
live_lib = LoadLibraryA(cpath);
|
||||
if (!live_lib) {
|
||||
puts("open failed");
|
||||
exit(1);
|
||||
return 0;
|
||||
}
|
||||
')
|
||||
|
||||
for so_fn in cgen.so_fns {
|
||||
cgen.genln('$so_fn = (void *)GetProcAddress(live_lib, "$so_fn"); ')
|
||||
}
|
||||
}
|
||||
|
||||
cgen.genln('return 1;
|
||||
@ -434,8 +497,17 @@ void reload_so() {
|
||||
|
||||
//v -o bounce -shared bounce.v
|
||||
sprintf(new_so_base, ".tmp.%d.${file_base}", _live_reloads);
|
||||
#ifdef _WIN32
|
||||
// We have to make this directory becuase windows WILL NOT
|
||||
// do it for us
|
||||
os__mkdir(string_all_before_last(tos2(new_so_base), tos2("/")));
|
||||
#endif
|
||||
#ifdef _MSC_VER
|
||||
sprintf(new_so_name, "%s.dll", new_so_base);
|
||||
#else
|
||||
sprintf(new_so_name, "%s.so", new_so_base);
|
||||
sprintf(compile_cmd, "$vexe -o %s -shared $file", new_so_base);
|
||||
#endif
|
||||
sprintf(compile_cmd, "$vexe $msvc -o %s -shared $file", new_so_base);
|
||||
os__system(tos2(compile_cmd));
|
||||
|
||||
if( !os__file_exists(tos2(new_so_name)) ) {
|
||||
@ -443,13 +515,17 @@ void reload_so() {
|
||||
continue;
|
||||
}
|
||||
|
||||
lfnmutex_print("reload_so locking...");
|
||||
pthread_mutex_lock(&live_fn_mutex);
|
||||
lfnmutex_print("reload_so locking...");
|
||||
pthread_mutex_lock(&live_fn_mutex);
|
||||
lfnmutex_print("reload_so locked");
|
||||
|
||||
live_lib = 0; // hack: force skipping dlclose/1, the code may be still used...
|
||||
load_so(new_so_name);
|
||||
load_so(new_so_name);
|
||||
#ifndef _WIN32
|
||||
unlink(new_so_name); // removing the .so file from the filesystem after dlopen-ing it is safe, since it will still be mapped in memory.
|
||||
#else
|
||||
_unlink(new_so_name);
|
||||
#endif
|
||||
//if(0 == rename(new_so_name, "${so_name}")){
|
||||
// load_so("${so_name}");
|
||||
//}
|
||||
@ -485,7 +561,8 @@ void reload_so() {
|
||||
'./' + v.out_name
|
||||
}
|
||||
$if windows {
|
||||
cmd = v.out_name
|
||||
cmd = v.out_name
|
||||
cmd = cmd.replace('/', '\\')
|
||||
}
|
||||
if os.args.len > 3 {
|
||||
cmd += ' ' + os.args.right(3).join(' ')
|
||||
@ -569,8 +646,6 @@ fn (c &V) cc_windows_cross() {
|
||||
}
|
||||
println('Done!')
|
||||
}
|
||||
|
||||
|
||||
|
||||
fn (v mut V) cc() {
|
||||
// Cross compiling for Windows
|
||||
@ -580,6 +655,11 @@ fn (v mut V) cc() {
|
||||
return
|
||||
}
|
||||
}
|
||||
if v.os == .msvc {
|
||||
cc_msvc(v)
|
||||
return
|
||||
}
|
||||
|
||||
linux_host := os.user_os() == 'linux'
|
||||
v.log('cc() isprod=$v.pref.is_prod outname=$v.out_name')
|
||||
mut a := [v.pref.cflags, '-w'] // arguments for the C compiler
|
||||
@ -752,7 +832,7 @@ fn (v &V) v_files_from_dir(dir string) []string {
|
||||
if file.ends_with('_test.v') {
|
||||
continue
|
||||
}
|
||||
if file.ends_with('_win.v') && v.os != .windows {
|
||||
if file.ends_with('_win.v') && (v.os != .windows && v.os != .msvc) {
|
||||
continue
|
||||
}
|
||||
if file.ends_with('_lin.v') && v.os != .linux {
|
||||
@ -761,7 +841,7 @@ fn (v &V) v_files_from_dir(dir string) []string {
|
||||
if file.ends_with('_mac.v') && v.os != .mac {
|
||||
continue
|
||||
}
|
||||
if file.ends_with('_nix.v') && v.os == .windows {
|
||||
if file.ends_with('_nix.v') && (v.os == .windows || v.os == .msvc) {
|
||||
continue
|
||||
}
|
||||
res << '$dir/$file'
|
||||
@ -1004,6 +1084,7 @@ fn new_v(args[]string) *V {
|
||||
case 'openbsd': _os = .openbsd
|
||||
case 'netbsd': _os = .netbsd
|
||||
case 'dragonfly': _os = .dragonfly
|
||||
case 'msvc': _os = .msvc
|
||||
}
|
||||
}
|
||||
builtins := [
|
||||
|
278
compiler/msvc.v
Normal file
278
compiler/msvc.v
Normal file
@ -0,0 +1,278 @@
|
||||
module main
|
||||
|
||||
import os
|
||||
|
||||
#flag -I @VROOT/thirdparty/microsoft_craziness
|
||||
#flag windows @VROOT/thirdparty/microsoft_craziness/microsoft_craziness.o
|
||||
#flag windows -l ole32
|
||||
#flag windows -l oleaut32
|
||||
|
||||
// Emily: If these arent included then msvc assumes that
|
||||
// these return int (which should be 64bit)
|
||||
// but then it goes and sign extends our pointer types anyway
|
||||
// which breaks everything
|
||||
#include <microsoft_craziness.h>
|
||||
|
||||
struct MsvcResult {
|
||||
sdk_ver int
|
||||
|
||||
windows_sdk_root_path string
|
||||
exe_path string
|
||||
|
||||
um_lib_path string
|
||||
ucrt_lib_path string
|
||||
vs_lib_path string
|
||||
|
||||
um_include_path string
|
||||
ucrt_include_path string
|
||||
vs_include_path string
|
||||
shared_include_path string
|
||||
}
|
||||
|
||||
struct FindResult {
|
||||
sdk_ver int
|
||||
windows_sdk_root byteptr
|
||||
windows_sdk_um_library_path byteptr
|
||||
windows_sdk_ucrt_library_path byteptr
|
||||
vs_exe_path byteptr
|
||||
vs_library_path byteptr
|
||||
}
|
||||
|
||||
fn C.find_visual_studio_and_windows_sdk() *FindResult
|
||||
fn C.wide_string_to_narrow_temp(byteptr) byteptr
|
||||
|
||||
fn find_msvc() *MsvcResult {
|
||||
$if windows {
|
||||
r := C.find_visual_studio_and_windows_sdk()
|
||||
|
||||
windows_sdk_root := tos_clone(C.wide_string_to_narrow_temp(r.windows_sdk_root))
|
||||
ucrt_lib_folder := tos_clone(C.wide_string_to_narrow_temp(r.windows_sdk_ucrt_library_path))
|
||||
um_lib_folder := tos_clone(C.wide_string_to_narrow_temp(r.windows_sdk_um_library_path))
|
||||
vs_lib_folder := tos_clone(C.wide_string_to_narrow_temp(r.vs_library_path))
|
||||
exe_folder := tos_clone(C.wide_string_to_narrow_temp(r.vs_exe_path))
|
||||
|
||||
mut ucrt_include_folder := ucrt_lib_folder.replace('Lib', 'Include')
|
||||
mut vs_include_folder := vs_lib_folder.replace('lib', 'include')
|
||||
|
||||
if ucrt_include_folder.ends_with('\\x64') {
|
||||
ucrt_include_folder = ucrt_include_folder.left(ucrt_include_folder.len - 4)
|
||||
}
|
||||
if vs_include_folder.ends_with('\\x64') {
|
||||
vs_include_folder = vs_include_folder.left(vs_include_folder.len - 4)
|
||||
}
|
||||
|
||||
um_include_folder := ucrt_include_folder.replace('ucrt', 'um')
|
||||
shared_include_folder := ucrt_include_folder.replace('ucrt', 'shared')
|
||||
|
||||
return &MsvcResult {
|
||||
sdk_ver: r.sdk_ver,
|
||||
windows_sdk_root_path: windows_sdk_root,
|
||||
exe_path: exe_folder,
|
||||
|
||||
um_lib_path: um_lib_folder,
|
||||
ucrt_lib_path: ucrt_lib_folder,
|
||||
vs_lib_path: vs_lib_folder,
|
||||
|
||||
um_include_path: um_include_folder,
|
||||
ucrt_include_path: ucrt_include_folder,
|
||||
vs_include_path: vs_include_folder,
|
||||
shared_include_path: shared_include_folder,
|
||||
}
|
||||
}
|
||||
$else {
|
||||
panic('Cannot find msvc on this platform')
|
||||
}
|
||||
}
|
||||
|
||||
pub fn cc_msvc(v *V) {
|
||||
r := find_msvc()
|
||||
|
||||
mut a := ['-w', '/volatile:ms'] // arguments for the C compiler
|
||||
|
||||
// cl.exe is stupid so these are in a different order to the ones below!
|
||||
|
||||
if v.pref.is_prod {
|
||||
a << '/O2'
|
||||
a << '/MD'
|
||||
} else {
|
||||
a << '/Z7'
|
||||
a << '/MDd'
|
||||
}
|
||||
|
||||
if v.pref.is_so {
|
||||
// Dont think we have to do anything for this
|
||||
if !v.out_name.ends_with('.dll') {
|
||||
v.out_name = v.out_name + '.dll'
|
||||
}
|
||||
|
||||
// Build dll
|
||||
a << '/LD'
|
||||
} else if !v.out_name.ends_with('.exe') {
|
||||
v.out_name = v.out_name + '.exe'
|
||||
}
|
||||
|
||||
mut libs := ''// builtin.o os.o http.o etc
|
||||
if v.pref.build_mode == .build {
|
||||
}
|
||||
else if v.pref.build_mode == .embed_vlib {
|
||||
//
|
||||
}
|
||||
else if v.pref.build_mode == .default_mode {
|
||||
libs = '"$ModPath/vlib/builtin.obj"'
|
||||
if !os.file_exists(libs) {
|
||||
println('`builtin.obj` not found')
|
||||
exit(1)
|
||||
}
|
||||
for imp in v.table.imports {
|
||||
if imp == 'webview' {
|
||||
continue
|
||||
}
|
||||
libs += ' "$ModPath/vlib/${imp}.obj"'
|
||||
}
|
||||
}
|
||||
|
||||
if v.pref.sanitize {
|
||||
println('Sanitize not supported on msvc.')
|
||||
}
|
||||
|
||||
// The C file we are compiling
|
||||
//a << '"$TmpPath/$v.out_name_c"'
|
||||
// this isnt correct for some reason
|
||||
// so fix that now
|
||||
|
||||
a << '".$v.out_name_c"'
|
||||
|
||||
mut other_flags := []string{}
|
||||
mut real_libs := []string{}
|
||||
mut lib_paths := []string{}
|
||||
|
||||
for f in v.table.flags {
|
||||
// We need to see if the flag contains -l
|
||||
// -l isnt recognised and these libs will be passed straight to the linker
|
||||
// by the compiler
|
||||
if f.starts_with('-l') {
|
||||
lib_base := f.right(2).trim_space()
|
||||
|
||||
// MSVC has no method of linking against a .dll
|
||||
// TODO: we should look for .defs aswell
|
||||
lib_lib := lib_base + '.lib'
|
||||
real_libs << lib_lib
|
||||
}
|
||||
else if f.starts_with('-L') {
|
||||
lib_paths << f.right(2).trim_space()
|
||||
}
|
||||
else if f.ends_with('.o') {
|
||||
// msvc expects .obj not .o
|
||||
other_flags << f + 'bj'
|
||||
}
|
||||
else {
|
||||
other_flags << f
|
||||
}
|
||||
}
|
||||
|
||||
default_libs := [
|
||||
'kernel32.lib',
|
||||
'user32.lib',
|
||||
'gdi32.lib',
|
||||
'winspool.lib',
|
||||
'comdlg32.lib',
|
||||
'advapi32.lib',
|
||||
'shell32.lib',
|
||||
'ole32.lib',
|
||||
'oleaut32.lib',
|
||||
'uuid.lib',
|
||||
'odbc32.lib',
|
||||
'odbccp32.lib',
|
||||
'vcruntime.lib',
|
||||
'kernel32.lib',
|
||||
]
|
||||
|
||||
for l in default_libs {
|
||||
real_libs << l
|
||||
}
|
||||
|
||||
|
||||
// flags := v.table.flags.join(' ')
|
||||
|
||||
// Include the base paths
|
||||
a << '-I "$r.ucrt_include_path" -I "$r.vs_include_path" -I "$r.um_include_path" -I "$r.shared_include_path"'
|
||||
|
||||
// Msvc also doesnt have atomic
|
||||
// TODO: dont rely on gcc's _Atomic semantics!
|
||||
a << other_flags
|
||||
|
||||
// TODO: libs will need to be actually handled properly
|
||||
a << real_libs.join(' ')
|
||||
|
||||
a << '/link'
|
||||
a << '/NOLOGO'
|
||||
a << '/OUT:$v.out_name'
|
||||
a << '/LIBPATH:"$r.ucrt_lib_path"'
|
||||
a << '/LIBPATH:"$r.um_lib_path"'
|
||||
a << '/LIBPATH:"$r.vs_lib_path"'
|
||||
a << '/INCREMENTAL:NO' // Disable incremental linking
|
||||
|
||||
for l in lib_paths {
|
||||
a << '/LIBPATH:"$l"'
|
||||
}
|
||||
|
||||
if !v.pref.is_prod {
|
||||
a << '/DEBUG:FULL'
|
||||
}
|
||||
|
||||
args := a.join(' ')
|
||||
|
||||
// println('$args')
|
||||
// println('$exe_path')
|
||||
|
||||
escaped_path := r.exe_path
|
||||
|
||||
cmd := '""$escaped_path\\cl.exe" $args"'
|
||||
|
||||
// println('$cmd')
|
||||
|
||||
res := os.exec(cmd)
|
||||
// println(res)
|
||||
// println('C OUTPUT:')
|
||||
if res.contains('error') {
|
||||
println(res)
|
||||
panic('msvc error')
|
||||
}
|
||||
|
||||
if !v.pref.is_debug && v.out_name_c != 'v.c' && v.out_name_c != 'v_macos.c' {
|
||||
os.rm('.$v.out_name_c')
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
fn build_thirdparty_obj_file_with_msvc(flag string) {
|
||||
msvc := find_msvc()
|
||||
|
||||
mut obj_path := flag.all_after(' ')
|
||||
|
||||
if obj_path.ends_with('.o') {
|
||||
// msvc expects .obj not .o
|
||||
obj_path = obj_path + 'bj'
|
||||
}
|
||||
|
||||
if os.file_exists(obj_path) {
|
||||
return
|
||||
}
|
||||
println('$obj_path not found, building it (with msvc)...')
|
||||
parent := obj_path.all_before_last('/').trim_space()
|
||||
files := os.ls(parent)
|
||||
|
||||
mut cfiles := ''
|
||||
for file in files {
|
||||
if file.ends_with('.c') {
|
||||
cfiles += parent + '/' + file + ' '
|
||||
}
|
||||
}
|
||||
|
||||
include_string := '-I "$msvc.ucrt_include_path" -I "$msvc.vs_include_path" -I "$msvc.um_include_path" -I "$msvc.shared_include_path"'
|
||||
|
||||
println('$cfiles')
|
||||
|
||||
res := os.exec('""$msvc.exe_path\\cl.exe" /volatile:ms /Z7 $include_string /c $cfiles /Fo"$obj_path""')
|
||||
println(res)
|
||||
}
|
@ -540,6 +540,7 @@ fn (p mut Parser) struct_decl() {
|
||||
}
|
||||
println('fmt max len = $max_len nrfields=$typ.fields.len pass=$p.run')
|
||||
*/
|
||||
mut did_gen_something := false
|
||||
for p.tok != .rcbr {
|
||||
if p.tok == .key_pub {
|
||||
if is_pub {
|
||||
@ -604,16 +605,25 @@ fn (p mut Parser) struct_decl() {
|
||||
attr = p.check_name()
|
||||
p.check(.rsbr)
|
||||
}
|
||||
did_gen_something = true
|
||||
|
||||
typ.add_field(field_name, field_type, is_mut, attr, access_mod)
|
||||
p.fgenln('')
|
||||
}
|
||||
|
||||
if !is_ph && p.first_run() {
|
||||
p.table.register_type2(typ)
|
||||
//println('registering 1 nrfields=$typ.fields.len')
|
||||
}
|
||||
|
||||
p.check(.rcbr)
|
||||
if !is_c {
|
||||
p.gen_type('}; ')
|
||||
if p.os == .msvc && !did_gen_something {
|
||||
p.gen_type('void *____dummy_variable; };')
|
||||
p.fgenln('')
|
||||
} else {
|
||||
p.gen_type('}; ')
|
||||
}
|
||||
}
|
||||
if is_objc {
|
||||
p.gen_type('@end')
|
||||
@ -1891,7 +1901,10 @@ fn (p mut Parser) index_expr(typ string, fn_ph int) string {
|
||||
tmp_ok := p.get_tmp()
|
||||
if is_map {
|
||||
p.gen('$tmp')
|
||||
def := type_default(typ)
|
||||
mut def := type_default(typ)
|
||||
if p.os == .msvc && def == '{}' {
|
||||
def = '{0}'
|
||||
}
|
||||
p.cgen.insert_before('$typ $tmp = $def; bool $tmp_ok = map_get($index_expr, & $tmp);')
|
||||
}
|
||||
else if is_arr {
|
||||
@ -2015,6 +2028,11 @@ fn (p mut Parser) expression() string {
|
||||
}
|
||||
// 3 + 4
|
||||
else if is_num {
|
||||
if p.os == .msvc && typ == 'void*' {
|
||||
// Msvc errors on void* pointer arithmatic
|
||||
// ... So cast to byte* and then do the add
|
||||
p.cgen.set_placeholder(ph, '(byte*)')
|
||||
}
|
||||
p.gen(tok_op.str())
|
||||
}
|
||||
// Vec + Vec
|
||||
@ -2455,7 +2473,11 @@ fn (p mut Parser) array_init() string {
|
||||
name := p.check_name()
|
||||
if p.table.known_type(name) {
|
||||
p.cgen.resetln('')
|
||||
p.gen('{}')
|
||||
if p.os == .msvc {
|
||||
p.gen('{0}')
|
||||
} else {
|
||||
p.gen('{}')
|
||||
}
|
||||
return '[$lit]$name'
|
||||
}
|
||||
else {
|
||||
@ -2539,7 +2561,11 @@ fn (p mut Parser) array_init() string {
|
||||
// p.gen('$new_arr($vals.len, $vals.len, sizeof($typ), ($typ[]) $c_arr );')
|
||||
// TODO why need !first_run()?? Otherwise it goes to the very top of the out.c file
|
||||
if !p.first_run() {
|
||||
p.cgen.set_placeholder(new_arr_ph, '$new_arr($i, $i, sizeof($typ), ($typ[]) { ')
|
||||
if p.os == .msvc && i == 0 {
|
||||
p.cgen.set_placeholder(new_arr_ph, '$new_arr($i, $i, sizeof($typ), ($typ[]) {0 ')
|
||||
} else {
|
||||
p.cgen.set_placeholder(new_arr_ph, '$new_arr($i, $i, sizeof($typ), ($typ[]) { ')
|
||||
}
|
||||
}
|
||||
typ = 'array_$typ'
|
||||
p.register_array(typ)
|
||||
@ -2602,6 +2628,7 @@ fn (p mut Parser) struct_init(is_c_struct_init bool) string {
|
||||
no_star := typ.replace('*', '')
|
||||
p.gen('ALLOC_INIT($no_star, {')
|
||||
}
|
||||
mut did_gen_something := false
|
||||
// Loop thru all struct init keys and assign values
|
||||
// u := User{age:20, name:'bob'}
|
||||
// Remember which fields were set, so that we dont have to zero them later
|
||||
@ -2627,6 +2654,7 @@ fn (p mut Parser) struct_init(is_c_struct_init bool) string {
|
||||
p.gen(',')
|
||||
}
|
||||
p.fgenln('')
|
||||
did_gen_something = true
|
||||
}
|
||||
// If we already set some fields, need to prepend a comma
|
||||
if t.fields.len != inited_fields.len && inited_fields.len > 0 {
|
||||
@ -2650,6 +2678,7 @@ fn (p mut Parser) struct_init(is_c_struct_init bool) string {
|
||||
p.gen(',')
|
||||
}
|
||||
}
|
||||
did_gen_something = true
|
||||
}
|
||||
}
|
||||
// Point{3,4} syntax
|
||||
@ -2680,7 +2709,13 @@ fn (p mut Parser) struct_init(is_c_struct_init bool) string {
|
||||
if p.tok != .rcbr {
|
||||
p.error('too many fields initialized: `$typ` has $T.fields.len field(s)')
|
||||
}
|
||||
did_gen_something = true
|
||||
}
|
||||
|
||||
if p.os == .msvc && !did_gen_something {
|
||||
p.gen('0')
|
||||
}
|
||||
|
||||
p.gen('}')
|
||||
if ptr {
|
||||
p.gen(')')
|
||||
@ -2761,6 +2796,7 @@ fn os_name_to_ifdef(name string) string {
|
||||
case 'openbsd': return '__OpenBSD__'
|
||||
case 'netbsd': return '__NetBSD__'
|
||||
case 'dragonfly': return '__DragonFly__'
|
||||
case 'msvc': return '_MSC_VER'
|
||||
}
|
||||
panic('bad os ifdef name "$name"')
|
||||
return ''
|
||||
@ -2852,7 +2888,7 @@ fn (p mut Parser) chash() {
|
||||
else if hash.contains('darwin') && p.os != .mac {
|
||||
return
|
||||
}
|
||||
else if hash.contains('windows') && p.os != .windows {
|
||||
else if hash.contains('windows') && (p.os != .windows && p.os != .msvc) {
|
||||
return
|
||||
}
|
||||
// Remove "linux" etc from flag
|
||||
@ -2867,8 +2903,13 @@ fn (p mut Parser) chash() {
|
||||
}
|
||||
p.log('adding flag "$flag"')
|
||||
// `@VROOT/thirdparty/glad/glad.o`, make sure it exists, otherwise build it
|
||||
if has_vroot && flag.contains('.o') {
|
||||
build_thirdparty_obj_file(flag)
|
||||
if has_vroot && flag.contains('.o') {
|
||||
if p.os == .msvc {
|
||||
build_thirdparty_obj_file_with_msvc(flag)
|
||||
}
|
||||
else {
|
||||
build_thirdparty_obj_file(flag)
|
||||
}
|
||||
}
|
||||
p.table.flags << flag
|
||||
return
|
||||
@ -3072,7 +3113,10 @@ fn (p mut Parser) for_st() {
|
||||
p.genln('for (int l = 0; l < keys_$tmp .len; l++) {')
|
||||
p.genln(' string $i = ((string*)keys_$tmp .data)[l];')
|
||||
//p.genln(' string $i = *(string*) ( array__get(keys_$tmp, l) );')
|
||||
def := type_default(var_typ)
|
||||
mut def := type_default(typ)
|
||||
if def == '{}' {
|
||||
def = '{0}'
|
||||
}
|
||||
// TODO don't call map_get() for each key, fetch values while traversing
|
||||
// the tree (replace `map_keys()` above with `map_key_vals()`)
|
||||
p.genln('$var_typ $val = $def; map_get($tmp, $i, & $val);')
|
||||
@ -3262,8 +3306,15 @@ fn (p mut Parser) return_st() {
|
||||
tmp := p.get_tmp()
|
||||
ret := p.cgen.cur_line.right(ph)
|
||||
|
||||
p.cgen.resetln('$expr_type $tmp = ($expr_type)($ret);')
|
||||
p.cgen(p.cur_fn.defer_text)
|
||||
if p.os != .msvc {
|
||||
p.cgen.cur_line = '$expr_type $tmp = ($expr_type)($ret);'
|
||||
p.cgen.resetln('$expr_type $tmp = ($expr_type)($ret);')
|
||||
} else {
|
||||
// Both the return type and the expression type have already been concluded
|
||||
// to be the same - the cast is slightly pointless
|
||||
// and msvc cant do it
|
||||
p.cgen.resetln('$expr_type $tmp = ($ret);')
|
||||
}
|
||||
p.gen('return opt_ok(&$tmp, sizeof($expr_type))')
|
||||
}
|
||||
else {
|
||||
|
Reference in New Issue
Block a user