uninstalled: Symbolic link support for the script file
[platform/upstream/gstreamer.git] / msys2_setup.py
1 #!/usr/bin/env python3
2 """Setup meson based GStreamer uninstalled environment based on msys2."""
3
4 import argparse
5 import itertools
6 import os
7 import re
8 import sys
9 import shlex
10 import shutil
11 import subprocess
12 import tempfile
13
14 from common import git
15 from setup import GstBuildConfigurer
16
17
18 PROJECTNAME = "GStreamer build"
19
20 ROOTDIR = os.path.abspath(os.path.dirname(__file__))
21
22
23 class Msys2Configurer(GstBuildConfigurer):
24     MESON_GIT = 'https://github.com/mesonbuild/meson.git'
25     DEPENDENCIES = ['git',
26                     'bison',
27                     'mingw-w64-x86_64-pkg-config',
28                     'mingw-w64-x86_64-ninja',
29                     'mingw-w64-x86_64-libxml2',
30                     'mingw-w64-x86_64-ffmpeg',
31                     'mingw-w64-x86_64-python3',
32                     'mingw-w64-x86_64-json-glib']
33     LIBNAME_EXCEPTIONS = {
34         r'^zlib1.lib$': 'z.lib',
35         r'^nettle-.*': 'nettle.lib',
36         r'^hogweed-.*': 'hogweed.lib',
37         # Fancy, but it seems to be the correct way to do it
38         r'^eay32.lib$': 'crypto.lib',
39         r'^ssleay32.lib$': 'ssl.lib',
40     }
41
42     def get_libname(self, dll_name):
43         lib_name = re.sub(r'(?:lib)?(.*?)(?:-\d+)?\.dll', r'\1.lib', dll_name)
44
45         for exception_name, exception_libname in self.LIBNAME_EXCEPTIONS.items():
46             if re.findall(exception_name, lib_name):
47                 return exception_libname
48         return lib_name
49
50     def make_lib(self, lib, dll, dll_name):
51         print('%s... ' % os.path.basename(lib), end='', flush=True)
52         try:
53             os.remove(lib)
54         except FileNotFoundError:
55             pass
56
57         dumpbin = subprocess.check_output(['dumpbin', '/exports', dll])
58         lines = dumpbin.decode().splitlines()
59         export_start = [i for i in enumerate(
60             lines) if i[1].find('ordinal hint') != -1][0][0] + 2
61         exports = itertools.takewhile(lambda x: x != '', lines[export_start:])
62         exports = [i.split() for i in exports]
63         def_file = tempfile.NamedTemporaryFile(
64             suffix='.def', delete=False, mode='w')
65         def_file.write('LIBRARY ' + dll_name + '\r\n')
66         def_file.write('EXPORTS\r\n')
67         for ordinal, _, _, name in exports:
68             def_file.write(name + ' @' + ordinal + '\r\n')
69         def_file.close()
70         subprocess.check_output(['lib', '/def:' + def_file.name,
71                                  '/out:' + lib])
72         os.remove(def_file.name)
73
74     def make_lib_if_needed(self, dll):
75         if not dll.endswith('.dll'):
76             return
77
78         lib_dir, dll_name = os.path.split(dll)
79         if lib_dir.endswith('bin'):
80             lib_dir = lib_dir[:-3] + 'lib'
81
82         lib_name = self.get_libname(dll_name)
83         lib = os.path.join(lib_dir, lib_name)
84         if os.path.exists(lib) and os.stat(dll).st_mtime_ns < os.stat(lib).st_mtime_ns:
85             return
86
87         print('Generating .lib file for %s ...' % os.path.basename(dll), end='', flush=True)
88         self.make_lib(lib, dll, dll_name)
89         print('DONE', flush=True)
90
91     def make_libs(self):
92         base = os.path.join(self.options.msys2_path, 'mingw64', 'bin')
93         for f in os.listdir(base):
94             if f.endswith('.dll'):
95                 self.make_lib_if_needed(os.path.join(base, f))
96
97     def get_configs(self):
98         return GstBuildConfigurer.get_configs(self) + [
99             '-D' + m + ':disable_introspection=true' for m in [
100                 'gst-devtools', 'gstreamer', 'gst-plugins-base',
101                 'gst-editing-services']]
102
103     def setup(self, args):
104         if not os.path.exists(self.options.msys2_path):
105             print("msys2 not found in %s. Please make sure to install"
106                   " (from http://msys2.github.io/) specify --msys2-path"
107                   " if you did not install in the default directory.", flush=True)
108             return False
109
110         for path in ['mingw64/bin', 'bin', 'usr/bin']:
111             os.environ['PATH'] = os.environ.get(
112                 'PATH', '') + os.pathsep + os.path.normpath(os.path.join(self.options.msys2_path, path))
113         os.environ['PATH'] = os.environ['PATH'].replace(';;', ';')
114         os.environ['PKG_CONFIG_PATH'] = os.environ.get(
115             'PKG_CONFIG_PATH', '') + ':/mingw64/lib/pkgconfig:/mingw64/share/pkgconfig'
116
117         subprocess.check_call(['pacman', '-S', '--needed', '--noconfirm'] + self.DEPENDENCIES)
118         source_path = os.path.abspath(os.path.curdir)
119
120         print('Making sure meson is present in root folder... ', end='', flush=True)
121         if not os.path.isdir(os.path.join(source_path, 'meson')):
122             print('\nCloning meson', flush=True)
123             git('clone', self.MESON_GIT, repository_path=source_path)
124         else:
125             print('\nDONE', flush=True)
126
127         print("Making libs", flush=True)
128         self.make_libs()
129         print("Done making .lib files.", flush=True)
130         if not os.path.exists(os.path.join(source_path, 'build', 'build.ninja')) or \
131                 self.options.reconfigure:
132             print("Running meson", flush=True)
133             if not self.configure_meson():
134                 return False
135
136         try:
137             if not args:
138                 print("Getting into msys2 environment", flush=True)
139                 subprocess.check_call([sys.executable,
140                                 os.path.join(source_path, 'gst-uninstalled.py'),
141                                 '--builddir', os.path.join(source_path, 'build')])
142             else:
143                 print("Running %s" ' '.join(args), flush=True)
144                 res = subprocess.check_call(args)
145         except subprocess.CalledProcessError as e:
146             return False
147
148         return True
149
150
151 if __name__ == "__main__":
152     parser = argparse.ArgumentParser(description='Process some integers.')
153     parser.add_argument("--no-error", action='store_true',
154                         default=False, help="Do not error out on warnings")
155     parser.add_argument("--reconfigure", action='store_true',
156                         default=False, help='Force a full reconfiguration'
157                         ' meaning the build/ folder is removed.'
158                         ' You can also use `ninja reconfigure` to just'
159                         ' make sure meson is rerun but the build folder'
160                         ' is kept.')
161     if os.name != 'nt':
162         print("Using this script outside windows does not make sense.", flush=True)
163         exit(1)
164
165     parser.add_argument("-m", "--msys2-path", dest="msys2_path",
166                         help="Where to find msys2 root directory."
167                         "(deactivates msys if unset)",
168                         default="C:\msys64")
169
170     parser.add_argument("-c", "--command", dest="command",
171                         help="Command to run instead of entering environment.",
172                         default="")
173     options, args = parser.parse_known_args()
174
175     if not shutil.which('cl'):
176         print("Can not find MSVC on windows,"
177                 " make sure you are in a 'Visual Studio"
178                 " Native Tools Command Prompt'", flush=True)
179         exit(1)
180
181     configurer = Msys2Configurer(options, args)
182
183     exit(not configurer.setup(shlex.split(options.command)))