2020-02-03 07:00:36 +03:00
|
|
|
// Copyright (c) 2019-2020 Alexander Medvednikov. All rights reserved.
|
2019-09-06 15:12:04 +03:00
|
|
|
// Use of this source code is governed by an MIT license
|
|
|
|
// that can be found in the LICENSE file.
|
2019-10-13 16:37:43 +03:00
|
|
|
module compiler
|
2019-09-06 15:12:04 +03:00
|
|
|
|
|
|
|
import os
|
2019-09-06 15:43:03 +03:00
|
|
|
// parsed cflag
|
2019-12-20 00:29:37 +03:00
|
|
|
struct CFlag {
|
2019-09-23 00:51:59 +03:00
|
|
|
mod string // the module in which the flag was given
|
2019-09-06 15:12:04 +03:00
|
|
|
os string // eg. windows | darwin | linux
|
|
|
|
name string // eg. -I
|
2019-09-06 15:43:03 +03:00
|
|
|
value string // eg. /path/to/include
|
2019-09-06 15:12:04 +03:00
|
|
|
}
|
|
|
|
|
2019-12-11 03:24:26 +03:00
|
|
|
pub fn (c &CFlag) str() string {
|
2019-09-23 00:51:59 +03:00
|
|
|
return 'CFlag{ name: "$c.name" value: "$c.value" mod: "$c.mod" os: "$c.os" }'
|
|
|
|
}
|
|
|
|
|
2019-09-06 15:12:04 +03:00
|
|
|
// get flags for current os
|
2019-09-23 00:51:59 +03:00
|
|
|
fn (v &V) get_os_cflags() []CFlag {
|
2019-09-06 15:12:04 +03:00
|
|
|
mut flags := []CFlag
|
2019-12-23 13:09:00 +03:00
|
|
|
mut ctimedefines := []string
|
2020-02-09 12:08:04 +03:00
|
|
|
if v.pref.compile_defines.len > 0 {
|
|
|
|
ctimedefines << v.pref.compile_defines
|
2019-12-23 13:09:00 +03:00
|
|
|
}
|
|
|
|
|
2019-09-06 15:12:04 +03:00
|
|
|
for flag in v.table.cflags {
|
2020-02-09 12:08:04 +03:00
|
|
|
if flag.os == '' || (flag.os == 'linux' && v.pref.os == .linux) || (flag.os == 'darwin' && v.pref.os == .mac) || (flag.os == 'freebsd' && v.pref.os == .freebsd) || (flag.os == 'windows' && v.pref.os == .windows) {
|
2019-09-06 15:12:04 +03:00
|
|
|
flags << flag
|
|
|
|
}
|
2019-12-23 13:09:00 +03:00
|
|
|
if flag.os in ctimedefines {
|
|
|
|
flags << flag
|
|
|
|
}
|
2019-09-06 15:12:04 +03:00
|
|
|
}
|
|
|
|
return flags
|
|
|
|
}
|
|
|
|
|
2019-09-23 00:51:59 +03:00
|
|
|
fn (v &V) get_rest_of_module_cflags(c &CFlag) []CFlag {
|
|
|
|
mut flags := []CFlag
|
|
|
|
cflags := v.get_os_cflags()
|
|
|
|
for flag in cflags {
|
|
|
|
if c.mod == flag.mod {
|
2019-12-20 00:29:37 +03:00
|
|
|
if c.name == flag.name && c.value == flag.value && c.os == flag.os {
|
|
|
|
continue
|
|
|
|
}
|
2019-09-23 00:51:59 +03:00
|
|
|
flags << flag
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return flags
|
|
|
|
}
|
|
|
|
|
2019-09-06 15:12:04 +03:00
|
|
|
// format flag
|
|
|
|
fn (cf &CFlag) format() string {
|
|
|
|
mut value := cf.value
|
2019-12-20 00:29:37 +03:00
|
|
|
if cf.name in ['-l', '-Wa', '-Wl', '-Wp'] && value.len > 0 {
|
2019-09-16 00:41:12 +03:00
|
|
|
return '${cf.name}${value}'.trim_space()
|
|
|
|
}
|
2019-09-06 15:12:04 +03:00
|
|
|
// convert to absolute path
|
|
|
|
if cf.name == '-I' || cf.name == '-L' || value.ends_with('.o') {
|
2019-12-20 00:29:37 +03:00
|
|
|
value = '"' + os.realpath(value) + '"'
|
2019-09-06 15:12:04 +03:00
|
|
|
}
|
|
|
|
return '$cf.name $value'.trim_space()
|
|
|
|
}
|
|
|
|
|
2019-09-06 15:43:03 +03:00
|
|
|
// check if cflag is in table
|
|
|
|
fn (table &Table) has_cflag(cflag CFlag) bool {
|
|
|
|
for cf in table.cflags {
|
|
|
|
if cf.os == cflag.os && cf.name == cflag.name && cf.value == cflag.value {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// parse the flags to (table.cflags) []CFlag
|
2019-09-06 15:12:04 +03:00
|
|
|
// Note: clean up big time (joe-c)
|
2019-12-23 13:09:00 +03:00
|
|
|
fn (table mut Table) parse_cflag(cflag string, mod string, ctimedefines []string) ?bool {
|
2019-12-20 00:29:37 +03:00
|
|
|
allowed_flags := ['framework', 'library', 'Wa', 'Wl', 'Wp', 'I', 'l', 'L', ]
|
2019-10-19 00:18:08 +03:00
|
|
|
flag_orig := cflag.trim_space()
|
|
|
|
mut flag := flag_orig
|
2019-09-06 15:12:04 +03:00
|
|
|
if flag == '' {
|
2019-10-19 00:18:08 +03:00
|
|
|
return true
|
2019-09-06 15:12:04 +03:00
|
|
|
}
|
|
|
|
mut fos := ''
|
2019-12-23 13:09:00 +03:00
|
|
|
mut allowed_os_overrides := ['linux','darwin','freebsd','windows']
|
|
|
|
allowed_os_overrides << ctimedefines
|
|
|
|
for os_override in allowed_os_overrides {
|
|
|
|
if !flag.starts_with( os_override ) { continue }
|
2019-12-20 00:29:37 +03:00
|
|
|
pos := flag.index(' ') or {
|
|
|
|
return none
|
|
|
|
}
|
2019-10-27 10:03:15 +03:00
|
|
|
fos = flag[..pos].trim_space()
|
|
|
|
flag = flag[pos..].trim_space()
|
2019-09-06 15:12:04 +03:00
|
|
|
}
|
|
|
|
for {
|
2019-12-15 06:18:14 +03:00
|
|
|
mut name := ''
|
2019-09-06 15:12:04 +03:00
|
|
|
mut value := ''
|
|
|
|
if flag[0] == `-` {
|
|
|
|
for f in allowed_flags {
|
2019-12-20 00:29:37 +03:00
|
|
|
i := 1 + f.len
|
2019-10-27 10:03:15 +03:00
|
|
|
if i <= flag.len && f == flag[1..i] {
|
|
|
|
name = flag[..i].trim_space()
|
|
|
|
flag = flag[i..].trim_space()
|
2019-09-06 15:12:04 +03:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-12-20 00:29:37 +03:00
|
|
|
mut index := flag.index(' -') or {
|
|
|
|
-1
|
|
|
|
}
|
2019-12-15 06:18:14 +03:00
|
|
|
for index > -1 {
|
|
|
|
mut has_next := false
|
2019-09-06 15:12:04 +03:00
|
|
|
for f in allowed_flags {
|
2019-12-20 00:29:37 +03:00
|
|
|
i := index + 2 + f.len
|
|
|
|
if i <= flag.len && f == flag[index + 2..i] {
|
|
|
|
value = flag[..index + 1].trim_space()
|
|
|
|
flag = flag[index + 1..].trim_space()
|
2019-12-15 06:18:14 +03:00
|
|
|
has_next = true
|
2019-09-06 15:12:04 +03:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2019-12-20 00:29:37 +03:00
|
|
|
if has_next {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
index = flag.index_after(' -', index + 1)
|
2019-09-07 19:19:17 +03:00
|
|
|
}
|
2019-12-15 06:18:14 +03:00
|
|
|
if index == -1 {
|
2019-09-06 15:12:04 +03:00
|
|
|
value = flag.trim_space()
|
|
|
|
}
|
2019-10-19 00:18:08 +03:00
|
|
|
if (name in ['-I', '-l', '-L']) && value == '' {
|
2019-10-19 03:38:02 +03:00
|
|
|
hint := if name == '-l' { 'library name' } else { 'path' }
|
|
|
|
return error('bad #flag `$flag_orig`: missing $hint after `$name`')
|
|
|
|
}
|
2019-09-06 15:12:04 +03:00
|
|
|
cf := CFlag{
|
2019-12-20 00:29:37 +03:00
|
|
|
mod: mod
|
|
|
|
os: fos
|
|
|
|
name: name
|
2019-09-06 15:12:04 +03:00
|
|
|
value: value
|
|
|
|
}
|
|
|
|
if !table.has_cflag(cf) {
|
|
|
|
table.cflags << cf
|
|
|
|
}
|
|
|
|
if index == -1 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2019-10-19 00:18:08 +03:00
|
|
|
return true
|
2019-09-06 15:12:04 +03:00
|
|
|
}
|
2019-09-23 00:51:59 +03:00
|
|
|
|
2019-12-20 00:29:37 +03:00
|
|
|
// TODO: implement msvc specific c_options_before_target and c_options_after_target ...
|
|
|
|
fn (cflags []CFlag) c_options_before_target_msvc() string {
|
|
|
|
return ''
|
|
|
|
}
|
|
|
|
|
|
|
|
fn (cflags []CFlag) c_options_after_target_msvc() string {
|
|
|
|
return ''
|
|
|
|
}
|
2019-10-14 08:41:46 +03:00
|
|
|
|
2019-11-30 13:09:05 +03:00
|
|
|
fn (cflags []CFlag) c_options_before_target() string {
|
2019-09-23 00:51:59 +03:00
|
|
|
// -I flags, optimization flags and so on
|
2019-12-20 00:29:37 +03:00
|
|
|
mut args := []string
|
2019-09-23 00:51:59 +03:00
|
|
|
for flag in cflags {
|
|
|
|
if flag.name != '-l' {
|
|
|
|
args << flag.format()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return args.join(' ')
|
|
|
|
}
|
|
|
|
|
|
|
|
fn (cflags []CFlag) c_options_after_target() string {
|
|
|
|
// -l flags (libs)
|
2019-12-20 00:29:37 +03:00
|
|
|
mut args := []string
|
2019-09-23 00:51:59 +03:00
|
|
|
for flag in cflags {
|
|
|
|
if flag.name == '-l' {
|
|
|
|
args << flag.format()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return args.join(' ')
|
|
|
|
}
|
|
|
|
|
|
|
|
fn (cflags []CFlag) c_options_without_object_files() string {
|
2019-12-20 00:29:37 +03:00
|
|
|
mut args := []string
|
2019-09-23 00:51:59 +03:00
|
|
|
for flag in cflags {
|
|
|
|
if flag.value.ends_with('.o') || flag.value.ends_with('.obj') {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
args << flag.format()
|
|
|
|
}
|
|
|
|
return args.join(' ')
|
|
|
|
}
|
|
|
|
|
|
|
|
fn (cflags []CFlag) c_options_only_object_files() string {
|
2019-12-20 00:29:37 +03:00
|
|
|
mut args := []string
|
2019-09-23 00:51:59 +03:00
|
|
|
for flag in cflags {
|
2019-11-30 13:09:05 +03:00
|
|
|
if flag.value.ends_with('.o') || flag.value.ends_with('.obj') {
|
2019-09-23 00:51:59 +03:00
|
|
|
args << flag.format()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return args.join(' ')
|
|
|
|
}
|
2019-12-20 00:29:37 +03:00
|
|
|
|