From 20a22453cf6ba66f99197df571566214082110a9 Mon Sep 17 00:00:00 2001 From: Delyan Angelov Date: Fri, 14 May 2021 14:28:53 +0300 Subject: [PATCH] v.builder: remove default link flags -lm, -ldl, -lpthread (use module specific link flags instead) (#10099) --- vlib/dl/dl_nix.c.v | 4 ++++ vlib/fontstash/fontstash.v | 9 +++++++++ vlib/math/math.c.v | 4 ++++ vlib/sokol/c/declaration.c.v | 2 ++ vlib/sync/threads/threads.c.v | 13 +++++++++++++ vlib/sync/threads/threads.v | 4 ++++ vlib/v/builder/cc.v | 9 --------- vlib/v/parser/pratt.v | 1 + 8 files changed, 37 insertions(+), 9 deletions(-) create mode 100644 vlib/sync/threads/threads.c.v create mode 100644 vlib/sync/threads/threads.v diff --git a/vlib/dl/dl_nix.c.v b/vlib/dl/dl_nix.c.v index 2a43bffd9d..40f63b542c 100644 --- a/vlib/dl/dl_nix.c.v +++ b/vlib/dl/dl_nix.c.v @@ -2,6 +2,10 @@ module dl #include +$if linux { + #flag -ldl +} + pub const ( rtld_now = C.RTLD_NOW rtld_lazy = C.RTLD_LAZY diff --git a/vlib/fontstash/fontstash.v b/vlib/fontstash/fontstash.v index 00f72cf13f..6149a01f53 100644 --- a/vlib/fontstash/fontstash.v +++ b/vlib/fontstash/fontstash.v @@ -9,6 +9,15 @@ $if gcboehm ? { } #include "fontstash.h" #flag -I /usr/local/Cellar/freetype/2.10.2/include/freetype2 + +$if windows { + $if tinyc { + #flag @VEXEROOT/thirdparty/tcc/lib/openlibm.o + } +} $else { + #flag -lm +} + //#flag -lfreetype pub const ( // TODO: fontstash.used_import is used to keep v from warning about unused imports diff --git a/vlib/math/math.c.v b/vlib/math/math.c.v index 534d9ae842..689e648691 100644 --- a/vlib/math/math.c.v +++ b/vlib/math/math.c.v @@ -4,11 +4,15 @@ module math #include + $if windows { $if tinyc { #flag @VEXEROOT/thirdparty/tcc/lib/openlibm.o } +} $else { + #flag -lm } + fn C.acos(x f64) f64 fn C.asin(x f64) f64 diff --git a/vlib/sokol/c/declaration.c.v b/vlib/sokol/c/declaration.c.v index c1ee5b7782..a0120c2166 100644 --- a/vlib/sokol/c/declaration.c.v +++ b/vlib/sokol/c/declaration.c.v @@ -40,6 +40,8 @@ $if ios { #flag solaris -DSOKOL_NO_ENTRY // TODO end +#flag linux -ldl + $if gcboehm ? { #define SOKOL_MALLOC GC_MALLOC #define SOKOL_CALLOC(n,m) GC_MALLOC((n)*(m)) diff --git a/vlib/sync/threads/threads.c.v b/vlib/sync/threads/threads.c.v new file mode 100644 index 0000000000..02506c20c9 --- /dev/null +++ b/vlib/sync/threads/threads.c.v @@ -0,0 +1,13 @@ +module threads + +// This module adds the necessary compiler flags for using threads. +// It is automatically imported by code that does `go func()` . +// See vlib/v/parser/pratt.v, search for ast.GoExpr . +// The goal is that programs, that do not use threads at all will not need +// to link to -lpthread etc. +// NB: on some platforms like Android, linking -lpthread is not needed too. +// See https://stackoverflow.com/a/31277163/1904615 + +$if !windows && !android { + #flag -lpthread +} diff --git a/vlib/sync/threads/threads.v b/vlib/sync/threads/threads.v new file mode 100644 index 0000000000..f20fc0e480 --- /dev/null +++ b/vlib/sync/threads/threads.v @@ -0,0 +1,4 @@ +module threads + +// This file is just a placeholder. +// The actual implementation is backend/platform specific, so see threads.c.v, threads.js.v etc. diff --git a/vlib/v/builder/cc.v b/vlib/v/builder/cc.v index a99876563f..5270b004cf 100644 --- a/vlib/v/builder/cc.v +++ b/vlib/v/builder/cc.v @@ -380,20 +380,11 @@ fn (mut v Builder) setup_ccompiler_options(ccompiler string) { // || os.user_os() == 'linux' if !v.pref.is_bare && v.pref.build_mode != .build_module && v.pref.os in [.linux, .freebsd, .openbsd, .netbsd, .dragonfly, .solaris, .haiku] { - ccoptions.linker_flags << '-lm' - ccoptions.linker_flags << '-lpthread' - // -ldl is a Linux only thing. BSDs have it in libc. - if v.pref.os == .linux { - ccoptions.linker_flags << '-ldl' - } if v.pref.os in [.freebsd, .netbsd] { // Free/NetBSD: backtrace needs execinfo library while linking ccoptions.linker_flags << '-lexecinfo' } } - if !v.pref.is_bare && v.pref.os == .js && os.user_os() == 'linux' { - ccoptions.linker_flags << '-lm' - } ccoptions.env_cflags = os.getenv('CFLAGS') ccoptions.env_ldflags = os.getenv('LDFLAGS') $if trace_ccoptions ? { diff --git a/vlib/v/parser/pratt.v b/vlib/v/parser/pratt.v index 99057164f7..9012be1588 100644 --- a/vlib/v/parser/pratt.v +++ b/vlib/v/parser/pratt.v @@ -519,6 +519,7 @@ fn (mut p Parser) go_expr() ast.GoExpr { } } pos := spos.extend(p.prev_tok.position()) + p.register_auto_import('sync.threads') return ast.GoExpr{ call_expr: call_expr pos: pos