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

Use self.cpp_info.libdirs as folders for tools.collect_libs() #3503

Merged
merged 2 commits into from
Sep 25, 2018
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
34 changes: 22 additions & 12 deletions conans/client/tools/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,21 +251,31 @@ def _path_equals(path1, path2):
return path1 == path2


def collect_libs(conanfile, folder="lib"):
def collect_libs(conanfile, folder=None):
if not conanfile.package_folder:
return []
lib_folder = os.path.join(conanfile.package_folder, folder)
if not os.path.exists(lib_folder):
conanfile.output.warn("Lib folder doesn't exist, can't collect libraries: {0}".format(lib_folder))
return []
files = os.listdir(lib_folder)
if folder:
lib_folders = [os.path.join(conanfile.package_folder, folder)]
else:
lib_folders = [os.path.join(conanfile.package_folder, folder)
for folder in conanfile.cpp_info.libdirs]
result = []
for f in files:
name, ext = os.path.splitext(f)
if ext in (".so", ".lib", ".a", ".dylib"):
if ext != ".lib" and name.startswith("lib"):
name = name[3:]
result.append(name)
for lib_folder in lib_folders:
if not os.path.exists(lib_folder):
conanfile.output.warn("Lib folder doesn't exist, can't collect libraries: "
"{0}".format(lib_folder))
continue
files = os.listdir(lib_folder)
for f in files:
name, ext = os.path.splitext(f)
if ext in (".so", ".lib", ".a", ".dylib"):
if ext != ".lib" and name.startswith("lib"):
name = name[3:]
if name in result:
conanfile.output.warn("Library '%s' already found in a previous "
"'conanfile.cpp_info.libdirs' folder" % name)
else:
result.append(name)
return result


Expand Down
67 changes: 66 additions & 1 deletion conans/test/util/tools_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@
from conans.client.tools.scm import Git

from conans.errors import ConanException, NotFoundException
from conans.model.build_info import CppInfo
from conans.model.settings import Settings
from conans.test.build_helpers.cmake_test import ConanFileMock

from conans.test.utils.runner import TestRunner
from conans.test.utils.test_files import temp_folder
Expand All @@ -31,7 +33,7 @@
from conans.tools import which
from conans.tools import OSInfo, SystemPackageTool, replace_in_file, AptTool, ChocolateyTool,\
set_global_instances
from conans.util.files import save, load, md5
from conans.util.files import save, load, md5, mkdir
import requests

from nose.plugins.attrib import attr
Expand Down Expand Up @@ -1383,3 +1385,66 @@ def build(self):
"""
client.save({"conanfile.py": conanfile, "file.txt": "hello\r\n"})
client.run("create . user/channel")

def collect_libs_test(self):
conanfile = ConanFileMock()
# Without package_folder
conanfile.package_folder = None
result = tools.collect_libs(conanfile)
self.assertEqual([], result)

# Default behavior
conanfile.package_folder = temp_folder()
mylib_path = os.path.join(conanfile.package_folder, "lib", "mylib.lib")
save(mylib_path, "")
conanfile.cpp_info = CppInfo("")
result = tools.collect_libs(conanfile)
self.assertEqual(["mylib"], result)

# Custom folder
customlib_path = os.path.join(conanfile.package_folder, "custom_folder", "customlib.lib")
save(customlib_path, "")
result = tools.collect_libs(conanfile, folder="custom_folder")
self.assertEqual(["customlib"], result)

# Custom folder doesn't exist
result = tools.collect_libs(conanfile, folder="fake_folder")
self.assertEqual([], result)
self.assertIn("Lib folder doesn't exist, can't collect libraries:", conanfile.output)

# Use cpp_info.libdirs
conanfile.cpp_info.libdirs = ["lib", "custom_folder"]
result = tools.collect_libs(conanfile)
self.assertEqual(["mylib", "customlib"], result)

# Custom folder with multiple libdirs should only collect from custom folder
self.assertEqual(["lib", "custom_folder"], conanfile.cpp_info.libdirs)
result = tools.collect_libs(conanfile, folder="custom_folder")
self.assertEqual(["customlib"], result)

# Warn same lib different folders
conanfile = ConanFileMock()
conanfile.package_folder = temp_folder()
conanfile.cpp_info = CppInfo("")
custom_mylib_path = os.path.join(conanfile.package_folder, "custom_folder", "mylib.lib")
lib_mylib_path = os.path.join(conanfile.package_folder, "lib", "mylib.lib")
save(custom_mylib_path, "")
save(lib_mylib_path, "")
conanfile.cpp_info.libdirs = ["lib", "custom_folder"]
result = tools.collect_libs(conanfile)
self.assertEqual(["mylib"], result)
self.assertIn("Library 'mylib' already found in a previous 'conanfile.cpp_info.libdirs' "
"folder", conanfile.output)

# Warn lib folder does not exist with correct result
conanfile = ConanFileMock()
conanfile.package_folder = temp_folder()
conanfile.cpp_info = CppInfo("")
lib_mylib_path = os.path.join(conanfile.package_folder, "lib", "mylib.lib")
save(lib_mylib_path, "")
no_folder_path = os.path.join(conanfile.package_folder, "no_folder")
conanfile.cpp_info.libdirs = ["no_folder", "lib"] # 'no_folder' does NOT exist
result = tools.collect_libs(conanfile)
self.assertEqual(["mylib"], result)
self.assertIn("WARN: Lib folder doesn't exist, can't collect libraries: %s"
% no_folder_path, conanfile.output)