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

Fix typo where fPIC option is not templated, so it is always enabled #9752

Merged
Merged
Show file tree
Hide file tree
Changes from 8 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: 5 additions & 3 deletions conan/tools/cmake/toolchain.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,10 @@ def context(self):

class FPicBlock(Block):
template = textwrap.dedent("""
message(STATUS "Conan toolchain: Setting CMAKE_POSITION_INDEPENDENT_CODE=ON (options.fPIC)")
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
{% if fpic %}
message(STATUS "Conan toolchain: Setting CMAKE_POSITION_INDEPENDENT_CODE={{ fpic }} (options.fPIC)")
set(CMAKE_POSITION_INDEPENDENT_CODE {{ fpic }})
{% endif %}
""")

def context(self):
Expand All @@ -151,7 +153,7 @@ def context(self):
self._conanfile.output.warn("Toolchain: Ignoring fPIC option defined "
"for a shared library")
return None
return {"fpic": fpic}
return {"fpic": "ON" if fpic else "OFF"}


class GLibCXXBlock(Block):
Expand Down
54 changes: 54 additions & 0 deletions conans/test/unittests/tools/cmake/test_cmaketoolchain.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,13 @@ def test_cmake_toolchain(conanfile):
assert 'set(CMAKE_BUILD_TYPE "Release"' in content


def test_no_fpic_on_windows(conanfile):
conanfile.options.fPIC = True
toolchain = CMakeToolchain(conanfile)
content = toolchain.content
assert 'set(CMAKE_POSITION_INDEPENDENT_CODE' not in content


def test_remove(conanfile):
toolchain = CMakeToolchain(conanfile)
toolchain.blocks.remove("generic_system")
Expand Down Expand Up @@ -192,3 +199,50 @@ def test_toolset(conanfile_msvc):
assert 'Visual Studio 17 2022' in toolchain.generator
assert 'CMAKE_CXX_STANDARD 20' in toolchain.content


@pytest.fixture
def conanfile_linux():
c = ConanFile(Mock(), None)
c.settings = "os", "compiler", "build_type", "arch"
c.initialize(Settings({"os": ["Linux"],
"compiler": {"gcc": {"version": ["11"], "cppstd": ["20"]}},
"build_type": ["Release"],
"arch": ["x86_64"]}), EnvValues())
c.settings.build_type = "Release"
c.settings.arch = "x86_64"
c.settings.compiler = "gcc"
c.settings.compiler.version = "11"
c.settings.compiler.cppstd = "20"
c.settings.os = "Linux"
c.conf = Conf()
c.folders.set_base_generators(".")
c._conan_node = Mock()
c._conan_node.dependencies = []
return c


def test_no_fpic_when_not_an_option(conanfile_linux):
toolchain = CMakeToolchain(conanfile_linux)
content = toolchain.content
assert 'set(CMAKE_POSITION_INDEPENDENT_CODE' not in content


def test_no_fpic_when_shared(conanfile_linux):
conanfile_linux.options.shared = True
toolchain = CMakeToolchain(conanfile_linux)
content = toolchain.content
assert 'set(CMAKE_POSITION_INDEPENDENT_CODE' not in content


def test_fpic_disabled(conanfile_linux):
conanfile_linux.options.fPIC = False
toolchain = CMakeToolchain(conanfile_linux)
content = toolchain.content
assert 'set(CMAKE_POSITION_INDEPENDENT_CODE OFF' in content


def test_fpic_enabled(conanfile_linux):
conanfile_linux.options.fPIC = True
toolchain = CMakeToolchain(conanfile_linux)
content = toolchain.content
assert 'set(CMAKE_POSITION_INDEPENDENT_CODE ON' in content
1 change: 1 addition & 0 deletions contributors.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,4 @@ Many thanks to all of them!
- Ries, Uilian (uilianries@gmail.com, @uilianries)
- Sechet, Olivier (osechet@gmail.com)
- Sturm, Fabian (f@rtfs.org, @sturmf)
- Williams, Jordan (jordan@jwillikers.com)