Skip to content

Commit 54e099f

Browse files
SpaceImAbrilRBS
authored andcommitted
(conan-io#14998) libsodium: fix several regressions of conan-io#13792
* rely on similar CMakeLists in test v1 package & test package * modernize more * simplify msvc build logic * properly inject props files generated by MSBuildToolchain * relocatable shared lib on macOS * restore autoreconf call if mingw * honor runtime from profile
1 parent 417304c commit 54e099f

File tree

4 files changed

+91
-116
lines changed

4 files changed

+91
-116
lines changed

recipes/libsodium/all/conanfile.py

Lines changed: 84 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
from conan import ConanFile
2-
from conan.errors import ConanInvalidConfiguration, ConanException
2+
from conan.errors import ConanInvalidConfiguration
3+
from conan.tools.apple import fix_apple_shared_install_name
34
from conan.tools.env import VirtualBuildEnv
4-
from conan.tools.files import export_conandata_patches, apply_conandata_patches, get, rmdir, copy, rm, replace_in_file
5+
from conan.tools.files import apply_conandata_patches, copy, export_conandata_patches, get, replace_in_file, rm, rmdir
56
from conan.tools.gnu import Autotools, AutotoolsToolchain
67
from conan.tools.layout import basic_layout
7-
from conan.tools.microsoft import is_msvc, is_msvc_static_runtime, MSBuildDeps, MSBuildToolchain, MSBuild, VCVars, unix_path, msvc_runtime_flag, vs_layout
8+
from conan.tools.microsoft import is_msvc, is_msvc_static_runtime, MSBuild, MSBuildToolchain
89
import os
910

10-
required_conan_version = ">=1.52.0"
11+
required_conan_version = ">=1.54.0"
1112

1213

1314
class LibsodiumConan(ConanFile):
@@ -49,24 +50,12 @@ def config_options(self):
4950

5051
def configure(self):
5152
if self.options.shared:
52-
try:
53-
del self.options.fPIC
54-
except Exception:
55-
pass
56-
try:
57-
del self.settings.compiler.libcxx
58-
except Exception:
59-
pass
60-
try:
61-
del self.settings.compiler.cppstd
62-
except Exception:
63-
pass
53+
self.options.rm_safe("fPIC")
54+
self.settings.rm_safe("compiler.cppstd")
55+
self.settings.rm_safe("compiler.libcxx")
6456

6557
def layout(self):
66-
if is_msvc(self):
67-
vs_layout(self)
68-
else:
69-
basic_layout(self, src_folder="src")
58+
basic_layout(self, src_folder="src")
7059

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

73+
def source(self):
74+
get(self, **self.conan_data["sources"][self.version], strip_root=True)
75+
8476
def generate(self):
8577
if is_msvc(self):
8678
tc = MSBuildToolchain(self)
8779
tc.generate()
88-
tc = MSBuildDeps(self)
89-
tc.generate()
90-
tc = VCVars(self)
91-
tc.generate()
9280
else:
93-
tc = AutotoolsToolchain(self)
81+
env = VirtualBuildEnv(self)
82+
env.generate()
9483

84+
tc = AutotoolsToolchain(self)
9585
yes_no = lambda v: "yes" if v else "no"
9686
tc.configure_args.append("--enable-soname-versions={}".format(yes_no(self.options.use_soname)))
9787
tc.configure_args.append("--enable-pie={}".format(yes_no(self.options.PIE)))
98-
99-
env = tc.environment()
100-
101-
# if self._is_mingw:
102-
# add libssp (gcc support library) for some missing symbols (e.g. __strcpy_chk)
103-
# FIXME how do I do this in conan v2?
104-
# autotools.libs.append("ssp")
105-
88+
if self._is_mingw:
89+
tc.extra_ldflags.append("-lssp")
10690
if self.settings.os == "Emscripten":
10791
# FIXME: this is an old comment/test, has not been re-tested with conan2 upgrade
10892
self.output.warn("os=Emscripten is not tested/supported by this recipe")
10993
# FIXME: ./dist-build/emscripten.sh does not respect options of this recipe
110-
111-
tc.generate(env)
112-
113-
env = VirtualBuildEnv(self)
114-
env.generate()
115-
116-
def source(self):
117-
get(self, **self.conan_data["sources"][self.version], destination=self.source_folder, strip_root=True)
94+
tc.generate()
11895

11996
@property
12097
def _msvc_sln_folder(self):
121-
if self.settings.compiler == "Visual Studio":
122-
folder = {
98+
sln_folders = {
99+
"Visual Studio": {
123100
"10": "vs2010",
124101
"11": "vs2012",
125102
"12": "vs2013",
126103
"14": "vs2015",
127104
"15": "vs2017",
128105
"16": "vs2019",
129-
}
130-
elif self.settings.compiler == "msvc":
131-
folder = {
106+
},
107+
"msvc": {
108+
"170": "vs2012",
109+
"180": "vs2013",
132110
"190": "vs2015",
133111
"191": "vs2017",
134112
"192": "vs2019",
135-
}
136-
else:
137-
raise ConanException("Should not call this function with any other compiler")
138-
113+
},
114+
}
115+
default_folder = "vs2019"
139116
if self.version != "1.0.18":
140-
if self.settings.compiler == "Visual Studio":
141-
folder["17"] = "vs2022"
142-
else:
143-
folder["193"] = "vs2022"
117+
sln_folders["Visual Studio"]["17"] = "vs2022"
118+
sln_folders["msvc"]["193"] = "vs2022"
119+
default_folder = "vs2022"
144120

145-
return folder.get(str(self.settings.compiler.version))
121+
return sln_folders.get(str(self.settings.compiler), {}).get(str(self.settings.compiler.version), default_folder)
146122

147-
@property
148-
def _msvc_platform(self):
149-
return {
150-
"x86": "Win32",
151-
"x86_64": "x64",
152-
}[str(self.settings.arch)]
123+
def _build_msvc(self):
124+
msvc_builds_folder = os.path.join(self.source_folder, "builds", "msvc")
125+
msvc_sln_folder = os.path.join(msvc_builds_folder, self._msvc_sln_folder)
126+
vcxproj_path = os.path.join(msvc_sln_folder, "libsodium", "libsodium.vcxproj")
153127

154-
# Method copied from xz_utils
155-
def _fix_msvc_platform_toolset(self, vcxproj_file, old_toolset):
156-
platform_toolset = {
157-
"Visual Studio": {
158-
"8": "v80",
159-
"9": "v90",
160-
"10": "v100",
161-
"11": "v110",
162-
"12": "v120",
163-
"14": "v140",
164-
"15": "v141",
165-
"16": "v142",
166-
"17": "v143",
167-
},
168-
"msvc": {
169-
"170": "v110",
170-
"180": "v120",
171-
"190": "v140",
172-
"191": "v141",
173-
"192": "v142",
174-
"193": "v143",
175-
}
176-
}.get(str(self.settings.compiler), {}).get(str(self.settings.compiler.version))
177-
if not platform_toolset:
178-
raise ConanException(
179-
f"Unknown platform toolset for {self.settings.compiler} {self.settings.compiler.version}",
128+
# 1.0.18 only supported up to vs2019. Add support for newer toolsets.
129+
if self.version == "1.0.18" and self._msvc_sln_folder == "vs2019":
130+
toolset = MSBuildToolchain(self).toolset
131+
replace_in_file(
132+
self, vcxproj_path,
133+
"<PlatformToolset>v142</PlatformToolset>",
134+
f"<PlatformToolset>{toolset}</PlatformToolset>",
180135
)
136+
137+
# FIXME: There is currently no guarantee from new MSBuild helper that props files
138+
# generated by MSBuildToolchain and MSBuildDeps have precedence over custom values of project.
139+
# Therefore conantoolchain.props is injected manually before Microsoft.Cpp.Default.props like it was done
140+
# with /p:ForceImportBeforeCppTargets in legacy MSBuild helper.
141+
# see:
142+
# - https://learn.microsoft.com/en-us/cpp/build/modify-project-properties-without-changing-project-file
143+
# - https://github.com/conan-io/conan/issues/12155
144+
conantoolchain_props = os.path.join(self.generators_folder, "conantoolchain.props")
181145
replace_in_file(
182-
self,
183-
vcxproj_file,
184-
f"<PlatformToolset>{old_toolset}</PlatformToolset>",
185-
f"<PlatformToolset>{platform_toolset}</PlatformToolset>",
146+
self, vcxproj_path,
147+
"<Import Project=\"$(VCTargetsPath)\\Microsoft.Cpp.Default.props\" />",
148+
f"<Import Project=\"{conantoolchain_props}\" />\n<Import Project=\"$(VCTargetsPath)\\Microsoft.Cpp.Default.props\" />",
186149
)
187150

188-
def _build_msvc(self):
189-
msvc_ver_subfolder = self._msvc_sln_folder or ("vs2022" if self.version != "1.0.18" else "vs2019")
190-
msvc_sln_folder = os.path.join(self.build_folder, self.source_folder, "builds", "msvc", msvc_ver_subfolder)
191-
192-
msvc_sln_path = os.path.join(msvc_sln_folder, "libsodium.sln")
193-
194-
# 1.0.18 only supported up to vs2019. Add support for newer toolsets.
195-
if self.version == "1.0.18" and msvc_ver_subfolder == "vs2019":
196-
vcxproj_path = os.path.join(msvc_sln_folder, "libsodium", "libsodium.vcxproj")
197-
self._fix_msvc_platform_toolset(vcxproj_path, "v142")
151+
# Honor runtime library from profile
152+
runtime_library = MSBuildToolchain(self).runtime_library
153+
for prop_file, runtime_library_old in [
154+
("DebugDEXE.props", "MultiThreadedDebugDLL"),
155+
("DebugDLL.props", "MultiThreadedDebugDLL"),
156+
("DebugLEXE.props", "MultiThreadedDebug"),
157+
("DebugLIB.props", "MultiThreadedDebug"),
158+
("DebugLTCG.props", "MultiThreadedDebug"),
159+
("DebugSEXE.props", "MultiThreadedDebug"),
160+
("ReleaseDEXE.props", "MultiThreadedDLL"),
161+
("ReleaseDLL.props", "MultiThreadedDLL"),
162+
("ReleaseLEXE.props", "MultiThreaded"),
163+
("ReleaseLIB.props", "MultiThreaded"),
164+
("ReleaseLTCG.props", "MultiThreaded"),
165+
("ReleaseSEXE.props", "MultiThreaded"),
166+
]:
167+
replace_in_file(
168+
self, os.path.join(msvc_builds_folder, "properties", prop_file),
169+
f"<RuntimeLibrary>{runtime_library_old}</RuntimeLibrary>",
170+
f"<RuntimeLibrary>{runtime_library}</RuntimeLibrary>",
171+
)
198172

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

209-
msbuild = MSBuild(self)
210184
msbuild.build_type = build_type
211185
msbuild.platform = platform
212-
msbuild.build(msvc_sln_path)
213-
186+
msbuild.build(os.path.join(msvc_sln_folder, "libsodium.sln"))
214187

215188
def build(self):
216189
apply_conandata_patches(self)
217190
if is_msvc(self):
218191
self._build_msvc()
219192
else:
220193
autotools = Autotools(self)
194+
if self._is_mingw:
195+
autotools.autoreconf()
221196
autotools.configure()
222197
autotools.make()
223198

224199
def package(self):
225-
copy(self, "*LICENSE", src=self.source_folder, dst=os.path.join(self.package_folder, "licenses"), keep_path=False)
200+
copy(self, "*LICENSE", src=self.source_folder, dst=os.path.join(self.package_folder, "licenses"))
226201
if is_msvc(self):
227202
copy(self, "*.lib", src=self.source_folder, dst=os.path.join(self.package_folder, "lib"), keep_path=False)
228203
copy(self, "*.dll", src=self.source_folder, dst=os.path.join(self.package_folder, "bin"), keep_path=False)
229-
inc_src = os.path.join(self.source_folder, "src", self.name, "include")
230-
copy(self, "*.h", src=inc_src, dst=os.path.join(self.package_folder, "include"), keep_path=True, excludes=("*/private/*"))
204+
inc_src = os.path.join(self.source_folder, "src", "libsodium", "include")
205+
copy(self, "*.h", src=inc_src, dst=os.path.join(self.package_folder, "include"), excludes=("*/private/*"))
231206
else:
232207
autotools = Autotools(self)
233-
# TODO: replace by autotools.install() once https://github.com/conan-io/conan/issues/12153 fixed
234-
autotools.install(args=[f"DESTDIR={unix_path(self, self.package_folder)}"])
208+
autotools.install()
235209
rmdir(self, os.path.join(self.package_folder, "lib", "pkgconfig"))
236210
rm(self, "*.la", os.path.join(self.package_folder, "lib"))
211+
fix_apple_shared_install_name(self)
237212

238213
def package_info(self):
239214
self.cpp_info.set_property("pkg_config_name", "libsodium")

recipes/libsodium/all/test_package/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
cmake_minimum_required(VERSION 3.1)
2-
project(test_package C)
2+
project(test_package LANGUAGES C)
33

44
find_package(libsodium REQUIRED CONFIG)
55

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
cmake_minimum_required(VERSION 3.1)
2-
project(test_package C)
2+
project(test_package)
33

44
include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
5-
conan_basic_setup()
5+
conan_basic_setup(TARGETS)
66

7-
add_executable(${PROJECT_NAME} ../test_package/test_package.c)
8-
target_link_libraries(${PROJECT_NAME} ${CONAN_LIBS})
7+
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/../test_package
8+
${CMAKE_CURRENT_BINARY_DIR}/test_package)

recipes/libsodium/all/test_v1_package/conanfile.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33

44

55
class TestPackageConan(ConanFile):
6-
settings = "os", "compiler", "arch", "build_type"
7-
generators = "cmake"
6+
settings = "os", "arch", "compiler", "build_type"
7+
generators = "cmake", "cmake_find_package_multi"
88

99
def build(self):
1010
cmake = CMake(self)

0 commit comments

Comments
 (0)