Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions conan/internal/default_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@
# There is no ABI compatibility guarantee between versions
version: [ANY]
libcxx: [null, libstdc++, libstdc++11, libc++]
threads: [null, posix, wasm_workers]
cppstd: [null, 98, gnu98, 11, gnu11, 14, gnu14, 17, gnu17, 20, gnu20, 23, gnu23, 26, gnu26]
cstd: [null, 99, gnu99, 11, gnu11, 17, gnu17, 23, gnu23]

Expand Down
12 changes: 12 additions & 0 deletions conan/tools/build/flags.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,18 @@ def build_type_flags(conanfile):
return flags
return []

def threads_flags(conanfile):
"""
returns flags specific to the threading model used by the compiler
"""
compiler = conanfile.settings.get_safe("compiler")
threads = conanfile.settings.get_safe("compiler.threads")
if compiler == "emcc":
if threads == "posix":
return ["-pthread"]
elif threads == "wasm_workers":
return ["-sWASM_WORKERS=1"]
return []

def llvm_clang_front(conanfile):
# Only Windows clang with MSVC backend (LLVM/Clang, not MSYS2 clang)
Expand Down
16 changes: 12 additions & 4 deletions conan/tools/cmake/toolchain/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from conan.tools.android.utils import android_abi
from conan.tools.apple.apple import is_apple_os, to_apple_arch
from conan.tools.build import build_jobs
from conan.tools.build.flags import architecture_flag, architecture_link_flag, libcxx_flags
from conan.tools.build.flags import architecture_flag, architecture_link_flag, libcxx_flags, threads_flags
from conan.tools.build.cross_building import cross_building
from conan.tools.cmake.toolchain import CONAN_TOOLCHAIN_FILENAME
from conan.tools.cmake.utils import is_multi_configuration
Expand Down Expand Up @@ -250,15 +250,23 @@ class ArchitectureBlock(Block):
string(APPEND CONAN_SHARED_LINKER_FLAGS " {{ arch_link_flag }}")
string(APPEND CONAN_EXE_LINKER_FLAGS " {{ arch_link_flag }}")
{% endif %}
{% if thread_flags_list %}
# Define C++ flags, C flags and linker flags from 'compiler.threads'
message(STATUS "Conan toolchain: Defining thread flags: {{ thread_flags_list }}")
string(APPEND CONAN_CXX_FLAGS " {{ thread_flags_list }}")
string(APPEND CONAN_C_FLAGS " {{ thread_flags_list }}")
string(APPEND CONAN_SHARED_LINKER_FLAGS " {{ thread_flags_list }}")
string(APPEND CONAN_EXE_LINKER_FLAGS " {{ thread_flags_list }}")
{% endif %}
""")

def context(self):
arch_flag = architecture_flag(self._conanfile)
arch_link_flag = architecture_link_flag(self._conanfile)
if not arch_flag and not arch_link_flag:
thread_flags_list = " ".join(threads_flags(self._conanfile))
if not arch_flag and not arch_link_flag and not thread_flags_list:
return
return {"arch_flag": arch_flag, "arch_link_flag": arch_link_flag}

return {"arch_flag": arch_flag, "arch_link_flag": arch_link_flag, "thread_flags_list": thread_flags_list}

class LinkerScriptsBlock(Block):
template = textwrap.dedent("""\
Expand Down
9 changes: 5 additions & 4 deletions conan/tools/gnu/autotoolstoolchain.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from conan.tools.build import cmd_args_to_string, save_toolchain_args
from conan.tools.build.cross_building import cross_building
from conan.tools.build.flags import architecture_flag, architecture_link_flag, build_type_flags, cppstd_flag, \
build_type_link_flags, libcxx_flags, cstd_flag, llvm_clang_front
build_type_link_flags, libcxx_flags, cstd_flag, llvm_clang_front, threads_flags
from conan.tools.env import Environment, VirtualBuildEnv
from conan.tools.gnu.get_gnu_triplet import _get_gnu_triplet
from conan.tools.microsoft import VCVars, msvc_runtime_flag, unix_path, check_min_vs, is_msvc
Expand Down Expand Up @@ -53,6 +53,7 @@ def __init__(self, conanfile, namespace=None, prefix="/"):
self.cstd = cstd_flag(self._conanfile)
self.arch_flag = architecture_flag(self._conanfile)
self.arch_ld_flag = architecture_link_flag(self._conanfile)
self.threads_flags = threads_flags(self._conanfile)
self.libcxx, self.gcc_cxx11_abi = libcxx_flags(self._conanfile)
self.fpic = self._conanfile.options.get_safe("fPIC")
self.msvc_runtime_flag = self._get_msvc_runtime_flag()
Expand Down Expand Up @@ -203,7 +204,7 @@ def _filter_list_empty_fields(v):
def cxxflags(self):
fpic = "-fPIC" if self.fpic else None
ret = [self.libcxx, self.cppstd, self.arch_flag, fpic, self.msvc_runtime_flag,
self.sysroot_flag]
self.sysroot_flag] + self.threads_flags
apple_flags = [self.apple_isysroot_flag, self.apple_arch_flag, self.apple_min_version_flag]
apple_flags += self.apple_extra_flags
conf_flags = self._conanfile.conf.get("tools.build:cxxflags", default=[], check_type=list)
Expand All @@ -214,7 +215,7 @@ def cxxflags(self):
@property
def cflags(self):
fpic = "-fPIC" if self.fpic else None
ret = [self.cstd, self.arch_flag, fpic, self.msvc_runtime_flag, self.sysroot_flag]
ret = [self.cstd, self.arch_flag, fpic, self.msvc_runtime_flag, self.sysroot_flag] + self.threads_flags
apple_flags = [self.apple_isysroot_flag, self.apple_arch_flag, self.apple_min_version_flag]
apple_flags += self.apple_extra_flags
conf_flags = self._conanfile.conf.get("tools.build:cflags", default=[], check_type=list)
Expand All @@ -224,7 +225,7 @@ def cflags(self):

@property
def ldflags(self):
ret = [self.arch_flag, self.sysroot_flag, self.arch_ld_flag]
ret = [self.arch_flag, self.sysroot_flag, self.arch_ld_flag] + self.threads_flags
apple_flags = [self.apple_isysroot_flag, self.apple_arch_flag, self.apple_min_version_flag]
apple_flags += self.apple_extra_flags
conf_flags = self._conanfile.conf.get("tools.build:sharedlinkflags", default=[],
Expand Down
9 changes: 5 additions & 4 deletions conan/tools/gnu/gnutoolchain.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from conan.tools.build.cross_building import cross_building
from conan.tools.build.flags import architecture_flag, architecture_link_flag, build_type_flags, cppstd_flag, \
build_type_link_flags, \
libcxx_flags, llvm_clang_front
libcxx_flags, llvm_clang_front, threads_flags
from conan.tools.env import Environment, VirtualBuildEnv
from conan.tools.gnu.get_gnu_triplet import _get_gnu_triplet
from conan.tools.microsoft import VCVars, msvc_runtime_flag, unix_path, check_min_vs, is_msvc
Expand Down Expand Up @@ -58,6 +58,7 @@ def __init__(self, conanfile, namespace=None, prefix="/"):
self.cppstd = cppstd_flag(self._conanfile)
self.arch_flag = architecture_flag(self._conanfile)
self.arch_ld_flag = architecture_link_flag(self._conanfile)
self.threads_flags = threads_flags(self._conanfile)
self.libcxx, self.gcc_cxx11_abi = libcxx_flags(self._conanfile)
self.fpic = self._conanfile.options.get_safe("fPIC")
self.msvc_runtime_flag = self._get_msvc_runtime_flag()
Expand Down Expand Up @@ -268,7 +269,7 @@ def _dict_to_list(flags):
def cxxflags(self):
fpic = "-fPIC" if self.fpic else None
ret = [self.libcxx, self.cppstd, self.arch_flag, fpic, self.msvc_runtime_flag,
self.sysroot_flag]
self.sysroot_flag] + self.threads_flags
apple_flags = [self.apple_isysroot_flag, self.apple_arch_flag, self.apple_min_version_flag]
apple_flags += self.apple_extra_flags
conf_flags = self._conanfile.conf.get("tools.build:cxxflags", default=[], check_type=list)
Expand All @@ -279,7 +280,7 @@ def cxxflags(self):
@property
def cflags(self):
fpic = "-fPIC" if self.fpic else None
ret = [self.arch_flag, fpic, self.msvc_runtime_flag, self.sysroot_flag]
ret = [self.arch_flag, fpic, self.msvc_runtime_flag, self.sysroot_flag] + self.threads_flags
apple_flags = [self.apple_isysroot_flag, self.apple_arch_flag, self.apple_min_version_flag]
apple_flags += self.apple_extra_flags
conf_flags = self._conanfile.conf.get("tools.build:cflags", default=[], check_type=list)
Expand All @@ -289,7 +290,7 @@ def cflags(self):

@property
def ldflags(self):
ret = [self.arch_flag, self.sysroot_flag, self.arch_ld_flag]
ret = [self.arch_flag, self.sysroot_flag, self.arch_ld_flag] + self.threads_flags
apple_flags = [self.apple_isysroot_flag, self.apple_arch_flag, self.apple_min_version_flag]
apple_flags += self.apple_extra_flags
conf_flags = self._conanfile.conf.get("tools.build:sharedlinkflags", default=[],
Expand Down
10 changes: 6 additions & 4 deletions conan/tools/meson/toolchain.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from conan.tools.apple.apple import is_apple_os, apple_min_version_flag, \
resolve_apple_flags, apple_extra_flags
from conan.tools.build.cross_building import cross_building
from conan.tools.build.flags import architecture_link_flag, libcxx_flags, architecture_flag
from conan.tools.build.flags import architecture_link_flag, libcxx_flags, architecture_flag, threads_flags
from conan.tools.env import VirtualBuildEnv
from conan.tools.meson.helpers import *
from conan.tools.meson.helpers import get_apple_subsystem
Expand Down Expand Up @@ -218,6 +218,8 @@ def __init__(self, conanfile, backend=None, native=False):
self.arch_flag = architecture_flag(self._conanfile) # https://github.com/conan-io/conan/issues/17624
#: Architecture link flag deduced by Conan and added to ``c_link_args`` and ``cpp_link_args``
self.arch_link_flag = architecture_link_flag(self._conanfile)
#: Threads flags deduced by Conan and added to ``c_args``, ``cpp_args``, ``c_link_args`` and ``cpp_link_args``
self.threads_flags = threads_flags(self._conanfile)
#: Dict-like object that defines Meson ``properties`` with ``key=value`` format
self.properties = {}
#: Dict-like object that defines Meson ``project options`` with ``key=value`` format
Expand Down Expand Up @@ -453,14 +455,14 @@ def _get_extra_flags(self):
linker_script_flags = ['-T"' + linker_script + '"' for linker_script in linker_scripts]
defines = self._conanfile_conf.get("tools.build:defines", default=[], check_type=list)
sys_root = [f"--sysroot={self._sys_root}"] if self._sys_root else [""]
ld = sharedlinkflags + exelinkflags + linker_script_flags + sys_root + self.extra_ldflags
ld = sharedlinkflags + exelinkflags + linker_script_flags + sys_root + self.extra_ldflags + self.threads_flags
# Apple extra flags from confs (visibilty, bitcode, arc)
cxxflags += self.apple_extra_flags
cflags += self.apple_extra_flags
ld += self.apple_extra_flags
return {
"cxxflags": [self.arch_flag] + cxxflags + sys_root + self.extra_cxxflags,
"cflags": [self.arch_flag] + cflags + sys_root + self.extra_cflags,
"cxxflags": [self.arch_flag] + cxxflags + sys_root + self.extra_cxxflags + self.threads_flags,
"cflags": [self.arch_flag] + cflags + sys_root + self.extra_cflags + self.threads_flags,
"ldflags": [self.arch_flag] + [self.arch_link_flag] + ld,
"defines": [f"-D{d}" for d in (defines + self.extra_defines)]
}
Expand Down
10 changes: 8 additions & 2 deletions conan/tools/premake/toolchain.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from conan.tools.build.flags import architecture_flag, architecture_link_flag, libcxx_flags
from conan.tools.build.flags import architecture_flag, architecture_link_flag, libcxx_flags, threads_flags
import os
import textwrap
from pathlib import Path
Expand Down Expand Up @@ -47,27 +47,33 @@ def to_list(value):
arch_flags = to_list(architecture_flag(self._conanfile))
cxx_flags, libcxx_compile_definitions = libcxx_flags(self._conanfile)
arch_link_flags = to_list(architecture_link_flag(self._conanfile))
thread_flags_list = threads_flags(self._conanfile)

extra_defines = format_list(
conanfile.conf.get("tools.build:defines", default=[], check_type=list)
+ self.extra_defines
+ to_list(libcxx_compile_definitions)
)
extra_c_flags = format_list(
conanfile.conf.get("tools.build:cflags", default=[], check_type=list) + self.extra_cflags + arch_flags
conanfile.conf.get("tools.build:cflags", default=[], check_type=list)
+ self.extra_cflags
+ arch_flags
+ thread_flags_list
)
extra_cxx_flags = format_list(
conanfile.conf.get("tools.build:cxxflags", default=[], check_type=list)
+ to_list(cxx_flags)
+ self.extra_cxxflags
+ arch_flags
+ thread_flags_list
)
extra_ld_flags = format_list(
conanfile.conf.get("tools.build:sharedlinkflags", default=[], check_type=list)
+ conanfile.conf.get("tools.build:exelinkflags", default=[], check_type=list)
+ self.extra_ldflags
+ arch_flags
+ arch_link_flags
+ thread_flags_list
)

return (
Expand Down
1 change: 1 addition & 0 deletions test/integration/toolchains/meson/test_mesontoolchain.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@ def generate(self):
content = t.load(MesonToolchain.native_filename)
assert "cpp_args = ['-flag0', '-other=val', '-m64', '-flag1', '-flag2', '-Ddefine1=0', '-D_GLIBCXX_USE_CXX11_ABI=0']" in content
assert "c_args = ['-flag0', '-other=val', '-m64', '-flag3', '-flag4', '-Ddefine1=0']" in content
print(content)
assert "c_link_args = ['-flag0', '-other=val', '-m64', '-flag5', '-flag6']" in content
assert "cpp_link_args = ['-flag0', '-other=val', '-m64', '-flag5', '-flag6']" in content

Expand Down
15 changes: 14 additions & 1 deletion test/unittests/client/build/compiler_flags_test.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import unittest
from parameterized.parameterized import parameterized

from conan.tools.build.flags import architecture_flag, build_type_flags
from conan.tools.build.flags import architecture_flag, build_type_flags, threads_flags
from conan.test.utils.mocks import MockSettings, ConanFileMock


Expand Down Expand Up @@ -37,6 +37,19 @@ def test_arch_flag(self, compiler, arch, the_os, flag):
conanfile.settings = settings
self.assertEqual(architecture_flag(conanfile), flag)


@parameterized.expand([("clang", None, []),
("emcc", None, []),
("emcc", "posix", ["-pthread"]),
("emcc", "wasm_workers", ["-sWASM_WORKERS=1"]),
])
def test_threads_flag(self, compiler, threads, flag):
settings = MockSettings({"compiler": compiler,
"compiler.threads": threads})
conanfile = ConanFileMock()
conanfile.settings = settings
self.assertEqual(threads_flags(conanfile), flag)

@parameterized.expand([("clang", "x86", "Windows", ""),
("clang", "x86_64", "Windows", "")
])
Expand Down
Loading