Prevent people on platforms that arent 'windows' from being able to build with msvc

pull/1584/head
Emily Hudson 2019-08-12 11:17:26 +00:00 committed by Alexander Medvednikov
parent 15a42f1f35
commit decdd8be9f
3 changed files with 110 additions and 94 deletions

View File

@ -210,6 +210,11 @@ fn main() {
}
fn (v mut V) compile() {
// Emily: Stop people on linux from being able to build with msvc
if os.user_os() != 'windows' && v.os == .msvc {
panic('Cannot build with msvc on ${os.user_os()}')
}
mut cgen := v.cgen
cgen.genln('// Generated by V')
v.add_v_files_to_compile()
@ -754,6 +759,7 @@ fn (v mut V) cc() {
return
}
}
linux_host := os.user_os() == 'linux'
v.log('cc() isprod=$v.pref.is_prod outname=$v.out_name')
mut a := [v.pref.cflags, '-std=gnu11', '-w'] // arguments for the C compiler

View File

@ -30,6 +30,7 @@ const (
// Given a root key look for one of the subkeys in 'versions' and get the path
fn find_windows_kit_internal(key RegKey, versions []string) ?string {
$if windows {
for version in versions {
required_bytes := 0 // TODO mut
result := C.RegQueryValueExW(key, version.to_wide(), 0, 0, 0, &required_bytes)
@ -61,6 +62,7 @@ fn find_windows_kit_internal(key RegKey, versions []string) ?string {
return string_from_wide(value)
}
}
return error('windows kit not found')
}
@ -75,6 +77,7 @@ struct WindowsKit {
// Try and find the root key for installed windows kits
fn find_windows_kit_root() ?WindowsKit {
$if windows {
root_key := RegKey(0)
rc := C.RegOpenKeyExA(
HKEY_LOCAL_MACHINE, 'SOFTWARE\\Microsoft\\Windows Kits\\Installed Roots', 0, KEY_QUERY_VALUE | KEY_WOW64_32KEY | KEY_ENUMERATE_SUB_KEYS, &root_key)
@ -119,6 +122,8 @@ fn find_windows_kit_root() ?WindowsKit {
ucrt_include_path: kit_include_highest + '\\ucrt'
shared_include_path: kit_include_highest + '\\shared'
}
}
return error('Host OS does not support funding a windows kit')
}
struct VsInstallation {
@ -128,6 +133,9 @@ struct VsInstallation {
}
fn find_vs() ?VsInstallation {
$if !windows {
return error('Host OS does not support finding a Vs installation')
}
// Emily:
// VSWhere is guaranteed to be installed at this location now
// If its not there then end user needs to update their visual studio
@ -192,23 +200,28 @@ fn find_msvc() ?MsvcResult {
}
}
$else {
panic('Cannot find msvc on this platform')
panic('Cannot find msvc on this OS')
}
}
pub fn (v mut V) cc_msvc() {
r := find_msvc() or {
println('Could not find MSVC')
// TODO: code reuse
if !v.pref.is_debug && v.out_name_c != 'v.c' && v.out_name_c != 'v_macos.c' {
os.rm('.$v.out_name_c')
}
return
panic('Cannot find MSVC on this OS.')
}
out_name_obj := v.out_name_c + '.obj'
// Default arguments
mut a := ['-w', '/volatile:ms', '/D_UNICODE', '/DUNICODE']
// volatile:ms enables atomic volatile (gcc _Atomic)
// -w: no warnings
// 2 unicode defines
// /Fo sets the object file name - needed so we can clean up after ourselves properly
mut a := ['-w', '/volatile:ms', '/D_UNICODE', '/DUNICODE', '/Fo$out_name_obj']
if v.pref.is_prod {
a << '/O2'
@ -219,7 +232,6 @@ pub fn (v mut V) cc_msvc() {
}
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'
}
@ -258,6 +270,9 @@ pub fn (v mut V) cc_msvc() {
//a << '"$TmpPath/$v.out_name_c"'
a << '".$v.out_name_c"'
// Emily:
// Not all of these are needed (but the compiler should discard them if they are not used)
// these are the defaults used by msbuild and visual studio
mut real_libs := [
'kernel32.lib',
'user32.lib',
@ -287,6 +302,10 @@ pub fn (v mut V) cc_msvc() {
if f.starts_with('-l') {
lib_base := f.right(2).trim_space()
if lib_base.ends_with('.dll') {
panic('MSVC cannot link against a dll (`#flag -l $lib_base`)')
}
// MSVC has no method of linking against a .dll
// TODO: we should look for .defs aswell
lib_lib := lib_base + '.lib'
@ -307,11 +326,9 @@ pub fn (v mut V) cc_msvc() {
// 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
// Libs are passed to cl.exe which passes them to the linker
a << real_libs.join(' ')
a << '/link'
@ -350,6 +367,7 @@ pub fn (v mut V) cc_msvc() {
if !v.pref.is_debug && v.out_name_c != 'v.c' && v.out_name_c != 'v_macos.c' {
os.rm('.$v.out_name_c')
os.rm('$out_name_obj')
}
}

View File

@ -1,8 +0,0 @@
module main
fn (v mut V) cc_msvc() {
}
fn build_thirdparty_obj_file_with_msvc(flag string) {
}