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

NMakeDeps: inject defines & system_libs of dependencies #12944

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
12 changes: 12 additions & 0 deletions conan/tools/microsoft/nmakedeps.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,23 @@ def format_lib(lib):
ret.extend(cpp_info.exelinkflags or [])
ret.extend(cpp_info.sharedlinkflags or [])
ret.extend([format_lib(lib) for lib in cpp_info.libs or []])
ret.extend([format_lib(lib) for lib in cpp_info.system_libs or []])
link_args = " ".join(ret)

def format_define(define):
if "=" in define:
# CL env-var can't accept '=' sign in /D option, it can be replaced by '#' sign:
# https://learn.microsoft.com/en-us/cpp/build/reference/cl-environment-variables
macro, value = define.split("=", 1)
if value and not value.isnumeric():
value = f'\\"{value}\\"'
define = f"{macro}#{value}"
return f"/D{define}"

cl_flags = [f'-I"{p}"' for p in cpp_info.includedirs or []]
cl_flags.extend(cpp_info.cflags or [])
cl_flags.extend(cpp_info.cxxflags or [])
cl_flags.extend([format_define(define) for define in cpp_info.defines or []])

env = Environment()
env.append("CL", " ".join(cl_flags))
Expand Down
46 changes: 46 additions & 0 deletions conans/test/integration/toolchains/microsoft/test_nmakedeps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import platform
import re
import textwrap

import pytest

from conans.test.utils.tools import TestClient


@pytest.mark.skipif(platform.system() != "Windows", reason="Only for windows")
def test_nmakedeps():
client = TestClient()
conanfile = textwrap.dedent("""
from conan import ConanFile
class Pkg(ConanFile):
settings = "os", "arch", "compiler", "build_type"
name = "test-nmakedeps"
version = "1.0"

def package_info(self):
self.cpp_info.components["pkg-1"].libs = ["pkg-1"]
self.cpp_info.components["pkg-1"].defines = ["TEST_DEFINITION1"]
self.cpp_info.components["pkg-1"].system_libs = ["ws2_32"]
self.cpp_info.components["pkg-2"].libs = ["pkg-2"]
self.cpp_info.components["pkg-2"].defines = ["TEST_DEFINITION2=0"]
self.cpp_info.components["pkg-2"].requires = ["pkg-1"]
self.cpp_info.components["pkg-3"].libs = ["pkg-3"]
self.cpp_info.components["pkg-3"].defines = ["TEST_DEFINITION3="]
self.cpp_info.components["pkg-3"].requires = ["pkg-1", "pkg-2"]
self.cpp_info.components["pkg-4"].libs = ["pkg-4"]
self.cpp_info.components["pkg-4"].defines = ["TEST_DEFINITION4=foo"]
""")
client.save({"conanfile.py": conanfile})
client.run("create . -s arch=x86_64")
client.run("install test-nmakedeps/1.0@ -g NMakeDeps -s build_type=Release -s arch=x86_64")
# Checking that NMakeDeps builds correctly .bat file
bat_file = client.load("conannmakedeps.bat")
# Checking that defines are added to CL
for flag in (
r"/DTEST_DEFINITION1", r"/DTEST_DEFINITION2#0",
r"/DTEST_DEFINITION3#", r'/DTEST_DEFINITION4#\\"foo\\"',
):
assert re.search(fr'set "CL=%CL%.*\s{flag}(?:\s|")', bat_file)
# Checking that libs and system libs are added to _LINK_
for flag in (r"pkg-1\.lib", r"pkg-2\.lib", r"pkg-3\.lib", r"pkg-4\.lib", r"ws2_32\.lib"):
assert re.search(fr'set "_LINK_=%_LINK_%.*\s{flag}(?:\s|")', bat_file)