Skip to content

libsodium: fix several regressions of #13792 #14998

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

Merged
merged 7 commits into from
Dec 31, 2022
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
193 changes: 84 additions & 109 deletions recipes/libsodium/all/conanfile.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
from conan import ConanFile
from conan.errors import ConanInvalidConfiguration, ConanException
from conan.errors import ConanInvalidConfiguration
from conan.tools.apple import fix_apple_shared_install_name
from conan.tools.env import VirtualBuildEnv
from conan.tools.files import export_conandata_patches, apply_conandata_patches, get, rmdir, copy, rm, replace_in_file
from conan.tools.files import apply_conandata_patches, copy, export_conandata_patches, get, replace_in_file, rm, rmdir
from conan.tools.gnu import Autotools, AutotoolsToolchain
from conan.tools.layout import basic_layout
from conan.tools.microsoft import is_msvc, is_msvc_static_runtime, MSBuildDeps, MSBuildToolchain, MSBuild, VCVars, unix_path, msvc_runtime_flag, vs_layout
from conan.tools.microsoft import is_msvc, is_msvc_static_runtime, MSBuild, MSBuildToolchain
import os

required_conan_version = ">=1.52.0"
required_conan_version = ">=1.54.0"


class LibsodiumConan(ConanFile):
Expand Down Expand Up @@ -49,24 +50,12 @@ def config_options(self):

def configure(self):
if self.options.shared:
try:
del self.options.fPIC
except Exception:
pass
try:
del self.settings.compiler.libcxx
except Exception:
pass
try:
del self.settings.compiler.cppstd
except Exception:
pass
self.options.rm_safe("fPIC")
self.settings.rm_safe("compiler.cppstd")
self.settings.rm_safe("compiler.libcxx")

def layout(self):
if is_msvc(self):
vs_layout(self)
else:
basic_layout(self, src_folder="src")
basic_layout(self, src_folder="src")

def validate(self):
if self.options.shared and is_msvc(self) and is_msvc_static_runtime(self):
Expand All @@ -81,121 +70,107 @@ def build_requirements(self):
if not self.conf.get("tools.microsoft.bash:path", check_type=str):
self.tool_requires("msys2/cci.latest")

def source(self):
get(self, **self.conan_data["sources"][self.version], strip_root=True)

def generate(self):
if is_msvc(self):
tc = MSBuildToolchain(self)
tc.generate()
tc = MSBuildDeps(self)
tc.generate()
tc = VCVars(self)
tc.generate()
else:
tc = AutotoolsToolchain(self)
env = VirtualBuildEnv(self)
env.generate()

tc = AutotoolsToolchain(self)
yes_no = lambda v: "yes" if v else "no"
tc.configure_args.append("--enable-soname-versions={}".format(yes_no(self.options.use_soname)))
tc.configure_args.append("--enable-pie={}".format(yes_no(self.options.PIE)))

env = tc.environment()

# if self._is_mingw:
# add libssp (gcc support library) for some missing symbols (e.g. __strcpy_chk)
# FIXME how do I do this in conan v2?
# autotools.libs.append("ssp")

if self._is_mingw:
tc.extra_ldflags.append("-lssp")
if self.settings.os == "Emscripten":
# FIXME: this is an old comment/test, has not been re-tested with conan2 upgrade
self.output.warn("os=Emscripten is not tested/supported by this recipe")
# FIXME: ./dist-build/emscripten.sh does not respect options of this recipe

tc.generate(env)

env = VirtualBuildEnv(self)
env.generate()

def source(self):
get(self, **self.conan_data["sources"][self.version], destination=self.source_folder, strip_root=True)
tc.generate()

@property
def _msvc_sln_folder(self):
if self.settings.compiler == "Visual Studio":
folder = {
sln_folders = {
"Visual Studio": {
"10": "vs2010",
"11": "vs2012",
"12": "vs2013",
"14": "vs2015",
"15": "vs2017",
"16": "vs2019",
}
elif self.settings.compiler == "msvc":
folder = {
},
"msvc": {
"170": "vs2012",
"180": "vs2013",
"190": "vs2015",
"191": "vs2017",
"192": "vs2019",
}
else:
raise ConanException("Should not call this function with any other compiler")

},
}
default_folder = "vs2019"
if self.version != "1.0.18":
if self.settings.compiler == "Visual Studio":
folder["17"] = "vs2022"
else:
folder["193"] = "vs2022"
sln_folders["Visual Studio"]["17"] = "vs2022"
sln_folders["msvc"]["193"] = "vs2022"
default_folder = "vs2022"

return folder.get(str(self.settings.compiler.version))
return sln_folders.get(str(self.settings.compiler), {}).get(str(self.settings.compiler.version), default_folder)

@property
def _msvc_platform(self):
return {
"x86": "Win32",
"x86_64": "x64",
}[str(self.settings.arch)]
def _build_msvc(self):
msvc_builds_folder = os.path.join(self.source_folder, "builds", "msvc")
msvc_sln_folder = os.path.join(msvc_builds_folder, self._msvc_sln_folder)
vcxproj_path = os.path.join(msvc_sln_folder, "libsodium", "libsodium.vcxproj")

# Method copied from xz_utils
def _fix_msvc_platform_toolset(self, vcxproj_file, old_toolset):
platform_toolset = {
"Visual Studio": {
"8": "v80",
"9": "v90",
"10": "v100",
"11": "v110",
"12": "v120",
"14": "v140",
"15": "v141",
"16": "v142",
"17": "v143",
},
"msvc": {
"170": "v110",
"180": "v120",
"190": "v140",
"191": "v141",
"192": "v142",
"193": "v143",
}
}.get(str(self.settings.compiler), {}).get(str(self.settings.compiler.version))
if not platform_toolset:
raise ConanException(
f"Unknown platform toolset for {self.settings.compiler} {self.settings.compiler.version}",
# 1.0.18 only supported up to vs2019. Add support for newer toolsets.
if self.version == "1.0.18" and self._msvc_sln_folder == "vs2019":
toolset = MSBuildToolchain(self).toolset
replace_in_file(
self, vcxproj_path,
"<PlatformToolset>v142</PlatformToolset>",
f"<PlatformToolset>{toolset}</PlatformToolset>",
)

# FIXME: There is currently no guarantee from new MSBuild helper that props files
# generated by MSBuildToolchain and MSBuildDeps have precedence over custom values of project.
# Therefore conantoolchain.props is injected manually before Microsoft.Cpp.Default.props like it was done
# with /p:ForceImportBeforeCppTargets in legacy MSBuild helper.
# see:
# - https://learn.microsoft.com/en-us/cpp/build/modify-project-properties-without-changing-project-file
# - https://github.com/conan-io/conan/issues/12155
conantoolchain_props = os.path.join(self.generators_folder, "conantoolchain.props")
replace_in_file(
self,
vcxproj_file,
f"<PlatformToolset>{old_toolset}</PlatformToolset>",
f"<PlatformToolset>{platform_toolset}</PlatformToolset>",
self, vcxproj_path,
"<Import Project=\"$(VCTargetsPath)\\Microsoft.Cpp.Default.props\" />",
f"<Import Project=\"{conantoolchain_props}\" />\n<Import Project=\"$(VCTargetsPath)\\Microsoft.Cpp.Default.props\" />",
)

def _build_msvc(self):
msvc_ver_subfolder = self._msvc_sln_folder or ("vs2022" if self.version != "1.0.18" else "vs2019")
msvc_sln_folder = os.path.join(self.build_folder, self.source_folder, "builds", "msvc", msvc_ver_subfolder)

msvc_sln_path = os.path.join(msvc_sln_folder, "libsodium.sln")

# 1.0.18 only supported up to vs2019. Add support for newer toolsets.
if self.version == "1.0.18" and msvc_ver_subfolder == "vs2019":
vcxproj_path = os.path.join(msvc_sln_folder, "libsodium", "libsodium.vcxproj")
self._fix_msvc_platform_toolset(vcxproj_path, "v142")
# Honor runtime library from profile
runtime_library = MSBuildToolchain(self).runtime_library
for prop_file, runtime_library_old in [
("DebugDEXE.props", "MultiThreadedDebugDLL"),
("DebugDLL.props", "MultiThreadedDebugDLL"),
("DebugLEXE.props", "MultiThreadedDebug"),
("DebugLIB.props", "MultiThreadedDebug"),
("DebugLTCG.props", "MultiThreadedDebug"),
("DebugSEXE.props", "MultiThreadedDebug"),
("ReleaseDEXE.props", "MultiThreadedDLL"),
("ReleaseDLL.props", "MultiThreadedDLL"),
("ReleaseLEXE.props", "MultiThreaded"),
("ReleaseLIB.props", "MultiThreaded"),
("ReleaseLTCG.props", "MultiThreaded"),
("ReleaseSEXE.props", "MultiThreaded"),
]:
replace_in_file(
self, os.path.join(msvc_builds_folder, "properties", prop_file),
f"<RuntimeLibrary>{runtime_library_old}</RuntimeLibrary>",
f"<RuntimeLibrary>{runtime_library}</RuntimeLibrary>",
)
Comment on lines +137 to +171
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've opened conan-io/conan#12817, I think it will avoid all this mess (which was unecessary with legacy MSBuild helper).


msbuild = MSBuild(self)
build_type = "{}{}".format(
"Dyn" if self.options.shared else "Static",
"Debug" if self.settings.build_type == "Debug" else "Release",
Expand All @@ -206,34 +181,34 @@ def _build_msvc(self):
"x86_64": "x64",
}[str(self.settings.arch)]

msbuild = MSBuild(self)
msbuild.build_type = build_type
msbuild.platform = platform
msbuild.build(msvc_sln_path)

msbuild.build(os.path.join(msvc_sln_folder, "libsodium.sln"))

def build(self):
apply_conandata_patches(self)
if is_msvc(self):
self._build_msvc()
else:
autotools = Autotools(self)
if self._is_mingw:
autotools.autoreconf()
autotools.configure()
autotools.make()

def package(self):
copy(self, "*LICENSE", src=self.source_folder, dst=os.path.join(self.package_folder, "licenses"), keep_path=False)
copy(self, "*LICENSE", src=self.source_folder, dst=os.path.join(self.package_folder, "licenses"))
if is_msvc(self):
copy(self, "*.lib", src=self.source_folder, dst=os.path.join(self.package_folder, "lib"), keep_path=False)
copy(self, "*.dll", src=self.source_folder, dst=os.path.join(self.package_folder, "bin"), keep_path=False)
inc_src = os.path.join(self.source_folder, "src", self.name, "include")
copy(self, "*.h", src=inc_src, dst=os.path.join(self.package_folder, "include"), keep_path=True, excludes=("*/private/*"))
inc_src = os.path.join(self.source_folder, "src", "libsodium", "include")
copy(self, "*.h", src=inc_src, dst=os.path.join(self.package_folder, "include"), excludes=("*/private/*"))
else:
autotools = Autotools(self)
# TODO: replace by autotools.install() once https://github.com/conan-io/conan/issues/12153 fixed
autotools.install(args=[f"DESTDIR={unix_path(self, self.package_folder)}"])
autotools.install()
rmdir(self, os.path.join(self.package_folder, "lib", "pkgconfig"))
rm(self, "*.la", os.path.join(self.package_folder, "lib"))
fix_apple_shared_install_name(self)

def package_info(self):
self.cpp_info.set_property("pkg_config_name", "libsodium")
Expand Down
2 changes: 1 addition & 1 deletion recipes/libsodium/all/test_package/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
cmake_minimum_required(VERSION 3.1)
project(test_package C)
project(test_package LANGUAGES C)

find_package(libsodium REQUIRED CONFIG)

Expand Down
8 changes: 4 additions & 4 deletions recipes/libsodium/all/test_v1_package/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
cmake_minimum_required(VERSION 3.1)
project(test_package C)
project(test_package)

include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
conan_basic_setup()
conan_basic_setup(TARGETS)

add_executable(${PROJECT_NAME} ../test_package/test_package.c)
target_link_libraries(${PROJECT_NAME} ${CONAN_LIBS})
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/../test_package
${CMAKE_CURRENT_BINARY_DIR}/test_package)
4 changes: 2 additions & 2 deletions recipes/libsodium/all/test_v1_package/conanfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@


class TestPackageConan(ConanFile):
settings = "os", "compiler", "arch", "build_type"
generators = "cmake"
settings = "os", "arch", "compiler", "build_type"
generators = "cmake", "cmake_find_package_multi"

def build(self):
cmake = CMake(self)
Expand Down