2019-07-24 00:23:13 +03:00
|
|
|
module main
|
|
|
|
|
|
|
|
import os
|
|
|
|
|
2019-07-24 14:49:00 +03:00
|
|
|
#flag windows -l shell32
|
2019-07-24 00:23:13 +03:00
|
|
|
|
|
|
|
struct MsvcResult {
|
2019-09-03 16:09:43 +03:00
|
|
|
full_cl_exe_path string
|
2019-08-12 14:17:26 +03:00
|
|
|
exe_path string
|
2019-07-24 00:23:13 +03:00
|
|
|
|
2019-08-12 14:17:26 +03:00
|
|
|
um_lib_path string
|
|
|
|
ucrt_lib_path string
|
|
|
|
vs_lib_path string
|
2019-07-24 00:23:13 +03:00
|
|
|
|
2019-08-12 14:17:26 +03:00
|
|
|
um_include_path string
|
|
|
|
ucrt_include_path string
|
|
|
|
vs_include_path string
|
|
|
|
shared_include_path string
|
2019-07-24 00:23:13 +03:00
|
|
|
}
|
|
|
|
|
2019-07-25 13:29:24 +03:00
|
|
|
// Mimics a HKEY
|
|
|
|
type RegKey voidptr
|
|
|
|
|
|
|
|
// Taken from the windows SDK
|
|
|
|
const (
|
|
|
|
HKEY_LOCAL_MACHINE = RegKey(0x80000002)
|
|
|
|
KEY_QUERY_VALUE = (0x0001)
|
|
|
|
KEY_WOW64_32KEY = (0x0200)
|
|
|
|
KEY_ENUMERATE_SUB_KEYS = (0x0008)
|
|
|
|
)
|
|
|
|
|
2019-08-10 11:26:42 +03:00
|
|
|
// 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 {
|
2019-08-12 14:17:26 +03:00
|
|
|
$if windows {
|
|
|
|
for version in versions {
|
|
|
|
required_bytes := 0 // TODO mut
|
|
|
|
result := C.RegQueryValueExW(key, version.to_wide(), 0, 0, 0, &required_bytes)
|
2019-07-25 13:29:24 +03:00
|
|
|
|
2019-08-12 14:17:26 +03:00
|
|
|
length := required_bytes / 2
|
2019-07-25 13:29:24 +03:00
|
|
|
|
2019-08-12 14:17:26 +03:00
|
|
|
if result != 0 {
|
|
|
|
continue
|
|
|
|
}
|
2019-07-25 13:29:24 +03:00
|
|
|
|
2019-08-12 14:17:26 +03:00
|
|
|
alloc_length := (required_bytes + 2)
|
2019-07-25 13:29:24 +03:00
|
|
|
|
2019-08-12 14:17:26 +03:00
|
|
|
mut value := &u16(malloc(alloc_length))
|
|
|
|
if !value {
|
|
|
|
continue
|
|
|
|
}
|
2019-07-25 13:29:24 +03:00
|
|
|
|
2019-08-12 14:17:26 +03:00
|
|
|
result2 := C.RegQueryValueExW(key, version.to_wide(), 0, 0, value, &alloc_length)
|
2019-07-25 13:29:24 +03:00
|
|
|
|
2019-08-12 14:17:26 +03:00
|
|
|
if result2 != 0 {
|
|
|
|
continue
|
|
|
|
}
|
2019-07-25 13:29:24 +03:00
|
|
|
|
2019-08-12 14:17:26 +03:00
|
|
|
// We might need to manually null terminate this thing
|
|
|
|
// So just make sure that we do that
|
|
|
|
if (value[length - 1] != u16(0)) {
|
|
|
|
value[length] = u16(0)
|
|
|
|
}
|
2019-07-25 13:29:24 +03:00
|
|
|
|
2019-08-12 14:17:26 +03:00
|
|
|
return string_from_wide(value)
|
|
|
|
}
|
2019-08-10 11:26:42 +03:00
|
|
|
}
|
|
|
|
return error('windows kit not found')
|
2019-07-24 00:23:13 +03:00
|
|
|
}
|
|
|
|
|
2019-07-25 13:29:24 +03:00
|
|
|
struct WindowsKit {
|
|
|
|
um_lib_path string
|
|
|
|
ucrt_lib_path string
|
2019-07-24 00:23:13 +03:00
|
|
|
|
2019-07-25 13:29:24 +03:00
|
|
|
um_include_path string
|
|
|
|
ucrt_include_path string
|
|
|
|
shared_include_path string
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try and find the root key for installed windows kits
|
|
|
|
fn find_windows_kit_root() ?WindowsKit {
|
2019-08-12 14:17:26 +03:00
|
|
|
$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)
|
|
|
|
|
|
|
|
defer {C.RegCloseKey(root_key)}
|
|
|
|
|
|
|
|
if rc != 0 {
|
|
|
|
return error('Unable to open root key')
|
|
|
|
}
|
|
|
|
// Try and find win10 kit
|
|
|
|
kit_root := find_windows_kit_internal(root_key, ['KitsRoot10', 'KitsRoot81']) or {
|
|
|
|
return error('Unable to find a windows kit')
|
|
|
|
}
|
2019-07-25 13:29:24 +03:00
|
|
|
|
2019-08-12 14:17:26 +03:00
|
|
|
kit_lib := kit_root + 'Lib'
|
2019-07-24 00:23:13 +03:00
|
|
|
|
2019-08-12 14:17:26 +03:00
|
|
|
// println(kit_lib)
|
2019-07-24 00:23:13 +03:00
|
|
|
|
2019-08-12 14:17:26 +03:00
|
|
|
files := os.ls(kit_lib)
|
|
|
|
mut highest_path := ''
|
|
|
|
mut highest_int := 0
|
|
|
|
for f in files {
|
|
|
|
no_dot := f.replace('.', '')
|
|
|
|
v_int := no_dot.int()
|
2019-07-24 00:23:13 +03:00
|
|
|
|
2019-08-12 14:17:26 +03:00
|
|
|
if v_int > highest_int {
|
|
|
|
highest_int = v_int
|
|
|
|
highest_path = f
|
|
|
|
}
|
2019-07-24 00:23:13 +03:00
|
|
|
}
|
2019-07-25 13:29:24 +03:00
|
|
|
|
2019-08-12 14:17:26 +03:00
|
|
|
kit_lib_highest := kit_lib + '\\$highest_path'
|
|
|
|
kit_include_highest := kit_lib_highest.replace('Lib', 'Include')
|
2019-07-25 13:29:24 +03:00
|
|
|
|
2019-08-12 14:17:26 +03:00
|
|
|
// println('$kit_lib_highest $kit_include_highest')
|
2019-07-25 13:29:24 +03:00
|
|
|
|
2019-08-12 14:17:26 +03:00
|
|
|
return WindowsKit {
|
|
|
|
um_lib_path: kit_lib_highest + '\\um\\x64'
|
|
|
|
ucrt_lib_path: kit_lib_highest + '\\ucrt\\x64'
|
2019-07-25 13:29:24 +03:00
|
|
|
|
2019-08-12 14:17:26 +03:00
|
|
|
um_include_path: kit_include_highest + '\\um'
|
|
|
|
ucrt_include_path: kit_include_highest + '\\ucrt'
|
|
|
|
shared_include_path: kit_include_highest + '\\shared'
|
|
|
|
}
|
2019-07-25 13:29:24 +03:00
|
|
|
}
|
2019-08-12 14:17:26 +03:00
|
|
|
return error('Host OS does not support funding a windows kit')
|
2019-07-25 13:29:24 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
struct VsInstallation {
|
|
|
|
include_path string
|
|
|
|
lib_path string
|
|
|
|
exe_path string
|
|
|
|
}
|
|
|
|
|
|
|
|
fn find_vs() ?VsInstallation {
|
2019-08-12 14:17:26 +03:00
|
|
|
$if !windows {
|
|
|
|
return error('Host OS does not support finding a Vs installation')
|
|
|
|
}
|
2019-07-25 13:29:24 +03:00
|
|
|
// Emily:
|
|
|
|
// VSWhere is guaranteed to be installed at this location now
|
|
|
|
// If its not there then end user needs to update their visual studio
|
|
|
|
// installation!
|
2019-08-08 08:30:05 +03:00
|
|
|
res := os.exec('""%ProgramFiles(x86)%\\Microsoft Visual Studio\\Installer\\vswhere.exe" -latest -products * -requires Microsoft.VisualStudio.Component.VC.Tools.x86.x64 -property installationPath"') or {
|
2019-08-10 11:26:42 +03:00
|
|
|
return error(err)
|
2019-08-08 08:30:05 +03:00
|
|
|
}
|
2019-07-25 13:29:24 +03:00
|
|
|
// println('res: "$res"')
|
|
|
|
|
2019-08-17 20:21:59 +03:00
|
|
|
version := os.read_file('$res.output\\VC\\Auxiliary\\Build\\Microsoft.VCToolsVersion.default.txt') or {
|
2019-07-25 13:29:24 +03:00
|
|
|
println('Unable to find msvc version')
|
|
|
|
return error('Unable to find vs installation')
|
|
|
|
}
|
|
|
|
|
|
|
|
// println('version: $version')
|
|
|
|
|
|
|
|
v := if version.ends_with('\n') {
|
|
|
|
version.left(version.len - 2)
|
|
|
|
} else {
|
|
|
|
version
|
|
|
|
}
|
|
|
|
|
2019-08-17 20:21:59 +03:00
|
|
|
lib_path := '$res.output\\VC\\Tools\\MSVC\\$v\\lib\\x64'
|
|
|
|
include_path := '$res.output\\VC\\Tools\\MSVC\\$v\\include'
|
2019-07-25 13:29:24 +03:00
|
|
|
|
|
|
|
if os.file_exists('$lib_path\\vcruntime.lib') {
|
2019-08-17 20:21:59 +03:00
|
|
|
p := '$res.output\\VC\\Tools\\MSVC\\$v\\bin\\Hostx64\\x64'
|
2019-07-25 13:29:24 +03:00
|
|
|
|
|
|
|
// println('$lib_path $include_path')
|
|
|
|
|
|
|
|
return VsInstallation{
|
|
|
|
exe_path: p
|
|
|
|
lib_path: lib_path
|
|
|
|
include_path: include_path
|
2019-07-24 00:23:13 +03:00
|
|
|
}
|
2019-07-25 13:29:24 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
println('Unable to find vs installation (attempted to use lib path "$lib_path")')
|
|
|
|
return error('Unable to find vs exe folder')
|
|
|
|
}
|
2019-07-24 00:23:13 +03:00
|
|
|
|
2019-07-25 13:29:24 +03:00
|
|
|
fn find_msvc() ?MsvcResult {
|
|
|
|
$if windows {
|
|
|
|
wk := find_windows_kit_root() or {
|
|
|
|
return error('Unable to find windows sdk')
|
|
|
|
}
|
|
|
|
vs := find_vs() or {
|
|
|
|
return error('Unable to find visual studio')
|
|
|
|
}
|
2019-07-24 00:23:13 +03:00
|
|
|
|
2019-07-25 13:29:24 +03:00
|
|
|
return MsvcResult {
|
2019-09-03 16:09:43 +03:00
|
|
|
full_cl_exe_path: os.realpath( vs.exe_path + os.PathSeparator + 'cl.exe' )
|
2019-07-25 13:29:24 +03:00
|
|
|
exe_path: vs.exe_path,
|
2019-07-24 00:23:13 +03:00
|
|
|
|
2019-07-25 13:29:24 +03:00
|
|
|
um_lib_path: wk.um_lib_path,
|
|
|
|
ucrt_lib_path: wk.ucrt_lib_path,
|
|
|
|
vs_lib_path: vs.lib_path,
|
2019-07-24 00:23:13 +03:00
|
|
|
|
2019-07-25 13:29:24 +03:00
|
|
|
um_include_path: wk.um_include_path,
|
|
|
|
ucrt_include_path: wk.ucrt_include_path,
|
|
|
|
vs_include_path: vs.include_path,
|
|
|
|
shared_include_path: wk.shared_include_path,
|
2019-07-24 00:23:13 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
$else {
|
2019-08-29 03:30:17 +03:00
|
|
|
cerror('Cannot find msvc on this OS')
|
|
|
|
return error('msvc not found')
|
2019-07-24 00:23:13 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-20 16:32:15 +03:00
|
|
|
struct ParsedFlag {
|
|
|
|
f string
|
|
|
|
arg string
|
|
|
|
}
|
|
|
|
|
2019-08-07 17:26:08 +03:00
|
|
|
pub fn (v mut V) cc_msvc() {
|
2019-07-25 13:29:24 +03:00
|
|
|
r := find_msvc() or {
|
|
|
|
// TODO: code reuse
|
|
|
|
if !v.pref.is_debug && v.out_name_c != 'v.c' && v.out_name_c != 'v_macos.c' {
|
2019-09-03 16:09:43 +03:00
|
|
|
os.rm(v.out_name_c)
|
2019-07-25 13:29:24 +03:00
|
|
|
}
|
2019-08-29 03:30:17 +03:00
|
|
|
cerror('Cannot find MSVC on this OS.')
|
|
|
|
return
|
2019-07-25 13:29:24 +03:00
|
|
|
}
|
2019-07-24 00:23:13 +03:00
|
|
|
|
2019-09-03 16:09:43 +03:00
|
|
|
out_name_obj := os.realpath( v.out_name_c + '.obj' )
|
2019-08-12 14:17:26 +03:00
|
|
|
|
2019-07-25 13:29:24 +03:00
|
|
|
// Default arguments
|
2019-08-12 14:17:26 +03:00
|
|
|
|
|
|
|
// 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
|
2019-09-03 16:09:43 +03:00
|
|
|
mut a := ['-w', '/we4013', '/volatile:ms', '/D_UNICODE', '/DUNICODE', '/Fo"$out_name_obj"']
|
2019-07-24 00:23:13 +03:00
|
|
|
|
|
|
|
if v.pref.is_prod {
|
|
|
|
a << '/O2'
|
|
|
|
a << '/MD'
|
|
|
|
} else {
|
|
|
|
a << '/Z7'
|
|
|
|
a << '/MDd'
|
|
|
|
}
|
|
|
|
|
|
|
|
if v.pref.is_so {
|
|
|
|
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'
|
|
|
|
}
|
|
|
|
|
2019-09-03 16:09:43 +03:00
|
|
|
v.out_name = os.realpath( v.out_name )
|
|
|
|
|
|
|
|
mut alibs := []string // builtin.o os.o http.o etc
|
2019-07-24 00:23:13 +03:00
|
|
|
if v.pref.build_mode == .build {
|
|
|
|
}
|
|
|
|
else if v.pref.build_mode == .embed_vlib {
|
|
|
|
//
|
|
|
|
}
|
|
|
|
else if v.pref.build_mode == .default_mode {
|
2019-09-03 16:09:43 +03:00
|
|
|
b := os.realpath( '$ModPath/vlib/builtin.obj' )
|
|
|
|
alibs << '"$b"'
|
|
|
|
if !os.file_exists(b) {
|
2019-07-24 00:23:13 +03:00
|
|
|
println('`builtin.obj` not found')
|
|
|
|
exit(1)
|
|
|
|
}
|
|
|
|
for imp in v.table.imports {
|
|
|
|
if imp == 'webview' {
|
|
|
|
continue
|
|
|
|
}
|
2019-09-03 16:09:43 +03:00
|
|
|
alibs << '"' + os.realpath( '$ModPath/vlib/${imp}.obj' ) + '"'
|
2019-07-24 00:23:13 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if v.pref.sanitize {
|
|
|
|
println('Sanitize not supported on msvc.')
|
|
|
|
}
|
|
|
|
|
|
|
|
// The C file we are compiling
|
|
|
|
//a << '"$TmpPath/$v.out_name_c"'
|
2019-09-03 16:09:43 +03:00
|
|
|
a << '"' + os.realpath( v.out_name_c ) + '"'
|
2019-07-24 00:23:13 +03:00
|
|
|
|
2019-08-12 14:17:26 +03:00
|
|
|
// 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
|
2019-07-25 13:29:24 +03:00
|
|
|
mut real_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',
|
|
|
|
]
|
|
|
|
|
2019-09-03 16:09:43 +03:00
|
|
|
mut inc_paths := []string{}
|
2019-07-24 00:23:13 +03:00
|
|
|
mut lib_paths := []string{}
|
2019-07-25 13:29:24 +03:00
|
|
|
mut other_flags := []string{}
|
2019-07-24 00:23:13 +03:00
|
|
|
|
2019-07-25 13:29:24 +03:00
|
|
|
// Emily:
|
|
|
|
// this is a hack to try and support -l -L and object files
|
|
|
|
// passed on the command line
|
2019-09-03 16:09:43 +03:00
|
|
|
mut seenflags := map[string]int // no need to add the same flags more than once
|
2019-07-24 00:23:13 +03:00
|
|
|
for f in v.table.flags {
|
2019-09-03 16:09:43 +03:00
|
|
|
seenflags[ f ] = seenflags[ f ] + 1
|
|
|
|
if seenflags[ f ] > 1 { continue }
|
2019-08-20 16:32:15 +03:00
|
|
|
// People like to put multiple flags per line (which really complicates things)
|
|
|
|
// ...so we need to handle that
|
|
|
|
mut rest := f
|
|
|
|
|
|
|
|
mut flags := []ParsedFlag{}
|
|
|
|
for {
|
|
|
|
mut base := rest
|
|
|
|
|
|
|
|
fl := if rest.starts_with('-') {
|
|
|
|
base = rest.right(2).trim_space()
|
|
|
|
rest.left(2)
|
|
|
|
} else {
|
|
|
|
''
|
2019-08-12 14:17:26 +03:00
|
|
|
}
|
|
|
|
|
2019-08-20 16:32:15 +03:00
|
|
|
// Which ever one of these is lowest we use
|
|
|
|
// TODO: we really shouldnt support all of these cmon
|
|
|
|
mut lowest := base.index('-')
|
2019-09-05 02:57:29 +03:00
|
|
|
// dont break paths with hyphens
|
|
|
|
if lowest != 0 {
|
|
|
|
lowest = -1
|
|
|
|
}
|
2019-08-20 16:32:15 +03:00
|
|
|
for x in [base.index(' '), base.index(',')] {
|
|
|
|
if (x < lowest && x != -1) || lowest == -1 {
|
|
|
|
lowest = x
|
|
|
|
}
|
|
|
|
}
|
|
|
|
arg := if lowest != -1 {
|
2019-08-27 07:53:56 +03:00
|
|
|
rest = base.right(lowest).trim_space().trim(',')
|
|
|
|
base.left(lowest).trim_space().trim(',')
|
2019-08-20 16:32:15 +03:00
|
|
|
} else {
|
|
|
|
rest = ''
|
|
|
|
base.trim_space()
|
|
|
|
}
|
|
|
|
|
|
|
|
flags << ParsedFlag {
|
|
|
|
fl, arg
|
|
|
|
}
|
|
|
|
|
|
|
|
if rest.len == 0 {
|
|
|
|
break
|
|
|
|
}
|
2019-07-24 00:23:13 +03:00
|
|
|
}
|
2019-08-20 16:32:15 +03:00
|
|
|
|
|
|
|
for flag in flags {
|
|
|
|
fl := flag.f
|
2019-09-03 16:09:43 +03:00
|
|
|
mut arg := flag.arg
|
|
|
|
if fl == '-I' || fl == '-L' {
|
|
|
|
arg = os.realpath( arg )
|
|
|
|
}
|
|
|
|
//println('fl: $fl | flag arg: $arg')
|
|
|
|
|
2019-08-20 16:32:15 +03:00
|
|
|
// 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 fl == '-l' {
|
|
|
|
if arg.ends_with('.dll') {
|
2019-08-29 03:30:17 +03:00
|
|
|
cerror('MSVC cannot link against a dll (`#flag -l $arg`)')
|
2019-08-20 16:32:15 +03:00
|
|
|
}
|
|
|
|
// MSVC has no method of linking against a .dll
|
|
|
|
// TODO: we should look for .defs aswell
|
|
|
|
lib_lib := arg + '.lib'
|
|
|
|
real_libs << lib_lib
|
2019-09-03 16:09:43 +03:00
|
|
|
}
|
|
|
|
else if fl == '-I' {
|
|
|
|
inc_paths << ' -I "$arg" '
|
|
|
|
}
|
2019-08-20 16:32:15 +03:00
|
|
|
else if fl == '-L' {
|
2019-09-03 16:09:43 +03:00
|
|
|
lpath := f.right(2).trim_space()
|
|
|
|
lib_paths << lpath
|
|
|
|
lib_paths << lpath + os.PathSeparator + 'msvc'
|
|
|
|
// The above allows putting msvc specific .lib files in a subfolder msvc/ ,
|
|
|
|
// where gcc will NOT find them, but cl will do...
|
|
|
|
// NB: gcc is smart enough to not need .lib files at all in most cases, the .dll is enough.
|
|
|
|
// When both a msvc .lib file and .dll file are present in the same folder,
|
|
|
|
// as for example for glfw3, compilation with gcc would fail.
|
2019-08-20 16:32:15 +03:00
|
|
|
}
|
|
|
|
else if arg.ends_with('.o') {
|
|
|
|
// msvc expects .obj not .o
|
|
|
|
other_flags << arg + 'bj'
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
other_flags << arg
|
|
|
|
}
|
2019-07-24 00:23:13 +03:00
|
|
|
}
|
2019-08-20 16:32:15 +03:00
|
|
|
|
2019-07-24 00:23:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Include the base paths
|
2019-09-03 16:09:43 +03:00
|
|
|
a << ' -I "$r.ucrt_include_path" '
|
|
|
|
a << ' -I "$r.vs_include_path" '
|
|
|
|
a << ' -I "$r.um_include_path" '
|
|
|
|
a << ' -I "$r.shared_include_path" '
|
|
|
|
|
|
|
|
a << inc_paths
|
2019-07-24 00:23:13 +03:00
|
|
|
|
|
|
|
a << other_flags
|
|
|
|
|
2019-08-12 14:17:26 +03:00
|
|
|
// Libs are passed to cl.exe which passes them to the linker
|
2019-07-24 00:23:13 +03:00
|
|
|
a << real_libs.join(' ')
|
|
|
|
|
|
|
|
a << '/link'
|
|
|
|
a << '/NOLOGO'
|
2019-09-03 16:09:43 +03:00
|
|
|
a << '/OUT:"$v.out_name"'
|
2019-07-24 00:23:13 +03:00
|
|
|
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 {
|
2019-09-03 16:09:43 +03:00
|
|
|
a << '/LIBPATH:"' + os.realpath(l) + '"'
|
2019-07-24 00:23:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if !v.pref.is_prod {
|
|
|
|
a << '/DEBUG:FULL'
|
2019-08-18 17:25:33 +03:00
|
|
|
} else {
|
|
|
|
a << '/DEBUG:NONE'
|
2019-07-24 00:23:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
args := a.join(' ')
|
|
|
|
|
2019-09-03 16:09:43 +03:00
|
|
|
cmd := '""$r.full_cl_exe_path" $args"'
|
|
|
|
// It is hard to see it at first, but the quotes above ARE balanced :-| ...
|
|
|
|
// Also the double quotes at the start ARE needed.
|
2019-08-31 00:59:21 +03:00
|
|
|
if v.pref.show_c_cmd || v.pref.is_verbose {
|
2019-09-03 16:09:43 +03:00
|
|
|
println('\n========== cl cmd line:')
|
2019-08-31 00:59:21 +03:00
|
|
|
println(cmd)
|
2019-09-03 16:09:43 +03:00
|
|
|
println('==========\n')
|
2019-08-31 00:59:21 +03:00
|
|
|
}
|
|
|
|
|
2019-07-24 00:23:13 +03:00
|
|
|
// println('$cmd')
|
|
|
|
|
2019-08-28 13:06:25 +03:00
|
|
|
res := os.exec(cmd) or {
|
2019-08-08 08:30:05 +03:00
|
|
|
println(err)
|
2019-08-29 03:30:17 +03:00
|
|
|
cerror('msvc error')
|
|
|
|
return
|
2019-07-24 00:23:13 +03:00
|
|
|
}
|
2019-08-28 13:06:25 +03:00
|
|
|
if res.exit_code != 0 {
|
2019-08-29 03:30:17 +03:00
|
|
|
cerror(res.output)
|
2019-08-28 13:06:25 +03:00
|
|
|
}
|
2019-08-08 08:30:05 +03:00
|
|
|
// println(res)
|
|
|
|
// println('C OUTPUT:')
|
2019-07-24 00:23:13 +03:00
|
|
|
|
|
|
|
if !v.pref.is_debug && v.out_name_c != 'v.c' && v.out_name_c != 'v_macos.c' {
|
2019-09-03 16:09:43 +03:00
|
|
|
os.rm(v.out_name_c)
|
2019-08-12 14:17:26 +03:00
|
|
|
}
|
2019-07-24 00:23:13 +03:00
|
|
|
|
2019-08-20 16:32:15 +03:00
|
|
|
// Always remove the object file - it is completely unnecessary
|
2019-09-03 16:09:43 +03:00
|
|
|
os.rm(out_name_obj)
|
2019-07-24 00:23:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fn build_thirdparty_obj_file_with_msvc(flag string) {
|
2019-07-25 13:29:24 +03:00
|
|
|
msvc := find_msvc() or {
|
|
|
|
println('Could not find visual studio')
|
|
|
|
return
|
|
|
|
}
|
2019-07-24 00:23:13 +03:00
|
|
|
|
|
|
|
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) {
|
2019-09-03 16:09:43 +03:00
|
|
|
println('$obj_path already build.')
|
2019-07-24 00:23:13 +03:00
|
|
|
return
|
|
|
|
}
|
2019-09-03 16:09:43 +03:00
|
|
|
|
2019-07-24 00:23:13 +03:00
|
|
|
println('$obj_path not found, building it (with msvc)...')
|
2019-09-03 16:09:43 +03:00
|
|
|
parent := os.dir( obj_path )
|
2019-07-24 00:23:13 +03:00
|
|
|
files := os.ls(parent)
|
|
|
|
|
|
|
|
mut cfiles := ''
|
|
|
|
for file in files {
|
|
|
|
if file.ends_with('.c') {
|
2019-09-03 16:09:43 +03:00
|
|
|
cfiles += '"' + os.realpath( parent + os.PathSeparator + file ) + '" '
|
2019-07-24 00:23:13 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
include_string := '-I "$msvc.ucrt_include_path" -I "$msvc.vs_include_path" -I "$msvc.um_include_path" -I "$msvc.shared_include_path"'
|
|
|
|
|
2019-09-03 16:09:43 +03:00
|
|
|
//println('cfiles: $cfiles')
|
2019-07-24 00:23:13 +03:00
|
|
|
|
2019-09-03 16:09:43 +03:00
|
|
|
cmd := '""$msvc.full_cl_exe_path" /volatile:ms /Z7 $include_string /c $cfiles /Fo"$obj_path" /D_UNICODE /DUNICODE"'
|
|
|
|
//NB: the quotes above ARE balanced.
|
|
|
|
println('thirdparty cmd line: $cmd')
|
|
|
|
res := os.exec(cmd) or {
|
2019-08-29 03:30:17 +03:00
|
|
|
cerror(err)
|
|
|
|
return
|
2019-08-08 08:30:05 +03:00
|
|
|
}
|
2019-08-17 20:21:59 +03:00
|
|
|
println(res.output)
|
2019-08-12 14:17:26 +03:00
|
|
|
}
|
2019-07-25 16:05:44 +03:00
|
|
|
|