libx264/20191217

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