Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add extra flags to CMakeToolchain #14966

Merged
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
8 changes: 4 additions & 4 deletions conan/tools/cmake/toolchain/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -565,10 +565,10 @@ class ExtraFlagsBlock(Block):

def context(self):
# Now, it's time to get all the flags defined by the user
cxxflags = self._conanfile.conf.get("tools.build:cxxflags", default=[], check_type=list)
cflags = self._conanfile.conf.get("tools.build:cflags", default=[], check_type=list)
sharedlinkflags = self._conanfile.conf.get("tools.build:sharedlinkflags", default=[], check_type=list)
exelinkflags = self._conanfile.conf.get("tools.build:exelinkflags", default=[], check_type=list)
cxxflags = self._toolchain.extra_cxxflags + self._conanfile.conf.get("tools.build:cxxflags", default=[], check_type=list)
cflags = self._toolchain.extra_cflags + self._conanfile.conf.get("tools.build:cflags", default=[], check_type=list)
sharedlinkflags = self._toolchain.extra_sharedlinkflags + self._conanfile.conf.get("tools.build:sharedlinkflags", default=[], check_type=list)
exelinkflags = self._toolchain.extra_exelinkflags + self._conanfile.conf.get("tools.build:exelinkflags", default=[], check_type=list)
defines = self._conanfile.conf.get("tools.build:defines", default=[], check_type=list)

# See https://github.com/conan-io/conan/issues/13374
Expand Down
5 changes: 5 additions & 0 deletions conan/tools/cmake/toolchain/toolchain.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,11 @@ def __init__(self, conanfile, generator=None):
self.cache_variables = {}
self.preprocessor_definitions = Variables()

self.extra_cxxflags = []
self.extra_cflags = []
self.extra_sharedlinkflags = []
self.extra_exelinkflags = []

self.blocks = ToolchainBlocks(self._conanfile, self,
[("user_toolchain", UserToolchain),
("generic_system", GenericSystemBlock),
Expand Down
36 changes: 36 additions & 0 deletions conans/test/integration/toolchains/cmake/test_cmaketoolchain.py
Original file line number Diff line number Diff line change
Expand Up @@ -1140,3 +1140,39 @@ def layout(self):
presets = load(os.path.join(build_folder,
"build/windows-shared/Debug/generators/CMakePresets.json"))
assert "conan-windows-shared-debug" in presets


def test_extra_flags():
client = TestClient()
conanfile = textwrap.dedent("""
from conan import ConanFile
from conan.tools.cmake import CMakeToolchain

class Conan(ConanFile):
name = "pkg"
version = "0.1"
settings = "os", "arch", "build_type"
def generate(self):
tc = CMakeToolchain(self)
tc.extra_cxxflags = ["extra_cxxflags"]
tc.extra_cflags = ["extra_cflags"]
tc.extra_sharedlinkflags = ["extra_sharedlinkflags"]
tc.extra_exelinkflags = ["extra_exelinkflags"]
tc.generate()
""")
profile = textwrap.dedent("""
include(default)
[conf]
tools.build:cxxflags+=['cxxflags']
tools.build:cflags+=['cflags']
tools.build:sharedlinkflags+=['sharedlinkflags']
tools.build:exelinkflags+=['exelinkflags']
""")
client.save({"conanfile.py": conanfile, "profile": profile})
client.run('install . -pr=./profile')
toolchain = client.load("conan_toolchain.cmake")

assert 'string(APPEND CONAN_CXX_FLAGS " extra_cxxflags cxxflags")' in toolchain
assert 'string(APPEND CONAN_C_FLAGS " extra_cflags cflags")' in toolchain
assert 'string(APPEND CONAN_SHARED_LINKER_FLAGS " extra_sharedlinkflags sharedlinkflags")' in toolchain
assert 'string(APPEND CONAN_EXE_LINKER_FLAGS " extra_exelinkflags exelinkflags")' in toolchain