Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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_flag(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
17 changes: 16 additions & 1 deletion 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_flag
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 @@ -259,6 +259,21 @@ def context(self):
return
return {"arch_flag": arch_flag, "arch_link_flag": arch_link_flag}

class ThreadsBlock(Block):
template = textwrap.dedent("""\
# Define C++ flags, C flags and linker flags from 'compiler.threads'
message(STATUS "Conan toolchain: Defining threads flag: {{ threads_flag }}")
string(APPEND CONAN_CXX_FLAGS " {{ threads_flag }}")
string(APPEND CONAN_C_FLAGS " {{ threads_flag }}")
string(APPEND CONAN_SHARED_LINKER_FLAGS " {{ threads_flag }}")
string(APPEND CONAN_EXE_LINKER_FLAGS " {{ threads_flag }}")
""")

def context(self):
flags = threads_flag(self._conanfile)
if not flags:
return
return {"threads_flag": flags}

class LinkerScriptsBlock(Block):
template = textwrap.dedent("""\
Expand Down
3 changes: 2 additions & 1 deletion conan/tools/cmake/toolchain/toolchain.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from conan.tools.build import use_win_mingw
from conan.tools.cmake.presets import write_cmake_presets
from conan.tools.cmake.toolchain import CONAN_TOOLCHAIN_FILENAME
from conan.tools.cmake.toolchain.blocks import ExtraVariablesBlock, ToolchainBlocks, UserToolchain, \
from conan.tools.cmake.toolchain.blocks import ExtraVariablesBlock, ThreadsBlock, ToolchainBlocks, UserToolchain, \
GenericSystemBlock, \
AndroidSystemBlock, AppleSystemBlock, FPicBlock, ArchitectureBlock, GLibCXXBlock, VSRuntimeBlock, \
CppStdBlock, ParallelBlock, CMakeFlagsInitBlock, TryCompileBlock, FindFiles, PkgConfigBlock, \
Expand Down Expand Up @@ -103,6 +103,7 @@ def __init__(self, conanfile, generator=None):
("apple_system", AppleSystemBlock),
("fpic", FPicBlock),
("arch_flags", ArchitectureBlock),
("threads_flags", ThreadsBlock),
("linker_scripts", LinkerScriptsBlock),
("libcxx", GLibCXXBlock),
("vs_runtime", VSRuntimeBlock),
Expand Down
7 changes: 4 additions & 3 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_flag
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_flag = threads_flag(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_flag]
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_flag]
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 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_flag
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_flag = threads_flag(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_flag]
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_flag]
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_flag]
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_flag
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 flag deduced by Conan and added to ``c_args``, ``cpp_args``, ``c_link_args`` and ``cpp_link_args``
self.threads_flag = threads_flag(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 @@ -459,9 +461,9 @@ def _get_extra_flags(self):
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,
"ldflags": [self.arch_flag] + [self.arch_link_flag] + ld,
"cxxflags": [self.arch_flag] + cxxflags + sys_root + self.extra_cxxflags + [self.threads_flag],
"cflags": [self.arch_flag] + cflags + sys_root + self.extra_cflags + [self.threads_flag],
"ldflags": [self.arch_flag] + [self.arch_link_flag] + ld + self.extra_ldflags,
"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_flag
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))
threads_flags = to_list(threads_flag(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
+ threads_flags
)
extra_cxx_flags = format_list(
conanfile.conf.get("tools.build:cxxflags", default=[], check_type=list)
+ to_list(cxx_flags)
+ self.extra_cxxflags
+ arch_flags
+ threads_flags
)
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
+ threads_flags
)

return (
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_flag
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_flag(conanfile), flag)

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