libx264/cci.20220602

GPL-2.0
License
cci.20220602
Version
11,724
Downloads
Loading...
Loading...
1
from conan import ConanFile
2
from conan.tools.apple import is_apple_os
3
from conan.tools.build import cross_building
4
from conan.tools.files import get, rename, rmdir
5
from conans import tools, AutoToolsBuildEnvironment
6
import contextlib
7
import os
8
9
required_conan_version = ">=1.51.3"
10
11
12
class LibX264Conan(ConanFile):
13
    name = "libx264"
14
    url = "https://github.com/conan-io/conan-center-index"
15
    homepage = "https://www.videolan.org/developers/x264.html"
16
    description = "x264 is a free software library and application for encoding video streams into the " \
17
                  "H.264/MPEG-4 AVC compression format"
18
    topics = ("libx264", "video", "encoding")
19
    license = "GPL-2.0"
20
21
    settings = "os", "arch", "compiler", "build_type"
22
    options = {
23
        "shared": [True, False],
24
        "fPIC": [True, False],
25
        "bit_depth": [8, 10, "all"],
26
    }
27
    default_options = {
28
        "shared": False,
29
        "fPIC": True,
30
        "bit_depth": "all",
31
    }
32
33
    _autotools = None
34
    _override_env = {}
35
36
    @property
37
    def _is_msvc(self):
38
        return str(self.settings.compiler) in ["Visual Studio", "msvc"]
39
40
    @property
41
    def _source_subfolder(self):
42
        return "source_subfolder"
43
44
    @property
45
    def _settings_build(self):
46
        return getattr(self, "settings_build", self.settings)
47
48
    def config_options(self):
49
        if self.settings.os == "Windows":
50
            del self.options.fPIC
51
52
    def configure(self):
53
        if self.options.shared:
54
            del self.options.fPIC
55
        del self.settings.compiler.libcxx
56
        del self.settings.compiler.cppstd
57
58
    @property
59
    def _with_nasm(self):
60
        return self.settings.arch in ("x86", "x86_64")
61
62
    def build_requirements(self):
63
        if self._with_nasm:
64
            self.build_requires("nasm/2.15.05")
65
        if self._settings_build.os == "Windows" and not tools.get_env("CONAN_BASH_PATH"):
66
            self.build_requires("msys2/cci.latest")
67
68
    def source(self):
69
        get(self, **self.conan_data["sources"][self.version],
70
            destination=self._source_subfolder, strip_root=True)
71
72
    @contextlib.contextmanager
73
    def _build_context(self):
74
        with tools.vcvars(self) if self._is_msvc else tools.no_op():
75
            yield
76
77
    @property
78
    def env(self):
79
        ret = super(LibX264Conan, self).env
80
        ret.update(self._override_env)
81
        return ret
82
83
    def _configure_autotools(self):
84
        if self._autotools:
85
            return self._autotools
86
        self._autotools = AutoToolsBuildEnvironment(self, win_bash=tools.os_info.is_windows)
87
        self._autotools.libs = []
88
        extra_asflags = []
89
        extra_cflags = []
90
        extra_ldflags = []
91
        args = [
92
            "--bit-depth=%s" % str(self.options.bit_depth),
93
            "--disable-cli",
94
            "--prefix={}".format(tools.unix_path(self.package_folder)),
95
        ]
96
        if self.options.shared:
97
            args.append("--enable-shared")
98
        else:
99
            args.append("--enable-static")
100
        if self.options.get_safe("fPIC", self.settings.os != "Windows"):
101
            args.append("--enable-pic")
102
        if self.settings.build_type == "Debug":
103
            args.append("--enable-debug")
104
        if is_apple_os(self) and self.settings.arch == "armv8":
105
            # bitstream-a.S:29:18: error: unknown token in expression
106
            extra_asflags.append("-arch arm64")
107
            extra_ldflags.append("-arch arm64")
108
            args.append("--host=aarch64-apple-darwin")
109
            if self.settings.os != "Macos":
110
                deployment_target_flag = tools.apple_deployment_target_flag(
111
                    self.settings.os,
112
                    self.settings.get_safe("os.version"),
113
                    self.settings.get_safe("os.sdk"),
114
                    self.settings.get_safe("os.subsystem"),
115
                    self.settings.get_safe("arch")
116
                )
117
                platform_flags = ["-isysroot", tools.XCRun(self.settings).sdk_path, deployment_target_flag]
118
                extra_asflags.extend(platform_flags)
119
                extra_cflags.extend(platform_flags)
120
                extra_ldflags.extend(platform_flags)
121
122
        if self._with_nasm:
123
            # FIXME: get using user_build_info
124
            self._override_env["AS"] = os.path.join(self.dependencies.build["nasm"].package_folder, "bin", "nasm{}".format(".exe" if tools.os_info.is_windows else "")).replace("\\", "/")
125
        if cross_building(self):
126
            if self.settings.os == "Android":
127
                # the as of ndk does not work well for building libx264
128
                self._override_env["AS"] = os.environ["CC"]
129
                ndk_root = tools.unix_path(os.environ["NDK_ROOT"])
130
                arch = {
131
                    "armv7": "arm",
132
                    "armv8": "aarch64",
133
                    "x86": "i686",
134
                    "x86_64": "x86_64",
135
                }.get(str(self.settings.arch))
136
                abi = "androideabi" if self.settings.arch == "armv7" else "android"
137
                args.append("--cross-prefix={}".format("{}/bin/{}-linux-{}-".format(ndk_root, arch, abi)))
138
        if self._is_msvc:
139
            self._override_env["CC"] = "cl -nologo"
140
            extra_cflags.extend(self._autotools.flags)
141
            if not (self.settings.compiler == "Visual Studio" and tools.Version(self.settings.compiler.version) < "12"):
142
                extra_cflags.append("-FS")
143
        build_canonical_name = None
144
        host_canonical_name = None
145
        if self._is_msvc or self.settings.os in ["iOS", "watchOS", "tvOS"]:
146
            # autotools does not know about the msvc and Apple embedded OS canonical name(s)
147
            build_canonical_name = False
148
            host_canonical_name = False
149
        if extra_asflags:
150
            args.append("--extra-asflags={}".format(" ".join(extra_asflags)))
151
        if extra_cflags:
152
            args.append("--extra-cflags={}".format(" ".join(extra_cflags)))
153
        if extra_ldflags:
154
            args.append("--extra-ldflags={}".format(" ".join(extra_ldflags)))
155
        self._autotools.configure(args=args, vars=self._override_env, configure_dir=self._source_subfolder, build=build_canonical_name, host=host_canonical_name)
156
        return self._autotools
157
158
    def build(self):
159
        with self._build_context():
160
            # relocatable shared lib on macOS
161
            tools.replace_in_file(os.path.join(self._source_subfolder, "configure"),
162
                                  "-install_name \\$(DESTDIR)\\$(libdir)/",
163
                                  "-install_name @rpath/")
164
            autotools = self._configure_autotools()
165
            autotools.make()
166
167
    def package(self):
168
        self.copy(pattern="COPYING", src=self._source_subfolder, dst="licenses")
169
        with self._build_context():
170
            autotools = self._configure_autotools()
171
            autotools.install()
172
        rmdir(self, os.path.join(self.package_folder, "lib", "pkgconfig"))
173
        if self._is_msvc:
174
            ext = ".dll.lib" if self.options.shared else ".lib"
175
            rename(self, os.path.join(self.package_folder, "lib", "libx264{}".format(ext)),
176
                         os.path.join(self.package_folder, "lib", "x264.lib"))
177
178
    def package_info(self):
179
        self.cpp_info.set_property("pkg_config_name", "x264")
180
        self.cpp_info.libs = ["x264"]
181
        if self._is_msvc and self.options.shared:
182
            self.cpp_info.defines.append("X264_API_IMPORTS")
183
        if self.settings.os in ("FreeBSD", "Linux"):
184
            self.cpp_info.system_libs.extend(["dl", "pthread", "m"])
185
        elif self.settings.os == "Android":
186
            self.cpp_info.system_libs.extend(["dl", "m"])
187
188
        # TODO: to remove in conan v2 once pkg_config generator removed
189
        self.cpp_info.names["pkg_config"] = "x264"
190
191