Fix for Geolocation webTCT failures
[platform/framework/web/chromium-efl.git] / third_party / weston / generate_configs.py
1 #!/usr/bin/env python
2 #
3 # Copyright 2020 The Chromium Authors
4 # Use of this source code is governed by a BSD-style license that can be
5 # found in the LICENSE file.
6 """Creates config files for building Weston."""
7
8 from __future__ import print_function
9
10 import os
11 import pathlib
12 import re
13 import shutil
14 import subprocess
15 import sys
16 import tempfile
17
18
19 BASE_DIR = os.path.abspath(os.path.dirname(__file__))
20
21 CHROMIUM_ROOT_DIR = os.path.abspath(os.path.join(BASE_DIR, '..', '..'))
22
23 CLANG_DIR = os.path.join(CHROMIUM_ROOT_DIR, 'third_party', 'llvm-build', 'Release+Asserts', 'bin')
24
25 sys.path.append(os.path.join(CHROMIUM_ROOT_DIR, 'build'))
26
27 import gn_helpers
28
29 MESON = ['meson', 'setup']
30
31 DEFAULT_BUILD_ARGS = [
32     '--buildtype', 'release',
33     '-Dbackend-drm=false',
34     '-Dbackend-drm-screencast-vaapi=false',
35     '-Dbackend-pipewire=false',
36     '-Dbackend-rdp=false',
37     '-Dscreenshare=false',
38     '-Dbackend-vnc=false',
39     '-Dbackend-default=auto',
40     '-Dxwayland=false',
41     '-Dremoting=false',
42     '-Dpipewire=false',
43     '-Dshell-ivi=false',
44     '-Dcolor-management-lcms=false',
45     '-Dimage-jpeg=false',
46     '-Dimage-webp=false',
47     '-Ddemo-clients=false',
48     '-Dsimple-clients=egl',
49     '-Dwcap-decode=false',
50 ]
51
52
53 def GetAbsPath(relative_path):
54     return os.path.join(BASE_DIR, relative_path)
55
56
57 def PrintAndCheckCall(argv, *args, **kwargs):
58     print('\n-------------------------------------------------\nRunning %s' %
59           ' '.join(argv))
60     c = subprocess.check_call(argv, *args, **kwargs)
61
62
63 def RewriteFile(path, search_replace):
64     with open(path) as f:
65         contents = f.read()
66     with open(path, 'w') as f:
67         for search, replace in search_replace:
68             contents = re.sub(search, replace, contents)
69
70         # Cleanup trailing newlines.
71         f.write(contents.strip() + '\n')
72
73 def AddAttributeInConfig(path):
74     with open(path) as f:
75         contents = f.read()
76     with open(path, 'w') as f:
77         f.write(contents.strip() + '\n')
78         f.write('\n' + '__attribute__((visibility("default"))) int main(int argc, char* argv[]);' + '\n')
79
80 def CopyConfigsAndCleanup(config_dir, dest_dir):
81     if not os.path.exists(dest_dir):
82         os.makedirs(dest_dir)
83
84     shutil.copy(os.path.join(config_dir, 'config.h'), dest_dir)
85     shutil.rmtree(config_dir)
86
87
88 def RewriteGitFile(path, data):
89     with open(path, 'w') as f:
90         contents = data
91
92         # Cleanup trailing newlines.
93         f.write(contents.strip() + '\n')
94
95
96 def CopyGitConfigsAndCleanup(config_dir, dest_dir):
97     if not os.path.exists(dest_dir):
98         os.makedirs(dest_dir)
99
100     shutil.copy(os.path.join(config_dir, 'git-version.h'), dest_dir)
101     shutil.rmtree(config_dir)
102
103
104 def GenerateGitConfig(config_dir, env, special_args=[]):
105     temp_dir = tempfile.mkdtemp()
106     PrintAndCheckCall(
107         MESON + DEFAULT_BUILD_ARGS + special_args + [temp_dir],
108         cwd=GetAbsPath('src'),
109         env=env)
110
111     label = subprocess.check_output(["git", "describe", "--always"]).strip()
112     label = label.decode("utf-8")
113     RewriteGitFile(
114         os.path.join(temp_dir, 'git-version.h'),
115         "#define BUILD_ID \"{label}\"".format(label=label))
116     CopyGitConfigsAndCleanup(temp_dir, config_dir)
117
118
119 def GenerateConfig(config_dir, env, special_args=[]):
120     temp_dir = tempfile.mkdtemp()
121     PrintAndCheckCall(
122         MESON + DEFAULT_BUILD_ARGS + special_args + [temp_dir],
123         cwd=GetAbsPath('src'),
124         env=env)
125
126     CopyConfigsAndCleanup(temp_dir, config_dir)
127
128
129 def ChangeConfigPath():
130     configfile = GetAbsPath("config/config.h")
131     DIRS = ["BINDIR",
132             "DATADIR",
133             "LIBEXECDIR",
134             "LIBWESTON_MODULEDIR",
135             "MODULEDIR"]
136     for dir in DIRS:
137         pattern = "#define {dir} \"/[a-zA-Z0-9\\-_/]+\"".format(dir=dir)
138         RewriteFile(configfile, [(pattern, "")])
139
140     # Add attribute in config.h to suppress all undefined symbol(function) warnings
141     AddAttributeInConfig(configfile)
142
143
144 def GenerateWestonVersion():
145     dirname = GetAbsPath("version/libweston")
146     if not os.path.exists(dirname):
147         os.makedirs(dirname)
148     version_op_file = GetAbsPath("version/libweston/version.h")
149     configfile = GetAbsPath("config/config.h")
150     version_in_file = GetAbsPath("src/include/libweston/version.h.in")
151     version_number = "0.0.0"
152     with open(configfile, 'r') as f:
153         for line in f:
154             if "PACKAGE_VERSION" in line:
155                 package_version_list = (line.strip("\n")).split(" ")
156                 version_number = package_version_list[-1]
157
158     version_number_list = (version_number.strip('"\n"')).split(".")
159     version_number_list.append(version_number.strip("\"\""))
160     VERSIONS = ["@WESTON_VERSION_MAJOR@", "@WESTON_VERSION_MINOR@",
161                 "@WESTON_VERSION_MICRO@", "@WESTON_VERSION@"]
162     with open(version_in_file) as f:
163         contents = f.read()
164
165     for version, version_number in zip(VERSIONS, version_number_list):
166         pattern = version
167         repl_string = version_number
168         with open(version_op_file, 'w') as f:
169             contents = re.sub(pattern, repl_string, contents)
170
171             # Cleanup trailing newlines.
172             f.write(contents.strip() + '\n')
173     print("Created version.h file from version.h.in\n")
174
175
176 def RemoveUndesiredDefines():
177     configfile = GetAbsPath('config/config.h')
178     # Weston doesn't have a meson option to avoid using memfd_create() method that was
179     # introduced in GLIBC 2.27. That results in weston failing to run on Xenial based bot as
180     # it has GLIBC 2.23, because this config might be generated on a system that has newer
181     # libc libraries that meson checks with has_function() method. Thus, explicitly rewrite
182     # the config to disable usage of that method.
183     RewriteFile(configfile, [("#define HAVE_MEMFD_CREATE .*", "")])
184
185
186 def main():
187     env = os.environ
188     env['CC'] = 'clang'
189     env['PATH'] = ':'.join([CLANG_DIR, env['PATH']])
190     GenerateGitConfig(GetAbsPath('version'), env)
191     GenerateConfig(GetAbsPath('config'), env)
192     ChangeConfigPath()
193     RemoveUndesiredDefines()
194     GenerateWestonVersion()
195
196 if __name__ == '__main__':
197     main()