-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
Copy pathconanfile.py
194 lines (164 loc) · 8.62 KB
/
conanfile.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
from conan import ConanFile
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 apply_conandata_patches, chdir, copy, export_conandata_patches, get, load, replace_in_file, rm, rmdir, save
from conan.tools.gnu import Autotools, AutotoolsToolchain
from conan.tools.layout import basic_layout
from conan.tools.microsoft import MSBuild, MSBuildToolchain
import os
import re
required_conan_version = ">=1.55.0"
class LibjpegConan(ConanFile):
name = "libjpeg"
description = "Libjpeg is a widely used C library for reading and writing JPEG image files."
url = "https://github.com/conan-io/conan-center-index"
topics = ("image", "format", "jpg", "jpeg", "picture", "multimedia", "graphics")
license = "IJG"
homepage = "http://ijg.org"
settings = "os", "arch", "compiler", "build_type"
options = {
"shared": [True, False],
"fPIC": [True, False],
}
default_options = {
"shared": False,
"fPIC": True,
}
@property
def _is_cl_like(self):
return self.settings.compiler.get_safe("runtime") is not None
@property
def _settings_build(self):
return getattr(self, "settings_build", self.settings)
def export_sources(self):
export_conandata_patches(self)
def config_options(self):
if self.settings.os == "Windows":
del self.options.fPIC
def configure(self):
if self.options.shared:
self.options.rm_safe("fPIC")
self.settings.rm_safe("compiler.cppstd")
self.settings.rm_safe("compiler.libcxx")
def validate(self):
if self.version == "9d" and self.settings.os == "Windows" and self.settings.arch == "armv8":
raise ConanInvalidConfiguration("This version of libjpeg does not support ARM64, please use a newer version")
def layout(self):
basic_layout(self, src_folder="src")
def build_requirements(self):
if self._settings_build.os == "Windows" and not self._is_cl_like:
self.win_bash = True
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 self._is_cl_like:
tc = MSBuildToolchain(self)
tc.cflags.append("-DLIBJPEG_BUILDING")
if not self.options.shared:
tc.cflags.append(" -DLIBJPEG_STATIC")
tc.generate()
else:
env = VirtualBuildEnv(self)
env.generate()
tc = AutotoolsToolchain(self)
tc.extra_defines.append("LIBJPEG_BUILDING")
tc.generate()
def build(self):
apply_conandata_patches(self)
if self._is_cl_like:
with chdir(self, self.source_folder):
self.run("nmake /f makefile.vs setupcopy-v16")
# Rename target to 'libjpeg.lib' to match legacy behaviour (otherwise we break backwards compatibility)
# static: "libjpeg.lib"
# shared: "libjpeg.lib" (import), "libjpeg-9.dll" (DLL)
jpeg_vcxproj = os.path.join(self.source_folder, "jpeg.vcxproj")
target_name = "libjpeg-9" if self.options.shared else "libjpeg"
replace_in_file(self, jpeg_vcxproj, """<PropertyGroup Label="UserMacros" />""",
f""" <PropertyGroup Label="UserMacros" /><PropertyGroup Label="TargetName"> <TargetName>{target_name}</TargetName></PropertyGroup>
""")
if self.options.shared:
replace_in_file(self, jpeg_vcxproj, "</SubSystem>",
"</SubSystem><ImportLibrary>$(OutDir)libjpeg.lib</ImportLibrary>")
# Support static/shared
if self.options.shared:
replace_in_file(self, jpeg_vcxproj,
"<ConfigurationType>StaticLibrary</ConfigurationType>",
"<ConfigurationType>DynamicLibrary</ConfigurationType>"
)
# Don't force LTO
replace_in_file(self, jpeg_vcxproj, "<WholeProgramOptimization>true</WholeProgramOptimization>", "")
# Inject conan-generated .props file
# Note: importing it right before Microsoft.Cpp.props also ensures we correctly
# handle the toolset setting
conantoolchain_props = os.path.join(self.generators_folder, MSBuildToolchain.filename)
replace_in_file(
self, jpeg_vcxproj,
"""<Import Project="$(VCTargetsPath)\\Microsoft.Cpp.props" />""",
f"""<Import Project="{conantoolchain_props}" /><Import Project="$(VCTargetsPath)\\Microsoft.Cpp.props" />""",
)
# Patch settings for a different build type
if self.settings.build_type is not "Release":
replacements = {
"Release": str(self.settings.build_type)
}
if self.settings.build_type == "Debug":
replacements.update({
"<Optimization>Full": "<Optimization>Disabled",
"NDEBUG;": "_DEBUG;",
})
for key, value in replacements.items():
replace_in_file(self, jpeg_vcxproj, key, value)
replace_in_file(self, os.path.join(self.source_folder, "jpeg.sln"), "Release", str(self.settings.build_type))
msbuild = MSBuild(self)
if self.settings.arch == "x86":
# This .sln uses "Win32" instead of the usual "x86"
# as the solution platform, so need to override this
msbuild.platform = "Win32"
msbuild.build(sln="jpeg.sln")
else:
autotools = Autotools(self)
autotools.configure()
autotools.make()
def package(self):
copy(self, "README", src=self.source_folder, dst=os.path.join(self.package_folder, "licenses"))
if self._is_cl_like:
for filename in ["jpeglib.h", "jerror.h", "jconfig.h", "jmorecfg.h"]:
copy(self, filename, src=self.source_folder, dst=os.path.join(self.package_folder, "include"), keep_path=False)
copy(self, "*.lib", src=self.source_folder, dst=os.path.join(self.package_folder, "lib"), keep_path=False)
if self.options.shared:
copy(self, "*.dll", src=self.source_folder, dst=os.path.join(self.package_folder, "bin"), keep_path=False)
else:
autotools = Autotools(self)
autotools.install()
if self.settings.os == "Windows" and self.options.shared:
rm(self, "*[!.dll]", os.path.join(self.package_folder, "bin"))
else:
rmdir(self, os.path.join(self.package_folder, "bin"))
rm(self, "*.la", os.path.join(self.package_folder, "lib"))
rmdir(self, os.path.join(self.package_folder, "lib", "pkgconfig"))
rmdir(self, os.path.join(self.package_folder, "share"))
fix_apple_shared_install_name(self)
for fn in ("jpegint.h", "transupp.h",):
copy(self, fn, src=self.source_folder, dst=os.path.join(self.package_folder, "include"))
for fn in ("jinclude.h", "transupp.c",):
copy(self, fn, src=self.source_folder, dst=os.path.join(self.package_folder, "res"))
# Remove export decorations of transupp symbols
for relpath in os.path.join("include", "transupp.h"), os.path.join("res", "transupp.c"):
path = os.path.join(self.package_folder, relpath)
save(self, path, re.subn(r"(?:EXTERN|GLOBAL)\(([^)]+)\)", r"\1", load(self, path))[0])
def package_info(self):
self.cpp_info.set_property("cmake_find_mode", "both")
self.cpp_info.set_property("cmake_file_name", "JPEG")
self.cpp_info.set_property("cmake_target_name", "JPEG::JPEG")
self.cpp_info.set_property("pkg_config_name", "libjpeg")
prefix = "lib" if self._is_cl_like else ""
self.cpp_info.libs = [f"{prefix}jpeg"]
self.cpp_info.resdirs = ["res"]
if not self.options.shared:
self.cpp_info.defines.append("LIBJPEG_STATIC")
# TODO: to remove in conan v2 once legacy generators removed
self.cpp_info.names["cmake_find_package"] = "JPEG"
self.cpp_info.names["cmake_find_package_multi"] = "JPEG"