2017-05-18 10:41:47 +02:00
|
|
|
# Copyright (c) 2017 The Chromium Embedded Framework Authors. All rights
|
|
|
|
# reserved. Use of this source code is governed by a BSD-style license that
|
|
|
|
# can be found in the LICENSE file
|
|
|
|
|
2020-01-09 21:22:11 +01:00
|
|
|
from __future__ import absolute_import
|
|
|
|
from __future__ import print_function
|
2017-05-18 10:41:47 +02:00
|
|
|
from exec_util import exec_cmd
|
2017-05-19 11:00:44 +02:00
|
|
|
import os
|
2017-05-18 10:41:47 +02:00
|
|
|
import sys
|
|
|
|
|
2017-05-19 11:00:44 +02:00
|
|
|
# Script directory.
|
|
|
|
script_dir = os.path.dirname(__file__)
|
|
|
|
root_dir = os.path.join(script_dir, os.pardir)
|
|
|
|
|
2017-05-18 10:41:47 +02:00
|
|
|
if sys.platform == 'win32':
|
|
|
|
# Force use of the clang-format version bundled with depot_tools.
|
|
|
|
clang_format_exe = 'clang-format.bat'
|
|
|
|
else:
|
|
|
|
clang_format_exe = 'clang-format'
|
|
|
|
|
2017-05-28 15:04:18 +02:00
|
|
|
|
2017-05-19 11:00:44 +02:00
|
|
|
def clang_format(file_name, file_contents):
|
|
|
|
# -assume-filename is necessary to find the .clang-format file and determine
|
|
|
|
# the language when specifying contents via stdin.
|
|
|
|
result = exec_cmd("%s -assume-filename=%s" % (clang_format_exe, file_name), \
|
2020-01-09 21:22:11 +01:00
|
|
|
root_dir, file_contents.encode('utf-8'))
|
2017-05-19 11:00:44 +02:00
|
|
|
if result['err'] != '':
|
2020-01-09 21:22:11 +01:00
|
|
|
print("clang-format error: %s" % result['err'])
|
2017-05-18 10:41:47 +02:00
|
|
|
if result['out'] != '':
|
|
|
|
output = result['out']
|
|
|
|
if sys.platform == 'win32':
|
|
|
|
# Convert to Unix line endings.
|
|
|
|
output = output.replace("\r", "")
|
|
|
|
return output
|
|
|
|
return None
|