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."""
8 from __future__ import print_function
19 BASE_DIR = os.path.abspath(os.path.dirname(__file__))
21 CHROMIUM_ROOT_DIR = os.path.abspath(os.path.join(BASE_DIR, '..', '..'))
23 CLANG_DIR = os.path.join(CHROMIUM_ROOT_DIR, 'third_party', 'llvm-build', 'Release+Asserts', 'bin')
25 sys.path.append(os.path.join(CHROMIUM_ROOT_DIR, 'build'))
29 MESON = ['meson', 'setup']
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',
44 '-Dcolor-management-lcms=false',
47 '-Ddemo-clients=false',
48 '-Dsimple-clients=egl',
49 '-Dwcap-decode=false',
53 def GetAbsPath(relative_path):
54 return os.path.join(BASE_DIR, relative_path)
57 def PrintAndCheckCall(argv, *args, **kwargs):
58 print('\n-------------------------------------------------\nRunning %s' %
60 c = subprocess.check_call(argv, *args, **kwargs)
63 def RewriteFile(path, search_replace):
66 with open(path, 'w') as f:
67 for search, replace in search_replace:
68 contents = re.sub(search, replace, contents)
70 # Cleanup trailing newlines.
71 f.write(contents.strip() + '\n')
73 def AddAttributeInConfig(path):
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')
80 def CopyConfigsAndCleanup(config_dir, dest_dir):
81 if not os.path.exists(dest_dir):
84 shutil.copy(os.path.join(config_dir, 'config.h'), dest_dir)
85 shutil.rmtree(config_dir)
88 def RewriteGitFile(path, data):
89 with open(path, 'w') as f:
92 # Cleanup trailing newlines.
93 f.write(contents.strip() + '\n')
96 def CopyGitConfigsAndCleanup(config_dir, dest_dir):
97 if not os.path.exists(dest_dir):
100 shutil.copy(os.path.join(config_dir, 'git-version.h'), dest_dir)
101 shutil.rmtree(config_dir)
104 def GenerateGitConfig(config_dir, env, special_args=[]):
105 temp_dir = tempfile.mkdtemp()
107 MESON + DEFAULT_BUILD_ARGS + special_args + [temp_dir],
108 cwd=GetAbsPath('src'),
111 label = subprocess.check_output(["git", "describe", "--always"]).strip()
112 label = label.decode("utf-8")
114 os.path.join(temp_dir, 'git-version.h'),
115 "#define BUILD_ID \"{label}\"".format(label=label))
116 CopyGitConfigsAndCleanup(temp_dir, config_dir)
119 def GenerateConfig(config_dir, env, special_args=[]):
120 temp_dir = tempfile.mkdtemp()
122 MESON + DEFAULT_BUILD_ARGS + special_args + [temp_dir],
123 cwd=GetAbsPath('src'),
126 CopyConfigsAndCleanup(temp_dir, config_dir)
129 def ChangeConfigPath():
130 configfile = GetAbsPath("config/config.h")
134 "LIBWESTON_MODULEDIR",
137 pattern = "#define {dir} \"/[a-zA-Z0-9\\-_/]+\"".format(dir=dir)
138 RewriteFile(configfile, [(pattern, "")])
140 # Add attribute in config.h to suppress all undefined symbol(function) warnings
141 AddAttributeInConfig(configfile)
144 def GenerateWestonVersion():
145 dirname = GetAbsPath("version/libweston")
146 if not os.path.exists(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:
154 if "PACKAGE_VERSION" in line:
155 package_version_list = (line.strip("\n")).split(" ")
156 version_number = package_version_list[-1]
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:
165 for version, version_number in zip(VERSIONS, version_number_list):
167 repl_string = version_number
168 with open(version_op_file, 'w') as f:
169 contents = re.sub(pattern, repl_string, contents)
171 # Cleanup trailing newlines.
172 f.write(contents.strip() + '\n')
173 print("Created version.h file from version.h.in\n")
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 .*", "")])
189 env['PATH'] = ':'.join([CLANG_DIR, env['PATH']])
190 GenerateGitConfig(GetAbsPath('version'), env)
191 GenerateConfig(GetAbsPath('config'), env)
193 RemoveUndesiredDefines()
194 GenerateWestonVersion()
196 if __name__ == '__main__':