Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
18 changes: 13 additions & 5 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 @@ -237,8 +237,8 @@ def context(self):

class ArchitectureBlock(Block):
template = textwrap.dedent("""\
# Define C++ flags, C flags and linker flags from 'settings.arch'
{% if arch_flag %}
# Define C++ flags, C flags and linker flags from 'settings.arch'
message(STATUS "Conan toolchain: Defining architecture flag: {{ arch_flag }}")
string(APPEND CONAN_CXX_FLAGS " {{ arch_flag }}")
string(APPEND CONAN_C_FLAGS " {{ arch_flag }}")
Expand All @@ -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
16 changes: 11 additions & 5 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 All @@ -16,19 +16,19 @@ def _generate_flags(self, conanfile):
template = textwrap.dedent(
"""\
{% if extra_cflags %}
-- C flags retrieved from CFLAGS environment, conan.conf(tools.build:cflags) and extra_cflags
-- C flags retrieved from CFLAGS environment, conan.conf(tools.build:cflags), extra_cflags and compiler settings
filter { files { "**.c" } }
buildoptions { {{ extra_cflags }} }
filter {}
{% endif %}
{% if extra_cxxflags %}
-- CXX flags retrieved from CXXFLAGS environment, conan.conf(tools.build:cxxflags) and extra_cxxflags
-- CXX flags retrieved from CXXFLAGS environment, conan.conf(tools.build:cxxflags), extra_cxxflags and compiler settings
filter { files { "**.cpp", "**.cxx", "**.cc" } }
buildoptions { {{ extra_cxxflags }} }
filter {}
{% endif %}
{% if extra_ldflags %}
-- Link flags retrieved from LDFLAGS environment, conan.conf(tools.build:sharedlinkflags), conan.conf(tools.build:exelinkflags) and extra_cxxflags
-- Link flags retrieved from LDFLAGS environment, conan.conf(tools.build:sharedlinkflags), conan.conf(tools.build:exelinkflags), extra_cxxflags and compiler settings
linkoptions { {{ extra_ldflags }} }
{% endif %}
{% if extra_defines %}
Expand All @@ -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
32 changes: 32 additions & 0 deletions test/integration/toolchains/cmake/test_cmaketoolchain.py
Original file line number Diff line number Diff line change
Expand Up @@ -1772,3 +1772,35 @@ def test_cmake_presets_compiler():
# https://github.com/microsoft/vscode-cmake-tools/blob/a1ceda25ea93fc0060324de15970a8baa69addf6/src/presets/preset.ts#L1095C23-L1095C35
assert cache_variables["CMAKE_C_COMPILER"] == "cl"
assert cache_variables["CMAKE_CXX_COMPILER"] == "cl.exe"

@pytest.mark.parametrize(
"threads, flags",
[("posix", "-pthread"), ("wasm_workers", "-sWASM_WORKERS=1")],
)
def test_thread_flags(threads, flags):
client = TestClient()
profile = textwrap.dedent(f"""
[settings]
arch=wasm
build_type=Release
compiler=emcc
compiler.cppstd=17
compiler.threads={threads}
compiler.libcxx=libc++
compiler.version=4.0.10
os=Emscripten
""")
client.save(
{
"conanfile.py": GenConanfile("pkg", "1.0")
.with_settings("os", "arch", "compiler", "build_type")
.with_generator("CMakeToolchain"),
"profile": profile,
}
)
client.run("install . -pr=./profile")
toolchain = client.load("conan_toolchain.cmake")
assert f'string(APPEND CONAN_CXX_FLAGS " {flags}")' in toolchain
assert f'string(APPEND CONAN_C_FLAGS " {flags}")' in toolchain
assert f'string(APPEND CONAN_SHARED_LINKER_FLAGS " {flags}")' in toolchain
assert f'string(APPEND CONAN_EXE_LINKER_FLAGS " {flags}")' in toolchain
38 changes: 38 additions & 0 deletions test/integration/toolchains/gnu/test_autotoolstoolchain.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import os
import platform
import textwrap
import pytest

from conan.test.assets.genconanfile import GenConanfile
from conan.test.utils.tools import TestClient
Expand Down Expand Up @@ -367,3 +368,40 @@ def test_conf_build_does_not_exist():
tc = c.load("conanautotoolstoolchain.sh")
assert 'export CC_FOR_BUILD="x86_64-linux-gnu-gcc"' in tc
assert 'export CXX_FOR_BUILD="x86_64-linux-gnu-g++"' in tc

@pytest.mark.parametrize(
"threads, flags",
[("posix", "-pthread"), ("wasm_workers", "-sWASM_WORKERS=1")],
)
def test_thread_flags(threads, flags):
os = platform.system()
client = TestClient()
profile = textwrap.dedent(f"""
[settings]
arch=wasm
build_type=Release
compiler=emcc
compiler.cppstd=17
compiler.threads={threads}
compiler.libcxx=libc++
compiler.version=4.0.10
os=Emscripten
""")
client.save(
{
"conanfile.py": GenConanfile("pkg", "1.0")
.with_settings("os", "arch", "compiler", "build_type")
.with_generator("AutotoolsToolchain"),
"profile": profile,
}
)
client.run("install . -pr=./profile")
toolchain = client.load("conanautotoolstoolchain{}".format('.bat' if os == "Windows" else '.sh'))
if os == "Windows":
assert f'set "CXXFLAGS=%CXXFLAGS% -stdlib=libc++ {flags}"' in toolchain
assert f'set "CFLAGS=%CFLAGS% {flags}"' in toolchain
assert f'set "LDFLAGS=%LDFLAGS% {flags}' in toolchain
else:
assert f'export CXXFLAGS="$CXXFLAGS -stdlib=libc++ {flags}"' in toolchain
assert f'export CFLAGS="$CFLAGS {flags}"' in toolchain
assert f'export LDFLAGS="$LDFLAGS {flags}"' in toolchain
40 changes: 40 additions & 0 deletions test/integration/toolchains/gnu/test_gnutoolchain.py
Original file line number Diff line number Diff line change
Expand Up @@ -592,3 +592,43 @@ def build(self):
"conanfile.py": consumer
})
client.run("create . -pr:h host -pr:b build")


@pytest.mark.parametrize(
"threads, flags",
[("posix", "-pthread"), ("wasm_workers", "-sWASM_WORKERS=1")],
)
def test_thread_flags(threads, flags):
client = TestClient()
profile = textwrap.dedent(f"""
[settings]
arch=wasm
build_type=Release
compiler=emcc
compiler.cppstd=17
compiler.threads={threads}
compiler.libcxx=libc++
compiler.version=4.0.10
os=Emscripten
""")
client.save(
{
"conanfile.py": GenConanfile("pkg", "1.0")
.with_settings("os", "arch", "compiler", "build_type")
.with_generator("GnuToolchain"),
"profile": profile,
}
)
client.run("install . -pr=./profile")
os = platform.system()
toolchain = client.load(
"conangnutoolchain{}".format('.bat' if os == "Windows" else '.sh'))

if os == "Windows":
assert f'set "CXXFLAGS=%CXXFLAGS% -stdlib=libc++ {flags}"' in toolchain
assert f'set "CFLAGS=%CFLAGS% {flags}"' in toolchain
assert f'set "LDFLAGS=%LDFLAGS% {flags}' in toolchain
else:
assert f'export CXXFLAGS="$CXXFLAGS -stdlib=libc++ {flags}"' in toolchain
assert f'export CFLAGS="$CFLAGS {flags}"' in toolchain
assert f'export LDFLAGS="$LDFLAGS {flags}"' in toolchain
Loading
Loading