Skip to content

Commit bab5a20

Browse files
perseoGImemsharded
andcommitted
Added threads subsetting in emcc compiler model (conan-io#18520)
* Added threads subsetting on emcc compiler model * Fixed test * Return list instead * Update test/integration/toolchains/meson/test_mesontoolchain.py * Added integration tests to every toolchain * WIP fix windows tests * Fix windows autotools tests --------- Co-authored-by: James <[email protected]>
1 parent 30b760b commit bab5a20

File tree

13 files changed

+260
-25
lines changed

13 files changed

+260
-25
lines changed

conan/internal/default_settings.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@
168168
# There is no ABI compatibility guarantee between versions
169169
version: [ANY]
170170
libcxx: [null, libstdc++, libstdc++11, libc++]
171+
threads: [null, posix, wasm_workers]
171172
cppstd: [null, 98, gnu98, 11, gnu11, 14, gnu14, 17, gnu17, 20, gnu20, 23, gnu23, 26, gnu26]
172173
cstd: [null, 99, gnu99, 11, gnu11, 17, gnu17, 23, gnu23]
173174

conan/tools/build/flags.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,18 @@ def build_type_flags(conanfile):
195195
return flags
196196
return []
197197

198+
def threads_flags(conanfile):
199+
"""
200+
returns flags specific to the threading model used by the compiler
201+
"""
202+
compiler = conanfile.settings.get_safe("compiler")
203+
threads = conanfile.settings.get_safe("compiler.threads")
204+
if compiler == "emcc":
205+
if threads == "posix":
206+
return ["-pthread"]
207+
elif threads == "wasm_workers":
208+
return ["-sWASM_WORKERS=1"]
209+
return []
198210

199211
def llvm_clang_front(conanfile):
200212
# Only Windows clang with MSVC backend (LLVM/Clang, not MSYS2 clang)

conan/tools/cmake/toolchain/blocks.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from conan.tools.android.utils import android_abi
1111
from conan.tools.apple.apple import is_apple_os, to_apple_arch
1212
from conan.tools.build import build_jobs
13-
from conan.tools.build.flags import architecture_flag, architecture_link_flag, libcxx_flags
13+
from conan.tools.build.flags import architecture_flag, architecture_link_flag, libcxx_flags, threads_flags
1414
from conan.tools.build.cross_building import cross_building
1515
from conan.tools.cmake.toolchain import CONAN_TOOLCHAIN_FILENAME
1616
from conan.tools.cmake.utils import is_multi_configuration
@@ -237,8 +237,8 @@ def context(self):
237237

238238
class ArchitectureBlock(Block):
239239
template = textwrap.dedent("""\
240-
# Define C++ flags, C flags and linker flags from 'settings.arch'
241240
{% if arch_flag %}
241+
# Define C++ flags, C flags and linker flags from 'settings.arch'
242242
message(STATUS "Conan toolchain: Defining architecture flag: {{ arch_flag }}")
243243
string(APPEND CONAN_CXX_FLAGS " {{ arch_flag }}")
244244
string(APPEND CONAN_C_FLAGS " {{ arch_flag }}")
@@ -250,15 +250,23 @@ class ArchitectureBlock(Block):
250250
string(APPEND CONAN_SHARED_LINKER_FLAGS " {{ arch_link_flag }}")
251251
string(APPEND CONAN_EXE_LINKER_FLAGS " {{ arch_link_flag }}")
252252
{% endif %}
253+
{% if thread_flags_list %}
254+
# Define C++ flags, C flags and linker flags from 'compiler.threads'
255+
message(STATUS "Conan toolchain: Defining thread flags: {{ thread_flags_list }}")
256+
string(APPEND CONAN_CXX_FLAGS " {{ thread_flags_list }}")
257+
string(APPEND CONAN_C_FLAGS " {{ thread_flags_list }}")
258+
string(APPEND CONAN_SHARED_LINKER_FLAGS " {{ thread_flags_list }}")
259+
string(APPEND CONAN_EXE_LINKER_FLAGS " {{ thread_flags_list }}")
260+
{% endif %}
253261
""")
254262

255263
def context(self):
256264
arch_flag = architecture_flag(self._conanfile)
257265
arch_link_flag = architecture_link_flag(self._conanfile)
258-
if not arch_flag and not arch_link_flag:
266+
thread_flags_list = " ".join(threads_flags(self._conanfile))
267+
if not arch_flag and not arch_link_flag and not thread_flags_list:
259268
return
260-
return {"arch_flag": arch_flag, "arch_link_flag": arch_link_flag}
261-
269+
return {"arch_flag": arch_flag, "arch_link_flag": arch_link_flag, "thread_flags_list": thread_flags_list}
262270

263271
class LinkerScriptsBlock(Block):
264272
template = textwrap.dedent("""\

conan/tools/gnu/autotoolstoolchain.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from conan.tools.build import cmd_args_to_string, save_toolchain_args
88
from conan.tools.build.cross_building import cross_building
99
from conan.tools.build.flags import architecture_flag, architecture_link_flag, build_type_flags, cppstd_flag, \
10-
build_type_link_flags, libcxx_flags, cstd_flag, llvm_clang_front
10+
build_type_link_flags, libcxx_flags, cstd_flag, llvm_clang_front, threads_flags
1111
from conan.tools.env import Environment, VirtualBuildEnv
1212
from conan.tools.gnu.get_gnu_triplet import _get_gnu_triplet
1313
from conan.tools.microsoft import VCVars, msvc_runtime_flag, unix_path, check_min_vs, is_msvc
@@ -53,6 +53,7 @@ def __init__(self, conanfile, namespace=None, prefix="/"):
5353
self.cstd = cstd_flag(self._conanfile)
5454
self.arch_flag = architecture_flag(self._conanfile)
5555
self.arch_ld_flag = architecture_link_flag(self._conanfile)
56+
self.threads_flags = threads_flags(self._conanfile)
5657
self.libcxx, self.gcc_cxx11_abi = libcxx_flags(self._conanfile)
5758
self.fpic = self._conanfile.options.get_safe("fPIC")
5859
self.msvc_runtime_flag = self._get_msvc_runtime_flag()
@@ -203,7 +204,7 @@ def _filter_list_empty_fields(v):
203204
def cxxflags(self):
204205
fpic = "-fPIC" if self.fpic else None
205206
ret = [self.libcxx, self.cppstd, self.arch_flag, fpic, self.msvc_runtime_flag,
206-
self.sysroot_flag]
207+
self.sysroot_flag] + self.threads_flags
207208
apple_flags = [self.apple_isysroot_flag, self.apple_arch_flag, self.apple_min_version_flag]
208209
apple_flags += self.apple_extra_flags
209210
conf_flags = self._conanfile.conf.get("tools.build:cxxflags", default=[], check_type=list)
@@ -214,7 +215,7 @@ def cxxflags(self):
214215
@property
215216
def cflags(self):
216217
fpic = "-fPIC" if self.fpic else None
217-
ret = [self.cstd, self.arch_flag, fpic, self.msvc_runtime_flag, self.sysroot_flag]
218+
ret = [self.cstd, self.arch_flag, fpic, self.msvc_runtime_flag, self.sysroot_flag] + self.threads_flags
218219
apple_flags = [self.apple_isysroot_flag, self.apple_arch_flag, self.apple_min_version_flag]
219220
apple_flags += self.apple_extra_flags
220221
conf_flags = self._conanfile.conf.get("tools.build:cflags", default=[], check_type=list)
@@ -224,7 +225,7 @@ def cflags(self):
224225

225226
@property
226227
def ldflags(self):
227-
ret = [self.arch_flag, self.sysroot_flag, self.arch_ld_flag]
228+
ret = [self.arch_flag, self.sysroot_flag, self.arch_ld_flag] + self.threads_flags
228229
apple_flags = [self.apple_isysroot_flag, self.apple_arch_flag, self.apple_min_version_flag]
229230
apple_flags += self.apple_extra_flags
230231
conf_flags = self._conanfile.conf.get("tools.build:sharedlinkflags", default=[],

conan/tools/gnu/gnutoolchain.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from conan.tools.build.cross_building import cross_building
88
from conan.tools.build.flags import architecture_flag, architecture_link_flag, build_type_flags, cppstd_flag, \
99
build_type_link_flags, \
10-
libcxx_flags, llvm_clang_front
10+
libcxx_flags, llvm_clang_front, threads_flags
1111
from conan.tools.env import Environment, VirtualBuildEnv
1212
from conan.tools.gnu.get_gnu_triplet import _get_gnu_triplet
1313
from conan.tools.microsoft import VCVars, msvc_runtime_flag, unix_path, check_min_vs, is_msvc
@@ -58,6 +58,7 @@ def __init__(self, conanfile, namespace=None, prefix="/"):
5858
self.cppstd = cppstd_flag(self._conanfile)
5959
self.arch_flag = architecture_flag(self._conanfile)
6060
self.arch_ld_flag = architecture_link_flag(self._conanfile)
61+
self.threads_flags = threads_flags(self._conanfile)
6162
self.libcxx, self.gcc_cxx11_abi = libcxx_flags(self._conanfile)
6263
self.fpic = self._conanfile.options.get_safe("fPIC")
6364
self.msvc_runtime_flag = self._get_msvc_runtime_flag()
@@ -268,7 +269,7 @@ def _dict_to_list(flags):
268269
def cxxflags(self):
269270
fpic = "-fPIC" if self.fpic else None
270271
ret = [self.libcxx, self.cppstd, self.arch_flag, fpic, self.msvc_runtime_flag,
271-
self.sysroot_flag]
272+
self.sysroot_flag] + self.threads_flags
272273
apple_flags = [self.apple_isysroot_flag, self.apple_arch_flag, self.apple_min_version_flag]
273274
apple_flags += self.apple_extra_flags
274275
conf_flags = self._conanfile.conf.get("tools.build:cxxflags", default=[], check_type=list)
@@ -279,7 +280,7 @@ def cxxflags(self):
279280
@property
280281
def cflags(self):
281282
fpic = "-fPIC" if self.fpic else None
282-
ret = [self.arch_flag, fpic, self.msvc_runtime_flag, self.sysroot_flag]
283+
ret = [self.arch_flag, fpic, self.msvc_runtime_flag, self.sysroot_flag] + self.threads_flags
283284
apple_flags = [self.apple_isysroot_flag, self.apple_arch_flag, self.apple_min_version_flag]
284285
apple_flags += self.apple_extra_flags
285286
conf_flags = self._conanfile.conf.get("tools.build:cflags", default=[], check_type=list)
@@ -289,7 +290,7 @@ def cflags(self):
289290

290291
@property
291292
def ldflags(self):
292-
ret = [self.arch_flag, self.sysroot_flag, self.arch_ld_flag]
293+
ret = [self.arch_flag, self.sysroot_flag, self.arch_ld_flag] + self.threads_flags
293294
apple_flags = [self.apple_isysroot_flag, self.apple_arch_flag, self.apple_min_version_flag]
294295
apple_flags += self.apple_extra_flags
295296
conf_flags = self._conanfile.conf.get("tools.build:sharedlinkflags", default=[],

conan/tools/meson/toolchain.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from conan.tools.apple.apple import is_apple_os, apple_min_version_flag, \
1010
resolve_apple_flags, apple_extra_flags
1111
from conan.tools.build.cross_building import cross_building
12-
from conan.tools.build.flags import architecture_link_flag, libcxx_flags, architecture_flag
12+
from conan.tools.build.flags import architecture_link_flag, libcxx_flags, architecture_flag, threads_flags
1313
from conan.tools.env import VirtualBuildEnv
1414
from conan.tools.meson.helpers import *
1515
from conan.tools.meson.helpers import get_apple_subsystem
@@ -218,6 +218,8 @@ def __init__(self, conanfile, backend=None, native=False):
218218
self.arch_flag = architecture_flag(self._conanfile) # https://github.com/conan-io/conan/issues/17624
219219
#: Architecture link flag deduced by Conan and added to ``c_link_args`` and ``cpp_link_args``
220220
self.arch_link_flag = architecture_link_flag(self._conanfile)
221+
#: Threads flags deduced by Conan and added to ``c_args``, ``cpp_args``, ``c_link_args`` and ``cpp_link_args``
222+
self.threads_flags = threads_flags(self._conanfile)
221223
#: Dict-like object that defines Meson ``properties`` with ``key=value`` format
222224
self.properties = {}
223225
#: Dict-like object that defines Meson ``project options`` with ``key=value`` format
@@ -453,14 +455,14 @@ def _get_extra_flags(self):
453455
linker_script_flags = ['-T"' + linker_script + '"' for linker_script in linker_scripts]
454456
defines = self._conanfile_conf.get("tools.build:defines", default=[], check_type=list)
455457
sys_root = [f"--sysroot={self._sys_root}"] if self._sys_root else [""]
456-
ld = sharedlinkflags + exelinkflags + linker_script_flags + sys_root + self.extra_ldflags
458+
ld = sharedlinkflags + exelinkflags + linker_script_flags + sys_root + self.extra_ldflags + self.threads_flags
457459
# Apple extra flags from confs (visibilty, bitcode, arc)
458460
cxxflags += self.apple_extra_flags
459461
cflags += self.apple_extra_flags
460462
ld += self.apple_extra_flags
461463
return {
462-
"cxxflags": [self.arch_flag] + cxxflags + sys_root + self.extra_cxxflags,
463-
"cflags": [self.arch_flag] + cflags + sys_root + self.extra_cflags,
464+
"cxxflags": [self.arch_flag] + cxxflags + sys_root + self.extra_cxxflags + self.threads_flags,
465+
"cflags": [self.arch_flag] + cflags + sys_root + self.extra_cflags + self.threads_flags,
464466
"ldflags": [self.arch_flag] + [self.arch_link_flag] + ld,
465467
"defines": [f"-D{d}" for d in (defines + self.extra_defines)]
466468
}

conan/tools/premake/toolchain.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from conan.tools.build.flags import architecture_flag, architecture_link_flag, libcxx_flags
1+
from conan.tools.build.flags import architecture_flag, architecture_link_flag, libcxx_flags, threads_flags
22
import os
33
import textwrap
44
from pathlib import Path
@@ -16,19 +16,19 @@ def _generate_flags(self, conanfile):
1616
template = textwrap.dedent(
1717
"""\
1818
{% if extra_cflags %}
19-
-- C flags retrieved from CFLAGS environment, conan.conf(tools.build:cflags) and extra_cflags
19+
-- C flags retrieved from CFLAGS environment, conan.conf(tools.build:cflags), extra_cflags and compiler settings
2020
filter { files { "**.c" } }
2121
buildoptions { {{ extra_cflags }} }
2222
filter {}
2323
{% endif %}
2424
{% if extra_cxxflags %}
25-
-- CXX flags retrieved from CXXFLAGS environment, conan.conf(tools.build:cxxflags) and extra_cxxflags
25+
-- CXX flags retrieved from CXXFLAGS environment, conan.conf(tools.build:cxxflags), extra_cxxflags and compiler settings
2626
filter { files { "**.cpp", "**.cxx", "**.cc" } }
2727
buildoptions { {{ extra_cxxflags }} }
2828
filter {}
2929
{% endif %}
3030
{% if extra_ldflags %}
31-
-- Link flags retrieved from LDFLAGS environment, conan.conf(tools.build:sharedlinkflags), conan.conf(tools.build:exelinkflags) and extra_cxxflags
31+
-- Link flags retrieved from LDFLAGS environment, conan.conf(tools.build:sharedlinkflags), conan.conf(tools.build:exelinkflags), extra_cxxflags and compiler settings
3232
linkoptions { {{ extra_ldflags }} }
3333
{% endif %}
3434
{% if extra_defines %}
@@ -47,27 +47,33 @@ def to_list(value):
4747
arch_flags = to_list(architecture_flag(self._conanfile))
4848
cxx_flags, libcxx_compile_definitions = libcxx_flags(self._conanfile)
4949
arch_link_flags = to_list(architecture_link_flag(self._conanfile))
50+
thread_flags_list = threads_flags(self._conanfile)
5051

5152
extra_defines = format_list(
5253
conanfile.conf.get("tools.build:defines", default=[], check_type=list)
5354
+ self.extra_defines
5455
+ to_list(libcxx_compile_definitions)
5556
)
5657
extra_c_flags = format_list(
57-
conanfile.conf.get("tools.build:cflags", default=[], check_type=list) + self.extra_cflags + arch_flags
58+
conanfile.conf.get("tools.build:cflags", default=[], check_type=list)
59+
+ self.extra_cflags
60+
+ arch_flags
61+
+ thread_flags_list
5862
)
5963
extra_cxx_flags = format_list(
6064
conanfile.conf.get("tools.build:cxxflags", default=[], check_type=list)
6165
+ to_list(cxx_flags)
6266
+ self.extra_cxxflags
6367
+ arch_flags
68+
+ thread_flags_list
6469
)
6570
extra_ld_flags = format_list(
6671
conanfile.conf.get("tools.build:sharedlinkflags", default=[], check_type=list)
6772
+ conanfile.conf.get("tools.build:exelinkflags", default=[], check_type=list)
6873
+ self.extra_ldflags
6974
+ arch_flags
7075
+ arch_link_flags
76+
+ thread_flags_list
7177
)
7278

7379
return (

test/integration/toolchains/cmake/test_cmaketoolchain.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1772,3 +1772,35 @@ def test_cmake_presets_compiler():
17721772
# https://github.com/microsoft/vscode-cmake-tools/blob/a1ceda25ea93fc0060324de15970a8baa69addf6/src/presets/preset.ts#L1095C23-L1095C35
17731773
assert cache_variables["CMAKE_C_COMPILER"] == "cl"
17741774
assert cache_variables["CMAKE_CXX_COMPILER"] == "cl.exe"
1775+
1776+
@pytest.mark.parametrize(
1777+
"threads, flags",
1778+
[("posix", "-pthread"), ("wasm_workers", "-sWASM_WORKERS=1")],
1779+
)
1780+
def test_thread_flags(threads, flags):
1781+
client = TestClient()
1782+
profile = textwrap.dedent(f"""
1783+
[settings]
1784+
arch=wasm
1785+
build_type=Release
1786+
compiler=emcc
1787+
compiler.cppstd=17
1788+
compiler.threads={threads}
1789+
compiler.libcxx=libc++
1790+
compiler.version=4.0.10
1791+
os=Emscripten
1792+
""")
1793+
client.save(
1794+
{
1795+
"conanfile.py": GenConanfile("pkg", "1.0")
1796+
.with_settings("os", "arch", "compiler", "build_type")
1797+
.with_generator("CMakeToolchain"),
1798+
"profile": profile,
1799+
}
1800+
)
1801+
client.run("install . -pr=./profile")
1802+
toolchain = client.load("conan_toolchain.cmake")
1803+
assert f'string(APPEND CONAN_CXX_FLAGS " {flags}")' in toolchain
1804+
assert f'string(APPEND CONAN_C_FLAGS " {flags}")' in toolchain
1805+
assert f'string(APPEND CONAN_SHARED_LINKER_FLAGS " {flags}")' in toolchain
1806+
assert f'string(APPEND CONAN_EXE_LINKER_FLAGS " {flags}")' in toolchain

test/integration/toolchains/gnu/test_autotoolstoolchain.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import os
22
import platform
33
import textwrap
4+
import pytest
45

56
from conan.test.assets.genconanfile import GenConanfile
67
from conan.test.utils.tools import TestClient
@@ -367,3 +368,40 @@ def test_conf_build_does_not_exist():
367368
tc = c.load("conanautotoolstoolchain.sh")
368369
assert 'export CC_FOR_BUILD="x86_64-linux-gnu-gcc"' in tc
369370
assert 'export CXX_FOR_BUILD="x86_64-linux-gnu-g++"' in tc
371+
372+
@pytest.mark.parametrize(
373+
"threads, flags",
374+
[("posix", "-pthread"), ("wasm_workers", "-sWASM_WORKERS=1")],
375+
)
376+
def test_thread_flags(threads, flags):
377+
os = platform.system()
378+
client = TestClient()
379+
profile = textwrap.dedent(f"""
380+
[settings]
381+
arch=wasm
382+
build_type=Release
383+
compiler=emcc
384+
compiler.cppstd=17
385+
compiler.threads={threads}
386+
compiler.libcxx=libc++
387+
compiler.version=4.0.10
388+
os=Emscripten
389+
""")
390+
client.save(
391+
{
392+
"conanfile.py": GenConanfile("pkg", "1.0")
393+
.with_settings("os", "arch", "compiler", "build_type")
394+
.with_generator("AutotoolsToolchain"),
395+
"profile": profile,
396+
}
397+
)
398+
client.run("install . -pr=./profile")
399+
toolchain = client.load("conanautotoolstoolchain{}".format('.bat' if os == "Windows" else '.sh'))
400+
if os == "Windows":
401+
assert f'set "CXXFLAGS=%CXXFLAGS% -stdlib=libc++ {flags}"' in toolchain
402+
assert f'set "CFLAGS=%CFLAGS% {flags}"' in toolchain
403+
assert f'set "LDFLAGS=%LDFLAGS% {flags}' in toolchain
404+
else:
405+
assert f'export CXXFLAGS="$CXXFLAGS -stdlib=libc++ {flags}"' in toolchain
406+
assert f'export CFLAGS="$CFLAGS {flags}"' in toolchain
407+
assert f'export LDFLAGS="$LDFLAGS {flags}"' in toolchain

test/integration/toolchains/gnu/test_gnutoolchain.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -592,3 +592,43 @@ def build(self):
592592
"conanfile.py": consumer
593593
})
594594
client.run("create . -pr:h host -pr:b build")
595+
596+
597+
@pytest.mark.parametrize(
598+
"threads, flags",
599+
[("posix", "-pthread"), ("wasm_workers", "-sWASM_WORKERS=1")],
600+
)
601+
def test_thread_flags(threads, flags):
602+
client = TestClient()
603+
profile = textwrap.dedent(f"""
604+
[settings]
605+
arch=wasm
606+
build_type=Release
607+
compiler=emcc
608+
compiler.cppstd=17
609+
compiler.threads={threads}
610+
compiler.libcxx=libc++
611+
compiler.version=4.0.10
612+
os=Emscripten
613+
""")
614+
client.save(
615+
{
616+
"conanfile.py": GenConanfile("pkg", "1.0")
617+
.with_settings("os", "arch", "compiler", "build_type")
618+
.with_generator("GnuToolchain"),
619+
"profile": profile,
620+
}
621+
)
622+
client.run("install . -pr=./profile")
623+
os = platform.system()
624+
toolchain = client.load(
625+
"conangnutoolchain{}".format('.bat' if os == "Windows" else '.sh'))
626+
627+
if os == "Windows":
628+
assert f'set "CXXFLAGS=%CXXFLAGS% -stdlib=libc++ {flags}"' in toolchain
629+
assert f'set "CFLAGS=%CFLAGS% {flags}"' in toolchain
630+
assert f'set "LDFLAGS=%LDFLAGS% {flags}' in toolchain
631+
else:
632+
assert f'export CXXFLAGS="$CXXFLAGS -stdlib=libc++ {flags}"' in toolchain
633+
assert f'export CFLAGS="$CFLAGS {flags}"' in toolchain
634+
assert f'export LDFLAGS="$LDFLAGS {flags}"' in toolchain

0 commit comments

Comments
 (0)